]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - libxfs/xfs_refcount_btree.c
libxfs: silence static warnings about platform_* functions
[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));
bc859611
DW
72 xfs_rmap_ag_owner(&args.oinfo, XFS_RMAP_OWN_REFC);
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));
108 struct xfs_owner_info oinfo;
02cc8b2a 109 int error;
bc859611
DW
110
111 trace_xfs_refcountbt_free_block(cur->bc_mp, cur->bc_private.a.agno,
112 XFS_FSB_TO_AGBNO(cur->bc_mp, fsbno), 1);
113 xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_REFC);
114 be32_add_cpu(&agf->agf_refcount_blocks, -1);
115 xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_REFCOUNT_BLOCKS);
02cc8b2a
DW
116 error = xfs_free_extent(cur->bc_tp, fsbno, 1, &oinfo,
117 XFS_AG_RESV_METADATA);
118 if (error)
119 return error;
bc859611 120
02cc8b2a 121 return error;
bc859611
DW
122}
123
124STATIC int
125xfs_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
132STATIC int
133xfs_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
140STATIC void
141xfs_refcountbt_init_key_from_rec(
142 union xfs_btree_key *key,
143 union xfs_btree_rec *rec)
144{
145 key->refc.rc_startblock = rec->refc.rc_startblock;
146}
147
148STATIC void
149xfs_refcountbt_init_high_key_from_rec(
150 union xfs_btree_key *key,
151 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
160STATIC void
161xfs_refcountbt_init_rec_from_cur(
162 struct xfs_btree_cur *cur,
163 union xfs_btree_rec *rec)
164{
165 rec->refc.rc_startblock = cpu_to_be32(cur->bc_rec.rc.rc_startblock);
166 rec->refc.rc_blockcount = cpu_to_be32(cur->bc_rec.rc.rc_blockcount);
167 rec->refc.rc_refcount = cpu_to_be32(cur->bc_rec.rc.rc_refcount);
168}
169
170STATIC void
171xfs_refcountbt_init_ptr_from_cur(
172 struct xfs_btree_cur *cur,
173 union xfs_btree_ptr *ptr)
174{
175 struct xfs_agf *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
176
177 ASSERT(cur->bc_private.a.agno == be32_to_cpu(agf->agf_seqno));
bc859611
DW
178
179 ptr->s = agf->agf_refcount_root;
180}
181
4a492e72 182STATIC int64_t
bc859611
DW
183xfs_refcountbt_key_diff(
184 struct xfs_btree_cur *cur,
185 union xfs_btree_key *key)
186{
187 struct xfs_refcount_irec *rec = &cur->bc_rec.rc;
188 struct xfs_refcount_key *kp = &key->refc;
189
4a492e72 190 return (int64_t)be32_to_cpu(kp->rc_startblock) - rec->rc_startblock;
bc859611
DW
191}
192
4a492e72 193STATIC int64_t
bc859611
DW
194xfs_refcountbt_diff_two_keys(
195 struct xfs_btree_cur *cur,
196 union xfs_btree_key *k1,
197 union xfs_btree_key *k2)
198{
4a492e72 199 return (int64_t)be32_to_cpu(k1->refc.rc_startblock) -
bc859611
DW
200 be32_to_cpu(k2->refc.rc_startblock);
201}
202
bc01119d 203STATIC xfs_failaddr_t
d8079fe0
DW
204xfs_refcountbt_verify(
205 struct xfs_buf *bp)
206{
207 struct xfs_mount *mp = bp->b_target->bt_mount;
208 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
209 struct xfs_perag *pag = bp->b_pag;
bc01119d 210 xfs_failaddr_t fa;
d8079fe0
DW
211 unsigned int level;
212
213 if (block->bb_magic != cpu_to_be32(XFS_REFC_CRC_MAGIC))
bc01119d 214 return __this_address;
d8079fe0
DW
215
216 if (!xfs_sb_version_hasreflink(&mp->m_sb))
bc01119d
DW
217 return __this_address;
218 fa = xfs_btree_sblock_v5hdr_verify(bp);
219 if (fa)
220 return fa;
d8079fe0
DW
221
222 level = be16_to_cpu(block->bb_level);
223 if (pag && pag->pagf_init) {
224 if (level >= pag->pagf_refcount_level)
bc01119d 225 return __this_address;
d8079fe0 226 } else if (level >= mp->m_refc_maxlevels)
bc01119d 227 return __this_address;
d8079fe0
DW
228
229 return xfs_btree_sblock_verify(bp, mp->m_refc_mxr[level != 0]);
230}
231
232STATIC void
233xfs_refcountbt_read_verify(
234 struct xfs_buf *bp)
235{
1e697959
DW
236 xfs_failaddr_t fa;
237
d8079fe0 238 if (!xfs_btree_sblock_verify_crc(bp))
1e697959
DW
239 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
240 else {
241 fa = xfs_refcountbt_verify(bp);
242 if (fa)
243 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
244 }
d8079fe0 245
7e6c95f1 246 if (bp->b_error)
d8079fe0 247 trace_xfs_btree_corrupt(bp, _RET_IP_);
d8079fe0
DW
248}
249
250STATIC void
251xfs_refcountbt_write_verify(
252 struct xfs_buf *bp)
253{
1e697959
DW
254 xfs_failaddr_t fa;
255
256 fa = xfs_refcountbt_verify(bp);
257 if (fa) {
d8079fe0 258 trace_xfs_btree_corrupt(bp, _RET_IP_);
1e697959 259 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
d8079fe0
DW
260 return;
261 }
262 xfs_btree_sblock_calc_crc(bp);
263
264}
265
266const struct xfs_buf_ops xfs_refcountbt_buf_ops = {
267 .name = "xfs_refcountbt",
268 .verify_read = xfs_refcountbt_read_verify,
269 .verify_write = xfs_refcountbt_write_verify,
95d9582b 270 .verify_struct = xfs_refcountbt_verify,
d8079fe0
DW
271};
272
bc859611
DW
273STATIC int
274xfs_refcountbt_keys_inorder(
275 struct xfs_btree_cur *cur,
276 union xfs_btree_key *k1,
277 union xfs_btree_key *k2)
278{
279 return be32_to_cpu(k1->refc.rc_startblock) <
280 be32_to_cpu(k2->refc.rc_startblock);
281}
282
283STATIC int
284xfs_refcountbt_recs_inorder(
285 struct xfs_btree_cur *cur,
286 union xfs_btree_rec *r1,
287 union xfs_btree_rec *r2)
288{
289 return be32_to_cpu(r1->refc.rc_startblock) +
290 be32_to_cpu(r1->refc.rc_blockcount) <=
291 be32_to_cpu(r2->refc.rc_startblock);
292}
bc859611 293
d8079fe0
DW
294static const struct xfs_btree_ops xfs_refcountbt_ops = {
295 .rec_len = sizeof(struct xfs_refcount_rec),
296 .key_len = sizeof(struct xfs_refcount_key),
297
298 .dup_cursor = xfs_refcountbt_dup_cursor,
bc859611
DW
299 .set_root = xfs_refcountbt_set_root,
300 .alloc_block = xfs_refcountbt_alloc_block,
301 .free_block = xfs_refcountbt_free_block,
302 .get_minrecs = xfs_refcountbt_get_minrecs,
303 .get_maxrecs = xfs_refcountbt_get_maxrecs,
304 .init_key_from_rec = xfs_refcountbt_init_key_from_rec,
305 .init_high_key_from_rec = xfs_refcountbt_init_high_key_from_rec,
306 .init_rec_from_cur = xfs_refcountbt_init_rec_from_cur,
307 .init_ptr_from_cur = xfs_refcountbt_init_ptr_from_cur,
308 .key_diff = xfs_refcountbt_key_diff,
d8079fe0 309 .buf_ops = &xfs_refcountbt_buf_ops,
bc859611 310 .diff_two_keys = xfs_refcountbt_diff_two_keys,
bc859611
DW
311 .keys_inorder = xfs_refcountbt_keys_inorder,
312 .recs_inorder = xfs_refcountbt_recs_inorder,
d8079fe0
DW
313};
314
315/*
316 * Allocate a new refcount btree cursor.
317 */
318struct xfs_btree_cur *
319xfs_refcountbt_init_cursor(
320 struct xfs_mount *mp,
321 struct xfs_trans *tp,
322 struct xfs_buf *agbp,
5ff5ced0 323 xfs_agnumber_t agno)
d8079fe0
DW
324{
325 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
326 struct xfs_btree_cur *cur;
327
328 ASSERT(agno != NULLAGNUMBER);
329 ASSERT(agno < mp->m_sb.sb_agcount);
330 cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_NOFS);
331
332 cur->bc_tp = tp;
333 cur->bc_mp = mp;
334 cur->bc_btnum = XFS_BTNUM_REFC;
335 cur->bc_blocklog = mp->m_sb.sb_blocklog;
336 cur->bc_ops = &xfs_refcountbt_ops;
5d8acc46 337 cur->bc_statoff = XFS_STATS_CALC_INDEX(xs_refcbt_2);
d8079fe0
DW
338
339 cur->bc_nlevels = be32_to_cpu(agf->agf_refcount_level);
340
341 cur->bc_private.a.agbp = agbp;
342 cur->bc_private.a.agno = agno;
d8079fe0
DW
343 cur->bc_flags |= XFS_BTREE_CRC_BLOCKS;
344
345 cur->bc_private.a.priv.refc.nr_ops = 0;
346 cur->bc_private.a.priv.refc.shape_changes = 0;
347
348 return cur;
349}
350
351/*
352 * Calculate the number of records in a refcount btree block.
353 */
354int
355xfs_refcountbt_maxrecs(
d8079fe0
DW
356 int blocklen,
357 bool leaf)
358{
359 blocklen -= XFS_REFCOUNT_BLOCK_LEN;
360
361 if (leaf)
362 return blocklen / sizeof(struct xfs_refcount_rec);
363 return blocklen / (sizeof(struct xfs_refcount_key) +
364 sizeof(xfs_refcount_ptr_t));
365}
366
367/* Compute the maximum height of a refcount btree. */
368void
369xfs_refcountbt_compute_maxlevels(
370 struct xfs_mount *mp)
371{
1421de38 372 mp->m_refc_maxlevels = xfs_btree_compute_maxlevels(
d8079fe0
DW
373 mp->m_refc_mnr, mp->m_sb.sb_agblocks);
374}
02cc8b2a
DW
375
376/* Calculate the refcount btree size for some records. */
377xfs_extlen_t
378xfs_refcountbt_calc_size(
379 struct xfs_mount *mp,
380 unsigned long long len)
381{
1421de38 382 return xfs_btree_calc_size(mp->m_refc_mnr, len);
02cc8b2a
DW
383}
384
385/*
386 * Calculate the maximum refcount btree size.
387 */
388xfs_extlen_t
389xfs_refcountbt_max_size(
f21c57ed
DW
390 struct xfs_mount *mp,
391 xfs_agblock_t agblocks)
02cc8b2a
DW
392{
393 /* Bail out if we're uninitialized, which can happen in mkfs. */
394 if (mp->m_refc_mxr[0] == 0)
395 return 0;
396
f21c57ed 397 return xfs_refcountbt_calc_size(mp, agblocks);
02cc8b2a
DW
398}
399
400/*
401 * Figure out how many blocks to reserve and how many are used by this btree.
402 */
403int
404xfs_refcountbt_calc_reserves(
405 struct xfs_mount *mp,
0d802327 406 struct xfs_trans *tp,
02cc8b2a
DW
407 xfs_agnumber_t agno,
408 xfs_extlen_t *ask,
409 xfs_extlen_t *used)
410{
411 struct xfs_buf *agbp;
412 struct xfs_agf *agf;
f21c57ed 413 xfs_agblock_t agblocks;
02cc8b2a
DW
414 xfs_extlen_t tree_len;
415 int error;
416
417 if (!xfs_sb_version_hasreflink(&mp->m_sb))
418 return 0;
419
02cc8b2a 420
0d802327 421 error = xfs_alloc_read_agf(mp, tp, agno, 0, &agbp);
02cc8b2a
DW
422 if (error)
423 return error;
424
425 agf = XFS_BUF_TO_AGF(agbp);
f21c57ed 426 agblocks = be32_to_cpu(agf->agf_length);
02cc8b2a 427 tree_len = be32_to_cpu(agf->agf_refcount_blocks);
0d802327 428 xfs_trans_brelse(tp, agbp);
02cc8b2a 429
f21c57ed 430 *ask += xfs_refcountbt_max_size(mp, agblocks);
02cc8b2a
DW
431 *used += tree_len;
432
433 return error;
434}