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