]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxfs/xfs_rmap_btree.c
xfs: fix rm_offset flag handling in rmap keys
[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 const struct xfs_btree_ops xfs_rmapbt_ops = {
446 .rec_len = sizeof(struct xfs_rmap_rec),
447 .key_len = 2 * sizeof(struct xfs_rmap_key),
448
449 .dup_cursor = xfs_rmapbt_dup_cursor,
450 .set_root = xfs_rmapbt_set_root,
451 .alloc_block = xfs_rmapbt_alloc_block,
452 .free_block = xfs_rmapbt_free_block,
453 .get_minrecs = xfs_rmapbt_get_minrecs,
454 .get_maxrecs = xfs_rmapbt_get_maxrecs,
455 .init_key_from_rec = xfs_rmapbt_init_key_from_rec,
456 .init_high_key_from_rec = xfs_rmapbt_init_high_key_from_rec,
457 .init_rec_from_cur = xfs_rmapbt_init_rec_from_cur,
458 .init_ptr_from_cur = xfs_rmapbt_init_ptr_from_cur,
459 .key_diff = xfs_rmapbt_key_diff,
460 .buf_ops = &xfs_rmapbt_buf_ops,
461 .diff_two_keys = xfs_rmapbt_diff_two_keys,
462 .keys_inorder = xfs_rmapbt_keys_inorder,
463 .recs_inorder = xfs_rmapbt_recs_inorder,
464 };
465
466 static struct xfs_btree_cur *
467 xfs_rmapbt_init_common(
468 struct xfs_mount *mp,
469 struct xfs_trans *tp,
470 struct xfs_perag *pag)
471 {
472 struct xfs_btree_cur *cur;
473
474 /* Overlapping btree; 2 keys per pointer. */
475 cur = xfs_btree_alloc_cursor(mp, tp, XFS_BTNUM_RMAP,
476 mp->m_rmap_maxlevels, xfs_rmapbt_cur_cache);
477 cur->bc_flags = XFS_BTREE_CRC_BLOCKS | XFS_BTREE_OVERLAPPING;
478 cur->bc_statoff = XFS_STATS_CALC_INDEX(xs_rmap_2);
479 cur->bc_ops = &xfs_rmapbt_ops;
480
481 cur->bc_ag.pag = xfs_perag_hold(pag);
482 return cur;
483 }
484
485 /* Create a new reverse mapping btree cursor. */
486 struct xfs_btree_cur *
487 xfs_rmapbt_init_cursor(
488 struct xfs_mount *mp,
489 struct xfs_trans *tp,
490 struct xfs_buf *agbp,
491 struct xfs_perag *pag)
492 {
493 struct xfs_agf *agf = agbp->b_addr;
494 struct xfs_btree_cur *cur;
495
496 cur = xfs_rmapbt_init_common(mp, tp, pag);
497 cur->bc_nlevels = be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAP]);
498 cur->bc_ag.agbp = agbp;
499 return cur;
500 }
501
502 /* Create a new reverse mapping btree cursor with a fake root for staging. */
503 struct xfs_btree_cur *
504 xfs_rmapbt_stage_cursor(
505 struct xfs_mount *mp,
506 struct xbtree_afakeroot *afake,
507 struct xfs_perag *pag)
508 {
509 struct xfs_btree_cur *cur;
510
511 cur = xfs_rmapbt_init_common(mp, NULL, pag);
512 xfs_btree_stage_afakeroot(cur, afake);
513 return cur;
514 }
515
516 /*
517 * Install a new reverse mapping btree root. Caller is responsible for
518 * invalidating and freeing the old btree blocks.
519 */
520 void
521 xfs_rmapbt_commit_staged_btree(
522 struct xfs_btree_cur *cur,
523 struct xfs_trans *tp,
524 struct xfs_buf *agbp)
525 {
526 struct xfs_agf *agf = agbp->b_addr;
527 struct xbtree_afakeroot *afake = cur->bc_ag.afake;
528
529 ASSERT(cur->bc_flags & XFS_BTREE_STAGING);
530
531 agf->agf_roots[cur->bc_btnum] = cpu_to_be32(afake->af_root);
532 agf->agf_levels[cur->bc_btnum] = cpu_to_be32(afake->af_levels);
533 agf->agf_rmap_blocks = cpu_to_be32(afake->af_blocks);
534 xfs_alloc_log_agf(tp, agbp, XFS_AGF_ROOTS | XFS_AGF_LEVELS |
535 XFS_AGF_RMAP_BLOCKS);
536 xfs_btree_commit_afakeroot(cur, tp, agbp, &xfs_rmapbt_ops);
537 }
538
539 /* Calculate number of records in a reverse mapping btree block. */
540 static inline unsigned int
541 xfs_rmapbt_block_maxrecs(
542 unsigned int blocklen,
543 bool leaf)
544 {
545 if (leaf)
546 return blocklen / sizeof(struct xfs_rmap_rec);
547 return blocklen /
548 (2 * sizeof(struct xfs_rmap_key) + sizeof(xfs_rmap_ptr_t));
549 }
550
551 /*
552 * Calculate number of records in an rmap btree block.
553 */
554 int
555 xfs_rmapbt_maxrecs(
556 int blocklen,
557 int leaf)
558 {
559 blocklen -= XFS_RMAP_BLOCK_LEN;
560 return xfs_rmapbt_block_maxrecs(blocklen, leaf);
561 }
562
563 /* Compute the max possible height for reverse mapping btrees. */
564 unsigned int
565 xfs_rmapbt_maxlevels_ondisk(void)
566 {
567 unsigned int minrecs[2];
568 unsigned int blocklen;
569
570 blocklen = XFS_MIN_CRC_BLOCKSIZE - XFS_BTREE_SBLOCK_CRC_LEN;
571
572 minrecs[0] = xfs_rmapbt_block_maxrecs(blocklen, true) / 2;
573 minrecs[1] = xfs_rmapbt_block_maxrecs(blocklen, false) / 2;
574
575 /*
576 * Compute the asymptotic maxlevels for an rmapbt on any reflink fs.
577 *
578 * On a reflink filesystem, each AG block can have up to 2^32 (per the
579 * refcount record format) owners, which means that theoretically we
580 * could face up to 2^64 rmap records. However, we're likely to run
581 * out of blocks in the AG long before that happens, which means that
582 * we must compute the max height based on what the btree will look
583 * like if it consumes almost all the blocks in the AG due to maximal
584 * sharing factor.
585 */
586 return xfs_btree_space_to_height(minrecs, XFS_MAX_CRC_AG_BLOCKS);
587 }
588
589 /* Compute the maximum height of an rmap btree. */
590 void
591 xfs_rmapbt_compute_maxlevels(
592 struct xfs_mount *mp)
593 {
594 if (!xfs_has_rmapbt(mp)) {
595 mp->m_rmap_maxlevels = 0;
596 return;
597 }
598
599 if (xfs_has_reflink(mp)) {
600 /*
601 * Compute the asymptotic maxlevels for an rmap btree on a
602 * filesystem that supports reflink.
603 *
604 * On a reflink filesystem, each AG block can have up to 2^32
605 * (per the refcount record format) owners, which means that
606 * theoretically we could face up to 2^64 rmap records.
607 * However, we're likely to run out of blocks in the AG long
608 * before that happens, which means that we must compute the
609 * max height based on what the btree will look like if it
610 * consumes almost all the blocks in the AG due to maximal
611 * sharing factor.
612 */
613 mp->m_rmap_maxlevels = xfs_btree_space_to_height(mp->m_rmap_mnr,
614 mp->m_sb.sb_agblocks);
615 } else {
616 /*
617 * If there's no block sharing, compute the maximum rmapbt
618 * height assuming one rmap record per AG block.
619 */
620 mp->m_rmap_maxlevels = xfs_btree_compute_maxlevels(
621 mp->m_rmap_mnr, mp->m_sb.sb_agblocks);
622 }
623 ASSERT(mp->m_rmap_maxlevels <= xfs_rmapbt_maxlevels_ondisk());
624 }
625
626 /* Calculate the refcount btree size for some records. */
627 xfs_extlen_t
628 xfs_rmapbt_calc_size(
629 struct xfs_mount *mp,
630 unsigned long long len)
631 {
632 return xfs_btree_calc_size(mp->m_rmap_mnr, len);
633 }
634
635 /*
636 * Calculate the maximum refcount btree size.
637 */
638 xfs_extlen_t
639 xfs_rmapbt_max_size(
640 struct xfs_mount *mp,
641 xfs_agblock_t agblocks)
642 {
643 /* Bail out if we're uninitialized, which can happen in mkfs. */
644 if (mp->m_rmap_mxr[0] == 0)
645 return 0;
646
647 return xfs_rmapbt_calc_size(mp, agblocks);
648 }
649
650 /*
651 * Figure out how many blocks to reserve and how many are used by this btree.
652 */
653 int
654 xfs_rmapbt_calc_reserves(
655 struct xfs_mount *mp,
656 struct xfs_trans *tp,
657 struct xfs_perag *pag,
658 xfs_extlen_t *ask,
659 xfs_extlen_t *used)
660 {
661 struct xfs_buf *agbp;
662 struct xfs_agf *agf;
663 xfs_agblock_t agblocks;
664 xfs_extlen_t tree_len;
665 int error;
666
667 if (!xfs_has_rmapbt(mp))
668 return 0;
669
670 error = xfs_alloc_read_agf(pag, tp, 0, &agbp);
671 if (error)
672 return error;
673
674 agf = agbp->b_addr;
675 agblocks = be32_to_cpu(agf->agf_length);
676 tree_len = be32_to_cpu(agf->agf_rmap_blocks);
677 xfs_trans_brelse(tp, agbp);
678
679 /*
680 * The log is permanently allocated, so the space it occupies will
681 * never be available for the kinds of things that would require btree
682 * expansion. We therefore can pretend the space isn't there.
683 */
684 if (xfs_ag_contains_log(mp, pag->pag_agno))
685 agblocks -= mp->m_sb.sb_logblocks;
686
687 /* Reserve 1% of the AG or enough for 1 block per record. */
688 *ask += max(agblocks / 100, xfs_rmapbt_max_size(mp, agblocks));
689 *used += tree_len;
690
691 return error;
692 }
693
694 int __init
695 xfs_rmapbt_init_cur_cache(void)
696 {
697 xfs_rmapbt_cur_cache = kmem_cache_create("xfs_rmapbt_cur",
698 xfs_btree_cur_sizeof(xfs_rmapbt_maxlevels_ondisk()),
699 0, 0, NULL);
700
701 if (!xfs_rmapbt_cur_cache)
702 return -ENOMEM;
703 return 0;
704 }
705
706 void
707 xfs_rmapbt_destroy_cur_cache(void)
708 {
709 kmem_cache_destroy(xfs_rmapbt_cur_cache);
710 xfs_rmapbt_cur_cache = NULL;
711 }