···590590 return r;591591}592592EXPORT_SYMBOL_GPL(dm_btree_remove);593593+594594+/*----------------------------------------------------------------*/595595+596596+static int remove_nearest(struct shadow_spine *s, struct dm_btree_info *info,597597+ struct dm_btree_value_type *vt, dm_block_t root,598598+ uint64_t key, int *index)599599+{600600+ int i = *index, r;601601+ struct btree_node *n;602602+603603+ for (;;) {604604+ r = shadow_step(s, root, vt);605605+ if (r < 0)606606+ break;607607+608608+ /*609609+ * We have to patch up the parent node, ugly, but I don't610610+ * see a way to do this automatically as part of the spine611611+ * op.612612+ */613613+ if (shadow_has_parent(s)) {614614+ __le64 location = cpu_to_le64(dm_block_location(shadow_current(s)));615615+ memcpy(value_ptr(dm_block_data(shadow_parent(s)), i),616616+ &location, sizeof(__le64));617617+ }618618+619619+ n = dm_block_data(shadow_current(s));620620+621621+ if (le32_to_cpu(n->header.flags) & LEAF_NODE) {622622+ *index = lower_bound(n, key);623623+ return 0;624624+ }625625+626626+ r = rebalance_children(s, info, vt, key);627627+ if (r)628628+ break;629629+630630+ n = dm_block_data(shadow_current(s));631631+ if (le32_to_cpu(n->header.flags) & LEAF_NODE) {632632+ *index = lower_bound(n, key);633633+ return 0;634634+ }635635+636636+ i = lower_bound(n, key);637637+638638+ /*639639+ * We know the key is present, or else640640+ * rebalance_children would have returned641641+ * -ENODATA642642+ */643643+ root = value64(n, i);644644+ }645645+646646+ return r;647647+}648648+649649+static int remove_one(struct dm_btree_info *info, dm_block_t root,650650+ uint64_t *keys, uint64_t end_key,651651+ dm_block_t *new_root, unsigned *nr_removed)652652+{653653+ unsigned level, last_level = info->levels - 1;654654+ int index = 0, r = 0;655655+ struct shadow_spine spine;656656+ struct btree_node *n;657657+ uint64_t k;658658+659659+ init_shadow_spine(&spine, info);660660+ for (level = 0; level < last_level; level++) {661661+ r = remove_raw(&spine, info, &le64_type,662662+ root, keys[level], (unsigned *) &index);663663+ if (r < 0)664664+ goto out;665665+666666+ n = dm_block_data(shadow_current(&spine));667667+ root = value64(n, index);668668+ }669669+670670+ r = remove_nearest(&spine, info, &info->value_type,671671+ root, keys[last_level], &index);672672+ if (r < 0)673673+ goto out;674674+675675+ n = dm_block_data(shadow_current(&spine));676676+677677+ if (index < 0)678678+ index = 0;679679+680680+ if (index >= le32_to_cpu(n->header.nr_entries)) {681681+ r = -ENODATA;682682+ goto out;683683+ }684684+685685+ k = le64_to_cpu(n->keys[index]);686686+ if (k >= keys[last_level] && k < end_key) {687687+ if (info->value_type.dec)688688+ info->value_type.dec(info->value_type.context,689689+ value_ptr(n, index));690690+691691+ delete_at(n, index);692692+693693+ } else694694+ r = -ENODATA;695695+696696+out:697697+ *new_root = shadow_root(&spine);698698+ exit_shadow_spine(&spine);699699+700700+ return r;701701+}702702+703703+int dm_btree_remove_leaves(struct dm_btree_info *info, dm_block_t root,704704+ uint64_t *first_key, uint64_t end_key,705705+ dm_block_t *new_root, unsigned *nr_removed)706706+{707707+ int r;708708+709709+ *nr_removed = 0;710710+ do {711711+ r = remove_one(info, root, first_key, end_key, &root, nr_removed);712712+ if (!r)713713+ (*nr_removed)++;714714+ } while (!r);715715+716716+ *new_root = root;717717+ return r == -ENODATA ? 0 : r;718718+}719719+EXPORT_SYMBOL_GPL(dm_btree_remove_leaves);
+9
drivers/md/persistent-data/dm-btree.h
···135135 uint64_t *keys, dm_block_t *new_root);136136137137/*138138+ * Removes values between 'keys' and keys2, where keys2 is keys with the139139+ * final key replaced with 'end_key'. 'end_key' is the one-past-the-end140140+ * value. 'keys' may be altered.141141+ */142142+int dm_btree_remove_leaves(struct dm_btree_info *info, dm_block_t root,143143+ uint64_t *keys, uint64_t end_key,144144+ dm_block_t *new_root, unsigned *nr_removed);145145+146146+/*138147 * Returns < 0 on failure. Otherwise the number of key entries that have139148 * been filled out. Remember trees can have zero entries, and as such have140149 * no lowest key.