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"
24 static struct xfs_buf
*
30 const struct xfs_buf_ops
*ops
)
34 bp
= xfs_buf_get_uncached(mp
->m_ddev_targp
, numblks
, flags
);
38 xfs_buf_zero(bp
, 0, BBTOB(bp
->b_length
));
40 bp
->b_maps
[0].bm_bn
= blkno
;
47 * Generic btree root block init function
53 struct aghdr_init_data
*id
)
55 xfs_btree_init_block(mp
, bp
, id
->type
, 0, 0, id
->agno
, 0);
59 * Alloc btree root block init functions
65 struct aghdr_init_data
*id
)
67 struct xfs_alloc_rec
*arec
;
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
));
80 struct aghdr_init_data
*id
)
82 struct xfs_alloc_rec
*arec
;
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
));
92 * Reverse map root block init
98 struct aghdr_init_data
*id
)
100 struct xfs_btree_block
*block
= XFS_BUF_TO_BLOCK(bp
);
101 struct xfs_rmap_rec
*rrec
;
103 xfs_btree_init_block(mp
, bp
, XFS_BTNUM_RMAP
, 0, 4, id
->agno
, 0);
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
111 * Note: unlike mkfs, we never have to account for log
112 * space when growing the data regions
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
);
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
);
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
) -
132 rrec
->rm_owner
= cpu_to_be64(XFS_RMAP_OWN_INOBT
);
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
);
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
);
149 be16_add_cpu(&block
->bb_numrecs
, 1);
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.
163 struct xfs_mount
*mp
,
165 struct aghdr_init_data
*id
)
167 struct xfs_dsb
*dsb
= XFS_BUF_TO_SBP(bp
);
169 xfs_sb_to_disk(dsb
, &mp
->m_sb
);
170 dsb
->sb_inprogress
= 1;
175 struct xfs_mount
*mp
,
177 struct aghdr_init_data
*id
)
179 struct xfs_agf
*agf
= XFS_BUF_TO_AGF(bp
);
180 xfs_extlen_t tmpsize
;
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);
197 agf
->agf_flfirst
= cpu_to_be32(1);
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(
208 agf
->agf_refcount_level
= cpu_to_be32(1);
209 agf
->agf_refcount_blocks
= cpu_to_be32(1);
215 struct xfs_mount
*mp
,
217 struct aghdr_init_data
*id
)
219 struct xfs_agfl
*agfl
= XFS_BUF_TO_AGFL(bp
);
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
);
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
);
236 struct xfs_mount
*mp
,
238 struct aghdr_init_data
*id
)
240 struct xfs_agi
*agi
= XFS_BUF_TO_AGI(bp
);
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
);
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);
259 for (bucket
= 0; bucket
< XFS_AGI_UNLINKED_BUCKETS
; bucket
++)
260 agi
->agi_unlinked
[bucket
] = cpu_to_be32(NULLAGINO
);
263 typedef void (*aghdr_init_work_f
)(struct xfs_mount
*mp
, struct xfs_buf
*bp
,
264 struct aghdr_init_data
*id
);
267 struct xfs_mount
*mp
,
268 struct aghdr_init_data
*id
,
269 aghdr_init_work_f work
,
270 const struct xfs_buf_ops
*ops
)
275 bp
= xfs_get_aghdr_buf(mp
, id
->daddr
, id
->numblks
, 0, ops
);
281 xfs_buf_delwri_queue(bp
, &id
->buffer_list
);
286 struct xfs_aghdr_grow_data
{
289 const struct xfs_buf_ops
*ops
;
290 aghdr_init_work_f work
;
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.
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.
307 struct xfs_mount
*mp
,
308 struct aghdr_init_data
*id
)
311 struct xfs_aghdr_grow_data aghdr_data
[] = {
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
,
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
,
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
,
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
,
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
,
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
,
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
,
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
)
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
)
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
)
385 { /* NULL terminating block */
386 .daddr
= XFS_BUF_DADDR_NULL
,
389 struct xfs_aghdr_grow_data
*dp
;
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
++) {
398 id
->daddr
= dp
->daddr
;
399 id
->numblks
= dp
->numblks
;
401 error
= xfs_ag_init_hdr(mp
, id
, dp
->work
, dp
->ops
);
409 * Extent the AG indicated by the @id by the length passed in
413 struct xfs_mount
*mp
,
414 struct xfs_trans
*tp
,
415 struct aghdr_init_data
*id
,
418 struct xfs_owner_info oinfo
;
425 * Change the agi length.
427 error
= xfs_ialloc_read_agi(mp
, tp
, id
->agno
, &bp
);
431 agi
= XFS_BUF_TO_AGI(bp
);
432 be32_add_cpu(&agi
->agi_length
, len
);
433 ASSERT(id
->agno
== mp
->m_sb
.sb_agcount
- 1 ||
434 be32_to_cpu(agi
->agi_length
) == mp
->m_sb
.sb_agblocks
);
435 xfs_ialloc_log_agi(tp
, bp
, XFS_AGI_LENGTH
);
440 error
= xfs_alloc_read_agf(mp
, tp
, id
->agno
, 0, &bp
);
444 agf
= XFS_BUF_TO_AGF(bp
);
445 be32_add_cpu(&agf
->agf_length
, len
);
446 ASSERT(agf
->agf_length
== agi
->agi_length
);
447 xfs_alloc_log_agf(tp
, bp
, XFS_AGF_LENGTH
);
450 * Free the new space.
452 * XFS_RMAP_OWN_NULL is used here to tell the rmap btree that
453 * this doesn't actually exist in the rmap btree.
455 xfs_rmap_ag_owner(&oinfo
, XFS_RMAP_OWN_NULL
);
456 error
= xfs_rmap_free(tp
, bp
, id
->agno
,
457 be32_to_cpu(agf
->agf_length
) - len
,
462 return xfs_free_extent(tp
, XFS_AGB_TO_FSB(mp
, id
->agno
,
463 be32_to_cpu(agf
->agf_length
) - len
),
464 len
, &oinfo
, XFS_AG_RESV_NONE
);