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