1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2020 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
13 #include "xfs_mount.h"
14 #include "xfs_inode.h"
15 #include "xfs_trans.h"
16 #include "xfs_btree.h"
17 #include "xfs_trace.h"
18 #include "xfs_btree_staging.h"
21 * Staging Cursors and Fake Roots for Btrees
22 * =========================================
24 * A staging btree cursor is a special type of btree cursor that callers must
25 * use to construct a new btree index using the btree bulk loader code. The
26 * bulk loading code uses the staging btree cursor to abstract the details of
27 * initializing new btree blocks and filling them with records or key/ptr
28 * pairs. Regular btree operations (e.g. queries and modifications) are not
29 * supported with staging cursors, and callers must not invoke them.
31 * Fake root structures contain all the information about a btree that is under
32 * construction by the bulk loading code. Staging btree cursors point to fake
33 * root structures instead of the usual AG header or inode structure.
35 * Callers are expected to initialize a fake root structure and pass it into
36 * the _stage_cursor function for a specific btree type. When bulk loading is
37 * complete, callers should call the _commit_staged_btree function for that
38 * specific btree type to commit the new btree into the filesystem.
42 * Don't allow staging cursors to be duplicated because they're supposed to be
43 * kept private to a single thread.
45 STATIC struct xfs_btree_cur *
46 xfs_btree_fakeroot_dup_cursor(
47 struct xfs_btree_cur *cur)
54 * Don't allow block allocation for a staging cursor, because staging cursors
55 * do not support regular btree modifications.
57 * Bulk loading uses a separate callback to obtain new blocks from a
58 * preallocated list, which prevents ENOSPC failures during loading.
61 xfs_btree_fakeroot_alloc_block(
62 struct xfs_btree_cur *cur,
63 const union xfs_btree_ptr *start_bno,
64 union xfs_btree_ptr *new_bno,
72 * Don't allow block freeing for a staging cursor, because staging cursors
73 * do not support regular btree modifications.
76 xfs_btree_fakeroot_free_block(
77 struct xfs_btree_cur *cur,
84 /* Initialize a pointer to the root block from the fakeroot. */
86 xfs_btree_fakeroot_init_ptr_from_cur(
87 struct xfs_btree_cur *cur,
88 union xfs_btree_ptr *ptr)
90 struct xbtree_afakeroot *afake;
92 ASSERT(cur->bc_flags & XFS_BTREE_STAGING);
94 afake = cur->bc_ag.afake;
95 ptr->s = cpu_to_be32(afake->af_root);
99 * Bulk Loading for AG Btrees
100 * ==========================
102 * For a btree rooted in an AG header, pass a xbtree_afakeroot structure to the
103 * staging cursor. Callers should initialize this to zero.
105 * The _stage_cursor() function for a specific btree type should call
106 * xfs_btree_stage_afakeroot to set up the in-memory cursor as a staging
107 * cursor. The corresponding _commit_staged_btree() function should log the
108 * new root and call xfs_btree_commit_afakeroot() to transform the staging
109 * cursor into a regular btree cursor.
112 /* Update the btree root information for a per-AG fake root. */
114 xfs_btree_afakeroot_set_root(
115 struct xfs_btree_cur *cur,
116 const union xfs_btree_ptr *ptr,
119 struct xbtree_afakeroot *afake = cur->bc_ag.afake;
121 ASSERT(cur->bc_flags & XFS_BTREE_STAGING);
122 afake->af_root = be32_to_cpu(ptr->s);
123 afake->af_levels += inc;
127 * Initialize a AG-rooted btree cursor with the given AG btree fake root.
128 * The btree cursor's bc_ops will be overridden as needed to make the staging
129 * functionality work.
132 xfs_btree_stage_afakeroot(
133 struct xfs_btree_cur *cur,
134 struct xbtree_afakeroot *afake)
136 struct xfs_btree_ops *nops;
138 ASSERT(!(cur->bc_flags & XFS_BTREE_STAGING));
139 ASSERT(!(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE));
140 ASSERT(cur->bc_tp == NULL);
142 nops = kmem_alloc(sizeof(struct xfs_btree_ops), KM_NOFS);
143 memcpy(nops, cur->bc_ops, sizeof(struct xfs_btree_ops));
144 nops->alloc_block = xfs_btree_fakeroot_alloc_block;
145 nops->free_block = xfs_btree_fakeroot_free_block;
146 nops->init_ptr_from_cur = xfs_btree_fakeroot_init_ptr_from_cur;
147 nops->set_root = xfs_btree_afakeroot_set_root;
148 nops->dup_cursor = xfs_btree_fakeroot_dup_cursor;
150 cur->bc_ag.afake = afake;
151 cur->bc_nlevels = afake->af_levels;
153 cur->bc_flags |= XFS_BTREE_STAGING;
157 * Transform an AG-rooted staging btree cursor back into a regular cursor by
158 * substituting a real btree root for the fake one and restoring normal btree
159 * cursor ops. The caller must log the btree root change prior to calling
163 xfs_btree_commit_afakeroot(
164 struct xfs_btree_cur *cur,
165 struct xfs_trans *tp,
166 struct xfs_buf *agbp,
167 const struct xfs_btree_ops *ops)
169 ASSERT(cur->bc_flags & XFS_BTREE_STAGING);
170 ASSERT(cur->bc_tp == NULL);
172 trace_xfs_btree_commit_afakeroot(cur);
174 kmem_free((void *)cur->bc_ops);
175 cur->bc_ag.agbp = agbp;
177 cur->bc_flags &= ~XFS_BTREE_STAGING;
182 * Bulk Loading for Inode-Rooted Btrees
183 * ====================================
185 * For a btree rooted in an inode fork, pass a xbtree_ifakeroot structure to
186 * the staging cursor. This structure should be initialized as follows:
188 * - if_fork_size field should be set to the number of bytes available to the
191 * - if_fork should point to a freshly allocated struct xfs_ifork.
193 * - if_format should be set to the appropriate fork type (e.g.
194 * XFS_DINODE_FMT_BTREE).
196 * All other fields must be zero.
198 * The _stage_cursor() function for a specific btree type should call
199 * xfs_btree_stage_ifakeroot to set up the in-memory cursor as a staging
200 * cursor. The corresponding _commit_staged_btree() function should log the
201 * new root and call xfs_btree_commit_ifakeroot() to transform the staging
202 * cursor into a regular btree cursor.
206 * Initialize an inode-rooted btree cursor with the given inode btree fake
207 * root. The btree cursor's bc_ops will be overridden as needed to make the
208 * staging functionality work. If new_ops is not NULL, these new ops will be
209 * passed out to the caller for further overriding.
212 xfs_btree_stage_ifakeroot(
213 struct xfs_btree_cur *cur,
214 struct xbtree_ifakeroot *ifake,
215 struct xfs_btree_ops **new_ops)
217 struct xfs_btree_ops *nops;
219 ASSERT(!(cur->bc_flags & XFS_BTREE_STAGING));
220 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
221 ASSERT(cur->bc_tp == NULL);
223 nops = kmem_alloc(sizeof(struct xfs_btree_ops), KM_NOFS);
224 memcpy(nops, cur->bc_ops, sizeof(struct xfs_btree_ops));
225 nops->alloc_block = xfs_btree_fakeroot_alloc_block;
226 nops->free_block = xfs_btree_fakeroot_free_block;
227 nops->init_ptr_from_cur = xfs_btree_fakeroot_init_ptr_from_cur;
228 nops->dup_cursor = xfs_btree_fakeroot_dup_cursor;
230 cur->bc_ino.ifake = ifake;
231 cur->bc_nlevels = ifake->if_levels;
233 cur->bc_flags |= XFS_BTREE_STAGING;
240 * Transform an inode-rooted staging btree cursor back into a regular cursor by
241 * substituting a real btree root for the fake one and restoring normal btree
242 * cursor ops. The caller must log the btree root change prior to calling
246 xfs_btree_commit_ifakeroot(
247 struct xfs_btree_cur *cur,
248 struct xfs_trans *tp,
250 const struct xfs_btree_ops *ops)
252 ASSERT(cur->bc_flags & XFS_BTREE_STAGING);
253 ASSERT(cur->bc_tp == NULL);
255 trace_xfs_btree_commit_ifakeroot(cur);
257 kmem_free((void *)cur->bc_ops);
258 cur->bc_ino.ifake = NULL;
259 cur->bc_ino.whichfork = whichfork;
261 cur->bc_flags &= ~XFS_BTREE_STAGING;
266 * Bulk Loading of Staged Btrees
267 * =============================
269 * This interface is used with a staged btree cursor to create a totally new
270 * btree with a large number of records (i.e. more than what would fit in a
271 * single root block). When the creation is complete, the new root can be
272 * linked atomically into the filesystem by committing the staged cursor.
274 * Creation of a new btree proceeds roughly as follows:
276 * The first step is to initialize an appropriate fake btree root structure and
277 * then construct a staged btree cursor. Refer to the block comments about
278 * "Bulk Loading for AG Btrees" and "Bulk Loading for Inode-Rooted Btrees" for
279 * more information about how to do this.
281 * The second step is to initialize a struct xfs_btree_bload context as
282 * documented in the structure definition.
284 * The third step is to call xfs_btree_bload_compute_geometry to compute the
285 * height of and the number of blocks needed to construct the btree. See the
286 * section "Computing the Geometry of the New Btree" for details about this
289 * In step four, the caller must allocate xfs_btree_bload.nr_blocks blocks and
290 * save them for later use by ->claim_block(). Bulk loading requires all
291 * blocks to be allocated beforehand to avoid ENOSPC failures midway through a
292 * rebuild, and to minimize seek distances of the new btree.
294 * Step five is to call xfs_btree_bload() to start constructing the btree.
296 * The final step is to commit the staging btree cursor, which logs the new
297 * btree root and turns the staging cursor into a regular cursor. The caller
298 * is responsible for cleaning up the previous btree blocks, if any.
300 * Computing the Geometry of the New Btree
301 * =======================================
303 * The number of items placed in each btree block is computed via the following
304 * algorithm: For leaf levels, the number of items for the level is nr_records
305 * in the bload structure. For node levels, the number of items for the level
306 * is the number of blocks in the next lower level of the tree. For each
307 * level, the desired number of items per block is defined as:
309 * desired = max(minrecs, maxrecs - slack factor)
311 * The number of blocks for the level is defined to be:
313 * blocks = floor(nr_items / desired)
315 * Note this is rounded down so that the npb calculation below will never fall
316 * below minrecs. The number of items that will actually be loaded into each
317 * btree block is defined as:
319 * npb = nr_items / blocks
321 * Some of the leftmost blocks in the level will contain one extra record as
322 * needed to handle uneven division. If the number of records in any block
323 * would exceed maxrecs for that level, blocks is incremented and npb is
326 * In other words, we compute the number of blocks needed to satisfy a given
327 * loading level, then spread the items as evenly as possible.
329 * The height and number of fs blocks required to create the btree are computed
330 * and returned via btree_height and nr_blocks.
334 * Put a btree block that we're loading onto the ordered list and release it.
335 * The btree blocks will be written to disk when bulk loading is finished.
336 * If we reach the dirty buffer threshold, flush them to disk before
340 xfs_btree_bload_drop_buf(
341 struct xfs_btree_bload *bbl,
342 struct list_head *buffers_list,
343 struct xfs_buf **bpp)
345 struct xfs_buf *bp = *bpp;
352 * Mark this buffer XBF_DONE (i.e. uptodate) so that a subsequent
353 * xfs_buf_read will not pointlessly reread the contents from the disk.
355 bp->b_flags |= XBF_DONE;
357 xfs_buf_delwri_queue_here(bp, buffers_list);
362 if (!bbl->max_dirty || bbl->nr_dirty < bbl->max_dirty)
365 error = xfs_buf_delwri_submit(buffers_list);
374 * Allocate and initialize one btree block for bulk loading.
376 * The new btree block will have its level and numrecs fields set to the values
377 * of the level and nr_this_block parameters, respectively.
379 * The caller should ensure that ptrp, bpp, and blockp refer to the left
380 * sibling of the new block, if there is any. On exit, ptrp, bpp, and blockp
381 * will all point to the new block.
384 xfs_btree_bload_prep_block(
385 struct xfs_btree_cur *cur,
386 struct xfs_btree_bload *bbl,
387 struct list_head *buffers_list,
389 unsigned int nr_this_block,
390 union xfs_btree_ptr *ptrp, /* in/out */
391 struct xfs_buf **bpp, /* in/out */
392 struct xfs_btree_block **blockp, /* in/out */
395 union xfs_btree_ptr new_ptr;
396 struct xfs_buf *new_bp;
397 struct xfs_btree_block *new_block;
400 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
401 level == cur->bc_nlevels - 1) {
402 struct xfs_ifork *ifp = xfs_btree_ifork_ptr(cur);
405 ASSERT(*bpp == NULL);
407 /* Allocate a new incore btree root block. */
408 new_size = bbl->iroot_size(cur, level, nr_this_block, priv);
409 ifp->if_broot = kmem_zalloc(new_size, 0);
410 ifp->if_broot_bytes = (int)new_size;
412 /* Initialize it and send it out. */
413 xfs_btree_init_block_int(cur->bc_mp, ifp->if_broot,
414 XFS_BUF_DADDR_NULL, cur->bc_btnum, level,
415 nr_this_block, cur->bc_ino.ip->i_ino,
419 *blockp = ifp->if_broot;
420 xfs_btree_set_ptr_null(cur, ptrp);
424 /* Claim one of the caller's preallocated blocks. */
425 xfs_btree_set_ptr_null(cur, &new_ptr);
426 ret = bbl->claim_block(cur, &new_ptr, priv);
430 ASSERT(!xfs_btree_ptr_is_null(cur, &new_ptr));
432 ret = xfs_btree_get_buf_block(cur, &new_ptr, &new_block, &new_bp);
437 * The previous block (if any) is the left sibling of the new block,
438 * so set its right sibling pointer to the new block and drop it.
441 xfs_btree_set_sibling(cur, *blockp, &new_ptr, XFS_BB_RIGHTSIB);
443 ret = xfs_btree_bload_drop_buf(bbl, buffers_list, bpp);
447 /* Initialize the new btree block. */
448 xfs_btree_init_block_cur(cur, new_bp, level, nr_this_block);
449 xfs_btree_set_sibling(cur, new_block, ptrp, XFS_BB_LEFTSIB);
451 /* Set the out parameters. */
454 xfs_btree_copy_ptrs(cur, ptrp, &new_ptr, 1);
458 /* Load one leaf block. */
460 xfs_btree_bload_leaf(
461 struct xfs_btree_cur *cur,
462 unsigned int recs_this_block,
463 xfs_btree_bload_get_records_fn get_records,
464 struct xfs_btree_block *block,
470 /* Fill the leaf block with records. */
471 while (j <= recs_this_block) {
472 ret = get_records(cur, j, block, recs_this_block - j + 1, priv);
482 * Load one node block with key/ptr pairs.
484 * child_ptr must point to a block within the next level down in the tree. A
485 * key/ptr entry will be created in the new node block to the block pointed to
486 * by child_ptr. On exit, child_ptr points to the next block on the child
487 * level that needs processing.
490 xfs_btree_bload_node(
491 struct xfs_btree_cur *cur,
492 unsigned int recs_this_block,
493 union xfs_btree_ptr *child_ptr,
494 struct xfs_btree_block *block)
499 /* Fill the node block with keys and pointers. */
500 for (j = 1; j <= recs_this_block; j++) {
501 union xfs_btree_key child_key;
502 union xfs_btree_ptr *block_ptr;
503 union xfs_btree_key *block_key;
504 struct xfs_btree_block *child_block;
505 struct xfs_buf *child_bp;
507 ASSERT(!xfs_btree_ptr_is_null(cur, child_ptr));
510 * Read the lower-level block in case the buffer for it has
511 * been reclaimed. LRU refs will be set on the block, which is
512 * desirable if the new btree commits.
514 ret = xfs_btree_read_buf_block(cur, child_ptr, 0, &child_block,
519 block_ptr = xfs_btree_ptr_addr(cur, j, block);
520 xfs_btree_copy_ptrs(cur, block_ptr, child_ptr, 1);
522 block_key = xfs_btree_key_addr(cur, j, block);
523 xfs_btree_get_keys(cur, child_block, &child_key);
524 xfs_btree_copy_keys(cur, block_key, &child_key, 1);
526 xfs_btree_get_sibling(cur, child_block, child_ptr,
528 xfs_buf_relse(child_bp);
535 * Compute the maximum number of records (or keyptrs) per block that we want to
536 * install at this level in the btree. Caller is responsible for having set
537 * @cur->bc_ino.forksize to the desired fork size, if appropriate.
540 xfs_btree_bload_max_npb(
541 struct xfs_btree_cur *cur,
542 struct xfs_btree_bload *bbl,
547 if (level == cur->bc_nlevels - 1 && cur->bc_ops->get_dmaxrecs)
548 return cur->bc_ops->get_dmaxrecs(cur, level);
550 ret = cur->bc_ops->get_maxrecs(cur, level);
552 ret -= bbl->leaf_slack;
554 ret -= bbl->node_slack;
559 * Compute the desired number of records (or keyptrs) per block that we want to
560 * install at this level in the btree, which must be somewhere between minrecs
561 * and max_npb. The caller is free to install fewer records per block.
564 xfs_btree_bload_desired_npb(
565 struct xfs_btree_cur *cur,
566 struct xfs_btree_bload *bbl,
569 unsigned int npb = xfs_btree_bload_max_npb(cur, bbl, level);
571 /* Root blocks are not subject to minrecs rules. */
572 if (level == cur->bc_nlevels - 1)
575 return max_t(unsigned int, cur->bc_ops->get_minrecs(cur, level), npb);
579 * Compute the number of records to be stored in each block at this level and
580 * the number of blocks for this level. For leaf levels, we must populate an
581 * empty root block even if there are no records, so we have to have at least
585 xfs_btree_bload_level_geometry(
586 struct xfs_btree_cur *cur,
587 struct xfs_btree_bload *bbl,
589 uint64_t nr_this_level,
590 unsigned int *avg_per_block,
592 uint64_t *blocks_with_extra)
596 unsigned int desired_npb;
600 * Compute the absolute maximum number of records that we can store in
601 * the ondisk block or inode root.
603 if (cur->bc_ops->get_dmaxrecs)
604 maxnr = cur->bc_ops->get_dmaxrecs(cur, level);
606 maxnr = cur->bc_ops->get_maxrecs(cur, level);
609 * Compute the number of blocks we need to fill each block with the
610 * desired number of records/keyptrs per block. Because desired_npb
611 * could be minrecs, we use regular integer division (which rounds
612 * the block count down) so that in the next step the effective # of
613 * items per block will never be less than desired_npb.
615 desired_npb = xfs_btree_bload_desired_npb(cur, bbl, level);
616 *blocks = div64_u64_rem(nr_this_level, desired_npb, &dontcare);
617 *blocks = max(1ULL, *blocks);
620 * Compute the number of records that we will actually put in each
621 * block, assuming that we want to spread the records evenly between
622 * the blocks. Take care that the effective # of items per block (npb)
623 * won't exceed maxrecs even for the blocks that get an extra record,
624 * since desired_npb could be maxrecs, and in the previous step we
625 * rounded the block count down.
627 npb = div64_u64_rem(nr_this_level, *blocks, blocks_with_extra);
628 if (npb > maxnr || (npb == maxnr && *blocks_with_extra > 0)) {
630 npb = div64_u64_rem(nr_this_level, *blocks, blocks_with_extra);
633 *avg_per_block = min_t(uint64_t, npb, nr_this_level);
635 trace_xfs_btree_bload_level_geometry(cur, level, nr_this_level,
636 *avg_per_block, desired_npb, *blocks,
641 * Ensure a slack value is appropriate for the btree.
643 * If the slack value is negative, set slack so that we fill the block to
644 * halfway between minrecs and maxrecs. Make sure the slack is never so large
645 * that we can underflow minrecs.
648 xfs_btree_bload_ensure_slack(
649 struct xfs_btree_cur *cur,
656 maxr = cur->bc_ops->get_maxrecs(cur, level);
657 minr = cur->bc_ops->get_minrecs(cur, level);
660 * If slack is negative, automatically set slack so that we load the
661 * btree block approximately halfway between minrecs and maxrecs.
662 * Generally, this will net us 75% loading.
665 *slack = maxr - ((maxr + minr) >> 1);
667 *slack = min(*slack, maxr - minr);
671 * Prepare a btree cursor for a bulk load operation by computing the geometry
672 * fields in bbl. Caller must ensure that the btree cursor is a staging
673 * cursor. This function can be called multiple times.
676 xfs_btree_bload_compute_geometry(
677 struct xfs_btree_cur *cur,
678 struct xfs_btree_bload *bbl,
681 uint64_t nr_blocks = 0;
682 uint64_t nr_this_level;
684 ASSERT(cur->bc_flags & XFS_BTREE_STAGING);
687 * Make sure that the slack values make sense for traditional leaf and
688 * node blocks. Inode-rooted btrees will return different minrecs and
689 * maxrecs values for the root block (bc_nlevels == level - 1). We're
690 * checking levels 0 and 1 here, so set bc_nlevels such that the btree
691 * code doesn't interpret either as the root level.
693 cur->bc_nlevels = cur->bc_maxlevels - 1;
694 xfs_btree_bload_ensure_slack(cur, &bbl->leaf_slack, 0);
695 xfs_btree_bload_ensure_slack(cur, &bbl->node_slack, 1);
697 bbl->nr_records = nr_this_level = nr_records;
698 for (cur->bc_nlevels = 1; cur->bc_nlevels <= cur->bc_maxlevels;) {
699 uint64_t level_blocks;
701 unsigned int level = cur->bc_nlevels - 1;
702 unsigned int avg_per_block;
704 xfs_btree_bload_level_geometry(cur, bbl, level, nr_this_level,
705 &avg_per_block, &level_blocks, &dontcare64);
707 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
709 * If all the items we want to store at this level
710 * would fit in the inode root block, then we have our
711 * btree root and are done.
713 * Note that bmap btrees forbid records in the root.
715 if (level != 0 && nr_this_level <= avg_per_block) {
721 * Otherwise, we have to store all the items for this
722 * level in traditional btree blocks and therefore need
723 * another level of btree to point to those blocks.
725 * We have to re-compute the geometry for each level of
726 * an inode-rooted btree because the geometry differs
727 * between a btree root in an inode fork and a
728 * traditional btree block.
730 * This distinction is made in the btree code based on
731 * whether level == bc_nlevels - 1. Based on the
732 * previous root block size check against the root
733 * block geometry, we know that we aren't yet ready to
734 * populate the root. Increment bc_nevels and
735 * recalculate the geometry for a traditional
736 * block-based btree level.
739 ASSERT(cur->bc_nlevels <= cur->bc_maxlevels);
740 xfs_btree_bload_level_geometry(cur, bbl, level,
741 nr_this_level, &avg_per_block,
742 &level_blocks, &dontcare64);
745 * If all the items we want to store at this level
746 * would fit in a single root block, we're done.
748 if (nr_this_level <= avg_per_block) {
753 /* Otherwise, we need another level of btree. */
755 ASSERT(cur->bc_nlevels <= cur->bc_maxlevels);
758 nr_blocks += level_blocks;
759 nr_this_level = level_blocks;
762 if (cur->bc_nlevels > cur->bc_maxlevels)
765 bbl->btree_height = cur->bc_nlevels;
766 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
767 bbl->nr_blocks = nr_blocks - 1;
769 bbl->nr_blocks = nr_blocks;
773 /* Bulk load a btree given the parameters and geometry established in bbl. */
776 struct xfs_btree_cur *cur,
777 struct xfs_btree_bload *bbl,
780 struct list_head buffers_list;
781 union xfs_btree_ptr child_ptr;
782 union xfs_btree_ptr ptr;
783 struct xfs_buf *bp = NULL;
784 struct xfs_btree_block *block = NULL;
785 uint64_t nr_this_level = bbl->nr_records;
788 uint64_t blocks_with_extra;
789 uint64_t total_blocks = 0;
790 unsigned int avg_per_block;
791 unsigned int level = 0;
794 ASSERT(cur->bc_flags & XFS_BTREE_STAGING);
796 INIT_LIST_HEAD(&buffers_list);
797 cur->bc_nlevels = bbl->btree_height;
798 xfs_btree_set_ptr_null(cur, &child_ptr);
799 xfs_btree_set_ptr_null(cur, &ptr);
802 xfs_btree_bload_level_geometry(cur, bbl, level, nr_this_level,
803 &avg_per_block, &blocks, &blocks_with_extra);
805 /* Load each leaf block. */
806 for (i = 0; i < blocks; i++) {
807 unsigned int nr_this_block = avg_per_block;
810 * Due to rounding, btree blocks will not be evenly populated
811 * in most cases. blocks_with_extra tells us how many blocks
812 * will receive an extra record to distribute the excess across
813 * the current level as evenly as possible.
815 if (i < blocks_with_extra)
818 ret = xfs_btree_bload_prep_block(cur, bbl, &buffers_list, level,
819 nr_this_block, &ptr, &bp, &block, priv);
823 trace_xfs_btree_bload_block(cur, level, i, blocks, &ptr,
826 ret = xfs_btree_bload_leaf(cur, nr_this_block, bbl->get_records,
832 * Record the leftmost leaf pointer so we know where to start
833 * with the first node level.
836 xfs_btree_copy_ptrs(cur, &child_ptr, &ptr, 1);
838 total_blocks += blocks;
840 ret = xfs_btree_bload_drop_buf(bbl, &buffers_list, &bp);
844 /* Populate the internal btree nodes. */
845 for (level = 1; level < cur->bc_nlevels; level++) {
846 union xfs_btree_ptr first_ptr;
848 nr_this_level = blocks;
850 xfs_btree_set_ptr_null(cur, &ptr);
852 xfs_btree_bload_level_geometry(cur, bbl, level, nr_this_level,
853 &avg_per_block, &blocks, &blocks_with_extra);
855 /* Load each node block. */
856 for (i = 0; i < blocks; i++) {
857 unsigned int nr_this_block = avg_per_block;
859 if (i < blocks_with_extra)
862 ret = xfs_btree_bload_prep_block(cur, bbl,
863 &buffers_list, level, nr_this_block,
864 &ptr, &bp, &block, priv);
868 trace_xfs_btree_bload_block(cur, level, i, blocks,
869 &ptr, nr_this_block);
871 ret = xfs_btree_bload_node(cur, nr_this_block,
877 * Record the leftmost node pointer so that we know
878 * where to start the next node level above this one.
881 xfs_btree_copy_ptrs(cur, &first_ptr, &ptr, 1);
883 total_blocks += blocks;
885 ret = xfs_btree_bload_drop_buf(bbl, &buffers_list, &bp);
889 xfs_btree_copy_ptrs(cur, &child_ptr, &first_ptr, 1);
892 /* Initialize the new root. */
893 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
894 ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
895 cur->bc_ino.ifake->if_levels = cur->bc_nlevels;
896 cur->bc_ino.ifake->if_blocks = total_blocks - 1;
898 cur->bc_ag.afake->af_root = be32_to_cpu(ptr.s);
899 cur->bc_ag.afake->af_levels = cur->bc_nlevels;
900 cur->bc_ag.afake->af_blocks = total_blocks;
904 * Write the new blocks to disk. If the ordered list isn't empty after
905 * that, then something went wrong and we have to fail. This should
906 * never happen, but we'll check anyway.
908 ret = xfs_buf_delwri_submit(&buffers_list);
911 if (!list_empty(&buffers_list)) {
912 ASSERT(list_empty(&buffers_list));
917 xfs_buf_delwri_cancel(&buffers_list);