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