btree remove tweaks
cover btree remove tweaks
Here's a set of patches that do some tidying in the btree remove code and fix an issue that caused under populated nodes.
Tested on my system, more testing required before pushing upstream.
Joe Thornber (5):
[dm-thin] Remove redundant arg from value_ptr()
[dm-thin] Remove 2 BUG_ONs from btree_remove code. [dm-thin] [btree-remove] break up __rebalance3 function. [dm-thin] [btree-remove] Fix bug that allowed the nr of entries in a
btree node to drop below 1/3.
[dm-thin] [space-map-common] Remove entries from the ref_count tree
if they're no longer needed.
drivers/md/persistent-data/dm-btree-internal.h | 7 +- drivers/md/persistent-data/dm-btree-remove.c | 157 ++++++++++----------- drivers/md/persistent-data/dm-btree.c | 27 ++-- drivers/md/persistent-data/dm-space-map-common.c | 3 - 4 files changed, 90 insertions(+), 104 deletions(-)
Signature · 1 line
1.7.5.4
PATCH 1/5 Remove redundant arg from value_ptr()
Now that the value_size is held within every node of the btrees we can remove this argument from value_ptr().
For the last few months a BUG_ON has been checking this argument is the same as that held in the node. No issues were reported. So this is a safe change.
--- drivers/md/persistent-data/dm-btree-internal.h | 7 +---- drivers/md/persistent-data/dm-btree-remove.c | 28 ++++++++++++------------ drivers/md/persistent-data/dm-btree.c | 27 +++++++++++------------ 3 files changed, 29 insertions(+), 33 deletions(-)
drivers/md/persistent-data/dm-btree-internal.h +2 -5
@@ -108,12 +108,9 @@ static inline void *value_base(struct node *n)return &n->keys[le32_to_cpu (n->header.max_entries)]; } -/* -* FIXME: Now that value size is stored in node we don't need the third parm. -*/ -static inline void *value_ptr (struct node *n,uint32_t index,size_t value_size) +static inline void *value_ptr (struct node *n,uint32_t index) { -BUG_ON (value_size !=le32_to_cpu (n->header.value_size)); +uint32_t value_size =le32_to_cpu (n->header.value_size);return value_base (n) + (value_size * index); }
drivers/md/persistent-data/dm-btree-remove.c +14 -14
@@ -61,20 +61,20 @@ static void node_shift(struct node *n, int shift)if (shift <0 ) { shift = -shift;BUG_ON (shift > nr_entries); -BUG_ON ((void *)key_ptr (n, shift) >=value_ptr (n, shift, value_size)); +BUG_ON ((void *)key_ptr (n, shift) >=value_ptr (n, shift));memmove (key_ptr (n,0 ),key_ptr (n, shift), (nr_entries - shift) *sizeof (__le64)); -memmove (value_ptr (n,0 , value_size), -value_ptr (n, shift, value_size), +memmove (value_ptr (n,0 ), +value_ptr (n, shift), (nr_entries - shift) * value_size); }else {BUG_ON (nr_entries + shift >le32_to_cpu (n->header.max_entries));memmove (key_ptr (n, shift),key_ptr (n,0 ), nr_entries *sizeof (__le64)); -memmove (value_ptr (n, shift, value_size), -value_ptr (n,0 , value_size), +memmove (value_ptr (n, shift), +value_ptr (n,0 ), nr_entries * value_size); } }
@@ -91,16 +91,16 @@ static void node_copy(struct node *left, struct node *right, int shift)memcpy (key_ptr (left, nr_left),key_ptr (right,0 ), shift *sizeof (__le64)); -memcpy (value_ptr (left, nr_left, value_size), -value_ptr (right,0 , value_size), +memcpy (value_ptr (left, nr_left), +value_ptr (right,0 ), shift * value_size); }else {BUG_ON (shift >le32_to_cpu (right->header.max_entries));memcpy (key_ptr (right,0 ),key_ptr (left, nr_left - shift), shift *sizeof (__le64)); -memcpy (value_ptr (right,0 , value_size), -value_ptr (left, nr_left - shift, value_size), +memcpy (value_ptr (right,0 ), +value_ptr (left, nr_left - shift), shift * value_size); } }
@@ -120,8 +120,8 @@ static void delete_at(struct node *n, unsigned index)key_ptr (n, index +1 ), nr_to_copy * sizeof(__le64)); -memmove (value_ptr (n, index, value_size), -value_ptr (n, index +1 , value_size), +memmove (value_ptr (n, index), +value_ptr (n, index +1 ), nr_to_copy * value_size); }
@@ -175,7 +175,7 @@ static int init_child(struct dm_btree_info *info, struct node *parent,if (inc)inc_children (info->tm, result->n, &le64_type); - *((__le64 *)value_ptr (parent, index,sizeof (__le64))) = + *((__le64 *)value_ptr (parent, index)) =cpu_to_le64 (dm_block_location (result->block));return 0 ;
@@ -496,7 +496,7 @@ static int remove_raw(struct shadow_spine *s, struct dm_btree_info *info,*/ if (shadow_has_parent (s)) {__le64 location =cpu_to_le64 (dm_block_location (shadow_current (s))); -memcpy (value_ptr (dm_block_data (shadow_parent (s)), i,sizeof (__le64)), +memcpy (value_ptr (dm_block_data (shadow_parent (s)), i), &location,sizeof (__le64)); }
@@ -553,7 +553,7 @@ int dm_btree_remove(struct dm_btree_info *info, dm_block_t root,if (info->value_type.dec) info->value_type.dec (info->value_type.context, -value_ptr (n, index, info->value_type.size)); +value_ptr (n, index));delete_at (n, index); }
drivers/md/persistent-data/dm-btree.c +13 -14
@@ -74,8 +74,7 @@ void inc_children(struct dm_transaction_manager *tm, struct node *n,dm_tm_inc (tm,value64 (n, i)); else if (vt->inc) for (i = 0; i < nr_entries; i++) - vt->inc (vt->context, -value_ptr (n, i, vt->size)); + vt->inc (vt->context,value_ptr (n, i)); } static int insert_at(size_t value_size, struct node *node, unsigned index,
@@ -281,7 +280,7 @@ int dm_btree_del(struct dm_btree_info *info, dm_block_t root)for (i =0 ; i < f->nr_children; i++) info->value_type.dec (info->value_type.context, -value_ptr (f->n, i, info->value_type.size)); +value_ptr (f->n, i)); } f->current_child = f->nr_children; }
@@ -320,7 +319,7 @@ static int btree_lookup_raw(struct ro_spine *s, dm_block_t block, uint64_t key, }while (!(flags &LEAF_NODE )); *result_key =le64_to_cpu (ro_node (s)->keys[i]); -memcpy (v,value_ptr (ro_node (s), i, value_size), value_size); +memcpy (v,value_ptr (ro_node (s), i), value_size);return 0 ; }
@@ -432,7 +431,7 @@ static int btree_split_sibling(struct shadow_spine *s, dm_block_t root, size =le32_to_cpu (ln->header.flags) &INTERNAL_NODE ?sizeof (uint64_t ) : s->info->value_type.size; -memcpy (value_ptr (rn,0 , size),value_ptr (ln, nr_left, size), +memcpy (value_ptr (rn,0 ),value_ptr (ln, nr_left), size * nr_right);/*
@@ -443,7 +442,7 @@ static int btree_split_sibling(struct shadow_spine *s, dm_block_t root, pn =dm_block_data (parent); location =cpu_to_le64 (dm_block_location (left));__dm_bless_for_disk (&location); -memcpy_disk (value_ptr (pn, parent_index,sizeof (__le64)), +memcpy_disk (value_ptr (pn, parent_index), &location,sizeof (__le64)); location =cpu_to_le64 (dm_block_location (right));
@@ -529,8 +528,8 @@ static int btree_split_beneath(struct shadow_spine *s, uint64_t key) size =le32_to_cpu (pn->header.flags) &INTERNAL_NODE ?sizeof (__le64) : s->info->value_type.size; -memcpy (value_ptr (ln,0 , size),value_ptr (pn,0 , size), nr_left * size); -memcpy (value_ptr (rn,0 , size),value_ptr (pn, nr_left, size), +memcpy (value_ptr (ln,0 ),value_ptr (pn,0 ), nr_left * size); +memcpy (value_ptr (rn,0 ),value_ptr (pn, nr_left), nr_right * size);/* new_parent should just point to l and r now */
@@ -545,12 +544,12 @@ static int btree_split_beneath(struct shadow_spine *s, uint64_t key) val =cpu_to_le64 (dm_block_location (left));__dm_bless_for_disk (&val); pn->keys[0 ] = ln->keys[0 ]; -memcpy_disk (value_ptr (pn,0 ,sizeof (__le64)), &val,sizeof (__le64)); +memcpy_disk (value_ptr (pn,0 ), &val,sizeof (__le64)); val =cpu_to_le64 (dm_block_location (right));__dm_bless_for_disk (&val); pn->keys[1 ] = rn->keys[0 ]; -memcpy_disk (value_ptr (pn,1 ,sizeof (__le64)), &val,sizeof (__le64)); +memcpy_disk (value_ptr (pn,1 ), &val,sizeof (__le64));/* * rejig the spine. This is ugly, since it knows too
@@ -595,7 +594,7 @@ static int btree_insert_raw(struct shadow_spine *s, dm_block_t root,__le64 location =cpu_to_le64 (dm_block_location (shadow_current (s)));__dm_bless_for_disk (&location); -memcpy_disk (value_ptr (dm_block_data (shadow_parent (s)), i,sizeof (uint64_t )), +memcpy_disk (value_ptr (dm_block_data (shadow_parent (s)), i), &location,sizeof (__le64)); }
@@ -710,12 +709,12 @@ static int insert(struct dm_btree_info *info, dm_block_t root, (!info->value_type.equal || !info->value_type.equal ( info->value_type.context, -value_ptr (n, index, info->value_type.size), +value_ptr (n, index), value))) { info->value_type.dec(info->value_type.context, -value_ptr (n, index, info->value_type.size)); +value_ptr (n, index)); } -memcpy_disk (value_ptr (n, index, info->value_type.size), +memcpy_disk (value_ptr (n, index), value, info->value_type.size); }
Signature · 1 line
1.7.5.4
PATCH 2/5 Remove 2 BUG_ONs from btree_remove code.
In reply to Joe ThornberThese paranoia checks are no longer needed.
--- drivers/md/persistent-data/dm-btree-remove.c | 3 --- 1 files changed, 0 insertions(+), 3 deletions(-)
drivers/md/persistent-data/dm-btree-remove.c +0 -3
@@ -234,9 +234,6 @@ static void __rebalance2(struct dm_btree_info *info, struct node *parent,* Rebalance. */ unsigned target_left = (nr_left + nr_right) /2 ; -unsigned shift_ = nr_left - target_left; -BUG_ON (le32_to_cpu (left->header.max_entries) <= nr_left - shift_); -BUG_ON (le32_to_cpu (right->header.max_entries) <= nr_right + shift_);shift (left, right, nr_left - target_left); *key_ptr (parent, r->index) = right->keys[0 ]; }
Signature · 1 line
1.7.5.4
PATCH 3/5 break up __rebalance3 function.
In reply to Joe ThornberThis function was getting too big. Two functions are factored out delete_center_node() and redistribute3(). No functional changes.
--- drivers/md/persistent-data/dm-btree-remove.c | 109 +++++++++++++------------ 1 files changed, 57 insertions(+), 52 deletions(-)
drivers/md/persistent-data/dm-btree-remove.c +57 -52
@@ -269,6 +269,57 @@ static int rebalance2(struct shadow_spine *s, struct dm_btree_info *info,return exit_child (info, &right); } +/* +* We dump as many entries from center as possible into left, then the rest +* in right, then rebalance2. This wastes some cpu, but I want something +* simple atm. +*/ +static void delete_center_node (struct dm_btree_info *info,struct node *parent, +struct child *l,struct child *c,struct child *r, +struct node *left,struct node *center,struct node *right, +uint32_t nr_left,uint32_t nr_center,uint32_t nr_right) +{ +uint32_t max_entries =le32_to_cpu (left->header.max_entries); +unsigned shift =min (max_entries - nr_left, nr_center); + +BUG_ON (nr_left + shift > max_entries); +node_copy (left, center, -shift); + left->header.nr_entries =cpu_to_le32 (nr_left + shift); + +if (shift != nr_center) { + shift = nr_center - shift; +BUG_ON ((nr_right + shift) >= max_entries); +node_shift (right, shift); +node_copy (center, right, shift); + right->header.nr_entries =cpu_to_le32 (nr_right + shift); + } + *key_ptr (parent, r->index) = right->keys[0 ]; + +delete_at (parent, c->index); + r->index--; + +dm_tm_dec (info->tm,dm_block_location (c->block)); +__rebalance2 (info, parent, l, r); +} + +/* +* Redistributes entries among 3 sibling nodes. +*/ +static void redistribute3 (struct dm_btree_info *info,struct node *parent, +struct child *l,struct child *c,struct child *r, +struct node *left,struct node *center,struct node *right, +uint32_t nr_left,uint32_t nr_center,uint32_t nr_right) +{ +uint32_t max_entries =le32_to_cpu (left->header.max_entries); +unsigned target = (nr_left + nr_center + nr_right) /3 ; +BUG_ON (target > max_entries); + +shift (left, center, nr_left - target); +shift (center, right, target - nr_right); + *key_ptr (parent, c->index) = center->keys[0 ]; + *key_ptr (parent, r->index) = right->keys[0 ]; +} +static void __rebalance3 (struct dm_btree_info *info,struct node *parent,struct child *l,struct child *c,struct child *r) {
@@ -279,62 +330,16 @@ static void __rebalance3(struct dm_btree_info *info, struct node *parent,uint32_t nr_left =le32_to_cpu (left->header.nr_entries);uint32_t nr_center =le32_to_cpu (center->header.nr_entries);uint32_t nr_right =le32_to_cpu (right->header.nr_entries); -uint32_t max_entries =le32_to_cpu (left->header.max_entries); - -unsigned target;BUG_ON (left->header.max_entries != center->header.max_entries);BUG_ON (center->header.max_entries != right->header.max_entries); -if (((nr_left + nr_center + nr_right) /2 ) <merge_threshold (center)) { -/* -* Delete center node: -* -* We dump as many entries from center as possible into -* left, then the rest in right, then rebalance2. This -* wastes some cpu, but I want something simple atm. -*/ -unsigned shift =min (max_entries - nr_left, nr_center); - -BUG_ON (nr_left + shift > max_entries); -node_copy (left, center, -shift); - left->header.nr_entries =cpu_to_le32 (nr_left + shift); - -if (shift != nr_center) { - shift = nr_center - shift; -BUG_ON ((nr_right + shift) >= max_entries); -node_shift (right, shift); -node_copy (center, right, shift); - right->header.nr_entries =cpu_to_le32 (nr_right + shift); - } - *key_ptr (parent, r->index) = right->keys[0 ]; - -delete_at (parent, c->index); - r->index--; - -dm_tm_dec (info->tm,dm_block_location (c->block)); -__rebalance2 (info, parent, l, r); - -return ; - } - -/* -* Rebalance -*/ - target = (nr_left + nr_center + nr_right) /3 ; -BUG_ON (target > max_entries); - -/* -* Adjust the left node -*/ -shift (left, center, nr_left - target); - -/* -* Adjust the right node -*/ -shift (center, right, target - nr_right); - *key_ptr (parent, c->index) = center->keys[0 ]; - *key_ptr (parent, r->index) = right->keys[0 ]; +if (((nr_left + nr_center + nr_right) /2 ) <merge_threshold (center)) +delete_center_node (info, parent, l, c, r, left, center, right, + nr_left, nr_center, nr_right); +else +redistribute3 (info, parent, l, c, r, left, center, right, + nr_left, nr_center, nr_right); } static int rebalance3(struct shadow_spine *s, struct dm_btree_info *info,
Signature · 1 line
1.7.5.4
PATCH 4/5 Fix bug that allowed the nr of entries in a btree node to drop below 1/3.
In reply to Joe ThornberFor performance reasons we try and keep all btree nodes at least 1/3 full.
Doing so requires spotting when we should delete a node and move it's entries to it's neighbours. Sometimes this deletion wasn't occuring.
--- drivers/md/persistent-data/dm-btree-remove.c | 21 ++++++--------------- 1 files changed, 6 insertions(+), 15 deletions(-)
drivers/md/persistent-data/dm-btree-remove.c +6 -15
@@ -128,18 +128,9 @@ static void delete_at(struct node *n, unsigned index) n->header.nr_entries =cpu_to_le32 (nr_entries -1 ); } -static unsigned del_threshold (struct node *n) -{ -return le32_to_cpu (n->header.max_entries) /3 ; -} -static unsigned merge_threshold (struct node *n) { -/* -* The extra one is because we know we're potentially going to -* delete an entry. -*/ -return 2 * (le32_to_cpu (n->header.max_entries) /3 ) +1 ; +return le32_to_cpu (n->header.max_entries) /3 ; }struct child {
@@ -215,8 +206,9 @@ static void __rebalance2(struct dm_btree_info *info, struct node *parent,struct node *right = r->n;uint32_t nr_left =le32_to_cpu (left->header.nr_entries);uint32_t nr_right =le32_to_cpu (right->header.nr_entries); +unsigned threshold =2 *merge_threshold (left) +1 ; -if (nr_left + nr_right <=merge_threshold (left)) { +if (nr_left + nr_right < threshold) {/* * Merge */
@@ -331,10 +323,12 @@ static void __rebalance3(struct dm_btree_info *info, struct node *parent,uint32_t nr_center =le32_to_cpu (center->header.nr_entries);uint32_t nr_right =le32_to_cpu (right->header.nr_entries); +unsigned threshold =merge_threshold (left) *4 +1 ; +BUG_ON (left->header.max_entries != center->header.max_entries);BUG_ON (center->header.max_entries != right->header.max_entries); -if (((nr_left + nr_center + nr_right) /2 ) <merge_threshold (center)) +if ((nr_left + nr_center + nr_right) /2 < threshold)delete_center_node (info, parent, l, c, r, left, center, right, nr_left, nr_center, nr_right);else
@@ -443,9 +437,6 @@ static int rebalance_children(struct shadow_spine *s,if (r)return r; -if (child_entries >del_threshold (n)) -return 0 ; - has_left_sibling = i >0 ; has_right_sibling = i < (le32_to_cpu (n->header.nr_entries) -1 );
Signature · 1 line
1.7.5.4
PATCH 5/5 Remove entries from the ref_count tree if they're no longer needed.
In reply to Joe ThornberRef counts are stored in two places: a bitmap if the ref_count is below 3, or a btreeof uint32_t if 3 or above.
When a ref_count that was above 3 drops below we can remove it from the tree and save some metadata space. This removal was commented out before because I was unsure why this was causing under populated btree nodes. Earlier patches have fixed this issue.
--- drivers/md/persistent-data/dm-space-map-common.c | 3 --- 1 files changed, 0 insertions(+), 3 deletions(-)
drivers/md/persistent-data/dm-space-map-common.c +0 -3
@@ -405,8 +405,6 @@ int sm_ll_insert(struct ll_disk *ll, dm_block_t b,if (r <0 )return r; -#if 0 -/* FIXME: dm_btree_remove doesn't handle this yet */ if (old >2 ) { r = dm_btree_remove(&ll->ref_count_info, ll->ref_count_root,
@@ -414,7 +412,6 @@ int sm_ll_insert(struct ll_disk *ll, dm_block_t b,if (r)return r; } -#endif }else {__le32 le_rc =cpu_to_le32 (ref_count);
Signature · 1 line
1.7.5.4