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