xref: /openbmc/linux/fs/xfs/libxfs/xfs_ag.c (revision 1edd0337)
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 #include "xfs_trace.h"
31 #include "xfs_inode.h"
32 #include "xfs_icache.h"
33 
34 
35 /*
36  * Passive reference counting access wrappers to the perag structures.  If the
37  * per-ag structure is to be freed, the freeing code is responsible for cleaning
38  * up objects with passive references before freeing the structure. This is
39  * things like cached buffers.
40  */
41 struct xfs_perag *
42 xfs_perag_get(
43 	struct xfs_mount	*mp,
44 	xfs_agnumber_t		agno)
45 {
46 	struct xfs_perag	*pag;
47 
48 	rcu_read_lock();
49 	pag = radix_tree_lookup(&mp->m_perag_tree, agno);
50 	if (pag) {
51 		trace_xfs_perag_get(pag, _RET_IP_);
52 		ASSERT(atomic_read(&pag->pag_ref) >= 0);
53 		atomic_inc(&pag->pag_ref);
54 	}
55 	rcu_read_unlock();
56 	return pag;
57 }
58 
59 /*
60  * search from @first to find the next perag with the given tag set.
61  */
62 struct xfs_perag *
63 xfs_perag_get_tag(
64 	struct xfs_mount	*mp,
65 	xfs_agnumber_t		first,
66 	unsigned int		tag)
67 {
68 	struct xfs_perag	*pag;
69 	int			found;
70 
71 	rcu_read_lock();
72 	found = radix_tree_gang_lookup_tag(&mp->m_perag_tree,
73 					(void **)&pag, first, 1, tag);
74 	if (found <= 0) {
75 		rcu_read_unlock();
76 		return NULL;
77 	}
78 	trace_xfs_perag_get_tag(pag, _RET_IP_);
79 	atomic_inc(&pag->pag_ref);
80 	rcu_read_unlock();
81 	return pag;
82 }
83 
84 void
85 xfs_perag_put(
86 	struct xfs_perag	*pag)
87 {
88 	trace_xfs_perag_put(pag, _RET_IP_);
89 	ASSERT(atomic_read(&pag->pag_ref) > 0);
90 	atomic_dec(&pag->pag_ref);
91 }
92 
93 /*
94  * Active references for perag structures. This is for short term access to the
95  * per ag structures for walking trees or accessing state. If an AG is being
96  * shrunk or is offline, then this will fail to find that AG and return NULL
97  * instead.
98  */
99 struct xfs_perag *
100 xfs_perag_grab(
101 	struct xfs_mount	*mp,
102 	xfs_agnumber_t		agno)
103 {
104 	struct xfs_perag	*pag;
105 
106 	rcu_read_lock();
107 	pag = radix_tree_lookup(&mp->m_perag_tree, agno);
108 	if (pag) {
109 		trace_xfs_perag_grab(pag, _RET_IP_);
110 		if (!atomic_inc_not_zero(&pag->pag_active_ref))
111 			pag = NULL;
112 	}
113 	rcu_read_unlock();
114 	return pag;
115 }
116 
117 /*
118  * search from @first to find the next perag with the given tag set.
119  */
120 struct xfs_perag *
121 xfs_perag_grab_tag(
122 	struct xfs_mount	*mp,
123 	xfs_agnumber_t		first,
124 	int			tag)
125 {
126 	struct xfs_perag	*pag;
127 	int			found;
128 
129 	rcu_read_lock();
130 	found = radix_tree_gang_lookup_tag(&mp->m_perag_tree,
131 					(void **)&pag, first, 1, tag);
132 	if (found <= 0) {
133 		rcu_read_unlock();
134 		return NULL;
135 	}
136 	trace_xfs_perag_grab_tag(pag, _RET_IP_);
137 	if (!atomic_inc_not_zero(&pag->pag_active_ref))
138 		pag = NULL;
139 	rcu_read_unlock();
140 	return pag;
141 }
142 
143 void
144 xfs_perag_rele(
145 	struct xfs_perag	*pag)
146 {
147 	trace_xfs_perag_rele(pag, _RET_IP_);
148 	if (atomic_dec_and_test(&pag->pag_active_ref))
149 		wake_up(&pag->pag_active_wq);
150 }
151 
152 /*
153  * xfs_initialize_perag_data
154  *
155  * Read in each per-ag structure so we can count up the number of
156  * allocated inodes, free inodes and used filesystem blocks as this
157  * information is no longer persistent in the superblock. Once we have
158  * this information, write it into the in-core superblock structure.
159  */
160 int
161 xfs_initialize_perag_data(
162 	struct xfs_mount	*mp,
163 	xfs_agnumber_t		agcount)
164 {
165 	xfs_agnumber_t		index;
166 	struct xfs_perag	*pag;
167 	struct xfs_sb		*sbp = &mp->m_sb;
168 	uint64_t		ifree = 0;
169 	uint64_t		ialloc = 0;
170 	uint64_t		bfree = 0;
171 	uint64_t		bfreelst = 0;
172 	uint64_t		btree = 0;
173 	uint64_t		fdblocks;
174 	int			error = 0;
175 
176 	for (index = 0; index < agcount; index++) {
177 		/*
178 		 * Read the AGF and AGI buffers to populate the per-ag
179 		 * structures for us.
180 		 */
181 		pag = xfs_perag_get(mp, index);
182 		error = xfs_alloc_read_agf(pag, NULL, 0, NULL);
183 		if (!error)
184 			error = xfs_ialloc_read_agi(pag, NULL, NULL);
185 		if (error) {
186 			xfs_perag_put(pag);
187 			return error;
188 		}
189 
190 		ifree += pag->pagi_freecount;
191 		ialloc += pag->pagi_count;
192 		bfree += pag->pagf_freeblks;
193 		bfreelst += pag->pagf_flcount;
194 		btree += pag->pagf_btreeblks;
195 		xfs_perag_put(pag);
196 	}
197 	fdblocks = bfree + bfreelst + btree;
198 
199 	/*
200 	 * If the new summary counts are obviously incorrect, fail the
201 	 * mount operation because that implies the AGFs are also corrupt.
202 	 * Clear FS_COUNTERS so that we don't unmount with a dirty log, which
203 	 * will prevent xfs_repair from fixing anything.
204 	 */
205 	if (fdblocks > sbp->sb_dblocks || ifree > ialloc) {
206 		xfs_alert(mp, "AGF corruption. Please run xfs_repair.");
207 		error = -EFSCORRUPTED;
208 		goto out;
209 	}
210 
211 	/* Overwrite incore superblock counters with just-read data */
212 	spin_lock(&mp->m_sb_lock);
213 	sbp->sb_ifree = ifree;
214 	sbp->sb_icount = ialloc;
215 	sbp->sb_fdblocks = fdblocks;
216 	spin_unlock(&mp->m_sb_lock);
217 
218 	xfs_reinit_percpu_counters(mp);
219 out:
220 	xfs_fs_mark_healthy(mp, XFS_SICK_FS_COUNTERS);
221 	return error;
222 }
223 
224 STATIC void
225 __xfs_free_perag(
226 	struct rcu_head	*head)
227 {
228 	struct xfs_perag *pag = container_of(head, struct xfs_perag, rcu_head);
229 
230 	ASSERT(!delayed_work_pending(&pag->pag_blockgc_work));
231 	kmem_free(pag);
232 }
233 
234 /*
235  * Free up the per-ag resources associated with the mount structure.
236  */
237 void
238 xfs_free_perag(
239 	struct xfs_mount	*mp)
240 {
241 	struct xfs_perag	*pag;
242 	xfs_agnumber_t		agno;
243 
244 	for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
245 		spin_lock(&mp->m_perag_lock);
246 		pag = radix_tree_delete(&mp->m_perag_tree, agno);
247 		spin_unlock(&mp->m_perag_lock);
248 		ASSERT(pag);
249 		XFS_IS_CORRUPT(pag->pag_mount, atomic_read(&pag->pag_ref) != 0);
250 
251 		cancel_delayed_work_sync(&pag->pag_blockgc_work);
252 		xfs_buf_hash_destroy(pag);
253 
254 		/* drop the mount's active reference */
255 		xfs_perag_rele(pag);
256 		XFS_IS_CORRUPT(pag->pag_mount,
257 				atomic_read(&pag->pag_active_ref) != 0);
258 		call_rcu(&pag->rcu_head, __xfs_free_perag);
259 	}
260 }
261 
262 /* Find the size of the AG, in blocks. */
263 static xfs_agblock_t
264 __xfs_ag_block_count(
265 	struct xfs_mount	*mp,
266 	xfs_agnumber_t		agno,
267 	xfs_agnumber_t		agcount,
268 	xfs_rfsblock_t		dblocks)
269 {
270 	ASSERT(agno < agcount);
271 
272 	if (agno < agcount - 1)
273 		return mp->m_sb.sb_agblocks;
274 	return dblocks - (agno * mp->m_sb.sb_agblocks);
275 }
276 
277 xfs_agblock_t
278 xfs_ag_block_count(
279 	struct xfs_mount	*mp,
280 	xfs_agnumber_t		agno)
281 {
282 	return __xfs_ag_block_count(mp, agno, mp->m_sb.sb_agcount,
283 			mp->m_sb.sb_dblocks);
284 }
285 
286 /* Calculate the first and last possible inode number in an AG. */
287 static void
288 __xfs_agino_range(
289 	struct xfs_mount	*mp,
290 	xfs_agblock_t		eoag,
291 	xfs_agino_t		*first,
292 	xfs_agino_t		*last)
293 {
294 	xfs_agblock_t		bno;
295 
296 	/*
297 	 * Calculate the first inode, which will be in the first
298 	 * cluster-aligned block after the AGFL.
299 	 */
300 	bno = round_up(XFS_AGFL_BLOCK(mp) + 1, M_IGEO(mp)->cluster_align);
301 	*first = XFS_AGB_TO_AGINO(mp, bno);
302 
303 	/*
304 	 * Calculate the last inode, which will be at the end of the
305 	 * last (aligned) cluster that can be allocated in the AG.
306 	 */
307 	bno = round_down(eoag, M_IGEO(mp)->cluster_align);
308 	*last = XFS_AGB_TO_AGINO(mp, bno) - 1;
309 }
310 
311 void
312 xfs_agino_range(
313 	struct xfs_mount	*mp,
314 	xfs_agnumber_t		agno,
315 	xfs_agino_t		*first,
316 	xfs_agino_t		*last)
317 {
318 	return __xfs_agino_range(mp, xfs_ag_block_count(mp, agno), first, last);
319 }
320 
321 int
322 xfs_initialize_perag(
323 	struct xfs_mount	*mp,
324 	xfs_agnumber_t		agcount,
325 	xfs_rfsblock_t		dblocks,
326 	xfs_agnumber_t		*maxagi)
327 {
328 	struct xfs_perag	*pag;
329 	xfs_agnumber_t		index;
330 	xfs_agnumber_t		first_initialised = NULLAGNUMBER;
331 	int			error;
332 
333 	/*
334 	 * Walk the current per-ag tree so we don't try to initialise AGs
335 	 * that already exist (growfs case). Allocate and insert all the
336 	 * AGs we don't find ready for initialisation.
337 	 */
338 	for (index = 0; index < agcount; index++) {
339 		pag = xfs_perag_get(mp, index);
340 		if (pag) {
341 			xfs_perag_put(pag);
342 			continue;
343 		}
344 
345 		pag = kmem_zalloc(sizeof(*pag), KM_MAYFAIL);
346 		if (!pag) {
347 			error = -ENOMEM;
348 			goto out_unwind_new_pags;
349 		}
350 		pag->pag_agno = index;
351 		pag->pag_mount = mp;
352 
353 		error = radix_tree_preload(GFP_NOFS);
354 		if (error)
355 			goto out_free_pag;
356 
357 		spin_lock(&mp->m_perag_lock);
358 		if (radix_tree_insert(&mp->m_perag_tree, index, pag)) {
359 			WARN_ON_ONCE(1);
360 			spin_unlock(&mp->m_perag_lock);
361 			radix_tree_preload_end();
362 			error = -EEXIST;
363 			goto out_free_pag;
364 		}
365 		spin_unlock(&mp->m_perag_lock);
366 		radix_tree_preload_end();
367 
368 #ifdef __KERNEL__
369 		/* Place kernel structure only init below this point. */
370 		spin_lock_init(&pag->pag_ici_lock);
371 		spin_lock_init(&pag->pagb_lock);
372 		spin_lock_init(&pag->pag_state_lock);
373 		INIT_DELAYED_WORK(&pag->pag_blockgc_work, xfs_blockgc_worker);
374 		INIT_RADIX_TREE(&pag->pag_ici_root, GFP_ATOMIC);
375 		init_waitqueue_head(&pag->pagb_wait);
376 		init_waitqueue_head(&pag->pag_active_wq);
377 		pag->pagb_count = 0;
378 		pag->pagb_tree = RB_ROOT;
379 #endif /* __KERNEL__ */
380 
381 		error = xfs_buf_hash_init(pag);
382 		if (error)
383 			goto out_remove_pag;
384 
385 		/* Active ref owned by mount indicates AG is online. */
386 		atomic_set(&pag->pag_active_ref, 1);
387 
388 		/* first new pag is fully initialized */
389 		if (first_initialised == NULLAGNUMBER)
390 			first_initialised = index;
391 
392 		/*
393 		 * Pre-calculated geometry
394 		 */
395 		pag->block_count = __xfs_ag_block_count(mp, index, agcount,
396 				dblocks);
397 		pag->min_block = XFS_AGFL_BLOCK(mp);
398 		__xfs_agino_range(mp, pag->block_count, &pag->agino_min,
399 				&pag->agino_max);
400 	}
401 
402 	index = xfs_set_inode_alloc(mp, agcount);
403 
404 	if (maxagi)
405 		*maxagi = index;
406 
407 	mp->m_ag_prealloc_blocks = xfs_prealloc_blocks(mp);
408 	return 0;
409 
410 out_remove_pag:
411 	radix_tree_delete(&mp->m_perag_tree, index);
412 out_free_pag:
413 	kmem_free(pag);
414 out_unwind_new_pags:
415 	/* unwind any prior newly initialized pags */
416 	for (index = first_initialised; index < agcount; index++) {
417 		pag = radix_tree_delete(&mp->m_perag_tree, index);
418 		if (!pag)
419 			break;
420 		xfs_buf_hash_destroy(pag);
421 		kmem_free(pag);
422 	}
423 	return error;
424 }
425 
426 static int
427 xfs_get_aghdr_buf(
428 	struct xfs_mount	*mp,
429 	xfs_daddr_t		blkno,
430 	size_t			numblks,
431 	struct xfs_buf		**bpp,
432 	const struct xfs_buf_ops *ops)
433 {
434 	struct xfs_buf		*bp;
435 	int			error;
436 
437 	error = xfs_buf_get_uncached(mp->m_ddev_targp, numblks, 0, &bp);
438 	if (error)
439 		return error;
440 
441 	bp->b_maps[0].bm_bn = blkno;
442 	bp->b_ops = ops;
443 
444 	*bpp = bp;
445 	return 0;
446 }
447 
448 /*
449  * Generic btree root block init function
450  */
451 static void
452 xfs_btroot_init(
453 	struct xfs_mount	*mp,
454 	struct xfs_buf		*bp,
455 	struct aghdr_init_data	*id)
456 {
457 	xfs_btree_init_block(mp, bp, id->type, 0, 0, id->agno);
458 }
459 
460 /* Finish initializing a free space btree. */
461 static void
462 xfs_freesp_init_recs(
463 	struct xfs_mount	*mp,
464 	struct xfs_buf		*bp,
465 	struct aghdr_init_data	*id)
466 {
467 	struct xfs_alloc_rec	*arec;
468 	struct xfs_btree_block	*block = XFS_BUF_TO_BLOCK(bp);
469 
470 	arec = XFS_ALLOC_REC_ADDR(mp, XFS_BUF_TO_BLOCK(bp), 1);
471 	arec->ar_startblock = cpu_to_be32(mp->m_ag_prealloc_blocks);
472 
473 	if (xfs_ag_contains_log(mp, id->agno)) {
474 		struct xfs_alloc_rec	*nrec;
475 		xfs_agblock_t		start = XFS_FSB_TO_AGBNO(mp,
476 							mp->m_sb.sb_logstart);
477 
478 		ASSERT(start >= mp->m_ag_prealloc_blocks);
479 		if (start != mp->m_ag_prealloc_blocks) {
480 			/*
481 			 * Modify first record to pad stripe align of log
482 			 */
483 			arec->ar_blockcount = cpu_to_be32(start -
484 						mp->m_ag_prealloc_blocks);
485 			nrec = arec + 1;
486 
487 			/*
488 			 * Insert second record at start of internal log
489 			 * which then gets trimmed.
490 			 */
491 			nrec->ar_startblock = cpu_to_be32(
492 					be32_to_cpu(arec->ar_startblock) +
493 					be32_to_cpu(arec->ar_blockcount));
494 			arec = nrec;
495 			be16_add_cpu(&block->bb_numrecs, 1);
496 		}
497 		/*
498 		 * Change record start to after the internal log
499 		 */
500 		be32_add_cpu(&arec->ar_startblock, mp->m_sb.sb_logblocks);
501 	}
502 
503 	/*
504 	 * Calculate the record block count and check for the case where
505 	 * the log might have consumed all available space in the AG. If
506 	 * so, reset the record count to 0 to avoid exposure of an invalid
507 	 * record start block.
508 	 */
509 	arec->ar_blockcount = cpu_to_be32(id->agsize -
510 					  be32_to_cpu(arec->ar_startblock));
511 	if (!arec->ar_blockcount)
512 		block->bb_numrecs = 0;
513 }
514 
515 /*
516  * Alloc btree root block init functions
517  */
518 static void
519 xfs_bnoroot_init(
520 	struct xfs_mount	*mp,
521 	struct xfs_buf		*bp,
522 	struct aghdr_init_data	*id)
523 {
524 	xfs_btree_init_block(mp, bp, XFS_BTNUM_BNO, 0, 1, id->agno);
525 	xfs_freesp_init_recs(mp, bp, id);
526 }
527 
528 static void
529 xfs_cntroot_init(
530 	struct xfs_mount	*mp,
531 	struct xfs_buf		*bp,
532 	struct aghdr_init_data	*id)
533 {
534 	xfs_btree_init_block(mp, bp, XFS_BTNUM_CNT, 0, 1, id->agno);
535 	xfs_freesp_init_recs(mp, bp, id);
536 }
537 
538 /*
539  * Reverse map root block init
540  */
541 static void
542 xfs_rmaproot_init(
543 	struct xfs_mount	*mp,
544 	struct xfs_buf		*bp,
545 	struct aghdr_init_data	*id)
546 {
547 	struct xfs_btree_block	*block = XFS_BUF_TO_BLOCK(bp);
548 	struct xfs_rmap_rec	*rrec;
549 
550 	xfs_btree_init_block(mp, bp, XFS_BTNUM_RMAP, 0, 4, id->agno);
551 
552 	/*
553 	 * mark the AG header regions as static metadata The BNO
554 	 * btree block is the first block after the headers, so
555 	 * it's location defines the size of region the static
556 	 * metadata consumes.
557 	 *
558 	 * Note: unlike mkfs, we never have to account for log
559 	 * space when growing the data regions
560 	 */
561 	rrec = XFS_RMAP_REC_ADDR(block, 1);
562 	rrec->rm_startblock = 0;
563 	rrec->rm_blockcount = cpu_to_be32(XFS_BNO_BLOCK(mp));
564 	rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_FS);
565 	rrec->rm_offset = 0;
566 
567 	/* account freespace btree root blocks */
568 	rrec = XFS_RMAP_REC_ADDR(block, 2);
569 	rrec->rm_startblock = cpu_to_be32(XFS_BNO_BLOCK(mp));
570 	rrec->rm_blockcount = cpu_to_be32(2);
571 	rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_AG);
572 	rrec->rm_offset = 0;
573 
574 	/* account inode btree root blocks */
575 	rrec = XFS_RMAP_REC_ADDR(block, 3);
576 	rrec->rm_startblock = cpu_to_be32(XFS_IBT_BLOCK(mp));
577 	rrec->rm_blockcount = cpu_to_be32(XFS_RMAP_BLOCK(mp) -
578 					  XFS_IBT_BLOCK(mp));
579 	rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_INOBT);
580 	rrec->rm_offset = 0;
581 
582 	/* account for rmap btree root */
583 	rrec = XFS_RMAP_REC_ADDR(block, 4);
584 	rrec->rm_startblock = cpu_to_be32(XFS_RMAP_BLOCK(mp));
585 	rrec->rm_blockcount = cpu_to_be32(1);
586 	rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_AG);
587 	rrec->rm_offset = 0;
588 
589 	/* account for refc btree root */
590 	if (xfs_has_reflink(mp)) {
591 		rrec = XFS_RMAP_REC_ADDR(block, 5);
592 		rrec->rm_startblock = cpu_to_be32(xfs_refc_block(mp));
593 		rrec->rm_blockcount = cpu_to_be32(1);
594 		rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_REFC);
595 		rrec->rm_offset = 0;
596 		be16_add_cpu(&block->bb_numrecs, 1);
597 	}
598 
599 	/* account for the log space */
600 	if (xfs_ag_contains_log(mp, id->agno)) {
601 		rrec = XFS_RMAP_REC_ADDR(block,
602 				be16_to_cpu(block->bb_numrecs) + 1);
603 		rrec->rm_startblock = cpu_to_be32(
604 				XFS_FSB_TO_AGBNO(mp, mp->m_sb.sb_logstart));
605 		rrec->rm_blockcount = cpu_to_be32(mp->m_sb.sb_logblocks);
606 		rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_LOG);
607 		rrec->rm_offset = 0;
608 		be16_add_cpu(&block->bb_numrecs, 1);
609 	}
610 }
611 
612 /*
613  * Initialise new secondary superblocks with the pre-grow geometry, but mark
614  * them as "in progress" so we know they haven't yet been activated. This will
615  * get cleared when the update with the new geometry information is done after
616  * changes to the primary are committed. This isn't strictly necessary, but we
617  * get it for free with the delayed buffer write lists and it means we can tell
618  * if a grow operation didn't complete properly after the fact.
619  */
620 static void
621 xfs_sbblock_init(
622 	struct xfs_mount	*mp,
623 	struct xfs_buf		*bp,
624 	struct aghdr_init_data	*id)
625 {
626 	struct xfs_dsb		*dsb = bp->b_addr;
627 
628 	xfs_sb_to_disk(dsb, &mp->m_sb);
629 	dsb->sb_inprogress = 1;
630 }
631 
632 static void
633 xfs_agfblock_init(
634 	struct xfs_mount	*mp,
635 	struct xfs_buf		*bp,
636 	struct aghdr_init_data	*id)
637 {
638 	struct xfs_agf		*agf = bp->b_addr;
639 	xfs_extlen_t		tmpsize;
640 
641 	agf->agf_magicnum = cpu_to_be32(XFS_AGF_MAGIC);
642 	agf->agf_versionnum = cpu_to_be32(XFS_AGF_VERSION);
643 	agf->agf_seqno = cpu_to_be32(id->agno);
644 	agf->agf_length = cpu_to_be32(id->agsize);
645 	agf->agf_roots[XFS_BTNUM_BNOi] = cpu_to_be32(XFS_BNO_BLOCK(mp));
646 	agf->agf_roots[XFS_BTNUM_CNTi] = cpu_to_be32(XFS_CNT_BLOCK(mp));
647 	agf->agf_levels[XFS_BTNUM_BNOi] = cpu_to_be32(1);
648 	agf->agf_levels[XFS_BTNUM_CNTi] = cpu_to_be32(1);
649 	if (xfs_has_rmapbt(mp)) {
650 		agf->agf_roots[XFS_BTNUM_RMAPi] =
651 					cpu_to_be32(XFS_RMAP_BLOCK(mp));
652 		agf->agf_levels[XFS_BTNUM_RMAPi] = cpu_to_be32(1);
653 		agf->agf_rmap_blocks = cpu_to_be32(1);
654 	}
655 
656 	agf->agf_flfirst = cpu_to_be32(1);
657 	agf->agf_fllast = 0;
658 	agf->agf_flcount = 0;
659 	tmpsize = id->agsize - mp->m_ag_prealloc_blocks;
660 	agf->agf_freeblks = cpu_to_be32(tmpsize);
661 	agf->agf_longest = cpu_to_be32(tmpsize);
662 	if (xfs_has_crc(mp))
663 		uuid_copy(&agf->agf_uuid, &mp->m_sb.sb_meta_uuid);
664 	if (xfs_has_reflink(mp)) {
665 		agf->agf_refcount_root = cpu_to_be32(
666 				xfs_refc_block(mp));
667 		agf->agf_refcount_level = cpu_to_be32(1);
668 		agf->agf_refcount_blocks = cpu_to_be32(1);
669 	}
670 
671 	if (xfs_ag_contains_log(mp, id->agno)) {
672 		int64_t	logblocks = mp->m_sb.sb_logblocks;
673 
674 		be32_add_cpu(&agf->agf_freeblks, -logblocks);
675 		agf->agf_longest = cpu_to_be32(id->agsize -
676 			XFS_FSB_TO_AGBNO(mp, mp->m_sb.sb_logstart) - logblocks);
677 	}
678 }
679 
680 static void
681 xfs_agflblock_init(
682 	struct xfs_mount	*mp,
683 	struct xfs_buf		*bp,
684 	struct aghdr_init_data	*id)
685 {
686 	struct xfs_agfl		*agfl = XFS_BUF_TO_AGFL(bp);
687 	__be32			*agfl_bno;
688 	int			bucket;
689 
690 	if (xfs_has_crc(mp)) {
691 		agfl->agfl_magicnum = cpu_to_be32(XFS_AGFL_MAGIC);
692 		agfl->agfl_seqno = cpu_to_be32(id->agno);
693 		uuid_copy(&agfl->agfl_uuid, &mp->m_sb.sb_meta_uuid);
694 	}
695 
696 	agfl_bno = xfs_buf_to_agfl_bno(bp);
697 	for (bucket = 0; bucket < xfs_agfl_size(mp); bucket++)
698 		agfl_bno[bucket] = cpu_to_be32(NULLAGBLOCK);
699 }
700 
701 static void
702 xfs_agiblock_init(
703 	struct xfs_mount	*mp,
704 	struct xfs_buf		*bp,
705 	struct aghdr_init_data	*id)
706 {
707 	struct xfs_agi		*agi = bp->b_addr;
708 	int			bucket;
709 
710 	agi->agi_magicnum = cpu_to_be32(XFS_AGI_MAGIC);
711 	agi->agi_versionnum = cpu_to_be32(XFS_AGI_VERSION);
712 	agi->agi_seqno = cpu_to_be32(id->agno);
713 	agi->agi_length = cpu_to_be32(id->agsize);
714 	agi->agi_count = 0;
715 	agi->agi_root = cpu_to_be32(XFS_IBT_BLOCK(mp));
716 	agi->agi_level = cpu_to_be32(1);
717 	agi->agi_freecount = 0;
718 	agi->agi_newino = cpu_to_be32(NULLAGINO);
719 	agi->agi_dirino = cpu_to_be32(NULLAGINO);
720 	if (xfs_has_crc(mp))
721 		uuid_copy(&agi->agi_uuid, &mp->m_sb.sb_meta_uuid);
722 	if (xfs_has_finobt(mp)) {
723 		agi->agi_free_root = cpu_to_be32(XFS_FIBT_BLOCK(mp));
724 		agi->agi_free_level = cpu_to_be32(1);
725 	}
726 	for (bucket = 0; bucket < XFS_AGI_UNLINKED_BUCKETS; bucket++)
727 		agi->agi_unlinked[bucket] = cpu_to_be32(NULLAGINO);
728 	if (xfs_has_inobtcounts(mp)) {
729 		agi->agi_iblocks = cpu_to_be32(1);
730 		if (xfs_has_finobt(mp))
731 			agi->agi_fblocks = cpu_to_be32(1);
732 	}
733 }
734 
735 typedef void (*aghdr_init_work_f)(struct xfs_mount *mp, struct xfs_buf *bp,
736 				  struct aghdr_init_data *id);
737 static int
738 xfs_ag_init_hdr(
739 	struct xfs_mount	*mp,
740 	struct aghdr_init_data	*id,
741 	aghdr_init_work_f	work,
742 	const struct xfs_buf_ops *ops)
743 {
744 	struct xfs_buf		*bp;
745 	int			error;
746 
747 	error = xfs_get_aghdr_buf(mp, id->daddr, id->numblks, &bp, ops);
748 	if (error)
749 		return error;
750 
751 	(*work)(mp, bp, id);
752 
753 	xfs_buf_delwri_queue(bp, &id->buffer_list);
754 	xfs_buf_relse(bp);
755 	return 0;
756 }
757 
758 struct xfs_aghdr_grow_data {
759 	xfs_daddr_t		daddr;
760 	size_t			numblks;
761 	const struct xfs_buf_ops *ops;
762 	aghdr_init_work_f	work;
763 	xfs_btnum_t		type;
764 	bool			need_init;
765 };
766 
767 /*
768  * Prepare new AG headers to be written to disk. We use uncached buffers here,
769  * as it is assumed these new AG headers are currently beyond the currently
770  * valid filesystem address space. Using cached buffers would trip over EOFS
771  * corruption detection alogrithms in the buffer cache lookup routines.
772  *
773  * This is a non-transactional function, but the prepared buffers are added to a
774  * delayed write buffer list supplied by the caller so they can submit them to
775  * disk and wait on them as required.
776  */
777 int
778 xfs_ag_init_headers(
779 	struct xfs_mount	*mp,
780 	struct aghdr_init_data	*id)
781 
782 {
783 	struct xfs_aghdr_grow_data aghdr_data[] = {
784 	{ /* SB */
785 		.daddr = XFS_AG_DADDR(mp, id->agno, XFS_SB_DADDR),
786 		.numblks = XFS_FSS_TO_BB(mp, 1),
787 		.ops = &xfs_sb_buf_ops,
788 		.work = &xfs_sbblock_init,
789 		.need_init = true
790 	},
791 	{ /* AGF */
792 		.daddr = XFS_AG_DADDR(mp, id->agno, XFS_AGF_DADDR(mp)),
793 		.numblks = XFS_FSS_TO_BB(mp, 1),
794 		.ops = &xfs_agf_buf_ops,
795 		.work = &xfs_agfblock_init,
796 		.need_init = true
797 	},
798 	{ /* AGFL */
799 		.daddr = XFS_AG_DADDR(mp, id->agno, XFS_AGFL_DADDR(mp)),
800 		.numblks = XFS_FSS_TO_BB(mp, 1),
801 		.ops = &xfs_agfl_buf_ops,
802 		.work = &xfs_agflblock_init,
803 		.need_init = true
804 	},
805 	{ /* AGI */
806 		.daddr = XFS_AG_DADDR(mp, id->agno, XFS_AGI_DADDR(mp)),
807 		.numblks = XFS_FSS_TO_BB(mp, 1),
808 		.ops = &xfs_agi_buf_ops,
809 		.work = &xfs_agiblock_init,
810 		.need_init = true
811 	},
812 	{ /* BNO root block */
813 		.daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_BNO_BLOCK(mp)),
814 		.numblks = BTOBB(mp->m_sb.sb_blocksize),
815 		.ops = &xfs_bnobt_buf_ops,
816 		.work = &xfs_bnoroot_init,
817 		.need_init = true
818 	},
819 	{ /* CNT root block */
820 		.daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_CNT_BLOCK(mp)),
821 		.numblks = BTOBB(mp->m_sb.sb_blocksize),
822 		.ops = &xfs_cntbt_buf_ops,
823 		.work = &xfs_cntroot_init,
824 		.need_init = true
825 	},
826 	{ /* INO root block */
827 		.daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_IBT_BLOCK(mp)),
828 		.numblks = BTOBB(mp->m_sb.sb_blocksize),
829 		.ops = &xfs_inobt_buf_ops,
830 		.work = &xfs_btroot_init,
831 		.type = XFS_BTNUM_INO,
832 		.need_init = true
833 	},
834 	{ /* FINO root block */
835 		.daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_FIBT_BLOCK(mp)),
836 		.numblks = BTOBB(mp->m_sb.sb_blocksize),
837 		.ops = &xfs_finobt_buf_ops,
838 		.work = &xfs_btroot_init,
839 		.type = XFS_BTNUM_FINO,
840 		.need_init =  xfs_has_finobt(mp)
841 	},
842 	{ /* RMAP root block */
843 		.daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_RMAP_BLOCK(mp)),
844 		.numblks = BTOBB(mp->m_sb.sb_blocksize),
845 		.ops = &xfs_rmapbt_buf_ops,
846 		.work = &xfs_rmaproot_init,
847 		.need_init = xfs_has_rmapbt(mp)
848 	},
849 	{ /* REFC root block */
850 		.daddr = XFS_AGB_TO_DADDR(mp, id->agno, xfs_refc_block(mp)),
851 		.numblks = BTOBB(mp->m_sb.sb_blocksize),
852 		.ops = &xfs_refcountbt_buf_ops,
853 		.work = &xfs_btroot_init,
854 		.type = XFS_BTNUM_REFC,
855 		.need_init = xfs_has_reflink(mp)
856 	},
857 	{ /* NULL terminating block */
858 		.daddr = XFS_BUF_DADDR_NULL,
859 	}
860 	};
861 	struct  xfs_aghdr_grow_data *dp;
862 	int			error = 0;
863 
864 	/* Account for AG free space in new AG */
865 	id->nfree += id->agsize - mp->m_ag_prealloc_blocks;
866 	for (dp = &aghdr_data[0]; dp->daddr != XFS_BUF_DADDR_NULL; dp++) {
867 		if (!dp->need_init)
868 			continue;
869 
870 		id->daddr = dp->daddr;
871 		id->numblks = dp->numblks;
872 		id->type = dp->type;
873 		error = xfs_ag_init_hdr(mp, id, dp->work, dp->ops);
874 		if (error)
875 			break;
876 	}
877 	return error;
878 }
879 
880 int
881 xfs_ag_shrink_space(
882 	struct xfs_perag	*pag,
883 	struct xfs_trans	**tpp,
884 	xfs_extlen_t		delta)
885 {
886 	struct xfs_mount	*mp = pag->pag_mount;
887 	struct xfs_alloc_arg	args = {
888 		.tp	= *tpp,
889 		.mp	= mp,
890 		.pag	= pag,
891 		.minlen = delta,
892 		.maxlen = delta,
893 		.oinfo	= XFS_RMAP_OINFO_SKIP_UPDATE,
894 		.resv	= XFS_AG_RESV_NONE,
895 		.prod	= 1
896 	};
897 	struct xfs_buf		*agibp, *agfbp;
898 	struct xfs_agi		*agi;
899 	struct xfs_agf		*agf;
900 	xfs_agblock_t		aglen;
901 	int			error, err2;
902 
903 	ASSERT(pag->pag_agno == mp->m_sb.sb_agcount - 1);
904 	error = xfs_ialloc_read_agi(pag, *tpp, &agibp);
905 	if (error)
906 		return error;
907 
908 	agi = agibp->b_addr;
909 
910 	error = xfs_alloc_read_agf(pag, *tpp, 0, &agfbp);
911 	if (error)
912 		return error;
913 
914 	agf = agfbp->b_addr;
915 	aglen = be32_to_cpu(agi->agi_length);
916 	/* some extra paranoid checks before we shrink the ag */
917 	if (XFS_IS_CORRUPT(mp, agf->agf_length != agi->agi_length))
918 		return -EFSCORRUPTED;
919 	if (delta >= aglen)
920 		return -EINVAL;
921 
922 	/*
923 	 * Make sure that the last inode cluster cannot overlap with the new
924 	 * end of the AG, even if it's sparse.
925 	 */
926 	error = xfs_ialloc_check_shrink(pag, *tpp, agibp, aglen - delta);
927 	if (error)
928 		return error;
929 
930 	/*
931 	 * Disable perag reservations so it doesn't cause the allocation request
932 	 * to fail. We'll reestablish reservation before we return.
933 	 */
934 	error = xfs_ag_resv_free(pag);
935 	if (error)
936 		return error;
937 
938 	/* internal log shouldn't also show up in the free space btrees */
939 	error = xfs_alloc_vextent_exact_bno(&args,
940 			XFS_AGB_TO_FSB(mp, pag->pag_agno, aglen - delta));
941 	if (!error && args.agbno == NULLAGBLOCK)
942 		error = -ENOSPC;
943 
944 	if (error) {
945 		/*
946 		 * if extent allocation fails, need to roll the transaction to
947 		 * ensure that the AGFL fixup has been committed anyway.
948 		 */
949 		xfs_trans_bhold(*tpp, agfbp);
950 		err2 = xfs_trans_roll(tpp);
951 		if (err2)
952 			return err2;
953 		xfs_trans_bjoin(*tpp, agfbp);
954 		goto resv_init_out;
955 	}
956 
957 	/*
958 	 * if successfully deleted from freespace btrees, need to confirm
959 	 * per-AG reservation works as expected.
960 	 */
961 	be32_add_cpu(&agi->agi_length, -delta);
962 	be32_add_cpu(&agf->agf_length, -delta);
963 
964 	err2 = xfs_ag_resv_init(pag, *tpp);
965 	if (err2) {
966 		be32_add_cpu(&agi->agi_length, delta);
967 		be32_add_cpu(&agf->agf_length, delta);
968 		if (err2 != -ENOSPC)
969 			goto resv_err;
970 
971 		__xfs_free_extent_later(*tpp, args.fsbno, delta, NULL, true);
972 
973 		/*
974 		 * Roll the transaction before trying to re-init the per-ag
975 		 * reservation. The new transaction is clean so it will cancel
976 		 * without any side effects.
977 		 */
978 		error = xfs_defer_finish(tpp);
979 		if (error)
980 			return error;
981 
982 		error = -ENOSPC;
983 		goto resv_init_out;
984 	}
985 	xfs_ialloc_log_agi(*tpp, agibp, XFS_AGI_LENGTH);
986 	xfs_alloc_log_agf(*tpp, agfbp, XFS_AGF_LENGTH);
987 	return 0;
988 
989 resv_init_out:
990 	err2 = xfs_ag_resv_init(pag, *tpp);
991 	if (!err2)
992 		return error;
993 resv_err:
994 	xfs_warn(mp, "Error %d reserving per-AG metadata reserve pool.", err2);
995 	xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
996 	return err2;
997 }
998 
999 /*
1000  * Extent the AG indicated by the @id by the length passed in
1001  */
1002 int
1003 xfs_ag_extend_space(
1004 	struct xfs_perag	*pag,
1005 	struct xfs_trans	*tp,
1006 	xfs_extlen_t		len)
1007 {
1008 	struct xfs_buf		*bp;
1009 	struct xfs_agi		*agi;
1010 	struct xfs_agf		*agf;
1011 	int			error;
1012 
1013 	ASSERT(pag->pag_agno == pag->pag_mount->m_sb.sb_agcount - 1);
1014 
1015 	error = xfs_ialloc_read_agi(pag, tp, &bp);
1016 	if (error)
1017 		return error;
1018 
1019 	agi = bp->b_addr;
1020 	be32_add_cpu(&agi->agi_length, len);
1021 	xfs_ialloc_log_agi(tp, bp, XFS_AGI_LENGTH);
1022 
1023 	/*
1024 	 * Change agf length.
1025 	 */
1026 	error = xfs_alloc_read_agf(pag, tp, 0, &bp);
1027 	if (error)
1028 		return error;
1029 
1030 	agf = bp->b_addr;
1031 	be32_add_cpu(&agf->agf_length, len);
1032 	ASSERT(agf->agf_length == agi->agi_length);
1033 	xfs_alloc_log_agf(tp, bp, XFS_AGF_LENGTH);
1034 
1035 	/*
1036 	 * Free the new space.
1037 	 *
1038 	 * XFS_RMAP_OINFO_SKIP_UPDATE is used here to tell the rmap btree that
1039 	 * this doesn't actually exist in the rmap btree.
1040 	 */
1041 	error = xfs_rmap_free(tp, bp, pag, be32_to_cpu(agf->agf_length) - len,
1042 				len, &XFS_RMAP_OINFO_SKIP_UPDATE);
1043 	if (error)
1044 		return error;
1045 
1046 	error = xfs_free_extent(tp, XFS_AGB_TO_FSB(pag->pag_mount, pag->pag_agno,
1047 					be32_to_cpu(agf->agf_length) - len),
1048 				len, &XFS_RMAP_OINFO_SKIP_UPDATE,
1049 				XFS_AG_RESV_NONE);
1050 	if (error)
1051 		return error;
1052 
1053 	/* Update perag geometry */
1054 	pag->block_count = be32_to_cpu(agf->agf_length);
1055 	__xfs_agino_range(pag->pag_mount, pag->block_count, &pag->agino_min,
1056 				&pag->agino_max);
1057 	return 0;
1058 }
1059 
1060 /* Retrieve AG geometry. */
1061 int
1062 xfs_ag_get_geometry(
1063 	struct xfs_perag	*pag,
1064 	struct xfs_ag_geometry	*ageo)
1065 {
1066 	struct xfs_buf		*agi_bp;
1067 	struct xfs_buf		*agf_bp;
1068 	struct xfs_agi		*agi;
1069 	struct xfs_agf		*agf;
1070 	unsigned int		freeblks;
1071 	int			error;
1072 
1073 	/* Lock the AG headers. */
1074 	error = xfs_ialloc_read_agi(pag, NULL, &agi_bp);
1075 	if (error)
1076 		return error;
1077 	error = xfs_alloc_read_agf(pag, NULL, 0, &agf_bp);
1078 	if (error)
1079 		goto out_agi;
1080 
1081 	/* Fill out form. */
1082 	memset(ageo, 0, sizeof(*ageo));
1083 	ageo->ag_number = pag->pag_agno;
1084 
1085 	agi = agi_bp->b_addr;
1086 	ageo->ag_icount = be32_to_cpu(agi->agi_count);
1087 	ageo->ag_ifree = be32_to_cpu(agi->agi_freecount);
1088 
1089 	agf = agf_bp->b_addr;
1090 	ageo->ag_length = be32_to_cpu(agf->agf_length);
1091 	freeblks = pag->pagf_freeblks +
1092 		   pag->pagf_flcount +
1093 		   pag->pagf_btreeblks -
1094 		   xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE);
1095 	ageo->ag_freeblks = freeblks;
1096 	xfs_ag_geom_health(pag, ageo);
1097 
1098 	/* Release resources. */
1099 	xfs_buf_relse(agf_bp);
1100 out_agi:
1101 	xfs_buf_relse(agi_bp);
1102 	return error;
1103 }
1104