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_sb.h"
13 #include "xfs_mount.h"
14 #include "xfs_btree.h"
15 #include "xfs_bmap.h"
16 #include "xfs_refcount_btree.h"
17 #include "xfs_alloc.h"
18 #include "xfs_error.h"
19 #include "xfs_trace.h"
20 #include "xfs_cksum.h"
21 #include "xfs_trans.h"
22 #include "xfs_bit.h"
23 #include "xfs_rmap.h"
24 
25 static struct xfs_btree_cur *
26 xfs_refcountbt_dup_cursor(
27 	struct xfs_btree_cur	*cur)
28 {
29 	return xfs_refcountbt_init_cursor(cur->bc_mp, cur->bc_tp,
30 			cur->bc_private.a.agbp, cur->bc_private.a.agno,
31 			cur->bc_private.a.dfops);
32 }
33 
34 STATIC void
35 xfs_refcountbt_set_root(
36 	struct xfs_btree_cur	*cur,
37 	union xfs_btree_ptr	*ptr,
38 	int			inc)
39 {
40 	struct xfs_buf		*agbp = cur->bc_private.a.agbp;
41 	struct xfs_agf		*agf = XFS_BUF_TO_AGF(agbp);
42 	xfs_agnumber_t		seqno = be32_to_cpu(agf->agf_seqno);
43 	struct xfs_perag	*pag = xfs_perag_get(cur->bc_mp, seqno);
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 	xfs_perag_put(pag);
51 
52 	xfs_alloc_log_agf(cur->bc_tp, agbp,
53 			XFS_AGF_REFCOUNT_ROOT | XFS_AGF_REFCOUNT_LEVEL);
54 }
55 
56 STATIC int
57 xfs_refcountbt_alloc_block(
58 	struct xfs_btree_cur	*cur,
59 	union xfs_btree_ptr	*start,
60 	union xfs_btree_ptr	*new,
61 	int			*stat)
62 {
63 	struct xfs_buf		*agbp = cur->bc_private.a.agbp;
64 	struct xfs_agf		*agf = XFS_BUF_TO_AGF(agbp);
65 	struct xfs_alloc_arg	args;		/* block allocation args */
66 	int			error;		/* error return value */
67 
68 	memset(&args, 0, sizeof(args));
69 	args.tp = cur->bc_tp;
70 	args.mp = cur->bc_mp;
71 	args.type = XFS_ALLOCTYPE_NEAR_BNO;
72 	args.fsbno = XFS_AGB_TO_FSB(cur->bc_mp, cur->bc_private.a.agno,
73 			xfs_refc_block(args.mp));
74 	args.firstblock = args.fsbno;
75 	xfs_rmap_ag_owner(&args.oinfo, XFS_RMAP_OWN_REFC);
76 	args.minlen = args.maxlen = args.prod = 1;
77 	args.resv = XFS_AG_RESV_METADATA;
78 
79 	error = xfs_alloc_vextent(&args);
80 	if (error)
81 		goto out_error;
82 	trace_xfs_refcountbt_alloc_block(cur->bc_mp, cur->bc_private.a.agno,
83 			args.agbno, 1);
84 	if (args.fsbno == NULLFSBLOCK) {
85 		*stat = 0;
86 		return 0;
87 	}
88 	ASSERT(args.agno == cur->bc_private.a.agno);
89 	ASSERT(args.len == 1);
90 
91 	new->s = cpu_to_be32(args.agbno);
92 	be32_add_cpu(&agf->agf_refcount_blocks, 1);
93 	xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_REFCOUNT_BLOCKS);
94 
95 	*stat = 1;
96 	return 0;
97 
98 out_error:
99 	return error;
100 }
101 
102 STATIC int
103 xfs_refcountbt_free_block(
104 	struct xfs_btree_cur	*cur,
105 	struct xfs_buf		*bp)
106 {
107 	struct xfs_mount	*mp = cur->bc_mp;
108 	struct xfs_buf		*agbp = cur->bc_private.a.agbp;
109 	struct xfs_agf		*agf = XFS_BUF_TO_AGF(agbp);
110 	xfs_fsblock_t		fsbno = XFS_DADDR_TO_FSB(mp, XFS_BUF_ADDR(bp));
111 	struct xfs_owner_info	oinfo;
112 	int			error;
113 
114 	trace_xfs_refcountbt_free_block(cur->bc_mp, cur->bc_private.a.agno,
115 			XFS_FSB_TO_AGBNO(cur->bc_mp, fsbno), 1);
116 	xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_REFC);
117 	be32_add_cpu(&agf->agf_refcount_blocks, -1);
118 	xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_REFCOUNT_BLOCKS);
119 	error = xfs_free_extent(cur->bc_tp, fsbno, 1, &oinfo,
120 			XFS_AG_RESV_METADATA);
121 	if (error)
122 		return error;
123 
124 	return error;
125 }
126 
127 STATIC int
128 xfs_refcountbt_get_minrecs(
129 	struct xfs_btree_cur	*cur,
130 	int			level)
131 {
132 	return cur->bc_mp->m_refc_mnr[level != 0];
133 }
134 
135 STATIC int
136 xfs_refcountbt_get_maxrecs(
137 	struct xfs_btree_cur	*cur,
138 	int			level)
139 {
140 	return cur->bc_mp->m_refc_mxr[level != 0];
141 }
142 
143 STATIC void
144 xfs_refcountbt_init_key_from_rec(
145 	union xfs_btree_key	*key,
146 	union xfs_btree_rec	*rec)
147 {
148 	key->refc.rc_startblock = rec->refc.rc_startblock;
149 }
150 
151 STATIC void
152 xfs_refcountbt_init_high_key_from_rec(
153 	union xfs_btree_key	*key,
154 	union xfs_btree_rec	*rec)
155 {
156 	__u32			x;
157 
158 	x = be32_to_cpu(rec->refc.rc_startblock);
159 	x += be32_to_cpu(rec->refc.rc_blockcount) - 1;
160 	key->refc.rc_startblock = cpu_to_be32(x);
161 }
162 
163 STATIC void
164 xfs_refcountbt_init_rec_from_cur(
165 	struct xfs_btree_cur	*cur,
166 	union xfs_btree_rec	*rec)
167 {
168 	rec->refc.rc_startblock = cpu_to_be32(cur->bc_rec.rc.rc_startblock);
169 	rec->refc.rc_blockcount = cpu_to_be32(cur->bc_rec.rc.rc_blockcount);
170 	rec->refc.rc_refcount = cpu_to_be32(cur->bc_rec.rc.rc_refcount);
171 }
172 
173 STATIC void
174 xfs_refcountbt_init_ptr_from_cur(
175 	struct xfs_btree_cur	*cur,
176 	union xfs_btree_ptr	*ptr)
177 {
178 	struct xfs_agf		*agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
179 
180 	ASSERT(cur->bc_private.a.agno == be32_to_cpu(agf->agf_seqno));
181 
182 	ptr->s = agf->agf_refcount_root;
183 }
184 
185 STATIC int64_t
186 xfs_refcountbt_key_diff(
187 	struct xfs_btree_cur	*cur,
188 	union xfs_btree_key	*key)
189 {
190 	struct xfs_refcount_irec	*rec = &cur->bc_rec.rc;
191 	struct xfs_refcount_key		*kp = &key->refc;
192 
193 	return (int64_t)be32_to_cpu(kp->rc_startblock) - rec->rc_startblock;
194 }
195 
196 STATIC int64_t
197 xfs_refcountbt_diff_two_keys(
198 	struct xfs_btree_cur	*cur,
199 	union xfs_btree_key	*k1,
200 	union xfs_btree_key	*k2)
201 {
202 	return (int64_t)be32_to_cpu(k1->refc.rc_startblock) -
203 			  be32_to_cpu(k2->refc.rc_startblock);
204 }
205 
206 STATIC xfs_failaddr_t
207 xfs_refcountbt_verify(
208 	struct xfs_buf		*bp)
209 {
210 	struct xfs_mount	*mp = bp->b_target->bt_mount;
211 	struct xfs_btree_block	*block = XFS_BUF_TO_BLOCK(bp);
212 	struct xfs_perag	*pag = bp->b_pag;
213 	xfs_failaddr_t		fa;
214 	unsigned int		level;
215 
216 	if (block->bb_magic != cpu_to_be32(XFS_REFC_CRC_MAGIC))
217 		return __this_address;
218 
219 	if (!xfs_sb_version_hasreflink(&mp->m_sb))
220 		return __this_address;
221 	fa = xfs_btree_sblock_v5hdr_verify(bp);
222 	if (fa)
223 		return fa;
224 
225 	level = be16_to_cpu(block->bb_level);
226 	if (pag && pag->pagf_init) {
227 		if (level >= pag->pagf_refcount_level)
228 			return __this_address;
229 	} else if (level >= mp->m_refc_maxlevels)
230 		return __this_address;
231 
232 	return xfs_btree_sblock_verify(bp, mp->m_refc_mxr[level != 0]);
233 }
234 
235 STATIC void
236 xfs_refcountbt_read_verify(
237 	struct xfs_buf	*bp)
238 {
239 	xfs_failaddr_t	fa;
240 
241 	if (!xfs_btree_sblock_verify_crc(bp))
242 		xfs_verifier_error(bp, -EFSBADCRC, __this_address);
243 	else {
244 		fa = xfs_refcountbt_verify(bp);
245 		if (fa)
246 			xfs_verifier_error(bp, -EFSCORRUPTED, fa);
247 	}
248 
249 	if (bp->b_error)
250 		trace_xfs_btree_corrupt(bp, _RET_IP_);
251 }
252 
253 STATIC void
254 xfs_refcountbt_write_verify(
255 	struct xfs_buf	*bp)
256 {
257 	xfs_failaddr_t	fa;
258 
259 	fa = xfs_refcountbt_verify(bp);
260 	if (fa) {
261 		trace_xfs_btree_corrupt(bp, _RET_IP_);
262 		xfs_verifier_error(bp, -EFSCORRUPTED, fa);
263 		return;
264 	}
265 	xfs_btree_sblock_calc_crc(bp);
266 
267 }
268 
269 const struct xfs_buf_ops xfs_refcountbt_buf_ops = {
270 	.name			= "xfs_refcountbt",
271 	.verify_read		= xfs_refcountbt_read_verify,
272 	.verify_write		= xfs_refcountbt_write_verify,
273 	.verify_struct		= xfs_refcountbt_verify,
274 };
275 
276 STATIC int
277 xfs_refcountbt_keys_inorder(
278 	struct xfs_btree_cur	*cur,
279 	union xfs_btree_key	*k1,
280 	union xfs_btree_key	*k2)
281 {
282 	return be32_to_cpu(k1->refc.rc_startblock) <
283 	       be32_to_cpu(k2->refc.rc_startblock);
284 }
285 
286 STATIC int
287 xfs_refcountbt_recs_inorder(
288 	struct xfs_btree_cur	*cur,
289 	union xfs_btree_rec	*r1,
290 	union xfs_btree_rec	*r2)
291 {
292 	return  be32_to_cpu(r1->refc.rc_startblock) +
293 		be32_to_cpu(r1->refc.rc_blockcount) <=
294 		be32_to_cpu(r2->refc.rc_startblock);
295 }
296 
297 static const struct xfs_btree_ops xfs_refcountbt_ops = {
298 	.rec_len		= sizeof(struct xfs_refcount_rec),
299 	.key_len		= sizeof(struct xfs_refcount_key),
300 
301 	.dup_cursor		= xfs_refcountbt_dup_cursor,
302 	.set_root		= xfs_refcountbt_set_root,
303 	.alloc_block		= xfs_refcountbt_alloc_block,
304 	.free_block		= xfs_refcountbt_free_block,
305 	.get_minrecs		= xfs_refcountbt_get_minrecs,
306 	.get_maxrecs		= xfs_refcountbt_get_maxrecs,
307 	.init_key_from_rec	= xfs_refcountbt_init_key_from_rec,
308 	.init_high_key_from_rec	= xfs_refcountbt_init_high_key_from_rec,
309 	.init_rec_from_cur	= xfs_refcountbt_init_rec_from_cur,
310 	.init_ptr_from_cur	= xfs_refcountbt_init_ptr_from_cur,
311 	.key_diff		= xfs_refcountbt_key_diff,
312 	.buf_ops		= &xfs_refcountbt_buf_ops,
313 	.diff_two_keys		= xfs_refcountbt_diff_two_keys,
314 	.keys_inorder		= xfs_refcountbt_keys_inorder,
315 	.recs_inorder		= xfs_refcountbt_recs_inorder,
316 };
317 
318 /*
319  * Allocate a new refcount btree cursor.
320  */
321 struct xfs_btree_cur *
322 xfs_refcountbt_init_cursor(
323 	struct xfs_mount	*mp,
324 	struct xfs_trans	*tp,
325 	struct xfs_buf		*agbp,
326 	xfs_agnumber_t		agno,
327 	struct xfs_defer_ops	*dfops)
328 {
329 	struct xfs_agf		*agf = XFS_BUF_TO_AGF(agbp);
330 	struct xfs_btree_cur	*cur;
331 
332 	ASSERT(agno != NULLAGNUMBER);
333 	ASSERT(agno < mp->m_sb.sb_agcount);
334 	cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_NOFS);
335 
336 	cur->bc_tp = tp;
337 	cur->bc_mp = mp;
338 	cur->bc_btnum = XFS_BTNUM_REFC;
339 	cur->bc_blocklog = mp->m_sb.sb_blocklog;
340 	cur->bc_ops = &xfs_refcountbt_ops;
341 	cur->bc_statoff = XFS_STATS_CALC_INDEX(xs_refcbt_2);
342 
343 	cur->bc_nlevels = be32_to_cpu(agf->agf_refcount_level);
344 
345 	cur->bc_private.a.agbp = agbp;
346 	cur->bc_private.a.agno = agno;
347 	cur->bc_private.a.dfops = dfops;
348 	cur->bc_flags |= XFS_BTREE_CRC_BLOCKS;
349 
350 	cur->bc_private.a.priv.refc.nr_ops = 0;
351 	cur->bc_private.a.priv.refc.shape_changes = 0;
352 
353 	return cur;
354 }
355 
356 /*
357  * Calculate the number of records in a refcount btree block.
358  */
359 int
360 xfs_refcountbt_maxrecs(
361 	int			blocklen,
362 	bool			leaf)
363 {
364 	blocklen -= XFS_REFCOUNT_BLOCK_LEN;
365 
366 	if (leaf)
367 		return blocklen / sizeof(struct xfs_refcount_rec);
368 	return blocklen / (sizeof(struct xfs_refcount_key) +
369 			   sizeof(xfs_refcount_ptr_t));
370 }
371 
372 /* Compute the maximum height of a refcount btree. */
373 void
374 xfs_refcountbt_compute_maxlevels(
375 	struct xfs_mount		*mp)
376 {
377 	mp->m_refc_maxlevels = xfs_btree_compute_maxlevels(
378 			mp->m_refc_mnr, mp->m_sb.sb_agblocks);
379 }
380 
381 /* Calculate the refcount btree size for some records. */
382 xfs_extlen_t
383 xfs_refcountbt_calc_size(
384 	struct xfs_mount	*mp,
385 	unsigned long long	len)
386 {
387 	return xfs_btree_calc_size(mp->m_refc_mnr, len);
388 }
389 
390 /*
391  * Calculate the maximum refcount btree size.
392  */
393 xfs_extlen_t
394 xfs_refcountbt_max_size(
395 	struct xfs_mount	*mp,
396 	xfs_agblock_t		agblocks)
397 {
398 	/* Bail out if we're uninitialized, which can happen in mkfs. */
399 	if (mp->m_refc_mxr[0] == 0)
400 		return 0;
401 
402 	return xfs_refcountbt_calc_size(mp, agblocks);
403 }
404 
405 /*
406  * Figure out how many blocks to reserve and how many are used by this btree.
407  */
408 int
409 xfs_refcountbt_calc_reserves(
410 	struct xfs_mount	*mp,
411 	xfs_agnumber_t		agno,
412 	xfs_extlen_t		*ask,
413 	xfs_extlen_t		*used)
414 {
415 	struct xfs_buf		*agbp;
416 	struct xfs_agf		*agf;
417 	xfs_agblock_t		agblocks;
418 	xfs_extlen_t		tree_len;
419 	int			error;
420 
421 	if (!xfs_sb_version_hasreflink(&mp->m_sb))
422 		return 0;
423 
424 
425 	error = xfs_alloc_read_agf(mp, NULL, agno, 0, &agbp);
426 	if (error)
427 		return error;
428 
429 	agf = XFS_BUF_TO_AGF(agbp);
430 	agblocks = be32_to_cpu(agf->agf_length);
431 	tree_len = be32_to_cpu(agf->agf_refcount_blocks);
432 	xfs_buf_relse(agbp);
433 
434 	*ask += xfs_refcountbt_max_size(mp, agblocks);
435 	*used += tree_len;
436 
437 	return error;
438 }
439