]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - libxfs/xfs_ag.c
libxfs: refactor manage_zones()
[thirdparty/xfsprogs-dev.git] / libxfs / xfs_ag.c
CommitLineData
971ce259
DC
1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * Copyright (c) 2018 Red Hat, Inc.
5 * All rights reserved.
6 */
7
8#include "libxfs_priv.h"
9#include "xfs.h"
10#include "xfs_fs.h"
11#include "xfs_shared.h"
12#include "xfs_format.h"
13#include "xfs_trans_resv.h"
14#include "xfs_sb.h"
15#include "xfs_mount.h"
16#include "xfs_btree.h"
17#include "xfs_alloc_btree.h"
18#include "xfs_rmap_btree.h"
19#include "xfs_alloc.h"
8a895420 20#include "xfs_ialloc.h"
971ce259
DC
21#include "xfs_rmap.h"
22#include "xfs_ag.h"
23
24static struct xfs_buf *
25xfs_get_aghdr_buf(
26 struct xfs_mount *mp,
27 xfs_daddr_t blkno,
28 size_t numblks,
29 int flags,
30 const struct xfs_buf_ops *ops)
31{
32 struct xfs_buf *bp;
33
34 bp = xfs_buf_get_uncached(mp->m_ddev_targp, numblks, flags);
35 if (!bp)
36 return NULL;
37
38 xfs_buf_zero(bp, 0, BBTOB(bp->b_length));
39 bp->b_bn = blkno;
40 bp->b_maps[0].bm_bn = blkno;
41 bp->b_ops = ops;
42
43 return bp;
44}
45
46/*
47 * Generic btree root block init function
48 */
49static void
50xfs_btroot_init(
51 struct xfs_mount *mp,
52 struct xfs_buf *bp,
53 struct aghdr_init_data *id)
54{
55 xfs_btree_init_block(mp, bp, id->type, 0, 0, id->agno, 0);
56}
57
58/*
59 * Alloc btree root block init functions
60 */
61static void
62xfs_bnoroot_init(
63 struct xfs_mount *mp,
64 struct xfs_buf *bp,
65 struct aghdr_init_data *id)
66{
67 struct xfs_alloc_rec *arec;
68
69 xfs_btree_init_block(mp, bp, XFS_BTNUM_BNO, 0, 1, id->agno, 0);
70 arec = XFS_ALLOC_REC_ADDR(mp, XFS_BUF_TO_BLOCK(bp), 1);
71 arec->ar_startblock = cpu_to_be32(mp->m_ag_prealloc_blocks);
72 arec->ar_blockcount = cpu_to_be32(id->agsize -
73 be32_to_cpu(arec->ar_startblock));
74}
75
76static void
77xfs_cntroot_init(
78 struct xfs_mount *mp,
79 struct xfs_buf *bp,
80 struct aghdr_init_data *id)
81{
82 struct xfs_alloc_rec *arec;
83
84 xfs_btree_init_block(mp, bp, XFS_BTNUM_CNT, 0, 1, id->agno, 0);
85 arec = XFS_ALLOC_REC_ADDR(mp, XFS_BUF_TO_BLOCK(bp), 1);
86 arec->ar_startblock = cpu_to_be32(mp->m_ag_prealloc_blocks);
87 arec->ar_blockcount = cpu_to_be32(id->agsize -
88 be32_to_cpu(arec->ar_startblock));
89}
90
91/*
92 * Reverse map root block init
93 */
94static void
95xfs_rmaproot_init(
96 struct xfs_mount *mp,
97 struct xfs_buf *bp,
98 struct aghdr_init_data *id)
99{
100 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
101 struct xfs_rmap_rec *rrec;
102
103 xfs_btree_init_block(mp, bp, XFS_BTNUM_RMAP, 0, 4, id->agno, 0);
104
105 /*
106 * mark the AG header regions as static metadata The BNO
107 * btree block is the first block after the headers, so
108 * it's location defines the size of region the static
109 * metadata consumes.
110 *
111 * Note: unlike mkfs, we never have to account for log
112 * space when growing the data regions
113 */
114 rrec = XFS_RMAP_REC_ADDR(block, 1);
115 rrec->rm_startblock = 0;
116 rrec->rm_blockcount = cpu_to_be32(XFS_BNO_BLOCK(mp));
117 rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_FS);
118 rrec->rm_offset = 0;
119
120 /* account freespace btree root blocks */
121 rrec = XFS_RMAP_REC_ADDR(block, 2);
122 rrec->rm_startblock = cpu_to_be32(XFS_BNO_BLOCK(mp));
123 rrec->rm_blockcount = cpu_to_be32(2);
124 rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_AG);
125 rrec->rm_offset = 0;
126
127 /* account inode btree root blocks */
128 rrec = XFS_RMAP_REC_ADDR(block, 3);
129 rrec->rm_startblock = cpu_to_be32(XFS_IBT_BLOCK(mp));
130 rrec->rm_blockcount = cpu_to_be32(XFS_RMAP_BLOCK(mp) -
131 XFS_IBT_BLOCK(mp));
132 rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_INOBT);
133 rrec->rm_offset = 0;
134
135 /* account for rmap btree root */
136 rrec = XFS_RMAP_REC_ADDR(block, 4);
137 rrec->rm_startblock = cpu_to_be32(XFS_RMAP_BLOCK(mp));
138 rrec->rm_blockcount = cpu_to_be32(1);
139 rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_AG);
140 rrec->rm_offset = 0;
141
142 /* account for refc btree root */
143 if (xfs_sb_version_hasreflink(&mp->m_sb)) {
144 rrec = XFS_RMAP_REC_ADDR(block, 5);
145 rrec->rm_startblock = cpu_to_be32(xfs_refc_block(mp));
146 rrec->rm_blockcount = cpu_to_be32(1);
147 rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_REFC);
148 rrec->rm_offset = 0;
149 be16_add_cpu(&block->bb_numrecs, 1);
150 }
151}
152
153/*
154 * Initialise new secondary superblocks with the pre-grow geometry, but mark
155 * them as "in progress" so we know they haven't yet been activated. This will
156 * get cleared when the update with the new geometry information is done after
157 * changes to the primary are committed. This isn't strictly necessary, but we
158 * get it for free with the delayed buffer write lists and it means we can tell
159 * if a grow operation didn't complete properly after the fact.
160 */
161static void
162xfs_sbblock_init(
163 struct xfs_mount *mp,
164 struct xfs_buf *bp,
165 struct aghdr_init_data *id)
166{
167 struct xfs_dsb *dsb = XFS_BUF_TO_SBP(bp);
168
169 xfs_sb_to_disk(dsb, &mp->m_sb);
170 dsb->sb_inprogress = 1;
171}
172
173static void
174xfs_agfblock_init(
175 struct xfs_mount *mp,
176 struct xfs_buf *bp,
177 struct aghdr_init_data *id)
178{
179 struct xfs_agf *agf = XFS_BUF_TO_AGF(bp);
180 xfs_extlen_t tmpsize;
181
182 agf->agf_magicnum = cpu_to_be32(XFS_AGF_MAGIC);
183 agf->agf_versionnum = cpu_to_be32(XFS_AGF_VERSION);
184 agf->agf_seqno = cpu_to_be32(id->agno);
185 agf->agf_length = cpu_to_be32(id->agsize);
186 agf->agf_roots[XFS_BTNUM_BNOi] = cpu_to_be32(XFS_BNO_BLOCK(mp));
187 agf->agf_roots[XFS_BTNUM_CNTi] = cpu_to_be32(XFS_CNT_BLOCK(mp));
188 agf->agf_levels[XFS_BTNUM_BNOi] = cpu_to_be32(1);
189 agf->agf_levels[XFS_BTNUM_CNTi] = cpu_to_be32(1);
190 if (xfs_sb_version_hasrmapbt(&mp->m_sb)) {
191 agf->agf_roots[XFS_BTNUM_RMAPi] =
192 cpu_to_be32(XFS_RMAP_BLOCK(mp));
193 agf->agf_levels[XFS_BTNUM_RMAPi] = cpu_to_be32(1);
194 agf->agf_rmap_blocks = cpu_to_be32(1);
195 }
196
197 agf->agf_flfirst = cpu_to_be32(1);
198 agf->agf_fllast = 0;
199 agf->agf_flcount = 0;
200 tmpsize = id->agsize - mp->m_ag_prealloc_blocks;
201 agf->agf_freeblks = cpu_to_be32(tmpsize);
202 agf->agf_longest = cpu_to_be32(tmpsize);
203 if (xfs_sb_version_hascrc(&mp->m_sb))
204 uuid_copy(&agf->agf_uuid, &mp->m_sb.sb_meta_uuid);
205 if (xfs_sb_version_hasreflink(&mp->m_sb)) {
206 agf->agf_refcount_root = cpu_to_be32(
207 xfs_refc_block(mp));
208 agf->agf_refcount_level = cpu_to_be32(1);
209 agf->agf_refcount_blocks = cpu_to_be32(1);
210 }
211}
212
213static void
214xfs_agflblock_init(
215 struct xfs_mount *mp,
216 struct xfs_buf *bp,
217 struct aghdr_init_data *id)
218{
219 struct xfs_agfl *agfl = XFS_BUF_TO_AGFL(bp);
220 __be32 *agfl_bno;
221 int bucket;
222
223 if (xfs_sb_version_hascrc(&mp->m_sb)) {
224 agfl->agfl_magicnum = cpu_to_be32(XFS_AGFL_MAGIC);
225 agfl->agfl_seqno = cpu_to_be32(id->agno);
226 uuid_copy(&agfl->agfl_uuid, &mp->m_sb.sb_meta_uuid);
227 }
228
229 agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, bp);
230 for (bucket = 0; bucket < xfs_agfl_size(mp); bucket++)
231 agfl_bno[bucket] = cpu_to_be32(NULLAGBLOCK);
232}
233
234static void
235xfs_agiblock_init(
236 struct xfs_mount *mp,
237 struct xfs_buf *bp,
238 struct aghdr_init_data *id)
239{
240 struct xfs_agi *agi = XFS_BUF_TO_AGI(bp);
241 int bucket;
242
243 agi->agi_magicnum = cpu_to_be32(XFS_AGI_MAGIC);
244 agi->agi_versionnum = cpu_to_be32(XFS_AGI_VERSION);
245 agi->agi_seqno = cpu_to_be32(id->agno);
246 agi->agi_length = cpu_to_be32(id->agsize);
247 agi->agi_count = 0;
248 agi->agi_root = cpu_to_be32(XFS_IBT_BLOCK(mp));
249 agi->agi_level = cpu_to_be32(1);
250 agi->agi_freecount = 0;
251 agi->agi_newino = cpu_to_be32(NULLAGINO);
252 agi->agi_dirino = cpu_to_be32(NULLAGINO);
253 if (xfs_sb_version_hascrc(&mp->m_sb))
254 uuid_copy(&agi->agi_uuid, &mp->m_sb.sb_meta_uuid);
255 if (xfs_sb_version_hasfinobt(&mp->m_sb)) {
256 agi->agi_free_root = cpu_to_be32(XFS_FIBT_BLOCK(mp));
257 agi->agi_free_level = cpu_to_be32(1);
258 }
259 for (bucket = 0; bucket < XFS_AGI_UNLINKED_BUCKETS; bucket++)
260 agi->agi_unlinked[bucket] = cpu_to_be32(NULLAGINO);
261}
262
263typedef void (*aghdr_init_work_f)(struct xfs_mount *mp, struct xfs_buf *bp,
264 struct aghdr_init_data *id);
265static int
266xfs_ag_init_hdr(
267 struct xfs_mount *mp,
268 struct aghdr_init_data *id,
269 aghdr_init_work_f work,
270 const struct xfs_buf_ops *ops)
271
272{
273 struct xfs_buf *bp;
274
275 bp = xfs_get_aghdr_buf(mp, id->daddr, id->numblks, 0, ops);
276 if (!bp)
277 return -ENOMEM;
278
279 (*work)(mp, bp, id);
280
281 xfs_buf_delwri_queue(bp, &id->buffer_list);
282 xfs_buf_relse(bp);
283 return 0;
284}
285
286struct xfs_aghdr_grow_data {
287 xfs_daddr_t daddr;
288 size_t numblks;
289 const struct xfs_buf_ops *ops;
290 aghdr_init_work_f work;
291 xfs_btnum_t type;
292 bool need_init;
293};
294
295/*
296 * Prepare new AG headers to be written to disk. We use uncached buffers here,
297 * as it is assumed these new AG headers are currently beyond the currently
298 * valid filesystem address space. Using cached buffers would trip over EOFS
299 * corruption detection alogrithms in the buffer cache lookup routines.
300 *
301 * This is a non-transactional function, but the prepared buffers are added to a
302 * delayed write buffer list supplied by the caller so they can submit them to
303 * disk and wait on them as required.
304 */
305int
306xfs_ag_init_headers(
307 struct xfs_mount *mp,
308 struct aghdr_init_data *id)
309
310{
311 struct xfs_aghdr_grow_data aghdr_data[] = {
312 { /* SB */
313 .daddr = XFS_AG_DADDR(mp, id->agno, XFS_SB_DADDR),
314 .numblks = XFS_FSS_TO_BB(mp, 1),
315 .ops = &xfs_sb_buf_ops,
316 .work = &xfs_sbblock_init,
317 .need_init = true
318 },
319 { /* AGF */
320 .daddr = XFS_AG_DADDR(mp, id->agno, XFS_AGF_DADDR(mp)),
321 .numblks = XFS_FSS_TO_BB(mp, 1),
322 .ops = &xfs_agf_buf_ops,
323 .work = &xfs_agfblock_init,
324 .need_init = true
325 },
326 { /* AGFL */
327 .daddr = XFS_AG_DADDR(mp, id->agno, XFS_AGFL_DADDR(mp)),
328 .numblks = XFS_FSS_TO_BB(mp, 1),
329 .ops = &xfs_agfl_buf_ops,
330 .work = &xfs_agflblock_init,
331 .need_init = true
332 },
333 { /* AGI */
334 .daddr = XFS_AG_DADDR(mp, id->agno, XFS_AGI_DADDR(mp)),
335 .numblks = XFS_FSS_TO_BB(mp, 1),
336 .ops = &xfs_agi_buf_ops,
337 .work = &xfs_agiblock_init,
338 .need_init = true
339 },
340 { /* BNO root block */
341 .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_BNO_BLOCK(mp)),
342 .numblks = BTOBB(mp->m_sb.sb_blocksize),
343 .ops = &xfs_allocbt_buf_ops,
344 .work = &xfs_bnoroot_init,
345 .need_init = true
346 },
347 { /* CNT root block */
348 .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_CNT_BLOCK(mp)),
349 .numblks = BTOBB(mp->m_sb.sb_blocksize),
350 .ops = &xfs_allocbt_buf_ops,
351 .work = &xfs_cntroot_init,
352 .need_init = true
353 },
354 { /* INO root block */
355 .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_IBT_BLOCK(mp)),
356 .numblks = BTOBB(mp->m_sb.sb_blocksize),
357 .ops = &xfs_inobt_buf_ops,
358 .work = &xfs_btroot_init,
359 .type = XFS_BTNUM_INO,
360 .need_init = true
361 },
362 { /* FINO root block */
363 .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_FIBT_BLOCK(mp)),
364 .numblks = BTOBB(mp->m_sb.sb_blocksize),
365 .ops = &xfs_inobt_buf_ops,
366 .work = &xfs_btroot_init,
367 .type = XFS_BTNUM_FINO,
368 .need_init = xfs_sb_version_hasfinobt(&mp->m_sb)
369 },
370 { /* RMAP root block */
371 .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_RMAP_BLOCK(mp)),
372 .numblks = BTOBB(mp->m_sb.sb_blocksize),
373 .ops = &xfs_rmapbt_buf_ops,
374 .work = &xfs_rmaproot_init,
375 .need_init = xfs_sb_version_hasrmapbt(&mp->m_sb)
376 },
377 { /* REFC root block */
378 .daddr = XFS_AGB_TO_DADDR(mp, id->agno, xfs_refc_block(mp)),
379 .numblks = BTOBB(mp->m_sb.sb_blocksize),
380 .ops = &xfs_refcountbt_buf_ops,
381 .work = &xfs_btroot_init,
382 .type = XFS_BTNUM_REFC,
383 .need_init = xfs_sb_version_hasreflink(&mp->m_sb)
384 },
385 { /* NULL terminating block */
386 .daddr = XFS_BUF_DADDR_NULL,
387 }
388 };
389 struct xfs_aghdr_grow_data *dp;
390 int error = 0;
391
392 /* Account for AG free space in new AG */
393 id->nfree += id->agsize - mp->m_ag_prealloc_blocks;
394 for (dp = &aghdr_data[0]; dp->daddr != XFS_BUF_DADDR_NULL; dp++) {
395 if (!dp->need_init)
396 continue;
397
398 id->daddr = dp->daddr;
399 id->numblks = dp->numblks;
400 id->type = dp->type;
401 error = xfs_ag_init_hdr(mp, id, dp->work, dp->ops);
402 if (error)
403 break;
404 }
405 return error;
406}
8a895420
DC
407
408/*
409 * Extent the AG indicated by the @id by the length passed in
410 */
411int
412xfs_ag_extend_space(
413 struct xfs_mount *mp,
414 struct xfs_trans *tp,
415 struct aghdr_init_data *id,
416 xfs_extlen_t len)
417{
8a895420
DC
418 struct xfs_buf *bp;
419 struct xfs_agi *agi;
420 struct xfs_agf *agf;
421 int error;
422
423 /*
424 * Change the agi length.
425 */
426 error = xfs_ialloc_read_agi(mp, tp, id->agno, &bp);
427 if (error)
428 return error;
429
430 agi = XFS_BUF_TO_AGI(bp);
431 be32_add_cpu(&agi->agi_length, len);
432 ASSERT(id->agno == mp->m_sb.sb_agcount - 1 ||
433 be32_to_cpu(agi->agi_length) == mp->m_sb.sb_agblocks);
434 xfs_ialloc_log_agi(tp, bp, XFS_AGI_LENGTH);
435
436 /*
437 * Change agf length.
438 */
439 error = xfs_alloc_read_agf(mp, tp, id->agno, 0, &bp);
440 if (error)
441 return error;
442
443 agf = XFS_BUF_TO_AGF(bp);
444 be32_add_cpu(&agf->agf_length, len);
445 ASSERT(agf->agf_length == agi->agi_length);
446 xfs_alloc_log_agf(tp, bp, XFS_AGF_LENGTH);
447
448 /*
449 * Free the new space.
450 *
007347e3 451 * XFS_RMAP_OINFO_SKIP_UPDATE is used here to tell the rmap btree that
8a895420
DC
452 * this doesn't actually exist in the rmap btree.
453 */
8a895420
DC
454 error = xfs_rmap_free(tp, bp, id->agno,
455 be32_to_cpu(agf->agf_length) - len,
007347e3 456 len, &XFS_RMAP_OINFO_SKIP_UPDATE);
8a895420
DC
457 if (error)
458 return error;
459
460 return xfs_free_extent(tp, XFS_AGB_TO_FSB(mp, id->agno,
461 be32_to_cpu(agf->agf_length) - len),
007347e3
DW
462 len, &XFS_RMAP_OINFO_SKIP_UPDATE,
463 XFS_AG_RESV_NONE);
8a895420 464}