xref: /openbmc/linux/fs/xfs/libxfs/xfs_ag.c (revision f8bcb061)
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 "xfs.h"
9 #include "xfs_fs.h"
10 #include "xfs_shared.h"
11 #include "xfs_format.h"
12 #include "xfs_trans_resv.h"
13 #include "xfs_bit.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 #include "xfs_error.h"
26 #include "xfs_bmap.h"
27 #include "xfs_defer.h"
28 #include "xfs_log_format.h"
29 #include "xfs_trans.h"
30 
31 static int
32 xfs_get_aghdr_buf(
33 	struct xfs_mount	*mp,
34 	xfs_daddr_t		blkno,
35 	size_t			numblks,
36 	struct xfs_buf		**bpp,
37 	const struct xfs_buf_ops *ops)
38 {
39 	struct xfs_buf		*bp;
40 	int			error;
41 
42 	error = xfs_buf_get_uncached(mp->m_ddev_targp, numblks, 0, &bp);
43 	if (error)
44 		return error;
45 
46 	xfs_buf_zero(bp, 0, BBTOB(bp->b_length));
47 	bp->b_bn = blkno;
48 	bp->b_maps[0].bm_bn = blkno;
49 	bp->b_ops = ops;
50 
51 	*bpp = bp;
52 	return 0;
53 }
54 
55 static inline bool is_log_ag(struct xfs_mount *mp, struct aghdr_init_data *id)
56 {
57 	return mp->m_sb.sb_logstart > 0 &&
58 	       id->agno == XFS_FSB_TO_AGNO(mp, mp->m_sb.sb_logstart);
59 }
60 
61 /*
62  * Generic btree root block init function
63  */
64 static void
65 xfs_btroot_init(
66 	struct xfs_mount	*mp,
67 	struct xfs_buf		*bp,
68 	struct aghdr_init_data	*id)
69 {
70 	xfs_btree_init_block(mp, bp, id->type, 0, 0, id->agno);
71 }
72 
73 /* Finish initializing a free space btree. */
74 static void
75 xfs_freesp_init_recs(
76 	struct xfs_mount	*mp,
77 	struct xfs_buf		*bp,
78 	struct aghdr_init_data	*id)
79 {
80 	struct xfs_alloc_rec	*arec;
81 	struct xfs_btree_block	*block = XFS_BUF_TO_BLOCK(bp);
82 
83 	arec = XFS_ALLOC_REC_ADDR(mp, XFS_BUF_TO_BLOCK(bp), 1);
84 	arec->ar_startblock = cpu_to_be32(mp->m_ag_prealloc_blocks);
85 
86 	if (is_log_ag(mp, id)) {
87 		struct xfs_alloc_rec	*nrec;
88 		xfs_agblock_t		start = XFS_FSB_TO_AGBNO(mp,
89 							mp->m_sb.sb_logstart);
90 
91 		ASSERT(start >= mp->m_ag_prealloc_blocks);
92 		if (start != mp->m_ag_prealloc_blocks) {
93 			/*
94 			 * Modify first record to pad stripe align of log
95 			 */
96 			arec->ar_blockcount = cpu_to_be32(start -
97 						mp->m_ag_prealloc_blocks);
98 			nrec = arec + 1;
99 
100 			/*
101 			 * Insert second record at start of internal log
102 			 * which then gets trimmed.
103 			 */
104 			nrec->ar_startblock = cpu_to_be32(
105 					be32_to_cpu(arec->ar_startblock) +
106 					be32_to_cpu(arec->ar_blockcount));
107 			arec = nrec;
108 			be16_add_cpu(&block->bb_numrecs, 1);
109 		}
110 		/*
111 		 * Change record start to after the internal log
112 		 */
113 		be32_add_cpu(&arec->ar_startblock, mp->m_sb.sb_logblocks);
114 	}
115 
116 	/*
117 	 * Calculate the record block count and check for the case where
118 	 * the log might have consumed all available space in the AG. If
119 	 * so, reset the record count to 0 to avoid exposure of an invalid
120 	 * record start block.
121 	 */
122 	arec->ar_blockcount = cpu_to_be32(id->agsize -
123 					  be32_to_cpu(arec->ar_startblock));
124 	if (!arec->ar_blockcount)
125 		block->bb_numrecs = 0;
126 }
127 
128 /*
129  * Alloc btree root block init functions
130  */
131 static void
132 xfs_bnoroot_init(
133 	struct xfs_mount	*mp,
134 	struct xfs_buf		*bp,
135 	struct aghdr_init_data	*id)
136 {
137 	xfs_btree_init_block(mp, bp, XFS_BTNUM_BNO, 0, 1, id->agno);
138 	xfs_freesp_init_recs(mp, bp, id);
139 }
140 
141 static void
142 xfs_cntroot_init(
143 	struct xfs_mount	*mp,
144 	struct xfs_buf		*bp,
145 	struct aghdr_init_data	*id)
146 {
147 	xfs_btree_init_block(mp, bp, XFS_BTNUM_CNT, 0, 1, id->agno);
148 	xfs_freesp_init_recs(mp, bp, id);
149 }
150 
151 /*
152  * Reverse map root block init
153  */
154 static void
155 xfs_rmaproot_init(
156 	struct xfs_mount	*mp,
157 	struct xfs_buf		*bp,
158 	struct aghdr_init_data	*id)
159 {
160 	struct xfs_btree_block	*block = XFS_BUF_TO_BLOCK(bp);
161 	struct xfs_rmap_rec	*rrec;
162 
163 	xfs_btree_init_block(mp, bp, XFS_BTNUM_RMAP, 0, 4, id->agno);
164 
165 	/*
166 	 * mark the AG header regions as static metadata The BNO
167 	 * btree block is the first block after the headers, so
168 	 * it's location defines the size of region the static
169 	 * metadata consumes.
170 	 *
171 	 * Note: unlike mkfs, we never have to account for log
172 	 * space when growing the data regions
173 	 */
174 	rrec = XFS_RMAP_REC_ADDR(block, 1);
175 	rrec->rm_startblock = 0;
176 	rrec->rm_blockcount = cpu_to_be32(XFS_BNO_BLOCK(mp));
177 	rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_FS);
178 	rrec->rm_offset = 0;
179 
180 	/* account freespace btree root blocks */
181 	rrec = XFS_RMAP_REC_ADDR(block, 2);
182 	rrec->rm_startblock = cpu_to_be32(XFS_BNO_BLOCK(mp));
183 	rrec->rm_blockcount = cpu_to_be32(2);
184 	rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_AG);
185 	rrec->rm_offset = 0;
186 
187 	/* account inode btree root blocks */
188 	rrec = XFS_RMAP_REC_ADDR(block, 3);
189 	rrec->rm_startblock = cpu_to_be32(XFS_IBT_BLOCK(mp));
190 	rrec->rm_blockcount = cpu_to_be32(XFS_RMAP_BLOCK(mp) -
191 					  XFS_IBT_BLOCK(mp));
192 	rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_INOBT);
193 	rrec->rm_offset = 0;
194 
195 	/* account for rmap btree root */
196 	rrec = XFS_RMAP_REC_ADDR(block, 4);
197 	rrec->rm_startblock = cpu_to_be32(XFS_RMAP_BLOCK(mp));
198 	rrec->rm_blockcount = cpu_to_be32(1);
199 	rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_AG);
200 	rrec->rm_offset = 0;
201 
202 	/* account for refc btree root */
203 	if (xfs_sb_version_hasreflink(&mp->m_sb)) {
204 		rrec = XFS_RMAP_REC_ADDR(block, 5);
205 		rrec->rm_startblock = cpu_to_be32(xfs_refc_block(mp));
206 		rrec->rm_blockcount = cpu_to_be32(1);
207 		rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_REFC);
208 		rrec->rm_offset = 0;
209 		be16_add_cpu(&block->bb_numrecs, 1);
210 	}
211 
212 	/* account for the log space */
213 	if (is_log_ag(mp, id)) {
214 		rrec = XFS_RMAP_REC_ADDR(block,
215 				be16_to_cpu(block->bb_numrecs) + 1);
216 		rrec->rm_startblock = cpu_to_be32(
217 				XFS_FSB_TO_AGBNO(mp, mp->m_sb.sb_logstart));
218 		rrec->rm_blockcount = cpu_to_be32(mp->m_sb.sb_logblocks);
219 		rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_LOG);
220 		rrec->rm_offset = 0;
221 		be16_add_cpu(&block->bb_numrecs, 1);
222 	}
223 }
224 
225 /*
226  * Initialise new secondary superblocks with the pre-grow geometry, but mark
227  * them as "in progress" so we know they haven't yet been activated. This will
228  * get cleared when the update with the new geometry information is done after
229  * changes to the primary are committed. This isn't strictly necessary, but we
230  * get it for free with the delayed buffer write lists and it means we can tell
231  * if a grow operation didn't complete properly after the fact.
232  */
233 static void
234 xfs_sbblock_init(
235 	struct xfs_mount	*mp,
236 	struct xfs_buf		*bp,
237 	struct aghdr_init_data	*id)
238 {
239 	struct xfs_dsb		*dsb = bp->b_addr;
240 
241 	xfs_sb_to_disk(dsb, &mp->m_sb);
242 	dsb->sb_inprogress = 1;
243 }
244 
245 static void
246 xfs_agfblock_init(
247 	struct xfs_mount	*mp,
248 	struct xfs_buf		*bp,
249 	struct aghdr_init_data	*id)
250 {
251 	struct xfs_agf		*agf = bp->b_addr;
252 	xfs_extlen_t		tmpsize;
253 
254 	agf->agf_magicnum = cpu_to_be32(XFS_AGF_MAGIC);
255 	agf->agf_versionnum = cpu_to_be32(XFS_AGF_VERSION);
256 	agf->agf_seqno = cpu_to_be32(id->agno);
257 	agf->agf_length = cpu_to_be32(id->agsize);
258 	agf->agf_roots[XFS_BTNUM_BNOi] = cpu_to_be32(XFS_BNO_BLOCK(mp));
259 	agf->agf_roots[XFS_BTNUM_CNTi] = cpu_to_be32(XFS_CNT_BLOCK(mp));
260 	agf->agf_levels[XFS_BTNUM_BNOi] = cpu_to_be32(1);
261 	agf->agf_levels[XFS_BTNUM_CNTi] = cpu_to_be32(1);
262 	if (xfs_sb_version_hasrmapbt(&mp->m_sb)) {
263 		agf->agf_roots[XFS_BTNUM_RMAPi] =
264 					cpu_to_be32(XFS_RMAP_BLOCK(mp));
265 		agf->agf_levels[XFS_BTNUM_RMAPi] = cpu_to_be32(1);
266 		agf->agf_rmap_blocks = cpu_to_be32(1);
267 	}
268 
269 	agf->agf_flfirst = cpu_to_be32(1);
270 	agf->agf_fllast = 0;
271 	agf->agf_flcount = 0;
272 	tmpsize = id->agsize - mp->m_ag_prealloc_blocks;
273 	agf->agf_freeblks = cpu_to_be32(tmpsize);
274 	agf->agf_longest = cpu_to_be32(tmpsize);
275 	if (xfs_sb_version_hascrc(&mp->m_sb))
276 		uuid_copy(&agf->agf_uuid, &mp->m_sb.sb_meta_uuid);
277 	if (xfs_sb_version_hasreflink(&mp->m_sb)) {
278 		agf->agf_refcount_root = cpu_to_be32(
279 				xfs_refc_block(mp));
280 		agf->agf_refcount_level = cpu_to_be32(1);
281 		agf->agf_refcount_blocks = cpu_to_be32(1);
282 	}
283 
284 	if (is_log_ag(mp, id)) {
285 		int64_t	logblocks = mp->m_sb.sb_logblocks;
286 
287 		be32_add_cpu(&agf->agf_freeblks, -logblocks);
288 		agf->agf_longest = cpu_to_be32(id->agsize -
289 			XFS_FSB_TO_AGBNO(mp, mp->m_sb.sb_logstart) - logblocks);
290 	}
291 }
292 
293 static void
294 xfs_agflblock_init(
295 	struct xfs_mount	*mp,
296 	struct xfs_buf		*bp,
297 	struct aghdr_init_data	*id)
298 {
299 	struct xfs_agfl		*agfl = XFS_BUF_TO_AGFL(bp);
300 	__be32			*agfl_bno;
301 	int			bucket;
302 
303 	if (xfs_sb_version_hascrc(&mp->m_sb)) {
304 		agfl->agfl_magicnum = cpu_to_be32(XFS_AGFL_MAGIC);
305 		agfl->agfl_seqno = cpu_to_be32(id->agno);
306 		uuid_copy(&agfl->agfl_uuid, &mp->m_sb.sb_meta_uuid);
307 	}
308 
309 	agfl_bno = xfs_buf_to_agfl_bno(bp);
310 	for (bucket = 0; bucket < xfs_agfl_size(mp); bucket++)
311 		agfl_bno[bucket] = cpu_to_be32(NULLAGBLOCK);
312 }
313 
314 static void
315 xfs_agiblock_init(
316 	struct xfs_mount	*mp,
317 	struct xfs_buf		*bp,
318 	struct aghdr_init_data	*id)
319 {
320 	struct xfs_agi		*agi = bp->b_addr;
321 	int			bucket;
322 
323 	agi->agi_magicnum = cpu_to_be32(XFS_AGI_MAGIC);
324 	agi->agi_versionnum = cpu_to_be32(XFS_AGI_VERSION);
325 	agi->agi_seqno = cpu_to_be32(id->agno);
326 	agi->agi_length = cpu_to_be32(id->agsize);
327 	agi->agi_count = 0;
328 	agi->agi_root = cpu_to_be32(XFS_IBT_BLOCK(mp));
329 	agi->agi_level = cpu_to_be32(1);
330 	agi->agi_freecount = 0;
331 	agi->agi_newino = cpu_to_be32(NULLAGINO);
332 	agi->agi_dirino = cpu_to_be32(NULLAGINO);
333 	if (xfs_sb_version_hascrc(&mp->m_sb))
334 		uuid_copy(&agi->agi_uuid, &mp->m_sb.sb_meta_uuid);
335 	if (xfs_sb_version_hasfinobt(&mp->m_sb)) {
336 		agi->agi_free_root = cpu_to_be32(XFS_FIBT_BLOCK(mp));
337 		agi->agi_free_level = cpu_to_be32(1);
338 	}
339 	for (bucket = 0; bucket < XFS_AGI_UNLINKED_BUCKETS; bucket++)
340 		agi->agi_unlinked[bucket] = cpu_to_be32(NULLAGINO);
341 	if (xfs_sb_version_hasinobtcounts(&mp->m_sb)) {
342 		agi->agi_iblocks = cpu_to_be32(1);
343 		if (xfs_sb_version_hasfinobt(&mp->m_sb))
344 			agi->agi_fblocks = cpu_to_be32(1);
345 	}
346 }
347 
348 typedef void (*aghdr_init_work_f)(struct xfs_mount *mp, struct xfs_buf *bp,
349 				  struct aghdr_init_data *id);
350 static int
351 xfs_ag_init_hdr(
352 	struct xfs_mount	*mp,
353 	struct aghdr_init_data	*id,
354 	aghdr_init_work_f	work,
355 	const struct xfs_buf_ops *ops)
356 {
357 	struct xfs_buf		*bp;
358 	int			error;
359 
360 	error = xfs_get_aghdr_buf(mp, id->daddr, id->numblks, &bp, ops);
361 	if (error)
362 		return error;
363 
364 	(*work)(mp, bp, id);
365 
366 	xfs_buf_delwri_queue(bp, &id->buffer_list);
367 	xfs_buf_relse(bp);
368 	return 0;
369 }
370 
371 struct xfs_aghdr_grow_data {
372 	xfs_daddr_t		daddr;
373 	size_t			numblks;
374 	const struct xfs_buf_ops *ops;
375 	aghdr_init_work_f	work;
376 	xfs_btnum_t		type;
377 	bool			need_init;
378 };
379 
380 /*
381  * Prepare new AG headers to be written to disk. We use uncached buffers here,
382  * as it is assumed these new AG headers are currently beyond the currently
383  * valid filesystem address space. Using cached buffers would trip over EOFS
384  * corruption detection alogrithms in the buffer cache lookup routines.
385  *
386  * This is a non-transactional function, but the prepared buffers are added to a
387  * delayed write buffer list supplied by the caller so they can submit them to
388  * disk and wait on them as required.
389  */
390 int
391 xfs_ag_init_headers(
392 	struct xfs_mount	*mp,
393 	struct aghdr_init_data	*id)
394 
395 {
396 	struct xfs_aghdr_grow_data aghdr_data[] = {
397 	{ /* SB */
398 		.daddr = XFS_AG_DADDR(mp, id->agno, XFS_SB_DADDR),
399 		.numblks = XFS_FSS_TO_BB(mp, 1),
400 		.ops = &xfs_sb_buf_ops,
401 		.work = &xfs_sbblock_init,
402 		.need_init = true
403 	},
404 	{ /* AGF */
405 		.daddr = XFS_AG_DADDR(mp, id->agno, XFS_AGF_DADDR(mp)),
406 		.numblks = XFS_FSS_TO_BB(mp, 1),
407 		.ops = &xfs_agf_buf_ops,
408 		.work = &xfs_agfblock_init,
409 		.need_init = true
410 	},
411 	{ /* AGFL */
412 		.daddr = XFS_AG_DADDR(mp, id->agno, XFS_AGFL_DADDR(mp)),
413 		.numblks = XFS_FSS_TO_BB(mp, 1),
414 		.ops = &xfs_agfl_buf_ops,
415 		.work = &xfs_agflblock_init,
416 		.need_init = true
417 	},
418 	{ /* AGI */
419 		.daddr = XFS_AG_DADDR(mp, id->agno, XFS_AGI_DADDR(mp)),
420 		.numblks = XFS_FSS_TO_BB(mp, 1),
421 		.ops = &xfs_agi_buf_ops,
422 		.work = &xfs_agiblock_init,
423 		.need_init = true
424 	},
425 	{ /* BNO root block */
426 		.daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_BNO_BLOCK(mp)),
427 		.numblks = BTOBB(mp->m_sb.sb_blocksize),
428 		.ops = &xfs_bnobt_buf_ops,
429 		.work = &xfs_bnoroot_init,
430 		.need_init = true
431 	},
432 	{ /* CNT root block */
433 		.daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_CNT_BLOCK(mp)),
434 		.numblks = BTOBB(mp->m_sb.sb_blocksize),
435 		.ops = &xfs_cntbt_buf_ops,
436 		.work = &xfs_cntroot_init,
437 		.need_init = true
438 	},
439 	{ /* INO root block */
440 		.daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_IBT_BLOCK(mp)),
441 		.numblks = BTOBB(mp->m_sb.sb_blocksize),
442 		.ops = &xfs_inobt_buf_ops,
443 		.work = &xfs_btroot_init,
444 		.type = XFS_BTNUM_INO,
445 		.need_init = true
446 	},
447 	{ /* FINO root block */
448 		.daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_FIBT_BLOCK(mp)),
449 		.numblks = BTOBB(mp->m_sb.sb_blocksize),
450 		.ops = &xfs_finobt_buf_ops,
451 		.work = &xfs_btroot_init,
452 		.type = XFS_BTNUM_FINO,
453 		.need_init =  xfs_sb_version_hasfinobt(&mp->m_sb)
454 	},
455 	{ /* RMAP root block */
456 		.daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_RMAP_BLOCK(mp)),
457 		.numblks = BTOBB(mp->m_sb.sb_blocksize),
458 		.ops = &xfs_rmapbt_buf_ops,
459 		.work = &xfs_rmaproot_init,
460 		.need_init = xfs_sb_version_hasrmapbt(&mp->m_sb)
461 	},
462 	{ /* REFC root block */
463 		.daddr = XFS_AGB_TO_DADDR(mp, id->agno, xfs_refc_block(mp)),
464 		.numblks = BTOBB(mp->m_sb.sb_blocksize),
465 		.ops = &xfs_refcountbt_buf_ops,
466 		.work = &xfs_btroot_init,
467 		.type = XFS_BTNUM_REFC,
468 		.need_init = xfs_sb_version_hasreflink(&mp->m_sb)
469 	},
470 	{ /* NULL terminating block */
471 		.daddr = XFS_BUF_DADDR_NULL,
472 	}
473 	};
474 	struct  xfs_aghdr_grow_data *dp;
475 	int			error = 0;
476 
477 	/* Account for AG free space in new AG */
478 	id->nfree += id->agsize - mp->m_ag_prealloc_blocks;
479 	for (dp = &aghdr_data[0]; dp->daddr != XFS_BUF_DADDR_NULL; dp++) {
480 		if (!dp->need_init)
481 			continue;
482 
483 		id->daddr = dp->daddr;
484 		id->numblks = dp->numblks;
485 		id->type = dp->type;
486 		error = xfs_ag_init_hdr(mp, id, dp->work, dp->ops);
487 		if (error)
488 			break;
489 	}
490 	return error;
491 }
492 
493 int
494 xfs_ag_shrink_space(
495 	struct xfs_mount	*mp,
496 	struct xfs_trans	**tpp,
497 	xfs_agnumber_t		agno,
498 	xfs_extlen_t		delta)
499 {
500 	struct xfs_alloc_arg	args = {
501 		.tp	= *tpp,
502 		.mp	= mp,
503 		.type	= XFS_ALLOCTYPE_THIS_BNO,
504 		.minlen = delta,
505 		.maxlen = delta,
506 		.oinfo	= XFS_RMAP_OINFO_SKIP_UPDATE,
507 		.resv	= XFS_AG_RESV_NONE,
508 		.prod	= 1
509 	};
510 	struct xfs_buf		*agibp, *agfbp;
511 	struct xfs_agi		*agi;
512 	struct xfs_agf		*agf;
513 	int			error, err2;
514 
515 	ASSERT(agno == mp->m_sb.sb_agcount - 1);
516 	error = xfs_ialloc_read_agi(mp, *tpp, agno, &agibp);
517 	if (error)
518 		return error;
519 
520 	agi = agibp->b_addr;
521 
522 	error = xfs_alloc_read_agf(mp, *tpp, agno, 0, &agfbp);
523 	if (error)
524 		return error;
525 
526 	agf = agfbp->b_addr;
527 	/* some extra paranoid checks before we shrink the ag */
528 	if (XFS_IS_CORRUPT(mp, agf->agf_length != agi->agi_length))
529 		return -EFSCORRUPTED;
530 	if (delta >= agi->agi_length)
531 		return -EINVAL;
532 
533 	args.fsbno = XFS_AGB_TO_FSB(mp, agno,
534 				    be32_to_cpu(agi->agi_length) - delta);
535 
536 	/*
537 	 * Disable perag reservations so it doesn't cause the allocation request
538 	 * to fail. We'll reestablish reservation before we return.
539 	 */
540 	error = xfs_ag_resv_free(agibp->b_pag);
541 	if (error)
542 		return error;
543 
544 	/* internal log shouldn't also show up in the free space btrees */
545 	error = xfs_alloc_vextent(&args);
546 	if (!error && args.agbno == NULLAGBLOCK)
547 		error = -ENOSPC;
548 
549 	if (error) {
550 		/*
551 		 * if extent allocation fails, need to roll the transaction to
552 		 * ensure that the AGFL fixup has been committed anyway.
553 		 */
554 		xfs_trans_bhold(*tpp, agfbp);
555 		err2 = xfs_trans_roll(tpp);
556 		if (err2)
557 			return err2;
558 		xfs_trans_bjoin(*tpp, agfbp);
559 		goto resv_init_out;
560 	}
561 
562 	/*
563 	 * if successfully deleted from freespace btrees, need to confirm
564 	 * per-AG reservation works as expected.
565 	 */
566 	be32_add_cpu(&agi->agi_length, -delta);
567 	be32_add_cpu(&agf->agf_length, -delta);
568 
569 	err2 = xfs_ag_resv_init(agibp->b_pag, *tpp);
570 	if (err2) {
571 		be32_add_cpu(&agi->agi_length, delta);
572 		be32_add_cpu(&agf->agf_length, delta);
573 		if (err2 != -ENOSPC)
574 			goto resv_err;
575 
576 		__xfs_bmap_add_free(*tpp, args.fsbno, delta, NULL, true);
577 
578 		/*
579 		 * Roll the transaction before trying to re-init the per-ag
580 		 * reservation. The new transaction is clean so it will cancel
581 		 * without any side effects.
582 		 */
583 		error = xfs_defer_finish(tpp);
584 		if (error)
585 			return error;
586 
587 		error = -ENOSPC;
588 		goto resv_init_out;
589 	}
590 	xfs_ialloc_log_agi(*tpp, agibp, XFS_AGI_LENGTH);
591 	xfs_alloc_log_agf(*tpp, agfbp, XFS_AGF_LENGTH);
592 	return 0;
593 resv_init_out:
594 	err2 = xfs_ag_resv_init(agibp->b_pag, *tpp);
595 	if (!err2)
596 		return error;
597 resv_err:
598 	xfs_warn(mp, "Error %d reserving per-AG metadata reserve pool.", err2);
599 	xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
600 	return err2;
601 }
602 
603 /*
604  * Extent the AG indicated by the @id by the length passed in
605  */
606 int
607 xfs_ag_extend_space(
608 	struct xfs_mount	*mp,
609 	struct xfs_trans	*tp,
610 	struct aghdr_init_data	*id,
611 	xfs_extlen_t		len)
612 {
613 	struct xfs_buf		*bp;
614 	struct xfs_agi		*agi;
615 	struct xfs_agf		*agf;
616 	int			error;
617 
618 	/*
619 	 * Change the agi length.
620 	 */
621 	error = xfs_ialloc_read_agi(mp, tp, id->agno, &bp);
622 	if (error)
623 		return error;
624 
625 	agi = bp->b_addr;
626 	be32_add_cpu(&agi->agi_length, len);
627 	ASSERT(id->agno == mp->m_sb.sb_agcount - 1 ||
628 	       be32_to_cpu(agi->agi_length) == mp->m_sb.sb_agblocks);
629 	xfs_ialloc_log_agi(tp, bp, XFS_AGI_LENGTH);
630 
631 	/*
632 	 * Change agf length.
633 	 */
634 	error = xfs_alloc_read_agf(mp, tp, id->agno, 0, &bp);
635 	if (error)
636 		return error;
637 
638 	agf = bp->b_addr;
639 	be32_add_cpu(&agf->agf_length, len);
640 	ASSERT(agf->agf_length == agi->agi_length);
641 	xfs_alloc_log_agf(tp, bp, XFS_AGF_LENGTH);
642 
643 	/*
644 	 * Free the new space.
645 	 *
646 	 * XFS_RMAP_OINFO_SKIP_UPDATE is used here to tell the rmap btree that
647 	 * this doesn't actually exist in the rmap btree.
648 	 */
649 	error = xfs_rmap_free(tp, bp, id->agno,
650 				be32_to_cpu(agf->agf_length) - len,
651 				len, &XFS_RMAP_OINFO_SKIP_UPDATE);
652 	if (error)
653 		return error;
654 
655 	return  xfs_free_extent(tp, XFS_AGB_TO_FSB(mp, id->agno,
656 					be32_to_cpu(agf->agf_length) - len),
657 				len, &XFS_RMAP_OINFO_SKIP_UPDATE,
658 				XFS_AG_RESV_NONE);
659 }
660 
661 /* Retrieve AG geometry. */
662 int
663 xfs_ag_get_geometry(
664 	struct xfs_mount	*mp,
665 	xfs_agnumber_t		agno,
666 	struct xfs_ag_geometry	*ageo)
667 {
668 	struct xfs_buf		*agi_bp;
669 	struct xfs_buf		*agf_bp;
670 	struct xfs_agi		*agi;
671 	struct xfs_agf		*agf;
672 	struct xfs_perag	*pag;
673 	unsigned int		freeblks;
674 	int			error;
675 
676 	if (agno >= mp->m_sb.sb_agcount)
677 		return -EINVAL;
678 
679 	/* Lock the AG headers. */
680 	error = xfs_ialloc_read_agi(mp, NULL, agno, &agi_bp);
681 	if (error)
682 		return error;
683 	error = xfs_alloc_read_agf(mp, NULL, agno, 0, &agf_bp);
684 	if (error)
685 		goto out_agi;
686 
687 	pag = agi_bp->b_pag;
688 
689 	/* Fill out form. */
690 	memset(ageo, 0, sizeof(*ageo));
691 	ageo->ag_number = agno;
692 
693 	agi = agi_bp->b_addr;
694 	ageo->ag_icount = be32_to_cpu(agi->agi_count);
695 	ageo->ag_ifree = be32_to_cpu(agi->agi_freecount);
696 
697 	agf = agf_bp->b_addr;
698 	ageo->ag_length = be32_to_cpu(agf->agf_length);
699 	freeblks = pag->pagf_freeblks +
700 		   pag->pagf_flcount +
701 		   pag->pagf_btreeblks -
702 		   xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE);
703 	ageo->ag_freeblks = freeblks;
704 	xfs_ag_geom_health(pag, ageo);
705 
706 	/* Release resources. */
707 	xfs_buf_relse(agf_bp);
708 out_agi:
709 	xfs_buf_relse(agi_bp);
710 	return error;
711 }
712