1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * Copyright (c) 2018 Red Hat, Inc.
8 #include "libxfs_priv.h"
11 #include "xfs_shared.h"
12 #include "xfs_format.h"
13 #include "xfs_trans_resv.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"
23 #include "xfs_ag_resv.h"
24 #include "xfs_health.h"
26 static struct xfs_buf
*
32 const struct xfs_buf_ops
*ops
)
36 bp
= xfs_buf_get_uncached(mp
->m_ddev_targp
, numblks
, flags
);
40 xfs_buf_zero(bp
, 0, BBTOB(bp
->b_length
));
42 bp
->b_maps
[0].bm_bn
= blkno
;
49 * Generic btree root block init function
55 struct aghdr_init_data
*id
)
57 xfs_btree_init_block(mp
, bp
, id
->type
, 0, 0, id
->agno
, 0);
61 * Alloc btree root block init functions
67 struct aghdr_init_data
*id
)
69 struct xfs_alloc_rec
*arec
;
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
));
82 struct aghdr_init_data
*id
)
84 struct xfs_alloc_rec
*arec
;
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
));
94 * Reverse map root block init
100 struct aghdr_init_data
*id
)
102 struct xfs_btree_block
*block
= XFS_BUF_TO_BLOCK(bp
);
103 struct xfs_rmap_rec
*rrec
;
105 xfs_btree_init_block(mp
, bp
, XFS_BTNUM_RMAP
, 0, 4, id
->agno
, 0);
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
113 * Note: unlike mkfs, we never have to account for log
114 * space when growing the data regions
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
);
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
);
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
) -
134 rrec
->rm_owner
= cpu_to_be64(XFS_RMAP_OWN_INOBT
);
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
);
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
);
151 be16_add_cpu(&block
->bb_numrecs
, 1);
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.
165 struct xfs_mount
*mp
,
167 struct aghdr_init_data
*id
)
169 struct xfs_dsb
*dsb
= XFS_BUF_TO_SBP(bp
);
171 xfs_sb_to_disk(dsb
, &mp
->m_sb
);
172 dsb
->sb_inprogress
= 1;
177 struct xfs_mount
*mp
,
179 struct aghdr_init_data
*id
)
181 struct xfs_agf
*agf
= XFS_BUF_TO_AGF(bp
);
182 xfs_extlen_t tmpsize
;
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);
199 agf
->agf_flfirst
= cpu_to_be32(1);
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(
210 agf
->agf_refcount_level
= cpu_to_be32(1);
211 agf
->agf_refcount_blocks
= cpu_to_be32(1);
217 struct xfs_mount
*mp
,
219 struct aghdr_init_data
*id
)
221 struct xfs_agfl
*agfl
= XFS_BUF_TO_AGFL(bp
);
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
);
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
);
238 struct xfs_mount
*mp
,
240 struct aghdr_init_data
*id
)
242 struct xfs_agi
*agi
= XFS_BUF_TO_AGI(bp
);
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
);
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);
261 for (bucket
= 0; bucket
< XFS_AGI_UNLINKED_BUCKETS
; bucket
++)
262 agi
->agi_unlinked
[bucket
] = cpu_to_be32(NULLAGINO
);
265 typedef void (*aghdr_init_work_f
)(struct xfs_mount
*mp
, struct xfs_buf
*bp
,
266 struct aghdr_init_data
*id
);
269 struct xfs_mount
*mp
,
270 struct aghdr_init_data
*id
,
271 aghdr_init_work_f work
,
272 const struct xfs_buf_ops
*ops
)
277 bp
= xfs_get_aghdr_buf(mp
, id
->daddr
, id
->numblks
, 0, ops
);
283 xfs_buf_delwri_queue(bp
, &id
->buffer_list
);
288 struct xfs_aghdr_grow_data
{
291 const struct xfs_buf_ops
*ops
;
292 aghdr_init_work_f work
;
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.
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.
309 struct xfs_mount
*mp
,
310 struct aghdr_init_data
*id
)
313 struct xfs_aghdr_grow_data aghdr_data
[] = {
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
,
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
,
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
,
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
,
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
,
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
,
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
,
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
)
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
)
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
)
387 { /* NULL terminating block */
388 .daddr
= XFS_BUF_DADDR_NULL
,
391 struct xfs_aghdr_grow_data
*dp
;
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
++) {
400 id
->daddr
= dp
->daddr
;
401 id
->numblks
= dp
->numblks
;
403 error
= xfs_ag_init_hdr(mp
, id
, dp
->work
, dp
->ops
);
411 * Extent the AG indicated by the @id by the length passed in
415 struct xfs_mount
*mp
,
416 struct xfs_trans
*tp
,
417 struct aghdr_init_data
*id
,
426 * Change the agi length.
428 error
= xfs_ialloc_read_agi(mp
, tp
, id
->agno
, &bp
);
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
);
441 error
= xfs_alloc_read_agf(mp
, tp
, id
->agno
, 0, &bp
);
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
);
451 * Free the new space.
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.
456 error
= xfs_rmap_free(tp
, bp
, id
->agno
,
457 be32_to_cpu(agf
->agf_length
) - len
,
458 len
, &XFS_RMAP_OINFO_SKIP_UPDATE
);
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
,
468 /* Retrieve AG geometry. */
471 struct xfs_mount
*mp
,
473 struct xfs_ag_geometry
*ageo
)
475 struct xfs_buf
*agi_bp
;
476 struct xfs_buf
*agf_bp
;
479 struct xfs_perag
*pag
;
480 unsigned int freeblks
;
483 if (agno
>= mp
->m_sb
.sb_agcount
)
486 /* Lock the AG headers. */
487 error
= xfs_ialloc_read_agi(mp
, NULL
, agno
, &agi_bp
);
490 error
= xfs_alloc_read_agf(mp
, NULL
, agno
, 0, &agf_bp
);
493 pag
= xfs_perag_get(mp
, agno
);
496 memset(ageo
, 0, sizeof(*ageo
));
497 ageo
->ag_number
= agno
;
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
);
503 agf
= XFS_BUF_TO_AGF(agf_bp
);
504 ageo
->ag_length
= be32_to_cpu(agf
->agf_length
);
505 freeblks
= pag
->pagf_freeblks
+
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
);
512 /* Release resources. */
514 xfs_buf_relse(agf_bp
);
516 xfs_buf_relse(agi_bp
);