HAMMER VFS - frontload kmalloc()'s when rebalancing
[dragonfly.git] / sys / vfs / hammer / hammer_rebalance.c
1 /*
2  * Copyright (c) 2009 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34
35 #include "hammer.h"
36
37 static int rebalance_node(struct hammer_ioc_rebalance *rebal,
38                         hammer_cursor_t cursor, hammer_node_lock_t lcache);
39 static void rebalance_closeout(hammer_node_lock_t base_item, int base_count,
40                         hammer_btree_elm_t elm);
41 static void rebalance_parent_ptrs(hammer_node_lock_t base_item, int index,
42                         hammer_node_lock_t item, hammer_node_lock_t chld_item);
43
44 /*
45  * Iterate through the specified range of object ids and rebalance B-Tree
46  * leaf and internal nodes we encounter.  A forwards iteration is used.
47  *
48  * All leafs are at the same depth.  We use the b-tree scan code loosely
49  * to position ourselves and create degenerate cases to skip indices
50  * that we have rebalanced in bulk.
51  */
52
53 int
54 hammer_ioc_rebalance(hammer_transaction_t trans, hammer_inode_t ip,
55                  struct hammer_ioc_rebalance *rebal)
56 {
57         struct hammer_cursor cursor;
58         struct hammer_node_lock lcache;
59         hammer_btree_leaf_elm_t elm;
60         int error;
61         int seq;
62
63         if ((rebal->key_beg.localization | rebal->key_end.localization) &
64             HAMMER_LOCALIZE_PSEUDOFS_MASK) {
65                 return(EINVAL);
66         }
67         if (rebal->key_beg.localization > rebal->key_end.localization)
68                 return(EINVAL);
69         if (rebal->key_beg.localization == rebal->key_end.localization) {
70                 if (rebal->key_beg.obj_id > rebal->key_end.obj_id)
71                         return(EINVAL);
72                 /* key-space limitations - no check needed */
73         }
74         if (rebal->saturation < HAMMER_BTREE_INT_ELMS / 2)
75                 rebal->saturation = HAMMER_BTREE_INT_ELMS / 2;
76         if (rebal->saturation > HAMMER_BTREE_INT_ELMS)
77                 rebal->saturation = HAMMER_BTREE_INT_ELMS;
78
79         rebal->key_cur = rebal->key_beg;
80         rebal->key_cur.localization &= HAMMER_LOCALIZE_MASK;
81         rebal->key_cur.localization += ip->obj_localization;
82
83         hammer_btree_lcache_init(trans->hmp, &lcache, 2);
84
85         seq = trans->hmp->flusher.act;
86
87         /*
88          * Scan forwards.  Retries typically occur if a deadlock is detected.
89          */
90 retry:
91         error = hammer_init_cursor(trans, &cursor, NULL, NULL);
92         if (error) {
93                 hammer_done_cursor(&cursor);
94                 goto failed;
95         }
96         cursor.key_beg = rebal->key_cur;
97         cursor.key_end = rebal->key_end;
98         cursor.key_end.localization &= HAMMER_LOCALIZE_MASK;
99         cursor.key_end.localization += ip->obj_localization;
100         cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
101         cursor.flags |= HAMMER_CURSOR_BACKEND;
102
103         /*
104          * Cause internal nodes to be returned on the way up.  Internal nodes
105          * are not returned on the way down so we can create a degenerate
106          * case to handle internal nodes as a trailing function of their
107          * sub-trees.
108          *
109          * Note that by not setting INSERTING or PRUNING no boundary
110          * corrections will be made and a sync lock is not needed for the
111          * B-Tree scan itself.
112          */
113         cursor.flags |= HAMMER_CURSOR_REBLOCKING;
114
115         error = hammer_btree_first(&cursor);
116
117         while (error == 0) {
118                 /*
119                  * Rebalancing can be hard on the memory allocator, make
120                  * sure there is enough free memory before doing it.
121                  */
122                 vm_wait_nominal();
123
124                 /*
125                  * We only care about internal nodes visited for the last
126                  * time on the way up... that is, a trailing scan of the
127                  * internal node after all of its children have been recursed
128                  * through.
129                  */
130                 if (cursor.node->ondisk->type == HAMMER_BTREE_TYPE_INTERNAL) {
131                         /*
132                          * Leave cursor.index alone, we want to recurse
133                          * through all children of the internal node before
134                          * visiting it.
135                          *
136                          * Process the internal node on the way up after
137                          * the last child's sub-tree has been balanced.
138                          */
139                         if (cursor.index == cursor.node->ondisk->count - 1) {
140                                 hammer_sync_lock_sh(trans);
141                                 error = rebalance_node(rebal, &cursor, &lcache);
142                                 hammer_sync_unlock(trans);
143                         }
144                 } else {
145                         /*
146                          * We don't need to iterate through all the leaf
147                          * elements, we only care about the parent (internal)
148                          * node.
149                          */
150                         cursor.index = cursor.node->ondisk->count - 1;
151                 }
152                 if (error)
153                         break;
154
155                 /*
156                  * Update returned scan position and do a flush if
157                  * necessary.
158                  *
159                  * WARNING: We extract the base using the leaf element
160                  *          type but this could be an internal node.  The
161                  *          base is the same either way.
162                  *
163                  *          However, due to the rebalancing operation the
164                  *          cursor position may have exceeded the right-hand
165                  *          boundary.
166                  *
167                  * WARNING: See warnings in hammer_unlock_cursor()
168                  *          function.
169                  */
170                 elm = &cursor.node->ondisk->elms[cursor.index].leaf;
171                 rebal->key_cur = elm->base;
172                 ++rebal->stat_ncount;
173
174                 while (hammer_flusher_meta_halflimit(trans->hmp) ||
175                        hammer_flusher_undo_exhausted(trans, 2)) {
176                         hammer_unlock_cursor(&cursor);
177                         hammer_flusher_wait(trans->hmp, seq);
178                         hammer_lock_cursor(&cursor);
179                         seq = hammer_flusher_async_one(trans->hmp);
180                 }
181
182                 /*
183                  * Before iterating check if the rebalance operation caused
184                  * the cursor to index past the right-hand boundary and make
185                  * sure to stop if it does.  Otherwise the iteration may
186                  * panic e.g. due to the key maxing out its fields and no
187                  * longer being within the strict bounds of the root node.
188                  */
189                 if (hammer_btree_cmp(&rebal->key_cur, &cursor.key_end) > 0) {
190                         rebal->key_cur = cursor.key_end;
191                         break;
192                 }
193
194                 /*
195                  * Iterate, stop if a signal was received.
196                  */
197                 if ((error = hammer_signal_check(trans->hmp)) != 0)
198                         break;
199                 error = hammer_btree_iterate(&cursor);
200         }
201         if (error == ENOENT)
202                 error = 0;
203         hammer_done_cursor(&cursor);
204         if (error == EDEADLK) {
205                 ++rebal->stat_collisions;
206                 goto retry;
207         }
208         if (error == EINTR) {
209                 rebal->head.flags |= HAMMER_IOC_HEAD_INTR;
210                 error = 0;
211         }
212 failed:
213         rebal->key_cur.localization &= HAMMER_LOCALIZE_MASK;
214         hammer_btree_lcache_free(trans->hmp, &lcache);
215         return(error);
216 }
217
218 /*
219  * Rebalance an internal node, called via a trailing upward recursion.
220  * All the children have already been individually rebalanced.
221  *
222  * To rebalance we scan the elements in the children and pack them,
223  * so we actually need to lock the children and the children's children.
224  *
225  *      INTERNAL_NODE
226  *      / / | | | \ \
227  *     C C  C C C  C C  children (first level) (internal or leaf nodes)
228  *                      children's elements (second level)
229  *
230  *      <<<----------   pack children's elements, possibly remove excess
231  *                      children after packing.
232  *
233  * NOTE: The mirror_tids, parent pointers, and child pointers must be updated.
234  *       Any live tracked B-Tree nodes must be updated (we worm out of that
235  *       by not allowing any).  And boundary elements must be preserved.
236  *
237  * NOTE: If the children are leaf nodes we may have a degenerate case
238  *       case where there are no elements in the leafs.
239  *
240  * XXX live-tracked
241  */
242 static int
243 rebalance_node(struct hammer_ioc_rebalance *rebal, hammer_cursor_t cursor,
244                struct hammer_node_lock *lcache)
245 {
246         struct hammer_node_lock lockroot;
247         hammer_node_lock_t base_item;
248         hammer_node_lock_t chld_item;
249         hammer_node_lock_t item;
250         hammer_btree_elm_t elm;
251         hammer_node_t node;
252         hammer_tid_t tid;
253         u_int8_t type1;
254         int base_count;
255         int root_count;
256         int avg_elms;
257         int count;
258         int error;
259         int i;
260         int n;
261
262         /*
263          * Lock the parent node via the cursor, collect and lock our
264          * children and children's children.
265          *
266          * By the way, this is a LOT of locks.
267          */
268         hammer_node_lock_init(&lockroot, cursor->node);
269         error = hammer_cursor_upgrade(cursor);
270         if (error)
271                 goto done;
272         error = hammer_btree_lock_children(cursor, 2, &lockroot, lcache);
273         if (error)
274                 goto done;
275
276         /*
277          * Make a copy of all the locked on-disk data to simplify the element
278          * shifting we are going to have to do.  We will modify the copy
279          * first.
280          */
281         hammer_btree_lock_copy(cursor, &lockroot);
282
283         /*
284          * Look at the first child node.
285          */
286         if (TAILQ_FIRST(&lockroot.list) == NULL)
287                 goto done;
288         type1 = TAILQ_FIRST(&lockroot.list)->node->ondisk->type;
289
290         /*
291          * Figure out the total number of children's children and
292          * calculate the average number of elements per child.
293          *
294          * The minimum avg_elms is 1 when count > 0.  avg_elms * root_elms
295          * is always greater or equal to count.
296          *
297          * If count == 0 we hit a degenerate case which will cause
298          * avg_elms to also calculate as 0.
299          */
300         if (hammer_debug_general & 0x1000)
301                 kprintf("lockroot %p count %d\n", &lockroot, lockroot.count);
302         count = 0;
303         TAILQ_FOREACH(item, &lockroot.list, entry) {
304                 if (hammer_debug_general & 0x1000)
305                         kprintf("add count %d\n", item->count);
306                 count += item->count;
307                 KKASSERT(item->node->ondisk->type == type1);
308         }
309         avg_elms = (count + (lockroot.count - 1)) / lockroot.count;
310         KKASSERT(avg_elms >= 0);
311
312         /*
313          * If the average number of elements per child is too low then
314          * calculate the desired number of children (n) such that the
315          * average number of elements is reasonable.
316          *
317          * If the desired number of children is 1 then avg_elms will
318          * wind up being count, which may still be smaller then saturation
319          * but that is ok.
320          */
321         if (count && avg_elms < rebal->saturation) {
322                 n = (count + (rebal->saturation - 1)) / rebal->saturation;
323                 avg_elms = (count + (n - 1)) / n;
324         }
325
326         /*
327          * Pack the elements in the children.  Elements for each item is
328          * packed into base_item until avg_elms is reached, then base_item
329          * iterates.
330          *
331          * hammer_cursor_moved_element() is called for each element moved
332          * to update tracked cursors, including the index beyond the last
333          * element (at count).
334          *
335          * Any cursors tracking the internal node itself must also be
336          * updated, potentially repointing them at a leaf and clearing
337          * ATEDISK.
338          */
339         base_item = TAILQ_FIRST(&lockroot.list);
340         base_count = 0;
341         root_count = 0;
342
343         TAILQ_FOREACH(item, &lockroot.list, entry) {
344                 node = item->node;
345                 KKASSERT(item->count == node->ondisk->count);
346                 chld_item = TAILQ_FIRST(&item->list);
347                 for (i = 0; i < item->count; ++i) {
348                         /*
349                          * Closeout.  If the next element is at index 0
350                          * just use the existing separator in the parent.
351                          */
352                         if (base_count == avg_elms) {
353                                 if (i == 0) {
354                                         elm = &lockroot.node->ondisk->elms[
355                                                 item->index];
356                                 } else {
357                                         elm = &node->ondisk->elms[i];
358                                 }
359                                 rebalance_closeout(base_item, base_count, elm);
360                                 base_item = TAILQ_NEXT(base_item, entry);
361                                 KKASSERT(base_item);
362                                 base_count = 0;
363                                 ++root_count;
364                         }
365
366                         /*
367                          * Check degenerate no-work case.  Otherwise pack
368                          * the element.
369                          *
370                          * All changes are made to the copy.
371                          */
372                         if (item == base_item && i == base_count) {
373                                 ++base_count;
374                                 if (chld_item)
375                                         chld_item = TAILQ_NEXT(chld_item, entry);
376                                 continue;
377                         }
378
379                         /*
380                          * Pack element.
381                          */
382                         elm = &base_item->copy->elms[base_count];
383                         *elm = node->ondisk->elms[i];
384                         base_item->flags |= HAMMER_NODE_LOCK_UPDATED;
385
386                         /*
387                          * Adjust the mirror_tid of the target and the
388                          * internal element linkage.
389                          *
390                          * The parent node (lockroot.node) should already
391                          * have an aggregate mirror_tid so we do not have
392                          * to update that.  However, it is possible for us
393                          * to catch a hammer_btree_mirror_propagate() with
394                          * its pants down.  Update the parent if necessary.
395                          */
396                         tid = node->ondisk->mirror_tid;
397
398                         if (base_item->copy->mirror_tid < tid) {
399                                 base_item->copy->mirror_tid = tid;
400                                 if (lockroot.copy->mirror_tid < tid) {
401                                         lockroot.copy->mirror_tid = tid;
402                                         lockroot.flags |=
403                                                 HAMMER_NODE_LOCK_UPDATED;
404                                 }
405                                 if (lockroot.copy->elms[root_count].
406                                     internal.mirror_tid < tid) {
407                                         lockroot.copy->elms[root_count].
408                                                 internal.mirror_tid = tid;
409                                         lockroot.flags |=
410                                                 HAMMER_NODE_LOCK_UPDATED;
411                                 }
412                                 base_item->flags |= HAMMER_NODE_LOCK_UPDATED;
413                         }
414
415                         /*
416                          * We moved elm.  The parent pointers for any
417                          * children of elm must be repointed.
418                          */
419                         if (item != base_item &&
420                             node->ondisk->type == HAMMER_BTREE_TYPE_INTERNAL) {
421                                 KKASSERT(chld_item);
422                                 rebalance_parent_ptrs(base_item, base_count,
423                                                       item, chld_item);
424                         }
425                         hammer_cursor_moved_element(item->parent->node,
426                                                     item->index,
427                                                     node, i,
428                                                     base_item->node,
429                                                     base_count);
430                         ++base_count;
431                         if (chld_item)
432                                 chld_item = TAILQ_NEXT(chld_item, entry);
433                 }
434
435                 /*
436                  * Always call at the end (i == number of elements) in
437                  * case a cursor is sitting indexed there.
438                  */
439                 hammer_cursor_moved_element(item->parent->node, item->index,
440                                             node, i,
441                                             base_item->node, base_count);
442         }
443
444         /*
445          * Packing complete, close-out base_item using the right-hand
446          * boundary of the original parent.
447          *
448          * If we will be deleting nodes from the root shift the old
449          * right-hand-boundary to the new ending index.
450          */
451         elm = &lockroot.node->ondisk->elms[lockroot.node->ondisk->count];
452         rebalance_closeout(base_item, base_count, elm);
453         ++root_count;
454         if (lockroot.copy->count != root_count) {
455                 lockroot.copy->count = root_count;
456                 lockroot.copy->elms[root_count] = *elm;
457                 lockroot.flags |= HAMMER_NODE_LOCK_UPDATED;
458         }
459
460         /*
461          * Any extra items beyond base_item are now completely empty and
462          * can be destroyed.  Queue the destruction up in the copy.  Note
463          * that none of the destroyed nodes are part of our cursor.
464          *
465          * The cursor is locked so it isn't on the tracking list.  It
466          * should have been pointing at the boundary element (at root_count).
467          * When deleting elements from the root (which is cursor.node), we
468          * have to update the cursor.index manually to keep it in bounds.
469          */
470         while ((base_item = TAILQ_NEXT(base_item, entry)) != NULL) {
471                 hammer_cursor_removed_node(base_item->node, lockroot.node,
472                                            base_count);
473                 hammer_cursor_deleted_element(lockroot.node, base_count);
474                 base_item->copy->type = HAMMER_BTREE_TYPE_DELETED;
475                 base_item->copy->count = 0;
476                 base_item->flags |= HAMMER_NODE_LOCK_UPDATED;
477                 if (cursor->index > lockroot.copy->count)
478                         --cursor->index;
479                 ++rebal->stat_deletions;
480         }
481
482         /*
483          * All done, sync the locked child tree to disk.  This will also
484          * flush and delete deleted nodes.
485          */
486         rebal->stat_nrebal += hammer_btree_sync_copy(cursor, &lockroot);
487 done:
488         hammer_btree_unlock_children(cursor->trans->hmp, &lockroot, lcache);
489         hammer_cursor_downgrade(cursor);
490         return (error);
491 }
492
493 /*
494  * Close-out the child base_item.  This node contains base_count
495  * elements.
496  *
497  * If the node is an internal node the right-hand boundary must be
498  * set to elm.
499  */
500 static
501 void
502 rebalance_closeout(hammer_node_lock_t base_item, int base_count,
503                    hammer_btree_elm_t elm)
504 {
505         hammer_node_lock_t parent;
506         hammer_btree_elm_t base_elm;
507         hammer_btree_elm_t rbound_elm;
508         u_int8_t save;
509
510         /*
511          * Update the count.  NOTE:  base_count can be 0 for the
512          * degenerate leaf case.
513          */
514         if (hammer_debug_general & 0x1000) {
515                 kprintf("rebalance_closeout %016llx:",
516                         (long long)base_item->node->node_offset);
517         }
518         if (base_item->copy->count != base_count) {
519                 base_item->flags |= HAMMER_NODE_LOCK_UPDATED;
520                 base_item->copy->count = base_count;
521                 if (hammer_debug_general & 0x1000)
522                         kprintf(" (count update)");
523         }
524
525         /*
526          * If we are closing out an internal node we must assign
527          * a right-hand boundary.  Use the element contents as the
528          * right-hand boundary.
529          *
530          * Internal nodes are required to have at least one child,
531          * otherwise the left and right boundary would end up being
532          * the same element.  Only leaf nodes can be empty.
533          *
534          * Rebalancing may cut-off an internal node such that the
535          * new right hand boundary is the next element anyway, but
536          * we still have to make sure that subtree_offset, btype,
537          * and mirror_tid are all 0.
538          */
539         if (base_item->copy->type == HAMMER_BTREE_TYPE_INTERNAL) {
540                 KKASSERT(base_count != 0);
541                 base_elm = &base_item->copy->elms[base_count];
542
543                 if (bcmp(base_elm, elm, sizeof(*elm)) != 0 ||
544                     elm->internal.subtree_offset ||
545                     elm->internal.mirror_tid ||
546                     elm->base.btype) {
547                         *base_elm = *elm;
548                         base_elm->internal.subtree_offset = 0;
549                         base_elm->internal.mirror_tid = 0;
550                         base_elm->base.btype = 0;
551                         base_item->flags |= HAMMER_NODE_LOCK_UPDATED;
552                         if (hammer_debug_general & 0x1000)
553                                 kprintf(" (rhs update)");
554                 } else {
555                         if (hammer_debug_general & 0x1000)
556                                 kprintf(" (rhs same)");
557                 }
558         }
559
560         /*
561          * The parent's boundary must be updated.  Be careful to retain
562          * the btype and non-base internal fields as that information is
563          * unrelated.
564          */
565         parent = base_item->parent;
566         rbound_elm = &parent->copy->elms[base_item->index + 1];
567         if (bcmp(&rbound_elm->base, &elm->base, sizeof(elm->base)) != 0) {
568                 save = rbound_elm->base.btype;
569                 rbound_elm->base = elm->base;
570                 rbound_elm->base.btype = save;
571                 parent->flags |= HAMMER_NODE_LOCK_UPDATED;
572                 if (hammer_debug_general & 0x1000) {
573                         kprintf(" (parent bound update %d)",
574                                 base_item->index + 1);
575                 }
576         }
577         if (hammer_debug_general & 0x1000)
578                 kprintf("\n");
579 }
580
581 /*
582  * An element in item has moved to base_item.  We must update the parent
583  * pointer of the node the element points to (which is chld_item).
584  */
585 static
586 void
587 rebalance_parent_ptrs(hammer_node_lock_t base_item, int index,
588                       hammer_node_lock_t item, hammer_node_lock_t chld_item)
589 {
590         KKASSERT(chld_item->node->ondisk->parent == item->node->node_offset);
591         chld_item->copy->parent = base_item->node->node_offset;
592         chld_item->flags |= HAMMER_NODE_LOCK_UPDATED;
593         hammer_cursor_parent_changed(chld_item->node,
594                                      item->node, base_item->node, index);
595 }