1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * Copyright (c) 2018 Red Hat, Inc.
10 #include "xfs_shared.h"
11 #include "xfs_format.h"
12 #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"
25 #include "xfs_error.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"
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.
46 struct xfs_perag *pag;
49 pag = radix_tree_lookup(&mp->m_perag_tree, agno);
51 trace_xfs_perag_get(pag, _RET_IP_);
52 ASSERT(atomic_read(&pag->pag_ref) >= 0);
53 atomic_inc(&pag->pag_ref);
60 * search from @first to find the next perag with the given tag set.
68 struct xfs_perag *pag;
72 found = radix_tree_gang_lookup_tag(&mp->m_perag_tree,
73 (void **)&pag, first, 1, tag);
78 trace_xfs_perag_get_tag(pag, _RET_IP_);
79 atomic_inc(&pag->pag_ref);
84 /* Get a passive reference to the given perag. */
87 struct xfs_perag *pag)
89 ASSERT(atomic_read(&pag->pag_ref) > 0 ||
90 atomic_read(&pag->pag_active_ref) > 0);
92 trace_xfs_perag_hold(pag, _RET_IP_);
93 atomic_inc(&pag->pag_ref);
99 struct xfs_perag *pag)
101 trace_xfs_perag_put(pag, _RET_IP_);
102 ASSERT(atomic_read(&pag->pag_ref) > 0);
103 atomic_dec(&pag->pag_ref);
107 * Active references for perag structures. This is for short term access to the
108 * per ag structures for walking trees or accessing state. If an AG is being
109 * shrunk or is offline, then this will fail to find that AG and return NULL
114 struct xfs_mount *mp,
117 struct xfs_perag *pag;
120 pag = radix_tree_lookup(&mp->m_perag_tree, agno);
122 trace_xfs_perag_grab(pag, _RET_IP_);
123 if (!atomic_inc_not_zero(&pag->pag_active_ref))
131 * search from @first to find the next perag with the given tag set.
135 struct xfs_mount *mp,
136 xfs_agnumber_t first,
139 struct xfs_perag *pag;
143 found = radix_tree_gang_lookup_tag(&mp->m_perag_tree,
144 (void **)&pag, first, 1, tag);
149 trace_xfs_perag_grab_tag(pag, _RET_IP_);
150 if (!atomic_inc_not_zero(&pag->pag_active_ref))
158 struct xfs_perag *pag)
160 trace_xfs_perag_rele(pag, _RET_IP_);
161 if (atomic_dec_and_test(&pag->pag_active_ref))
162 wake_up(&pag->pag_active_wq);
166 * xfs_initialize_perag_data
168 * Read in each per-ag structure so we can count up the number of
169 * allocated inodes, free inodes and used filesystem blocks as this
170 * information is no longer persistent in the superblock. Once we have
171 * this information, write it into the in-core superblock structure.
174 xfs_initialize_perag_data(
175 struct xfs_mount *mp,
176 xfs_agnumber_t agcount)
178 xfs_agnumber_t index;
179 struct xfs_perag *pag;
180 struct xfs_sb *sbp = &mp->m_sb;
184 uint64_t bfreelst = 0;
189 for (index = 0; index < agcount; index++) {
191 * Read the AGF and AGI buffers to populate the per-ag
194 pag = xfs_perag_get(mp, index);
195 error = xfs_alloc_read_agf(pag, NULL, 0, NULL);
197 error = xfs_ialloc_read_agi(pag, NULL, NULL);
203 ifree += pag->pagi_freecount;
204 ialloc += pag->pagi_count;
205 bfree += pag->pagf_freeblks;
206 bfreelst += pag->pagf_flcount;
207 btree += pag->pagf_btreeblks;
210 fdblocks = bfree + bfreelst + btree;
213 * If the new summary counts are obviously incorrect, fail the
214 * mount operation because that implies the AGFs are also corrupt.
215 * Clear FS_COUNTERS so that we don't unmount with a dirty log, which
216 * will prevent xfs_repair from fixing anything.
218 if (fdblocks > sbp->sb_dblocks || ifree > ialloc) {
219 xfs_alert(mp, "AGF corruption. Please run xfs_repair.");
220 xfs_fs_mark_sick(mp, XFS_SICK_FS_COUNTERS);
221 error = -EFSCORRUPTED;
225 /* Overwrite incore superblock counters with just-read data */
226 spin_lock(&mp->m_sb_lock);
227 sbp->sb_ifree = ifree;
228 sbp->sb_icount = ialloc;
229 sbp->sb_fdblocks = fdblocks;
230 spin_unlock(&mp->m_sb_lock);
232 xfs_reinit_percpu_counters(mp);
234 xfs_fs_mark_healthy(mp, XFS_SICK_FS_COUNTERS);
240 struct rcu_head *head)
242 struct xfs_perag *pag = container_of(head, struct xfs_perag, rcu_head);
244 ASSERT(!delayed_work_pending(&pag->pag_blockgc_work));
249 * Free up the per-ag resources associated with the mount structure.
253 struct xfs_mount *mp)
255 struct xfs_perag *pag;
258 for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
259 spin_lock(&mp->m_perag_lock);
260 pag = radix_tree_delete(&mp->m_perag_tree, agno);
261 spin_unlock(&mp->m_perag_lock);
263 XFS_IS_CORRUPT(pag->pag_mount, atomic_read(&pag->pag_ref) != 0);
264 xfs_defer_drain_free(&pag->pag_intents_drain);
266 cancel_delayed_work_sync(&pag->pag_blockgc_work);
267 xfs_buf_cache_destroy(&pag->pag_bcache);
269 /* drop the mount's active reference */
271 XFS_IS_CORRUPT(pag->pag_mount,
272 atomic_read(&pag->pag_active_ref) != 0);
273 call_rcu(&pag->rcu_head, __xfs_free_perag);
277 /* Find the size of the AG, in blocks. */
279 __xfs_ag_block_count(
280 struct xfs_mount *mp,
282 xfs_agnumber_t agcount,
283 xfs_rfsblock_t dblocks)
285 ASSERT(agno < agcount);
287 if (agno < agcount - 1)
288 return mp->m_sb.sb_agblocks;
289 return dblocks - (agno * mp->m_sb.sb_agblocks);
294 struct xfs_mount *mp,
297 return __xfs_ag_block_count(mp, agno, mp->m_sb.sb_agcount,
298 mp->m_sb.sb_dblocks);
301 /* Calculate the first and last possible inode number in an AG. */
304 struct xfs_mount *mp,
312 * Calculate the first inode, which will be in the first
313 * cluster-aligned block after the AGFL.
315 bno = round_up(XFS_AGFL_BLOCK(mp) + 1, M_IGEO(mp)->cluster_align);
316 *first = XFS_AGB_TO_AGINO(mp, bno);
319 * Calculate the last inode, which will be at the end of the
320 * last (aligned) cluster that can be allocated in the AG.
322 bno = round_down(eoag, M_IGEO(mp)->cluster_align);
323 *last = XFS_AGB_TO_AGINO(mp, bno) - 1;
328 struct xfs_mount *mp,
333 return __xfs_agino_range(mp, xfs_ag_block_count(mp, agno), first, last);
337 * Free perag within the specified AG range, it is only used to free unused
338 * perags under the error handling path.
341 xfs_free_unused_perag_range(
342 struct xfs_mount *mp,
343 xfs_agnumber_t agstart,
344 xfs_agnumber_t agend)
346 struct xfs_perag *pag;
347 xfs_agnumber_t index;
349 for (index = agstart; index < agend; index++) {
350 spin_lock(&mp->m_perag_lock);
351 pag = radix_tree_delete(&mp->m_perag_tree, index);
352 spin_unlock(&mp->m_perag_lock);
355 xfs_buf_cache_destroy(&pag->pag_bcache);
356 xfs_defer_drain_free(&pag->pag_intents_drain);
362 xfs_initialize_perag(
363 struct xfs_mount *mp,
364 xfs_agnumber_t agcount,
365 xfs_rfsblock_t dblocks,
366 xfs_agnumber_t *maxagi)
368 struct xfs_perag *pag;
369 xfs_agnumber_t index;
370 xfs_agnumber_t first_initialised = NULLAGNUMBER;
374 * Walk the current per-ag tree so we don't try to initialise AGs
375 * that already exist (growfs case). Allocate and insert all the
376 * AGs we don't find ready for initialisation.
378 for (index = 0; index < agcount; index++) {
379 pag = xfs_perag_get(mp, index);
385 pag = kzalloc(sizeof(*pag), GFP_KERNEL | __GFP_RETRY_MAYFAIL);
388 goto out_unwind_new_pags;
390 pag->pag_agno = index;
393 error = radix_tree_preload(GFP_KERNEL | __GFP_RETRY_MAYFAIL);
397 spin_lock(&mp->m_perag_lock);
398 if (radix_tree_insert(&mp->m_perag_tree, index, pag)) {
400 spin_unlock(&mp->m_perag_lock);
401 radix_tree_preload_end();
405 spin_unlock(&mp->m_perag_lock);
406 radix_tree_preload_end();
409 /* Place kernel structure only init below this point. */
410 spin_lock_init(&pag->pag_ici_lock);
411 spin_lock_init(&pag->pagb_lock);
412 spin_lock_init(&pag->pag_state_lock);
413 INIT_DELAYED_WORK(&pag->pag_blockgc_work, xfs_blockgc_worker);
414 INIT_RADIX_TREE(&pag->pag_ici_root, GFP_ATOMIC);
415 xfs_defer_drain_init(&pag->pag_intents_drain);
416 init_waitqueue_head(&pag->pagb_wait);
417 init_waitqueue_head(&pag->pag_active_wq);
419 pag->pagb_tree = RB_ROOT;
420 xfs_hooks_init(&pag->pag_rmap_update_hooks);
421 #endif /* __KERNEL__ */
423 error = xfs_buf_cache_init(&pag->pag_bcache);
427 /* Active ref owned by mount indicates AG is online. */
428 atomic_set(&pag->pag_active_ref, 1);
430 /* first new pag is fully initialized */
431 if (first_initialised == NULLAGNUMBER)
432 first_initialised = index;
435 * Pre-calculated geometry
437 pag->block_count = __xfs_ag_block_count(mp, index, agcount,
439 pag->min_block = XFS_AGFL_BLOCK(mp);
440 __xfs_agino_range(mp, pag->block_count, &pag->agino_min,
444 index = xfs_set_inode_alloc(mp, agcount);
449 mp->m_ag_prealloc_blocks = xfs_prealloc_blocks(mp);
453 xfs_defer_drain_free(&pag->pag_intents_drain);
454 spin_lock(&mp->m_perag_lock);
455 radix_tree_delete(&mp->m_perag_tree, index);
456 spin_unlock(&mp->m_perag_lock);
460 /* unwind any prior newly initialized pags */
461 xfs_free_unused_perag_range(mp, first_initialised, agcount);
467 struct xfs_mount *mp,
470 struct xfs_buf **bpp,
471 const struct xfs_buf_ops *ops)
476 error = xfs_buf_get_uncached(mp->m_ddev_targp, numblks, 0, &bp);
480 bp->b_maps[0].bm_bn = blkno;
488 * Generic btree root block init function
492 struct xfs_mount *mp,
494 struct aghdr_init_data *id)
496 xfs_btree_init_buf(mp, bp, id->bc_ops, 0, 0, id->agno);
499 /* Finish initializing a free space btree. */
501 xfs_freesp_init_recs(
502 struct xfs_mount *mp,
504 struct aghdr_init_data *id)
506 struct xfs_alloc_rec *arec;
507 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
509 arec = XFS_ALLOC_REC_ADDR(mp, XFS_BUF_TO_BLOCK(bp), 1);
510 arec->ar_startblock = cpu_to_be32(mp->m_ag_prealloc_blocks);
512 if (xfs_ag_contains_log(mp, id->agno)) {
513 struct xfs_alloc_rec *nrec;
514 xfs_agblock_t start = XFS_FSB_TO_AGBNO(mp,
515 mp->m_sb.sb_logstart);
517 ASSERT(start >= mp->m_ag_prealloc_blocks);
518 if (start != mp->m_ag_prealloc_blocks) {
520 * Modify first record to pad stripe align of log and
521 * bump the record count.
523 arec->ar_blockcount = cpu_to_be32(start -
524 mp->m_ag_prealloc_blocks);
525 be16_add_cpu(&block->bb_numrecs, 1);
529 * Insert second record at start of internal log
530 * which then gets trimmed.
532 nrec->ar_startblock = cpu_to_be32(
533 be32_to_cpu(arec->ar_startblock) +
534 be32_to_cpu(arec->ar_blockcount));
538 * Change record start to after the internal log
540 be32_add_cpu(&arec->ar_startblock, mp->m_sb.sb_logblocks);
544 * Calculate the block count of this record; if it is nonzero,
545 * increment the record count.
547 arec->ar_blockcount = cpu_to_be32(id->agsize -
548 be32_to_cpu(arec->ar_startblock));
549 if (arec->ar_blockcount)
550 be16_add_cpu(&block->bb_numrecs, 1);
554 * bnobt/cntbt btree root block init functions
558 struct xfs_mount *mp,
560 struct aghdr_init_data *id)
562 xfs_btree_init_buf(mp, bp, id->bc_ops, 0, 0, id->agno);
563 xfs_freesp_init_recs(mp, bp, id);
567 * Reverse map root block init
571 struct xfs_mount *mp,
573 struct aghdr_init_data *id)
575 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
576 struct xfs_rmap_rec *rrec;
578 xfs_btree_init_buf(mp, bp, id->bc_ops, 0, 4, id->agno);
581 * mark the AG header regions as static metadata The BNO
582 * btree block is the first block after the headers, so
583 * it's location defines the size of region the static
586 * Note: unlike mkfs, we never have to account for log
587 * space when growing the data regions
589 rrec = XFS_RMAP_REC_ADDR(block, 1);
590 rrec->rm_startblock = 0;
591 rrec->rm_blockcount = cpu_to_be32(XFS_BNO_BLOCK(mp));
592 rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_FS);
595 /* account freespace btree root blocks */
596 rrec = XFS_RMAP_REC_ADDR(block, 2);
597 rrec->rm_startblock = cpu_to_be32(XFS_BNO_BLOCK(mp));
598 rrec->rm_blockcount = cpu_to_be32(2);
599 rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_AG);
602 /* account inode btree root blocks */
603 rrec = XFS_RMAP_REC_ADDR(block, 3);
604 rrec->rm_startblock = cpu_to_be32(XFS_IBT_BLOCK(mp));
605 rrec->rm_blockcount = cpu_to_be32(XFS_RMAP_BLOCK(mp) -
607 rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_INOBT);
610 /* account for rmap btree root */
611 rrec = XFS_RMAP_REC_ADDR(block, 4);
612 rrec->rm_startblock = cpu_to_be32(XFS_RMAP_BLOCK(mp));
613 rrec->rm_blockcount = cpu_to_be32(1);
614 rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_AG);
617 /* account for refc btree root */
618 if (xfs_has_reflink(mp)) {
619 rrec = XFS_RMAP_REC_ADDR(block, 5);
620 rrec->rm_startblock = cpu_to_be32(xfs_refc_block(mp));
621 rrec->rm_blockcount = cpu_to_be32(1);
622 rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_REFC);
624 be16_add_cpu(&block->bb_numrecs, 1);
627 /* account for the log space */
628 if (xfs_ag_contains_log(mp, id->agno)) {
629 rrec = XFS_RMAP_REC_ADDR(block,
630 be16_to_cpu(block->bb_numrecs) + 1);
631 rrec->rm_startblock = cpu_to_be32(
632 XFS_FSB_TO_AGBNO(mp, mp->m_sb.sb_logstart));
633 rrec->rm_blockcount = cpu_to_be32(mp->m_sb.sb_logblocks);
634 rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_LOG);
636 be16_add_cpu(&block->bb_numrecs, 1);
641 * Initialise new secondary superblocks with the pre-grow geometry, but mark
642 * them as "in progress" so we know they haven't yet been activated. This will
643 * get cleared when the update with the new geometry information is done after
644 * changes to the primary are committed. This isn't strictly necessary, but we
645 * get it for free with the delayed buffer write lists and it means we can tell
646 * if a grow operation didn't complete properly after the fact.
650 struct xfs_mount *mp,
652 struct aghdr_init_data *id)
654 struct xfs_dsb *dsb = bp->b_addr;
656 xfs_sb_to_disk(dsb, &mp->m_sb);
657 dsb->sb_inprogress = 1;
662 struct xfs_mount *mp,
664 struct aghdr_init_data *id)
666 struct xfs_agf *agf = bp->b_addr;
667 xfs_extlen_t tmpsize;
669 agf->agf_magicnum = cpu_to_be32(XFS_AGF_MAGIC);
670 agf->agf_versionnum = cpu_to_be32(XFS_AGF_VERSION);
671 agf->agf_seqno = cpu_to_be32(id->agno);
672 agf->agf_length = cpu_to_be32(id->agsize);
673 agf->agf_bno_root = cpu_to_be32(XFS_BNO_BLOCK(mp));
674 agf->agf_cnt_root = cpu_to_be32(XFS_CNT_BLOCK(mp));
675 agf->agf_bno_level = cpu_to_be32(1);
676 agf->agf_cnt_level = cpu_to_be32(1);
677 if (xfs_has_rmapbt(mp)) {
678 agf->agf_rmap_root = cpu_to_be32(XFS_RMAP_BLOCK(mp));
679 agf->agf_rmap_level = cpu_to_be32(1);
680 agf->agf_rmap_blocks = cpu_to_be32(1);
683 agf->agf_flfirst = cpu_to_be32(1);
685 agf->agf_flcount = 0;
686 tmpsize = id->agsize - mp->m_ag_prealloc_blocks;
687 agf->agf_freeblks = cpu_to_be32(tmpsize);
688 agf->agf_longest = cpu_to_be32(tmpsize);
690 uuid_copy(&agf->agf_uuid, &mp->m_sb.sb_meta_uuid);
691 if (xfs_has_reflink(mp)) {
692 agf->agf_refcount_root = cpu_to_be32(
694 agf->agf_refcount_level = cpu_to_be32(1);
695 agf->agf_refcount_blocks = cpu_to_be32(1);
698 if (xfs_ag_contains_log(mp, id->agno)) {
699 int64_t logblocks = mp->m_sb.sb_logblocks;
701 be32_add_cpu(&agf->agf_freeblks, -logblocks);
702 agf->agf_longest = cpu_to_be32(id->agsize -
703 XFS_FSB_TO_AGBNO(mp, mp->m_sb.sb_logstart) - logblocks);
709 struct xfs_mount *mp,
711 struct aghdr_init_data *id)
713 struct xfs_agfl *agfl = XFS_BUF_TO_AGFL(bp);
717 if (xfs_has_crc(mp)) {
718 agfl->agfl_magicnum = cpu_to_be32(XFS_AGFL_MAGIC);
719 agfl->agfl_seqno = cpu_to_be32(id->agno);
720 uuid_copy(&agfl->agfl_uuid, &mp->m_sb.sb_meta_uuid);
723 agfl_bno = xfs_buf_to_agfl_bno(bp);
724 for (bucket = 0; bucket < xfs_agfl_size(mp); bucket++)
725 agfl_bno[bucket] = cpu_to_be32(NULLAGBLOCK);
730 struct xfs_mount *mp,
732 struct aghdr_init_data *id)
734 struct xfs_agi *agi = bp->b_addr;
737 agi->agi_magicnum = cpu_to_be32(XFS_AGI_MAGIC);
738 agi->agi_versionnum = cpu_to_be32(XFS_AGI_VERSION);
739 agi->agi_seqno = cpu_to_be32(id->agno);
740 agi->agi_length = cpu_to_be32(id->agsize);
742 agi->agi_root = cpu_to_be32(XFS_IBT_BLOCK(mp));
743 agi->agi_level = cpu_to_be32(1);
744 agi->agi_freecount = 0;
745 agi->agi_newino = cpu_to_be32(NULLAGINO);
746 agi->agi_dirino = cpu_to_be32(NULLAGINO);
748 uuid_copy(&agi->agi_uuid, &mp->m_sb.sb_meta_uuid);
749 if (xfs_has_finobt(mp)) {
750 agi->agi_free_root = cpu_to_be32(XFS_FIBT_BLOCK(mp));
751 agi->agi_free_level = cpu_to_be32(1);
753 for (bucket = 0; bucket < XFS_AGI_UNLINKED_BUCKETS; bucket++)
754 agi->agi_unlinked[bucket] = cpu_to_be32(NULLAGINO);
755 if (xfs_has_inobtcounts(mp)) {
756 agi->agi_iblocks = cpu_to_be32(1);
757 if (xfs_has_finobt(mp))
758 agi->agi_fblocks = cpu_to_be32(1);
762 typedef void (*aghdr_init_work_f)(struct xfs_mount *mp, struct xfs_buf *bp,
763 struct aghdr_init_data *id);
766 struct xfs_mount *mp,
767 struct aghdr_init_data *id,
768 aghdr_init_work_f work,
769 const struct xfs_buf_ops *ops)
774 error = xfs_get_aghdr_buf(mp, id->daddr, id->numblks, &bp, ops);
780 xfs_buf_delwri_queue(bp, &id->buffer_list);
785 struct xfs_aghdr_grow_data {
788 const struct xfs_buf_ops *ops;
789 aghdr_init_work_f work;
790 const struct xfs_btree_ops *bc_ops;
795 * Prepare new AG headers to be written to disk. We use uncached buffers here,
796 * as it is assumed these new AG headers are currently beyond the currently
797 * valid filesystem address space. Using cached buffers would trip over EOFS
798 * corruption detection alogrithms in the buffer cache lookup routines.
800 * This is a non-transactional function, but the prepared buffers are added to a
801 * delayed write buffer list supplied by the caller so they can submit them to
802 * disk and wait on them as required.
806 struct xfs_mount *mp,
807 struct aghdr_init_data *id)
810 struct xfs_aghdr_grow_data aghdr_data[] = {
812 .daddr = XFS_AG_DADDR(mp, id->agno, XFS_SB_DADDR),
813 .numblks = XFS_FSS_TO_BB(mp, 1),
814 .ops = &xfs_sb_buf_ops,
815 .work = &xfs_sbblock_init,
819 .daddr = XFS_AG_DADDR(mp, id->agno, XFS_AGF_DADDR(mp)),
820 .numblks = XFS_FSS_TO_BB(mp, 1),
821 .ops = &xfs_agf_buf_ops,
822 .work = &xfs_agfblock_init,
826 .daddr = XFS_AG_DADDR(mp, id->agno, XFS_AGFL_DADDR(mp)),
827 .numblks = XFS_FSS_TO_BB(mp, 1),
828 .ops = &xfs_agfl_buf_ops,
829 .work = &xfs_agflblock_init,
833 .daddr = XFS_AG_DADDR(mp, id->agno, XFS_AGI_DADDR(mp)),
834 .numblks = XFS_FSS_TO_BB(mp, 1),
835 .ops = &xfs_agi_buf_ops,
836 .work = &xfs_agiblock_init,
839 { /* BNO root block */
840 .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_BNO_BLOCK(mp)),
841 .numblks = BTOBB(mp->m_sb.sb_blocksize),
842 .ops = &xfs_bnobt_buf_ops,
843 .work = &xfs_bnoroot_init,
844 .bc_ops = &xfs_bnobt_ops,
847 { /* CNT root block */
848 .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_CNT_BLOCK(mp)),
849 .numblks = BTOBB(mp->m_sb.sb_blocksize),
850 .ops = &xfs_cntbt_buf_ops,
851 .work = &xfs_bnoroot_init,
852 .bc_ops = &xfs_cntbt_ops,
855 { /* INO root block */
856 .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_IBT_BLOCK(mp)),
857 .numblks = BTOBB(mp->m_sb.sb_blocksize),
858 .ops = &xfs_inobt_buf_ops,
859 .work = &xfs_btroot_init,
860 .bc_ops = &xfs_inobt_ops,
863 { /* FINO root block */
864 .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_FIBT_BLOCK(mp)),
865 .numblks = BTOBB(mp->m_sb.sb_blocksize),
866 .ops = &xfs_finobt_buf_ops,
867 .work = &xfs_btroot_init,
868 .bc_ops = &xfs_finobt_ops,
869 .need_init = xfs_has_finobt(mp)
871 { /* RMAP root block */
872 .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_RMAP_BLOCK(mp)),
873 .numblks = BTOBB(mp->m_sb.sb_blocksize),
874 .ops = &xfs_rmapbt_buf_ops,
875 .work = &xfs_rmaproot_init,
876 .bc_ops = &xfs_rmapbt_ops,
877 .need_init = xfs_has_rmapbt(mp)
879 { /* REFC root block */
880 .daddr = XFS_AGB_TO_DADDR(mp, id->agno, xfs_refc_block(mp)),
881 .numblks = BTOBB(mp->m_sb.sb_blocksize),
882 .ops = &xfs_refcountbt_buf_ops,
883 .work = &xfs_btroot_init,
884 .bc_ops = &xfs_refcountbt_ops,
885 .need_init = xfs_has_reflink(mp)
887 { /* NULL terminating block */
888 .daddr = XFS_BUF_DADDR_NULL,
891 struct xfs_aghdr_grow_data *dp;
894 /* Account for AG free space in new AG */
895 id->nfree += id->agsize - mp->m_ag_prealloc_blocks;
896 for (dp = &aghdr_data[0]; dp->daddr != XFS_BUF_DADDR_NULL; dp++) {
900 id->daddr = dp->daddr;
901 id->numblks = dp->numblks;
902 id->bc_ops = dp->bc_ops;
903 error = xfs_ag_init_hdr(mp, id, dp->work, dp->ops);
912 struct xfs_perag *pag,
913 struct xfs_trans **tpp,
916 struct xfs_mount *mp = pag->pag_mount;
917 struct xfs_alloc_arg args = {
923 .oinfo = XFS_RMAP_OINFO_SKIP_UPDATE,
924 .resv = XFS_AG_RESV_NONE,
927 struct xfs_buf *agibp, *agfbp;
933 ASSERT(pag->pag_agno == mp->m_sb.sb_agcount - 1);
934 error = xfs_ialloc_read_agi(pag, *tpp, &agibp);
940 error = xfs_alloc_read_agf(pag, *tpp, 0, &agfbp);
945 aglen = be32_to_cpu(agi->agi_length);
946 /* some extra paranoid checks before we shrink the ag */
947 if (XFS_IS_CORRUPT(mp, agf->agf_length != agi->agi_length)) {
948 xfs_ag_mark_sick(pag, XFS_SICK_AG_AGF);
949 return -EFSCORRUPTED;
955 * Make sure that the last inode cluster cannot overlap with the new
956 * end of the AG, even if it's sparse.
958 error = xfs_ialloc_check_shrink(pag, *tpp, agibp, aglen - delta);
963 * Disable perag reservations so it doesn't cause the allocation request
964 * to fail. We'll reestablish reservation before we return.
966 error = xfs_ag_resv_free(pag);
970 /* internal log shouldn't also show up in the free space btrees */
971 error = xfs_alloc_vextent_exact_bno(&args,
972 XFS_AGB_TO_FSB(mp, pag->pag_agno, aglen - delta));
973 if (!error && args.agbno == NULLAGBLOCK)
978 * If extent allocation fails, need to roll the transaction to
979 * ensure that the AGFL fixup has been committed anyway.
981 * We need to hold the AGF across the roll to ensure nothing can
982 * access the AG for allocation until the shrink is fully
983 * cleaned up. And due to the resetting of the AG block
984 * reservation space needing to lock the AGI, we also have to
985 * hold that so we don't get AGI/AGF lock order inversions in
986 * the error handling path.
988 xfs_trans_bhold(*tpp, agfbp);
989 xfs_trans_bhold(*tpp, agibp);
990 err2 = xfs_trans_roll(tpp);
993 xfs_trans_bjoin(*tpp, agfbp);
994 xfs_trans_bjoin(*tpp, agibp);
999 * if successfully deleted from freespace btrees, need to confirm
1000 * per-AG reservation works as expected.
1002 be32_add_cpu(&agi->agi_length, -delta);
1003 be32_add_cpu(&agf->agf_length, -delta);
1005 err2 = xfs_ag_resv_init(pag, *tpp);
1007 be32_add_cpu(&agi->agi_length, delta);
1008 be32_add_cpu(&agf->agf_length, delta);
1009 if (err2 != -ENOSPC)
1012 err2 = xfs_free_extent_later(*tpp, args.fsbno, delta, NULL,
1013 XFS_AG_RESV_NONE, true);
1018 * Roll the transaction before trying to re-init the per-ag
1019 * reservation. The new transaction is clean so it will cancel
1020 * without any side effects.
1022 error = xfs_defer_finish(tpp);
1030 /* Update perag geometry */
1031 pag->block_count -= delta;
1032 __xfs_agino_range(pag->pag_mount, pag->block_count, &pag->agino_min,
1035 xfs_ialloc_log_agi(*tpp, agibp, XFS_AGI_LENGTH);
1036 xfs_alloc_log_agf(*tpp, agfbp, XFS_AGF_LENGTH);
1040 err2 = xfs_ag_resv_init(pag, *tpp);
1044 xfs_warn(mp, "Error %d reserving per-AG metadata reserve pool.", err2);
1045 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
1050 * Extent the AG indicated by the @id by the length passed in
1053 xfs_ag_extend_space(
1054 struct xfs_perag *pag,
1055 struct xfs_trans *tp,
1059 struct xfs_agi *agi;
1060 struct xfs_agf *agf;
1063 ASSERT(pag->pag_agno == pag->pag_mount->m_sb.sb_agcount - 1);
1065 error = xfs_ialloc_read_agi(pag, tp, &bp);
1070 be32_add_cpu(&agi->agi_length, len);
1071 xfs_ialloc_log_agi(tp, bp, XFS_AGI_LENGTH);
1074 * Change agf length.
1076 error = xfs_alloc_read_agf(pag, tp, 0, &bp);
1081 be32_add_cpu(&agf->agf_length, len);
1082 ASSERT(agf->agf_length == agi->agi_length);
1083 xfs_alloc_log_agf(tp, bp, XFS_AGF_LENGTH);
1086 * Free the new space.
1088 * XFS_RMAP_OINFO_SKIP_UPDATE is used here to tell the rmap btree that
1089 * this doesn't actually exist in the rmap btree.
1091 error = xfs_rmap_free(tp, bp, pag, be32_to_cpu(agf->agf_length) - len,
1092 len, &XFS_RMAP_OINFO_SKIP_UPDATE);
1096 error = xfs_free_extent(tp, pag, be32_to_cpu(agf->agf_length) - len,
1097 len, &XFS_RMAP_OINFO_SKIP_UPDATE, XFS_AG_RESV_NONE);
1101 /* Update perag geometry */
1102 pag->block_count = be32_to_cpu(agf->agf_length);
1103 __xfs_agino_range(pag->pag_mount, pag->block_count, &pag->agino_min,
1108 /* Retrieve AG geometry. */
1110 xfs_ag_get_geometry(
1111 struct xfs_perag *pag,
1112 struct xfs_ag_geometry *ageo)
1114 struct xfs_buf *agi_bp;
1115 struct xfs_buf *agf_bp;
1116 struct xfs_agi *agi;
1117 struct xfs_agf *agf;
1118 unsigned int freeblks;
1121 /* Lock the AG headers. */
1122 error = xfs_ialloc_read_agi(pag, NULL, &agi_bp);
1125 error = xfs_alloc_read_agf(pag, NULL, 0, &agf_bp);
1129 /* Fill out form. */
1130 memset(ageo, 0, sizeof(*ageo));
1131 ageo->ag_number = pag->pag_agno;
1133 agi = agi_bp->b_addr;
1134 ageo->ag_icount = be32_to_cpu(agi->agi_count);
1135 ageo->ag_ifree = be32_to_cpu(agi->agi_freecount);
1137 agf = agf_bp->b_addr;
1138 ageo->ag_length = be32_to_cpu(agf->agf_length);
1139 freeblks = pag->pagf_freeblks +
1141 pag->pagf_btreeblks -
1142 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE);
1143 ageo->ag_freeblks = freeblks;
1144 xfs_ag_geom_health(pag, ageo);
1146 /* Release resources. */
1147 xfs_buf_relse(agf_bp);
1149 xfs_buf_relse(agi_bp);