xref: /linux/fs/xfs/libxfs/xfs_btree_staging.c (revision 546b928da0427b0d6c663cbb992bd7bfa9ac7971)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2020 Oracle.  All Rights Reserved.
4  * Author: Darrick J. Wong <darrick.wong@oracle.com>
5  */
6 #include "xfs_platform.h"
7 #include "xfs_fs.h"
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
12 #include "xfs_bit.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"
19 
20 /*
21  * Staging Cursors and Fake Roots for Btrees
22  * =========================================
23  *
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.
30  *
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.
34  *
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.
39  */
40 
41 /*
42  * Bulk Loading for AG Btrees
43  * ==========================
44  *
45  * For a btree rooted in an AG header, pass a xbtree_afakeroot structure to the
46  * staging cursor.  Callers should initialize this to zero.
47  *
48  * The _stage_cursor() function for a specific btree type should call
49  * xfs_btree_stage_afakeroot to set up the in-memory cursor as a staging
50  * cursor.  The corresponding _commit_staged_btree() function should log the
51  * new root and call xfs_btree_commit_afakeroot() to transform the staging
52  * cursor into a regular btree cursor.
53  */
54 
55 /*
56  * Initialize a AG-rooted btree cursor with the given AG btree fake root.
57  */
58 void
xfs_btree_stage_afakeroot(struct xfs_btree_cur * cur,struct xbtree_afakeroot * afake)59 xfs_btree_stage_afakeroot(
60 	struct xfs_btree_cur		*cur,
61 	struct xbtree_afakeroot		*afake)
62 {
63 	ASSERT(!(cur->bc_flags & XFS_BTREE_STAGING));
64 	ASSERT(cur->bc_ops->type != XFS_BTREE_TYPE_INODE);
65 	ASSERT(cur->bc_tp == NULL);
66 
67 	cur->bc_ag.afake = afake;
68 	cur->bc_nlevels = afake->af_levels;
69 	cur->bc_flags |= XFS_BTREE_STAGING;
70 }
71 
72 /*
73  * Transform an AG-rooted staging btree cursor back into a regular cursor by
74  * substituting a real btree root for the fake one and restoring normal btree
75  * cursor ops.  The caller must log the btree root change prior to calling
76  * this.
77  */
78 void
xfs_btree_commit_afakeroot(struct xfs_btree_cur * cur,struct xfs_trans * tp,struct xfs_buf * agbp)79 xfs_btree_commit_afakeroot(
80 	struct xfs_btree_cur		*cur,
81 	struct xfs_trans		*tp,
82 	struct xfs_buf			*agbp)
83 {
84 	ASSERT(cur->bc_flags & XFS_BTREE_STAGING);
85 	ASSERT(cur->bc_tp == NULL);
86 
87 	trace_xfs_btree_commit_afakeroot(cur);
88 
89 	cur->bc_ag.afake = NULL;
90 	cur->bc_ag.agbp = agbp;
91 	cur->bc_flags &= ~XFS_BTREE_STAGING;
92 	cur->bc_tp = tp;
93 }
94 
95 /*
96  * Bulk Loading for Inode-Rooted Btrees
97  * ====================================
98  *
99  * For a btree rooted in an inode fork, pass a xbtree_ifakeroot structure to
100  * the staging cursor.  This structure should be initialized as follows:
101  *
102  * - if_fork_size field should be set to the number of bytes available to the
103  *   fork in the inode.
104  *
105  * - if_fork should point to a freshly allocated struct xfs_ifork.
106  *
107  * - if_format should be set to the appropriate fork type (e.g.
108  *   XFS_DINODE_FMT_BTREE).
109  *
110  * All other fields must be zero.
111  *
112  * The _stage_cursor() function for a specific btree type should call
113  * xfs_btree_stage_ifakeroot to set up the in-memory cursor as a staging
114  * cursor.  The corresponding _commit_staged_btree() function should log the
115  * new root and call xfs_btree_commit_ifakeroot() to transform the staging
116  * cursor into a regular btree cursor.
117  */
118 
119 /*
120  * Initialize an inode-rooted btree cursor with the given inode btree fake
121  * root.  The btree cursor's bc_ops will be overridden as needed to make the
122  * staging functionality work.  If new_ops is not NULL, these new ops will be
123  * passed out to the caller for further overriding.
124  */
125 void
xfs_btree_stage_ifakeroot(struct xfs_btree_cur * cur,struct xbtree_ifakeroot * ifake)126 xfs_btree_stage_ifakeroot(
127 	struct xfs_btree_cur		*cur,
128 	struct xbtree_ifakeroot		*ifake)
129 {
130 	ASSERT(!(cur->bc_flags & XFS_BTREE_STAGING));
131 	ASSERT(cur->bc_ops->type == XFS_BTREE_TYPE_INODE);
132 	ASSERT(cur->bc_tp == NULL);
133 
134 	cur->bc_ino.ifake = ifake;
135 	cur->bc_nlevels = ifake->if_levels;
136 	cur->bc_ino.forksize = ifake->if_fork_size;
137 	cur->bc_ino.whichfork = XFS_STAGING_FORK;
138 	cur->bc_flags |= XFS_BTREE_STAGING;
139 }
140 
141 /*
142  * Transform an inode-rooted staging btree cursor back into a regular cursor by
143  * substituting a real btree root for the fake one and restoring normal btree
144  * cursor ops.  The caller must log the btree root change prior to calling
145  * this.
146  */
147 void
xfs_btree_commit_ifakeroot(struct xfs_btree_cur * cur,struct xfs_trans * tp,int whichfork)148 xfs_btree_commit_ifakeroot(
149 	struct xfs_btree_cur		*cur,
150 	struct xfs_trans		*tp,
151 	int				whichfork)
152 {
153 	ASSERT(cur->bc_flags & XFS_BTREE_STAGING);
154 	ASSERT(cur->bc_tp == NULL);
155 
156 	trace_xfs_btree_commit_ifakeroot(cur);
157 
158 	cur->bc_ino.ifake = NULL;
159 	cur->bc_ino.whichfork = whichfork;
160 	cur->bc_flags &= ~XFS_BTREE_STAGING;
161 	cur->bc_tp = tp;
162 }
163 
164 /*
165  * Bulk Loading of Staged Btrees
166  * =============================
167  *
168  * This interface is used with a staged btree cursor to create a totally new
169  * btree with a large number of records (i.e. more than what would fit in a
170  * single root block).  When the creation is complete, the new root can be
171  * linked atomically into the filesystem by committing the staged cursor.
172  *
173  * Creation of a new btree proceeds roughly as follows:
174  *
175  * The first step is to initialize an appropriate fake btree root structure and
176  * then construct a staged btree cursor.  Refer to the block comments about
177  * "Bulk Loading for AG Btrees" and "Bulk Loading for Inode-Rooted Btrees" for
178  * more information about how to do this.
179  *
180  * The second step is to initialize a struct xfs_btree_bload context as
181  * documented in the structure definition.
182  *
183  * The third step is to call xfs_btree_bload_compute_geometry to compute the
184  * height of and the number of blocks needed to construct the btree.  See the
185  * section "Computing the Geometry of the New Btree" for details about this
186  * computation.
187  *
188  * In step four, the caller must allocate xfs_btree_bload.nr_blocks blocks and
189  * save them for later use by ->claim_block().  Bulk loading requires all
190  * blocks to be allocated beforehand to avoid ENOSPC failures midway through a
191  * rebuild, and to minimize seek distances of the new btree.
192  *
193  * Step five is to call xfs_btree_bload() to start constructing the btree.
194  *
195  * The final step is to commit the staging btree cursor, which logs the new
196  * btree root and turns the staging cursor into a regular cursor.  The caller
197  * is responsible for cleaning up the previous btree blocks, if any.
198  *
199  * Computing the Geometry of the New Btree
200  * =======================================
201  *
202  * The number of items placed in each btree block is computed via the following
203  * algorithm: For leaf levels, the number of items for the level is nr_records
204  * in the bload structure.  For node levels, the number of items for the level
205  * is the number of blocks in the next lower level of the tree.  For each
206  * level, the desired number of items per block is defined as:
207  *
208  * desired = max(minrecs, maxrecs - slack factor)
209  *
210  * The number of blocks for the level is defined to be:
211  *
212  * blocks = floor(nr_items / desired)
213  *
214  * Note this is rounded down so that the npb calculation below will never fall
215  * below minrecs.  The number of items that will actually be loaded into each
216  * btree block is defined as:
217  *
218  * npb =  nr_items / blocks
219  *
220  * Some of the leftmost blocks in the level will contain one extra record as
221  * needed to handle uneven division.  If the number of records in any block
222  * would exceed maxrecs for that level, blocks is incremented and npb is
223  * recalculated.
224  *
225  * In other words, we compute the number of blocks needed to satisfy a given
226  * loading level, then spread the items as evenly as possible.
227  *
228  * The height and number of fs blocks required to create the btree are computed
229  * and returned via btree_height and nr_blocks.
230  */
231 
232 /*
233  * Put a btree block that we're loading onto the ordered list and release it.
234  * The btree blocks will be written to disk when bulk loading is finished.
235  * If we reach the dirty buffer threshold, flush them to disk before
236  * continuing.
237  */
238 static int
xfs_btree_bload_drop_buf(struct xfs_btree_bload * bbl,struct list_head * buffers_list,struct xfs_buf ** bpp)239 xfs_btree_bload_drop_buf(
240 	struct xfs_btree_bload		*bbl,
241 	struct list_head		*buffers_list,
242 	struct xfs_buf			**bpp)
243 {
244 	struct xfs_buf			*bp = *bpp;
245 	int				error;
246 
247 	if (!bp)
248 		return 0;
249 
250 	/*
251 	 * Mark this buffer uptodate so that a subsequent xfs_buf_read will
252 	 * not pointlessly reread the contents from the disk.
253 	 */
254 	xfs_buf_set_uptodate(bp);
255 	xfs_buf_delwri_queue_here(bp, buffers_list);
256 	xfs_buf_relse(bp);
257 	*bpp = NULL;
258 	bbl->nr_dirty++;
259 
260 	if (!bbl->max_dirty || bbl->nr_dirty < bbl->max_dirty)
261 		return 0;
262 
263 	error = xfs_buf_delwri_submit(buffers_list);
264 	if (error)
265 		return error;
266 
267 	bbl->nr_dirty = 0;
268 	return 0;
269 }
270 
271 /*
272  * Allocate and initialize one btree block for bulk loading.
273  *
274  * The new btree block will have its level and numrecs fields set to the values
275  * of the level and nr_this_block parameters, respectively.
276  *
277  * The caller should ensure that ptrp, bpp, and blockp refer to the left
278  * sibling of the new block, if there is any.  On exit, ptrp, bpp, and blockp
279  * will all point to the new block.
280  */
281 STATIC int
xfs_btree_bload_prep_block(struct xfs_btree_cur * cur,struct xfs_btree_bload * bbl,struct list_head * buffers_list,unsigned int level,unsigned int nr_this_block,union xfs_btree_ptr * ptrp,struct xfs_buf ** bpp,struct xfs_btree_block ** blockp,void * priv)282 xfs_btree_bload_prep_block(
283 	struct xfs_btree_cur		*cur,
284 	struct xfs_btree_bload		*bbl,
285 	struct list_head		*buffers_list,
286 	unsigned int			level,
287 	unsigned int			nr_this_block,
288 	union xfs_btree_ptr		*ptrp, /* in/out */
289 	struct xfs_buf			**bpp, /* in/out */
290 	struct xfs_btree_block		**blockp, /* in/out */
291 	void				*priv)
292 {
293 	union xfs_btree_ptr		new_ptr;
294 	struct xfs_buf			*new_bp;
295 	struct xfs_btree_block		*new_block;
296 	int				ret;
297 
298 	if (xfs_btree_at_iroot(cur, level)) {
299 		struct xfs_ifork	*ifp = xfs_btree_ifork_ptr(cur);
300 		size_t			new_size;
301 
302 		ASSERT(*bpp == NULL);
303 
304 		/* Allocate a new incore btree root block. */
305 		new_size = bbl->iroot_size(cur, level, nr_this_block, priv);
306 		ifp->if_broot = kzalloc(new_size, GFP_KERNEL | __GFP_NOFAIL);
307 		ifp->if_broot_bytes = (int)new_size;
308 
309 		/* Initialize it and send it out. */
310 		xfs_btree_init_block(cur->bc_mp, ifp->if_broot, cur->bc_ops,
311 				level, nr_this_block, I_INO(cur->bc_ino.ip));
312 
313 		*bpp = NULL;
314 		*blockp = ifp->if_broot;
315 		xfs_btree_set_ptr_null(cur, ptrp);
316 		return 0;
317 	}
318 
319 	/* Claim one of the caller's preallocated blocks. */
320 	xfs_btree_set_ptr_null(cur, &new_ptr);
321 	ret = bbl->claim_block(cur, &new_ptr, priv);
322 	if (ret)
323 		return ret;
324 
325 	ASSERT(!xfs_btree_ptr_is_null(cur, &new_ptr));
326 
327 	ret = xfs_btree_get_buf_block(cur, &new_ptr, &new_block, &new_bp);
328 	if (ret)
329 		return ret;
330 
331 	/*
332 	 * The previous block (if any) is the left sibling of the new block,
333 	 * so set its right sibling pointer to the new block and drop it.
334 	 */
335 	if (*blockp)
336 		xfs_btree_set_sibling(cur, *blockp, &new_ptr, XFS_BB_RIGHTSIB);
337 
338 	ret = xfs_btree_bload_drop_buf(bbl, buffers_list, bpp);
339 	if (ret) {
340 		xfs_buf_relse(new_bp);
341 		return ret;
342 	}
343 
344 	/* Initialize the new btree block. */
345 	xfs_btree_init_block_cur(cur, new_bp, level, nr_this_block);
346 	xfs_btree_set_sibling(cur, new_block, ptrp, XFS_BB_LEFTSIB);
347 
348 	/* Set the out parameters. */
349 	*bpp = new_bp;
350 	*blockp = new_block;
351 	xfs_btree_copy_ptrs(cur, ptrp, &new_ptr, 1);
352 	return 0;
353 }
354 
355 /* Load one leaf block. */
356 STATIC int
xfs_btree_bload_leaf(struct xfs_btree_cur * cur,unsigned int recs_this_block,xfs_btree_bload_get_records_fn get_records,struct xfs_btree_block * block,void * priv)357 xfs_btree_bload_leaf(
358 	struct xfs_btree_cur		*cur,
359 	unsigned int			recs_this_block,
360 	xfs_btree_bload_get_records_fn	get_records,
361 	struct xfs_btree_block		*block,
362 	void				*priv)
363 {
364 	unsigned int			j = 1;
365 	int				ret;
366 
367 	/* Fill the leaf block with records. */
368 	while (j <= recs_this_block) {
369 		ret = get_records(cur, j, block, recs_this_block - j + 1, priv);
370 		if (ret < 0)
371 			return ret;
372 		j += ret;
373 	}
374 
375 	return 0;
376 }
377 
378 /*
379  * Load one node block with key/ptr pairs.
380  *
381  * child_ptr must point to a block within the next level down in the tree.  A
382  * key/ptr entry will be created in the new node block to the block pointed to
383  * by child_ptr.  On exit, child_ptr points to the next block on the child
384  * level that needs processing.
385  */
386 STATIC int
xfs_btree_bload_node(struct xfs_btree_cur * cur,unsigned int recs_this_block,union xfs_btree_ptr * child_ptr,struct xfs_btree_block * block)387 xfs_btree_bload_node(
388 	struct xfs_btree_cur	*cur,
389 	unsigned int		recs_this_block,
390 	union xfs_btree_ptr	*child_ptr,
391 	struct xfs_btree_block	*block)
392 {
393 	unsigned int		j;
394 	int			ret;
395 
396 	/* Fill the node block with keys and pointers. */
397 	for (j = 1; j <= recs_this_block; j++) {
398 		union xfs_btree_key	child_key;
399 		union xfs_btree_ptr	*block_ptr;
400 		union xfs_btree_key	*block_key;
401 		struct xfs_btree_block	*child_block;
402 		struct xfs_buf		*child_bp;
403 
404 		ASSERT(!xfs_btree_ptr_is_null(cur, child_ptr));
405 
406 		/*
407 		 * Read the lower-level block in case the buffer for it has
408 		 * been reclaimed.  LRU refs will be set on the block, which is
409 		 * desirable if the new btree commits.
410 		 */
411 		ret = xfs_btree_read_buf_block(cur, child_ptr, 0, &child_block,
412 				&child_bp);
413 		if (ret)
414 			return ret;
415 
416 		block_ptr = xfs_btree_ptr_addr(cur, j, block);
417 		xfs_btree_copy_ptrs(cur, block_ptr, child_ptr, 1);
418 
419 		block_key = xfs_btree_key_addr(cur, j, block);
420 		xfs_btree_get_keys(cur, child_block, &child_key);
421 		xfs_btree_copy_keys(cur, block_key, &child_key, 1);
422 
423 		xfs_btree_get_sibling(cur, child_block, child_ptr,
424 				XFS_BB_RIGHTSIB);
425 		xfs_buf_relse(child_bp);
426 	}
427 
428 	return 0;
429 }
430 
431 /*
432  * Compute the maximum number of records (or keyptrs) per block that we want to
433  * install at this level in the btree.  Caller is responsible for having set
434  * @cur->bc_ino.forksize to the desired fork size, if appropriate.
435  */
436 STATIC unsigned int
xfs_btree_bload_max_npb(struct xfs_btree_cur * cur,struct xfs_btree_bload * bbl,unsigned int level)437 xfs_btree_bload_max_npb(
438 	struct xfs_btree_cur	*cur,
439 	struct xfs_btree_bload	*bbl,
440 	unsigned int		level)
441 {
442 	unsigned int		ret;
443 
444 	if (level == cur->bc_nlevels - 1 && cur->bc_ops->get_dmaxrecs)
445 		return cur->bc_ops->get_dmaxrecs(cur, level);
446 
447 	ret = cur->bc_ops->get_maxrecs(cur, level);
448 	if (level == 0)
449 		ret -= bbl->leaf_slack;
450 	else
451 		ret -= bbl->node_slack;
452 	return ret;
453 }
454 
455 /*
456  * Compute the desired number of records (or keyptrs) per block that we want to
457  * install at this level in the btree, which must be somewhere between minrecs
458  * and max_npb.  The caller is free to install fewer records per block.
459  */
460 STATIC unsigned int
xfs_btree_bload_desired_npb(struct xfs_btree_cur * cur,struct xfs_btree_bload * bbl,unsigned int level)461 xfs_btree_bload_desired_npb(
462 	struct xfs_btree_cur	*cur,
463 	struct xfs_btree_bload	*bbl,
464 	unsigned int		level)
465 {
466 	unsigned int		npb = xfs_btree_bload_max_npb(cur, bbl, level);
467 
468 	/* Root blocks are not subject to minrecs rules. */
469 	if (level == cur->bc_nlevels - 1)
470 		return max(1U, npb);
471 
472 	return max_t(unsigned int, cur->bc_ops->get_minrecs(cur, level), npb);
473 }
474 
475 /*
476  * Compute the number of records to be stored in each block at this level and
477  * the number of blocks for this level.  For leaf levels, we must populate an
478  * empty root block even if there are no records, so we have to have at least
479  * one block.
480  */
481 STATIC void
xfs_btree_bload_level_geometry(struct xfs_btree_cur * cur,struct xfs_btree_bload * bbl,unsigned int level,uint64_t nr_this_level,unsigned int * avg_per_block,uint64_t * blocks,uint64_t * blocks_with_extra)482 xfs_btree_bload_level_geometry(
483 	struct xfs_btree_cur	*cur,
484 	struct xfs_btree_bload	*bbl,
485 	unsigned int		level,
486 	uint64_t		nr_this_level,
487 	unsigned int		*avg_per_block,
488 	uint64_t		*blocks,
489 	uint64_t		*blocks_with_extra)
490 {
491 	uint64_t		npb;
492 	uint64_t		dontcare;
493 	unsigned int		desired_npb;
494 	unsigned int		maxnr;
495 
496 	/*
497 	 * Compute the absolute maximum number of records that we can store in
498 	 * the ondisk block or inode root.
499 	 */
500 	if (cur->bc_ops->get_dmaxrecs)
501 		maxnr = cur->bc_ops->get_dmaxrecs(cur, level);
502 	else
503 		maxnr = cur->bc_ops->get_maxrecs(cur, level);
504 
505 	/*
506 	 * Compute the number of blocks we need to fill each block with the
507 	 * desired number of records/keyptrs per block.  Because desired_npb
508 	 * could be minrecs, we use regular integer division (which rounds
509 	 * the block count down) so that in the next step the effective # of
510 	 * items per block will never be less than desired_npb.
511 	 */
512 	desired_npb = xfs_btree_bload_desired_npb(cur, bbl, level);
513 	*blocks = div64_u64_rem(nr_this_level, desired_npb, &dontcare);
514 	*blocks = max(1ULL, *blocks);
515 
516 	/*
517 	 * Compute the number of records that we will actually put in each
518 	 * block, assuming that we want to spread the records evenly between
519 	 * the blocks.  Take care that the effective # of items per block (npb)
520 	 * won't exceed maxrecs even for the blocks that get an extra record,
521 	 * since desired_npb could be maxrecs, and in the previous step we
522 	 * rounded the block count down.
523 	 */
524 	npb = div64_u64_rem(nr_this_level, *blocks, blocks_with_extra);
525 	if (npb > maxnr || (npb == maxnr && *blocks_with_extra > 0)) {
526 		(*blocks)++;
527 		npb = div64_u64_rem(nr_this_level, *blocks, blocks_with_extra);
528 	}
529 
530 	*avg_per_block = min_t(uint64_t, npb, nr_this_level);
531 
532 	trace_xfs_btree_bload_level_geometry(cur, level, nr_this_level,
533 			*avg_per_block, desired_npb, *blocks,
534 			*blocks_with_extra);
535 }
536 
537 /*
538  * Ensure a slack value is appropriate for the btree.
539  *
540  * If the slack value is negative, set slack so that we fill the block to
541  * halfway between minrecs and maxrecs.  Make sure the slack is never so large
542  * that we can underflow minrecs.
543  */
544 static void
xfs_btree_bload_ensure_slack(struct xfs_btree_cur * cur,int * slack,int level)545 xfs_btree_bload_ensure_slack(
546 	struct xfs_btree_cur	*cur,
547 	int			*slack,
548 	int			level)
549 {
550 	int			maxr;
551 	int			minr;
552 
553 	maxr = cur->bc_ops->get_maxrecs(cur, level);
554 	minr = cur->bc_ops->get_minrecs(cur, level);
555 
556 	/*
557 	 * If slack is negative, automatically set slack so that we load the
558 	 * btree block approximately halfway between minrecs and maxrecs.
559 	 * Generally, this will net us 75% loading.
560 	 */
561 	if (*slack < 0)
562 		*slack = maxr - ((maxr + minr) >> 1);
563 
564 	*slack = min(*slack, maxr - minr);
565 }
566 
567 /*
568  * Prepare a btree cursor for a bulk load operation by computing the geometry
569  * fields in bbl.  Caller must ensure that the btree cursor is a staging
570  * cursor.  This function can be called multiple times.
571  */
572 int
xfs_btree_bload_compute_geometry(struct xfs_btree_cur * cur,struct xfs_btree_bload * bbl,uint64_t nr_records)573 xfs_btree_bload_compute_geometry(
574 	struct xfs_btree_cur	*cur,
575 	struct xfs_btree_bload	*bbl,
576 	uint64_t		nr_records)
577 {
578 	const struct xfs_btree_ops *ops = cur->bc_ops;
579 	uint64_t		nr_blocks = 0;
580 	uint64_t		nr_this_level;
581 
582 	ASSERT(cur->bc_flags & XFS_BTREE_STAGING);
583 
584 	/*
585 	 * Make sure that the slack values make sense for traditional leaf and
586 	 * node blocks.  Inode-rooted btrees will return different minrecs and
587 	 * maxrecs values for the root block (bc_nlevels == level - 1).  We're
588 	 * checking levels 0 and 1 here, so set bc_nlevels such that the btree
589 	 * code doesn't interpret either as the root level.
590 	 */
591 	cur->bc_nlevels = cur->bc_maxlevels - 1;
592 	xfs_btree_bload_ensure_slack(cur, &bbl->leaf_slack, 0);
593 	xfs_btree_bload_ensure_slack(cur, &bbl->node_slack, 1);
594 
595 	bbl->nr_records = nr_this_level = nr_records;
596 	for (cur->bc_nlevels = 1; cur->bc_nlevels <= cur->bc_maxlevels;) {
597 		uint64_t	level_blocks;
598 		uint64_t	dontcare64;
599 		unsigned int	level = cur->bc_nlevels - 1;
600 		unsigned int	avg_per_block;
601 
602 		xfs_btree_bload_level_geometry(cur, bbl, level, nr_this_level,
603 				&avg_per_block, &level_blocks, &dontcare64);
604 
605 		if (ops->type == XFS_BTREE_TYPE_INODE) {
606 			/*
607 			 * If all the items we want to store at this level
608 			 * would fit in the inode root block, then we have our
609 			 * btree root and are done.
610 			 *
611 			 * Note that bmap btrees forbid records in the root.
612 			 */
613 			if ((level != 0 ||
614 			     (ops->geom_flags & XFS_BTGEO_IROOT_RECORDS)) &&
615 			    nr_this_level <= avg_per_block) {
616 				nr_blocks++;
617 				break;
618 			}
619 
620 			/*
621 			 * Otherwise, we have to store all the items for this
622 			 * level in traditional btree blocks and therefore need
623 			 * another level of btree to point to those blocks.
624 			 *
625 			 * We have to re-compute the geometry for each level of
626 			 * an inode-rooted btree because the geometry differs
627 			 * between a btree root in an inode fork and a
628 			 * traditional btree block.
629 			 *
630 			 * This distinction is made in the btree code based on
631 			 * whether level == bc_nlevels - 1.  Based on the
632 			 * previous root block size check against the root
633 			 * block geometry, we know that we aren't yet ready to
634 			 * populate the root.  Increment bc_nevels and
635 			 * recalculate the geometry for a traditional
636 			 * block-based btree level.
637 			 */
638 			cur->bc_nlevels++;
639 			ASSERT(cur->bc_nlevels <= cur->bc_maxlevels);
640 			xfs_btree_bload_level_geometry(cur, bbl, level,
641 					nr_this_level, &avg_per_block,
642 					&level_blocks, &dontcare64);
643 		} else {
644 			/*
645 			 * If all the items we want to store at this level
646 			 * would fit in a single root block, we're done.
647 			 */
648 			if (nr_this_level <= avg_per_block) {
649 				nr_blocks++;
650 				break;
651 			}
652 
653 			/* Otherwise, we need another level of btree. */
654 			cur->bc_nlevels++;
655 			ASSERT(cur->bc_nlevels <= cur->bc_maxlevels);
656 		}
657 
658 		nr_blocks += level_blocks;
659 		nr_this_level = level_blocks;
660 	}
661 
662 	if (cur->bc_nlevels > cur->bc_maxlevels)
663 		return -EOVERFLOW;
664 
665 	bbl->btree_height = cur->bc_nlevels;
666 	if (ops->type == XFS_BTREE_TYPE_INODE)
667 		bbl->nr_blocks = nr_blocks - 1;
668 	else
669 		bbl->nr_blocks = nr_blocks;
670 	return 0;
671 }
672 
673 /* Bulk load a btree given the parameters and geometry established in bbl. */
674 int
xfs_btree_bload(struct xfs_btree_cur * cur,struct xfs_btree_bload * bbl,void * priv)675 xfs_btree_bload(
676 	struct xfs_btree_cur		*cur,
677 	struct xfs_btree_bload		*bbl,
678 	void				*priv)
679 {
680 	struct list_head		buffers_list;
681 	union xfs_btree_ptr		child_ptr;
682 	union xfs_btree_ptr		ptr;
683 	struct xfs_buf			*bp = NULL;
684 	struct xfs_btree_block		*block = NULL;
685 	uint64_t			nr_this_level = bbl->nr_records;
686 	uint64_t			blocks;
687 	uint64_t			i;
688 	uint64_t			blocks_with_extra;
689 	uint64_t			total_blocks = 0;
690 	unsigned int			avg_per_block;
691 	unsigned int			level = 0;
692 	int				ret;
693 
694 	ASSERT(cur->bc_flags & XFS_BTREE_STAGING);
695 
696 	INIT_LIST_HEAD(&buffers_list);
697 	cur->bc_nlevels = bbl->btree_height;
698 	xfs_btree_set_ptr_null(cur, &child_ptr);
699 	xfs_btree_set_ptr_null(cur, &ptr);
700 	bbl->nr_dirty = 0;
701 
702 	xfs_btree_bload_level_geometry(cur, bbl, level, nr_this_level,
703 			&avg_per_block, &blocks, &blocks_with_extra);
704 
705 	/* Load each leaf block. */
706 	for (i = 0; i < blocks; i++) {
707 		unsigned int		nr_this_block = avg_per_block;
708 
709 		/*
710 		 * Due to rounding, btree blocks will not be evenly populated
711 		 * in most cases.  blocks_with_extra tells us how many blocks
712 		 * will receive an extra record to distribute the excess across
713 		 * the current level as evenly as possible.
714 		 */
715 		if (i < blocks_with_extra)
716 			nr_this_block++;
717 
718 		ret = xfs_btree_bload_prep_block(cur, bbl, &buffers_list, level,
719 				nr_this_block, &ptr, &bp, &block, priv);
720 		if (ret)
721 			goto out;
722 
723 		trace_xfs_btree_bload_block(cur, level, i, blocks, &ptr,
724 				nr_this_block);
725 
726 		ret = xfs_btree_bload_leaf(cur, nr_this_block, bbl->get_records,
727 				block, priv);
728 		if (ret)
729 			goto out;
730 
731 		/*
732 		 * Record the leftmost leaf pointer so we know where to start
733 		 * with the first node level.
734 		 */
735 		if (i == 0)
736 			xfs_btree_copy_ptrs(cur, &child_ptr, &ptr, 1);
737 	}
738 	total_blocks += blocks;
739 
740 	ret = xfs_btree_bload_drop_buf(bbl, &buffers_list, &bp);
741 	if (ret)
742 		goto out;
743 
744 	/* Populate the internal btree nodes. */
745 	for (level = 1; level < cur->bc_nlevels; level++) {
746 		union xfs_btree_ptr	first_ptr;
747 
748 		nr_this_level = blocks;
749 		block = NULL;
750 		xfs_btree_set_ptr_null(cur, &ptr);
751 
752 		xfs_btree_bload_level_geometry(cur, bbl, level, nr_this_level,
753 				&avg_per_block, &blocks, &blocks_with_extra);
754 
755 		/* Load each node block. */
756 		for (i = 0; i < blocks; i++) {
757 			unsigned int	nr_this_block = avg_per_block;
758 
759 			if (i < blocks_with_extra)
760 				nr_this_block++;
761 
762 			ret = xfs_btree_bload_prep_block(cur, bbl,
763 					&buffers_list, level, nr_this_block,
764 					&ptr, &bp, &block, priv);
765 			if (ret)
766 				goto out;
767 
768 			trace_xfs_btree_bload_block(cur, level, i, blocks,
769 					&ptr, nr_this_block);
770 
771 			ret = xfs_btree_bload_node(cur, nr_this_block,
772 					&child_ptr, block);
773 			if (ret)
774 				goto out;
775 
776 			/*
777 			 * Record the leftmost node pointer so that we know
778 			 * where to start the next node level above this one.
779 			 */
780 			if (i == 0)
781 				xfs_btree_copy_ptrs(cur, &first_ptr, &ptr, 1);
782 		}
783 		total_blocks += blocks;
784 
785 		ret = xfs_btree_bload_drop_buf(bbl, &buffers_list, &bp);
786 		if (ret)
787 			goto out;
788 
789 		xfs_btree_copy_ptrs(cur, &child_ptr, &first_ptr, 1);
790 	}
791 
792 	/* Initialize the new root. */
793 	if (cur->bc_ops->type == XFS_BTREE_TYPE_INODE) {
794 		ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
795 		cur->bc_ino.ifake->if_levels = cur->bc_nlevels;
796 		cur->bc_ino.ifake->if_blocks = total_blocks - 1;
797 	} else {
798 		cur->bc_ag.afake->af_root = be32_to_cpu(ptr.s);
799 		cur->bc_ag.afake->af_levels = cur->bc_nlevels;
800 		cur->bc_ag.afake->af_blocks = total_blocks;
801 	}
802 
803 	/*
804 	 * Write the new blocks to disk.  If the ordered list isn't empty after
805 	 * that, then something went wrong and we have to fail.  This should
806 	 * never happen, but we'll check anyway.
807 	 */
808 	ret = xfs_buf_delwri_submit(&buffers_list);
809 	if (ret)
810 		goto out;
811 	if (!list_empty(&buffers_list)) {
812 		ASSERT(list_empty(&buffers_list));
813 		ret = -EIO;
814 	}
815 
816 out:
817 	xfs_buf_delwri_cancel(&buffers_list);
818 	if (bp)
819 		xfs_buf_relse(bp);
820 	return ret;
821 }
822