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