10b61f8a4SDave Chinner // SPDX-License-Identifier: GPL-2.0
230f712c9SDave Chinner /*
330f712c9SDave Chinner * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
430f712c9SDave Chinner * All Rights Reserved.
530f712c9SDave Chinner */
630f712c9SDave Chinner #include "xfs.h"
730f712c9SDave Chinner #include "xfs_fs.h"
830f712c9SDave Chinner #include "xfs_shared.h"
930f712c9SDave Chinner #include "xfs_format.h"
1030f712c9SDave Chinner #include "xfs_log_format.h"
1130f712c9SDave Chinner #include "xfs_trans_resv.h"
1230f712c9SDave Chinner #include "xfs_bit.h"
1330f712c9SDave Chinner #include "xfs_mount.h"
1430f712c9SDave Chinner #include "xfs_inode.h"
1530f712c9SDave Chinner #include "xfs_btree.h"
1630f712c9SDave Chinner #include "xfs_ialloc.h"
1730f712c9SDave Chinner #include "xfs_ialloc_btree.h"
1830f712c9SDave Chinner #include "xfs_alloc.h"
19e9e899a2SDarrick J. Wong #include "xfs_errortag.h"
2030f712c9SDave Chinner #include "xfs_error.h"
2130f712c9SDave Chinner #include "xfs_bmap.h"
2230f712c9SDave Chinner #include "xfs_trans.h"
2330f712c9SDave Chinner #include "xfs_buf_item.h"
2430f712c9SDave Chinner #include "xfs_icreate_item.h"
2530f712c9SDave Chinner #include "xfs_icache.h"
2630f712c9SDave Chinner #include "xfs_trace.h"
27a45086e2SBrian Foster #include "xfs_log.h"
28340785ccSDarrick J. Wong #include "xfs_rmap.h"
299bbafc71SDave Chinner #include "xfs_ag.h"
3030f712c9SDave Chinner
3130f712c9SDave Chinner /*
3230f712c9SDave Chinner * Lookup a record by ino in the btree given by cur.
3330f712c9SDave Chinner */
3430f712c9SDave Chinner int /* error */
xfs_inobt_lookup(struct xfs_btree_cur * cur,xfs_agino_t ino,xfs_lookup_t dir,int * stat)3530f712c9SDave Chinner xfs_inobt_lookup(
3630f712c9SDave Chinner struct xfs_btree_cur *cur, /* btree cursor */
3730f712c9SDave Chinner xfs_agino_t ino, /* starting inode of chunk */
3830f712c9SDave Chinner xfs_lookup_t dir, /* <=, >=, == */
3930f712c9SDave Chinner int *stat) /* success/failure */
4030f712c9SDave Chinner {
4130f712c9SDave Chinner cur->bc_rec.i.ir_startino = ino;
425419040fSBrian Foster cur->bc_rec.i.ir_holemask = 0;
435419040fSBrian Foster cur->bc_rec.i.ir_count = 0;
4430f712c9SDave Chinner cur->bc_rec.i.ir_freecount = 0;
4530f712c9SDave Chinner cur->bc_rec.i.ir_free = 0;
4630f712c9SDave Chinner return xfs_btree_lookup(cur, dir, stat);
4730f712c9SDave Chinner }
4830f712c9SDave Chinner
4930f712c9SDave Chinner /*
5030f712c9SDave Chinner * Update the record referred to by cur to the value given.
5130f712c9SDave Chinner * This either works (return 0) or gets an EFSCORRUPTED error.
5230f712c9SDave Chinner */
5330f712c9SDave Chinner STATIC int /* error */
xfs_inobt_update(struct xfs_btree_cur * cur,xfs_inobt_rec_incore_t * irec)5430f712c9SDave Chinner xfs_inobt_update(
5530f712c9SDave Chinner struct xfs_btree_cur *cur, /* btree cursor */
5630f712c9SDave Chinner xfs_inobt_rec_incore_t *irec) /* btree record */
5730f712c9SDave Chinner {
5830f712c9SDave Chinner union xfs_btree_rec rec;
5930f712c9SDave Chinner
6030f712c9SDave Chinner rec.inobt.ir_startino = cpu_to_be32(irec->ir_startino);
6138c26bfdSDave Chinner if (xfs_has_sparseinodes(cur->bc_mp)) {
625419040fSBrian Foster rec.inobt.ir_u.sp.ir_holemask = cpu_to_be16(irec->ir_holemask);
635419040fSBrian Foster rec.inobt.ir_u.sp.ir_count = irec->ir_count;
645419040fSBrian Foster rec.inobt.ir_u.sp.ir_freecount = irec->ir_freecount;
655419040fSBrian Foster } else {
665419040fSBrian Foster /* ir_holemask/ir_count not supported on-disk */
675419040fSBrian Foster rec.inobt.ir_u.f.ir_freecount = cpu_to_be32(irec->ir_freecount);
685419040fSBrian Foster }
6930f712c9SDave Chinner rec.inobt.ir_free = cpu_to_be64(irec->ir_free);
7030f712c9SDave Chinner return xfs_btree_update(cur, &rec);
7130f712c9SDave Chinner }
7230f712c9SDave Chinner
73e936945eSDarrick J. Wong /* Convert on-disk btree record to incore inobt record. */
74e936945eSDarrick J. Wong void
xfs_inobt_btrec_to_irec(struct xfs_mount * mp,const union xfs_btree_rec * rec,struct xfs_inobt_rec_incore * irec)75e936945eSDarrick J. Wong xfs_inobt_btrec_to_irec(
76e936945eSDarrick J. Wong struct xfs_mount *mp,
77159eb69dSDarrick J. Wong const union xfs_btree_rec *rec,
78e936945eSDarrick J. Wong struct xfs_inobt_rec_incore *irec)
7930f712c9SDave Chinner {
805419040fSBrian Foster irec->ir_startino = be32_to_cpu(rec->inobt.ir_startino);
8138c26bfdSDave Chinner if (xfs_has_sparseinodes(mp)) {
825419040fSBrian Foster irec->ir_holemask = be16_to_cpu(rec->inobt.ir_u.sp.ir_holemask);
835419040fSBrian Foster irec->ir_count = rec->inobt.ir_u.sp.ir_count;
845419040fSBrian Foster irec->ir_freecount = rec->inobt.ir_u.sp.ir_freecount;
855419040fSBrian Foster } else {
865419040fSBrian Foster /*
875419040fSBrian Foster * ir_holemask/ir_count not supported on-disk. Fill in hardcoded
885419040fSBrian Foster * values for full inode chunks.
895419040fSBrian Foster */
905419040fSBrian Foster irec->ir_holemask = XFS_INOBT_HOLEMASK_FULL;
915419040fSBrian Foster irec->ir_count = XFS_INODES_PER_CHUNK;
925419040fSBrian Foster irec->ir_freecount =
935419040fSBrian Foster be32_to_cpu(rec->inobt.ir_u.f.ir_freecount);
945419040fSBrian Foster }
955419040fSBrian Foster irec->ir_free = be64_to_cpu(rec->inobt.ir_free);
96e936945eSDarrick J. Wong }
97e936945eSDarrick J. Wong
98366a0b8dSDarrick J. Wong /* Simple checks for inode records. */
99366a0b8dSDarrick J. Wong xfs_failaddr_t
xfs_inobt_check_irec(struct xfs_btree_cur * cur,const struct xfs_inobt_rec_incore * irec)100366a0b8dSDarrick J. Wong xfs_inobt_check_irec(
101366a0b8dSDarrick J. Wong struct xfs_btree_cur *cur,
102366a0b8dSDarrick J. Wong const struct xfs_inobt_rec_incore *irec)
103366a0b8dSDarrick J. Wong {
104366a0b8dSDarrick J. Wong uint64_t realfree;
105366a0b8dSDarrick J. Wong
106de1a9ce2SDarrick J. Wong /* Record has to be properly aligned within the AG. */
107366a0b8dSDarrick J. Wong if (!xfs_verify_agino(cur->bc_ag.pag, irec->ir_startino))
108366a0b8dSDarrick J. Wong return __this_address;
109de1a9ce2SDarrick J. Wong if (!xfs_verify_agino(cur->bc_ag.pag,
110de1a9ce2SDarrick J. Wong irec->ir_startino + XFS_INODES_PER_CHUNK - 1))
111de1a9ce2SDarrick J. Wong return __this_address;
112366a0b8dSDarrick J. Wong if (irec->ir_count < XFS_INODES_PER_HOLEMASK_BIT ||
113366a0b8dSDarrick J. Wong irec->ir_count > XFS_INODES_PER_CHUNK)
114366a0b8dSDarrick J. Wong return __this_address;
115366a0b8dSDarrick J. Wong if (irec->ir_freecount > XFS_INODES_PER_CHUNK)
116366a0b8dSDarrick J. Wong return __this_address;
117366a0b8dSDarrick J. Wong
118366a0b8dSDarrick J. Wong /* if there are no holes, return the first available offset */
119366a0b8dSDarrick J. Wong if (!xfs_inobt_issparse(irec->ir_holemask))
120366a0b8dSDarrick J. Wong realfree = irec->ir_free;
121366a0b8dSDarrick J. Wong else
122366a0b8dSDarrick J. Wong realfree = irec->ir_free & xfs_inobt_irec_to_allocmask(irec);
123366a0b8dSDarrick J. Wong if (hweight64(realfree) != irec->ir_freecount)
124366a0b8dSDarrick J. Wong return __this_address;
125366a0b8dSDarrick J. Wong
126366a0b8dSDarrick J. Wong return NULL;
127366a0b8dSDarrick J. Wong }
128366a0b8dSDarrick J. Wong
129ee12eaaaSDarrick J. Wong static inline int
xfs_inobt_complain_bad_rec(struct xfs_btree_cur * cur,xfs_failaddr_t fa,const struct xfs_inobt_rec_incore * irec)130ee12eaaaSDarrick J. Wong xfs_inobt_complain_bad_rec(
131ee12eaaaSDarrick J. Wong struct xfs_btree_cur *cur,
132ee12eaaaSDarrick J. Wong xfs_failaddr_t fa,
133ee12eaaaSDarrick J. Wong const struct xfs_inobt_rec_incore *irec)
134ee12eaaaSDarrick J. Wong {
135ee12eaaaSDarrick J. Wong struct xfs_mount *mp = cur->bc_mp;
136ee12eaaaSDarrick J. Wong
137ee12eaaaSDarrick J. Wong xfs_warn(mp,
138ee12eaaaSDarrick J. Wong "%s Inode BTree record corruption in AG %d detected at %pS!",
139ee12eaaaSDarrick J. Wong cur->bc_btnum == XFS_BTNUM_INO ? "Used" : "Free",
140ee12eaaaSDarrick J. Wong cur->bc_ag.pag->pag_agno, fa);
141ee12eaaaSDarrick J. Wong xfs_warn(mp,
142ee12eaaaSDarrick J. Wong "start inode 0x%x, count 0x%x, free 0x%x freemask 0x%llx, holemask 0x%x",
143ee12eaaaSDarrick J. Wong irec->ir_startino, irec->ir_count, irec->ir_freecount,
144ee12eaaaSDarrick J. Wong irec->ir_free, irec->ir_holemask);
145ee12eaaaSDarrick J. Wong return -EFSCORRUPTED;
146ee12eaaaSDarrick J. Wong }
147ee12eaaaSDarrick J. Wong
148e936945eSDarrick J. Wong /*
149e936945eSDarrick J. Wong * Get the data from the pointed-to record.
150e936945eSDarrick J. Wong */
151e936945eSDarrick J. Wong int
xfs_inobt_get_rec(struct xfs_btree_cur * cur,struct xfs_inobt_rec_incore * irec,int * stat)152e936945eSDarrick J. Wong xfs_inobt_get_rec(
153e936945eSDarrick J. Wong struct xfs_btree_cur *cur,
154e936945eSDarrick J. Wong struct xfs_inobt_rec_incore *irec,
155e936945eSDarrick J. Wong int *stat)
156e936945eSDarrick J. Wong {
1579e6c08d4SDave Chinner struct xfs_mount *mp = cur->bc_mp;
158e936945eSDarrick J. Wong union xfs_btree_rec *rec;
159366a0b8dSDarrick J. Wong xfs_failaddr_t fa;
160e936945eSDarrick J. Wong int error;
161e936945eSDarrick J. Wong
162e936945eSDarrick J. Wong error = xfs_btree_get_rec(cur, &rec, stat);
163e936945eSDarrick J. Wong if (error || *stat == 0)
164e936945eSDarrick J. Wong return error;
165e936945eSDarrick J. Wong
1669e6c08d4SDave Chinner xfs_inobt_btrec_to_irec(mp, rec, irec);
167366a0b8dSDarrick J. Wong fa = xfs_inobt_check_irec(cur, irec);
168366a0b8dSDarrick J. Wong if (fa)
169ee12eaaaSDarrick J. Wong return xfs_inobt_complain_bad_rec(cur, fa, irec);
1705419040fSBrian Foster
1715419040fSBrian Foster return 0;
17230f712c9SDave Chinner }
17330f712c9SDave Chinner
17430f712c9SDave Chinner /*
17530f712c9SDave Chinner * Insert a single inobt record. Cursor must already point to desired location.
17630f712c9SDave Chinner */
1777f8f1313SDarrick J. Wong int
xfs_inobt_insert_rec(struct xfs_btree_cur * cur,uint16_t holemask,uint8_t count,int32_t freecount,xfs_inofree_t free,int * stat)17830f712c9SDave Chinner xfs_inobt_insert_rec(
17930f712c9SDave Chinner struct xfs_btree_cur *cur,
180c8ce540dSDarrick J. Wong uint16_t holemask,
181c8ce540dSDarrick J. Wong uint8_t count,
182c8ce540dSDarrick J. Wong int32_t freecount,
18330f712c9SDave Chinner xfs_inofree_t free,
18430f712c9SDave Chinner int *stat)
18530f712c9SDave Chinner {
1865419040fSBrian Foster cur->bc_rec.i.ir_holemask = holemask;
1875419040fSBrian Foster cur->bc_rec.i.ir_count = count;
18830f712c9SDave Chinner cur->bc_rec.i.ir_freecount = freecount;
18930f712c9SDave Chinner cur->bc_rec.i.ir_free = free;
19030f712c9SDave Chinner return xfs_btree_insert(cur, stat);
19130f712c9SDave Chinner }
19230f712c9SDave Chinner
19330f712c9SDave Chinner /*
19430f712c9SDave Chinner * Insert records describing a newly allocated inode chunk into the inobt.
19530f712c9SDave Chinner */
19630f712c9SDave Chinner STATIC int
xfs_inobt_insert(struct xfs_perag * pag,struct xfs_trans * tp,struct xfs_buf * agbp,xfs_agino_t newino,xfs_agino_t newlen,xfs_btnum_t btnum)19730f712c9SDave Chinner xfs_inobt_insert(
198dedab3e4SDave Chinner struct xfs_perag *pag,
19930f712c9SDave Chinner struct xfs_trans *tp,
20030f712c9SDave Chinner struct xfs_buf *agbp,
20130f712c9SDave Chinner xfs_agino_t newino,
20230f712c9SDave Chinner xfs_agino_t newlen,
20330f712c9SDave Chinner xfs_btnum_t btnum)
20430f712c9SDave Chinner {
20530f712c9SDave Chinner struct xfs_btree_cur *cur;
20630f712c9SDave Chinner xfs_agino_t thisino;
20730f712c9SDave Chinner int i;
20830f712c9SDave Chinner int error;
20930f712c9SDave Chinner
210bab8b795SDave Chinner cur = xfs_inobt_init_cursor(pag, tp, agbp, btnum);
21130f712c9SDave Chinner
21230f712c9SDave Chinner for (thisino = newino;
21330f712c9SDave Chinner thisino < newino + newlen;
21430f712c9SDave Chinner thisino += XFS_INODES_PER_CHUNK) {
21530f712c9SDave Chinner error = xfs_inobt_lookup(cur, thisino, XFS_LOOKUP_EQ, &i);
21630f712c9SDave Chinner if (error) {
21730f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
21830f712c9SDave Chinner return error;
21930f712c9SDave Chinner }
22030f712c9SDave Chinner ASSERT(i == 0);
22130f712c9SDave Chinner
2225419040fSBrian Foster error = xfs_inobt_insert_rec(cur, XFS_INOBT_HOLEMASK_FULL,
2235419040fSBrian Foster XFS_INODES_PER_CHUNK,
2245419040fSBrian Foster XFS_INODES_PER_CHUNK,
22530f712c9SDave Chinner XFS_INOBT_ALL_FREE, &i);
22630f712c9SDave Chinner if (error) {
22730f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
22830f712c9SDave Chinner return error;
22930f712c9SDave Chinner }
23030f712c9SDave Chinner ASSERT(i == 1);
23130f712c9SDave Chinner }
23230f712c9SDave Chinner
23330f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
23430f712c9SDave Chinner
23530f712c9SDave Chinner return 0;
23630f712c9SDave Chinner }
23730f712c9SDave Chinner
23830f712c9SDave Chinner /*
23930f712c9SDave Chinner * Verify that the number of free inodes in the AGI is correct.
24030f712c9SDave Chinner */
24130f712c9SDave Chinner #ifdef DEBUG
2429ba0889eSDave Chinner static int
xfs_check_agi_freecount(struct xfs_btree_cur * cur)24330f712c9SDave Chinner xfs_check_agi_freecount(
2449ba0889eSDave Chinner struct xfs_btree_cur *cur)
24530f712c9SDave Chinner {
24630f712c9SDave Chinner if (cur->bc_nlevels == 1) {
24730f712c9SDave Chinner xfs_inobt_rec_incore_t rec;
24830f712c9SDave Chinner int freecount = 0;
24930f712c9SDave Chinner int error;
25030f712c9SDave Chinner int i;
25130f712c9SDave Chinner
25230f712c9SDave Chinner error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
25330f712c9SDave Chinner if (error)
25430f712c9SDave Chinner return error;
25530f712c9SDave Chinner
25630f712c9SDave Chinner do {
25730f712c9SDave Chinner error = xfs_inobt_get_rec(cur, &rec, &i);
25830f712c9SDave Chinner if (error)
25930f712c9SDave Chinner return error;
26030f712c9SDave Chinner
26130f712c9SDave Chinner if (i) {
26230f712c9SDave Chinner freecount += rec.ir_freecount;
26330f712c9SDave Chinner error = xfs_btree_increment(cur, 0, &i);
26430f712c9SDave Chinner if (error)
26530f712c9SDave Chinner return error;
26630f712c9SDave Chinner }
26730f712c9SDave Chinner } while (i == 1);
26830f712c9SDave Chinner
26975c8c50fSDave Chinner if (!xfs_is_shutdown(cur->bc_mp))
2709ba0889eSDave Chinner ASSERT(freecount == cur->bc_ag.pag->pagi_freecount);
27130f712c9SDave Chinner }
27230f712c9SDave Chinner return 0;
27330f712c9SDave Chinner }
27430f712c9SDave Chinner #else
2759ba0889eSDave Chinner #define xfs_check_agi_freecount(cur) 0
27630f712c9SDave Chinner #endif
27730f712c9SDave Chinner
27830f712c9SDave Chinner /*
27930f712c9SDave Chinner * Initialise a new set of inodes. When called without a transaction context
28030f712c9SDave Chinner * (e.g. from recovery) we initiate a delayed write of the inode buffers rather
28130f712c9SDave Chinner * than logging them (which in a transaction context puts them into the AIL
28230f712c9SDave Chinner * for writeback rather than the xfsbufd queue).
28330f712c9SDave Chinner */
28430f712c9SDave Chinner int
xfs_ialloc_inode_init(struct xfs_mount * mp,struct xfs_trans * tp,struct list_head * buffer_list,int icount,xfs_agnumber_t agno,xfs_agblock_t agbno,xfs_agblock_t length,unsigned int gen)28530f712c9SDave Chinner xfs_ialloc_inode_init(
28630f712c9SDave Chinner struct xfs_mount *mp,
28730f712c9SDave Chinner struct xfs_trans *tp,
28830f712c9SDave Chinner struct list_head *buffer_list,
289463958afSBrian Foster int icount,
29030f712c9SDave Chinner xfs_agnumber_t agno,
29130f712c9SDave Chinner xfs_agblock_t agbno,
29230f712c9SDave Chinner xfs_agblock_t length,
29330f712c9SDave Chinner unsigned int gen)
29430f712c9SDave Chinner {
29530f712c9SDave Chinner struct xfs_buf *fbuf;
29630f712c9SDave Chinner struct xfs_dinode *free;
29783dcdb44SDarrick J. Wong int nbufs;
29830f712c9SDave Chinner int version;
29930f712c9SDave Chinner int i, j;
30030f712c9SDave Chinner xfs_daddr_t d;
30130f712c9SDave Chinner xfs_ino_t ino = 0;
302ce92464cSDarrick J. Wong int error;
30330f712c9SDave Chinner
30430f712c9SDave Chinner /*
30530f712c9SDave Chinner * Loop over the new block(s), filling in the inodes. For small block
30630f712c9SDave Chinner * sizes, manipulate the inodes in buffers which are multiples of the
30730f712c9SDave Chinner * blocks size.
30830f712c9SDave Chinner */
309ef325959SDarrick J. Wong nbufs = length / M_IGEO(mp)->blocks_per_cluster;
31030f712c9SDave Chinner
31130f712c9SDave Chinner /*
31230f712c9SDave Chinner * Figure out what version number to use in the inodes we create. If
31330f712c9SDave Chinner * the superblock version has caught up to the one that supports the new
31430f712c9SDave Chinner * inode format, then use the new inode version. Otherwise use the old
31530f712c9SDave Chinner * version so that old kernels will continue to be able to use the file
31630f712c9SDave Chinner * system.
31730f712c9SDave Chinner *
31830f712c9SDave Chinner * For v3 inodes, we also need to write the inode number into the inode,
31930f712c9SDave Chinner * so calculate the first inode number of the chunk here as
32043004b2aSDarrick J. Wong * XFS_AGB_TO_AGINO() only works within a filesystem block, not
32130f712c9SDave Chinner * across multiple filesystem blocks (such as a cluster) and so cannot
32230f712c9SDave Chinner * be used in the cluster buffer loop below.
32330f712c9SDave Chinner *
32430f712c9SDave Chinner * Further, because we are writing the inode directly into the buffer
32530f712c9SDave Chinner * and calculating a CRC on the entire inode, we have ot log the entire
32630f712c9SDave Chinner * inode so that the entire range the CRC covers is present in the log.
32730f712c9SDave Chinner * That means for v3 inode we log the entire buffer rather than just the
32830f712c9SDave Chinner * inode cores.
32930f712c9SDave Chinner */
330ebd9027dSDave Chinner if (xfs_has_v3inodes(mp)) {
33130f712c9SDave Chinner version = 3;
33243004b2aSDarrick J. Wong ino = XFS_AGINO_TO_INO(mp, agno, XFS_AGB_TO_AGINO(mp, agbno));
33330f712c9SDave Chinner
33430f712c9SDave Chinner /*
33530f712c9SDave Chinner * log the initialisation that is about to take place as an
33630f712c9SDave Chinner * logical operation. This means the transaction does not
33730f712c9SDave Chinner * need to log the physical changes to the inode buffers as log
33830f712c9SDave Chinner * recovery will know what initialisation is actually needed.
33930f712c9SDave Chinner * Hence we only need to log the buffers as "ordered" buffers so
34030f712c9SDave Chinner * they track in the AIL as if they were physically logged.
34130f712c9SDave Chinner */
34230f712c9SDave Chinner if (tp)
343463958afSBrian Foster xfs_icreate_log(tp, agno, agbno, icount,
34430f712c9SDave Chinner mp->m_sb.sb_inodesize, length, gen);
34530f712c9SDave Chinner } else
34630f712c9SDave Chinner version = 2;
34730f712c9SDave Chinner
34830f712c9SDave Chinner for (j = 0; j < nbufs; j++) {
34930f712c9SDave Chinner /*
35030f712c9SDave Chinner * Get the block.
35130f712c9SDave Chinner */
35283dcdb44SDarrick J. Wong d = XFS_AGB_TO_DADDR(mp, agno, agbno +
353ef325959SDarrick J. Wong (j * M_IGEO(mp)->blocks_per_cluster));
354ce92464cSDarrick J. Wong error = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
355ce92464cSDarrick J. Wong mp->m_bsize * M_IGEO(mp)->blocks_per_cluster,
356ce92464cSDarrick J. Wong XBF_UNMAPPED, &fbuf);
357ce92464cSDarrick J. Wong if (error)
358ce92464cSDarrick J. Wong return error;
35930f712c9SDave Chinner
36030f712c9SDave Chinner /* Initialize the inode buffers and log them appropriately. */
36130f712c9SDave Chinner fbuf->b_ops = &xfs_inode_buf_ops;
36230f712c9SDave Chinner xfs_buf_zero(fbuf, 0, BBTOB(fbuf->b_length));
363ef325959SDarrick J. Wong for (i = 0; i < M_IGEO(mp)->inodes_per_cluster; i++) {
36430f712c9SDave Chinner int ioffset = i << mp->m_sb.sb_inodelog;
36530f712c9SDave Chinner
36630f712c9SDave Chinner free = xfs_make_iptr(mp, fbuf, i);
36730f712c9SDave Chinner free->di_magic = cpu_to_be16(XFS_DINODE_MAGIC);
36830f712c9SDave Chinner free->di_version = version;
36930f712c9SDave Chinner free->di_gen = cpu_to_be32(gen);
37030f712c9SDave Chinner free->di_next_unlinked = cpu_to_be32(NULLAGINO);
37130f712c9SDave Chinner
37230f712c9SDave Chinner if (version == 3) {
37330f712c9SDave Chinner free->di_ino = cpu_to_be64(ino);
37430f712c9SDave Chinner ino++;
375ce748eaaSEric Sandeen uuid_copy(&free->di_uuid,
376ce748eaaSEric Sandeen &mp->m_sb.sb_meta_uuid);
37730f712c9SDave Chinner xfs_dinode_calc_crc(mp, free);
37830f712c9SDave Chinner } else if (tp) {
37930f712c9SDave Chinner /* just log the inode core */
38030f712c9SDave Chinner xfs_trans_log_buf(tp, fbuf, ioffset,
381cf28e17cSDave Chinner ioffset + XFS_DINODE_SIZE(mp) - 1);
38230f712c9SDave Chinner }
38330f712c9SDave Chinner }
38430f712c9SDave Chinner
38530f712c9SDave Chinner if (tp) {
38630f712c9SDave Chinner /*
38730f712c9SDave Chinner * Mark the buffer as an inode allocation buffer so it
38830f712c9SDave Chinner * sticks in AIL at the point of this allocation
38930f712c9SDave Chinner * transaction. This ensures the they are on disk before
39030f712c9SDave Chinner * the tail of the log can be moved past this
39130f712c9SDave Chinner * transaction (i.e. by preventing relogging from moving
39230f712c9SDave Chinner * it forward in the log).
39330f712c9SDave Chinner */
39430f712c9SDave Chinner xfs_trans_inode_alloc_buf(tp, fbuf);
39530f712c9SDave Chinner if (version == 3) {
39630f712c9SDave Chinner /*
39730f712c9SDave Chinner * Mark the buffer as ordered so that they are
39830f712c9SDave Chinner * not physically logged in the transaction but
39930f712c9SDave Chinner * still tracked in the AIL as part of the
40030f712c9SDave Chinner * transaction and pin the log appropriately.
40130f712c9SDave Chinner */
40230f712c9SDave Chinner xfs_trans_ordered_buf(tp, fbuf);
40330f712c9SDave Chinner }
40430f712c9SDave Chinner } else {
40530f712c9SDave Chinner fbuf->b_flags |= XBF_DONE;
40630f712c9SDave Chinner xfs_buf_delwri_queue(fbuf, buffer_list);
40730f712c9SDave Chinner xfs_buf_relse(fbuf);
40830f712c9SDave Chinner }
40930f712c9SDave Chinner }
41030f712c9SDave Chinner return 0;
41130f712c9SDave Chinner }
41230f712c9SDave Chinner
41330f712c9SDave Chinner /*
41456d1115cSBrian Foster * Align startino and allocmask for a recently allocated sparse chunk such that
41556d1115cSBrian Foster * they are fit for insertion (or merge) into the on-disk inode btrees.
41656d1115cSBrian Foster *
41756d1115cSBrian Foster * Background:
41856d1115cSBrian Foster *
41956d1115cSBrian Foster * When enabled, sparse inode support increases the inode alignment from cluster
42056d1115cSBrian Foster * size to inode chunk size. This means that the minimum range between two
42156d1115cSBrian Foster * non-adjacent inode records in the inobt is large enough for a full inode
42256d1115cSBrian Foster * record. This allows for cluster sized, cluster aligned block allocation
42356d1115cSBrian Foster * without need to worry about whether the resulting inode record overlaps with
42456d1115cSBrian Foster * another record in the tree. Without this basic rule, we would have to deal
42556d1115cSBrian Foster * with the consequences of overlap by potentially undoing recent allocations in
42656d1115cSBrian Foster * the inode allocation codepath.
42756d1115cSBrian Foster *
42856d1115cSBrian Foster * Because of this alignment rule (which is enforced on mount), there are two
42956d1115cSBrian Foster * inobt possibilities for newly allocated sparse chunks. One is that the
43056d1115cSBrian Foster * aligned inode record for the chunk covers a range of inodes not already
43156d1115cSBrian Foster * covered in the inobt (i.e., it is safe to insert a new sparse record). The
43256d1115cSBrian Foster * other is that a record already exists at the aligned startino that considers
43356d1115cSBrian Foster * the newly allocated range as sparse. In the latter case, record content is
43456d1115cSBrian Foster * merged in hope that sparse inode chunks fill to full chunks over time.
43556d1115cSBrian Foster */
43656d1115cSBrian Foster STATIC void
xfs_align_sparse_ino(struct xfs_mount * mp,xfs_agino_t * startino,uint16_t * allocmask)43756d1115cSBrian Foster xfs_align_sparse_ino(
43856d1115cSBrian Foster struct xfs_mount *mp,
43956d1115cSBrian Foster xfs_agino_t *startino,
44056d1115cSBrian Foster uint16_t *allocmask)
44156d1115cSBrian Foster {
44256d1115cSBrian Foster xfs_agblock_t agbno;
44356d1115cSBrian Foster xfs_agblock_t mod;
44456d1115cSBrian Foster int offset;
44556d1115cSBrian Foster
44656d1115cSBrian Foster agbno = XFS_AGINO_TO_AGBNO(mp, *startino);
44756d1115cSBrian Foster mod = agbno % mp->m_sb.sb_inoalignmt;
44856d1115cSBrian Foster if (!mod)
44956d1115cSBrian Foster return;
45056d1115cSBrian Foster
45156d1115cSBrian Foster /* calculate the inode offset and align startino */
45243004b2aSDarrick J. Wong offset = XFS_AGB_TO_AGINO(mp, mod);
45356d1115cSBrian Foster *startino -= offset;
45456d1115cSBrian Foster
45556d1115cSBrian Foster /*
45656d1115cSBrian Foster * Since startino has been aligned down, left shift allocmask such that
45756d1115cSBrian Foster * it continues to represent the same physical inodes relative to the
45856d1115cSBrian Foster * new startino.
45956d1115cSBrian Foster */
46056d1115cSBrian Foster *allocmask <<= offset / XFS_INODES_PER_HOLEMASK_BIT;
46156d1115cSBrian Foster }
46256d1115cSBrian Foster
46356d1115cSBrian Foster /*
46456d1115cSBrian Foster * Determine whether the source inode record can merge into the target. Both
46556d1115cSBrian Foster * records must be sparse, the inode ranges must match and there must be no
46656d1115cSBrian Foster * allocation overlap between the records.
46756d1115cSBrian Foster */
46856d1115cSBrian Foster STATIC bool
__xfs_inobt_can_merge(struct xfs_inobt_rec_incore * trec,struct xfs_inobt_rec_incore * srec)46956d1115cSBrian Foster __xfs_inobt_can_merge(
47056d1115cSBrian Foster struct xfs_inobt_rec_incore *trec, /* tgt record */
47156d1115cSBrian Foster struct xfs_inobt_rec_incore *srec) /* src record */
47256d1115cSBrian Foster {
47356d1115cSBrian Foster uint64_t talloc;
47456d1115cSBrian Foster uint64_t salloc;
47556d1115cSBrian Foster
47656d1115cSBrian Foster /* records must cover the same inode range */
47756d1115cSBrian Foster if (trec->ir_startino != srec->ir_startino)
47856d1115cSBrian Foster return false;
47956d1115cSBrian Foster
48056d1115cSBrian Foster /* both records must be sparse */
48156d1115cSBrian Foster if (!xfs_inobt_issparse(trec->ir_holemask) ||
48256d1115cSBrian Foster !xfs_inobt_issparse(srec->ir_holemask))
48356d1115cSBrian Foster return false;
48456d1115cSBrian Foster
48556d1115cSBrian Foster /* both records must track some inodes */
48656d1115cSBrian Foster if (!trec->ir_count || !srec->ir_count)
48756d1115cSBrian Foster return false;
48856d1115cSBrian Foster
48956d1115cSBrian Foster /* can't exceed capacity of a full record */
49056d1115cSBrian Foster if (trec->ir_count + srec->ir_count > XFS_INODES_PER_CHUNK)
49156d1115cSBrian Foster return false;
49256d1115cSBrian Foster
49356d1115cSBrian Foster /* verify there is no allocation overlap */
49456d1115cSBrian Foster talloc = xfs_inobt_irec_to_allocmask(trec);
49556d1115cSBrian Foster salloc = xfs_inobt_irec_to_allocmask(srec);
49656d1115cSBrian Foster if (talloc & salloc)
49756d1115cSBrian Foster return false;
49856d1115cSBrian Foster
49956d1115cSBrian Foster return true;
50056d1115cSBrian Foster }
50156d1115cSBrian Foster
50256d1115cSBrian Foster /*
50356d1115cSBrian Foster * Merge the source inode record into the target. The caller must call
50456d1115cSBrian Foster * __xfs_inobt_can_merge() to ensure the merge is valid.
50556d1115cSBrian Foster */
50656d1115cSBrian Foster STATIC void
__xfs_inobt_rec_merge(struct xfs_inobt_rec_incore * trec,struct xfs_inobt_rec_incore * srec)50756d1115cSBrian Foster __xfs_inobt_rec_merge(
50856d1115cSBrian Foster struct xfs_inobt_rec_incore *trec, /* target */
50956d1115cSBrian Foster struct xfs_inobt_rec_incore *srec) /* src */
51056d1115cSBrian Foster {
51156d1115cSBrian Foster ASSERT(trec->ir_startino == srec->ir_startino);
51256d1115cSBrian Foster
51356d1115cSBrian Foster /* combine the counts */
51456d1115cSBrian Foster trec->ir_count += srec->ir_count;
51556d1115cSBrian Foster trec->ir_freecount += srec->ir_freecount;
51656d1115cSBrian Foster
51756d1115cSBrian Foster /*
51856d1115cSBrian Foster * Merge the holemask and free mask. For both fields, 0 bits refer to
51956d1115cSBrian Foster * allocated inodes. We combine the allocated ranges with bitwise AND.
52056d1115cSBrian Foster */
52156d1115cSBrian Foster trec->ir_holemask &= srec->ir_holemask;
52256d1115cSBrian Foster trec->ir_free &= srec->ir_free;
52356d1115cSBrian Foster }
52456d1115cSBrian Foster
52556d1115cSBrian Foster /*
52656d1115cSBrian Foster * Insert a new sparse inode chunk into the associated inode btree. The inode
52756d1115cSBrian Foster * record for the sparse chunk is pre-aligned to a startino that should match
52856d1115cSBrian Foster * any pre-existing sparse inode record in the tree. This allows sparse chunks
52956d1115cSBrian Foster * to fill over time.
53056d1115cSBrian Foster *
53156d1115cSBrian Foster * This function supports two modes of handling preexisting records depending on
53256d1115cSBrian Foster * the merge flag. If merge is true, the provided record is merged with the
53356d1115cSBrian Foster * existing record and updated in place. The merged record is returned in nrec.
53456d1115cSBrian Foster * If merge is false, an existing record is replaced with the provided record.
53556d1115cSBrian Foster * If no preexisting record exists, the provided record is always inserted.
53656d1115cSBrian Foster *
53756d1115cSBrian Foster * It is considered corruption if a merge is requested and not possible. Given
53856d1115cSBrian Foster * the sparse inode alignment constraints, this should never happen.
53956d1115cSBrian Foster */
54056d1115cSBrian Foster STATIC int
xfs_inobt_insert_sprec(struct xfs_perag * pag,struct xfs_trans * tp,struct xfs_buf * agbp,int btnum,struct xfs_inobt_rec_incore * nrec,bool merge)54156d1115cSBrian Foster xfs_inobt_insert_sprec(
542dedab3e4SDave Chinner struct xfs_perag *pag,
54356d1115cSBrian Foster struct xfs_trans *tp,
54456d1115cSBrian Foster struct xfs_buf *agbp,
54556d1115cSBrian Foster int btnum,
54656d1115cSBrian Foster struct xfs_inobt_rec_incore *nrec, /* in/out: new/merged rec. */
54756d1115cSBrian Foster bool merge) /* merge or replace */
54856d1115cSBrian Foster {
549dedab3e4SDave Chinner struct xfs_mount *mp = pag->pag_mount;
55056d1115cSBrian Foster struct xfs_btree_cur *cur;
55156d1115cSBrian Foster int error;
55256d1115cSBrian Foster int i;
55356d1115cSBrian Foster struct xfs_inobt_rec_incore rec;
55456d1115cSBrian Foster
555bab8b795SDave Chinner cur = xfs_inobt_init_cursor(pag, tp, agbp, btnum);
55656d1115cSBrian Foster
55756d1115cSBrian Foster /* the new record is pre-aligned so we know where to look */
55856d1115cSBrian Foster error = xfs_inobt_lookup(cur, nrec->ir_startino, XFS_LOOKUP_EQ, &i);
55956d1115cSBrian Foster if (error)
56056d1115cSBrian Foster goto error;
56156d1115cSBrian Foster /* if nothing there, insert a new record and return */
56256d1115cSBrian Foster if (i == 0) {
56356d1115cSBrian Foster error = xfs_inobt_insert_rec(cur, nrec->ir_holemask,
56456d1115cSBrian Foster nrec->ir_count, nrec->ir_freecount,
56556d1115cSBrian Foster nrec->ir_free, &i);
56656d1115cSBrian Foster if (error)
56756d1115cSBrian Foster goto error;
568f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(mp, i != 1)) {
569f9e03706SDarrick J. Wong error = -EFSCORRUPTED;
570f9e03706SDarrick J. Wong goto error;
571f9e03706SDarrick J. Wong }
57256d1115cSBrian Foster
57356d1115cSBrian Foster goto out;
57456d1115cSBrian Foster }
57556d1115cSBrian Foster
57656d1115cSBrian Foster /*
57756d1115cSBrian Foster * A record exists at this startino. Merge or replace the record
57856d1115cSBrian Foster * depending on what we've been asked to do.
57956d1115cSBrian Foster */
58056d1115cSBrian Foster if (merge) {
58156d1115cSBrian Foster error = xfs_inobt_get_rec(cur, &rec, &i);
58256d1115cSBrian Foster if (error)
58356d1115cSBrian Foster goto error;
584f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(mp, i != 1)) {
585f9e03706SDarrick J. Wong error = -EFSCORRUPTED;
586f9e03706SDarrick J. Wong goto error;
587f9e03706SDarrick J. Wong }
588f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(mp, rec.ir_startino != nrec->ir_startino)) {
589f9e03706SDarrick J. Wong error = -EFSCORRUPTED;
590f9e03706SDarrick J. Wong goto error;
591f9e03706SDarrick J. Wong }
59256d1115cSBrian Foster
59356d1115cSBrian Foster /*
59456d1115cSBrian Foster * This should never fail. If we have coexisting records that
59556d1115cSBrian Foster * cannot merge, something is seriously wrong.
59656d1115cSBrian Foster */
597f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(mp, !__xfs_inobt_can_merge(nrec, &rec))) {
598f9e03706SDarrick J. Wong error = -EFSCORRUPTED;
599f9e03706SDarrick J. Wong goto error;
600f9e03706SDarrick J. Wong }
60156d1115cSBrian Foster
6027b13c515SDave Chinner trace_xfs_irec_merge_pre(mp, pag->pag_agno, rec.ir_startino,
60356d1115cSBrian Foster rec.ir_holemask, nrec->ir_startino,
60456d1115cSBrian Foster nrec->ir_holemask);
60556d1115cSBrian Foster
60656d1115cSBrian Foster /* merge to nrec to output the updated record */
60756d1115cSBrian Foster __xfs_inobt_rec_merge(nrec, &rec);
60856d1115cSBrian Foster
6097b13c515SDave Chinner trace_xfs_irec_merge_post(mp, pag->pag_agno, nrec->ir_startino,
61056d1115cSBrian Foster nrec->ir_holemask);
61156d1115cSBrian Foster
61256d1115cSBrian Foster error = xfs_inobt_rec_check_count(mp, nrec);
61356d1115cSBrian Foster if (error)
61456d1115cSBrian Foster goto error;
61556d1115cSBrian Foster }
61656d1115cSBrian Foster
61756d1115cSBrian Foster error = xfs_inobt_update(cur, nrec);
61856d1115cSBrian Foster if (error)
61956d1115cSBrian Foster goto error;
62056d1115cSBrian Foster
62156d1115cSBrian Foster out:
62256d1115cSBrian Foster xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
62356d1115cSBrian Foster return 0;
62456d1115cSBrian Foster error:
62556d1115cSBrian Foster xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
62656d1115cSBrian Foster return error;
62756d1115cSBrian Foster }
62856d1115cSBrian Foster
62956d1115cSBrian Foster /*
6308237fbf5SDave Chinner * Allocate new inodes in the allocation group specified by agbp. Returns 0 if
6318237fbf5SDave Chinner * inodes were allocated in this AG; -EAGAIN if there was no space in this AG so
6328237fbf5SDave Chinner * the caller knows it can try another AG, a hard -ENOSPC when over the maximum
6338237fbf5SDave Chinner * inode count threshold, or the usual negative error code for other errors.
63430f712c9SDave Chinner */
635ef325959SDarrick J. Wong STATIC int
xfs_ialloc_ag_alloc(struct xfs_perag * pag,struct xfs_trans * tp,struct xfs_buf * agbp)63630f712c9SDave Chinner xfs_ialloc_ag_alloc(
637dedab3e4SDave Chinner struct xfs_perag *pag,
638ef325959SDarrick J. Wong struct xfs_trans *tp,
639dedab3e4SDave Chinner struct xfs_buf *agbp)
64030f712c9SDave Chinner {
641ef325959SDarrick J. Wong struct xfs_agi *agi;
642ef325959SDarrick J. Wong struct xfs_alloc_arg args;
64330f712c9SDave Chinner int error;
64430f712c9SDave Chinner xfs_agino_t newino; /* new first inode's number */
64530f712c9SDave Chinner xfs_agino_t newlen; /* new number of inodes */
646ef325959SDarrick J. Wong int isaligned = 0; /* inode allocation at stripe */
647ef325959SDarrick J. Wong /* unit boundary */
648ef325959SDarrick J. Wong /* init. to full chunk */
64956d1115cSBrian Foster struct xfs_inobt_rec_incore rec;
650ef325959SDarrick J. Wong struct xfs_ino_geometry *igeo = M_IGEO(tp->t_mountp);
6517b13c515SDave Chinner uint16_t allocmask = (uint16_t) -1;
6521cdadee1SBrian Foster int do_sparse = 0;
6531cdadee1SBrian Foster
65430f712c9SDave Chinner memset(&args, 0, sizeof(args));
65530f712c9SDave Chinner args.tp = tp;
65630f712c9SDave Chinner args.mp = tp->t_mountp;
6571cdadee1SBrian Foster args.fsbno = NULLFSBLOCK;
6587280fedaSDarrick J. Wong args.oinfo = XFS_RMAP_OINFO_INODES;
65974c36a86SDave Chinner args.pag = pag;
66030f712c9SDave Chinner
66146fc58daSBrian Foster #ifdef DEBUG
66246fc58daSBrian Foster /* randomly do sparse inode allocations */
663ebd9027dSDave Chinner if (xfs_has_sparseinodes(tp->t_mountp) &&
664ef325959SDarrick J. Wong igeo->ialloc_min_blks < igeo->ialloc_blks)
6658032bf12SJason A. Donenfeld do_sparse = get_random_u32_below(2);
66646fc58daSBrian Foster #endif
66746fc58daSBrian Foster
66830f712c9SDave Chinner /*
66930f712c9SDave Chinner * Locking will ensure that we don't have two callers in here
67030f712c9SDave Chinner * at one time.
67130f712c9SDave Chinner */
672ef325959SDarrick J. Wong newlen = igeo->ialloc_inos;
673ef325959SDarrick J. Wong if (igeo->maxicount &&
67474f9ce1cSGeorge Wang percpu_counter_read_positive(&args.mp->m_icount) + newlen >
675ef325959SDarrick J. Wong igeo->maxicount)
6762451337dSDave Chinner return -ENOSPC;
677ef325959SDarrick J. Wong args.minlen = args.maxlen = igeo->ialloc_blks;
67830f712c9SDave Chinner /*
67930f712c9SDave Chinner * First try to allocate inodes contiguous with the last-allocated
68030f712c9SDave Chinner * chunk of inodes. If the filesystem is striped, this will fill
68130f712c9SDave Chinner * an entire stripe unit with inodes.
68230f712c9SDave Chinner */
683370c782bSChristoph Hellwig agi = agbp->b_addr;
68430f712c9SDave Chinner newino = be32_to_cpu(agi->agi_newino);
68530f712c9SDave Chinner args.agbno = XFS_AGINO_TO_AGBNO(args.mp, newino) +
686ef325959SDarrick J. Wong igeo->ialloc_blks;
6871cdadee1SBrian Foster if (do_sparse)
6881cdadee1SBrian Foster goto sparse_alloc;
68930f712c9SDave Chinner if (likely(newino != NULLAGINO &&
69030f712c9SDave Chinner (args.agbno < be32_to_cpu(agi->agi_length)))) {
69130f712c9SDave Chinner args.prod = 1;
69230f712c9SDave Chinner
69330f712c9SDave Chinner /*
69430f712c9SDave Chinner * We need to take into account alignment here to ensure that
69530f712c9SDave Chinner * we don't modify the free list if we fail to have an exact
69630f712c9SDave Chinner * block. If we don't have an exact match, and every oher
69730f712c9SDave Chinner * attempt allocation attempt fails, we'll end up cancelling
69830f712c9SDave Chinner * a dirty transaction and shutting down.
69930f712c9SDave Chinner *
70030f712c9SDave Chinner * For an exact allocation, alignment must be 1,
70130f712c9SDave Chinner * however we need to take cluster alignment into account when
70230f712c9SDave Chinner * fixing up the freelist. Use the minalignslop field to
70330f712c9SDave Chinner * indicate that extra blocks might be required for alignment,
70430f712c9SDave Chinner * but not to use them in the actual exact allocation.
70530f712c9SDave Chinner */
70630f712c9SDave Chinner args.alignment = 1;
707ef325959SDarrick J. Wong args.minalignslop = igeo->cluster_align - 1;
70830f712c9SDave Chinner
70930f712c9SDave Chinner /* Allow space for the inode btree to split. */
710657f1019SBrian Foster args.minleft = igeo->inobt_maxlevels;
7115f36b2ceSDave Chinner error = xfs_alloc_vextent_exact_bno(&args,
7125f36b2ceSDave Chinner XFS_AGB_TO_FSB(args.mp, pag->pag_agno,
7135f36b2ceSDave Chinner args.agbno));
71474c36a86SDave Chinner if (error)
71530f712c9SDave Chinner return error;
71630f712c9SDave Chinner
71730f712c9SDave Chinner /*
71830f712c9SDave Chinner * This request might have dirtied the transaction if the AG can
71930f712c9SDave Chinner * satisfy the request, but the exact block was not available.
72030f712c9SDave Chinner * If the allocation did fail, subsequent requests will relax
72130f712c9SDave Chinner * the exact agbno requirement and increase the alignment
72230f712c9SDave Chinner * instead. It is critical that the total size of the request
72330f712c9SDave Chinner * (len + alignment + slop) does not increase from this point
72430f712c9SDave Chinner * on, so reset minalignslop to ensure it is not included in
72530f712c9SDave Chinner * subsequent requests.
72630f712c9SDave Chinner */
72730f712c9SDave Chinner args.minalignslop = 0;
7281cdadee1SBrian Foster }
72930f712c9SDave Chinner
73030f712c9SDave Chinner if (unlikely(args.fsbno == NULLFSBLOCK)) {
73130f712c9SDave Chinner /*
73230f712c9SDave Chinner * Set the alignment for the allocation.
73330f712c9SDave Chinner * If stripe alignment is turned on then align at stripe unit
73430f712c9SDave Chinner * boundary.
73530f712c9SDave Chinner * If the cluster size is smaller than a filesystem block
73630f712c9SDave Chinner * then we're doing I/O for inodes in filesystem block size
73730f712c9SDave Chinner * pieces, so don't need alignment anyway.
73830f712c9SDave Chinner */
73930f712c9SDave Chinner isaligned = 0;
740ef325959SDarrick J. Wong if (igeo->ialloc_align) {
7410560f31aSDave Chinner ASSERT(!xfs_has_noalign(args.mp));
74230f712c9SDave Chinner args.alignment = args.mp->m_dalign;
74330f712c9SDave Chinner isaligned = 1;
74430f712c9SDave Chinner } else
745ef325959SDarrick J. Wong args.alignment = igeo->cluster_align;
74630f712c9SDave Chinner /*
74730f712c9SDave Chinner * Allocate a fixed-size extent of inodes.
74830f712c9SDave Chinner */
74930f712c9SDave Chinner args.prod = 1;
75030f712c9SDave Chinner /*
75130f712c9SDave Chinner * Allow space for the inode btree to split.
75230f712c9SDave Chinner */
753657f1019SBrian Foster args.minleft = igeo->inobt_maxlevels;
754db4710fdSDave Chinner error = xfs_alloc_vextent_near_bno(&args,
755db4710fdSDave Chinner XFS_AGB_TO_FSB(args.mp, pag->pag_agno,
756db4710fdSDave Chinner be32_to_cpu(agi->agi_root)));
75774c36a86SDave Chinner if (error)
75830f712c9SDave Chinner return error;
75930f712c9SDave Chinner }
76030f712c9SDave Chinner
76130f712c9SDave Chinner /*
76230f712c9SDave Chinner * If stripe alignment is turned on, then try again with cluster
76330f712c9SDave Chinner * alignment.
76430f712c9SDave Chinner */
76530f712c9SDave Chinner if (isaligned && args.fsbno == NULLFSBLOCK) {
766ef325959SDarrick J. Wong args.alignment = igeo->cluster_align;
767db4710fdSDave Chinner error = xfs_alloc_vextent_near_bno(&args,
768db4710fdSDave Chinner XFS_AGB_TO_FSB(args.mp, pag->pag_agno,
769db4710fdSDave Chinner be32_to_cpu(agi->agi_root)));
770db4710fdSDave Chinner if (error)
77130f712c9SDave Chinner return error;
77230f712c9SDave Chinner }
77330f712c9SDave Chinner
77456d1115cSBrian Foster /*
77556d1115cSBrian Foster * Finally, try a sparse allocation if the filesystem supports it and
77656d1115cSBrian Foster * the sparse allocation length is smaller than a full chunk.
77756d1115cSBrian Foster */
778ebd9027dSDave Chinner if (xfs_has_sparseinodes(args.mp) &&
779ef325959SDarrick J. Wong igeo->ialloc_min_blks < igeo->ialloc_blks &&
78056d1115cSBrian Foster args.fsbno == NULLFSBLOCK) {
7811cdadee1SBrian Foster sparse_alloc:
78256d1115cSBrian Foster args.alignment = args.mp->m_sb.sb_spino_align;
78356d1115cSBrian Foster args.prod = 1;
78456d1115cSBrian Foster
785ef325959SDarrick J. Wong args.minlen = igeo->ialloc_min_blks;
78656d1115cSBrian Foster args.maxlen = args.minlen;
78756d1115cSBrian Foster
78856d1115cSBrian Foster /*
78956d1115cSBrian Foster * The inode record will be aligned to full chunk size. We must
79056d1115cSBrian Foster * prevent sparse allocation from AG boundaries that result in
79156d1115cSBrian Foster * invalid inode records, such as records that start at agbno 0
79256d1115cSBrian Foster * or extend beyond the AG.
79356d1115cSBrian Foster *
79456d1115cSBrian Foster * Set min agbno to the first aligned, non-zero agbno and max to
79556d1115cSBrian Foster * the last aligned agbno that is at least one full chunk from
79656d1115cSBrian Foster * the end of the AG.
79756d1115cSBrian Foster */
79856d1115cSBrian Foster args.min_agbno = args.mp->m_sb.sb_inoalignmt;
79956d1115cSBrian Foster args.max_agbno = round_down(args.mp->m_sb.sb_agblocks,
80056d1115cSBrian Foster args.mp->m_sb.sb_inoalignmt) -
801ef325959SDarrick J. Wong igeo->ialloc_blks;
80256d1115cSBrian Foster
803db4710fdSDave Chinner error = xfs_alloc_vextent_near_bno(&args,
804db4710fdSDave Chinner XFS_AGB_TO_FSB(args.mp, pag->pag_agno,
805db4710fdSDave Chinner be32_to_cpu(agi->agi_root)));
80656d1115cSBrian Foster if (error)
80756d1115cSBrian Foster return error;
80856d1115cSBrian Foster
80943004b2aSDarrick J. Wong newlen = XFS_AGB_TO_AGINO(args.mp, args.len);
81046fc58daSBrian Foster ASSERT(newlen <= XFS_INODES_PER_CHUNK);
81156d1115cSBrian Foster allocmask = (1 << (newlen / XFS_INODES_PER_HOLEMASK_BIT)) - 1;
81256d1115cSBrian Foster }
81356d1115cSBrian Foster
8143937493cSGao Xiang if (args.fsbno == NULLFSBLOCK)
8158237fbf5SDave Chinner return -EAGAIN;
8163937493cSGao Xiang
81730f712c9SDave Chinner ASSERT(args.len == args.minlen);
81830f712c9SDave Chinner
81930f712c9SDave Chinner /*
82030f712c9SDave Chinner * Stamp and write the inode buffers.
82130f712c9SDave Chinner *
82230f712c9SDave Chinner * Seed the new inode cluster with a random generation number. This
82330f712c9SDave Chinner * prevents short-term reuse of generation numbers if a chunk is
82430f712c9SDave Chinner * freed and then immediately reallocated. We use random numbers
82530f712c9SDave Chinner * rather than a linear progression to prevent the next generation
82630f712c9SDave Chinner * number from being easily guessable.
82730f712c9SDave Chinner */
8287b13c515SDave Chinner error = xfs_ialloc_inode_init(args.mp, tp, NULL, newlen, pag->pag_agno,
829a251c17aSJason A. Donenfeld args.agbno, args.len, get_random_u32());
83030f712c9SDave Chinner
83130f712c9SDave Chinner if (error)
83230f712c9SDave Chinner return error;
83330f712c9SDave Chinner /*
83430f712c9SDave Chinner * Convert the results.
83530f712c9SDave Chinner */
83643004b2aSDarrick J. Wong newino = XFS_AGB_TO_AGINO(args.mp, args.agbno);
83756d1115cSBrian Foster
83856d1115cSBrian Foster if (xfs_inobt_issparse(~allocmask)) {
83956d1115cSBrian Foster /*
84056d1115cSBrian Foster * We've allocated a sparse chunk. Align the startino and mask.
84156d1115cSBrian Foster */
84256d1115cSBrian Foster xfs_align_sparse_ino(args.mp, &newino, &allocmask);
84356d1115cSBrian Foster
84456d1115cSBrian Foster rec.ir_startino = newino;
84556d1115cSBrian Foster rec.ir_holemask = ~allocmask;
84656d1115cSBrian Foster rec.ir_count = newlen;
84756d1115cSBrian Foster rec.ir_freecount = newlen;
84856d1115cSBrian Foster rec.ir_free = XFS_INOBT_ALL_FREE;
84956d1115cSBrian Foster
85056d1115cSBrian Foster /*
85156d1115cSBrian Foster * Insert the sparse record into the inobt and allow for a merge
85256d1115cSBrian Foster * if necessary. If a merge does occur, rec is updated to the
85356d1115cSBrian Foster * merged record.
85456d1115cSBrian Foster */
855dedab3e4SDave Chinner error = xfs_inobt_insert_sprec(pag, tp, agbp,
8567b13c515SDave Chinner XFS_BTNUM_INO, &rec, true);
85756d1115cSBrian Foster if (error == -EFSCORRUPTED) {
85856d1115cSBrian Foster xfs_alert(args.mp,
85956d1115cSBrian Foster "invalid sparse inode record: ino 0x%llx holemask 0x%x count %u",
8607b13c515SDave Chinner XFS_AGINO_TO_INO(args.mp, pag->pag_agno,
86156d1115cSBrian Foster rec.ir_startino),
86256d1115cSBrian Foster rec.ir_holemask, rec.ir_count);
86356d1115cSBrian Foster xfs_force_shutdown(args.mp, SHUTDOWN_CORRUPT_INCORE);
86456d1115cSBrian Foster }
86556d1115cSBrian Foster if (error)
86656d1115cSBrian Foster return error;
86756d1115cSBrian Foster
86856d1115cSBrian Foster /*
86956d1115cSBrian Foster * We can't merge the part we've just allocated as for the inobt
87056d1115cSBrian Foster * due to finobt semantics. The original record may or may not
87156d1115cSBrian Foster * exist independent of whether physical inodes exist in this
87256d1115cSBrian Foster * sparse chunk.
87356d1115cSBrian Foster *
87456d1115cSBrian Foster * We must update the finobt record based on the inobt record.
87556d1115cSBrian Foster * rec contains the fully merged and up to date inobt record
87656d1115cSBrian Foster * from the previous call. Set merge false to replace any
87756d1115cSBrian Foster * existing record with this one.
87856d1115cSBrian Foster */
879ebd9027dSDave Chinner if (xfs_has_finobt(args.mp)) {
880dedab3e4SDave Chinner error = xfs_inobt_insert_sprec(pag, tp, agbp,
8817b13c515SDave Chinner XFS_BTNUM_FINO, &rec, false);
88256d1115cSBrian Foster if (error)
88356d1115cSBrian Foster return error;
88456d1115cSBrian Foster }
88556d1115cSBrian Foster } else {
88656d1115cSBrian Foster /* full chunk - insert new records to both btrees */
887dedab3e4SDave Chinner error = xfs_inobt_insert(pag, tp, agbp, newino, newlen,
88856d1115cSBrian Foster XFS_BTNUM_INO);
88956d1115cSBrian Foster if (error)
89056d1115cSBrian Foster return error;
89156d1115cSBrian Foster
892ebd9027dSDave Chinner if (xfs_has_finobt(args.mp)) {
893dedab3e4SDave Chinner error = xfs_inobt_insert(pag, tp, agbp, newino,
89456d1115cSBrian Foster newlen, XFS_BTNUM_FINO);
89556d1115cSBrian Foster if (error)
89656d1115cSBrian Foster return error;
89756d1115cSBrian Foster }
89856d1115cSBrian Foster }
89956d1115cSBrian Foster
90056d1115cSBrian Foster /*
90156d1115cSBrian Foster * Update AGI counts and newino.
90256d1115cSBrian Foster */
90330f712c9SDave Chinner be32_add_cpu(&agi->agi_count, newlen);
90430f712c9SDave Chinner be32_add_cpu(&agi->agi_freecount, newlen);
90530f712c9SDave Chinner pag->pagi_freecount += newlen;
90689e9b5c0SDarrick J. Wong pag->pagi_count += newlen;
90730f712c9SDave Chinner agi->agi_newino = cpu_to_be32(newino);
90830f712c9SDave Chinner
90930f712c9SDave Chinner /*
91030f712c9SDave Chinner * Log allocation group header fields
91130f712c9SDave Chinner */
91230f712c9SDave Chinner xfs_ialloc_log_agi(tp, agbp,
91330f712c9SDave Chinner XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO);
91430f712c9SDave Chinner /*
91530f712c9SDave Chinner * Modify/log superblock values for inode count and inode free count.
91630f712c9SDave Chinner */
91730f712c9SDave Chinner xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen);
91830f712c9SDave Chinner xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen);
91930f712c9SDave Chinner return 0;
92030f712c9SDave Chinner }
92130f712c9SDave Chinner
92230f712c9SDave Chinner /*
92330f712c9SDave Chinner * Try to retrieve the next record to the left/right from the current one.
92430f712c9SDave Chinner */
92530f712c9SDave Chinner STATIC int
xfs_ialloc_next_rec(struct xfs_btree_cur * cur,xfs_inobt_rec_incore_t * rec,int * done,int left)92630f712c9SDave Chinner xfs_ialloc_next_rec(
92730f712c9SDave Chinner struct xfs_btree_cur *cur,
92830f712c9SDave Chinner xfs_inobt_rec_incore_t *rec,
92930f712c9SDave Chinner int *done,
93030f712c9SDave Chinner int left)
93130f712c9SDave Chinner {
93230f712c9SDave Chinner int error;
93330f712c9SDave Chinner int i;
93430f712c9SDave Chinner
93530f712c9SDave Chinner if (left)
93630f712c9SDave Chinner error = xfs_btree_decrement(cur, 0, &i);
93730f712c9SDave Chinner else
93830f712c9SDave Chinner error = xfs_btree_increment(cur, 0, &i);
93930f712c9SDave Chinner
94030f712c9SDave Chinner if (error)
94130f712c9SDave Chinner return error;
94230f712c9SDave Chinner *done = !i;
94330f712c9SDave Chinner if (i) {
94430f712c9SDave Chinner error = xfs_inobt_get_rec(cur, rec, &i);
94530f712c9SDave Chinner if (error)
94630f712c9SDave Chinner return error;
947f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(cur->bc_mp, i != 1))
948f9e03706SDarrick J. Wong return -EFSCORRUPTED;
94930f712c9SDave Chinner }
95030f712c9SDave Chinner
95130f712c9SDave Chinner return 0;
95230f712c9SDave Chinner }
95330f712c9SDave Chinner
95430f712c9SDave Chinner STATIC int
xfs_ialloc_get_rec(struct xfs_btree_cur * cur,xfs_agino_t agino,xfs_inobt_rec_incore_t * rec,int * done)95530f712c9SDave Chinner xfs_ialloc_get_rec(
95630f712c9SDave Chinner struct xfs_btree_cur *cur,
95730f712c9SDave Chinner xfs_agino_t agino,
95830f712c9SDave Chinner xfs_inobt_rec_incore_t *rec,
95930f712c9SDave Chinner int *done)
96030f712c9SDave Chinner {
96130f712c9SDave Chinner int error;
96230f712c9SDave Chinner int i;
96330f712c9SDave Chinner
96430f712c9SDave Chinner error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_EQ, &i);
96530f712c9SDave Chinner if (error)
96630f712c9SDave Chinner return error;
96730f712c9SDave Chinner *done = !i;
96830f712c9SDave Chinner if (i) {
96930f712c9SDave Chinner error = xfs_inobt_get_rec(cur, rec, &i);
97030f712c9SDave Chinner if (error)
97130f712c9SDave Chinner return error;
972f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(cur->bc_mp, i != 1))
973f9e03706SDarrick J. Wong return -EFSCORRUPTED;
97430f712c9SDave Chinner }
97530f712c9SDave Chinner
97630f712c9SDave Chinner return 0;
97730f712c9SDave Chinner }
97830f712c9SDave Chinner
97930f712c9SDave Chinner /*
98026dd5217SBrian Foster * Return the offset of the first free inode in the record. If the inode chunk
98126dd5217SBrian Foster * is sparsely allocated, we convert the record holemask to inode granularity
98226dd5217SBrian Foster * and mask off the unallocated regions from the inode free mask.
983d4cc540bSBrian Foster */
984d4cc540bSBrian Foster STATIC int
xfs_inobt_first_free_inode(struct xfs_inobt_rec_incore * rec)985d4cc540bSBrian Foster xfs_inobt_first_free_inode(
986d4cc540bSBrian Foster struct xfs_inobt_rec_incore *rec)
987d4cc540bSBrian Foster {
98826dd5217SBrian Foster xfs_inofree_t realfree;
98926dd5217SBrian Foster
99026dd5217SBrian Foster /* if there are no holes, return the first available offset */
99126dd5217SBrian Foster if (!xfs_inobt_issparse(rec->ir_holemask))
992d4cc540bSBrian Foster return xfs_lowbit64(rec->ir_free);
99326dd5217SBrian Foster
99426dd5217SBrian Foster realfree = xfs_inobt_irec_to_allocmask(rec);
99526dd5217SBrian Foster realfree &= rec->ir_free;
99626dd5217SBrian Foster
99726dd5217SBrian Foster return xfs_lowbit64(realfree);
998d4cc540bSBrian Foster }
999d4cc540bSBrian Foster
1000d4cc540bSBrian Foster /*
100130f712c9SDave Chinner * Allocate an inode using the inobt-only algorithm.
100230f712c9SDave Chinner */
100330f712c9SDave Chinner STATIC int
xfs_dialloc_ag_inobt(struct xfs_perag * pag,struct xfs_trans * tp,struct xfs_buf * agbp,xfs_ino_t parent,xfs_ino_t * inop)100430f712c9SDave Chinner xfs_dialloc_ag_inobt(
1005dedab3e4SDave Chinner struct xfs_perag *pag,
100630f712c9SDave Chinner struct xfs_trans *tp,
100730f712c9SDave Chinner struct xfs_buf *agbp,
100830f712c9SDave Chinner xfs_ino_t parent,
100930f712c9SDave Chinner xfs_ino_t *inop)
101030f712c9SDave Chinner {
101130f712c9SDave Chinner struct xfs_mount *mp = tp->t_mountp;
1012370c782bSChristoph Hellwig struct xfs_agi *agi = agbp->b_addr;
101330f712c9SDave Chinner xfs_agnumber_t pagno = XFS_INO_TO_AGNO(mp, parent);
101430f712c9SDave Chinner xfs_agino_t pagino = XFS_INO_TO_AGINO(mp, parent);
101530f712c9SDave Chinner struct xfs_btree_cur *cur, *tcur;
101630f712c9SDave Chinner struct xfs_inobt_rec_incore rec, trec;
101730f712c9SDave Chinner xfs_ino_t ino;
101830f712c9SDave Chinner int error;
101930f712c9SDave Chinner int offset;
102030f712c9SDave Chinner int i, j;
10212d32311cSCarlos Maiolino int searchdistance = 10;
102230f712c9SDave Chinner
10237ac2ff8bSDave Chinner ASSERT(xfs_perag_initialised_agi(pag));
10247ac2ff8bSDave Chinner ASSERT(xfs_perag_allows_inodes(pag));
102530f712c9SDave Chinner ASSERT(pag->pagi_freecount > 0);
102630f712c9SDave Chinner
102730f712c9SDave Chinner restart_pagno:
1028bab8b795SDave Chinner cur = xfs_inobt_init_cursor(pag, tp, agbp, XFS_BTNUM_INO);
102930f712c9SDave Chinner /*
103030f712c9SDave Chinner * If pagino is 0 (this is the root inode allocation) use newino.
103130f712c9SDave Chinner * This must work because we've just allocated some.
103230f712c9SDave Chinner */
103330f712c9SDave Chinner if (!pagino)
103430f712c9SDave Chinner pagino = be32_to_cpu(agi->agi_newino);
103530f712c9SDave Chinner
10369ba0889eSDave Chinner error = xfs_check_agi_freecount(cur);
103730f712c9SDave Chinner if (error)
103830f712c9SDave Chinner goto error0;
103930f712c9SDave Chinner
104030f712c9SDave Chinner /*
104130f712c9SDave Chinner * If in the same AG as the parent, try to get near the parent.
104230f712c9SDave Chinner */
10437b13c515SDave Chinner if (pagno == pag->pag_agno) {
104430f712c9SDave Chinner int doneleft; /* done, to the left */
104530f712c9SDave Chinner int doneright; /* done, to the right */
104630f712c9SDave Chinner
104730f712c9SDave Chinner error = xfs_inobt_lookup(cur, pagino, XFS_LOOKUP_LE, &i);
104830f712c9SDave Chinner if (error)
104930f712c9SDave Chinner goto error0;
1050f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(mp, i != 1)) {
1051f9e03706SDarrick J. Wong error = -EFSCORRUPTED;
1052f9e03706SDarrick J. Wong goto error0;
1053f9e03706SDarrick J. Wong }
105430f712c9SDave Chinner
105530f712c9SDave Chinner error = xfs_inobt_get_rec(cur, &rec, &j);
105630f712c9SDave Chinner if (error)
105730f712c9SDave Chinner goto error0;
1058f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(mp, j != 1)) {
1059f9e03706SDarrick J. Wong error = -EFSCORRUPTED;
1060f9e03706SDarrick J. Wong goto error0;
1061f9e03706SDarrick J. Wong }
106230f712c9SDave Chinner
106330f712c9SDave Chinner if (rec.ir_freecount > 0) {
106430f712c9SDave Chinner /*
106530f712c9SDave Chinner * Found a free inode in the same chunk
106630f712c9SDave Chinner * as the parent, done.
106730f712c9SDave Chinner */
106830f712c9SDave Chinner goto alloc_inode;
106930f712c9SDave Chinner }
107030f712c9SDave Chinner
107130f712c9SDave Chinner
107230f712c9SDave Chinner /*
107330f712c9SDave Chinner * In the same AG as parent, but parent's chunk is full.
107430f712c9SDave Chinner */
107530f712c9SDave Chinner
107630f712c9SDave Chinner /* duplicate the cursor, search left & right simultaneously */
107730f712c9SDave Chinner error = xfs_btree_dup_cursor(cur, &tcur);
107830f712c9SDave Chinner if (error)
107930f712c9SDave Chinner goto error0;
108030f712c9SDave Chinner
108130f712c9SDave Chinner /*
108230f712c9SDave Chinner * Skip to last blocks looked up if same parent inode.
108330f712c9SDave Chinner */
108430f712c9SDave Chinner if (pagino != NULLAGINO &&
108530f712c9SDave Chinner pag->pagl_pagino == pagino &&
108630f712c9SDave Chinner pag->pagl_leftrec != NULLAGINO &&
108730f712c9SDave Chinner pag->pagl_rightrec != NULLAGINO) {
108830f712c9SDave Chinner error = xfs_ialloc_get_rec(tcur, pag->pagl_leftrec,
108930f712c9SDave Chinner &trec, &doneleft);
109030f712c9SDave Chinner if (error)
109130f712c9SDave Chinner goto error1;
109230f712c9SDave Chinner
109330f712c9SDave Chinner error = xfs_ialloc_get_rec(cur, pag->pagl_rightrec,
109430f712c9SDave Chinner &rec, &doneright);
109530f712c9SDave Chinner if (error)
109630f712c9SDave Chinner goto error1;
109730f712c9SDave Chinner } else {
109830f712c9SDave Chinner /* search left with tcur, back up 1 record */
109930f712c9SDave Chinner error = xfs_ialloc_next_rec(tcur, &trec, &doneleft, 1);
110030f712c9SDave Chinner if (error)
110130f712c9SDave Chinner goto error1;
110230f712c9SDave Chinner
110330f712c9SDave Chinner /* search right with cur, go forward 1 record. */
110430f712c9SDave Chinner error = xfs_ialloc_next_rec(cur, &rec, &doneright, 0);
110530f712c9SDave Chinner if (error)
110630f712c9SDave Chinner goto error1;
110730f712c9SDave Chinner }
110830f712c9SDave Chinner
110930f712c9SDave Chinner /*
111030f712c9SDave Chinner * Loop until we find an inode chunk with a free inode.
111130f712c9SDave Chinner */
11122d32311cSCarlos Maiolino while (--searchdistance > 0 && (!doneleft || !doneright)) {
111330f712c9SDave Chinner int useleft; /* using left inode chunk this time */
111430f712c9SDave Chinner
111530f712c9SDave Chinner /* figure out the closer block if both are valid. */
111630f712c9SDave Chinner if (!doneleft && !doneright) {
111730f712c9SDave Chinner useleft = pagino -
111830f712c9SDave Chinner (trec.ir_startino + XFS_INODES_PER_CHUNK - 1) <
111930f712c9SDave Chinner rec.ir_startino - pagino;
112030f712c9SDave Chinner } else {
112130f712c9SDave Chinner useleft = !doneleft;
112230f712c9SDave Chinner }
112330f712c9SDave Chinner
112430f712c9SDave Chinner /* free inodes to the left? */
112530f712c9SDave Chinner if (useleft && trec.ir_freecount) {
112630f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
112730f712c9SDave Chinner cur = tcur;
112830f712c9SDave Chinner
112930f712c9SDave Chinner pag->pagl_leftrec = trec.ir_startino;
113030f712c9SDave Chinner pag->pagl_rightrec = rec.ir_startino;
113130f712c9SDave Chinner pag->pagl_pagino = pagino;
1132c44245b3SOmar Sandoval rec = trec;
113330f712c9SDave Chinner goto alloc_inode;
113430f712c9SDave Chinner }
113530f712c9SDave Chinner
113630f712c9SDave Chinner /* free inodes to the right? */
113730f712c9SDave Chinner if (!useleft && rec.ir_freecount) {
113830f712c9SDave Chinner xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
113930f712c9SDave Chinner
114030f712c9SDave Chinner pag->pagl_leftrec = trec.ir_startino;
114130f712c9SDave Chinner pag->pagl_rightrec = rec.ir_startino;
114230f712c9SDave Chinner pag->pagl_pagino = pagino;
114330f712c9SDave Chinner goto alloc_inode;
114430f712c9SDave Chinner }
114530f712c9SDave Chinner
114630f712c9SDave Chinner /* get next record to check */
114730f712c9SDave Chinner if (useleft) {
114830f712c9SDave Chinner error = xfs_ialloc_next_rec(tcur, &trec,
114930f712c9SDave Chinner &doneleft, 1);
115030f712c9SDave Chinner } else {
115130f712c9SDave Chinner error = xfs_ialloc_next_rec(cur, &rec,
115230f712c9SDave Chinner &doneright, 0);
115330f712c9SDave Chinner }
115430f712c9SDave Chinner if (error)
115530f712c9SDave Chinner goto error1;
115630f712c9SDave Chinner }
115730f712c9SDave Chinner
11582d32311cSCarlos Maiolino if (searchdistance <= 0) {
11592d32311cSCarlos Maiolino /*
11602d32311cSCarlos Maiolino * Not in range - save last search
11612d32311cSCarlos Maiolino * location and allocate a new inode
11622d32311cSCarlos Maiolino */
11632d32311cSCarlos Maiolino xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
11642d32311cSCarlos Maiolino pag->pagl_leftrec = trec.ir_startino;
11652d32311cSCarlos Maiolino pag->pagl_rightrec = rec.ir_startino;
11662d32311cSCarlos Maiolino pag->pagl_pagino = pagino;
11672d32311cSCarlos Maiolino
11682d32311cSCarlos Maiolino } else {
116930f712c9SDave Chinner /*
117030f712c9SDave Chinner * We've reached the end of the btree. because
117130f712c9SDave Chinner * we are only searching a small chunk of the
117230f712c9SDave Chinner * btree each search, there is obviously free
117330f712c9SDave Chinner * inodes closer to the parent inode than we
117430f712c9SDave Chinner * are now. restart the search again.
117530f712c9SDave Chinner */
117630f712c9SDave Chinner pag->pagl_pagino = NULLAGINO;
117730f712c9SDave Chinner pag->pagl_leftrec = NULLAGINO;
117830f712c9SDave Chinner pag->pagl_rightrec = NULLAGINO;
117930f712c9SDave Chinner xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
118030f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
118130f712c9SDave Chinner goto restart_pagno;
118230f712c9SDave Chinner }
11832d32311cSCarlos Maiolino }
118430f712c9SDave Chinner
118530f712c9SDave Chinner /*
118630f712c9SDave Chinner * In a different AG from the parent.
118730f712c9SDave Chinner * See if the most recently allocated block has any free.
118830f712c9SDave Chinner */
118930f712c9SDave Chinner if (agi->agi_newino != cpu_to_be32(NULLAGINO)) {
119030f712c9SDave Chinner error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino),
119130f712c9SDave Chinner XFS_LOOKUP_EQ, &i);
119230f712c9SDave Chinner if (error)
119330f712c9SDave Chinner goto error0;
119430f712c9SDave Chinner
119530f712c9SDave Chinner if (i == 1) {
119630f712c9SDave Chinner error = xfs_inobt_get_rec(cur, &rec, &j);
119730f712c9SDave Chinner if (error)
119830f712c9SDave Chinner goto error0;
119930f712c9SDave Chinner
120030f712c9SDave Chinner if (j == 1 && rec.ir_freecount > 0) {
120130f712c9SDave Chinner /*
120230f712c9SDave Chinner * The last chunk allocated in the group
120330f712c9SDave Chinner * still has a free inode.
120430f712c9SDave Chinner */
120530f712c9SDave Chinner goto alloc_inode;
120630f712c9SDave Chinner }
120730f712c9SDave Chinner }
120830f712c9SDave Chinner }
120930f712c9SDave Chinner
121030f712c9SDave Chinner /*
121130f712c9SDave Chinner * None left in the last group, search the whole AG
121230f712c9SDave Chinner */
121330f712c9SDave Chinner error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
121430f712c9SDave Chinner if (error)
121530f712c9SDave Chinner goto error0;
1216f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(mp, i != 1)) {
1217f9e03706SDarrick J. Wong error = -EFSCORRUPTED;
1218f9e03706SDarrick J. Wong goto error0;
1219f9e03706SDarrick J. Wong }
122030f712c9SDave Chinner
122130f712c9SDave Chinner for (;;) {
122230f712c9SDave Chinner error = xfs_inobt_get_rec(cur, &rec, &i);
122330f712c9SDave Chinner if (error)
122430f712c9SDave Chinner goto error0;
1225f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(mp, i != 1)) {
1226f9e03706SDarrick J. Wong error = -EFSCORRUPTED;
1227f9e03706SDarrick J. Wong goto error0;
1228f9e03706SDarrick J. Wong }
122930f712c9SDave Chinner if (rec.ir_freecount > 0)
123030f712c9SDave Chinner break;
123130f712c9SDave Chinner error = xfs_btree_increment(cur, 0, &i);
123230f712c9SDave Chinner if (error)
123330f712c9SDave Chinner goto error0;
1234f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(mp, i != 1)) {
1235f9e03706SDarrick J. Wong error = -EFSCORRUPTED;
1236f9e03706SDarrick J. Wong goto error0;
1237f9e03706SDarrick J. Wong }
123830f712c9SDave Chinner }
123930f712c9SDave Chinner
124030f712c9SDave Chinner alloc_inode:
1241d4cc540bSBrian Foster offset = xfs_inobt_first_free_inode(&rec);
124230f712c9SDave Chinner ASSERT(offset >= 0);
124330f712c9SDave Chinner ASSERT(offset < XFS_INODES_PER_CHUNK);
124430f712c9SDave Chinner ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
124530f712c9SDave Chinner XFS_INODES_PER_CHUNK) == 0);
12467b13c515SDave Chinner ino = XFS_AGINO_TO_INO(mp, pag->pag_agno, rec.ir_startino + offset);
124730f712c9SDave Chinner rec.ir_free &= ~XFS_INOBT_MASK(offset);
124830f712c9SDave Chinner rec.ir_freecount--;
124930f712c9SDave Chinner error = xfs_inobt_update(cur, &rec);
125030f712c9SDave Chinner if (error)
125130f712c9SDave Chinner goto error0;
125230f712c9SDave Chinner be32_add_cpu(&agi->agi_freecount, -1);
125330f712c9SDave Chinner xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
125430f712c9SDave Chinner pag->pagi_freecount--;
125530f712c9SDave Chinner
12569ba0889eSDave Chinner error = xfs_check_agi_freecount(cur);
125730f712c9SDave Chinner if (error)
125830f712c9SDave Chinner goto error0;
125930f712c9SDave Chinner
126030f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
126130f712c9SDave Chinner xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
126230f712c9SDave Chinner *inop = ino;
126330f712c9SDave Chinner return 0;
126430f712c9SDave Chinner error1:
126530f712c9SDave Chinner xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
126630f712c9SDave Chinner error0:
126730f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
126830f712c9SDave Chinner return error;
126930f712c9SDave Chinner }
127030f712c9SDave Chinner
127130f712c9SDave Chinner /*
127230f712c9SDave Chinner * Use the free inode btree to allocate an inode based on distance from the
127330f712c9SDave Chinner * parent. Note that the provided cursor may be deleted and replaced.
127430f712c9SDave Chinner */
127530f712c9SDave Chinner STATIC int
xfs_dialloc_ag_finobt_near(xfs_agino_t pagino,struct xfs_btree_cur ** ocur,struct xfs_inobt_rec_incore * rec)127630f712c9SDave Chinner xfs_dialloc_ag_finobt_near(
127730f712c9SDave Chinner xfs_agino_t pagino,
127830f712c9SDave Chinner struct xfs_btree_cur **ocur,
127930f712c9SDave Chinner struct xfs_inobt_rec_incore *rec)
128030f712c9SDave Chinner {
128130f712c9SDave Chinner struct xfs_btree_cur *lcur = *ocur; /* left search cursor */
128230f712c9SDave Chinner struct xfs_btree_cur *rcur; /* right search cursor */
128330f712c9SDave Chinner struct xfs_inobt_rec_incore rrec;
128430f712c9SDave Chinner int error;
128530f712c9SDave Chinner int i, j;
128630f712c9SDave Chinner
128730f712c9SDave Chinner error = xfs_inobt_lookup(lcur, pagino, XFS_LOOKUP_LE, &i);
128830f712c9SDave Chinner if (error)
128930f712c9SDave Chinner return error;
129030f712c9SDave Chinner
129130f712c9SDave Chinner if (i == 1) {
129230f712c9SDave Chinner error = xfs_inobt_get_rec(lcur, rec, &i);
129330f712c9SDave Chinner if (error)
129430f712c9SDave Chinner return error;
1295f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(lcur->bc_mp, i != 1))
1296f9e03706SDarrick J. Wong return -EFSCORRUPTED;
129730f712c9SDave Chinner
129830f712c9SDave Chinner /*
129930f712c9SDave Chinner * See if we've landed in the parent inode record. The finobt
130030f712c9SDave Chinner * only tracks chunks with at least one free inode, so record
130130f712c9SDave Chinner * existence is enough.
130230f712c9SDave Chinner */
130330f712c9SDave Chinner if (pagino >= rec->ir_startino &&
130430f712c9SDave Chinner pagino < (rec->ir_startino + XFS_INODES_PER_CHUNK))
130530f712c9SDave Chinner return 0;
130630f712c9SDave Chinner }
130730f712c9SDave Chinner
130830f712c9SDave Chinner error = xfs_btree_dup_cursor(lcur, &rcur);
130930f712c9SDave Chinner if (error)
131030f712c9SDave Chinner return error;
131130f712c9SDave Chinner
131230f712c9SDave Chinner error = xfs_inobt_lookup(rcur, pagino, XFS_LOOKUP_GE, &j);
131330f712c9SDave Chinner if (error)
131430f712c9SDave Chinner goto error_rcur;
131530f712c9SDave Chinner if (j == 1) {
131630f712c9SDave Chinner error = xfs_inobt_get_rec(rcur, &rrec, &j);
131730f712c9SDave Chinner if (error)
131830f712c9SDave Chinner goto error_rcur;
1319f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(lcur->bc_mp, j != 1)) {
1320f9e03706SDarrick J. Wong error = -EFSCORRUPTED;
1321f9e03706SDarrick J. Wong goto error_rcur;
1322f9e03706SDarrick J. Wong }
132330f712c9SDave Chinner }
132430f712c9SDave Chinner
1325f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(lcur->bc_mp, i != 1 && j != 1)) {
1326f9e03706SDarrick J. Wong error = -EFSCORRUPTED;
1327f9e03706SDarrick J. Wong goto error_rcur;
1328f9e03706SDarrick J. Wong }
132930f712c9SDave Chinner if (i == 1 && j == 1) {
133030f712c9SDave Chinner /*
133130f712c9SDave Chinner * Both the left and right records are valid. Choose the closer
133230f712c9SDave Chinner * inode chunk to the target.
133330f712c9SDave Chinner */
133430f712c9SDave Chinner if ((pagino - rec->ir_startino + XFS_INODES_PER_CHUNK - 1) >
133530f712c9SDave Chinner (rrec.ir_startino - pagino)) {
133630f712c9SDave Chinner *rec = rrec;
133730f712c9SDave Chinner xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR);
133830f712c9SDave Chinner *ocur = rcur;
133930f712c9SDave Chinner } else {
134030f712c9SDave Chinner xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR);
134130f712c9SDave Chinner }
134230f712c9SDave Chinner } else if (j == 1) {
134330f712c9SDave Chinner /* only the right record is valid */
134430f712c9SDave Chinner *rec = rrec;
134530f712c9SDave Chinner xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR);
134630f712c9SDave Chinner *ocur = rcur;
134730f712c9SDave Chinner } else if (i == 1) {
134830f712c9SDave Chinner /* only the left record is valid */
134930f712c9SDave Chinner xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR);
135030f712c9SDave Chinner }
135130f712c9SDave Chinner
135230f712c9SDave Chinner return 0;
135330f712c9SDave Chinner
135430f712c9SDave Chinner error_rcur:
135530f712c9SDave Chinner xfs_btree_del_cursor(rcur, XFS_BTREE_ERROR);
135630f712c9SDave Chinner return error;
135730f712c9SDave Chinner }
135830f712c9SDave Chinner
135930f712c9SDave Chinner /*
136030f712c9SDave Chinner * Use the free inode btree to find a free inode based on a newino hint. If
136130f712c9SDave Chinner * the hint is NULL, find the first free inode in the AG.
136230f712c9SDave Chinner */
136330f712c9SDave Chinner STATIC int
xfs_dialloc_ag_finobt_newino(struct xfs_agi * agi,struct xfs_btree_cur * cur,struct xfs_inobt_rec_incore * rec)136430f712c9SDave Chinner xfs_dialloc_ag_finobt_newino(
136530f712c9SDave Chinner struct xfs_agi *agi,
136630f712c9SDave Chinner struct xfs_btree_cur *cur,
136730f712c9SDave Chinner struct xfs_inobt_rec_incore *rec)
136830f712c9SDave Chinner {
136930f712c9SDave Chinner int error;
137030f712c9SDave Chinner int i;
137130f712c9SDave Chinner
137230f712c9SDave Chinner if (agi->agi_newino != cpu_to_be32(NULLAGINO)) {
1373e68ed775SDave Chinner error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino),
1374e68ed775SDave Chinner XFS_LOOKUP_EQ, &i);
137530f712c9SDave Chinner if (error)
137630f712c9SDave Chinner return error;
137730f712c9SDave Chinner if (i == 1) {
137830f712c9SDave Chinner error = xfs_inobt_get_rec(cur, rec, &i);
137930f712c9SDave Chinner if (error)
138030f712c9SDave Chinner return error;
1381f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(cur->bc_mp, i != 1))
1382f9e03706SDarrick J. Wong return -EFSCORRUPTED;
138330f712c9SDave Chinner return 0;
138430f712c9SDave Chinner }
138530f712c9SDave Chinner }
138630f712c9SDave Chinner
138730f712c9SDave Chinner /*
138830f712c9SDave Chinner * Find the first inode available in the AG.
138930f712c9SDave Chinner */
139030f712c9SDave Chinner error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
139130f712c9SDave Chinner if (error)
139230f712c9SDave Chinner return error;
1393f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(cur->bc_mp, i != 1))
1394f9e03706SDarrick J. Wong return -EFSCORRUPTED;
139530f712c9SDave Chinner
139630f712c9SDave Chinner error = xfs_inobt_get_rec(cur, rec, &i);
139730f712c9SDave Chinner if (error)
139830f712c9SDave Chinner return error;
1399f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(cur->bc_mp, i != 1))
1400f9e03706SDarrick J. Wong return -EFSCORRUPTED;
140130f712c9SDave Chinner
140230f712c9SDave Chinner return 0;
140330f712c9SDave Chinner }
140430f712c9SDave Chinner
140530f712c9SDave Chinner /*
140630f712c9SDave Chinner * Update the inobt based on a modification made to the finobt. Also ensure that
140730f712c9SDave Chinner * the records from both trees are equivalent post-modification.
140830f712c9SDave Chinner */
140930f712c9SDave Chinner STATIC int
xfs_dialloc_ag_update_inobt(struct xfs_btree_cur * cur,struct xfs_inobt_rec_incore * frec,int offset)141030f712c9SDave Chinner xfs_dialloc_ag_update_inobt(
141130f712c9SDave Chinner struct xfs_btree_cur *cur, /* inobt cursor */
141230f712c9SDave Chinner struct xfs_inobt_rec_incore *frec, /* finobt record */
141330f712c9SDave Chinner int offset) /* inode offset */
141430f712c9SDave Chinner {
141530f712c9SDave Chinner struct xfs_inobt_rec_incore rec;
141630f712c9SDave Chinner int error;
141730f712c9SDave Chinner int i;
141830f712c9SDave Chinner
141930f712c9SDave Chinner error = xfs_inobt_lookup(cur, frec->ir_startino, XFS_LOOKUP_EQ, &i);
142030f712c9SDave Chinner if (error)
142130f712c9SDave Chinner return error;
1422f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(cur->bc_mp, i != 1))
1423f9e03706SDarrick J. Wong return -EFSCORRUPTED;
142430f712c9SDave Chinner
142530f712c9SDave Chinner error = xfs_inobt_get_rec(cur, &rec, &i);
142630f712c9SDave Chinner if (error)
142730f712c9SDave Chinner return error;
1428f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(cur->bc_mp, i != 1))
1429f9e03706SDarrick J. Wong return -EFSCORRUPTED;
143030f712c9SDave Chinner ASSERT((XFS_AGINO_TO_OFFSET(cur->bc_mp, rec.ir_startino) %
143130f712c9SDave Chinner XFS_INODES_PER_CHUNK) == 0);
143230f712c9SDave Chinner
143330f712c9SDave Chinner rec.ir_free &= ~XFS_INOBT_MASK(offset);
143430f712c9SDave Chinner rec.ir_freecount--;
143530f712c9SDave Chinner
1436f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(cur->bc_mp,
1437f9e03706SDarrick J. Wong rec.ir_free != frec->ir_free ||
1438f9e03706SDarrick J. Wong rec.ir_freecount != frec->ir_freecount))
1439f9e03706SDarrick J. Wong return -EFSCORRUPTED;
144030f712c9SDave Chinner
1441b72091f2Skbuild test robot return xfs_inobt_update(cur, &rec);
144230f712c9SDave Chinner }
144330f712c9SDave Chinner
144430f712c9SDave Chinner /*
144530f712c9SDave Chinner * Allocate an inode using the free inode btree, if available. Otherwise, fall
144630f712c9SDave Chinner * back to the inobt search algorithm.
144730f712c9SDave Chinner *
144830f712c9SDave Chinner * The caller selected an AG for us, and made sure that free inodes are
144930f712c9SDave Chinner * available.
145030f712c9SDave Chinner */
1451b652afd9SDave Chinner static int
xfs_dialloc_ag(struct xfs_perag * pag,struct xfs_trans * tp,struct xfs_buf * agbp,xfs_ino_t parent,xfs_ino_t * inop)145230f712c9SDave Chinner xfs_dialloc_ag(
1453dedab3e4SDave Chinner struct xfs_perag *pag,
145430f712c9SDave Chinner struct xfs_trans *tp,
145530f712c9SDave Chinner struct xfs_buf *agbp,
145630f712c9SDave Chinner xfs_ino_t parent,
145730f712c9SDave Chinner xfs_ino_t *inop)
145830f712c9SDave Chinner {
145930f712c9SDave Chinner struct xfs_mount *mp = tp->t_mountp;
1460370c782bSChristoph Hellwig struct xfs_agi *agi = agbp->b_addr;
146130f712c9SDave Chinner xfs_agnumber_t pagno = XFS_INO_TO_AGNO(mp, parent);
146230f712c9SDave Chinner xfs_agino_t pagino = XFS_INO_TO_AGINO(mp, parent);
146330f712c9SDave Chinner struct xfs_btree_cur *cur; /* finobt cursor */
146430f712c9SDave Chinner struct xfs_btree_cur *icur; /* inobt cursor */
146530f712c9SDave Chinner struct xfs_inobt_rec_incore rec;
146630f712c9SDave Chinner xfs_ino_t ino;
146730f712c9SDave Chinner int error;
146830f712c9SDave Chinner int offset;
146930f712c9SDave Chinner int i;
147030f712c9SDave Chinner
1471ebd9027dSDave Chinner if (!xfs_has_finobt(mp))
1472dedab3e4SDave Chinner return xfs_dialloc_ag_inobt(pag, tp, agbp, parent, inop);
147330f712c9SDave Chinner
147430f712c9SDave Chinner /*
147530f712c9SDave Chinner * If pagino is 0 (this is the root inode allocation) use newino.
147630f712c9SDave Chinner * This must work because we've just allocated some.
147730f712c9SDave Chinner */
147830f712c9SDave Chinner if (!pagino)
147930f712c9SDave Chinner pagino = be32_to_cpu(agi->agi_newino);
148030f712c9SDave Chinner
1481bab8b795SDave Chinner cur = xfs_inobt_init_cursor(pag, tp, agbp, XFS_BTNUM_FINO);
148230f712c9SDave Chinner
14839ba0889eSDave Chinner error = xfs_check_agi_freecount(cur);
148430f712c9SDave Chinner if (error)
148530f712c9SDave Chinner goto error_cur;
148630f712c9SDave Chinner
148730f712c9SDave Chinner /*
148830f712c9SDave Chinner * The search algorithm depends on whether we're in the same AG as the
148930f712c9SDave Chinner * parent. If so, find the closest available inode to the parent. If
149030f712c9SDave Chinner * not, consider the agi hint or find the first free inode in the AG.
149130f712c9SDave Chinner */
14927b13c515SDave Chinner if (pag->pag_agno == pagno)
149330f712c9SDave Chinner error = xfs_dialloc_ag_finobt_near(pagino, &cur, &rec);
149430f712c9SDave Chinner else
149530f712c9SDave Chinner error = xfs_dialloc_ag_finobt_newino(agi, cur, &rec);
149630f712c9SDave Chinner if (error)
149730f712c9SDave Chinner goto error_cur;
149830f712c9SDave Chinner
1499d4cc540bSBrian Foster offset = xfs_inobt_first_free_inode(&rec);
150030f712c9SDave Chinner ASSERT(offset >= 0);
150130f712c9SDave Chinner ASSERT(offset < XFS_INODES_PER_CHUNK);
150230f712c9SDave Chinner ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
150330f712c9SDave Chinner XFS_INODES_PER_CHUNK) == 0);
15047b13c515SDave Chinner ino = XFS_AGINO_TO_INO(mp, pag->pag_agno, rec.ir_startino + offset);
150530f712c9SDave Chinner
150630f712c9SDave Chinner /*
150730f712c9SDave Chinner * Modify or remove the finobt record.
150830f712c9SDave Chinner */
150930f712c9SDave Chinner rec.ir_free &= ~XFS_INOBT_MASK(offset);
151030f712c9SDave Chinner rec.ir_freecount--;
151130f712c9SDave Chinner if (rec.ir_freecount)
151230f712c9SDave Chinner error = xfs_inobt_update(cur, &rec);
151330f712c9SDave Chinner else
151430f712c9SDave Chinner error = xfs_btree_delete(cur, &i);
151530f712c9SDave Chinner if (error)
151630f712c9SDave Chinner goto error_cur;
151730f712c9SDave Chinner
151830f712c9SDave Chinner /*
151930f712c9SDave Chinner * The finobt has now been updated appropriately. We haven't updated the
152030f712c9SDave Chinner * agi and superblock yet, so we can create an inobt cursor and validate
152130f712c9SDave Chinner * the original freecount. If all is well, make the equivalent update to
152230f712c9SDave Chinner * the inobt using the finobt record and offset information.
152330f712c9SDave Chinner */
1524bab8b795SDave Chinner icur = xfs_inobt_init_cursor(pag, tp, agbp, XFS_BTNUM_INO);
152530f712c9SDave Chinner
15269ba0889eSDave Chinner error = xfs_check_agi_freecount(icur);
152730f712c9SDave Chinner if (error)
152830f712c9SDave Chinner goto error_icur;
152930f712c9SDave Chinner
153030f712c9SDave Chinner error = xfs_dialloc_ag_update_inobt(icur, &rec, offset);
153130f712c9SDave Chinner if (error)
153230f712c9SDave Chinner goto error_icur;
153330f712c9SDave Chinner
153430f712c9SDave Chinner /*
153530f712c9SDave Chinner * Both trees have now been updated. We must update the perag and
153630f712c9SDave Chinner * superblock before we can check the freecount for each btree.
153730f712c9SDave Chinner */
153830f712c9SDave Chinner be32_add_cpu(&agi->agi_freecount, -1);
153930f712c9SDave Chinner xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
15407b13c515SDave Chinner pag->pagi_freecount--;
154130f712c9SDave Chinner
154230f712c9SDave Chinner xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
154330f712c9SDave Chinner
15449ba0889eSDave Chinner error = xfs_check_agi_freecount(icur);
154530f712c9SDave Chinner if (error)
154630f712c9SDave Chinner goto error_icur;
15479ba0889eSDave Chinner error = xfs_check_agi_freecount(cur);
154830f712c9SDave Chinner if (error)
154930f712c9SDave Chinner goto error_icur;
155030f712c9SDave Chinner
155130f712c9SDave Chinner xfs_btree_del_cursor(icur, XFS_BTREE_NOERROR);
155230f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
155330f712c9SDave Chinner *inop = ino;
155430f712c9SDave Chinner return 0;
155530f712c9SDave Chinner
155630f712c9SDave Chinner error_icur:
155730f712c9SDave Chinner xfs_btree_del_cursor(icur, XFS_BTREE_ERROR);
155830f712c9SDave Chinner error_cur:
155930f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
156030f712c9SDave Chinner return error;
156130f712c9SDave Chinner }
156230f712c9SDave Chinner
1563f3bf6e0fSDave Chinner static int
xfs_dialloc_roll(struct xfs_trans ** tpp,struct xfs_buf * agibp)1564aececc9fSDave Chinner xfs_dialloc_roll(
1565aececc9fSDave Chinner struct xfs_trans **tpp,
1566aececc9fSDave Chinner struct xfs_buf *agibp)
1567aececc9fSDave Chinner {
1568aececc9fSDave Chinner struct xfs_trans *tp = *tpp;
1569aececc9fSDave Chinner struct xfs_dquot_acct *dqinfo;
1570aececc9fSDave Chinner int error;
1571aececc9fSDave Chinner
1572aececc9fSDave Chinner /*
1573aececc9fSDave Chinner * Hold to on to the agibp across the commit so no other allocation can
1574aececc9fSDave Chinner * come in and take the free inodes we just allocated for our caller.
1575aececc9fSDave Chinner */
1576aececc9fSDave Chinner xfs_trans_bhold(tp, agibp);
1577aececc9fSDave Chinner
1578aececc9fSDave Chinner /*
1579aececc9fSDave Chinner * We want the quota changes to be associated with the next transaction,
1580aececc9fSDave Chinner * NOT this one. So, detach the dqinfo from this and attach it to the
1581aececc9fSDave Chinner * next transaction.
1582aececc9fSDave Chinner */
1583aececc9fSDave Chinner dqinfo = tp->t_dqinfo;
1584aececc9fSDave Chinner tp->t_dqinfo = NULL;
1585aececc9fSDave Chinner
1586aececc9fSDave Chinner error = xfs_trans_roll(&tp);
1587aececc9fSDave Chinner
1588aececc9fSDave Chinner /* Re-attach the quota info that we detached from prev trx. */
1589aececc9fSDave Chinner tp->t_dqinfo = dqinfo;
1590aececc9fSDave Chinner
15918237fbf5SDave Chinner /*
15928237fbf5SDave Chinner * Join the buffer even on commit error so that the buffer is released
15938237fbf5SDave Chinner * when the caller cancels the transaction and doesn't have to handle
15948237fbf5SDave Chinner * this error case specially.
15958237fbf5SDave Chinner */
1596aececc9fSDave Chinner xfs_trans_bjoin(tp, agibp);
15978237fbf5SDave Chinner *tpp = tp;
15988237fbf5SDave Chinner return error;
1599aececc9fSDave Chinner }
1600aececc9fSDave Chinner
16018237fbf5SDave Chinner static bool
xfs_dialloc_good_ag(struct xfs_perag * pag,struct xfs_trans * tp,umode_t mode,int flags,bool ok_alloc)16028237fbf5SDave Chinner xfs_dialloc_good_ag(
16038237fbf5SDave Chinner struct xfs_perag *pag,
1604dedab3e4SDave Chinner struct xfs_trans *tp,
16058237fbf5SDave Chinner umode_t mode,
16068237fbf5SDave Chinner int flags,
16078237fbf5SDave Chinner bool ok_alloc)
16088237fbf5SDave Chinner {
16098237fbf5SDave Chinner struct xfs_mount *mp = tp->t_mountp;
16108237fbf5SDave Chinner xfs_extlen_t ineed;
16118237fbf5SDave Chinner xfs_extlen_t longest = 0;
16128237fbf5SDave Chinner int needspace;
16138237fbf5SDave Chinner int error;
16148237fbf5SDave Chinner
1615dedab3e4SDave Chinner if (!pag)
1616dedab3e4SDave Chinner return false;
16177ac2ff8bSDave Chinner if (!xfs_perag_allows_inodes(pag))
16188237fbf5SDave Chinner return false;
16198237fbf5SDave Chinner
16207ac2ff8bSDave Chinner if (!xfs_perag_initialised_agi(pag)) {
162199b13c7fSDave Chinner error = xfs_ialloc_read_agi(pag, tp, NULL);
16228237fbf5SDave Chinner if (error)
16238237fbf5SDave Chinner return false;
16248237fbf5SDave Chinner }
16258237fbf5SDave Chinner
16268237fbf5SDave Chinner if (pag->pagi_freecount)
16278237fbf5SDave Chinner return true;
16288237fbf5SDave Chinner if (!ok_alloc)
16298237fbf5SDave Chinner return false;
16308237fbf5SDave Chinner
16317ac2ff8bSDave Chinner if (!xfs_perag_initialised_agf(pag)) {
163208d3e84fSDave Chinner error = xfs_alloc_read_agf(pag, tp, flags, NULL);
16338237fbf5SDave Chinner if (error)
16348237fbf5SDave Chinner return false;
16358237fbf5SDave Chinner }
16368237fbf5SDave Chinner
163730f712c9SDave Chinner /*
16388237fbf5SDave Chinner * Check that there is enough free space for the file plus a chunk of
16398237fbf5SDave Chinner * inodes if we need to allocate some. If this is the first pass across
16408237fbf5SDave Chinner * the AGs, take into account the potential space needed for alignment
16418237fbf5SDave Chinner * of inode chunks when checking the longest contiguous free space in
16428237fbf5SDave Chinner * the AG - this prevents us from getting ENOSPC because we have free
16438237fbf5SDave Chinner * space larger than ialloc_blks but alignment constraints prevent us
16448237fbf5SDave Chinner * from using it.
16458237fbf5SDave Chinner *
16468237fbf5SDave Chinner * If we can't find an AG with space for full alignment slack to be
16478237fbf5SDave Chinner * taken into account, we must be near ENOSPC in all AGs. Hence we
16488237fbf5SDave Chinner * don't include alignment for the second pass and so if we fail
16498237fbf5SDave Chinner * allocation due to alignment issues then it is most likely a real
16508237fbf5SDave Chinner * ENOSPC condition.
16518237fbf5SDave Chinner *
16528237fbf5SDave Chinner * XXX(dgc): this calculation is now bogus thanks to the per-ag
16538237fbf5SDave Chinner * reservations that xfs_alloc_fix_freelist() now does via
16548237fbf5SDave Chinner * xfs_alloc_space_available(). When the AG fills up, pagf_freeblks will
16558237fbf5SDave Chinner * be more than large enough for the check below to succeed, but
16568237fbf5SDave Chinner * xfs_alloc_space_available() will fail because of the non-zero
16578237fbf5SDave Chinner * metadata reservation and hence we won't actually be able to allocate
16588237fbf5SDave Chinner * more inodes in this AG. We do soooo much unnecessary work near ENOSPC
16598237fbf5SDave Chinner * because of this.
16608237fbf5SDave Chinner */
16618237fbf5SDave Chinner ineed = M_IGEO(mp)->ialloc_min_blks;
16628237fbf5SDave Chinner if (flags && ineed > 1)
16638237fbf5SDave Chinner ineed += M_IGEO(mp)->cluster_align;
16648237fbf5SDave Chinner longest = pag->pagf_longest;
16658237fbf5SDave Chinner if (!longest)
16668237fbf5SDave Chinner longest = pag->pagf_flcount > 0;
16678237fbf5SDave Chinner needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode);
16688237fbf5SDave Chinner
16698237fbf5SDave Chinner if (pag->pagf_freeblks < needspace + ineed || longest < ineed)
16708237fbf5SDave Chinner return false;
16718237fbf5SDave Chinner return true;
16728237fbf5SDave Chinner }
16738237fbf5SDave Chinner
16748237fbf5SDave Chinner static int
xfs_dialloc_try_ag(struct xfs_perag * pag,struct xfs_trans ** tpp,xfs_ino_t parent,xfs_ino_t * new_ino,bool ok_alloc)16758237fbf5SDave Chinner xfs_dialloc_try_ag(
16768237fbf5SDave Chinner struct xfs_perag *pag,
1677dedab3e4SDave Chinner struct xfs_trans **tpp,
16788237fbf5SDave Chinner xfs_ino_t parent,
16798237fbf5SDave Chinner xfs_ino_t *new_ino,
16808237fbf5SDave Chinner bool ok_alloc)
16818237fbf5SDave Chinner {
16828237fbf5SDave Chinner struct xfs_buf *agbp;
16838237fbf5SDave Chinner xfs_ino_t ino;
16848237fbf5SDave Chinner int error;
16858237fbf5SDave Chinner
16868237fbf5SDave Chinner /*
16878237fbf5SDave Chinner * Then read in the AGI buffer and recheck with the AGI buffer
16888237fbf5SDave Chinner * lock held.
16898237fbf5SDave Chinner */
169099b13c7fSDave Chinner error = xfs_ialloc_read_agi(pag, *tpp, &agbp);
16918237fbf5SDave Chinner if (error)
16928237fbf5SDave Chinner return error;
16938237fbf5SDave Chinner
16948237fbf5SDave Chinner if (!pag->pagi_freecount) {
16958237fbf5SDave Chinner if (!ok_alloc) {
16968237fbf5SDave Chinner error = -EAGAIN;
16978237fbf5SDave Chinner goto out_release;
16988237fbf5SDave Chinner }
16998237fbf5SDave Chinner
1700dedab3e4SDave Chinner error = xfs_ialloc_ag_alloc(pag, *tpp, agbp);
17018237fbf5SDave Chinner if (error < 0)
17028237fbf5SDave Chinner goto out_release;
17038237fbf5SDave Chinner
17048237fbf5SDave Chinner /*
17058237fbf5SDave Chinner * We successfully allocated space for an inode cluster in this
17068237fbf5SDave Chinner * AG. Roll the transaction so that we can allocate one of the
17078237fbf5SDave Chinner * new inodes.
17088237fbf5SDave Chinner */
17098237fbf5SDave Chinner ASSERT(pag->pagi_freecount > 0);
17108237fbf5SDave Chinner error = xfs_dialloc_roll(tpp, agbp);
17118237fbf5SDave Chinner if (error)
17128237fbf5SDave Chinner goto out_release;
17138237fbf5SDave Chinner }
17148237fbf5SDave Chinner
17158237fbf5SDave Chinner /* Allocate an inode in the found AG */
1716dedab3e4SDave Chinner error = xfs_dialloc_ag(pag, *tpp, agbp, parent, &ino);
17178237fbf5SDave Chinner if (!error)
17188237fbf5SDave Chinner *new_ino = ino;
17198237fbf5SDave Chinner return error;
17208237fbf5SDave Chinner
17218237fbf5SDave Chinner out_release:
17228237fbf5SDave Chinner xfs_trans_brelse(*tpp, agbp);
17238237fbf5SDave Chinner return error;
17248237fbf5SDave Chinner }
17258237fbf5SDave Chinner
17268237fbf5SDave Chinner /*
17278237fbf5SDave Chinner * Allocate an on-disk inode.
172830f712c9SDave Chinner *
17298d822dc3SDave Chinner * Mode is used to tell whether the new inode is a directory and hence where to
17308237fbf5SDave Chinner * locate it. The on-disk inode that is allocated will be returned in @new_ino
17318237fbf5SDave Chinner * on success, otherwise an error will be set to indicate the failure (e.g.
17328237fbf5SDave Chinner * -ENOSPC).
173330f712c9SDave Chinner */
173430f712c9SDave Chinner int
xfs_dialloc(struct xfs_trans ** tpp,xfs_ino_t parent,umode_t mode,xfs_ino_t * new_ino)1735b652afd9SDave Chinner xfs_dialloc(
1736f3bf6e0fSDave Chinner struct xfs_trans **tpp,
173730f712c9SDave Chinner xfs_ino_t parent,
173830f712c9SDave Chinner umode_t mode,
1739b652afd9SDave Chinner xfs_ino_t *new_ino)
174030f712c9SDave Chinner {
1741f3bf6e0fSDave Chinner struct xfs_mount *mp = (*tpp)->t_mountp;
174230f712c9SDave Chinner xfs_agnumber_t agno;
1743b652afd9SDave Chinner int error = 0;
174430f712c9SDave Chinner xfs_agnumber_t start_agno;
174530f712c9SDave Chinner struct xfs_perag *pag;
1746ef325959SDarrick J. Wong struct xfs_ino_geometry *igeo = M_IGEO(mp);
17478237fbf5SDave Chinner bool ok_alloc = true;
1748f08f984cSDave Chinner bool low_space = false;
174989b1f55aSDave Chinner int flags;
175076257a15SDave Chinner xfs_ino_t ino = NULLFSINO;
17518d822dc3SDave Chinner
175230f712c9SDave Chinner /*
175389b1f55aSDave Chinner * Directories, symlinks, and regular files frequently allocate at least
175489b1f55aSDave Chinner * one block, so factor that potential expansion when we examine whether
175589b1f55aSDave Chinner * an AG has enough space for file creation.
175630f712c9SDave Chinner */
175789b1f55aSDave Chinner if (S_ISDIR(mode))
17586e2985c9SDarrick J. Wong start_agno = (atomic_inc_return(&mp->m_agirotor) - 1) %
17596e2985c9SDarrick J. Wong mp->m_maxagi;
176089b1f55aSDave Chinner else {
176189b1f55aSDave Chinner start_agno = XFS_INO_TO_AGNO(mp, parent);
176289b1f55aSDave Chinner if (start_agno >= mp->m_maxagi)
176389b1f55aSDave Chinner start_agno = 0;
176489b1f55aSDave Chinner }
176530f712c9SDave Chinner
176630f712c9SDave Chinner /*
176730f712c9SDave Chinner * If we have already hit the ceiling of inode blocks then clear
17688237fbf5SDave Chinner * ok_alloc so we scan all available agi structures for a free
176930f712c9SDave Chinner * inode.
177074f9ce1cSGeorge Wang *
177174f9ce1cSGeorge Wang * Read rough value of mp->m_icount by percpu_counter_read_positive,
177274f9ce1cSGeorge Wang * which will sacrifice the preciseness but improve the performance.
177330f712c9SDave Chinner */
1774ef325959SDarrick J. Wong if (igeo->maxicount &&
1775ef325959SDarrick J. Wong percpu_counter_read_positive(&mp->m_icount) + igeo->ialloc_inos
1776ef325959SDarrick J. Wong > igeo->maxicount) {
17778237fbf5SDave Chinner ok_alloc = false;
177830f712c9SDave Chinner }
177930f712c9SDave Chinner
178030f712c9SDave Chinner /*
1781f08f984cSDave Chinner * If we are near to ENOSPC, we want to prefer allocation from AGs that
1782f08f984cSDave Chinner * have free inodes in them rather than use up free space allocating new
1783f08f984cSDave Chinner * inode chunks. Hence we turn off allocation for the first non-blocking
1784f08f984cSDave Chinner * pass through the AGs if we are near ENOSPC to consume free inodes
1785f08f984cSDave Chinner * that we can immediately allocate, but then we allow allocation on the
1786f08f984cSDave Chinner * second pass if we fail to find an AG with free inodes in it.
1787f08f984cSDave Chinner */
1788f08f984cSDave Chinner if (percpu_counter_read_positive(&mp->m_fdblocks) <
1789f08f984cSDave Chinner mp->m_low_space[XFS_LOWSP_1_PCNT]) {
1790f08f984cSDave Chinner ok_alloc = false;
1791f08f984cSDave Chinner low_space = true;
1792f08f984cSDave Chinner }
1793f08f984cSDave Chinner
1794f08f984cSDave Chinner /*
179530f712c9SDave Chinner * Loop until we find an allocation group that either has free inodes
179630f712c9SDave Chinner * or in which we can allocate some inodes. Iterate through the
179730f712c9SDave Chinner * allocation groups upward, wrapping at the end.
179830f712c9SDave Chinner */
179989b1f55aSDave Chinner flags = XFS_ALLOC_FLAG_TRYLOCK;
180076257a15SDave Chinner retry:
180176257a15SDave Chinner for_each_perag_wrap_at(mp, start_agno, mp->m_maxagi, agno, pag) {
1802dedab3e4SDave Chinner if (xfs_dialloc_good_ag(pag, *tpp, mode, flags, ok_alloc)) {
1803dedab3e4SDave Chinner error = xfs_dialloc_try_ag(pag, tpp, parent,
18048237fbf5SDave Chinner &ino, ok_alloc);
18058237fbf5SDave Chinner if (error != -EAGAIN)
18068d822dc3SDave Chinner break;
180776257a15SDave Chinner error = 0;
180830f712c9SDave Chinner }
180930f712c9SDave Chinner
181075c8c50fSDave Chinner if (xfs_is_shutdown(mp)) {
181189b1f55aSDave Chinner error = -EFSCORRUPTED;
181242685473SDave Chinner break;
181389b1f55aSDave Chinner }
18148237fbf5SDave Chinner }
181576257a15SDave Chinner if (pag)
181676257a15SDave Chinner xfs_perag_rele(pag);
181776257a15SDave Chinner if (error)
181876257a15SDave Chinner return error;
181976257a15SDave Chinner if (ino == NULLFSINO) {
182076257a15SDave Chinner if (flags) {
182189b1f55aSDave Chinner flags = 0;
1822f08f984cSDave Chinner if (low_space)
1823f08f984cSDave Chinner ok_alloc = true;
182476257a15SDave Chinner goto retry;
182589b1f55aSDave Chinner }
182676257a15SDave Chinner return -ENOSPC;
182730f712c9SDave Chinner }
1828b652afd9SDave Chinner *new_ino = ino;
182976257a15SDave Chinner return 0;
183030f712c9SDave Chinner }
183130f712c9SDave Chinner
183210ae3dc7SBrian Foster /*
183310ae3dc7SBrian Foster * Free the blocks of an inode chunk. We must consider that the inode chunk
183410ae3dc7SBrian Foster * might be sparse and only free the regions that are allocated as part of the
183510ae3dc7SBrian Foster * chunk.
183610ae3dc7SBrian Foster */
1837*7dfee17bSDave Chinner static int
xfs_difree_inode_chunk(struct xfs_trans * tp,xfs_agnumber_t agno,struct xfs_inobt_rec_incore * rec)183810ae3dc7SBrian Foster xfs_difree_inode_chunk(
18390f37d178SBrian Foster struct xfs_trans *tp,
184010ae3dc7SBrian Foster xfs_agnumber_t agno,
18410f37d178SBrian Foster struct xfs_inobt_rec_incore *rec)
184210ae3dc7SBrian Foster {
18430f37d178SBrian Foster struct xfs_mount *mp = tp->t_mountp;
18440f37d178SBrian Foster xfs_agblock_t sagbno = XFS_AGINO_TO_AGBNO(mp,
18450f37d178SBrian Foster rec->ir_startino);
184610ae3dc7SBrian Foster int startidx, endidx;
184710ae3dc7SBrian Foster int nextbit;
184810ae3dc7SBrian Foster xfs_agblock_t agbno;
184910ae3dc7SBrian Foster int contigblk;
185010ae3dc7SBrian Foster DECLARE_BITMAP(holemask, XFS_INOBT_HOLEMASK_BITS);
185110ae3dc7SBrian Foster
185210ae3dc7SBrian Foster if (!xfs_inobt_issparse(rec->ir_holemask)) {
185310ae3dc7SBrian Foster /* not sparse, calculate extent info directly */
1854*7dfee17bSDave Chinner return xfs_free_extent_later(tp,
1855*7dfee17bSDave Chinner XFS_AGB_TO_FSB(mp, agno, sagbno),
1856ef325959SDarrick J. Wong M_IGEO(mp)->ialloc_blks, &XFS_RMAP_OINFO_INODES,
1857ef325959SDarrick J. Wong XFS_AG_RESV_NONE);
185810ae3dc7SBrian Foster }
185910ae3dc7SBrian Foster
186010ae3dc7SBrian Foster /* holemask is only 16-bits (fits in an unsigned long) */
186110ae3dc7SBrian Foster ASSERT(sizeof(rec->ir_holemask) <= sizeof(holemask[0]));
186210ae3dc7SBrian Foster holemask[0] = rec->ir_holemask;
186310ae3dc7SBrian Foster
186410ae3dc7SBrian Foster /*
186510ae3dc7SBrian Foster * Find contiguous ranges of zeroes (i.e., allocated regions) in the
186610ae3dc7SBrian Foster * holemask and convert the start/end index of each range to an extent.
186710ae3dc7SBrian Foster * We start with the start and end index both pointing at the first 0 in
186810ae3dc7SBrian Foster * the mask.
186910ae3dc7SBrian Foster */
187010ae3dc7SBrian Foster startidx = endidx = find_first_zero_bit(holemask,
187110ae3dc7SBrian Foster XFS_INOBT_HOLEMASK_BITS);
187210ae3dc7SBrian Foster nextbit = startidx + 1;
187310ae3dc7SBrian Foster while (startidx < XFS_INOBT_HOLEMASK_BITS) {
1874*7dfee17bSDave Chinner int error;
1875*7dfee17bSDave Chinner
187610ae3dc7SBrian Foster nextbit = find_next_zero_bit(holemask, XFS_INOBT_HOLEMASK_BITS,
187710ae3dc7SBrian Foster nextbit);
187810ae3dc7SBrian Foster /*
187910ae3dc7SBrian Foster * If the next zero bit is contiguous, update the end index of
188010ae3dc7SBrian Foster * the current range and continue.
188110ae3dc7SBrian Foster */
188210ae3dc7SBrian Foster if (nextbit != XFS_INOBT_HOLEMASK_BITS &&
188310ae3dc7SBrian Foster nextbit == endidx + 1) {
188410ae3dc7SBrian Foster endidx = nextbit;
188510ae3dc7SBrian Foster goto next;
188610ae3dc7SBrian Foster }
188710ae3dc7SBrian Foster
188810ae3dc7SBrian Foster /*
188910ae3dc7SBrian Foster * nextbit is not contiguous with the current end index. Convert
189010ae3dc7SBrian Foster * the current start/end to an extent and add it to the free
189110ae3dc7SBrian Foster * list.
189210ae3dc7SBrian Foster */
189310ae3dc7SBrian Foster agbno = sagbno + (startidx * XFS_INODES_PER_HOLEMASK_BIT) /
189410ae3dc7SBrian Foster mp->m_sb.sb_inopblock;
189510ae3dc7SBrian Foster contigblk = ((endidx - startidx + 1) *
189610ae3dc7SBrian Foster XFS_INODES_PER_HOLEMASK_BIT) /
189710ae3dc7SBrian Foster mp->m_sb.sb_inopblock;
189810ae3dc7SBrian Foster
189910ae3dc7SBrian Foster ASSERT(agbno % mp->m_sb.sb_spino_align == 0);
190010ae3dc7SBrian Foster ASSERT(contigblk % mp->m_sb.sb_spino_align == 0);
1901*7dfee17bSDave Chinner error = xfs_free_extent_later(tp,
1902*7dfee17bSDave Chinner XFS_AGB_TO_FSB(mp, agno, agbno), contigblk,
19037280fedaSDarrick J. Wong &XFS_RMAP_OINFO_INODES, XFS_AG_RESV_NONE);
1904*7dfee17bSDave Chinner if (error)
1905*7dfee17bSDave Chinner return error;
190610ae3dc7SBrian Foster
190710ae3dc7SBrian Foster /* reset range to current bit and carry on... */
190810ae3dc7SBrian Foster startidx = endidx = nextbit;
190910ae3dc7SBrian Foster
191010ae3dc7SBrian Foster next:
191110ae3dc7SBrian Foster nextbit++;
191210ae3dc7SBrian Foster }
1913*7dfee17bSDave Chinner return 0;
191410ae3dc7SBrian Foster }
191510ae3dc7SBrian Foster
191630f712c9SDave Chinner STATIC int
xfs_difree_inobt(struct xfs_perag * pag,struct xfs_trans * tp,struct xfs_buf * agbp,xfs_agino_t agino,struct xfs_icluster * xic,struct xfs_inobt_rec_incore * orec)191730f712c9SDave Chinner xfs_difree_inobt(
1918dedab3e4SDave Chinner struct xfs_perag *pag,
191930f712c9SDave Chinner struct xfs_trans *tp,
192030f712c9SDave Chinner struct xfs_buf *agbp,
192130f712c9SDave Chinner xfs_agino_t agino,
192209b56604SBrian Foster struct xfs_icluster *xic,
192330f712c9SDave Chinner struct xfs_inobt_rec_incore *orec)
192430f712c9SDave Chinner {
1925dedab3e4SDave Chinner struct xfs_mount *mp = pag->pag_mount;
1926370c782bSChristoph Hellwig struct xfs_agi *agi = agbp->b_addr;
192730f712c9SDave Chinner struct xfs_btree_cur *cur;
192830f712c9SDave Chinner struct xfs_inobt_rec_incore rec;
192930f712c9SDave Chinner int ilen;
193030f712c9SDave Chinner int error;
193130f712c9SDave Chinner int i;
193230f712c9SDave Chinner int off;
193330f712c9SDave Chinner
193430f712c9SDave Chinner ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC));
193530f712c9SDave Chinner ASSERT(XFS_AGINO_TO_AGBNO(mp, agino) < be32_to_cpu(agi->agi_length));
193630f712c9SDave Chinner
193730f712c9SDave Chinner /*
193830f712c9SDave Chinner * Initialize the cursor.
193930f712c9SDave Chinner */
1940bab8b795SDave Chinner cur = xfs_inobt_init_cursor(pag, tp, agbp, XFS_BTNUM_INO);
194130f712c9SDave Chinner
19429ba0889eSDave Chinner error = xfs_check_agi_freecount(cur);
194330f712c9SDave Chinner if (error)
194430f712c9SDave Chinner goto error0;
194530f712c9SDave Chinner
194630f712c9SDave Chinner /*
194730f712c9SDave Chinner * Look for the entry describing this inode.
194830f712c9SDave Chinner */
194930f712c9SDave Chinner if ((error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i))) {
195030f712c9SDave Chinner xfs_warn(mp, "%s: xfs_inobt_lookup() returned error %d.",
195130f712c9SDave Chinner __func__, error);
195230f712c9SDave Chinner goto error0;
195330f712c9SDave Chinner }
1954f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(mp, i != 1)) {
1955f9e03706SDarrick J. Wong error = -EFSCORRUPTED;
1956f9e03706SDarrick J. Wong goto error0;
1957f9e03706SDarrick J. Wong }
195830f712c9SDave Chinner error = xfs_inobt_get_rec(cur, &rec, &i);
195930f712c9SDave Chinner if (error) {
196030f712c9SDave Chinner xfs_warn(mp, "%s: xfs_inobt_get_rec() returned error %d.",
196130f712c9SDave Chinner __func__, error);
196230f712c9SDave Chinner goto error0;
196330f712c9SDave Chinner }
1964f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(mp, i != 1)) {
1965f9e03706SDarrick J. Wong error = -EFSCORRUPTED;
1966f9e03706SDarrick J. Wong goto error0;
1967f9e03706SDarrick J. Wong }
196830f712c9SDave Chinner /*
196930f712c9SDave Chinner * Get the offset in the inode chunk.
197030f712c9SDave Chinner */
197130f712c9SDave Chinner off = agino - rec.ir_startino;
197230f712c9SDave Chinner ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK);
197330f712c9SDave Chinner ASSERT(!(rec.ir_free & XFS_INOBT_MASK(off)));
197430f712c9SDave Chinner /*
197530f712c9SDave Chinner * Mark the inode free & increment the count.
197630f712c9SDave Chinner */
197730f712c9SDave Chinner rec.ir_free |= XFS_INOBT_MASK(off);
197830f712c9SDave Chinner rec.ir_freecount++;
197930f712c9SDave Chinner
198030f712c9SDave Chinner /*
1981999633d3SBrian Foster * When an inode chunk is free, it becomes eligible for removal. Don't
1982999633d3SBrian Foster * remove the chunk if the block size is large enough for multiple inode
1983999633d3SBrian Foster * chunks (that might not be free).
198430f712c9SDave Chinner */
19850560f31aSDave Chinner if (!xfs_has_ikeep(mp) && rec.ir_free == XFS_INOBT_ALL_FREE &&
1986999633d3SBrian Foster mp->m_sb.sb_inopblock <= XFS_INODES_PER_CHUNK) {
1987749f24f3SThomas Meyer xic->deleted = true;
19887b13c515SDave Chinner xic->first_ino = XFS_AGINO_TO_INO(mp, pag->pag_agno,
19897b13c515SDave Chinner rec.ir_startino);
199009b56604SBrian Foster xic->alloc = xfs_inobt_irec_to_allocmask(&rec);
199130f712c9SDave Chinner
199230f712c9SDave Chinner /*
199330f712c9SDave Chinner * Remove the inode cluster from the AGI B+Tree, adjust the
199430f712c9SDave Chinner * AGI and Superblock inode counts, and mark the disk space
199530f712c9SDave Chinner * to be freed when the transaction is committed.
199630f712c9SDave Chinner */
1997999633d3SBrian Foster ilen = rec.ir_freecount;
199830f712c9SDave Chinner be32_add_cpu(&agi->agi_count, -ilen);
199930f712c9SDave Chinner be32_add_cpu(&agi->agi_freecount, -(ilen - 1));
200030f712c9SDave Chinner xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT);
200130f712c9SDave Chinner pag->pagi_freecount -= ilen - 1;
200289e9b5c0SDarrick J. Wong pag->pagi_count -= ilen;
200330f712c9SDave Chinner xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen);
200430f712c9SDave Chinner xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1));
200530f712c9SDave Chinner
200630f712c9SDave Chinner if ((error = xfs_btree_delete(cur, &i))) {
200730f712c9SDave Chinner xfs_warn(mp, "%s: xfs_btree_delete returned error %d.",
200830f712c9SDave Chinner __func__, error);
200930f712c9SDave Chinner goto error0;
201030f712c9SDave Chinner }
201130f712c9SDave Chinner
2012*7dfee17bSDave Chinner error = xfs_difree_inode_chunk(tp, pag->pag_agno, &rec);
2013*7dfee17bSDave Chinner if (error)
2014*7dfee17bSDave Chinner goto error0;
201530f712c9SDave Chinner } else {
2016749f24f3SThomas Meyer xic->deleted = false;
201730f712c9SDave Chinner
201830f712c9SDave Chinner error = xfs_inobt_update(cur, &rec);
201930f712c9SDave Chinner if (error) {
202030f712c9SDave Chinner xfs_warn(mp, "%s: xfs_inobt_update returned error %d.",
202130f712c9SDave Chinner __func__, error);
202230f712c9SDave Chinner goto error0;
202330f712c9SDave Chinner }
202430f712c9SDave Chinner
202530f712c9SDave Chinner /*
202630f712c9SDave Chinner * Change the inode free counts and log the ag/sb changes.
202730f712c9SDave Chinner */
202830f712c9SDave Chinner be32_add_cpu(&agi->agi_freecount, 1);
202930f712c9SDave Chinner xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
20307b13c515SDave Chinner pag->pagi_freecount++;
203130f712c9SDave Chinner xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1);
203230f712c9SDave Chinner }
203330f712c9SDave Chinner
20349ba0889eSDave Chinner error = xfs_check_agi_freecount(cur);
203530f712c9SDave Chinner if (error)
203630f712c9SDave Chinner goto error0;
203730f712c9SDave Chinner
203830f712c9SDave Chinner *orec = rec;
203930f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
204030f712c9SDave Chinner return 0;
204130f712c9SDave Chinner
204230f712c9SDave Chinner error0:
204330f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
204430f712c9SDave Chinner return error;
204530f712c9SDave Chinner }
204630f712c9SDave Chinner
204730f712c9SDave Chinner /*
204830f712c9SDave Chinner * Free an inode in the free inode btree.
204930f712c9SDave Chinner */
205030f712c9SDave Chinner STATIC int
xfs_difree_finobt(struct xfs_perag * pag,struct xfs_trans * tp,struct xfs_buf * agbp,xfs_agino_t agino,struct xfs_inobt_rec_incore * ibtrec)205130f712c9SDave Chinner xfs_difree_finobt(
2052dedab3e4SDave Chinner struct xfs_perag *pag,
205330f712c9SDave Chinner struct xfs_trans *tp,
205430f712c9SDave Chinner struct xfs_buf *agbp,
205530f712c9SDave Chinner xfs_agino_t agino,
205630f712c9SDave Chinner struct xfs_inobt_rec_incore *ibtrec) /* inobt record */
205730f712c9SDave Chinner {
2058dedab3e4SDave Chinner struct xfs_mount *mp = pag->pag_mount;
205930f712c9SDave Chinner struct xfs_btree_cur *cur;
206030f712c9SDave Chinner struct xfs_inobt_rec_incore rec;
206130f712c9SDave Chinner int offset = agino - ibtrec->ir_startino;
206230f712c9SDave Chinner int error;
206330f712c9SDave Chinner int i;
206430f712c9SDave Chinner
2065bab8b795SDave Chinner cur = xfs_inobt_init_cursor(pag, tp, agbp, XFS_BTNUM_FINO);
206630f712c9SDave Chinner
206730f712c9SDave Chinner error = xfs_inobt_lookup(cur, ibtrec->ir_startino, XFS_LOOKUP_EQ, &i);
206830f712c9SDave Chinner if (error)
206930f712c9SDave Chinner goto error;
207030f712c9SDave Chinner if (i == 0) {
207130f712c9SDave Chinner /*
207230f712c9SDave Chinner * If the record does not exist in the finobt, we must have just
207330f712c9SDave Chinner * freed an inode in a previously fully allocated chunk. If not,
207430f712c9SDave Chinner * something is out of sync.
207530f712c9SDave Chinner */
2076f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(mp, ibtrec->ir_freecount != 1)) {
2077f9e03706SDarrick J. Wong error = -EFSCORRUPTED;
2078f9e03706SDarrick J. Wong goto error;
2079f9e03706SDarrick J. Wong }
208030f712c9SDave Chinner
20815419040fSBrian Foster error = xfs_inobt_insert_rec(cur, ibtrec->ir_holemask,
20825419040fSBrian Foster ibtrec->ir_count,
20835419040fSBrian Foster ibtrec->ir_freecount,
208430f712c9SDave Chinner ibtrec->ir_free, &i);
208530f712c9SDave Chinner if (error)
208630f712c9SDave Chinner goto error;
208730f712c9SDave Chinner ASSERT(i == 1);
208830f712c9SDave Chinner
208930f712c9SDave Chinner goto out;
209030f712c9SDave Chinner }
209130f712c9SDave Chinner
209230f712c9SDave Chinner /*
209330f712c9SDave Chinner * Read and update the existing record. We could just copy the ibtrec
209430f712c9SDave Chinner * across here, but that would defeat the purpose of having redundant
209530f712c9SDave Chinner * metadata. By making the modifications independently, we can catch
209630f712c9SDave Chinner * corruptions that we wouldn't see if we just copied from one record
209730f712c9SDave Chinner * to another.
209830f712c9SDave Chinner */
209930f712c9SDave Chinner error = xfs_inobt_get_rec(cur, &rec, &i);
210030f712c9SDave Chinner if (error)
210130f712c9SDave Chinner goto error;
2102f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(mp, i != 1)) {
2103f9e03706SDarrick J. Wong error = -EFSCORRUPTED;
2104f9e03706SDarrick J. Wong goto error;
2105f9e03706SDarrick J. Wong }
210630f712c9SDave Chinner
210730f712c9SDave Chinner rec.ir_free |= XFS_INOBT_MASK(offset);
210830f712c9SDave Chinner rec.ir_freecount++;
210930f712c9SDave Chinner
2110f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(mp,
2111f9e03706SDarrick J. Wong rec.ir_free != ibtrec->ir_free ||
2112f9e03706SDarrick J. Wong rec.ir_freecount != ibtrec->ir_freecount)) {
2113f9e03706SDarrick J. Wong error = -EFSCORRUPTED;
2114f9e03706SDarrick J. Wong goto error;
2115f9e03706SDarrick J. Wong }
211630f712c9SDave Chinner
211730f712c9SDave Chinner /*
211830f712c9SDave Chinner * The content of inobt records should always match between the inobt
211930f712c9SDave Chinner * and finobt. The lifecycle of records in the finobt is different from
212030f712c9SDave Chinner * the inobt in that the finobt only tracks records with at least one
212130f712c9SDave Chinner * free inode. Hence, if all of the inodes are free and we aren't
212230f712c9SDave Chinner * keeping inode chunks permanently on disk, remove the record.
212330f712c9SDave Chinner * Otherwise, update the record with the new information.
2124999633d3SBrian Foster *
2125999633d3SBrian Foster * Note that we currently can't free chunks when the block size is large
2126999633d3SBrian Foster * enough for multiple chunks. Leave the finobt record to remain in sync
2127999633d3SBrian Foster * with the inobt.
212830f712c9SDave Chinner */
21290560f31aSDave Chinner if (!xfs_has_ikeep(mp) && rec.ir_free == XFS_INOBT_ALL_FREE &&
21300560f31aSDave Chinner mp->m_sb.sb_inopblock <= XFS_INODES_PER_CHUNK) {
213130f712c9SDave Chinner error = xfs_btree_delete(cur, &i);
213230f712c9SDave Chinner if (error)
213330f712c9SDave Chinner goto error;
213430f712c9SDave Chinner ASSERT(i == 1);
213530f712c9SDave Chinner } else {
213630f712c9SDave Chinner error = xfs_inobt_update(cur, &rec);
213730f712c9SDave Chinner if (error)
213830f712c9SDave Chinner goto error;
213930f712c9SDave Chinner }
214030f712c9SDave Chinner
214130f712c9SDave Chinner out:
21429ba0889eSDave Chinner error = xfs_check_agi_freecount(cur);
214330f712c9SDave Chinner if (error)
214430f712c9SDave Chinner goto error;
214530f712c9SDave Chinner
214630f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
214730f712c9SDave Chinner return 0;
214830f712c9SDave Chinner
214930f712c9SDave Chinner error:
215030f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
215130f712c9SDave Chinner return error;
215230f712c9SDave Chinner }
215330f712c9SDave Chinner
215430f712c9SDave Chinner /*
215530f712c9SDave Chinner * Free disk inode. Carefully avoids touching the incore inode, all
215630f712c9SDave Chinner * manipulations incore are the caller's responsibility.
215730f712c9SDave Chinner * The on-disk inode is not changed by this operation, only the
215830f712c9SDave Chinner * btree (free inode mask) is changed.
215930f712c9SDave Chinner */
216030f712c9SDave Chinner int
xfs_difree(struct xfs_trans * tp,struct xfs_perag * pag,xfs_ino_t inode,struct xfs_icluster * xic)216130f712c9SDave Chinner xfs_difree(
2162f40aadb2SDave Chinner struct xfs_trans *tp,
2163f40aadb2SDave Chinner struct xfs_perag *pag,
2164f40aadb2SDave Chinner xfs_ino_t inode,
2165f40aadb2SDave Chinner struct xfs_icluster *xic)
216630f712c9SDave Chinner {
216730f712c9SDave Chinner /* REFERENCED */
216830f712c9SDave Chinner xfs_agblock_t agbno; /* block number containing inode */
216930f712c9SDave Chinner struct xfs_buf *agbp; /* buffer for allocation group header */
217030f712c9SDave Chinner xfs_agino_t agino; /* allocation group inode number */
217130f712c9SDave Chinner int error; /* error return value */
21727b13c515SDave Chinner struct xfs_mount *mp = tp->t_mountp;
217330f712c9SDave Chinner struct xfs_inobt_rec_incore rec;/* btree record */
217430f712c9SDave Chinner
217530f712c9SDave Chinner /*
217630f712c9SDave Chinner * Break up inode number into its components.
217730f712c9SDave Chinner */
2178f40aadb2SDave Chinner if (pag->pag_agno != XFS_INO_TO_AGNO(mp, inode)) {
2179f40aadb2SDave Chinner xfs_warn(mp, "%s: agno != pag->pag_agno (%d != %d).",
2180f40aadb2SDave Chinner __func__, XFS_INO_TO_AGNO(mp, inode), pag->pag_agno);
218130f712c9SDave Chinner ASSERT(0);
21822451337dSDave Chinner return -EINVAL;
218330f712c9SDave Chinner }
218430f712c9SDave Chinner agino = XFS_INO_TO_AGINO(mp, inode);
2185f40aadb2SDave Chinner if (inode != XFS_AGINO_TO_INO(mp, pag->pag_agno, agino)) {
218630f712c9SDave Chinner xfs_warn(mp, "%s: inode != XFS_AGINO_TO_INO() (%llu != %llu).",
218730f712c9SDave Chinner __func__, (unsigned long long)inode,
2188f40aadb2SDave Chinner (unsigned long long)XFS_AGINO_TO_INO(mp, pag->pag_agno, agino));
218930f712c9SDave Chinner ASSERT(0);
21902451337dSDave Chinner return -EINVAL;
219130f712c9SDave Chinner }
219230f712c9SDave Chinner agbno = XFS_AGINO_TO_AGBNO(mp, agino);
219330f712c9SDave Chinner if (agbno >= mp->m_sb.sb_agblocks) {
219430f712c9SDave Chinner xfs_warn(mp, "%s: agbno >= mp->m_sb.sb_agblocks (%d >= %d).",
219530f712c9SDave Chinner __func__, agbno, mp->m_sb.sb_agblocks);
219630f712c9SDave Chinner ASSERT(0);
21972451337dSDave Chinner return -EINVAL;
219830f712c9SDave Chinner }
219930f712c9SDave Chinner /*
220030f712c9SDave Chinner * Get the allocation group header.
220130f712c9SDave Chinner */
220299b13c7fSDave Chinner error = xfs_ialloc_read_agi(pag, tp, &agbp);
220330f712c9SDave Chinner if (error) {
220430f712c9SDave Chinner xfs_warn(mp, "%s: xfs_ialloc_read_agi() returned error %d.",
220530f712c9SDave Chinner __func__, error);
220630f712c9SDave Chinner return error;
220730f712c9SDave Chinner }
220830f712c9SDave Chinner
220930f712c9SDave Chinner /*
221030f712c9SDave Chinner * Fix up the inode allocation btree.
221130f712c9SDave Chinner */
2212dedab3e4SDave Chinner error = xfs_difree_inobt(pag, tp, agbp, agino, xic, &rec);
221330f712c9SDave Chinner if (error)
221430f712c9SDave Chinner goto error0;
221530f712c9SDave Chinner
221630f712c9SDave Chinner /*
221730f712c9SDave Chinner * Fix up the free inode btree.
221830f712c9SDave Chinner */
2219ebd9027dSDave Chinner if (xfs_has_finobt(mp)) {
2220dedab3e4SDave Chinner error = xfs_difree_finobt(pag, tp, agbp, agino, &rec);
222130f712c9SDave Chinner if (error)
222230f712c9SDave Chinner goto error0;
222330f712c9SDave Chinner }
222430f712c9SDave Chinner
222530f712c9SDave Chinner return 0;
222630f712c9SDave Chinner
222730f712c9SDave Chinner error0:
222830f712c9SDave Chinner return error;
222930f712c9SDave Chinner }
223030f712c9SDave Chinner
223130f712c9SDave Chinner STATIC int
xfs_imap_lookup(struct xfs_perag * pag,struct xfs_trans * tp,xfs_agino_t agino,xfs_agblock_t agbno,xfs_agblock_t * chunk_agbno,xfs_agblock_t * offset_agbno,int flags)223230f712c9SDave Chinner xfs_imap_lookup(
22337b13c515SDave Chinner struct xfs_perag *pag,
2234498f0adbSDave Chinner struct xfs_trans *tp,
223530f712c9SDave Chinner xfs_agino_t agino,
223630f712c9SDave Chinner xfs_agblock_t agbno,
223730f712c9SDave Chinner xfs_agblock_t *chunk_agbno,
223830f712c9SDave Chinner xfs_agblock_t *offset_agbno,
223930f712c9SDave Chinner int flags)
224030f712c9SDave Chinner {
2241498f0adbSDave Chinner struct xfs_mount *mp = pag->pag_mount;
224230f712c9SDave Chinner struct xfs_inobt_rec_incore rec;
224330f712c9SDave Chinner struct xfs_btree_cur *cur;
224430f712c9SDave Chinner struct xfs_buf *agbp;
224530f712c9SDave Chinner int error;
224630f712c9SDave Chinner int i;
224730f712c9SDave Chinner
224899b13c7fSDave Chinner error = xfs_ialloc_read_agi(pag, tp, &agbp);
224930f712c9SDave Chinner if (error) {
225030f712c9SDave Chinner xfs_alert(mp,
225130f712c9SDave Chinner "%s: xfs_ialloc_read_agi() returned error %d, agno %d",
22527b13c515SDave Chinner __func__, error, pag->pag_agno);
225330f712c9SDave Chinner return error;
225430f712c9SDave Chinner }
225530f712c9SDave Chinner
225630f712c9SDave Chinner /*
225730f712c9SDave Chinner * Lookup the inode record for the given agino. If the record cannot be
225830f712c9SDave Chinner * found, then it's an invalid inode number and we should abort. Once
225930f712c9SDave Chinner * we have a record, we need to ensure it contains the inode number
226030f712c9SDave Chinner * we are looking up.
226130f712c9SDave Chinner */
2262bab8b795SDave Chinner cur = xfs_inobt_init_cursor(pag, tp, agbp, XFS_BTNUM_INO);
226330f712c9SDave Chinner error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i);
226430f712c9SDave Chinner if (!error) {
226530f712c9SDave Chinner if (i)
226630f712c9SDave Chinner error = xfs_inobt_get_rec(cur, &rec, &i);
226730f712c9SDave Chinner if (!error && i == 0)
22682451337dSDave Chinner error = -EINVAL;
226930f712c9SDave Chinner }
227030f712c9SDave Chinner
227130f712c9SDave Chinner xfs_trans_brelse(tp, agbp);
22720b04b6b8SDarrick J. Wong xfs_btree_del_cursor(cur, error);
227330f712c9SDave Chinner if (error)
227430f712c9SDave Chinner return error;
227530f712c9SDave Chinner
227630f712c9SDave Chinner /* check that the returned record contains the required inode */
227730f712c9SDave Chinner if (rec.ir_startino > agino ||
2278ef325959SDarrick J. Wong rec.ir_startino + M_IGEO(mp)->ialloc_inos <= agino)
22792451337dSDave Chinner return -EINVAL;
228030f712c9SDave Chinner
228130f712c9SDave Chinner /* for untrusted inodes check it is allocated first */
228230f712c9SDave Chinner if ((flags & XFS_IGET_UNTRUSTED) &&
228330f712c9SDave Chinner (rec.ir_free & XFS_INOBT_MASK(agino - rec.ir_startino)))
22842451337dSDave Chinner return -EINVAL;
228530f712c9SDave Chinner
228630f712c9SDave Chinner *chunk_agbno = XFS_AGINO_TO_AGBNO(mp, rec.ir_startino);
228730f712c9SDave Chinner *offset_agbno = agbno - *chunk_agbno;
228830f712c9SDave Chinner return 0;
228930f712c9SDave Chinner }
229030f712c9SDave Chinner
229130f712c9SDave Chinner /*
229230f712c9SDave Chinner * Return the location of the inode in imap, for mapping it into a buffer.
229330f712c9SDave Chinner */
229430f712c9SDave Chinner int
xfs_imap(struct xfs_perag * pag,struct xfs_trans * tp,xfs_ino_t ino,struct xfs_imap * imap,uint flags)229530f712c9SDave Chinner xfs_imap(
2296498f0adbSDave Chinner struct xfs_perag *pag,
2297498f0adbSDave Chinner struct xfs_trans *tp,
229830f712c9SDave Chinner xfs_ino_t ino, /* inode to locate */
229930f712c9SDave Chinner struct xfs_imap *imap, /* location map structure */
230030f712c9SDave Chinner uint flags) /* flags for inode btree lookup */
230130f712c9SDave Chinner {
2302498f0adbSDave Chinner struct xfs_mount *mp = pag->pag_mount;
230330f712c9SDave Chinner xfs_agblock_t agbno; /* block number of inode in the alloc group */
230430f712c9SDave Chinner xfs_agino_t agino; /* inode number within alloc group */
230530f712c9SDave Chinner xfs_agblock_t chunk_agbno; /* first block in inode chunk */
230630f712c9SDave Chinner xfs_agblock_t cluster_agbno; /* first block in inode cluster */
230730f712c9SDave Chinner int error; /* error code */
230830f712c9SDave Chinner int offset; /* index of inode in its buffer */
230930f712c9SDave Chinner xfs_agblock_t offset_agbno; /* blks from chunk start to inode */
231030f712c9SDave Chinner
231130f712c9SDave Chinner ASSERT(ino != NULLFSINO);
231230f712c9SDave Chinner
231330f712c9SDave Chinner /*
231430f712c9SDave Chinner * Split up the inode number into its parts.
231530f712c9SDave Chinner */
231630f712c9SDave Chinner agino = XFS_INO_TO_AGINO(mp, ino);
231730f712c9SDave Chinner agbno = XFS_AGINO_TO_AGBNO(mp, agino);
2318498f0adbSDave Chinner if (agbno >= mp->m_sb.sb_agblocks ||
23197b13c515SDave Chinner ino != XFS_AGINO_TO_INO(mp, pag->pag_agno, agino)) {
23207b13c515SDave Chinner error = -EINVAL;
232130f712c9SDave Chinner #ifdef DEBUG
232230f712c9SDave Chinner /*
232330f712c9SDave Chinner * Don't output diagnostic information for untrusted inodes
232430f712c9SDave Chinner * as they can be invalid without implying corruption.
232530f712c9SDave Chinner */
232630f712c9SDave Chinner if (flags & XFS_IGET_UNTRUSTED)
2327498f0adbSDave Chinner return error;
232830f712c9SDave Chinner if (agbno >= mp->m_sb.sb_agblocks) {
232930f712c9SDave Chinner xfs_alert(mp,
233030f712c9SDave Chinner "%s: agbno (0x%llx) >= mp->m_sb.sb_agblocks (0x%lx)",
233130f712c9SDave Chinner __func__, (unsigned long long)agbno,
233230f712c9SDave Chinner (unsigned long)mp->m_sb.sb_agblocks);
233330f712c9SDave Chinner }
2334498f0adbSDave Chinner if (ino != XFS_AGINO_TO_INO(mp, pag->pag_agno, agino)) {
233530f712c9SDave Chinner xfs_alert(mp,
233630f712c9SDave Chinner "%s: ino (0x%llx) != XFS_AGINO_TO_INO() (0x%llx)",
233730f712c9SDave Chinner __func__, ino,
23387b13c515SDave Chinner XFS_AGINO_TO_INO(mp, pag->pag_agno, agino));
233930f712c9SDave Chinner }
234030f712c9SDave Chinner xfs_stack_trace();
234130f712c9SDave Chinner #endif /* DEBUG */
2342498f0adbSDave Chinner return error;
234330f712c9SDave Chinner }
234430f712c9SDave Chinner
234530f712c9SDave Chinner /*
234630f712c9SDave Chinner * For bulkstat and handle lookups, we have an untrusted inode number
234730f712c9SDave Chinner * that we have to verify is valid. We cannot do this just by reading
234830f712c9SDave Chinner * the inode buffer as it may have been unlinked and removed leaving
234930f712c9SDave Chinner * inodes in stale state on disk. Hence we have to do a btree lookup
235030f712c9SDave Chinner * in all cases where an untrusted inode number is passed.
235130f712c9SDave Chinner */
235230f712c9SDave Chinner if (flags & XFS_IGET_UNTRUSTED) {
2353498f0adbSDave Chinner error = xfs_imap_lookup(pag, tp, agino, agbno,
235430f712c9SDave Chinner &chunk_agbno, &offset_agbno, flags);
235530f712c9SDave Chinner if (error)
2356498f0adbSDave Chinner return error;
235730f712c9SDave Chinner goto out_map;
235830f712c9SDave Chinner }
235930f712c9SDave Chinner
236030f712c9SDave Chinner /*
236130f712c9SDave Chinner * If the inode cluster size is the same as the blocksize or
236230f712c9SDave Chinner * smaller we get to the buffer by simple arithmetics.
236330f712c9SDave Chinner */
2364ef325959SDarrick J. Wong if (M_IGEO(mp)->blocks_per_cluster == 1) {
236530f712c9SDave Chinner offset = XFS_INO_TO_OFFSET(mp, ino);
236630f712c9SDave Chinner ASSERT(offset < mp->m_sb.sb_inopblock);
236730f712c9SDave Chinner
23687b13c515SDave Chinner imap->im_blkno = XFS_AGB_TO_DADDR(mp, pag->pag_agno, agbno);
236930f712c9SDave Chinner imap->im_len = XFS_FSB_TO_BB(mp, 1);
2370755c7bf5SDarrick J. Wong imap->im_boffset = (unsigned short)(offset <<
2371755c7bf5SDarrick J. Wong mp->m_sb.sb_inodelog);
2372498f0adbSDave Chinner return 0;
237330f712c9SDave Chinner }
237430f712c9SDave Chinner
237530f712c9SDave Chinner /*
237630f712c9SDave Chinner * If the inode chunks are aligned then use simple maths to
237730f712c9SDave Chinner * find the location. Otherwise we have to do a btree
237830f712c9SDave Chinner * lookup to find the location.
237930f712c9SDave Chinner */
2380ef325959SDarrick J. Wong if (M_IGEO(mp)->inoalign_mask) {
2381ef325959SDarrick J. Wong offset_agbno = agbno & M_IGEO(mp)->inoalign_mask;
238230f712c9SDave Chinner chunk_agbno = agbno - offset_agbno;
238330f712c9SDave Chinner } else {
2384498f0adbSDave Chinner error = xfs_imap_lookup(pag, tp, agino, agbno,
238530f712c9SDave Chinner &chunk_agbno, &offset_agbno, flags);
238630f712c9SDave Chinner if (error)
2387498f0adbSDave Chinner return error;
238830f712c9SDave Chinner }
238930f712c9SDave Chinner
239030f712c9SDave Chinner out_map:
239130f712c9SDave Chinner ASSERT(agbno >= chunk_agbno);
239230f712c9SDave Chinner cluster_agbno = chunk_agbno +
2393ef325959SDarrick J. Wong ((offset_agbno / M_IGEO(mp)->blocks_per_cluster) *
2394ef325959SDarrick J. Wong M_IGEO(mp)->blocks_per_cluster);
239530f712c9SDave Chinner offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
239630f712c9SDave Chinner XFS_INO_TO_OFFSET(mp, ino);
239730f712c9SDave Chinner
23987b13c515SDave Chinner imap->im_blkno = XFS_AGB_TO_DADDR(mp, pag->pag_agno, cluster_agbno);
2399ef325959SDarrick J. Wong imap->im_len = XFS_FSB_TO_BB(mp, M_IGEO(mp)->blocks_per_cluster);
2400755c7bf5SDarrick J. Wong imap->im_boffset = (unsigned short)(offset << mp->m_sb.sb_inodelog);
240130f712c9SDave Chinner
240230f712c9SDave Chinner /*
240330f712c9SDave Chinner * If the inode number maps to a block outside the bounds
240430f712c9SDave Chinner * of the file system then return NULL rather than calling
240530f712c9SDave Chinner * read_buf and panicing when we get an error from the
240630f712c9SDave Chinner * driver.
240730f712c9SDave Chinner */
240830f712c9SDave Chinner if ((imap->im_blkno + imap->im_len) >
240930f712c9SDave Chinner XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) {
241030f712c9SDave Chinner xfs_alert(mp,
241130f712c9SDave Chinner "%s: (im_blkno (0x%llx) + im_len (0x%llx)) > sb_dblocks (0x%llx)",
241230f712c9SDave Chinner __func__, (unsigned long long) imap->im_blkno,
241330f712c9SDave Chinner (unsigned long long) imap->im_len,
241430f712c9SDave Chinner XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks));
2415498f0adbSDave Chinner return -EINVAL;
241630f712c9SDave Chinner }
2417498f0adbSDave Chinner return 0;
241830f712c9SDave Chinner }
241930f712c9SDave Chinner
242030f712c9SDave Chinner /*
242130f712c9SDave Chinner * Log specified fields for the ag hdr (inode section). The growth of the agi
242230f712c9SDave Chinner * structure over time requires that we interpret the buffer as two logical
242330f712c9SDave Chinner * regions delineated by the end of the unlinked list. This is due to the size
242430f712c9SDave Chinner * of the hash table and its location in the middle of the agi.
242530f712c9SDave Chinner *
242630f712c9SDave Chinner * For example, a request to log a field before agi_unlinked and a field after
242730f712c9SDave Chinner * agi_unlinked could cause us to log the entire hash table and use an excessive
242830f712c9SDave Chinner * amount of log space. To avoid this behavior, log the region up through
242930f712c9SDave Chinner * agi_unlinked in one call and the region after agi_unlinked through the end of
243030f712c9SDave Chinner * the structure in another.
243130f712c9SDave Chinner */
243230f712c9SDave Chinner void
xfs_ialloc_log_agi(struct xfs_trans * tp,struct xfs_buf * bp,uint32_t fields)243330f712c9SDave Chinner xfs_ialloc_log_agi(
24340d1b9769SDave Chinner struct xfs_trans *tp,
24350d1b9769SDave Chinner struct xfs_buf *bp,
24360d1b9769SDave Chinner uint32_t fields)
243730f712c9SDave Chinner {
243830f712c9SDave Chinner int first; /* first byte number */
243930f712c9SDave Chinner int last; /* last byte number */
244030f712c9SDave Chinner static const short offsets[] = { /* field starting offsets */
244130f712c9SDave Chinner /* keep in sync with bit definitions */
244230f712c9SDave Chinner offsetof(xfs_agi_t, agi_magicnum),
244330f712c9SDave Chinner offsetof(xfs_agi_t, agi_versionnum),
244430f712c9SDave Chinner offsetof(xfs_agi_t, agi_seqno),
244530f712c9SDave Chinner offsetof(xfs_agi_t, agi_length),
244630f712c9SDave Chinner offsetof(xfs_agi_t, agi_count),
244730f712c9SDave Chinner offsetof(xfs_agi_t, agi_root),
244830f712c9SDave Chinner offsetof(xfs_agi_t, agi_level),
244930f712c9SDave Chinner offsetof(xfs_agi_t, agi_freecount),
245030f712c9SDave Chinner offsetof(xfs_agi_t, agi_newino),
245130f712c9SDave Chinner offsetof(xfs_agi_t, agi_dirino),
245230f712c9SDave Chinner offsetof(xfs_agi_t, agi_unlinked),
245330f712c9SDave Chinner offsetof(xfs_agi_t, agi_free_root),
245430f712c9SDave Chinner offsetof(xfs_agi_t, agi_free_level),
24552a39946cSDarrick J. Wong offsetof(xfs_agi_t, agi_iblocks),
245630f712c9SDave Chinner sizeof(xfs_agi_t)
245730f712c9SDave Chinner };
245830f712c9SDave Chinner #ifdef DEBUG
2459370c782bSChristoph Hellwig struct xfs_agi *agi = bp->b_addr;
246030f712c9SDave Chinner
246130f712c9SDave Chinner ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC));
246230f712c9SDave Chinner #endif
246330f712c9SDave Chinner
246430f712c9SDave Chinner /*
246530f712c9SDave Chinner * Compute byte offsets for the first and last fields in the first
246630f712c9SDave Chinner * region and log the agi buffer. This only logs up through
246730f712c9SDave Chinner * agi_unlinked.
246830f712c9SDave Chinner */
246930f712c9SDave Chinner if (fields & XFS_AGI_ALL_BITS_R1) {
247030f712c9SDave Chinner xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R1,
247130f712c9SDave Chinner &first, &last);
247230f712c9SDave Chinner xfs_trans_log_buf(tp, bp, first, last);
247330f712c9SDave Chinner }
247430f712c9SDave Chinner
247530f712c9SDave Chinner /*
247630f712c9SDave Chinner * Mask off the bits in the first region and calculate the first and
247730f712c9SDave Chinner * last field offsets for any bits in the second region.
247830f712c9SDave Chinner */
247930f712c9SDave Chinner fields &= ~XFS_AGI_ALL_BITS_R1;
248030f712c9SDave Chinner if (fields) {
248130f712c9SDave Chinner xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R2,
248230f712c9SDave Chinner &first, &last);
248330f712c9SDave Chinner xfs_trans_log_buf(tp, bp, first, last);
248430f712c9SDave Chinner }
248530f712c9SDave Chinner }
248630f712c9SDave Chinner
2487a6a781a5SDarrick J. Wong static xfs_failaddr_t
xfs_agi_verify(struct xfs_buf * bp)248830f712c9SDave Chinner xfs_agi_verify(
248930f712c9SDave Chinner struct xfs_buf *bp)
249030f712c9SDave Chinner {
2491dbd329f1SChristoph Hellwig struct xfs_mount *mp = bp->b_mount;
2492370c782bSChristoph Hellwig struct xfs_agi *agi = bp->b_addr;
24939f96cc95SDave Chinner xfs_failaddr_t fa;
249430f712c9SDave Chinner uint32_t agi_seqno = be32_to_cpu(agi->agi_seqno);
249538c26bfdSDave Chinner uint32_t agi_length = be32_to_cpu(agi->agi_length);
2496a45086e2SBrian Foster int i;
2497a6a781a5SDarrick J. Wong
2498370c782bSChristoph Hellwig if (xfs_has_crc(mp)) {
2499a6a781a5SDarrick J. Wong if (!uuid_equal(&agi->agi_uuid, &mp->m_sb.sb_meta_uuid))
2500a45086e2SBrian Foster return __this_address;
2501a45086e2SBrian Foster if (!xfs_log_check_lsn(mp, be64_to_cpu(agi->agi_lsn)))
250230f712c9SDave Chinner return __this_address;
250330f712c9SDave Chinner }
250430f712c9SDave Chinner
250539708c20SBrian Foster /*
2506a6a781a5SDarrick J. Wong * Validate the magic number of the agi block.
250730f712c9SDave Chinner */
2508a6a781a5SDarrick J. Wong if (!xfs_verify_magic(bp, agi->agi_magicnum))
250930f712c9SDave Chinner return __this_address;
2510d2a047f3SDarrick J. Wong if (!XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum)))
2511973975b7SDarrick J. Wong return __this_address;
2512a6a781a5SDarrick J. Wong
2513d2a047f3SDarrick J. Wong fa = xfs_validate_ag_length(bp, agi_seqno, agi_length);
251438c26bfdSDave Chinner if (fa)
2515d2a047f3SDarrick J. Wong return fa;
2516973975b7SDarrick J. Wong
2517a6a781a5SDarrick J. Wong if (be32_to_cpu(agi->agi_level) < 1 ||
2518d2a047f3SDarrick J. Wong be32_to_cpu(agi->agi_level) > M_IGEO(mp)->inobt_maxlevels)
251930f712c9SDave Chinner return __this_address;
252030f712c9SDave Chinner
252130f712c9SDave Chinner if (xfs_has_finobt(mp) &&
252230f712c9SDave Chinner (be32_to_cpu(agi->agi_free_level) < 1 ||
252330f712c9SDave Chinner be32_to_cpu(agi->agi_free_level) > M_IGEO(mp)->inobt_maxlevels))
252430f712c9SDave Chinner return __this_address;
252530f712c9SDave Chinner
2526a6a781a5SDarrick J. Wong for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++) {
252730f712c9SDave Chinner if (agi->agi_unlinked[i] == cpu_to_be32(NULLAGINO))
25289f96cc95SDave Chinner continue;
25295089eaffSCarlos Maiolino if (!xfs_verify_ino(mp, be32_to_cpu(agi->agi_unlinked[i])))
25309f96cc95SDave Chinner return __this_address;
25319f96cc95SDave Chinner }
25329f96cc95SDave Chinner
25339f96cc95SDave Chinner return NULL;
25349f96cc95SDave Chinner }
2535a6a781a5SDarrick J. Wong
253630f712c9SDave Chinner static void
xfs_agi_read_verify(struct xfs_buf * bp)253730f712c9SDave Chinner xfs_agi_read_verify(
253830f712c9SDave Chinner struct xfs_buf *bp)
253930f712c9SDave Chinner {
254030f712c9SDave Chinner struct xfs_mount *mp = bp->b_mount;
254130f712c9SDave Chinner xfs_failaddr_t fa;
2542dbd329f1SChristoph Hellwig
2543bc1a09b8SDarrick J. Wong if (xfs_has_crc(mp) &&
254430f712c9SDave Chinner !xfs_buf_verify_cksum(bp, XFS_AGI_CRC_OFF))
254538c26bfdSDave Chinner xfs_verifier_error(bp, -EFSBADCRC, __this_address);
254630f712c9SDave Chinner else {
2547bc1a09b8SDarrick J. Wong fa = xfs_agi_verify(bp);
2548bc1a09b8SDarrick J. Wong if (XFS_TEST_ERROR(fa, mp, XFS_ERRTAG_IALLOC_READ_AGI))
2549bc1a09b8SDarrick J. Wong xfs_verifier_error(bp, -EFSCORRUPTED, fa);
2550bc1a09b8SDarrick J. Wong }
2551bc1a09b8SDarrick J. Wong }
2552bc1a09b8SDarrick J. Wong
255330f712c9SDave Chinner static void
xfs_agi_write_verify(struct xfs_buf * bp)255430f712c9SDave Chinner xfs_agi_write_verify(
255530f712c9SDave Chinner struct xfs_buf *bp)
255630f712c9SDave Chinner {
255730f712c9SDave Chinner struct xfs_mount *mp = bp->b_mount;
255830f712c9SDave Chinner struct xfs_buf_log_item *bip = bp->b_log_item;
2559dbd329f1SChristoph Hellwig struct xfs_agi *agi = bp->b_addr;
2560fb1755a6SCarlos Maiolino xfs_failaddr_t fa;
2561370c782bSChristoph Hellwig
2562bc1a09b8SDarrick J. Wong fa = xfs_agi_verify(bp);
256330f712c9SDave Chinner if (fa) {
2564bc1a09b8SDarrick J. Wong xfs_verifier_error(bp, -EFSCORRUPTED, fa);
2565bc1a09b8SDarrick J. Wong return;
2566bc1a09b8SDarrick J. Wong }
256730f712c9SDave Chinner
256830f712c9SDave Chinner if (!xfs_has_crc(mp))
256930f712c9SDave Chinner return;
257038c26bfdSDave Chinner
257130f712c9SDave Chinner if (bip)
257230f712c9SDave Chinner agi->agi_lsn = cpu_to_be64(bip->bli_item.li_lsn);
257330f712c9SDave Chinner xfs_buf_update_cksum(bp, XFS_AGI_CRC_OFF);
2574370c782bSChristoph Hellwig }
257530f712c9SDave Chinner
257630f712c9SDave Chinner const struct xfs_buf_ops xfs_agi_buf_ops = {
257730f712c9SDave Chinner .name = "xfs_agi",
257830f712c9SDave Chinner .magic = { cpu_to_be32(XFS_AGI_MAGIC), cpu_to_be32(XFS_AGI_MAGIC) },
2579233135b7SEric Sandeen .verify_read = xfs_agi_read_verify,
258039708c20SBrian Foster .verify_write = xfs_agi_write_verify,
258130f712c9SDave Chinner .verify_struct = xfs_agi_verify,
258230f712c9SDave Chinner };
2583b5572597SDarrick J. Wong
258430f712c9SDave Chinner /*
258530f712c9SDave Chinner * Read in the allocation group header (inode allocation section)
258630f712c9SDave Chinner */
258730f712c9SDave Chinner int
xfs_read_agi(struct xfs_perag * pag,struct xfs_trans * tp,struct xfs_buf ** agibpp)258830f712c9SDave Chinner xfs_read_agi(
258930f712c9SDave Chinner struct xfs_perag *pag,
259030f712c9SDave Chinner struct xfs_trans *tp,
259161021debSDave Chinner struct xfs_buf **agibpp)
259261021debSDave Chinner {
259361021debSDave Chinner struct xfs_mount *mp = pag->pag_mount;
259430f712c9SDave Chinner int error;
259561021debSDave Chinner
259630f712c9SDave Chinner trace_xfs_read_agi(pag->pag_mount, pag->pag_agno);
259730f712c9SDave Chinner
259861021debSDave Chinner error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
259930f712c9SDave Chinner XFS_AG_DADDR(mp, pag->pag_agno, XFS_AGI_DADDR(mp)),
260030f712c9SDave Chinner XFS_FSS_TO_BB(mp, 1), 0, agibpp, &xfs_agi_buf_ops);
260161021debSDave Chinner if (error)
260261021debSDave Chinner return error;
260330f712c9SDave Chinner if (tp)
260430f712c9SDave Chinner xfs_trans_buf_set_type(tp, *agibpp, XFS_BLFT_AGI_BUF);
2605200237d6SEric Sandeen
260661021debSDave Chinner xfs_buf_set_ref(*agibpp, XFS_AGI_REF);
260730f712c9SDave Chinner return 0;
260861021debSDave Chinner }
260930f712c9SDave Chinner
261030f712c9SDave Chinner /*
261130f712c9SDave Chinner * Read in the agi and initialise the per-ag data. If the caller supplies a
2612a95fee40SDave Chinner * @agibpp, return the locked AGI buffer to them, otherwise release it.
2613a95fee40SDave Chinner */
2614a95fee40SDave Chinner int
xfs_ialloc_read_agi(struct xfs_perag * pag,struct xfs_trans * tp,struct xfs_buf ** agibpp)2615a95fee40SDave Chinner xfs_ialloc_read_agi(
261630f712c9SDave Chinner struct xfs_perag *pag,
261730f712c9SDave Chinner struct xfs_trans *tp,
261899b13c7fSDave Chinner struct xfs_buf **agibpp)
261999b13c7fSDave Chinner {
2620a95fee40SDave Chinner struct xfs_buf *agibp;
262130f712c9SDave Chinner struct xfs_agi *agi;
2622a95fee40SDave Chinner int error;
262399b13c7fSDave Chinner
262430f712c9SDave Chinner trace_xfs_ialloc_read_agi(pag->pag_mount, pag->pag_agno);
262530f712c9SDave Chinner
262699b13c7fSDave Chinner error = xfs_read_agi(pag, tp, &agibp);
262730f712c9SDave Chinner if (error)
262861021debSDave Chinner return error;
262930f712c9SDave Chinner
263030f712c9SDave Chinner agi = agibp->b_addr;
263130f712c9SDave Chinner if (!xfs_perag_initialised_agi(pag)) {
2632a95fee40SDave Chinner pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
26337ac2ff8bSDave Chinner pag->pagi_count = be32_to_cpu(agi->agi_count);
263430f712c9SDave Chinner set_bit(XFS_AGSTATE_AGI_INIT, &pag->pag_opstate);
263530f712c9SDave Chinner }
26367ac2ff8bSDave Chinner
263730f712c9SDave Chinner /*
263830f712c9SDave Chinner * It's possible for these to be out of sync if
263930f712c9SDave Chinner * we are in the middle of a forced shutdown.
264030f712c9SDave Chinner */
264130f712c9SDave Chinner ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) ||
264230f712c9SDave Chinner xfs_is_shutdown(pag->pag_mount));
264330f712c9SDave Chinner if (agibpp)
264499b13c7fSDave Chinner *agibpp = agibp;
2645a95fee40SDave Chinner else
2646a95fee40SDave Chinner xfs_trans_brelse(tp, agibp);
2647a95fee40SDave Chinner return 0;
2648a95fee40SDave Chinner }
264930f712c9SDave Chinner
265030f712c9SDave Chinner /* How many inodes are backed by inode clusters ondisk? */
265191fb9afcSDarrick J. Wong STATIC int
xfs_ialloc_count_ondisk(struct xfs_btree_cur * cur,xfs_agino_t low,xfs_agino_t high,unsigned int * allocated)2652efc0845fSDarrick J. Wong xfs_ialloc_count_ondisk(
2653efc0845fSDarrick J. Wong struct xfs_btree_cur *cur,
2654efc0845fSDarrick J. Wong xfs_agino_t low,
26552e001266SDarrick J. Wong xfs_agino_t high,
26562e001266SDarrick J. Wong unsigned int *allocated)
26572e001266SDarrick J. Wong {
2658efc0845fSDarrick J. Wong struct xfs_inobt_rec_incore irec;
26592e001266SDarrick J. Wong unsigned int ret = 0;
26602e001266SDarrick J. Wong int has_record;
2661efc0845fSDarrick J. Wong int error;
26622e001266SDarrick J. Wong
26632e001266SDarrick J. Wong error = xfs_inobt_lookup(cur, low, XFS_LOOKUP_LE, &has_record);
26642e001266SDarrick J. Wong if (error)
26652e001266SDarrick J. Wong return error;
2666efc0845fSDarrick J. Wong
2667efc0845fSDarrick J. Wong while (has_record) {
2668efc0845fSDarrick J. Wong unsigned int i, hole_idx;
2669efc0845fSDarrick J. Wong
2670efc0845fSDarrick J. Wong error = xfs_inobt_get_rec(cur, &irec, &has_record);
2671efc0845fSDarrick J. Wong if (error)
26722e001266SDarrick J. Wong return error;
2673efc0845fSDarrick J. Wong if (irec.ir_startino > high)
2674efc0845fSDarrick J. Wong break;
2675efc0845fSDarrick J. Wong
26762e001266SDarrick J. Wong for (i = 0; i < XFS_INODES_PER_CHUNK; i++) {
26772e001266SDarrick J. Wong if (irec.ir_startino + i < low)
2678efc0845fSDarrick J. Wong continue;
2679efc0845fSDarrick J. Wong if (irec.ir_startino + i > high)
26802e001266SDarrick J. Wong break;
2681efc0845fSDarrick J. Wong
2682efc0845fSDarrick J. Wong hole_idx = i / XFS_INODES_PER_HOLEMASK_BIT;
2683efc0845fSDarrick J. Wong if (!(irec.ir_holemask & (1U << hole_idx)))
2684efc0845fSDarrick J. Wong ret++;
2685efc0845fSDarrick J. Wong }
2686efc0845fSDarrick J. Wong
26872e001266SDarrick J. Wong error = xfs_btree_increment(cur, 0, &has_record);
26882e001266SDarrick J. Wong if (error)
26892e001266SDarrick J. Wong return error;
2690efc0845fSDarrick J. Wong }
26912e001266SDarrick J. Wong
26922e001266SDarrick J. Wong *allocated = ret;
26932e001266SDarrick J. Wong return 0;
2694efc0845fSDarrick J. Wong }
2695efc0845fSDarrick J. Wong
2696efc0845fSDarrick J. Wong /* Is there an inode record covering a given extent? */
2697efc0845fSDarrick J. Wong int
xfs_ialloc_has_inodes_at_extent(struct xfs_btree_cur * cur,xfs_agblock_t bno,xfs_extlen_t len,enum xbtree_recpacking * outcome)26982e001266SDarrick J. Wong xfs_ialloc_has_inodes_at_extent(
26992e001266SDarrick J. Wong struct xfs_btree_cur *cur,
27002e001266SDarrick J. Wong xfs_agblock_t bno,
27012e001266SDarrick J. Wong xfs_extlen_t len,
27022e001266SDarrick J. Wong enum xbtree_recpacking *outcome)
27032e001266SDarrick J. Wong {
2704efc0845fSDarrick J. Wong xfs_agino_t agino;
27052e001266SDarrick J. Wong xfs_agino_t last_agino;
2706efc0845fSDarrick J. Wong unsigned int allocated;
2707efc0845fSDarrick J. Wong int error;
2708efc0845fSDarrick J. Wong
2709efc0845fSDarrick J. Wong agino = XFS_AGB_TO_AGINO(cur->bc_mp, bno);
27102e001266SDarrick J. Wong last_agino = XFS_AGB_TO_AGINO(cur->bc_mp, bno + len) - 1;
2711efc0845fSDarrick J. Wong
2712efc0845fSDarrick J. Wong error = xfs_ialloc_count_ondisk(cur, agino, last_agino, &allocated);
27132e001266SDarrick J. Wong if (error)
2714efc0845fSDarrick J. Wong return error;
2715efc0845fSDarrick J. Wong
2716efc0845fSDarrick J. Wong if (allocated == 0)
2717efc0845fSDarrick J. Wong *outcome = XBTREE_RECPACKING_EMPTY;
2718efc0845fSDarrick J. Wong else if (allocated == last_agino - agino + 1)
2719efc0845fSDarrick J. Wong *outcome = XBTREE_RECPACKING_FULL;
2720efc0845fSDarrick J. Wong else
2721efc0845fSDarrick J. Wong *outcome = XBTREE_RECPACKING_SPARSE;
2722efc0845fSDarrick J. Wong return 0;
2723efc0845fSDarrick J. Wong }
2724efc0845fSDarrick J. Wong
27252e001266SDarrick J. Wong struct xfs_ialloc_count_inodes {
27262e001266SDarrick J. Wong xfs_agino_t count;
27272e001266SDarrick J. Wong xfs_agino_t freecount;
27282e001266SDarrick J. Wong };
27292e001266SDarrick J. Wong
27302e001266SDarrick J. Wong /* Record inode counts across all inobt records. */
27312e001266SDarrick J. Wong STATIC int
xfs_ialloc_count_inodes_rec(struct xfs_btree_cur * cur,const union xfs_btree_rec * rec,void * priv)27322e001266SDarrick J. Wong xfs_ialloc_count_inodes_rec(
27332e001266SDarrick J. Wong struct xfs_btree_cur *cur,
27342e001266SDarrick J. Wong const union xfs_btree_rec *rec,
27352e001266SDarrick J. Wong void *priv)
2736159eb69dSDarrick J. Wong {
27372e001266SDarrick J. Wong struct xfs_inobt_rec_incore irec;
27382e001266SDarrick J. Wong struct xfs_ialloc_count_inodes *ci = priv;
27392e001266SDarrick J. Wong xfs_failaddr_t fa;
27402e001266SDarrick J. Wong
2741ee12eaaaSDarrick J. Wong xfs_inobt_btrec_to_irec(cur->bc_mp, rec, &irec);
27422e001266SDarrick J. Wong fa = xfs_inobt_check_irec(cur, &irec);
27432e001266SDarrick J. Wong if (fa)
2744ee12eaaaSDarrick J. Wong return xfs_inobt_complain_bad_rec(cur, fa, &irec);
2745ee12eaaaSDarrick J. Wong
2746ee12eaaaSDarrick J. Wong ci->count += irec.ir_count;
2747366a0b8dSDarrick J. Wong ci->freecount += irec.ir_freecount;
27482e001266SDarrick J. Wong
27492e001266SDarrick J. Wong return 0;
27502e001266SDarrick J. Wong }
27512e001266SDarrick J. Wong
27522e001266SDarrick J. Wong /* Count allocated and free inodes under an inobt. */
27532e001266SDarrick J. Wong int
xfs_ialloc_count_inodes(struct xfs_btree_cur * cur,xfs_agino_t * count,xfs_agino_t * freecount)27542e001266SDarrick J. Wong xfs_ialloc_count_inodes(
27552e001266SDarrick J. Wong struct xfs_btree_cur *cur,
27562e001266SDarrick J. Wong xfs_agino_t *count,
27572e001266SDarrick J. Wong xfs_agino_t *freecount)
27582e001266SDarrick J. Wong {
27592e001266SDarrick J. Wong struct xfs_ialloc_count_inodes ci = {0};
27602e001266SDarrick J. Wong int error;
27612e001266SDarrick J. Wong
27622e001266SDarrick J. Wong ASSERT(cur->bc_btnum == XFS_BTNUM_INO);
27632e001266SDarrick J. Wong error = xfs_btree_query_all(cur, xfs_ialloc_count_inodes_rec, &ci);
27642e001266SDarrick J. Wong if (error)
27652e001266SDarrick J. Wong return error;
27662e001266SDarrick J. Wong
27672e001266SDarrick J. Wong *count = ci.count;
27682e001266SDarrick J. Wong *freecount = ci.freecount;
27692e001266SDarrick J. Wong return 0;
27702e001266SDarrick J. Wong }
27712e001266SDarrick J. Wong
27722e001266SDarrick J. Wong /*
2773494dba7bSDarrick J. Wong * Initialize inode-related geometry information.
2774494dba7bSDarrick J. Wong *
2775494dba7bSDarrick J. Wong * Compute the inode btree min and max levels and set maxicount.
2776494dba7bSDarrick J. Wong *
2777494dba7bSDarrick J. Wong * Set the inode cluster size. This may still be overridden by the file
2778494dba7bSDarrick J. Wong * system block size if it is larger than the chosen cluster size.
2779494dba7bSDarrick J. Wong *
2780494dba7bSDarrick J. Wong * For v5 filesystems, scale the cluster size with the inode size to keep a
2781494dba7bSDarrick J. Wong * constant ratio of inode per cluster buffer, but only if mkfs has set the
2782494dba7bSDarrick J. Wong * inode alignment value appropriately for larger cluster sizes.
2783494dba7bSDarrick J. Wong *
2784494dba7bSDarrick J. Wong * Then compute the inode cluster alignment information.
2785494dba7bSDarrick J. Wong */
2786494dba7bSDarrick J. Wong void
xfs_ialloc_setup_geometry(struct xfs_mount * mp)2787494dba7bSDarrick J. Wong xfs_ialloc_setup_geometry(
2788494dba7bSDarrick J. Wong struct xfs_mount *mp)
2789494dba7bSDarrick J. Wong {
2790494dba7bSDarrick J. Wong struct xfs_sb *sbp = &mp->m_sb;
2791494dba7bSDarrick J. Wong struct xfs_ino_geometry *igeo = M_IGEO(mp);
2792494dba7bSDarrick J. Wong uint64_t icount;
2793494dba7bSDarrick J. Wong uint inodes;
2794494dba7bSDarrick J. Wong
2795494dba7bSDarrick J. Wong igeo->new_diflags2 = 0;
2796494dba7bSDarrick J. Wong if (xfs_has_bigtime(mp))
2797f93e5436SDarrick J. Wong igeo->new_diflags2 |= XFS_DIFLAG2_BIGTIME;
2798ebd9027dSDave Chinner if (xfs_has_large_extent_counts(mp))
2799f93e5436SDarrick J. Wong igeo->new_diflags2 |= XFS_DIFLAG2_NREXT64;
28009b7d16e3SChandan Babu R
28019b7d16e3SChandan Babu R /* Compute inode btree geometry. */
2802f93e5436SDarrick J. Wong igeo->agino_log = sbp->sb_inopblog + sbp->sb_agblklog;
2803494dba7bSDarrick J. Wong igeo->inobt_mxr[0] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 1);
2804494dba7bSDarrick J. Wong igeo->inobt_mxr[1] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 0);
2805494dba7bSDarrick J. Wong igeo->inobt_mnr[0] = igeo->inobt_mxr[0] / 2;
2806494dba7bSDarrick J. Wong igeo->inobt_mnr[1] = igeo->inobt_mxr[1] / 2;
2807494dba7bSDarrick J. Wong
2808494dba7bSDarrick J. Wong igeo->ialloc_inos = max_t(uint16_t, XFS_INODES_PER_CHUNK,
2809494dba7bSDarrick J. Wong sbp->sb_inopblock);
2810494dba7bSDarrick J. Wong igeo->ialloc_blks = igeo->ialloc_inos >> sbp->sb_inopblog;
2811494dba7bSDarrick J. Wong
2812494dba7bSDarrick J. Wong if (sbp->sb_spino_align)
2813494dba7bSDarrick J. Wong igeo->ialloc_min_blks = sbp->sb_spino_align;
2814494dba7bSDarrick J. Wong else
2815494dba7bSDarrick J. Wong igeo->ialloc_min_blks = igeo->ialloc_blks;
2816494dba7bSDarrick J. Wong
2817494dba7bSDarrick J. Wong /* Compute and fill in value of m_ino_geo.inobt_maxlevels. */
2818494dba7bSDarrick J. Wong inodes = (1LL << XFS_INO_AGINO_BITS(mp)) >> XFS_INODES_PER_CHUNK_LOG;
2819494dba7bSDarrick J. Wong igeo->inobt_maxlevels = xfs_btree_compute_maxlevels(igeo->inobt_mnr,
2820494dba7bSDarrick J. Wong inodes);
2821494dba7bSDarrick J. Wong ASSERT(igeo->inobt_maxlevels <= xfs_iallocbt_maxlevels_ondisk());
2822494dba7bSDarrick J. Wong
28230ed5f735SDarrick J. Wong /*
2824494dba7bSDarrick J. Wong * Set the maximum inode count for this filesystem, being careful not
2825c94613feSDarrick J. Wong * to use obviously garbage sb_inopblog/sb_inopblock values. Regular
2826c94613feSDarrick J. Wong * users should never get here due to failing sb verification, but
2827c94613feSDarrick J. Wong * certain users (xfs_db) need to be usable even with corrupt metadata.
2828c94613feSDarrick J. Wong */
2829c94613feSDarrick J. Wong if (sbp->sb_imax_pct && igeo->ialloc_blks) {
2830c94613feSDarrick J. Wong /*
2831c94613feSDarrick J. Wong * Make sure the maximum inode count is a multiple
2832494dba7bSDarrick J. Wong * of the units we allocate inodes in.
2833494dba7bSDarrick J. Wong */
2834494dba7bSDarrick J. Wong icount = sbp->sb_dblocks * sbp->sb_imax_pct;
2835494dba7bSDarrick J. Wong do_div(icount, 100);
2836494dba7bSDarrick J. Wong do_div(icount, igeo->ialloc_blks);
2837494dba7bSDarrick J. Wong igeo->maxicount = XFS_FSB_TO_INO(mp,
2838494dba7bSDarrick J. Wong icount * igeo->ialloc_blks);
2839494dba7bSDarrick J. Wong } else {
2840494dba7bSDarrick J. Wong igeo->maxicount = 0;
2841494dba7bSDarrick J. Wong }
2842494dba7bSDarrick J. Wong
2843494dba7bSDarrick J. Wong /*
2844494dba7bSDarrick J. Wong * Compute the desired size of an inode cluster buffer size, which
2845490d451fSDarrick J. Wong * starts at 8K and (on v5 filesystems) scales up with larger inode
2846490d451fSDarrick J. Wong * sizes.
2847490d451fSDarrick J. Wong *
2848490d451fSDarrick J. Wong * Preserve the desired inode cluster size because the sparse inodes
2849490d451fSDarrick J. Wong * feature uses that desired size (not the actual size) to compute the
2850490d451fSDarrick J. Wong * sparse inode alignment. The mount code validates this value, so we
2851490d451fSDarrick J. Wong * cannot change the behavior.
2852490d451fSDarrick J. Wong */
2853490d451fSDarrick J. Wong igeo->inode_cluster_size_raw = XFS_INODE_BIG_CLUSTER_SIZE;
2854490d451fSDarrick J. Wong if (xfs_has_v3inodes(mp)) {
2855490d451fSDarrick J. Wong int new_size = igeo->inode_cluster_size_raw;
2856ebd9027dSDave Chinner
2857490d451fSDarrick J. Wong new_size *= mp->m_sb.sb_inodesize / XFS_DINODE_MIN_SIZE;
2858494dba7bSDarrick J. Wong if (mp->m_sb.sb_inoalignmt >= XFS_B_TO_FSBT(mp, new_size))
2859494dba7bSDarrick J. Wong igeo->inode_cluster_size_raw = new_size;
2860494dba7bSDarrick J. Wong }
2861490d451fSDarrick J. Wong
2862494dba7bSDarrick J. Wong /* Calculate inode cluster ratios. */
2863494dba7bSDarrick J. Wong if (igeo->inode_cluster_size_raw > mp->m_sb.sb_blocksize)
2864494dba7bSDarrick J. Wong igeo->blocks_per_cluster = XFS_B_TO_FSBT(mp,
2865490d451fSDarrick J. Wong igeo->inode_cluster_size_raw);
2866494dba7bSDarrick J. Wong else
2867490d451fSDarrick J. Wong igeo->blocks_per_cluster = 1;
2868494dba7bSDarrick J. Wong igeo->inode_cluster_size = XFS_FSB_TO_B(mp, igeo->blocks_per_cluster);
2869494dba7bSDarrick J. Wong igeo->inodes_per_cluster = XFS_FSB_TO_INO(mp, igeo->blocks_per_cluster);
2870490d451fSDarrick J. Wong
2871494dba7bSDarrick J. Wong /* Calculate inode cluster alignment. */
2872494dba7bSDarrick J. Wong if (xfs_has_align(mp) &&
2873494dba7bSDarrick J. Wong mp->m_sb.sb_inoalignmt >= igeo->blocks_per_cluster)
2874ebd9027dSDave Chinner igeo->cluster_align = mp->m_sb.sb_inoalignmt;
2875494dba7bSDarrick J. Wong else
2876494dba7bSDarrick J. Wong igeo->cluster_align = 1;
2877494dba7bSDarrick J. Wong igeo->inoalign_mask = igeo->cluster_align - 1;
2878494dba7bSDarrick J. Wong igeo->cluster_align_inodes = XFS_FSB_TO_INO(mp, igeo->cluster_align);
2879494dba7bSDarrick J. Wong
2880494dba7bSDarrick J. Wong /*
2881494dba7bSDarrick J. Wong * If we are using stripe alignment, check whether
2882494dba7bSDarrick J. Wong * the stripe unit is a multiple of the inode alignment
2883494dba7bSDarrick J. Wong */
2884494dba7bSDarrick J. Wong if (mp->m_dalign && igeo->inoalign_mask &&
2885494dba7bSDarrick J. Wong !(mp->m_dalign & igeo->inoalign_mask))
2886494dba7bSDarrick J. Wong igeo->ialloc_align = mp->m_dalign;
2887494dba7bSDarrick J. Wong else
2888494dba7bSDarrick J. Wong igeo->ialloc_align = 0;
2889494dba7bSDarrick J. Wong }
2890494dba7bSDarrick J. Wong
2891494dba7bSDarrick J. Wong /* Compute the location of the root directory inode that is laid out by mkfs. */
289213eaec4bSDarrick J. Wong xfs_ino_t
xfs_ialloc_calc_rootino(struct xfs_mount * mp,int sunit)289313eaec4bSDarrick J. Wong xfs_ialloc_calc_rootino(
289413eaec4bSDarrick J. Wong struct xfs_mount *mp,
289513eaec4bSDarrick J. Wong int sunit)
289613eaec4bSDarrick J. Wong {
289713eaec4bSDarrick J. Wong struct xfs_ino_geometry *igeo = M_IGEO(mp);
289813eaec4bSDarrick J. Wong xfs_agblock_t first_bno;
289913eaec4bSDarrick J. Wong
290013eaec4bSDarrick J. Wong /*
290113eaec4bSDarrick J. Wong * Pre-calculate the geometry of AG 0. We know what it looks like
290213eaec4bSDarrick J. Wong * because libxfs knows how to create allocation groups now.
290313eaec4bSDarrick J. Wong *
290413eaec4bSDarrick J. Wong * first_bno is the first block in which mkfs could possibly have
290513eaec4bSDarrick J. Wong * allocated the root directory inode, once we factor in the metadata
290613eaec4bSDarrick J. Wong * that mkfs formats before it. Namely, the four AG headers...
290713eaec4bSDarrick J. Wong */
290813eaec4bSDarrick J. Wong first_bno = howmany(4 * mp->m_sb.sb_sectsize, mp->m_sb.sb_blocksize);
290913eaec4bSDarrick J. Wong
291013eaec4bSDarrick J. Wong /* ...the two free space btree roots... */
291113eaec4bSDarrick J. Wong first_bno += 2;
291213eaec4bSDarrick J. Wong
291313eaec4bSDarrick J. Wong /* ...the inode btree root... */
291413eaec4bSDarrick J. Wong first_bno += 1;
291513eaec4bSDarrick J. Wong
291613eaec4bSDarrick J. Wong /* ...the initial AGFL... */
291713eaec4bSDarrick J. Wong first_bno += xfs_alloc_min_freelist(mp, NULL);
291813eaec4bSDarrick J. Wong
291913eaec4bSDarrick J. Wong /* ...the free inode btree root... */
292013eaec4bSDarrick J. Wong if (xfs_has_finobt(mp))
292113eaec4bSDarrick J. Wong first_bno++;
2922ebd9027dSDave Chinner
292313eaec4bSDarrick J. Wong /* ...the reverse mapping btree root... */
292413eaec4bSDarrick J. Wong if (xfs_has_rmapbt(mp))
292513eaec4bSDarrick J. Wong first_bno++;
2926ebd9027dSDave Chinner
292713eaec4bSDarrick J. Wong /* ...the reference count btree... */
292813eaec4bSDarrick J. Wong if (xfs_has_reflink(mp))
292913eaec4bSDarrick J. Wong first_bno++;
2930ebd9027dSDave Chinner
293113eaec4bSDarrick J. Wong /*
293213eaec4bSDarrick J. Wong * ...and the log, if it is allocated in the first allocation group.
293313eaec4bSDarrick J. Wong *
293413eaec4bSDarrick J. Wong * This can happen with filesystems that only have a single
293513eaec4bSDarrick J. Wong * allocation group, or very odd geometries created by old mkfs
293613eaec4bSDarrick J. Wong * versions on very small filesystems.
293713eaec4bSDarrick J. Wong */
293813eaec4bSDarrick J. Wong if (xfs_ag_contains_log(mp, 0))
293913eaec4bSDarrick J. Wong first_bno += mp->m_sb.sb_logblocks;
294036029deeSDave Chinner
294113eaec4bSDarrick J. Wong /*
294213eaec4bSDarrick J. Wong * Now round first_bno up to whatever allocation alignment is given
294313eaec4bSDarrick J. Wong * by the filesystem or was passed in.
294413eaec4bSDarrick J. Wong */
294513eaec4bSDarrick J. Wong if (xfs_has_dalign(mp) && igeo->ialloc_align > 0)
294613eaec4bSDarrick J. Wong first_bno = roundup(first_bno, sunit);
2947ebd9027dSDave Chinner else if (xfs_has_align(mp) &&
294813eaec4bSDarrick J. Wong mp->m_sb.sb_inoalignmt > 1)
2949ebd9027dSDave Chinner first_bno = roundup(first_bno, mp->m_sb.sb_inoalignmt);
295013eaec4bSDarrick J. Wong
295113eaec4bSDarrick J. Wong return XFS_AGINO_TO_INO(mp, 0, XFS_AGB_TO_AGINO(mp, first_bno));
295213eaec4bSDarrick J. Wong }
295313eaec4bSDarrick J. Wong
295413eaec4bSDarrick J. Wong /*
2955da062d16SDarrick J. Wong * Ensure there are not sparse inode clusters that cross the new EOAG.
2956da062d16SDarrick J. Wong *
2957da062d16SDarrick J. Wong * This is a no-op for non-spinode filesystems since clusters are always fully
2958da062d16SDarrick J. Wong * allocated and checking the bnobt suffices. However, a spinode filesystem
2959da062d16SDarrick J. Wong * could have a record where the upper inodes are free blocks. If those blocks
2960da062d16SDarrick J. Wong * were removed from the filesystem, the inode record would extend beyond EOAG,
2961da062d16SDarrick J. Wong * which will be flagged as corruption.
2962da062d16SDarrick J. Wong */
2963da062d16SDarrick J. Wong int
xfs_ialloc_check_shrink(struct xfs_perag * pag,struct xfs_trans * tp,struct xfs_buf * agibp,xfs_agblock_t new_length)2964da062d16SDarrick J. Wong xfs_ialloc_check_shrink(
2965da062d16SDarrick J. Wong struct xfs_perag *pag,
2966da062d16SDarrick J. Wong struct xfs_trans *tp,
2967dedab3e4SDave Chinner struct xfs_buf *agibp,
2968da062d16SDarrick J. Wong xfs_agblock_t new_length)
2969da062d16SDarrick J. Wong {
2970da062d16SDarrick J. Wong struct xfs_inobt_rec_incore rec;
2971da062d16SDarrick J. Wong struct xfs_btree_cur *cur;
2972da062d16SDarrick J. Wong xfs_agino_t agino;
2973da062d16SDarrick J. Wong int has;
2974bab8b795SDave Chinner int error;
2975da062d16SDarrick J. Wong
2976da062d16SDarrick J. Wong if (!xfs_has_sparseinodes(pag->pag_mount))
2977da062d16SDarrick J. Wong return 0;
2978bab8b795SDave Chinner
2979da062d16SDarrick J. Wong cur = xfs_inobt_init_cursor(pag, tp, agibp, XFS_BTNUM_INO);
2980da062d16SDarrick J. Wong
2981bab8b795SDave Chinner /* Look up the inobt record that would correspond to the new EOFS. */
2982da062d16SDarrick J. Wong agino = XFS_AGB_TO_AGINO(pag->pag_mount, new_length);
2983da062d16SDarrick J. Wong error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &has);
2984bab8b795SDave Chinner if (error || !has)
2985da062d16SDarrick J. Wong goto out;
2986da062d16SDarrick J. Wong
2987da062d16SDarrick J. Wong error = xfs_inobt_get_rec(cur, &rec, &has);
2988da062d16SDarrick J. Wong if (error)
2989da062d16SDarrick J. Wong goto out;
2990da062d16SDarrick J. Wong
2991da062d16SDarrick J. Wong if (!has) {
2992da062d16SDarrick J. Wong error = -EFSCORRUPTED;
2993da062d16SDarrick J. Wong goto out;
2994da062d16SDarrick J. Wong }
2995da062d16SDarrick J. Wong
2996da062d16SDarrick J. Wong /* If the record covers inodes that would be beyond EOFS, bail out. */
2997da062d16SDarrick J. Wong if (rec.ir_startino + XFS_INODES_PER_CHUNK > agino) {
2998da062d16SDarrick J. Wong error = -ENOSPC;
2999da062d16SDarrick J. Wong goto out;
3000da062d16SDarrick J. Wong }
3001da062d16SDarrick J. Wong out:
3002da062d16SDarrick J. Wong xfs_btree_del_cursor(cur, error);
3003da062d16SDarrick J. Wong return error;
3004da062d16SDarrick J. Wong }
3005da062d16SDarrick J. Wong