xref: /openbmc/linux/fs/xfs/libxfs/xfs_rmap_btree.c (revision 9b2e5a23)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2014 Red Hat, Inc.
4  * All Rights Reserved.
5  */
6 #include "xfs.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_mount.h"
13 #include "xfs_trans.h"
14 #include "xfs_alloc.h"
15 #include "xfs_btree.h"
16 #include "xfs_btree_staging.h"
17 #include "xfs_rmap.h"
18 #include "xfs_rmap_btree.h"
19 #include "xfs_trace.h"
20 #include "xfs_error.h"
21 #include "xfs_extent_busy.h"
22 #include "xfs_ag.h"
23 #include "xfs_ag_resv.h"
24 
25 static struct kmem_cache	*xfs_rmapbt_cur_cache;
26 
27 /*
28  * Reverse map btree.
29  *
30  * This is a per-ag tree used to track the owner(s) of a given extent. With
31  * reflink it is possible for there to be multiple owners, which is a departure
32  * from classic XFS. Owner records for data extents are inserted when the
33  * extent is mapped and removed when an extent is unmapped.  Owner records for
34  * all other block types (i.e. metadata) are inserted when an extent is
35  * allocated and removed when an extent is freed. There can only be one owner
36  * of a metadata extent, usually an inode or some other metadata structure like
37  * an AG btree.
38  *
39  * The rmap btree is part of the free space management, so blocks for the tree
40  * are sourced from the agfl. Hence we need transaction reservation support for
41  * this tree so that the freelist is always large enough. This also impacts on
42  * the minimum space we need to leave free in the AG.
43  *
44  * The tree is ordered by [ag block, owner, offset]. This is a large key size,
45  * but it is the only way to enforce unique keys when a block can be owned by
46  * multiple files at any offset. There's no need to order/search by extent
47  * size for online updating/management of the tree. It is intended that most
48  * reverse lookups will be to find the owner(s) of a particular block, or to
49  * try to recover tree and file data from corrupt primary metadata.
50  */
51 
52 static struct xfs_btree_cur *
53 xfs_rmapbt_dup_cursor(
54 	struct xfs_btree_cur	*cur)
55 {
56 	return xfs_rmapbt_init_cursor(cur->bc_mp, cur->bc_tp,
57 				cur->bc_ag.agbp, cur->bc_ag.pag);
58 }
59 
60 STATIC void
61 xfs_rmapbt_set_root(
62 	struct xfs_btree_cur		*cur,
63 	const union xfs_btree_ptr	*ptr,
64 	int				inc)
65 {
66 	struct xfs_buf		*agbp = cur->bc_ag.agbp;
67 	struct xfs_agf		*agf = agbp->b_addr;
68 	int			btnum = cur->bc_btnum;
69 
70 	ASSERT(ptr->s != 0);
71 
72 	agf->agf_roots[btnum] = ptr->s;
73 	be32_add_cpu(&agf->agf_levels[btnum], inc);
74 	cur->bc_ag.pag->pagf_levels[btnum] += inc;
75 
76 	xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_ROOTS | XFS_AGF_LEVELS);
77 }
78 
79 STATIC int
80 xfs_rmapbt_alloc_block(
81 	struct xfs_btree_cur		*cur,
82 	const union xfs_btree_ptr	*start,
83 	union xfs_btree_ptr		*new,
84 	int				*stat)
85 {
86 	struct xfs_buf		*agbp = cur->bc_ag.agbp;
87 	struct xfs_agf		*agf = agbp->b_addr;
88 	struct xfs_perag	*pag = cur->bc_ag.pag;
89 	int			error;
90 	xfs_agblock_t		bno;
91 
92 	/* Allocate the new block from the freelist. If we can't, give up.  */
93 	error = xfs_alloc_get_freelist(pag, cur->bc_tp, cur->bc_ag.agbp,
94 				       &bno, 1);
95 	if (error)
96 		return error;
97 
98 	trace_xfs_rmapbt_alloc_block(cur->bc_mp, pag->pag_agno, bno, 1);
99 	if (bno == NULLAGBLOCK) {
100 		*stat = 0;
101 		return 0;
102 	}
103 
104 	xfs_extent_busy_reuse(cur->bc_mp, pag, bno, 1, false);
105 
106 	new->s = cpu_to_be32(bno);
107 	be32_add_cpu(&agf->agf_rmap_blocks, 1);
108 	xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_RMAP_BLOCKS);
109 
110 	xfs_ag_resv_rmapbt_alloc(cur->bc_mp, pag->pag_agno);
111 
112 	*stat = 1;
113 	return 0;
114 }
115 
116 STATIC int
117 xfs_rmapbt_free_block(
118 	struct xfs_btree_cur	*cur,
119 	struct xfs_buf		*bp)
120 {
121 	struct xfs_buf		*agbp = cur->bc_ag.agbp;
122 	struct xfs_agf		*agf = agbp->b_addr;
123 	struct xfs_perag	*pag = cur->bc_ag.pag;
124 	xfs_agblock_t		bno;
125 	int			error;
126 
127 	bno = xfs_daddr_to_agbno(cur->bc_mp, xfs_buf_daddr(bp));
128 	trace_xfs_rmapbt_free_block(cur->bc_mp, pag->pag_agno,
129 			bno, 1);
130 	be32_add_cpu(&agf->agf_rmap_blocks, -1);
131 	xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_RMAP_BLOCKS);
132 	error = xfs_alloc_put_freelist(pag, cur->bc_tp, agbp, NULL, bno, 1);
133 	if (error)
134 		return error;
135 
136 	xfs_extent_busy_insert(cur->bc_tp, pag, bno, 1,
137 			      XFS_EXTENT_BUSY_SKIP_DISCARD);
138 
139 	xfs_ag_resv_free_extent(pag, XFS_AG_RESV_RMAPBT, NULL, 1);
140 	return 0;
141 }
142 
143 STATIC int
144 xfs_rmapbt_get_minrecs(
145 	struct xfs_btree_cur	*cur,
146 	int			level)
147 {
148 	return cur->bc_mp->m_rmap_mnr[level != 0];
149 }
150 
151 STATIC int
152 xfs_rmapbt_get_maxrecs(
153 	struct xfs_btree_cur	*cur,
154 	int			level)
155 {
156 	return cur->bc_mp->m_rmap_mxr[level != 0];
157 }
158 
159 STATIC void
160 xfs_rmapbt_init_key_from_rec(
161 	union xfs_btree_key		*key,
162 	const union xfs_btree_rec	*rec)
163 {
164 	key->rmap.rm_startblock = rec->rmap.rm_startblock;
165 	key->rmap.rm_owner = rec->rmap.rm_owner;
166 	key->rmap.rm_offset = rec->rmap.rm_offset;
167 }
168 
169 /*
170  * The high key for a reverse mapping record can be computed by shifting
171  * the startblock and offset to the highest value that would still map
172  * to that record.  In practice this means that we add blockcount-1 to
173  * the startblock for all records, and if the record is for a data/attr
174  * fork mapping, we add blockcount-1 to the offset too.
175  */
176 STATIC void
177 xfs_rmapbt_init_high_key_from_rec(
178 	union xfs_btree_key		*key,
179 	const union xfs_btree_rec	*rec)
180 {
181 	uint64_t			off;
182 	int				adj;
183 
184 	adj = be32_to_cpu(rec->rmap.rm_blockcount) - 1;
185 
186 	key->rmap.rm_startblock = rec->rmap.rm_startblock;
187 	be32_add_cpu(&key->rmap.rm_startblock, adj);
188 	key->rmap.rm_owner = rec->rmap.rm_owner;
189 	key->rmap.rm_offset = rec->rmap.rm_offset;
190 	if (XFS_RMAP_NON_INODE_OWNER(be64_to_cpu(rec->rmap.rm_owner)) ||
191 	    XFS_RMAP_IS_BMBT_BLOCK(be64_to_cpu(rec->rmap.rm_offset)))
192 		return;
193 	off = be64_to_cpu(key->rmap.rm_offset);
194 	off = (XFS_RMAP_OFF(off) + adj) | (off & ~XFS_RMAP_OFF_MASK);
195 	key->rmap.rm_offset = cpu_to_be64(off);
196 }
197 
198 STATIC void
199 xfs_rmapbt_init_rec_from_cur(
200 	struct xfs_btree_cur	*cur,
201 	union xfs_btree_rec	*rec)
202 {
203 	rec->rmap.rm_startblock = cpu_to_be32(cur->bc_rec.r.rm_startblock);
204 	rec->rmap.rm_blockcount = cpu_to_be32(cur->bc_rec.r.rm_blockcount);
205 	rec->rmap.rm_owner = cpu_to_be64(cur->bc_rec.r.rm_owner);
206 	rec->rmap.rm_offset = cpu_to_be64(
207 			xfs_rmap_irec_offset_pack(&cur->bc_rec.r));
208 }
209 
210 STATIC void
211 xfs_rmapbt_init_ptr_from_cur(
212 	struct xfs_btree_cur	*cur,
213 	union xfs_btree_ptr	*ptr)
214 {
215 	struct xfs_agf		*agf = cur->bc_ag.agbp->b_addr;
216 
217 	ASSERT(cur->bc_ag.pag->pag_agno == be32_to_cpu(agf->agf_seqno));
218 
219 	ptr->s = agf->agf_roots[cur->bc_btnum];
220 }
221 
222 STATIC int64_t
223 xfs_rmapbt_key_diff(
224 	struct xfs_btree_cur		*cur,
225 	const union xfs_btree_key	*key)
226 {
227 	struct xfs_rmap_irec		*rec = &cur->bc_rec.r;
228 	const struct xfs_rmap_key	*kp = &key->rmap;
229 	__u64				x, y;
230 	int64_t				d;
231 
232 	d = (int64_t)be32_to_cpu(kp->rm_startblock) - rec->rm_startblock;
233 	if (d)
234 		return d;
235 
236 	x = be64_to_cpu(kp->rm_owner);
237 	y = rec->rm_owner;
238 	if (x > y)
239 		return 1;
240 	else if (y > x)
241 		return -1;
242 
243 	x = XFS_RMAP_OFF(be64_to_cpu(kp->rm_offset));
244 	y = rec->rm_offset;
245 	if (x > y)
246 		return 1;
247 	else if (y > x)
248 		return -1;
249 	return 0;
250 }
251 
252 STATIC int64_t
253 xfs_rmapbt_diff_two_keys(
254 	struct xfs_btree_cur		*cur,
255 	const union xfs_btree_key	*k1,
256 	const union xfs_btree_key	*k2)
257 {
258 	const struct xfs_rmap_key	*kp1 = &k1->rmap;
259 	const struct xfs_rmap_key	*kp2 = &k2->rmap;
260 	int64_t				d;
261 	__u64				x, y;
262 
263 	d = (int64_t)be32_to_cpu(kp1->rm_startblock) -
264 		       be32_to_cpu(kp2->rm_startblock);
265 	if (d)
266 		return d;
267 
268 	x = be64_to_cpu(kp1->rm_owner);
269 	y = be64_to_cpu(kp2->rm_owner);
270 	if (x > y)
271 		return 1;
272 	else if (y > x)
273 		return -1;
274 
275 	x = XFS_RMAP_OFF(be64_to_cpu(kp1->rm_offset));
276 	y = XFS_RMAP_OFF(be64_to_cpu(kp2->rm_offset));
277 	if (x > y)
278 		return 1;
279 	else if (y > x)
280 		return -1;
281 	return 0;
282 }
283 
284 static xfs_failaddr_t
285 xfs_rmapbt_verify(
286 	struct xfs_buf		*bp)
287 {
288 	struct xfs_mount	*mp = bp->b_mount;
289 	struct xfs_btree_block	*block = XFS_BUF_TO_BLOCK(bp);
290 	struct xfs_perag	*pag = bp->b_pag;
291 	xfs_failaddr_t		fa;
292 	unsigned int		level;
293 
294 	/*
295 	 * magic number and level verification
296 	 *
297 	 * During growfs operations, we can't verify the exact level or owner as
298 	 * the perag is not fully initialised and hence not attached to the
299 	 * buffer.  In this case, check against the maximum tree depth.
300 	 *
301 	 * Similarly, during log recovery we will have a perag structure
302 	 * attached, but the agf information will not yet have been initialised
303 	 * from the on disk AGF. Again, we can only check against maximum limits
304 	 * in this case.
305 	 */
306 	if (!xfs_verify_magic(bp, block->bb_magic))
307 		return __this_address;
308 
309 	if (!xfs_has_rmapbt(mp))
310 		return __this_address;
311 	fa = xfs_btree_sblock_v5hdr_verify(bp);
312 	if (fa)
313 		return fa;
314 
315 	level = be16_to_cpu(block->bb_level);
316 	if (pag && xfs_perag_initialised_agf(pag)) {
317 		if (level >= pag->pagf_levels[XFS_BTNUM_RMAPi])
318 			return __this_address;
319 	} else if (level >= mp->m_rmap_maxlevels)
320 		return __this_address;
321 
322 	return xfs_btree_sblock_verify(bp, mp->m_rmap_mxr[level != 0]);
323 }
324 
325 static void
326 xfs_rmapbt_read_verify(
327 	struct xfs_buf	*bp)
328 {
329 	xfs_failaddr_t	fa;
330 
331 	if (!xfs_btree_sblock_verify_crc(bp))
332 		xfs_verifier_error(bp, -EFSBADCRC, __this_address);
333 	else {
334 		fa = xfs_rmapbt_verify(bp);
335 		if (fa)
336 			xfs_verifier_error(bp, -EFSCORRUPTED, fa);
337 	}
338 
339 	if (bp->b_error)
340 		trace_xfs_btree_corrupt(bp, _RET_IP_);
341 }
342 
343 static void
344 xfs_rmapbt_write_verify(
345 	struct xfs_buf	*bp)
346 {
347 	xfs_failaddr_t	fa;
348 
349 	fa = xfs_rmapbt_verify(bp);
350 	if (fa) {
351 		trace_xfs_btree_corrupt(bp, _RET_IP_);
352 		xfs_verifier_error(bp, -EFSCORRUPTED, fa);
353 		return;
354 	}
355 	xfs_btree_sblock_calc_crc(bp);
356 
357 }
358 
359 const struct xfs_buf_ops xfs_rmapbt_buf_ops = {
360 	.name			= "xfs_rmapbt",
361 	.magic			= { 0, cpu_to_be32(XFS_RMAP_CRC_MAGIC) },
362 	.verify_read		= xfs_rmapbt_read_verify,
363 	.verify_write		= xfs_rmapbt_write_verify,
364 	.verify_struct		= xfs_rmapbt_verify,
365 };
366 
367 STATIC int
368 xfs_rmapbt_keys_inorder(
369 	struct xfs_btree_cur		*cur,
370 	const union xfs_btree_key	*k1,
371 	const union xfs_btree_key	*k2)
372 {
373 	uint32_t		x;
374 	uint32_t		y;
375 	uint64_t		a;
376 	uint64_t		b;
377 
378 	x = be32_to_cpu(k1->rmap.rm_startblock);
379 	y = be32_to_cpu(k2->rmap.rm_startblock);
380 	if (x < y)
381 		return 1;
382 	else if (x > y)
383 		return 0;
384 	a = be64_to_cpu(k1->rmap.rm_owner);
385 	b = be64_to_cpu(k2->rmap.rm_owner);
386 	if (a < b)
387 		return 1;
388 	else if (a > b)
389 		return 0;
390 	a = XFS_RMAP_OFF(be64_to_cpu(k1->rmap.rm_offset));
391 	b = XFS_RMAP_OFF(be64_to_cpu(k2->rmap.rm_offset));
392 	if (a <= b)
393 		return 1;
394 	return 0;
395 }
396 
397 STATIC int
398 xfs_rmapbt_recs_inorder(
399 	struct xfs_btree_cur		*cur,
400 	const union xfs_btree_rec	*r1,
401 	const union xfs_btree_rec	*r2)
402 {
403 	uint32_t		x;
404 	uint32_t		y;
405 	uint64_t		a;
406 	uint64_t		b;
407 
408 	x = be32_to_cpu(r1->rmap.rm_startblock);
409 	y = be32_to_cpu(r2->rmap.rm_startblock);
410 	if (x < y)
411 		return 1;
412 	else if (x > y)
413 		return 0;
414 	a = be64_to_cpu(r1->rmap.rm_owner);
415 	b = be64_to_cpu(r2->rmap.rm_owner);
416 	if (a < b)
417 		return 1;
418 	else if (a > b)
419 		return 0;
420 	a = XFS_RMAP_OFF(be64_to_cpu(r1->rmap.rm_offset));
421 	b = XFS_RMAP_OFF(be64_to_cpu(r2->rmap.rm_offset));
422 	if (a <= b)
423 		return 1;
424 	return 0;
425 }
426 
427 static const struct xfs_btree_ops xfs_rmapbt_ops = {
428 	.rec_len		= sizeof(struct xfs_rmap_rec),
429 	.key_len		= 2 * sizeof(struct xfs_rmap_key),
430 
431 	.dup_cursor		= xfs_rmapbt_dup_cursor,
432 	.set_root		= xfs_rmapbt_set_root,
433 	.alloc_block		= xfs_rmapbt_alloc_block,
434 	.free_block		= xfs_rmapbt_free_block,
435 	.get_minrecs		= xfs_rmapbt_get_minrecs,
436 	.get_maxrecs		= xfs_rmapbt_get_maxrecs,
437 	.init_key_from_rec	= xfs_rmapbt_init_key_from_rec,
438 	.init_high_key_from_rec	= xfs_rmapbt_init_high_key_from_rec,
439 	.init_rec_from_cur	= xfs_rmapbt_init_rec_from_cur,
440 	.init_ptr_from_cur	= xfs_rmapbt_init_ptr_from_cur,
441 	.key_diff		= xfs_rmapbt_key_diff,
442 	.buf_ops		= &xfs_rmapbt_buf_ops,
443 	.diff_two_keys		= xfs_rmapbt_diff_two_keys,
444 	.keys_inorder		= xfs_rmapbt_keys_inorder,
445 	.recs_inorder		= xfs_rmapbt_recs_inorder,
446 };
447 
448 static struct xfs_btree_cur *
449 xfs_rmapbt_init_common(
450 	struct xfs_mount	*mp,
451 	struct xfs_trans	*tp,
452 	struct xfs_perag	*pag)
453 {
454 	struct xfs_btree_cur	*cur;
455 
456 	/* Overlapping btree; 2 keys per pointer. */
457 	cur = xfs_btree_alloc_cursor(mp, tp, XFS_BTNUM_RMAP,
458 			mp->m_rmap_maxlevels, xfs_rmapbt_cur_cache);
459 	cur->bc_flags = XFS_BTREE_CRC_BLOCKS | XFS_BTREE_OVERLAPPING;
460 	cur->bc_statoff = XFS_STATS_CALC_INDEX(xs_rmap_2);
461 	cur->bc_ops = &xfs_rmapbt_ops;
462 
463 	cur->bc_ag.pag = xfs_perag_hold(pag);
464 	return cur;
465 }
466 
467 /* Create a new reverse mapping btree cursor. */
468 struct xfs_btree_cur *
469 xfs_rmapbt_init_cursor(
470 	struct xfs_mount	*mp,
471 	struct xfs_trans	*tp,
472 	struct xfs_buf		*agbp,
473 	struct xfs_perag	*pag)
474 {
475 	struct xfs_agf		*agf = agbp->b_addr;
476 	struct xfs_btree_cur	*cur;
477 
478 	cur = xfs_rmapbt_init_common(mp, tp, pag);
479 	cur->bc_nlevels = be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAP]);
480 	cur->bc_ag.agbp = agbp;
481 	return cur;
482 }
483 
484 /* Create a new reverse mapping btree cursor with a fake root for staging. */
485 struct xfs_btree_cur *
486 xfs_rmapbt_stage_cursor(
487 	struct xfs_mount	*mp,
488 	struct xbtree_afakeroot	*afake,
489 	struct xfs_perag	*pag)
490 {
491 	struct xfs_btree_cur	*cur;
492 
493 	cur = xfs_rmapbt_init_common(mp, NULL, pag);
494 	xfs_btree_stage_afakeroot(cur, afake);
495 	return cur;
496 }
497 
498 /*
499  * Install a new reverse mapping btree root.  Caller is responsible for
500  * invalidating and freeing the old btree blocks.
501  */
502 void
503 xfs_rmapbt_commit_staged_btree(
504 	struct xfs_btree_cur	*cur,
505 	struct xfs_trans	*tp,
506 	struct xfs_buf		*agbp)
507 {
508 	struct xfs_agf		*agf = agbp->b_addr;
509 	struct xbtree_afakeroot	*afake = cur->bc_ag.afake;
510 
511 	ASSERT(cur->bc_flags & XFS_BTREE_STAGING);
512 
513 	agf->agf_roots[cur->bc_btnum] = cpu_to_be32(afake->af_root);
514 	agf->agf_levels[cur->bc_btnum] = cpu_to_be32(afake->af_levels);
515 	agf->agf_rmap_blocks = cpu_to_be32(afake->af_blocks);
516 	xfs_alloc_log_agf(tp, agbp, XFS_AGF_ROOTS | XFS_AGF_LEVELS |
517 				    XFS_AGF_RMAP_BLOCKS);
518 	xfs_btree_commit_afakeroot(cur, tp, agbp, &xfs_rmapbt_ops);
519 }
520 
521 /* Calculate number of records in a reverse mapping btree block. */
522 static inline unsigned int
523 xfs_rmapbt_block_maxrecs(
524 	unsigned int		blocklen,
525 	bool			leaf)
526 {
527 	if (leaf)
528 		return blocklen / sizeof(struct xfs_rmap_rec);
529 	return blocklen /
530 		(2 * sizeof(struct xfs_rmap_key) + sizeof(xfs_rmap_ptr_t));
531 }
532 
533 /*
534  * Calculate number of records in an rmap btree block.
535  */
536 int
537 xfs_rmapbt_maxrecs(
538 	int			blocklen,
539 	int			leaf)
540 {
541 	blocklen -= XFS_RMAP_BLOCK_LEN;
542 	return xfs_rmapbt_block_maxrecs(blocklen, leaf);
543 }
544 
545 /* Compute the max possible height for reverse mapping btrees. */
546 unsigned int
547 xfs_rmapbt_maxlevels_ondisk(void)
548 {
549 	unsigned int		minrecs[2];
550 	unsigned int		blocklen;
551 
552 	blocklen = XFS_MIN_CRC_BLOCKSIZE - XFS_BTREE_SBLOCK_CRC_LEN;
553 
554 	minrecs[0] = xfs_rmapbt_block_maxrecs(blocklen, true) / 2;
555 	minrecs[1] = xfs_rmapbt_block_maxrecs(blocklen, false) / 2;
556 
557 	/*
558 	 * Compute the asymptotic maxlevels for an rmapbt on any reflink fs.
559 	 *
560 	 * On a reflink filesystem, each AG block can have up to 2^32 (per the
561 	 * refcount record format) owners, which means that theoretically we
562 	 * could face up to 2^64 rmap records.  However, we're likely to run
563 	 * out of blocks in the AG long before that happens, which means that
564 	 * we must compute the max height based on what the btree will look
565 	 * like if it consumes almost all the blocks in the AG due to maximal
566 	 * sharing factor.
567 	 */
568 	return xfs_btree_space_to_height(minrecs, XFS_MAX_CRC_AG_BLOCKS);
569 }
570 
571 /* Compute the maximum height of an rmap btree. */
572 void
573 xfs_rmapbt_compute_maxlevels(
574 	struct xfs_mount		*mp)
575 {
576 	if (!xfs_has_rmapbt(mp)) {
577 		mp->m_rmap_maxlevels = 0;
578 		return;
579 	}
580 
581 	if (xfs_has_reflink(mp)) {
582 		/*
583 		 * Compute the asymptotic maxlevels for an rmap btree on a
584 		 * filesystem that supports reflink.
585 		 *
586 		 * On a reflink filesystem, each AG block can have up to 2^32
587 		 * (per the refcount record format) owners, which means that
588 		 * theoretically we could face up to 2^64 rmap records.
589 		 * However, we're likely to run out of blocks in the AG long
590 		 * before that happens, which means that we must compute the
591 		 * max height based on what the btree will look like if it
592 		 * consumes almost all the blocks in the AG due to maximal
593 		 * sharing factor.
594 		 */
595 		mp->m_rmap_maxlevels = xfs_btree_space_to_height(mp->m_rmap_mnr,
596 				mp->m_sb.sb_agblocks);
597 	} else {
598 		/*
599 		 * If there's no block sharing, compute the maximum rmapbt
600 		 * height assuming one rmap record per AG block.
601 		 */
602 		mp->m_rmap_maxlevels = xfs_btree_compute_maxlevels(
603 				mp->m_rmap_mnr, mp->m_sb.sb_agblocks);
604 	}
605 	ASSERT(mp->m_rmap_maxlevels <= xfs_rmapbt_maxlevels_ondisk());
606 }
607 
608 /* Calculate the refcount btree size for some records. */
609 xfs_extlen_t
610 xfs_rmapbt_calc_size(
611 	struct xfs_mount	*mp,
612 	unsigned long long	len)
613 {
614 	return xfs_btree_calc_size(mp->m_rmap_mnr, len);
615 }
616 
617 /*
618  * Calculate the maximum refcount btree size.
619  */
620 xfs_extlen_t
621 xfs_rmapbt_max_size(
622 	struct xfs_mount	*mp,
623 	xfs_agblock_t		agblocks)
624 {
625 	/* Bail out if we're uninitialized, which can happen in mkfs. */
626 	if (mp->m_rmap_mxr[0] == 0)
627 		return 0;
628 
629 	return xfs_rmapbt_calc_size(mp, agblocks);
630 }
631 
632 /*
633  * Figure out how many blocks to reserve and how many are used by this btree.
634  */
635 int
636 xfs_rmapbt_calc_reserves(
637 	struct xfs_mount	*mp,
638 	struct xfs_trans	*tp,
639 	struct xfs_perag	*pag,
640 	xfs_extlen_t		*ask,
641 	xfs_extlen_t		*used)
642 {
643 	struct xfs_buf		*agbp;
644 	struct xfs_agf		*agf;
645 	xfs_agblock_t		agblocks;
646 	xfs_extlen_t		tree_len;
647 	int			error;
648 
649 	if (!xfs_has_rmapbt(mp))
650 		return 0;
651 
652 	error = xfs_alloc_read_agf(pag, tp, 0, &agbp);
653 	if (error)
654 		return error;
655 
656 	agf = agbp->b_addr;
657 	agblocks = be32_to_cpu(agf->agf_length);
658 	tree_len = be32_to_cpu(agf->agf_rmap_blocks);
659 	xfs_trans_brelse(tp, agbp);
660 
661 	/*
662 	 * The log is permanently allocated, so the space it occupies will
663 	 * never be available for the kinds of things that would require btree
664 	 * expansion.  We therefore can pretend the space isn't there.
665 	 */
666 	if (xfs_ag_contains_log(mp, pag->pag_agno))
667 		agblocks -= mp->m_sb.sb_logblocks;
668 
669 	/* Reserve 1% of the AG or enough for 1 block per record. */
670 	*ask += max(agblocks / 100, xfs_rmapbt_max_size(mp, agblocks));
671 	*used += tree_len;
672 
673 	return error;
674 }
675 
676 int __init
677 xfs_rmapbt_init_cur_cache(void)
678 {
679 	xfs_rmapbt_cur_cache = kmem_cache_create("xfs_rmapbt_cur",
680 			xfs_btree_cur_sizeof(xfs_rmapbt_maxlevels_ondisk()),
681 			0, 0, NULL);
682 
683 	if (!xfs_rmapbt_cur_cache)
684 		return -ENOMEM;
685 	return 0;
686 }
687 
688 void
689 xfs_rmapbt_destroy_cur_cache(void)
690 {
691 	kmem_cache_destroy(xfs_rmapbt_cur_cache);
692 	xfs_rmapbt_cur_cache = NULL;
693 }
694