]>
Commit | Line | Data |
---|---|---|
37b3b4d6 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
d8079fe0 DW |
2 | /* |
3 | * Copyright (C) 2016 Oracle. All Rights Reserved. | |
d8079fe0 | 4 | * Author: Darrick J. Wong <darrick.wong@oracle.com> |
d8079fe0 DW |
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" | |
d8079fe0 DW |
12 | #include "xfs_mount.h" |
13 | #include "xfs_btree.h" | |
c3302cbc | 14 | #include "xfs_btree_staging.h" |
d8079fe0 | 15 | #include "xfs_refcount_btree.h" |
6b2f464d | 16 | #include "xfs_refcount.h" |
d8079fe0 DW |
17 | #include "xfs_alloc.h" |
18 | #include "xfs_trace.h" | |
d8079fe0 | 19 | #include "xfs_trans.h" |
2cf10e4c | 20 | #include "xfs_bit.h" |
bc859611 | 21 | #include "xfs_rmap.h" |
f93d2173 | 22 | #include "xfs_ag.h" |
d8079fe0 | 23 | |
5c35b317 | 24 | static struct kmem_cache *xfs_refcountbt_cur_cache; |
7d10d094 | 25 | |
d8079fe0 DW |
26 | static struct xfs_btree_cur * |
27 | xfs_refcountbt_dup_cursor( | |
28 | struct xfs_btree_cur *cur) | |
29 | { | |
30 | return xfs_refcountbt_init_cursor(cur->bc_mp, cur->bc_tp, | |
971fceb4 | 31 | cur->bc_ag.agbp, cur->bc_ag.pag); |
d8079fe0 DW |
32 | } |
33 | ||
bc859611 DW |
34 | STATIC void |
35 | xfs_refcountbt_set_root( | |
67e6075e DW |
36 | struct xfs_btree_cur *cur, |
37 | const union xfs_btree_ptr *ptr, | |
38 | int inc) | |
bc859611 | 39 | { |
aa6fa37f | 40 | struct xfs_buf *agbp = cur->bc_ag.agbp; |
0bc284c2 | 41 | struct xfs_agf *agf = agbp->b_addr; |
10c0a390 | 42 | struct xfs_perag *pag = agbp->b_pag; |
bc859611 DW |
43 | |
44 | ASSERT(ptr->s != 0); | |
45 | ||
46 | agf->agf_refcount_root = ptr->s; | |
47 | be32_add_cpu(&agf->agf_refcount_level, inc); | |
48 | pag->pagf_refcount_level += inc; | |
bc859611 DW |
49 | |
50 | xfs_alloc_log_agf(cur->bc_tp, agbp, | |
51 | XFS_AGF_REFCOUNT_ROOT | XFS_AGF_REFCOUNT_LEVEL); | |
52 | } | |
53 | ||
54 | STATIC int | |
55 | xfs_refcountbt_alloc_block( | |
43cbf380 DW |
56 | struct xfs_btree_cur *cur, |
57 | const union xfs_btree_ptr *start, | |
58 | union xfs_btree_ptr *new, | |
59 | int *stat) | |
bc859611 | 60 | { |
aa6fa37f | 61 | struct xfs_buf *agbp = cur->bc_ag.agbp; |
0bc284c2 | 62 | struct xfs_agf *agf = agbp->b_addr; |
bc859611 DW |
63 | struct xfs_alloc_arg args; /* block allocation args */ |
64 | int error; /* error return value */ | |
65 | ||
66 | memset(&args, 0, sizeof(args)); | |
67 | args.tp = cur->bc_tp; | |
68 | args.mp = cur->bc_mp; | |
04215b9f | 69 | args.pag = cur->bc_ag.pag; |
007347e3 | 70 | args.oinfo = XFS_RMAP_OINFO_REFC; |
bc859611 | 71 | args.minlen = args.maxlen = args.prod = 1; |
02cc8b2a | 72 | args.resv = XFS_AG_RESV_METADATA; |
bc859611 | 73 | |
15653695 DC |
74 | error = xfs_alloc_vextent_near_bno(&args, |
75 | XFS_AGB_TO_FSB(args.mp, args.pag->pag_agno, | |
76 | xfs_refc_block(args.mp))); | |
bc859611 DW |
77 | if (error) |
78 | goto out_error; | |
a0577dbb | 79 | trace_xfs_refcountbt_alloc_block(cur->bc_mp, cur->bc_ag.pag->pag_agno, |
bc859611 DW |
80 | args.agbno, 1); |
81 | if (args.fsbno == NULLFSBLOCK) { | |
bc859611 DW |
82 | *stat = 0; |
83 | return 0; | |
84 | } | |
a0577dbb | 85 | ASSERT(args.agno == cur->bc_ag.pag->pag_agno); |
bc859611 DW |
86 | ASSERT(args.len == 1); |
87 | ||
88 | new->s = cpu_to_be32(args.agbno); | |
89 | be32_add_cpu(&agf->agf_refcount_blocks, 1); | |
90 | xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_REFCOUNT_BLOCKS); | |
91 | ||
bc859611 DW |
92 | *stat = 1; |
93 | return 0; | |
94 | ||
95 | out_error: | |
bc859611 DW |
96 | return error; |
97 | } | |
98 | ||
99 | STATIC int | |
100 | xfs_refcountbt_free_block( | |
101 | struct xfs_btree_cur *cur, | |
102 | struct xfs_buf *bp) | |
103 | { | |
104 | struct xfs_mount *mp = cur->bc_mp; | |
aa6fa37f | 105 | struct xfs_buf *agbp = cur->bc_ag.agbp; |
0bc284c2 | 106 | struct xfs_agf *agf = agbp->b_addr; |
d4aaa66b | 107 | xfs_fsblock_t fsbno = XFS_DADDR_TO_FSB(mp, xfs_buf_daddr(bp)); |
bc859611 | 108 | |
a0577dbb | 109 | trace_xfs_refcountbt_free_block(cur->bc_mp, cur->bc_ag.pag->pag_agno, |
bc859611 | 110 | XFS_FSB_TO_AGBNO(cur->bc_mp, fsbno), 1); |
bc859611 DW |
111 | be32_add_cpu(&agf->agf_refcount_blocks, -1); |
112 | xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_REFCOUNT_BLOCKS); | |
ef16737e | 113 | return xfs_free_extent_later(cur->bc_tp, fsbno, 1, |
42c1e5c1 | 114 | &XFS_RMAP_OINFO_REFC, XFS_AG_RESV_METADATA); |
bc859611 DW |
115 | } |
116 | ||
117 | STATIC int | |
118 | xfs_refcountbt_get_minrecs( | |
119 | struct xfs_btree_cur *cur, | |
120 | int level) | |
121 | { | |
122 | return cur->bc_mp->m_refc_mnr[level != 0]; | |
123 | } | |
124 | ||
125 | STATIC int | |
126 | xfs_refcountbt_get_maxrecs( | |
127 | struct xfs_btree_cur *cur, | |
128 | int level) | |
129 | { | |
130 | return cur->bc_mp->m_refc_mxr[level != 0]; | |
131 | } | |
132 | ||
133 | STATIC void | |
134 | xfs_refcountbt_init_key_from_rec( | |
c65978b6 DW |
135 | union xfs_btree_key *key, |
136 | const union xfs_btree_rec *rec) | |
bc859611 DW |
137 | { |
138 | key->refc.rc_startblock = rec->refc.rc_startblock; | |
139 | } | |
140 | ||
141 | STATIC void | |
142 | xfs_refcountbt_init_high_key_from_rec( | |
c65978b6 DW |
143 | union xfs_btree_key *key, |
144 | const union xfs_btree_rec *rec) | |
bc859611 | 145 | { |
c65978b6 | 146 | __u32 x; |
bc859611 DW |
147 | |
148 | x = be32_to_cpu(rec->refc.rc_startblock); | |
149 | x += be32_to_cpu(rec->refc.rc_blockcount) - 1; | |
150 | key->refc.rc_startblock = cpu_to_be32(x); | |
151 | } | |
152 | ||
153 | STATIC void | |
154 | xfs_refcountbt_init_rec_from_cur( | |
155 | struct xfs_btree_cur *cur, | |
156 | union xfs_btree_rec *rec) | |
157 | { | |
6b2f464d DW |
158 | const struct xfs_refcount_irec *irec = &cur->bc_rec.rc; |
159 | uint32_t start; | |
160 | ||
161 | start = xfs_refcount_encode_startblock(irec->rc_startblock, | |
162 | irec->rc_domain); | |
163 | rec->refc.rc_startblock = cpu_to_be32(start); | |
bc859611 DW |
164 | rec->refc.rc_blockcount = cpu_to_be32(cur->bc_rec.rc.rc_blockcount); |
165 | rec->refc.rc_refcount = cpu_to_be32(cur->bc_rec.rc.rc_refcount); | |
166 | } | |
167 | ||
168 | STATIC void | |
169 | xfs_refcountbt_init_ptr_from_cur( | |
170 | struct xfs_btree_cur *cur, | |
171 | union xfs_btree_ptr *ptr) | |
172 | { | |
aa6fa37f | 173 | struct xfs_agf *agf = cur->bc_ag.agbp->b_addr; |
bc859611 | 174 | |
a0577dbb | 175 | ASSERT(cur->bc_ag.pag->pag_agno == be32_to_cpu(agf->agf_seqno)); |
bc859611 DW |
176 | |
177 | ptr->s = agf->agf_refcount_root; | |
178 | } | |
179 | ||
4a492e72 | 180 | STATIC int64_t |
bc859611 | 181 | xfs_refcountbt_key_diff( |
901acb0e DW |
182 | struct xfs_btree_cur *cur, |
183 | const union xfs_btree_key *key) | |
bc859611 | 184 | { |
901acb0e | 185 | const struct xfs_refcount_key *kp = &key->refc; |
6b2f464d DW |
186 | const struct xfs_refcount_irec *irec = &cur->bc_rec.rc; |
187 | uint32_t start; | |
bc859611 | 188 | |
6b2f464d DW |
189 | start = xfs_refcount_encode_startblock(irec->rc_startblock, |
190 | irec->rc_domain); | |
191 | return (int64_t)be32_to_cpu(kp->rc_startblock) - start; | |
bc859611 DW |
192 | } |
193 | ||
4a492e72 | 194 | STATIC int64_t |
bc859611 | 195 | xfs_refcountbt_diff_two_keys( |
901acb0e DW |
196 | struct xfs_btree_cur *cur, |
197 | const union xfs_btree_key *k1, | |
d99b8900 DW |
198 | const union xfs_btree_key *k2, |
199 | const union xfs_btree_key *mask) | |
bc859611 | 200 | { |
d99b8900 DW |
201 | ASSERT(!mask || mask->refc.rc_startblock); |
202 | ||
4a492e72 | 203 | return (int64_t)be32_to_cpu(k1->refc.rc_startblock) - |
d99b8900 | 204 | be32_to_cpu(k2->refc.rc_startblock); |
bc859611 DW |
205 | } |
206 | ||
bc01119d | 207 | STATIC xfs_failaddr_t |
d8079fe0 DW |
208 | xfs_refcountbt_verify( |
209 | struct xfs_buf *bp) | |
210 | { | |
7861ef77 | 211 | struct xfs_mount *mp = bp->b_mount; |
d8079fe0 DW |
212 | struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp); |
213 | struct xfs_perag *pag = bp->b_pag; | |
bc01119d | 214 | xfs_failaddr_t fa; |
d8079fe0 DW |
215 | unsigned int level; |
216 | ||
68dbe77f | 217 | if (!xfs_verify_magic(bp, block->bb_magic)) |
bc01119d | 218 | return __this_address; |
d8079fe0 | 219 | |
b16a427a | 220 | if (!xfs_has_reflink(mp)) |
bc01119d DW |
221 | return __this_address; |
222 | fa = xfs_btree_sblock_v5hdr_verify(bp); | |
223 | if (fa) | |
224 | return fa; | |
d8079fe0 DW |
225 | |
226 | level = be16_to_cpu(block->bb_level); | |
03dc2ef2 | 227 | if (pag && xfs_perag_initialised_agf(pag)) { |
d8079fe0 | 228 | if (level >= pag->pagf_refcount_level) |
bc01119d | 229 | return __this_address; |
d8079fe0 | 230 | } else if (level >= mp->m_refc_maxlevels) |
bc01119d | 231 | return __this_address; |
d8079fe0 DW |
232 | |
233 | return xfs_btree_sblock_verify(bp, mp->m_refc_mxr[level != 0]); | |
234 | } | |
235 | ||
236 | STATIC void | |
237 | xfs_refcountbt_read_verify( | |
238 | struct xfs_buf *bp) | |
239 | { | |
1e697959 DW |
240 | xfs_failaddr_t fa; |
241 | ||
d8079fe0 | 242 | if (!xfs_btree_sblock_verify_crc(bp)) |
1e697959 DW |
243 | xfs_verifier_error(bp, -EFSBADCRC, __this_address); |
244 | else { | |
245 | fa = xfs_refcountbt_verify(bp); | |
246 | if (fa) | |
247 | xfs_verifier_error(bp, -EFSCORRUPTED, fa); | |
248 | } | |
d8079fe0 | 249 | |
7e6c95f1 | 250 | if (bp->b_error) |
d8079fe0 | 251 | trace_xfs_btree_corrupt(bp, _RET_IP_); |
d8079fe0 DW |
252 | } |
253 | ||
254 | STATIC void | |
255 | xfs_refcountbt_write_verify( | |
256 | struct xfs_buf *bp) | |
257 | { | |
1e697959 DW |
258 | xfs_failaddr_t fa; |
259 | ||
260 | fa = xfs_refcountbt_verify(bp); | |
261 | if (fa) { | |
d8079fe0 | 262 | trace_xfs_btree_corrupt(bp, _RET_IP_); |
1e697959 | 263 | xfs_verifier_error(bp, -EFSCORRUPTED, fa); |
d8079fe0 DW |
264 | return; |
265 | } | |
266 | xfs_btree_sblock_calc_crc(bp); | |
267 | ||
268 | } | |
269 | ||
270 | const struct xfs_buf_ops xfs_refcountbt_buf_ops = { | |
271 | .name = "xfs_refcountbt", | |
68dbe77f | 272 | .magic = { 0, cpu_to_be32(XFS_REFC_CRC_MAGIC) }, |
d8079fe0 DW |
273 | .verify_read = xfs_refcountbt_read_verify, |
274 | .verify_write = xfs_refcountbt_write_verify, | |
95d9582b | 275 | .verify_struct = xfs_refcountbt_verify, |
d8079fe0 DW |
276 | }; |
277 | ||
bc859611 DW |
278 | STATIC int |
279 | xfs_refcountbt_keys_inorder( | |
141bbc5c DW |
280 | struct xfs_btree_cur *cur, |
281 | const union xfs_btree_key *k1, | |
282 | const union xfs_btree_key *k2) | |
bc859611 DW |
283 | { |
284 | return be32_to_cpu(k1->refc.rc_startblock) < | |
285 | be32_to_cpu(k2->refc.rc_startblock); | |
286 | } | |
287 | ||
288 | STATIC int | |
289 | xfs_refcountbt_recs_inorder( | |
141bbc5c DW |
290 | struct xfs_btree_cur *cur, |
291 | const union xfs_btree_rec *r1, | |
292 | const union xfs_btree_rec *r2) | |
bc859611 DW |
293 | { |
294 | return be32_to_cpu(r1->refc.rc_startblock) + | |
295 | be32_to_cpu(r1->refc.rc_blockcount) <= | |
296 | be32_to_cpu(r2->refc.rc_startblock); | |
297 | } | |
bc859611 | 298 | |
9ba4dc82 DW |
299 | STATIC enum xbtree_key_contig |
300 | xfs_refcountbt_keys_contiguous( | |
301 | struct xfs_btree_cur *cur, | |
302 | const union xfs_btree_key *key1, | |
d99b8900 DW |
303 | const union xfs_btree_key *key2, |
304 | const union xfs_btree_key *mask) | |
9ba4dc82 | 305 | { |
d99b8900 DW |
306 | ASSERT(!mask || mask->refc.rc_startblock); |
307 | ||
9ba4dc82 DW |
308 | return xbtree_key_contig(be32_to_cpu(key1->refc.rc_startblock), |
309 | be32_to_cpu(key2->refc.rc_startblock)); | |
310 | } | |
311 | ||
d8079fe0 DW |
312 | static const struct xfs_btree_ops xfs_refcountbt_ops = { |
313 | .rec_len = sizeof(struct xfs_refcount_rec), | |
314 | .key_len = sizeof(struct xfs_refcount_key), | |
315 | ||
316 | .dup_cursor = xfs_refcountbt_dup_cursor, | |
bc859611 DW |
317 | .set_root = xfs_refcountbt_set_root, |
318 | .alloc_block = xfs_refcountbt_alloc_block, | |
319 | .free_block = xfs_refcountbt_free_block, | |
320 | .get_minrecs = xfs_refcountbt_get_minrecs, | |
321 | .get_maxrecs = xfs_refcountbt_get_maxrecs, | |
322 | .init_key_from_rec = xfs_refcountbt_init_key_from_rec, | |
323 | .init_high_key_from_rec = xfs_refcountbt_init_high_key_from_rec, | |
324 | .init_rec_from_cur = xfs_refcountbt_init_rec_from_cur, | |
325 | .init_ptr_from_cur = xfs_refcountbt_init_ptr_from_cur, | |
326 | .key_diff = xfs_refcountbt_key_diff, | |
d8079fe0 | 327 | .buf_ops = &xfs_refcountbt_buf_ops, |
bc859611 | 328 | .diff_two_keys = xfs_refcountbt_diff_two_keys, |
bc859611 DW |
329 | .keys_inorder = xfs_refcountbt_keys_inorder, |
330 | .recs_inorder = xfs_refcountbt_recs_inorder, | |
9ba4dc82 | 331 | .keys_contiguous = xfs_refcountbt_keys_contiguous, |
d8079fe0 DW |
332 | }; |
333 | ||
334 | /* | |
c3302cbc | 335 | * Initialize a new refcount btree cursor. |
d8079fe0 | 336 | */ |
c3302cbc DW |
337 | static struct xfs_btree_cur * |
338 | xfs_refcountbt_init_common( | |
d8079fe0 DW |
339 | struct xfs_mount *mp, |
340 | struct xfs_trans *tp, | |
ca7d293d | 341 | struct xfs_perag *pag) |
d8079fe0 | 342 | { |
d8079fe0 DW |
343 | struct xfs_btree_cur *cur; |
344 | ||
971fceb4 | 345 | ASSERT(pag->pag_agno < mp->m_sb.sb_agcount); |
d8079fe0 | 346 | |
22f64346 | 347 | cur = xfs_btree_alloc_cursor(mp, tp, XFS_BTNUM_REFC, |
7d10d094 | 348 | mp->m_refc_maxlevels, xfs_refcountbt_cur_cache); |
5d8acc46 | 349 | cur->bc_statoff = XFS_STATS_CALC_INDEX(xs_refcbt_2); |
d8079fe0 | 350 | |
d8079fe0 | 351 | cur->bc_flags |= XFS_BTREE_CRC_BLOCKS; |
971fceb4 | 352 | |
11e716f4 | 353 | cur->bc_ag.pag = xfs_perag_hold(pag); |
63789af3 DC |
354 | cur->bc_ag.refc.nr_ops = 0; |
355 | cur->bc_ag.refc.shape_changes = 0; | |
c3302cbc DW |
356 | cur->bc_ops = &xfs_refcountbt_ops; |
357 | return cur; | |
358 | } | |
d8079fe0 | 359 | |
c3302cbc DW |
360 | /* Create a btree cursor. */ |
361 | struct xfs_btree_cur * | |
362 | xfs_refcountbt_init_cursor( | |
363 | struct xfs_mount *mp, | |
364 | struct xfs_trans *tp, | |
365 | struct xfs_buf *agbp, | |
ca7d293d | 366 | struct xfs_perag *pag) |
c3302cbc DW |
367 | { |
368 | struct xfs_agf *agf = agbp->b_addr; | |
369 | struct xfs_btree_cur *cur; | |
370 | ||
971fceb4 | 371 | cur = xfs_refcountbt_init_common(mp, tp, pag); |
c3302cbc DW |
372 | cur->bc_nlevels = be32_to_cpu(agf->agf_refcount_level); |
373 | cur->bc_ag.agbp = agbp; | |
374 | return cur; | |
375 | } | |
376 | ||
377 | /* Create a btree cursor with a fake root for staging. */ | |
378 | struct xfs_btree_cur * | |
379 | xfs_refcountbt_stage_cursor( | |
380 | struct xfs_mount *mp, | |
381 | struct xbtree_afakeroot *afake, | |
971fceb4 | 382 | struct xfs_perag *pag) |
c3302cbc DW |
383 | { |
384 | struct xfs_btree_cur *cur; | |
385 | ||
971fceb4 | 386 | cur = xfs_refcountbt_init_common(mp, NULL, pag); |
c3302cbc | 387 | xfs_btree_stage_afakeroot(cur, afake); |
d8079fe0 DW |
388 | return cur; |
389 | } | |
390 | ||
c3302cbc DW |
391 | /* |
392 | * Swap in the new btree root. Once we pass this point the newly rebuilt btree | |
393 | * is in place and we have to kill off all the old btree blocks. | |
394 | */ | |
395 | void | |
396 | xfs_refcountbt_commit_staged_btree( | |
397 | struct xfs_btree_cur *cur, | |
398 | struct xfs_trans *tp, | |
399 | struct xfs_buf *agbp) | |
400 | { | |
401 | struct xfs_agf *agf = agbp->b_addr; | |
402 | struct xbtree_afakeroot *afake = cur->bc_ag.afake; | |
403 | ||
404 | ASSERT(cur->bc_flags & XFS_BTREE_STAGING); | |
405 | ||
406 | agf->agf_refcount_root = cpu_to_be32(afake->af_root); | |
407 | agf->agf_refcount_level = cpu_to_be32(afake->af_levels); | |
408 | agf->agf_refcount_blocks = cpu_to_be32(afake->af_blocks); | |
409 | xfs_alloc_log_agf(tp, agbp, XFS_AGF_REFCOUNT_BLOCKS | | |
410 | XFS_AGF_REFCOUNT_ROOT | | |
411 | XFS_AGF_REFCOUNT_LEVEL); | |
412 | xfs_btree_commit_afakeroot(cur, tp, agbp, &xfs_refcountbt_ops); | |
413 | } | |
414 | ||
441815c7 DW |
415 | /* Calculate number of records in a refcount btree block. */ |
416 | static inline unsigned int | |
417 | xfs_refcountbt_block_maxrecs( | |
418 | unsigned int blocklen, | |
419 | bool leaf) | |
420 | { | |
421 | if (leaf) | |
422 | return blocklen / sizeof(struct xfs_refcount_rec); | |
423 | return blocklen / (sizeof(struct xfs_refcount_key) + | |
424 | sizeof(xfs_refcount_ptr_t)); | |
425 | } | |
426 | ||
d8079fe0 DW |
427 | /* |
428 | * Calculate the number of records in a refcount btree block. | |
429 | */ | |
430 | int | |
431 | xfs_refcountbt_maxrecs( | |
d8079fe0 DW |
432 | int blocklen, |
433 | bool leaf) | |
434 | { | |
435 | blocklen -= XFS_REFCOUNT_BLOCK_LEN; | |
441815c7 DW |
436 | return xfs_refcountbt_block_maxrecs(blocklen, leaf); |
437 | } | |
d8079fe0 | 438 | |
441815c7 DW |
439 | /* Compute the max possible height of the maximally sized refcount btree. */ |
440 | unsigned int | |
441 | xfs_refcountbt_maxlevels_ondisk(void) | |
442 | { | |
443 | unsigned int minrecs[2]; | |
444 | unsigned int blocklen; | |
445 | ||
446 | blocklen = XFS_MIN_CRC_BLOCKSIZE - XFS_BTREE_SBLOCK_CRC_LEN; | |
447 | ||
448 | minrecs[0] = xfs_refcountbt_block_maxrecs(blocklen, true) / 2; | |
449 | minrecs[1] = xfs_refcountbt_block_maxrecs(blocklen, false) / 2; | |
450 | ||
451 | return xfs_btree_compute_maxlevels(minrecs, XFS_MAX_CRC_AG_BLOCKS); | |
d8079fe0 DW |
452 | } |
453 | ||
454 | /* Compute the maximum height of a refcount btree. */ | |
455 | void | |
456 | xfs_refcountbt_compute_maxlevels( | |
457 | struct xfs_mount *mp) | |
458 | { | |
441815c7 DW |
459 | if (!xfs_has_reflink(mp)) { |
460 | mp->m_refc_maxlevels = 0; | |
461 | return; | |
462 | } | |
463 | ||
1421de38 | 464 | mp->m_refc_maxlevels = xfs_btree_compute_maxlevels( |
d8079fe0 | 465 | mp->m_refc_mnr, mp->m_sb.sb_agblocks); |
441815c7 | 466 | ASSERT(mp->m_refc_maxlevels <= xfs_refcountbt_maxlevels_ondisk()); |
d8079fe0 | 467 | } |
02cc8b2a DW |
468 | |
469 | /* Calculate the refcount btree size for some records. */ | |
470 | xfs_extlen_t | |
471 | xfs_refcountbt_calc_size( | |
472 | struct xfs_mount *mp, | |
473 | unsigned long long len) | |
474 | { | |
1421de38 | 475 | return xfs_btree_calc_size(mp->m_refc_mnr, len); |
02cc8b2a DW |
476 | } |
477 | ||
478 | /* | |
479 | * Calculate the maximum refcount btree size. | |
480 | */ | |
481 | xfs_extlen_t | |
482 | xfs_refcountbt_max_size( | |
f21c57ed DW |
483 | struct xfs_mount *mp, |
484 | xfs_agblock_t agblocks) | |
02cc8b2a DW |
485 | { |
486 | /* Bail out if we're uninitialized, which can happen in mkfs. */ | |
487 | if (mp->m_refc_mxr[0] == 0) | |
488 | return 0; | |
489 | ||
f21c57ed | 490 | return xfs_refcountbt_calc_size(mp, agblocks); |
02cc8b2a DW |
491 | } |
492 | ||
493 | /* | |
494 | * Figure out how many blocks to reserve and how many are used by this btree. | |
495 | */ | |
496 | int | |
497 | xfs_refcountbt_calc_reserves( | |
498 | struct xfs_mount *mp, | |
0d802327 | 499 | struct xfs_trans *tp, |
653f37bc | 500 | struct xfs_perag *pag, |
02cc8b2a DW |
501 | xfs_extlen_t *ask, |
502 | xfs_extlen_t *used) | |
503 | { | |
504 | struct xfs_buf *agbp; | |
505 | struct xfs_agf *agf; | |
f21c57ed | 506 | xfs_agblock_t agblocks; |
02cc8b2a DW |
507 | xfs_extlen_t tree_len; |
508 | int error; | |
509 | ||
b16a427a | 510 | if (!xfs_has_reflink(mp)) |
02cc8b2a DW |
511 | return 0; |
512 | ||
f9084bd9 | 513 | error = xfs_alloc_read_agf(pag, tp, 0, &agbp); |
02cc8b2a DW |
514 | if (error) |
515 | return error; | |
516 | ||
0bc284c2 | 517 | agf = agbp->b_addr; |
f21c57ed | 518 | agblocks = be32_to_cpu(agf->agf_length); |
02cc8b2a | 519 | tree_len = be32_to_cpu(agf->agf_refcount_blocks); |
0d802327 | 520 | xfs_trans_brelse(tp, agbp); |
02cc8b2a | 521 | |
247c4999 DW |
522 | /* |
523 | * The log is permanently allocated, so the space it occupies will | |
524 | * never be available for the kinds of things that would require btree | |
525 | * expansion. We therefore can pretend the space isn't there. | |
526 | */ | |
54f6b9e5 | 527 | if (xfs_ag_contains_log(mp, pag->pag_agno)) |
247c4999 DW |
528 | agblocks -= mp->m_sb.sb_logblocks; |
529 | ||
f21c57ed | 530 | *ask += xfs_refcountbt_max_size(mp, agblocks); |
02cc8b2a DW |
531 | *used += tree_len; |
532 | ||
533 | return error; | |
534 | } | |
7d10d094 DW |
535 | |
536 | int __init | |
537 | xfs_refcountbt_init_cur_cache(void) | |
538 | { | |
539 | xfs_refcountbt_cur_cache = kmem_cache_create("xfs_refcbt_cur", | |
540 | xfs_btree_cur_sizeof(xfs_refcountbt_maxlevels_ondisk()), | |
541 | 0, 0, NULL); | |
542 | ||
543 | if (!xfs_refcountbt_cur_cache) | |
544 | return -ENOMEM; | |
545 | return 0; | |
546 | } | |
547 | ||
548 | void | |
549 | xfs_refcountbt_destroy_cur_cache(void) | |
550 | { | |
551 | kmem_cache_destroy(xfs_refcountbt_cur_cache); | |
552 | xfs_refcountbt_cur_cache = NULL; | |
553 | } |