xref: /openbmc/linux/fs/xfs/libxfs/xfs_ialloc.c (revision bc1a09b8e334bf5fca1d6727aec538dcff957961)
130f712c9SDave Chinner /*
230f712c9SDave Chinner  * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
330f712c9SDave Chinner  * All Rights Reserved.
430f712c9SDave Chinner  *
530f712c9SDave Chinner  * This program is free software; you can redistribute it and/or
630f712c9SDave Chinner  * modify it under the terms of the GNU General Public License as
730f712c9SDave Chinner  * published by the Free Software Foundation.
830f712c9SDave Chinner  *
930f712c9SDave Chinner  * This program is distributed in the hope that it would be useful,
1030f712c9SDave Chinner  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1130f712c9SDave Chinner  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1230f712c9SDave Chinner  * GNU General Public License for more details.
1330f712c9SDave Chinner  *
1430f712c9SDave Chinner  * You should have received a copy of the GNU General Public License
1530f712c9SDave Chinner  * along with this program; if not, write the Free Software Foundation,
1630f712c9SDave Chinner  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1730f712c9SDave Chinner  */
1830f712c9SDave Chinner #include "xfs.h"
1930f712c9SDave Chinner #include "xfs_fs.h"
2030f712c9SDave Chinner #include "xfs_shared.h"
2130f712c9SDave Chinner #include "xfs_format.h"
2230f712c9SDave Chinner #include "xfs_log_format.h"
2330f712c9SDave Chinner #include "xfs_trans_resv.h"
2430f712c9SDave Chinner #include "xfs_bit.h"
2530f712c9SDave Chinner #include "xfs_sb.h"
2630f712c9SDave Chinner #include "xfs_mount.h"
273ab78df2SDarrick J. Wong #include "xfs_defer.h"
2830f712c9SDave Chinner #include "xfs_inode.h"
2930f712c9SDave Chinner #include "xfs_btree.h"
3030f712c9SDave Chinner #include "xfs_ialloc.h"
3130f712c9SDave Chinner #include "xfs_ialloc_btree.h"
3230f712c9SDave Chinner #include "xfs_alloc.h"
3330f712c9SDave Chinner #include "xfs_rtalloc.h"
34e9e899a2SDarrick J. Wong #include "xfs_errortag.h"
3530f712c9SDave Chinner #include "xfs_error.h"
3630f712c9SDave Chinner #include "xfs_bmap.h"
3730f712c9SDave Chinner #include "xfs_cksum.h"
3830f712c9SDave Chinner #include "xfs_trans.h"
3930f712c9SDave Chinner #include "xfs_buf_item.h"
4030f712c9SDave Chinner #include "xfs_icreate_item.h"
4130f712c9SDave Chinner #include "xfs_icache.h"
4230f712c9SDave Chinner #include "xfs_trace.h"
43a45086e2SBrian Foster #include "xfs_log.h"
44340785ccSDarrick J. Wong #include "xfs_rmap.h"
4530f712c9SDave Chinner 
4630f712c9SDave Chinner 
4730f712c9SDave Chinner /*
4830f712c9SDave Chinner  * Allocation group level functions.
4930f712c9SDave Chinner  */
50e936945eSDarrick J. Wong int
5130f712c9SDave Chinner xfs_ialloc_cluster_alignment(
527a1df156SDave Chinner 	struct xfs_mount	*mp)
5330f712c9SDave Chinner {
547a1df156SDave Chinner 	if (xfs_sb_version_hasalign(&mp->m_sb) &&
558ee9fdbeSChandan Rajendra 	    mp->m_sb.sb_inoalignmt >= xfs_icluster_size_fsb(mp))
567a1df156SDave Chinner 		return mp->m_sb.sb_inoalignmt;
5730f712c9SDave Chinner 	return 1;
5830f712c9SDave Chinner }
5930f712c9SDave Chinner 
6030f712c9SDave Chinner /*
6130f712c9SDave Chinner  * Lookup a record by ino in the btree given by cur.
6230f712c9SDave Chinner  */
6330f712c9SDave Chinner int					/* error */
6430f712c9SDave Chinner xfs_inobt_lookup(
6530f712c9SDave Chinner 	struct xfs_btree_cur	*cur,	/* btree cursor */
6630f712c9SDave Chinner 	xfs_agino_t		ino,	/* starting inode of chunk */
6730f712c9SDave Chinner 	xfs_lookup_t		dir,	/* <=, >=, == */
6830f712c9SDave Chinner 	int			*stat)	/* success/failure */
6930f712c9SDave Chinner {
7030f712c9SDave Chinner 	cur->bc_rec.i.ir_startino = ino;
715419040fSBrian Foster 	cur->bc_rec.i.ir_holemask = 0;
725419040fSBrian Foster 	cur->bc_rec.i.ir_count = 0;
7330f712c9SDave Chinner 	cur->bc_rec.i.ir_freecount = 0;
7430f712c9SDave Chinner 	cur->bc_rec.i.ir_free = 0;
7530f712c9SDave Chinner 	return xfs_btree_lookup(cur, dir, stat);
7630f712c9SDave Chinner }
7730f712c9SDave Chinner 
7830f712c9SDave Chinner /*
7930f712c9SDave Chinner  * Update the record referred to by cur to the value given.
8030f712c9SDave Chinner  * This either works (return 0) or gets an EFSCORRUPTED error.
8130f712c9SDave Chinner  */
8230f712c9SDave Chinner STATIC int				/* error */
8330f712c9SDave Chinner xfs_inobt_update(
8430f712c9SDave Chinner 	struct xfs_btree_cur	*cur,	/* btree cursor */
8530f712c9SDave Chinner 	xfs_inobt_rec_incore_t	*irec)	/* btree record */
8630f712c9SDave Chinner {
8730f712c9SDave Chinner 	union xfs_btree_rec	rec;
8830f712c9SDave Chinner 
8930f712c9SDave Chinner 	rec.inobt.ir_startino = cpu_to_be32(irec->ir_startino);
905419040fSBrian Foster 	if (xfs_sb_version_hassparseinodes(&cur->bc_mp->m_sb)) {
915419040fSBrian Foster 		rec.inobt.ir_u.sp.ir_holemask = cpu_to_be16(irec->ir_holemask);
925419040fSBrian Foster 		rec.inobt.ir_u.sp.ir_count = irec->ir_count;
935419040fSBrian Foster 		rec.inobt.ir_u.sp.ir_freecount = irec->ir_freecount;
945419040fSBrian Foster 	} else {
955419040fSBrian Foster 		/* ir_holemask/ir_count not supported on-disk */
965419040fSBrian Foster 		rec.inobt.ir_u.f.ir_freecount = cpu_to_be32(irec->ir_freecount);
975419040fSBrian Foster 	}
9830f712c9SDave Chinner 	rec.inobt.ir_free = cpu_to_be64(irec->ir_free);
9930f712c9SDave Chinner 	return xfs_btree_update(cur, &rec);
10030f712c9SDave Chinner }
10130f712c9SDave Chinner 
102e936945eSDarrick J. Wong /* Convert on-disk btree record to incore inobt record. */
103e936945eSDarrick J. Wong void
104e936945eSDarrick J. Wong xfs_inobt_btrec_to_irec(
105e936945eSDarrick J. Wong 	struct xfs_mount		*mp,
106e936945eSDarrick J. Wong 	union xfs_btree_rec		*rec,
107e936945eSDarrick J. Wong 	struct xfs_inobt_rec_incore	*irec)
10830f712c9SDave Chinner {
1095419040fSBrian Foster 	irec->ir_startino = be32_to_cpu(rec->inobt.ir_startino);
110e936945eSDarrick J. Wong 	if (xfs_sb_version_hassparseinodes(&mp->m_sb)) {
1115419040fSBrian Foster 		irec->ir_holemask = be16_to_cpu(rec->inobt.ir_u.sp.ir_holemask);
1125419040fSBrian Foster 		irec->ir_count = rec->inobt.ir_u.sp.ir_count;
1135419040fSBrian Foster 		irec->ir_freecount = rec->inobt.ir_u.sp.ir_freecount;
1145419040fSBrian Foster 	} else {
1155419040fSBrian Foster 		/*
1165419040fSBrian Foster 		 * ir_holemask/ir_count not supported on-disk. Fill in hardcoded
1175419040fSBrian Foster 		 * values for full inode chunks.
1185419040fSBrian Foster 		 */
1195419040fSBrian Foster 		irec->ir_holemask = XFS_INOBT_HOLEMASK_FULL;
1205419040fSBrian Foster 		irec->ir_count = XFS_INODES_PER_CHUNK;
1215419040fSBrian Foster 		irec->ir_freecount =
1225419040fSBrian Foster 				be32_to_cpu(rec->inobt.ir_u.f.ir_freecount);
1235419040fSBrian Foster 	}
1245419040fSBrian Foster 	irec->ir_free = be64_to_cpu(rec->inobt.ir_free);
125e936945eSDarrick J. Wong }
126e936945eSDarrick J. Wong 
127e936945eSDarrick J. Wong /*
128e936945eSDarrick J. Wong  * Get the data from the pointed-to record.
129e936945eSDarrick J. Wong  */
130e936945eSDarrick J. Wong int
131e936945eSDarrick J. Wong xfs_inobt_get_rec(
132e936945eSDarrick J. Wong 	struct xfs_btree_cur		*cur,
133e936945eSDarrick J. Wong 	struct xfs_inobt_rec_incore	*irec,
134e936945eSDarrick J. Wong 	int				*stat)
135e936945eSDarrick J. Wong {
136e936945eSDarrick J. Wong 	union xfs_btree_rec		*rec;
137e936945eSDarrick J. Wong 	int				error;
138e936945eSDarrick J. Wong 
139e936945eSDarrick J. Wong 	error = xfs_btree_get_rec(cur, &rec, stat);
140e936945eSDarrick J. Wong 	if (error || *stat == 0)
141e936945eSDarrick J. Wong 		return error;
142e936945eSDarrick J. Wong 
143e936945eSDarrick J. Wong 	xfs_inobt_btrec_to_irec(cur->bc_mp, rec, irec);
1445419040fSBrian Foster 
1455419040fSBrian Foster 	return 0;
14630f712c9SDave Chinner }
14730f712c9SDave Chinner 
14830f712c9SDave Chinner /*
14930f712c9SDave Chinner  * Insert a single inobt record. Cursor must already point to desired location.
15030f712c9SDave Chinner  */
15130f712c9SDave Chinner STATIC int
15230f712c9SDave Chinner xfs_inobt_insert_rec(
15330f712c9SDave Chinner 	struct xfs_btree_cur	*cur,
154c8ce540dSDarrick J. Wong 	uint16_t		holemask,
155c8ce540dSDarrick J. Wong 	uint8_t			count,
156c8ce540dSDarrick J. Wong 	int32_t			freecount,
15730f712c9SDave Chinner 	xfs_inofree_t		free,
15830f712c9SDave Chinner 	int			*stat)
15930f712c9SDave Chinner {
1605419040fSBrian Foster 	cur->bc_rec.i.ir_holemask = holemask;
1615419040fSBrian Foster 	cur->bc_rec.i.ir_count = count;
16230f712c9SDave Chinner 	cur->bc_rec.i.ir_freecount = freecount;
16330f712c9SDave Chinner 	cur->bc_rec.i.ir_free = free;
16430f712c9SDave Chinner 	return xfs_btree_insert(cur, stat);
16530f712c9SDave Chinner }
16630f712c9SDave Chinner 
16730f712c9SDave Chinner /*
16830f712c9SDave Chinner  * Insert records describing a newly allocated inode chunk into the inobt.
16930f712c9SDave Chinner  */
17030f712c9SDave Chinner STATIC int
17130f712c9SDave Chinner xfs_inobt_insert(
17230f712c9SDave Chinner 	struct xfs_mount	*mp,
17330f712c9SDave Chinner 	struct xfs_trans	*tp,
17430f712c9SDave Chinner 	struct xfs_buf		*agbp,
17530f712c9SDave Chinner 	xfs_agino_t		newino,
17630f712c9SDave Chinner 	xfs_agino_t		newlen,
17730f712c9SDave Chinner 	xfs_btnum_t		btnum)
17830f712c9SDave Chinner {
17930f712c9SDave Chinner 	struct xfs_btree_cur	*cur;
18030f712c9SDave Chinner 	struct xfs_agi		*agi = XFS_BUF_TO_AGI(agbp);
18130f712c9SDave Chinner 	xfs_agnumber_t		agno = be32_to_cpu(agi->agi_seqno);
18230f712c9SDave Chinner 	xfs_agino_t		thisino;
18330f712c9SDave Chinner 	int			i;
18430f712c9SDave Chinner 	int			error;
18530f712c9SDave Chinner 
18630f712c9SDave Chinner 	cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, btnum);
18730f712c9SDave Chinner 
18830f712c9SDave Chinner 	for (thisino = newino;
18930f712c9SDave Chinner 	     thisino < newino + newlen;
19030f712c9SDave Chinner 	     thisino += XFS_INODES_PER_CHUNK) {
19130f712c9SDave Chinner 		error = xfs_inobt_lookup(cur, thisino, XFS_LOOKUP_EQ, &i);
19230f712c9SDave Chinner 		if (error) {
19330f712c9SDave Chinner 			xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
19430f712c9SDave Chinner 			return error;
19530f712c9SDave Chinner 		}
19630f712c9SDave Chinner 		ASSERT(i == 0);
19730f712c9SDave Chinner 
1985419040fSBrian Foster 		error = xfs_inobt_insert_rec(cur, XFS_INOBT_HOLEMASK_FULL,
1995419040fSBrian Foster 					     XFS_INODES_PER_CHUNK,
2005419040fSBrian Foster 					     XFS_INODES_PER_CHUNK,
20130f712c9SDave Chinner 					     XFS_INOBT_ALL_FREE, &i);
20230f712c9SDave Chinner 		if (error) {
20330f712c9SDave Chinner 			xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
20430f712c9SDave Chinner 			return error;
20530f712c9SDave Chinner 		}
20630f712c9SDave Chinner 		ASSERT(i == 1);
20730f712c9SDave Chinner 	}
20830f712c9SDave Chinner 
20930f712c9SDave Chinner 	xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
21030f712c9SDave Chinner 
21130f712c9SDave Chinner 	return 0;
21230f712c9SDave Chinner }
21330f712c9SDave Chinner 
21430f712c9SDave Chinner /*
21530f712c9SDave Chinner  * Verify that the number of free inodes in the AGI is correct.
21630f712c9SDave Chinner  */
21730f712c9SDave Chinner #ifdef DEBUG
21830f712c9SDave Chinner STATIC int
21930f712c9SDave Chinner xfs_check_agi_freecount(
22030f712c9SDave Chinner 	struct xfs_btree_cur	*cur,
22130f712c9SDave Chinner 	struct xfs_agi		*agi)
22230f712c9SDave Chinner {
22330f712c9SDave Chinner 	if (cur->bc_nlevels == 1) {
22430f712c9SDave Chinner 		xfs_inobt_rec_incore_t rec;
22530f712c9SDave Chinner 		int		freecount = 0;
22630f712c9SDave Chinner 		int		error;
22730f712c9SDave Chinner 		int		i;
22830f712c9SDave Chinner 
22930f712c9SDave Chinner 		error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
23030f712c9SDave Chinner 		if (error)
23130f712c9SDave Chinner 			return error;
23230f712c9SDave Chinner 
23330f712c9SDave Chinner 		do {
23430f712c9SDave Chinner 			error = xfs_inobt_get_rec(cur, &rec, &i);
23530f712c9SDave Chinner 			if (error)
23630f712c9SDave Chinner 				return error;
23730f712c9SDave Chinner 
23830f712c9SDave Chinner 			if (i) {
23930f712c9SDave Chinner 				freecount += rec.ir_freecount;
24030f712c9SDave Chinner 				error = xfs_btree_increment(cur, 0, &i);
24130f712c9SDave Chinner 				if (error)
24230f712c9SDave Chinner 					return error;
24330f712c9SDave Chinner 			}
24430f712c9SDave Chinner 		} while (i == 1);
24530f712c9SDave Chinner 
24630f712c9SDave Chinner 		if (!XFS_FORCED_SHUTDOWN(cur->bc_mp))
24730f712c9SDave Chinner 			ASSERT(freecount == be32_to_cpu(agi->agi_freecount));
24830f712c9SDave Chinner 	}
24930f712c9SDave Chinner 	return 0;
25030f712c9SDave Chinner }
25130f712c9SDave Chinner #else
25230f712c9SDave Chinner #define xfs_check_agi_freecount(cur, agi)	0
25330f712c9SDave Chinner #endif
25430f712c9SDave Chinner 
25530f712c9SDave Chinner /*
25630f712c9SDave Chinner  * Initialise a new set of inodes. When called without a transaction context
25730f712c9SDave Chinner  * (e.g. from recovery) we initiate a delayed write of the inode buffers rather
25830f712c9SDave Chinner  * than logging them (which in a transaction context puts them into the AIL
25930f712c9SDave Chinner  * for writeback rather than the xfsbufd queue).
26030f712c9SDave Chinner  */
26130f712c9SDave Chinner int
26230f712c9SDave Chinner xfs_ialloc_inode_init(
26330f712c9SDave Chinner 	struct xfs_mount	*mp,
26430f712c9SDave Chinner 	struct xfs_trans	*tp,
26530f712c9SDave Chinner 	struct list_head	*buffer_list,
266463958afSBrian Foster 	int			icount,
26730f712c9SDave Chinner 	xfs_agnumber_t		agno,
26830f712c9SDave Chinner 	xfs_agblock_t		agbno,
26930f712c9SDave Chinner 	xfs_agblock_t		length,
27030f712c9SDave Chinner 	unsigned int		gen)
27130f712c9SDave Chinner {
27230f712c9SDave Chinner 	struct xfs_buf		*fbuf;
27330f712c9SDave Chinner 	struct xfs_dinode	*free;
27430f712c9SDave Chinner 	int			nbufs, blks_per_cluster, inodes_per_cluster;
27530f712c9SDave Chinner 	int			version;
27630f712c9SDave Chinner 	int			i, j;
27730f712c9SDave Chinner 	xfs_daddr_t		d;
27830f712c9SDave Chinner 	xfs_ino_t		ino = 0;
27930f712c9SDave Chinner 
28030f712c9SDave Chinner 	/*
28130f712c9SDave Chinner 	 * Loop over the new block(s), filling in the inodes.  For small block
28230f712c9SDave Chinner 	 * sizes, manipulate the inodes in buffers  which are multiples of the
28330f712c9SDave Chinner 	 * blocks size.
28430f712c9SDave Chinner 	 */
28530f712c9SDave Chinner 	blks_per_cluster = xfs_icluster_size_fsb(mp);
28630f712c9SDave Chinner 	inodes_per_cluster = blks_per_cluster << mp->m_sb.sb_inopblog;
28730f712c9SDave Chinner 	nbufs = length / blks_per_cluster;
28830f712c9SDave Chinner 
28930f712c9SDave Chinner 	/*
29030f712c9SDave Chinner 	 * Figure out what version number to use in the inodes we create.  If
29130f712c9SDave Chinner 	 * the superblock version has caught up to the one that supports the new
29230f712c9SDave Chinner 	 * inode format, then use the new inode version.  Otherwise use the old
29330f712c9SDave Chinner 	 * version so that old kernels will continue to be able to use the file
29430f712c9SDave Chinner 	 * system.
29530f712c9SDave Chinner 	 *
29630f712c9SDave Chinner 	 * For v3 inodes, we also need to write the inode number into the inode,
29730f712c9SDave Chinner 	 * so calculate the first inode number of the chunk here as
29830f712c9SDave Chinner 	 * XFS_OFFBNO_TO_AGINO() only works within a filesystem block, not
29930f712c9SDave Chinner 	 * across multiple filesystem blocks (such as a cluster) and so cannot
30030f712c9SDave Chinner 	 * be used in the cluster buffer loop below.
30130f712c9SDave Chinner 	 *
30230f712c9SDave Chinner 	 * Further, because we are writing the inode directly into the buffer
30330f712c9SDave Chinner 	 * and calculating a CRC on the entire inode, we have ot log the entire
30430f712c9SDave Chinner 	 * inode so that the entire range the CRC covers is present in the log.
30530f712c9SDave Chinner 	 * That means for v3 inode we log the entire buffer rather than just the
30630f712c9SDave Chinner 	 * inode cores.
30730f712c9SDave Chinner 	 */
30830f712c9SDave Chinner 	if (xfs_sb_version_hascrc(&mp->m_sb)) {
30930f712c9SDave Chinner 		version = 3;
31030f712c9SDave Chinner 		ino = XFS_AGINO_TO_INO(mp, agno,
31130f712c9SDave Chinner 				       XFS_OFFBNO_TO_AGINO(mp, agbno, 0));
31230f712c9SDave Chinner 
31330f712c9SDave Chinner 		/*
31430f712c9SDave Chinner 		 * log the initialisation that is about to take place as an
31530f712c9SDave Chinner 		 * logical operation. This means the transaction does not
31630f712c9SDave Chinner 		 * need to log the physical changes to the inode buffers as log
31730f712c9SDave Chinner 		 * recovery will know what initialisation is actually needed.
31830f712c9SDave Chinner 		 * Hence we only need to log the buffers as "ordered" buffers so
31930f712c9SDave Chinner 		 * they track in the AIL as if they were physically logged.
32030f712c9SDave Chinner 		 */
32130f712c9SDave Chinner 		if (tp)
322463958afSBrian Foster 			xfs_icreate_log(tp, agno, agbno, icount,
32330f712c9SDave Chinner 					mp->m_sb.sb_inodesize, length, gen);
32430f712c9SDave Chinner 	} else
32530f712c9SDave Chinner 		version = 2;
32630f712c9SDave Chinner 
32730f712c9SDave Chinner 	for (j = 0; j < nbufs; j++) {
32830f712c9SDave Chinner 		/*
32930f712c9SDave Chinner 		 * Get the block.
33030f712c9SDave Chinner 		 */
33130f712c9SDave Chinner 		d = XFS_AGB_TO_DADDR(mp, agno, agbno + (j * blks_per_cluster));
33230f712c9SDave Chinner 		fbuf = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
33330f712c9SDave Chinner 					 mp->m_bsize * blks_per_cluster,
33430f712c9SDave Chinner 					 XBF_UNMAPPED);
33530f712c9SDave Chinner 		if (!fbuf)
3362451337dSDave Chinner 			return -ENOMEM;
33730f712c9SDave Chinner 
33830f712c9SDave Chinner 		/* Initialize the inode buffers and log them appropriately. */
33930f712c9SDave Chinner 		fbuf->b_ops = &xfs_inode_buf_ops;
34030f712c9SDave Chinner 		xfs_buf_zero(fbuf, 0, BBTOB(fbuf->b_length));
34130f712c9SDave Chinner 		for (i = 0; i < inodes_per_cluster; i++) {
34230f712c9SDave Chinner 			int	ioffset = i << mp->m_sb.sb_inodelog;
34330f712c9SDave Chinner 			uint	isize = xfs_dinode_size(version);
34430f712c9SDave Chinner 
34530f712c9SDave Chinner 			free = xfs_make_iptr(mp, fbuf, i);
34630f712c9SDave Chinner 			free->di_magic = cpu_to_be16(XFS_DINODE_MAGIC);
34730f712c9SDave Chinner 			free->di_version = version;
34830f712c9SDave Chinner 			free->di_gen = cpu_to_be32(gen);
34930f712c9SDave Chinner 			free->di_next_unlinked = cpu_to_be32(NULLAGINO);
35030f712c9SDave Chinner 
35130f712c9SDave Chinner 			if (version == 3) {
35230f712c9SDave Chinner 				free->di_ino = cpu_to_be64(ino);
35330f712c9SDave Chinner 				ino++;
354ce748eaaSEric Sandeen 				uuid_copy(&free->di_uuid,
355ce748eaaSEric Sandeen 					  &mp->m_sb.sb_meta_uuid);
35630f712c9SDave Chinner 				xfs_dinode_calc_crc(mp, free);
35730f712c9SDave Chinner 			} else if (tp) {
35830f712c9SDave Chinner 				/* just log the inode core */
35930f712c9SDave Chinner 				xfs_trans_log_buf(tp, fbuf, ioffset,
36030f712c9SDave Chinner 						  ioffset + isize - 1);
36130f712c9SDave Chinner 			}
36230f712c9SDave Chinner 		}
36330f712c9SDave Chinner 
36430f712c9SDave Chinner 		if (tp) {
36530f712c9SDave Chinner 			/*
36630f712c9SDave Chinner 			 * Mark the buffer as an inode allocation buffer so it
36730f712c9SDave Chinner 			 * sticks in AIL at the point of this allocation
36830f712c9SDave Chinner 			 * transaction. This ensures the they are on disk before
36930f712c9SDave Chinner 			 * the tail of the log can be moved past this
37030f712c9SDave Chinner 			 * transaction (i.e. by preventing relogging from moving
37130f712c9SDave Chinner 			 * it forward in the log).
37230f712c9SDave Chinner 			 */
37330f712c9SDave Chinner 			xfs_trans_inode_alloc_buf(tp, fbuf);
37430f712c9SDave Chinner 			if (version == 3) {
37530f712c9SDave Chinner 				/*
37630f712c9SDave Chinner 				 * Mark the buffer as ordered so that they are
37730f712c9SDave Chinner 				 * not physically logged in the transaction but
37830f712c9SDave Chinner 				 * still tracked in the AIL as part of the
37930f712c9SDave Chinner 				 * transaction and pin the log appropriately.
38030f712c9SDave Chinner 				 */
38130f712c9SDave Chinner 				xfs_trans_ordered_buf(tp, fbuf);
38230f712c9SDave Chinner 			}
38330f712c9SDave Chinner 		} else {
38430f712c9SDave Chinner 			fbuf->b_flags |= XBF_DONE;
38530f712c9SDave Chinner 			xfs_buf_delwri_queue(fbuf, buffer_list);
38630f712c9SDave Chinner 			xfs_buf_relse(fbuf);
38730f712c9SDave Chinner 		}
38830f712c9SDave Chinner 	}
38930f712c9SDave Chinner 	return 0;
39030f712c9SDave Chinner }
39130f712c9SDave Chinner 
39230f712c9SDave Chinner /*
39356d1115cSBrian Foster  * Align startino and allocmask for a recently allocated sparse chunk such that
39456d1115cSBrian Foster  * they are fit for insertion (or merge) into the on-disk inode btrees.
39556d1115cSBrian Foster  *
39656d1115cSBrian Foster  * Background:
39756d1115cSBrian Foster  *
39856d1115cSBrian Foster  * When enabled, sparse inode support increases the inode alignment from cluster
39956d1115cSBrian Foster  * size to inode chunk size. This means that the minimum range between two
40056d1115cSBrian Foster  * non-adjacent inode records in the inobt is large enough for a full inode
40156d1115cSBrian Foster  * record. This allows for cluster sized, cluster aligned block allocation
40256d1115cSBrian Foster  * without need to worry about whether the resulting inode record overlaps with
40356d1115cSBrian Foster  * another record in the tree. Without this basic rule, we would have to deal
40456d1115cSBrian Foster  * with the consequences of overlap by potentially undoing recent allocations in
40556d1115cSBrian Foster  * the inode allocation codepath.
40656d1115cSBrian Foster  *
40756d1115cSBrian Foster  * Because of this alignment rule (which is enforced on mount), there are two
40856d1115cSBrian Foster  * inobt possibilities for newly allocated sparse chunks. One is that the
40956d1115cSBrian Foster  * aligned inode record for the chunk covers a range of inodes not already
41056d1115cSBrian Foster  * covered in the inobt (i.e., it is safe to insert a new sparse record). The
41156d1115cSBrian Foster  * other is that a record already exists at the aligned startino that considers
41256d1115cSBrian Foster  * the newly allocated range as sparse. In the latter case, record content is
41356d1115cSBrian Foster  * merged in hope that sparse inode chunks fill to full chunks over time.
41456d1115cSBrian Foster  */
41556d1115cSBrian Foster STATIC void
41656d1115cSBrian Foster xfs_align_sparse_ino(
41756d1115cSBrian Foster 	struct xfs_mount		*mp,
41856d1115cSBrian Foster 	xfs_agino_t			*startino,
41956d1115cSBrian Foster 	uint16_t			*allocmask)
42056d1115cSBrian Foster {
42156d1115cSBrian Foster 	xfs_agblock_t			agbno;
42256d1115cSBrian Foster 	xfs_agblock_t			mod;
42356d1115cSBrian Foster 	int				offset;
42456d1115cSBrian Foster 
42556d1115cSBrian Foster 	agbno = XFS_AGINO_TO_AGBNO(mp, *startino);
42656d1115cSBrian Foster 	mod = agbno % mp->m_sb.sb_inoalignmt;
42756d1115cSBrian Foster 	if (!mod)
42856d1115cSBrian Foster 		return;
42956d1115cSBrian Foster 
43056d1115cSBrian Foster 	/* calculate the inode offset and align startino */
43156d1115cSBrian Foster 	offset = mod << mp->m_sb.sb_inopblog;
43256d1115cSBrian Foster 	*startino -= offset;
43356d1115cSBrian Foster 
43456d1115cSBrian Foster 	/*
43556d1115cSBrian Foster 	 * Since startino has been aligned down, left shift allocmask such that
43656d1115cSBrian Foster 	 * it continues to represent the same physical inodes relative to the
43756d1115cSBrian Foster 	 * new startino.
43856d1115cSBrian Foster 	 */
43956d1115cSBrian Foster 	*allocmask <<= offset / XFS_INODES_PER_HOLEMASK_BIT;
44056d1115cSBrian Foster }
44156d1115cSBrian Foster 
44256d1115cSBrian Foster /*
44356d1115cSBrian Foster  * Determine whether the source inode record can merge into the target. Both
44456d1115cSBrian Foster  * records must be sparse, the inode ranges must match and there must be no
44556d1115cSBrian Foster  * allocation overlap between the records.
44656d1115cSBrian Foster  */
44756d1115cSBrian Foster STATIC bool
44856d1115cSBrian Foster __xfs_inobt_can_merge(
44956d1115cSBrian Foster 	struct xfs_inobt_rec_incore	*trec,	/* tgt record */
45056d1115cSBrian Foster 	struct xfs_inobt_rec_incore	*srec)	/* src record */
45156d1115cSBrian Foster {
45256d1115cSBrian Foster 	uint64_t			talloc;
45356d1115cSBrian Foster 	uint64_t			salloc;
45456d1115cSBrian Foster 
45556d1115cSBrian Foster 	/* records must cover the same inode range */
45656d1115cSBrian Foster 	if (trec->ir_startino != srec->ir_startino)
45756d1115cSBrian Foster 		return false;
45856d1115cSBrian Foster 
45956d1115cSBrian Foster 	/* both records must be sparse */
46056d1115cSBrian Foster 	if (!xfs_inobt_issparse(trec->ir_holemask) ||
46156d1115cSBrian Foster 	    !xfs_inobt_issparse(srec->ir_holemask))
46256d1115cSBrian Foster 		return false;
46356d1115cSBrian Foster 
46456d1115cSBrian Foster 	/* both records must track some inodes */
46556d1115cSBrian Foster 	if (!trec->ir_count || !srec->ir_count)
46656d1115cSBrian Foster 		return false;
46756d1115cSBrian Foster 
46856d1115cSBrian Foster 	/* can't exceed capacity of a full record */
46956d1115cSBrian Foster 	if (trec->ir_count + srec->ir_count > XFS_INODES_PER_CHUNK)
47056d1115cSBrian Foster 		return false;
47156d1115cSBrian Foster 
47256d1115cSBrian Foster 	/* verify there is no allocation overlap */
47356d1115cSBrian Foster 	talloc = xfs_inobt_irec_to_allocmask(trec);
47456d1115cSBrian Foster 	salloc = xfs_inobt_irec_to_allocmask(srec);
47556d1115cSBrian Foster 	if (talloc & salloc)
47656d1115cSBrian Foster 		return false;
47756d1115cSBrian Foster 
47856d1115cSBrian Foster 	return true;
47956d1115cSBrian Foster }
48056d1115cSBrian Foster 
48156d1115cSBrian Foster /*
48256d1115cSBrian Foster  * Merge the source inode record into the target. The caller must call
48356d1115cSBrian Foster  * __xfs_inobt_can_merge() to ensure the merge is valid.
48456d1115cSBrian Foster  */
48556d1115cSBrian Foster STATIC void
48656d1115cSBrian Foster __xfs_inobt_rec_merge(
48756d1115cSBrian Foster 	struct xfs_inobt_rec_incore	*trec,	/* target */
48856d1115cSBrian Foster 	struct xfs_inobt_rec_incore	*srec)	/* src */
48956d1115cSBrian Foster {
49056d1115cSBrian Foster 	ASSERT(trec->ir_startino == srec->ir_startino);
49156d1115cSBrian Foster 
49256d1115cSBrian Foster 	/* combine the counts */
49356d1115cSBrian Foster 	trec->ir_count += srec->ir_count;
49456d1115cSBrian Foster 	trec->ir_freecount += srec->ir_freecount;
49556d1115cSBrian Foster 
49656d1115cSBrian Foster 	/*
49756d1115cSBrian Foster 	 * Merge the holemask and free mask. For both fields, 0 bits refer to
49856d1115cSBrian Foster 	 * allocated inodes. We combine the allocated ranges with bitwise AND.
49956d1115cSBrian Foster 	 */
50056d1115cSBrian Foster 	trec->ir_holemask &= srec->ir_holemask;
50156d1115cSBrian Foster 	trec->ir_free &= srec->ir_free;
50256d1115cSBrian Foster }
50356d1115cSBrian Foster 
50456d1115cSBrian Foster /*
50556d1115cSBrian Foster  * Insert a new sparse inode chunk into the associated inode btree. The inode
50656d1115cSBrian Foster  * record for the sparse chunk is pre-aligned to a startino that should match
50756d1115cSBrian Foster  * any pre-existing sparse inode record in the tree. This allows sparse chunks
50856d1115cSBrian Foster  * to fill over time.
50956d1115cSBrian Foster  *
51056d1115cSBrian Foster  * This function supports two modes of handling preexisting records depending on
51156d1115cSBrian Foster  * the merge flag. If merge is true, the provided record is merged with the
51256d1115cSBrian Foster  * existing record and updated in place. The merged record is returned in nrec.
51356d1115cSBrian Foster  * If merge is false, an existing record is replaced with the provided record.
51456d1115cSBrian Foster  * If no preexisting record exists, the provided record is always inserted.
51556d1115cSBrian Foster  *
51656d1115cSBrian Foster  * It is considered corruption if a merge is requested and not possible. Given
51756d1115cSBrian Foster  * the sparse inode alignment constraints, this should never happen.
51856d1115cSBrian Foster  */
51956d1115cSBrian Foster STATIC int
52056d1115cSBrian Foster xfs_inobt_insert_sprec(
52156d1115cSBrian Foster 	struct xfs_mount		*mp,
52256d1115cSBrian Foster 	struct xfs_trans		*tp,
52356d1115cSBrian Foster 	struct xfs_buf			*agbp,
52456d1115cSBrian Foster 	int				btnum,
52556d1115cSBrian Foster 	struct xfs_inobt_rec_incore	*nrec,	/* in/out: new/merged rec. */
52656d1115cSBrian Foster 	bool				merge)	/* merge or replace */
52756d1115cSBrian Foster {
52856d1115cSBrian Foster 	struct xfs_btree_cur		*cur;
52956d1115cSBrian Foster 	struct xfs_agi			*agi = XFS_BUF_TO_AGI(agbp);
53056d1115cSBrian Foster 	xfs_agnumber_t			agno = be32_to_cpu(agi->agi_seqno);
53156d1115cSBrian Foster 	int				error;
53256d1115cSBrian Foster 	int				i;
53356d1115cSBrian Foster 	struct xfs_inobt_rec_incore	rec;
53456d1115cSBrian Foster 
53556d1115cSBrian Foster 	cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, btnum);
53656d1115cSBrian Foster 
53756d1115cSBrian Foster 	/* the new record is pre-aligned so we know where to look */
53856d1115cSBrian Foster 	error = xfs_inobt_lookup(cur, nrec->ir_startino, XFS_LOOKUP_EQ, &i);
53956d1115cSBrian Foster 	if (error)
54056d1115cSBrian Foster 		goto error;
54156d1115cSBrian Foster 	/* if nothing there, insert a new record and return */
54256d1115cSBrian Foster 	if (i == 0) {
54356d1115cSBrian Foster 		error = xfs_inobt_insert_rec(cur, nrec->ir_holemask,
54456d1115cSBrian Foster 					     nrec->ir_count, nrec->ir_freecount,
54556d1115cSBrian Foster 					     nrec->ir_free, &i);
54656d1115cSBrian Foster 		if (error)
54756d1115cSBrian Foster 			goto error;
54856d1115cSBrian Foster 		XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error);
54956d1115cSBrian Foster 
55056d1115cSBrian Foster 		goto out;
55156d1115cSBrian Foster 	}
55256d1115cSBrian Foster 
55356d1115cSBrian Foster 	/*
55456d1115cSBrian Foster 	 * A record exists at this startino. Merge or replace the record
55556d1115cSBrian Foster 	 * depending on what we've been asked to do.
55656d1115cSBrian Foster 	 */
55756d1115cSBrian Foster 	if (merge) {
55856d1115cSBrian Foster 		error = xfs_inobt_get_rec(cur, &rec, &i);
55956d1115cSBrian Foster 		if (error)
56056d1115cSBrian Foster 			goto error;
56156d1115cSBrian Foster 		XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error);
56256d1115cSBrian Foster 		XFS_WANT_CORRUPTED_GOTO(mp,
56356d1115cSBrian Foster 					rec.ir_startino == nrec->ir_startino,
56456d1115cSBrian Foster 					error);
56556d1115cSBrian Foster 
56656d1115cSBrian Foster 		/*
56756d1115cSBrian Foster 		 * This should never fail. If we have coexisting records that
56856d1115cSBrian Foster 		 * cannot merge, something is seriously wrong.
56956d1115cSBrian Foster 		 */
57056d1115cSBrian Foster 		XFS_WANT_CORRUPTED_GOTO(mp, __xfs_inobt_can_merge(nrec, &rec),
57156d1115cSBrian Foster 					error);
57256d1115cSBrian Foster 
57356d1115cSBrian Foster 		trace_xfs_irec_merge_pre(mp, agno, rec.ir_startino,
57456d1115cSBrian Foster 					 rec.ir_holemask, nrec->ir_startino,
57556d1115cSBrian Foster 					 nrec->ir_holemask);
57656d1115cSBrian Foster 
57756d1115cSBrian Foster 		/* merge to nrec to output the updated record */
57856d1115cSBrian Foster 		__xfs_inobt_rec_merge(nrec, &rec);
57956d1115cSBrian Foster 
58056d1115cSBrian Foster 		trace_xfs_irec_merge_post(mp, agno, nrec->ir_startino,
58156d1115cSBrian Foster 					  nrec->ir_holemask);
58256d1115cSBrian Foster 
58356d1115cSBrian Foster 		error = xfs_inobt_rec_check_count(mp, nrec);
58456d1115cSBrian Foster 		if (error)
58556d1115cSBrian Foster 			goto error;
58656d1115cSBrian Foster 	}
58756d1115cSBrian Foster 
58856d1115cSBrian Foster 	error = xfs_inobt_update(cur, nrec);
58956d1115cSBrian Foster 	if (error)
59056d1115cSBrian Foster 		goto error;
59156d1115cSBrian Foster 
59256d1115cSBrian Foster out:
59356d1115cSBrian Foster 	xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
59456d1115cSBrian Foster 	return 0;
59556d1115cSBrian Foster error:
59656d1115cSBrian Foster 	xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
59756d1115cSBrian Foster 	return error;
59856d1115cSBrian Foster }
59956d1115cSBrian Foster 
60056d1115cSBrian Foster /*
60130f712c9SDave Chinner  * Allocate new inodes in the allocation group specified by agbp.
60230f712c9SDave Chinner  * Return 0 for success, else error code.
60330f712c9SDave Chinner  */
60430f712c9SDave Chinner STATIC int				/* error code or 0 */
60530f712c9SDave Chinner xfs_ialloc_ag_alloc(
60630f712c9SDave Chinner 	xfs_trans_t	*tp,		/* transaction pointer */
60730f712c9SDave Chinner 	xfs_buf_t	*agbp,		/* alloc group buffer */
60830f712c9SDave Chinner 	int		*alloc)
60930f712c9SDave Chinner {
61030f712c9SDave Chinner 	xfs_agi_t	*agi;		/* allocation group header */
61130f712c9SDave Chinner 	xfs_alloc_arg_t	args;		/* allocation argument structure */
61230f712c9SDave Chinner 	xfs_agnumber_t	agno;
61330f712c9SDave Chinner 	int		error;
61430f712c9SDave Chinner 	xfs_agino_t	newino;		/* new first inode's number */
61530f712c9SDave Chinner 	xfs_agino_t	newlen;		/* new number of inodes */
61630f712c9SDave Chinner 	int		isaligned = 0;	/* inode allocation at stripe unit */
61730f712c9SDave Chinner 					/* boundary */
61856d1115cSBrian Foster 	uint16_t	allocmask = (uint16_t) -1; /* init. to full chunk */
61956d1115cSBrian Foster 	struct xfs_inobt_rec_incore rec;
62030f712c9SDave Chinner 	struct xfs_perag *pag;
6211cdadee1SBrian Foster 	int		do_sparse = 0;
6221cdadee1SBrian Foster 
62330f712c9SDave Chinner 	memset(&args, 0, sizeof(args));
62430f712c9SDave Chinner 	args.tp = tp;
62530f712c9SDave Chinner 	args.mp = tp->t_mountp;
6261cdadee1SBrian Foster 	args.fsbno = NULLFSBLOCK;
627340785ccSDarrick J. Wong 	xfs_rmap_ag_owner(&args.oinfo, XFS_RMAP_OWN_INODES);
62830f712c9SDave Chinner 
62946fc58daSBrian Foster #ifdef DEBUG
63046fc58daSBrian Foster 	/* randomly do sparse inode allocations */
63146fc58daSBrian Foster 	if (xfs_sb_version_hassparseinodes(&tp->t_mountp->m_sb) &&
63246fc58daSBrian Foster 	    args.mp->m_ialloc_min_blks < args.mp->m_ialloc_blks)
63346fc58daSBrian Foster 		do_sparse = prandom_u32() & 1;
63446fc58daSBrian Foster #endif
63546fc58daSBrian Foster 
63630f712c9SDave Chinner 	/*
63730f712c9SDave Chinner 	 * Locking will ensure that we don't have two callers in here
63830f712c9SDave Chinner 	 * at one time.
63930f712c9SDave Chinner 	 */
64030f712c9SDave Chinner 	newlen = args.mp->m_ialloc_inos;
64130f712c9SDave Chinner 	if (args.mp->m_maxicount &&
64274f9ce1cSGeorge Wang 	    percpu_counter_read_positive(&args.mp->m_icount) + newlen >
643501ab323SDave Chinner 							args.mp->m_maxicount)
6442451337dSDave Chinner 		return -ENOSPC;
64530f712c9SDave Chinner 	args.minlen = args.maxlen = args.mp->m_ialloc_blks;
64630f712c9SDave Chinner 	/*
64730f712c9SDave Chinner 	 * First try to allocate inodes contiguous with the last-allocated
64830f712c9SDave Chinner 	 * chunk of inodes.  If the filesystem is striped, this will fill
64930f712c9SDave Chinner 	 * an entire stripe unit with inodes.
65030f712c9SDave Chinner 	 */
65130f712c9SDave Chinner 	agi = XFS_BUF_TO_AGI(agbp);
65230f712c9SDave Chinner 	newino = be32_to_cpu(agi->agi_newino);
65330f712c9SDave Chinner 	agno = be32_to_cpu(agi->agi_seqno);
65430f712c9SDave Chinner 	args.agbno = XFS_AGINO_TO_AGBNO(args.mp, newino) +
65530f712c9SDave Chinner 		     args.mp->m_ialloc_blks;
6561cdadee1SBrian Foster 	if (do_sparse)
6571cdadee1SBrian Foster 		goto sparse_alloc;
65830f712c9SDave Chinner 	if (likely(newino != NULLAGINO &&
65930f712c9SDave Chinner 		  (args.agbno < be32_to_cpu(agi->agi_length)))) {
66030f712c9SDave Chinner 		args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
66130f712c9SDave Chinner 		args.type = XFS_ALLOCTYPE_THIS_BNO;
66230f712c9SDave Chinner 		args.prod = 1;
66330f712c9SDave Chinner 
66430f712c9SDave Chinner 		/*
66530f712c9SDave Chinner 		 * We need to take into account alignment here to ensure that
66630f712c9SDave Chinner 		 * we don't modify the free list if we fail to have an exact
66730f712c9SDave Chinner 		 * block. If we don't have an exact match, and every oher
66830f712c9SDave Chinner 		 * attempt allocation attempt fails, we'll end up cancelling
66930f712c9SDave Chinner 		 * a dirty transaction and shutting down.
67030f712c9SDave Chinner 		 *
67130f712c9SDave Chinner 		 * For an exact allocation, alignment must be 1,
67230f712c9SDave Chinner 		 * however we need to take cluster alignment into account when
67330f712c9SDave Chinner 		 * fixing up the freelist. Use the minalignslop field to
67430f712c9SDave Chinner 		 * indicate that extra blocks might be required for alignment,
67530f712c9SDave Chinner 		 * but not to use them in the actual exact allocation.
67630f712c9SDave Chinner 		 */
67730f712c9SDave Chinner 		args.alignment = 1;
6787a1df156SDave Chinner 		args.minalignslop = xfs_ialloc_cluster_alignment(args.mp) - 1;
67930f712c9SDave Chinner 
68030f712c9SDave Chinner 		/* Allow space for the inode btree to split. */
68130f712c9SDave Chinner 		args.minleft = args.mp->m_in_maxlevels - 1;
68230f712c9SDave Chinner 		if ((error = xfs_alloc_vextent(&args)))
68330f712c9SDave Chinner 			return error;
68430f712c9SDave Chinner 
68530f712c9SDave Chinner 		/*
68630f712c9SDave Chinner 		 * This request might have dirtied the transaction if the AG can
68730f712c9SDave Chinner 		 * satisfy the request, but the exact block was not available.
68830f712c9SDave Chinner 		 * If the allocation did fail, subsequent requests will relax
68930f712c9SDave Chinner 		 * the exact agbno requirement and increase the alignment
69030f712c9SDave Chinner 		 * instead. It is critical that the total size of the request
69130f712c9SDave Chinner 		 * (len + alignment + slop) does not increase from this point
69230f712c9SDave Chinner 		 * on, so reset minalignslop to ensure it is not included in
69330f712c9SDave Chinner 		 * subsequent requests.
69430f712c9SDave Chinner 		 */
69530f712c9SDave Chinner 		args.minalignslop = 0;
6961cdadee1SBrian Foster 	}
69730f712c9SDave Chinner 
69830f712c9SDave Chinner 	if (unlikely(args.fsbno == NULLFSBLOCK)) {
69930f712c9SDave Chinner 		/*
70030f712c9SDave Chinner 		 * Set the alignment for the allocation.
70130f712c9SDave Chinner 		 * If stripe alignment is turned on then align at stripe unit
70230f712c9SDave Chinner 		 * boundary.
70330f712c9SDave Chinner 		 * If the cluster size is smaller than a filesystem block
70430f712c9SDave Chinner 		 * then we're doing I/O for inodes in filesystem block size
70530f712c9SDave Chinner 		 * pieces, so don't need alignment anyway.
70630f712c9SDave Chinner 		 */
70730f712c9SDave Chinner 		isaligned = 0;
70830f712c9SDave Chinner 		if (args.mp->m_sinoalign) {
70930f712c9SDave Chinner 			ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN));
71030f712c9SDave Chinner 			args.alignment = args.mp->m_dalign;
71130f712c9SDave Chinner 			isaligned = 1;
71230f712c9SDave Chinner 		} else
7137a1df156SDave Chinner 			args.alignment = xfs_ialloc_cluster_alignment(args.mp);
71430f712c9SDave Chinner 		/*
71530f712c9SDave Chinner 		 * Need to figure out where to allocate the inode blocks.
71630f712c9SDave Chinner 		 * Ideally they should be spaced out through the a.g.
71730f712c9SDave Chinner 		 * For now, just allocate blocks up front.
71830f712c9SDave Chinner 		 */
71930f712c9SDave Chinner 		args.agbno = be32_to_cpu(agi->agi_root);
72030f712c9SDave Chinner 		args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
72130f712c9SDave Chinner 		/*
72230f712c9SDave Chinner 		 * Allocate a fixed-size extent of inodes.
72330f712c9SDave Chinner 		 */
72430f712c9SDave Chinner 		args.type = XFS_ALLOCTYPE_NEAR_BNO;
72530f712c9SDave Chinner 		args.prod = 1;
72630f712c9SDave Chinner 		/*
72730f712c9SDave Chinner 		 * Allow space for the inode btree to split.
72830f712c9SDave Chinner 		 */
72930f712c9SDave Chinner 		args.minleft = args.mp->m_in_maxlevels - 1;
73030f712c9SDave Chinner 		if ((error = xfs_alloc_vextent(&args)))
73130f712c9SDave Chinner 			return error;
73230f712c9SDave Chinner 	}
73330f712c9SDave Chinner 
73430f712c9SDave Chinner 	/*
73530f712c9SDave Chinner 	 * If stripe alignment is turned on, then try again with cluster
73630f712c9SDave Chinner 	 * alignment.
73730f712c9SDave Chinner 	 */
73830f712c9SDave Chinner 	if (isaligned && args.fsbno == NULLFSBLOCK) {
73930f712c9SDave Chinner 		args.type = XFS_ALLOCTYPE_NEAR_BNO;
74030f712c9SDave Chinner 		args.agbno = be32_to_cpu(agi->agi_root);
74130f712c9SDave Chinner 		args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
7427a1df156SDave Chinner 		args.alignment = xfs_ialloc_cluster_alignment(args.mp);
74330f712c9SDave Chinner 		if ((error = xfs_alloc_vextent(&args)))
74430f712c9SDave Chinner 			return error;
74530f712c9SDave Chinner 	}
74630f712c9SDave Chinner 
74756d1115cSBrian Foster 	/*
74856d1115cSBrian Foster 	 * Finally, try a sparse allocation if the filesystem supports it and
74956d1115cSBrian Foster 	 * the sparse allocation length is smaller than a full chunk.
75056d1115cSBrian Foster 	 */
75156d1115cSBrian Foster 	if (xfs_sb_version_hassparseinodes(&args.mp->m_sb) &&
75256d1115cSBrian Foster 	    args.mp->m_ialloc_min_blks < args.mp->m_ialloc_blks &&
75356d1115cSBrian Foster 	    args.fsbno == NULLFSBLOCK) {
7541cdadee1SBrian Foster sparse_alloc:
75556d1115cSBrian Foster 		args.type = XFS_ALLOCTYPE_NEAR_BNO;
75656d1115cSBrian Foster 		args.agbno = be32_to_cpu(agi->agi_root);
75756d1115cSBrian Foster 		args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
75856d1115cSBrian Foster 		args.alignment = args.mp->m_sb.sb_spino_align;
75956d1115cSBrian Foster 		args.prod = 1;
76056d1115cSBrian Foster 
76156d1115cSBrian Foster 		args.minlen = args.mp->m_ialloc_min_blks;
76256d1115cSBrian Foster 		args.maxlen = args.minlen;
76356d1115cSBrian Foster 
76456d1115cSBrian Foster 		/*
76556d1115cSBrian Foster 		 * The inode record will be aligned to full chunk size. We must
76656d1115cSBrian Foster 		 * prevent sparse allocation from AG boundaries that result in
76756d1115cSBrian Foster 		 * invalid inode records, such as records that start at agbno 0
76856d1115cSBrian Foster 		 * or extend beyond the AG.
76956d1115cSBrian Foster 		 *
77056d1115cSBrian Foster 		 * Set min agbno to the first aligned, non-zero agbno and max to
77156d1115cSBrian Foster 		 * the last aligned agbno that is at least one full chunk from
77256d1115cSBrian Foster 		 * the end of the AG.
77356d1115cSBrian Foster 		 */
77456d1115cSBrian Foster 		args.min_agbno = args.mp->m_sb.sb_inoalignmt;
77556d1115cSBrian Foster 		args.max_agbno = round_down(args.mp->m_sb.sb_agblocks,
77656d1115cSBrian Foster 					    args.mp->m_sb.sb_inoalignmt) -
77756d1115cSBrian Foster 				 args.mp->m_ialloc_blks;
77856d1115cSBrian Foster 
77956d1115cSBrian Foster 		error = xfs_alloc_vextent(&args);
78056d1115cSBrian Foster 		if (error)
78156d1115cSBrian Foster 			return error;
78256d1115cSBrian Foster 
78356d1115cSBrian Foster 		newlen = args.len << args.mp->m_sb.sb_inopblog;
78446fc58daSBrian Foster 		ASSERT(newlen <= XFS_INODES_PER_CHUNK);
78556d1115cSBrian Foster 		allocmask = (1 << (newlen / XFS_INODES_PER_HOLEMASK_BIT)) - 1;
78656d1115cSBrian Foster 	}
78756d1115cSBrian Foster 
78830f712c9SDave Chinner 	if (args.fsbno == NULLFSBLOCK) {
78930f712c9SDave Chinner 		*alloc = 0;
79030f712c9SDave Chinner 		return 0;
79130f712c9SDave Chinner 	}
79230f712c9SDave Chinner 	ASSERT(args.len == args.minlen);
79330f712c9SDave Chinner 
79430f712c9SDave Chinner 	/*
79530f712c9SDave Chinner 	 * Stamp and write the inode buffers.
79630f712c9SDave Chinner 	 *
79730f712c9SDave Chinner 	 * Seed the new inode cluster with a random generation number. This
79830f712c9SDave Chinner 	 * prevents short-term reuse of generation numbers if a chunk is
79930f712c9SDave Chinner 	 * freed and then immediately reallocated. We use random numbers
80030f712c9SDave Chinner 	 * rather than a linear progression to prevent the next generation
80130f712c9SDave Chinner 	 * number from being easily guessable.
80230f712c9SDave Chinner 	 */
803463958afSBrian Foster 	error = xfs_ialloc_inode_init(args.mp, tp, NULL, newlen, agno,
804463958afSBrian Foster 			args.agbno, args.len, prandom_u32());
80530f712c9SDave Chinner 
80630f712c9SDave Chinner 	if (error)
80730f712c9SDave Chinner 		return error;
80830f712c9SDave Chinner 	/*
80930f712c9SDave Chinner 	 * Convert the results.
81030f712c9SDave Chinner 	 */
81130f712c9SDave Chinner 	newino = XFS_OFFBNO_TO_AGINO(args.mp, args.agbno, 0);
81256d1115cSBrian Foster 
81356d1115cSBrian Foster 	if (xfs_inobt_issparse(~allocmask)) {
81456d1115cSBrian Foster 		/*
81556d1115cSBrian Foster 		 * We've allocated a sparse chunk. Align the startino and mask.
81656d1115cSBrian Foster 		 */
81756d1115cSBrian Foster 		xfs_align_sparse_ino(args.mp, &newino, &allocmask);
81856d1115cSBrian Foster 
81956d1115cSBrian Foster 		rec.ir_startino = newino;
82056d1115cSBrian Foster 		rec.ir_holemask = ~allocmask;
82156d1115cSBrian Foster 		rec.ir_count = newlen;
82256d1115cSBrian Foster 		rec.ir_freecount = newlen;
82356d1115cSBrian Foster 		rec.ir_free = XFS_INOBT_ALL_FREE;
82456d1115cSBrian Foster 
82556d1115cSBrian Foster 		/*
82656d1115cSBrian Foster 		 * Insert the sparse record into the inobt and allow for a merge
82756d1115cSBrian Foster 		 * if necessary. If a merge does occur, rec is updated to the
82856d1115cSBrian Foster 		 * merged record.
82956d1115cSBrian Foster 		 */
83056d1115cSBrian Foster 		error = xfs_inobt_insert_sprec(args.mp, tp, agbp, XFS_BTNUM_INO,
83156d1115cSBrian Foster 					       &rec, true);
83256d1115cSBrian Foster 		if (error == -EFSCORRUPTED) {
83356d1115cSBrian Foster 			xfs_alert(args.mp,
83456d1115cSBrian Foster 	"invalid sparse inode record: ino 0x%llx holemask 0x%x count %u",
83556d1115cSBrian Foster 				  XFS_AGINO_TO_INO(args.mp, agno,
83656d1115cSBrian Foster 						   rec.ir_startino),
83756d1115cSBrian Foster 				  rec.ir_holemask, rec.ir_count);
83856d1115cSBrian Foster 			xfs_force_shutdown(args.mp, SHUTDOWN_CORRUPT_INCORE);
83956d1115cSBrian Foster 		}
84056d1115cSBrian Foster 		if (error)
84156d1115cSBrian Foster 			return error;
84256d1115cSBrian Foster 
84356d1115cSBrian Foster 		/*
84456d1115cSBrian Foster 		 * We can't merge the part we've just allocated as for the inobt
84556d1115cSBrian Foster 		 * due to finobt semantics. The original record may or may not
84656d1115cSBrian Foster 		 * exist independent of whether physical inodes exist in this
84756d1115cSBrian Foster 		 * sparse chunk.
84856d1115cSBrian Foster 		 *
84956d1115cSBrian Foster 		 * We must update the finobt record based on the inobt record.
85056d1115cSBrian Foster 		 * rec contains the fully merged and up to date inobt record
85156d1115cSBrian Foster 		 * from the previous call. Set merge false to replace any
85256d1115cSBrian Foster 		 * existing record with this one.
85356d1115cSBrian Foster 		 */
85456d1115cSBrian Foster 		if (xfs_sb_version_hasfinobt(&args.mp->m_sb)) {
85556d1115cSBrian Foster 			error = xfs_inobt_insert_sprec(args.mp, tp, agbp,
85656d1115cSBrian Foster 						       XFS_BTNUM_FINO, &rec,
85756d1115cSBrian Foster 						       false);
85856d1115cSBrian Foster 			if (error)
85956d1115cSBrian Foster 				return error;
86056d1115cSBrian Foster 		}
86156d1115cSBrian Foster 	} else {
86256d1115cSBrian Foster 		/* full chunk - insert new records to both btrees */
86356d1115cSBrian Foster 		error = xfs_inobt_insert(args.mp, tp, agbp, newino, newlen,
86456d1115cSBrian Foster 					 XFS_BTNUM_INO);
86556d1115cSBrian Foster 		if (error)
86656d1115cSBrian Foster 			return error;
86756d1115cSBrian Foster 
86856d1115cSBrian Foster 		if (xfs_sb_version_hasfinobt(&args.mp->m_sb)) {
86956d1115cSBrian Foster 			error = xfs_inobt_insert(args.mp, tp, agbp, newino,
87056d1115cSBrian Foster 						 newlen, XFS_BTNUM_FINO);
87156d1115cSBrian Foster 			if (error)
87256d1115cSBrian Foster 				return error;
87356d1115cSBrian Foster 		}
87456d1115cSBrian Foster 	}
87556d1115cSBrian Foster 
87656d1115cSBrian Foster 	/*
87756d1115cSBrian Foster 	 * Update AGI counts and newino.
87856d1115cSBrian Foster 	 */
87930f712c9SDave Chinner 	be32_add_cpu(&agi->agi_count, newlen);
88030f712c9SDave Chinner 	be32_add_cpu(&agi->agi_freecount, newlen);
88130f712c9SDave Chinner 	pag = xfs_perag_get(args.mp, agno);
88230f712c9SDave Chinner 	pag->pagi_freecount += newlen;
88330f712c9SDave Chinner 	xfs_perag_put(pag);
88430f712c9SDave Chinner 	agi->agi_newino = cpu_to_be32(newino);
88530f712c9SDave Chinner 
88630f712c9SDave Chinner 	/*
88730f712c9SDave Chinner 	 * Log allocation group header fields
88830f712c9SDave Chinner 	 */
88930f712c9SDave Chinner 	xfs_ialloc_log_agi(tp, agbp,
89030f712c9SDave Chinner 		XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO);
89130f712c9SDave Chinner 	/*
89230f712c9SDave Chinner 	 * Modify/log superblock values for inode count and inode free count.
89330f712c9SDave Chinner 	 */
89430f712c9SDave Chinner 	xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen);
89530f712c9SDave Chinner 	xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen);
89630f712c9SDave Chinner 	*alloc = 1;
89730f712c9SDave Chinner 	return 0;
89830f712c9SDave Chinner }
89930f712c9SDave Chinner 
90030f712c9SDave Chinner STATIC xfs_agnumber_t
90130f712c9SDave Chinner xfs_ialloc_next_ag(
90230f712c9SDave Chinner 	xfs_mount_t	*mp)
90330f712c9SDave Chinner {
90430f712c9SDave Chinner 	xfs_agnumber_t	agno;
90530f712c9SDave Chinner 
90630f712c9SDave Chinner 	spin_lock(&mp->m_agirotor_lock);
90730f712c9SDave Chinner 	agno = mp->m_agirotor;
90830f712c9SDave Chinner 	if (++mp->m_agirotor >= mp->m_maxagi)
90930f712c9SDave Chinner 		mp->m_agirotor = 0;
91030f712c9SDave Chinner 	spin_unlock(&mp->m_agirotor_lock);
91130f712c9SDave Chinner 
91230f712c9SDave Chinner 	return agno;
91330f712c9SDave Chinner }
91430f712c9SDave Chinner 
91530f712c9SDave Chinner /*
91630f712c9SDave Chinner  * Select an allocation group to look for a free inode in, based on the parent
91730f712c9SDave Chinner  * inode and the mode.  Return the allocation group buffer.
91830f712c9SDave Chinner  */
91930f712c9SDave Chinner STATIC xfs_agnumber_t
92030f712c9SDave Chinner xfs_ialloc_ag_select(
92130f712c9SDave Chinner 	xfs_trans_t	*tp,		/* transaction pointer */
92230f712c9SDave Chinner 	xfs_ino_t	parent,		/* parent directory inode number */
923f59cf5c2SChristoph Hellwig 	umode_t		mode)		/* bits set to indicate file type */
92430f712c9SDave Chinner {
92530f712c9SDave Chinner 	xfs_agnumber_t	agcount;	/* number of ag's in the filesystem */
92630f712c9SDave Chinner 	xfs_agnumber_t	agno;		/* current ag number */
92730f712c9SDave Chinner 	int		flags;		/* alloc buffer locking flags */
92830f712c9SDave Chinner 	xfs_extlen_t	ineed;		/* blocks needed for inode allocation */
92930f712c9SDave Chinner 	xfs_extlen_t	longest = 0;	/* longest extent available */
93030f712c9SDave Chinner 	xfs_mount_t	*mp;		/* mount point structure */
93130f712c9SDave Chinner 	int		needspace;	/* file mode implies space allocated */
93230f712c9SDave Chinner 	xfs_perag_t	*pag;		/* per allocation group data */
93330f712c9SDave Chinner 	xfs_agnumber_t	pagno;		/* parent (starting) ag number */
93430f712c9SDave Chinner 	int		error;
93530f712c9SDave Chinner 
93630f712c9SDave Chinner 	/*
93730f712c9SDave Chinner 	 * Files of these types need at least one block if length > 0
93830f712c9SDave Chinner 	 * (and they won't fit in the inode, but that's hard to figure out).
93930f712c9SDave Chinner 	 */
94030f712c9SDave Chinner 	needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode);
94130f712c9SDave Chinner 	mp = tp->t_mountp;
94230f712c9SDave Chinner 	agcount = mp->m_maxagi;
94330f712c9SDave Chinner 	if (S_ISDIR(mode))
94430f712c9SDave Chinner 		pagno = xfs_ialloc_next_ag(mp);
94530f712c9SDave Chinner 	else {
94630f712c9SDave Chinner 		pagno = XFS_INO_TO_AGNO(mp, parent);
94730f712c9SDave Chinner 		if (pagno >= agcount)
94830f712c9SDave Chinner 			pagno = 0;
94930f712c9SDave Chinner 	}
95030f712c9SDave Chinner 
95130f712c9SDave Chinner 	ASSERT(pagno < agcount);
95230f712c9SDave Chinner 
95330f712c9SDave Chinner 	/*
95430f712c9SDave Chinner 	 * Loop through allocation groups, looking for one with a little
95530f712c9SDave Chinner 	 * free space in it.  Note we don't look for free inodes, exactly.
95630f712c9SDave Chinner 	 * Instead, we include whether there is a need to allocate inodes
95730f712c9SDave Chinner 	 * to mean that blocks must be allocated for them,
95830f712c9SDave Chinner 	 * if none are currently free.
95930f712c9SDave Chinner 	 */
96030f712c9SDave Chinner 	agno = pagno;
96130f712c9SDave Chinner 	flags = XFS_ALLOC_FLAG_TRYLOCK;
96230f712c9SDave Chinner 	for (;;) {
96330f712c9SDave Chinner 		pag = xfs_perag_get(mp, agno);
96430f712c9SDave Chinner 		if (!pag->pagi_inodeok) {
96530f712c9SDave Chinner 			xfs_ialloc_next_ag(mp);
96630f712c9SDave Chinner 			goto nextag;
96730f712c9SDave Chinner 		}
96830f712c9SDave Chinner 
96930f712c9SDave Chinner 		if (!pag->pagi_init) {
97030f712c9SDave Chinner 			error = xfs_ialloc_pagi_init(mp, tp, agno);
97130f712c9SDave Chinner 			if (error)
97230f712c9SDave Chinner 				goto nextag;
97330f712c9SDave Chinner 		}
97430f712c9SDave Chinner 
97530f712c9SDave Chinner 		if (pag->pagi_freecount) {
97630f712c9SDave Chinner 			xfs_perag_put(pag);
97730f712c9SDave Chinner 			return agno;
97830f712c9SDave Chinner 		}
97930f712c9SDave Chinner 
98030f712c9SDave Chinner 		if (!pag->pagf_init) {
98130f712c9SDave Chinner 			error = xfs_alloc_pagf_init(mp, tp, agno, flags);
98230f712c9SDave Chinner 			if (error)
98330f712c9SDave Chinner 				goto nextag;
98430f712c9SDave Chinner 		}
98530f712c9SDave Chinner 
98630f712c9SDave Chinner 		/*
9877a1df156SDave Chinner 		 * Check that there is enough free space for the file plus a
9887a1df156SDave Chinner 		 * chunk of inodes if we need to allocate some. If this is the
9897a1df156SDave Chinner 		 * first pass across the AGs, take into account the potential
9907a1df156SDave Chinner 		 * space needed for alignment of inode chunks when checking the
9917a1df156SDave Chinner 		 * longest contiguous free space in the AG - this prevents us
9927a1df156SDave Chinner 		 * from getting ENOSPC because we have free space larger than
9937a1df156SDave Chinner 		 * m_ialloc_blks but alignment constraints prevent us from using
9947a1df156SDave Chinner 		 * it.
9957a1df156SDave Chinner 		 *
9967a1df156SDave Chinner 		 * If we can't find an AG with space for full alignment slack to
9977a1df156SDave Chinner 		 * be taken into account, we must be near ENOSPC in all AGs.
9987a1df156SDave Chinner 		 * Hence we don't include alignment for the second pass and so
9997a1df156SDave Chinner 		 * if we fail allocation due to alignment issues then it is most
10007a1df156SDave Chinner 		 * likely a real ENOSPC condition.
100130f712c9SDave Chinner 		 */
1002066a1884SBrian Foster 		ineed = mp->m_ialloc_min_blks;
10037a1df156SDave Chinner 		if (flags && ineed > 1)
10047a1df156SDave Chinner 			ineed += xfs_ialloc_cluster_alignment(mp);
100530f712c9SDave Chinner 		longest = pag->pagf_longest;
100630f712c9SDave Chinner 		if (!longest)
100730f712c9SDave Chinner 			longest = pag->pagf_flcount > 0;
100830f712c9SDave Chinner 
100930f712c9SDave Chinner 		if (pag->pagf_freeblks >= needspace + ineed &&
101030f712c9SDave Chinner 		    longest >= ineed) {
101130f712c9SDave Chinner 			xfs_perag_put(pag);
101230f712c9SDave Chinner 			return agno;
101330f712c9SDave Chinner 		}
101430f712c9SDave Chinner nextag:
101530f712c9SDave Chinner 		xfs_perag_put(pag);
101630f712c9SDave Chinner 		/*
101730f712c9SDave Chinner 		 * No point in iterating over the rest, if we're shutting
101830f712c9SDave Chinner 		 * down.
101930f712c9SDave Chinner 		 */
102030f712c9SDave Chinner 		if (XFS_FORCED_SHUTDOWN(mp))
102130f712c9SDave Chinner 			return NULLAGNUMBER;
102230f712c9SDave Chinner 		agno++;
102330f712c9SDave Chinner 		if (agno >= agcount)
102430f712c9SDave Chinner 			agno = 0;
102530f712c9SDave Chinner 		if (agno == pagno) {
102630f712c9SDave Chinner 			if (flags == 0)
102730f712c9SDave Chinner 				return NULLAGNUMBER;
102830f712c9SDave Chinner 			flags = 0;
102930f712c9SDave Chinner 		}
103030f712c9SDave Chinner 	}
103130f712c9SDave Chinner }
103230f712c9SDave Chinner 
103330f712c9SDave Chinner /*
103430f712c9SDave Chinner  * Try to retrieve the next record to the left/right from the current one.
103530f712c9SDave Chinner  */
103630f712c9SDave Chinner STATIC int
103730f712c9SDave Chinner xfs_ialloc_next_rec(
103830f712c9SDave Chinner 	struct xfs_btree_cur	*cur,
103930f712c9SDave Chinner 	xfs_inobt_rec_incore_t	*rec,
104030f712c9SDave Chinner 	int			*done,
104130f712c9SDave Chinner 	int			left)
104230f712c9SDave Chinner {
104330f712c9SDave Chinner 	int                     error;
104430f712c9SDave Chinner 	int			i;
104530f712c9SDave Chinner 
104630f712c9SDave Chinner 	if (left)
104730f712c9SDave Chinner 		error = xfs_btree_decrement(cur, 0, &i);
104830f712c9SDave Chinner 	else
104930f712c9SDave Chinner 		error = xfs_btree_increment(cur, 0, &i);
105030f712c9SDave Chinner 
105130f712c9SDave Chinner 	if (error)
105230f712c9SDave Chinner 		return error;
105330f712c9SDave Chinner 	*done = !i;
105430f712c9SDave Chinner 	if (i) {
105530f712c9SDave Chinner 		error = xfs_inobt_get_rec(cur, rec, &i);
105630f712c9SDave Chinner 		if (error)
105730f712c9SDave Chinner 			return error;
10585fb5aeeeSEric Sandeen 		XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
105930f712c9SDave Chinner 	}
106030f712c9SDave Chinner 
106130f712c9SDave Chinner 	return 0;
106230f712c9SDave Chinner }
106330f712c9SDave Chinner 
106430f712c9SDave Chinner STATIC int
106530f712c9SDave Chinner xfs_ialloc_get_rec(
106630f712c9SDave Chinner 	struct xfs_btree_cur	*cur,
106730f712c9SDave Chinner 	xfs_agino_t		agino,
106830f712c9SDave Chinner 	xfs_inobt_rec_incore_t	*rec,
106930f712c9SDave Chinner 	int			*done)
107030f712c9SDave Chinner {
107130f712c9SDave Chinner 	int                     error;
107230f712c9SDave Chinner 	int			i;
107330f712c9SDave Chinner 
107430f712c9SDave Chinner 	error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_EQ, &i);
107530f712c9SDave Chinner 	if (error)
107630f712c9SDave Chinner 		return error;
107730f712c9SDave Chinner 	*done = !i;
107830f712c9SDave Chinner 	if (i) {
107930f712c9SDave Chinner 		error = xfs_inobt_get_rec(cur, rec, &i);
108030f712c9SDave Chinner 		if (error)
108130f712c9SDave Chinner 			return error;
10825fb5aeeeSEric Sandeen 		XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
108330f712c9SDave Chinner 	}
108430f712c9SDave Chinner 
108530f712c9SDave Chinner 	return 0;
108630f712c9SDave Chinner }
108730f712c9SDave Chinner 
108830f712c9SDave Chinner /*
108926dd5217SBrian Foster  * Return the offset of the first free inode in the record. If the inode chunk
109026dd5217SBrian Foster  * is sparsely allocated, we convert the record holemask to inode granularity
109126dd5217SBrian Foster  * and mask off the unallocated regions from the inode free mask.
1092d4cc540bSBrian Foster  */
1093d4cc540bSBrian Foster STATIC int
1094d4cc540bSBrian Foster xfs_inobt_first_free_inode(
1095d4cc540bSBrian Foster 	struct xfs_inobt_rec_incore	*rec)
1096d4cc540bSBrian Foster {
109726dd5217SBrian Foster 	xfs_inofree_t			realfree;
109826dd5217SBrian Foster 
109926dd5217SBrian Foster 	/* if there are no holes, return the first available offset */
110026dd5217SBrian Foster 	if (!xfs_inobt_issparse(rec->ir_holemask))
1101d4cc540bSBrian Foster 		return xfs_lowbit64(rec->ir_free);
110226dd5217SBrian Foster 
110326dd5217SBrian Foster 	realfree = xfs_inobt_irec_to_allocmask(rec);
110426dd5217SBrian Foster 	realfree &= rec->ir_free;
110526dd5217SBrian Foster 
110626dd5217SBrian Foster 	return xfs_lowbit64(realfree);
1107d4cc540bSBrian Foster }
1108d4cc540bSBrian Foster 
1109d4cc540bSBrian Foster /*
111030f712c9SDave Chinner  * Allocate an inode using the inobt-only algorithm.
111130f712c9SDave Chinner  */
111230f712c9SDave Chinner STATIC int
111330f712c9SDave Chinner xfs_dialloc_ag_inobt(
111430f712c9SDave Chinner 	struct xfs_trans	*tp,
111530f712c9SDave Chinner 	struct xfs_buf		*agbp,
111630f712c9SDave Chinner 	xfs_ino_t		parent,
111730f712c9SDave Chinner 	xfs_ino_t		*inop)
111830f712c9SDave Chinner {
111930f712c9SDave Chinner 	struct xfs_mount	*mp = tp->t_mountp;
112030f712c9SDave Chinner 	struct xfs_agi		*agi = XFS_BUF_TO_AGI(agbp);
112130f712c9SDave Chinner 	xfs_agnumber_t		agno = be32_to_cpu(agi->agi_seqno);
112230f712c9SDave Chinner 	xfs_agnumber_t		pagno = XFS_INO_TO_AGNO(mp, parent);
112330f712c9SDave Chinner 	xfs_agino_t		pagino = XFS_INO_TO_AGINO(mp, parent);
112430f712c9SDave Chinner 	struct xfs_perag	*pag;
112530f712c9SDave Chinner 	struct xfs_btree_cur	*cur, *tcur;
112630f712c9SDave Chinner 	struct xfs_inobt_rec_incore rec, trec;
112730f712c9SDave Chinner 	xfs_ino_t		ino;
112830f712c9SDave Chinner 	int			error;
112930f712c9SDave Chinner 	int			offset;
113030f712c9SDave Chinner 	int			i, j;
11312d32311cSCarlos Maiolino 	int			searchdistance = 10;
113230f712c9SDave Chinner 
113330f712c9SDave Chinner 	pag = xfs_perag_get(mp, agno);
113430f712c9SDave Chinner 
113530f712c9SDave Chinner 	ASSERT(pag->pagi_init);
113630f712c9SDave Chinner 	ASSERT(pag->pagi_inodeok);
113730f712c9SDave Chinner 	ASSERT(pag->pagi_freecount > 0);
113830f712c9SDave Chinner 
113930f712c9SDave Chinner  restart_pagno:
114030f712c9SDave Chinner 	cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
114130f712c9SDave Chinner 	/*
114230f712c9SDave Chinner 	 * If pagino is 0 (this is the root inode allocation) use newino.
114330f712c9SDave Chinner 	 * This must work because we've just allocated some.
114430f712c9SDave Chinner 	 */
114530f712c9SDave Chinner 	if (!pagino)
114630f712c9SDave Chinner 		pagino = be32_to_cpu(agi->agi_newino);
114730f712c9SDave Chinner 
114830f712c9SDave Chinner 	error = xfs_check_agi_freecount(cur, agi);
114930f712c9SDave Chinner 	if (error)
115030f712c9SDave Chinner 		goto error0;
115130f712c9SDave Chinner 
115230f712c9SDave Chinner 	/*
115330f712c9SDave Chinner 	 * If in the same AG as the parent, try to get near the parent.
115430f712c9SDave Chinner 	 */
115530f712c9SDave Chinner 	if (pagno == agno) {
115630f712c9SDave Chinner 		int		doneleft;	/* done, to the left */
115730f712c9SDave Chinner 		int		doneright;	/* done, to the right */
115830f712c9SDave Chinner 
115930f712c9SDave Chinner 		error = xfs_inobt_lookup(cur, pagino, XFS_LOOKUP_LE, &i);
116030f712c9SDave Chinner 		if (error)
116130f712c9SDave Chinner 			goto error0;
1162c29aad41SEric Sandeen 		XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
116330f712c9SDave Chinner 
116430f712c9SDave Chinner 		error = xfs_inobt_get_rec(cur, &rec, &j);
116530f712c9SDave Chinner 		if (error)
116630f712c9SDave Chinner 			goto error0;
1167c29aad41SEric Sandeen 		XFS_WANT_CORRUPTED_GOTO(mp, j == 1, error0);
116830f712c9SDave Chinner 
116930f712c9SDave Chinner 		if (rec.ir_freecount > 0) {
117030f712c9SDave Chinner 			/*
117130f712c9SDave Chinner 			 * Found a free inode in the same chunk
117230f712c9SDave Chinner 			 * as the parent, done.
117330f712c9SDave Chinner 			 */
117430f712c9SDave Chinner 			goto alloc_inode;
117530f712c9SDave Chinner 		}
117630f712c9SDave Chinner 
117730f712c9SDave Chinner 
117830f712c9SDave Chinner 		/*
117930f712c9SDave Chinner 		 * In the same AG as parent, but parent's chunk is full.
118030f712c9SDave Chinner 		 */
118130f712c9SDave Chinner 
118230f712c9SDave Chinner 		/* duplicate the cursor, search left & right simultaneously */
118330f712c9SDave Chinner 		error = xfs_btree_dup_cursor(cur, &tcur);
118430f712c9SDave Chinner 		if (error)
118530f712c9SDave Chinner 			goto error0;
118630f712c9SDave Chinner 
118730f712c9SDave Chinner 		/*
118830f712c9SDave Chinner 		 * Skip to last blocks looked up if same parent inode.
118930f712c9SDave Chinner 		 */
119030f712c9SDave Chinner 		if (pagino != NULLAGINO &&
119130f712c9SDave Chinner 		    pag->pagl_pagino == pagino &&
119230f712c9SDave Chinner 		    pag->pagl_leftrec != NULLAGINO &&
119330f712c9SDave Chinner 		    pag->pagl_rightrec != NULLAGINO) {
119430f712c9SDave Chinner 			error = xfs_ialloc_get_rec(tcur, pag->pagl_leftrec,
119530f712c9SDave Chinner 						   &trec, &doneleft);
119630f712c9SDave Chinner 			if (error)
119730f712c9SDave Chinner 				goto error1;
119830f712c9SDave Chinner 
119930f712c9SDave Chinner 			error = xfs_ialloc_get_rec(cur, pag->pagl_rightrec,
120030f712c9SDave Chinner 						   &rec, &doneright);
120130f712c9SDave Chinner 			if (error)
120230f712c9SDave Chinner 				goto error1;
120330f712c9SDave Chinner 		} else {
120430f712c9SDave Chinner 			/* search left with tcur, back up 1 record */
120530f712c9SDave Chinner 			error = xfs_ialloc_next_rec(tcur, &trec, &doneleft, 1);
120630f712c9SDave Chinner 			if (error)
120730f712c9SDave Chinner 				goto error1;
120830f712c9SDave Chinner 
120930f712c9SDave Chinner 			/* search right with cur, go forward 1 record. */
121030f712c9SDave Chinner 			error = xfs_ialloc_next_rec(cur, &rec, &doneright, 0);
121130f712c9SDave Chinner 			if (error)
121230f712c9SDave Chinner 				goto error1;
121330f712c9SDave Chinner 		}
121430f712c9SDave Chinner 
121530f712c9SDave Chinner 		/*
121630f712c9SDave Chinner 		 * Loop until we find an inode chunk with a free inode.
121730f712c9SDave Chinner 		 */
12182d32311cSCarlos Maiolino 		while (--searchdistance > 0 && (!doneleft || !doneright)) {
121930f712c9SDave Chinner 			int	useleft;  /* using left inode chunk this time */
122030f712c9SDave Chinner 
122130f712c9SDave Chinner 			/* figure out the closer block if both are valid. */
122230f712c9SDave Chinner 			if (!doneleft && !doneright) {
122330f712c9SDave Chinner 				useleft = pagino -
122430f712c9SDave Chinner 				 (trec.ir_startino + XFS_INODES_PER_CHUNK - 1) <
122530f712c9SDave Chinner 				  rec.ir_startino - pagino;
122630f712c9SDave Chinner 			} else {
122730f712c9SDave Chinner 				useleft = !doneleft;
122830f712c9SDave Chinner 			}
122930f712c9SDave Chinner 
123030f712c9SDave Chinner 			/* free inodes to the left? */
123130f712c9SDave Chinner 			if (useleft && trec.ir_freecount) {
123230f712c9SDave Chinner 				xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
123330f712c9SDave Chinner 				cur = tcur;
123430f712c9SDave Chinner 
123530f712c9SDave Chinner 				pag->pagl_leftrec = trec.ir_startino;
123630f712c9SDave Chinner 				pag->pagl_rightrec = rec.ir_startino;
123730f712c9SDave Chinner 				pag->pagl_pagino = pagino;
1238c44245b3SOmar Sandoval 				rec = trec;
123930f712c9SDave Chinner 				goto alloc_inode;
124030f712c9SDave Chinner 			}
124130f712c9SDave Chinner 
124230f712c9SDave Chinner 			/* free inodes to the right? */
124330f712c9SDave Chinner 			if (!useleft && rec.ir_freecount) {
124430f712c9SDave Chinner 				xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
124530f712c9SDave Chinner 
124630f712c9SDave Chinner 				pag->pagl_leftrec = trec.ir_startino;
124730f712c9SDave Chinner 				pag->pagl_rightrec = rec.ir_startino;
124830f712c9SDave Chinner 				pag->pagl_pagino = pagino;
124930f712c9SDave Chinner 				goto alloc_inode;
125030f712c9SDave Chinner 			}
125130f712c9SDave Chinner 
125230f712c9SDave Chinner 			/* get next record to check */
125330f712c9SDave Chinner 			if (useleft) {
125430f712c9SDave Chinner 				error = xfs_ialloc_next_rec(tcur, &trec,
125530f712c9SDave Chinner 								 &doneleft, 1);
125630f712c9SDave Chinner 			} else {
125730f712c9SDave Chinner 				error = xfs_ialloc_next_rec(cur, &rec,
125830f712c9SDave Chinner 								 &doneright, 0);
125930f712c9SDave Chinner 			}
126030f712c9SDave Chinner 			if (error)
126130f712c9SDave Chinner 				goto error1;
126230f712c9SDave Chinner 		}
126330f712c9SDave Chinner 
12642d32311cSCarlos Maiolino 		if (searchdistance <= 0) {
12652d32311cSCarlos Maiolino 			/*
12662d32311cSCarlos Maiolino 			 * Not in range - save last search
12672d32311cSCarlos Maiolino 			 * location and allocate a new inode
12682d32311cSCarlos Maiolino 			 */
12692d32311cSCarlos Maiolino 			xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
12702d32311cSCarlos Maiolino 			pag->pagl_leftrec = trec.ir_startino;
12712d32311cSCarlos Maiolino 			pag->pagl_rightrec = rec.ir_startino;
12722d32311cSCarlos Maiolino 			pag->pagl_pagino = pagino;
12732d32311cSCarlos Maiolino 
12742d32311cSCarlos Maiolino 		} else {
127530f712c9SDave Chinner 			/*
127630f712c9SDave Chinner 			 * We've reached the end of the btree. because
127730f712c9SDave Chinner 			 * we are only searching a small chunk of the
127830f712c9SDave Chinner 			 * btree each search, there is obviously free
127930f712c9SDave Chinner 			 * inodes closer to the parent inode than we
128030f712c9SDave Chinner 			 * are now. restart the search again.
128130f712c9SDave Chinner 			 */
128230f712c9SDave Chinner 			pag->pagl_pagino = NULLAGINO;
128330f712c9SDave Chinner 			pag->pagl_leftrec = NULLAGINO;
128430f712c9SDave Chinner 			pag->pagl_rightrec = NULLAGINO;
128530f712c9SDave Chinner 			xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
128630f712c9SDave Chinner 			xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
128730f712c9SDave Chinner 			goto restart_pagno;
128830f712c9SDave Chinner 		}
12892d32311cSCarlos Maiolino 	}
129030f712c9SDave Chinner 
129130f712c9SDave Chinner 	/*
129230f712c9SDave Chinner 	 * In a different AG from the parent.
129330f712c9SDave Chinner 	 * See if the most recently allocated block has any free.
129430f712c9SDave Chinner 	 */
129530f712c9SDave Chinner 	if (agi->agi_newino != cpu_to_be32(NULLAGINO)) {
129630f712c9SDave Chinner 		error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino),
129730f712c9SDave Chinner 					 XFS_LOOKUP_EQ, &i);
129830f712c9SDave Chinner 		if (error)
129930f712c9SDave Chinner 			goto error0;
130030f712c9SDave Chinner 
130130f712c9SDave Chinner 		if (i == 1) {
130230f712c9SDave Chinner 			error = xfs_inobt_get_rec(cur, &rec, &j);
130330f712c9SDave Chinner 			if (error)
130430f712c9SDave Chinner 				goto error0;
130530f712c9SDave Chinner 
130630f712c9SDave Chinner 			if (j == 1 && rec.ir_freecount > 0) {
130730f712c9SDave Chinner 				/*
130830f712c9SDave Chinner 				 * The last chunk allocated in the group
130930f712c9SDave Chinner 				 * still has a free inode.
131030f712c9SDave Chinner 				 */
131130f712c9SDave Chinner 				goto alloc_inode;
131230f712c9SDave Chinner 			}
131330f712c9SDave Chinner 		}
131430f712c9SDave Chinner 	}
131530f712c9SDave Chinner 
131630f712c9SDave Chinner 	/*
131730f712c9SDave Chinner 	 * None left in the last group, search the whole AG
131830f712c9SDave Chinner 	 */
131930f712c9SDave Chinner 	error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
132030f712c9SDave Chinner 	if (error)
132130f712c9SDave Chinner 		goto error0;
1322c29aad41SEric Sandeen 	XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
132330f712c9SDave Chinner 
132430f712c9SDave Chinner 	for (;;) {
132530f712c9SDave Chinner 		error = xfs_inobt_get_rec(cur, &rec, &i);
132630f712c9SDave Chinner 		if (error)
132730f712c9SDave Chinner 			goto error0;
1328c29aad41SEric Sandeen 		XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
132930f712c9SDave Chinner 		if (rec.ir_freecount > 0)
133030f712c9SDave Chinner 			break;
133130f712c9SDave Chinner 		error = xfs_btree_increment(cur, 0, &i);
133230f712c9SDave Chinner 		if (error)
133330f712c9SDave Chinner 			goto error0;
1334c29aad41SEric Sandeen 		XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
133530f712c9SDave Chinner 	}
133630f712c9SDave Chinner 
133730f712c9SDave Chinner alloc_inode:
1338d4cc540bSBrian Foster 	offset = xfs_inobt_first_free_inode(&rec);
133930f712c9SDave Chinner 	ASSERT(offset >= 0);
134030f712c9SDave Chinner 	ASSERT(offset < XFS_INODES_PER_CHUNK);
134130f712c9SDave Chinner 	ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
134230f712c9SDave Chinner 				   XFS_INODES_PER_CHUNK) == 0);
134330f712c9SDave Chinner 	ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
134430f712c9SDave Chinner 	rec.ir_free &= ~XFS_INOBT_MASK(offset);
134530f712c9SDave Chinner 	rec.ir_freecount--;
134630f712c9SDave Chinner 	error = xfs_inobt_update(cur, &rec);
134730f712c9SDave Chinner 	if (error)
134830f712c9SDave Chinner 		goto error0;
134930f712c9SDave Chinner 	be32_add_cpu(&agi->agi_freecount, -1);
135030f712c9SDave Chinner 	xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
135130f712c9SDave Chinner 	pag->pagi_freecount--;
135230f712c9SDave Chinner 
135330f712c9SDave Chinner 	error = xfs_check_agi_freecount(cur, agi);
135430f712c9SDave Chinner 	if (error)
135530f712c9SDave Chinner 		goto error0;
135630f712c9SDave Chinner 
135730f712c9SDave Chinner 	xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
135830f712c9SDave Chinner 	xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
135930f712c9SDave Chinner 	xfs_perag_put(pag);
136030f712c9SDave Chinner 	*inop = ino;
136130f712c9SDave Chinner 	return 0;
136230f712c9SDave Chinner error1:
136330f712c9SDave Chinner 	xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
136430f712c9SDave Chinner error0:
136530f712c9SDave Chinner 	xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
136630f712c9SDave Chinner 	xfs_perag_put(pag);
136730f712c9SDave Chinner 	return error;
136830f712c9SDave Chinner }
136930f712c9SDave Chinner 
137030f712c9SDave Chinner /*
137130f712c9SDave Chinner  * Use the free inode btree to allocate an inode based on distance from the
137230f712c9SDave Chinner  * parent. Note that the provided cursor may be deleted and replaced.
137330f712c9SDave Chinner  */
137430f712c9SDave Chinner STATIC int
137530f712c9SDave Chinner xfs_dialloc_ag_finobt_near(
137630f712c9SDave Chinner 	xfs_agino_t			pagino,
137730f712c9SDave Chinner 	struct xfs_btree_cur		**ocur,
137830f712c9SDave Chinner 	struct xfs_inobt_rec_incore	*rec)
137930f712c9SDave Chinner {
138030f712c9SDave Chinner 	struct xfs_btree_cur		*lcur = *ocur;	/* left search cursor */
138130f712c9SDave Chinner 	struct xfs_btree_cur		*rcur;	/* right search cursor */
138230f712c9SDave Chinner 	struct xfs_inobt_rec_incore	rrec;
138330f712c9SDave Chinner 	int				error;
138430f712c9SDave Chinner 	int				i, j;
138530f712c9SDave Chinner 
138630f712c9SDave Chinner 	error = xfs_inobt_lookup(lcur, pagino, XFS_LOOKUP_LE, &i);
138730f712c9SDave Chinner 	if (error)
138830f712c9SDave Chinner 		return error;
138930f712c9SDave Chinner 
139030f712c9SDave Chinner 	if (i == 1) {
139130f712c9SDave Chinner 		error = xfs_inobt_get_rec(lcur, rec, &i);
139230f712c9SDave Chinner 		if (error)
139330f712c9SDave Chinner 			return error;
13945fb5aeeeSEric Sandeen 		XFS_WANT_CORRUPTED_RETURN(lcur->bc_mp, i == 1);
139530f712c9SDave Chinner 
139630f712c9SDave Chinner 		/*
139730f712c9SDave Chinner 		 * See if we've landed in the parent inode record. The finobt
139830f712c9SDave Chinner 		 * only tracks chunks with at least one free inode, so record
139930f712c9SDave Chinner 		 * existence is enough.
140030f712c9SDave Chinner 		 */
140130f712c9SDave Chinner 		if (pagino >= rec->ir_startino &&
140230f712c9SDave Chinner 		    pagino < (rec->ir_startino + XFS_INODES_PER_CHUNK))
140330f712c9SDave Chinner 			return 0;
140430f712c9SDave Chinner 	}
140530f712c9SDave Chinner 
140630f712c9SDave Chinner 	error = xfs_btree_dup_cursor(lcur, &rcur);
140730f712c9SDave Chinner 	if (error)
140830f712c9SDave Chinner 		return error;
140930f712c9SDave Chinner 
141030f712c9SDave Chinner 	error = xfs_inobt_lookup(rcur, pagino, XFS_LOOKUP_GE, &j);
141130f712c9SDave Chinner 	if (error)
141230f712c9SDave Chinner 		goto error_rcur;
141330f712c9SDave Chinner 	if (j == 1) {
141430f712c9SDave Chinner 		error = xfs_inobt_get_rec(rcur, &rrec, &j);
141530f712c9SDave Chinner 		if (error)
141630f712c9SDave Chinner 			goto error_rcur;
1417c29aad41SEric Sandeen 		XFS_WANT_CORRUPTED_GOTO(lcur->bc_mp, j == 1, error_rcur);
141830f712c9SDave Chinner 	}
141930f712c9SDave Chinner 
1420c29aad41SEric Sandeen 	XFS_WANT_CORRUPTED_GOTO(lcur->bc_mp, i == 1 || j == 1, error_rcur);
142130f712c9SDave Chinner 	if (i == 1 && j == 1) {
142230f712c9SDave Chinner 		/*
142330f712c9SDave Chinner 		 * Both the left and right records are valid. Choose the closer
142430f712c9SDave Chinner 		 * inode chunk to the target.
142530f712c9SDave Chinner 		 */
142630f712c9SDave Chinner 		if ((pagino - rec->ir_startino + XFS_INODES_PER_CHUNK - 1) >
142730f712c9SDave Chinner 		    (rrec.ir_startino - pagino)) {
142830f712c9SDave Chinner 			*rec = rrec;
142930f712c9SDave Chinner 			xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR);
143030f712c9SDave Chinner 			*ocur = rcur;
143130f712c9SDave Chinner 		} else {
143230f712c9SDave Chinner 			xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR);
143330f712c9SDave Chinner 		}
143430f712c9SDave Chinner 	} else if (j == 1) {
143530f712c9SDave Chinner 		/* only the right record is valid */
143630f712c9SDave Chinner 		*rec = rrec;
143730f712c9SDave Chinner 		xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR);
143830f712c9SDave Chinner 		*ocur = rcur;
143930f712c9SDave Chinner 	} else if (i == 1) {
144030f712c9SDave Chinner 		/* only the left record is valid */
144130f712c9SDave Chinner 		xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR);
144230f712c9SDave Chinner 	}
144330f712c9SDave Chinner 
144430f712c9SDave Chinner 	return 0;
144530f712c9SDave Chinner 
144630f712c9SDave Chinner error_rcur:
144730f712c9SDave Chinner 	xfs_btree_del_cursor(rcur, XFS_BTREE_ERROR);
144830f712c9SDave Chinner 	return error;
144930f712c9SDave Chinner }
145030f712c9SDave Chinner 
145130f712c9SDave Chinner /*
145230f712c9SDave Chinner  * Use the free inode btree to find a free inode based on a newino hint. If
145330f712c9SDave Chinner  * the hint is NULL, find the first free inode in the AG.
145430f712c9SDave Chinner  */
145530f712c9SDave Chinner STATIC int
145630f712c9SDave Chinner xfs_dialloc_ag_finobt_newino(
145730f712c9SDave Chinner 	struct xfs_agi			*agi,
145830f712c9SDave Chinner 	struct xfs_btree_cur		*cur,
145930f712c9SDave Chinner 	struct xfs_inobt_rec_incore	*rec)
146030f712c9SDave Chinner {
146130f712c9SDave Chinner 	int error;
146230f712c9SDave Chinner 	int i;
146330f712c9SDave Chinner 
146430f712c9SDave Chinner 	if (agi->agi_newino != cpu_to_be32(NULLAGINO)) {
1465e68ed775SDave Chinner 		error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino),
1466e68ed775SDave Chinner 					 XFS_LOOKUP_EQ, &i);
146730f712c9SDave Chinner 		if (error)
146830f712c9SDave Chinner 			return error;
146930f712c9SDave Chinner 		if (i == 1) {
147030f712c9SDave Chinner 			error = xfs_inobt_get_rec(cur, rec, &i);
147130f712c9SDave Chinner 			if (error)
147230f712c9SDave Chinner 				return error;
14735fb5aeeeSEric Sandeen 			XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
147430f712c9SDave Chinner 			return 0;
147530f712c9SDave Chinner 		}
147630f712c9SDave Chinner 	}
147730f712c9SDave Chinner 
147830f712c9SDave Chinner 	/*
147930f712c9SDave Chinner 	 * Find the first inode available in the AG.
148030f712c9SDave Chinner 	 */
148130f712c9SDave Chinner 	error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
148230f712c9SDave Chinner 	if (error)
148330f712c9SDave Chinner 		return error;
14845fb5aeeeSEric Sandeen 	XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
148530f712c9SDave Chinner 
148630f712c9SDave Chinner 	error = xfs_inobt_get_rec(cur, rec, &i);
148730f712c9SDave Chinner 	if (error)
148830f712c9SDave Chinner 		return error;
14895fb5aeeeSEric Sandeen 	XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
149030f712c9SDave Chinner 
149130f712c9SDave Chinner 	return 0;
149230f712c9SDave Chinner }
149330f712c9SDave Chinner 
149430f712c9SDave Chinner /*
149530f712c9SDave Chinner  * Update the inobt based on a modification made to the finobt. Also ensure that
149630f712c9SDave Chinner  * the records from both trees are equivalent post-modification.
149730f712c9SDave Chinner  */
149830f712c9SDave Chinner STATIC int
149930f712c9SDave Chinner xfs_dialloc_ag_update_inobt(
150030f712c9SDave Chinner 	struct xfs_btree_cur		*cur,	/* inobt cursor */
150130f712c9SDave Chinner 	struct xfs_inobt_rec_incore	*frec,	/* finobt record */
150230f712c9SDave Chinner 	int				offset) /* inode offset */
150330f712c9SDave Chinner {
150430f712c9SDave Chinner 	struct xfs_inobt_rec_incore	rec;
150530f712c9SDave Chinner 	int				error;
150630f712c9SDave Chinner 	int				i;
150730f712c9SDave Chinner 
150830f712c9SDave Chinner 	error = xfs_inobt_lookup(cur, frec->ir_startino, XFS_LOOKUP_EQ, &i);
150930f712c9SDave Chinner 	if (error)
151030f712c9SDave Chinner 		return error;
15115fb5aeeeSEric Sandeen 	XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
151230f712c9SDave Chinner 
151330f712c9SDave Chinner 	error = xfs_inobt_get_rec(cur, &rec, &i);
151430f712c9SDave Chinner 	if (error)
151530f712c9SDave Chinner 		return error;
15165fb5aeeeSEric Sandeen 	XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
151730f712c9SDave Chinner 	ASSERT((XFS_AGINO_TO_OFFSET(cur->bc_mp, rec.ir_startino) %
151830f712c9SDave Chinner 				   XFS_INODES_PER_CHUNK) == 0);
151930f712c9SDave Chinner 
152030f712c9SDave Chinner 	rec.ir_free &= ~XFS_INOBT_MASK(offset);
152130f712c9SDave Chinner 	rec.ir_freecount--;
152230f712c9SDave Chinner 
15235fb5aeeeSEric Sandeen 	XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, (rec.ir_free == frec->ir_free) &&
152430f712c9SDave Chinner 				  (rec.ir_freecount == frec->ir_freecount));
152530f712c9SDave Chinner 
1526b72091f2Skbuild test robot 	return xfs_inobt_update(cur, &rec);
152730f712c9SDave Chinner }
152830f712c9SDave Chinner 
152930f712c9SDave Chinner /*
153030f712c9SDave Chinner  * Allocate an inode using the free inode btree, if available. Otherwise, fall
153130f712c9SDave Chinner  * back to the inobt search algorithm.
153230f712c9SDave Chinner  *
153330f712c9SDave Chinner  * The caller selected an AG for us, and made sure that free inodes are
153430f712c9SDave Chinner  * available.
153530f712c9SDave Chinner  */
153630f712c9SDave Chinner STATIC int
153730f712c9SDave Chinner xfs_dialloc_ag(
153830f712c9SDave Chinner 	struct xfs_trans	*tp,
153930f712c9SDave Chinner 	struct xfs_buf		*agbp,
154030f712c9SDave Chinner 	xfs_ino_t		parent,
154130f712c9SDave Chinner 	xfs_ino_t		*inop)
154230f712c9SDave Chinner {
154330f712c9SDave Chinner 	struct xfs_mount		*mp = tp->t_mountp;
154430f712c9SDave Chinner 	struct xfs_agi			*agi = XFS_BUF_TO_AGI(agbp);
154530f712c9SDave Chinner 	xfs_agnumber_t			agno = be32_to_cpu(agi->agi_seqno);
154630f712c9SDave Chinner 	xfs_agnumber_t			pagno = XFS_INO_TO_AGNO(mp, parent);
154730f712c9SDave Chinner 	xfs_agino_t			pagino = XFS_INO_TO_AGINO(mp, parent);
154830f712c9SDave Chinner 	struct xfs_perag		*pag;
154930f712c9SDave Chinner 	struct xfs_btree_cur		*cur;	/* finobt cursor */
155030f712c9SDave Chinner 	struct xfs_btree_cur		*icur;	/* inobt cursor */
155130f712c9SDave Chinner 	struct xfs_inobt_rec_incore	rec;
155230f712c9SDave Chinner 	xfs_ino_t			ino;
155330f712c9SDave Chinner 	int				error;
155430f712c9SDave Chinner 	int				offset;
155530f712c9SDave Chinner 	int				i;
155630f712c9SDave Chinner 
155730f712c9SDave Chinner 	if (!xfs_sb_version_hasfinobt(&mp->m_sb))
155830f712c9SDave Chinner 		return xfs_dialloc_ag_inobt(tp, agbp, parent, inop);
155930f712c9SDave Chinner 
156030f712c9SDave Chinner 	pag = xfs_perag_get(mp, agno);
156130f712c9SDave Chinner 
156230f712c9SDave Chinner 	/*
156330f712c9SDave Chinner 	 * If pagino is 0 (this is the root inode allocation) use newino.
156430f712c9SDave Chinner 	 * This must work because we've just allocated some.
156530f712c9SDave Chinner 	 */
156630f712c9SDave Chinner 	if (!pagino)
156730f712c9SDave Chinner 		pagino = be32_to_cpu(agi->agi_newino);
156830f712c9SDave Chinner 
156930f712c9SDave Chinner 	cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_FINO);
157030f712c9SDave Chinner 
157130f712c9SDave Chinner 	error = xfs_check_agi_freecount(cur, agi);
157230f712c9SDave Chinner 	if (error)
157330f712c9SDave Chinner 		goto error_cur;
157430f712c9SDave Chinner 
157530f712c9SDave Chinner 	/*
157630f712c9SDave Chinner 	 * The search algorithm depends on whether we're in the same AG as the
157730f712c9SDave Chinner 	 * parent. If so, find the closest available inode to the parent. If
157830f712c9SDave Chinner 	 * not, consider the agi hint or find the first free inode in the AG.
157930f712c9SDave Chinner 	 */
158030f712c9SDave Chinner 	if (agno == pagno)
158130f712c9SDave Chinner 		error = xfs_dialloc_ag_finobt_near(pagino, &cur, &rec);
158230f712c9SDave Chinner 	else
158330f712c9SDave Chinner 		error = xfs_dialloc_ag_finobt_newino(agi, cur, &rec);
158430f712c9SDave Chinner 	if (error)
158530f712c9SDave Chinner 		goto error_cur;
158630f712c9SDave Chinner 
1587d4cc540bSBrian Foster 	offset = xfs_inobt_first_free_inode(&rec);
158830f712c9SDave Chinner 	ASSERT(offset >= 0);
158930f712c9SDave Chinner 	ASSERT(offset < XFS_INODES_PER_CHUNK);
159030f712c9SDave Chinner 	ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
159130f712c9SDave Chinner 				   XFS_INODES_PER_CHUNK) == 0);
159230f712c9SDave Chinner 	ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
159330f712c9SDave Chinner 
159430f712c9SDave Chinner 	/*
159530f712c9SDave Chinner 	 * Modify or remove the finobt record.
159630f712c9SDave Chinner 	 */
159730f712c9SDave Chinner 	rec.ir_free &= ~XFS_INOBT_MASK(offset);
159830f712c9SDave Chinner 	rec.ir_freecount--;
159930f712c9SDave Chinner 	if (rec.ir_freecount)
160030f712c9SDave Chinner 		error = xfs_inobt_update(cur, &rec);
160130f712c9SDave Chinner 	else
160230f712c9SDave Chinner 		error = xfs_btree_delete(cur, &i);
160330f712c9SDave Chinner 	if (error)
160430f712c9SDave Chinner 		goto error_cur;
160530f712c9SDave Chinner 
160630f712c9SDave Chinner 	/*
160730f712c9SDave Chinner 	 * The finobt has now been updated appropriately. We haven't updated the
160830f712c9SDave Chinner 	 * agi and superblock yet, so we can create an inobt cursor and validate
160930f712c9SDave Chinner 	 * the original freecount. If all is well, make the equivalent update to
161030f712c9SDave Chinner 	 * the inobt using the finobt record and offset information.
161130f712c9SDave Chinner 	 */
161230f712c9SDave Chinner 	icur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
161330f712c9SDave Chinner 
161430f712c9SDave Chinner 	error = xfs_check_agi_freecount(icur, agi);
161530f712c9SDave Chinner 	if (error)
161630f712c9SDave Chinner 		goto error_icur;
161730f712c9SDave Chinner 
161830f712c9SDave Chinner 	error = xfs_dialloc_ag_update_inobt(icur, &rec, offset);
161930f712c9SDave Chinner 	if (error)
162030f712c9SDave Chinner 		goto error_icur;
162130f712c9SDave Chinner 
162230f712c9SDave Chinner 	/*
162330f712c9SDave Chinner 	 * Both trees have now been updated. We must update the perag and
162430f712c9SDave Chinner 	 * superblock before we can check the freecount for each btree.
162530f712c9SDave Chinner 	 */
162630f712c9SDave Chinner 	be32_add_cpu(&agi->agi_freecount, -1);
162730f712c9SDave Chinner 	xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
162830f712c9SDave Chinner 	pag->pagi_freecount--;
162930f712c9SDave Chinner 
163030f712c9SDave Chinner 	xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
163130f712c9SDave Chinner 
163230f712c9SDave Chinner 	error = xfs_check_agi_freecount(icur, agi);
163330f712c9SDave Chinner 	if (error)
163430f712c9SDave Chinner 		goto error_icur;
163530f712c9SDave Chinner 	error = xfs_check_agi_freecount(cur, agi);
163630f712c9SDave Chinner 	if (error)
163730f712c9SDave Chinner 		goto error_icur;
163830f712c9SDave Chinner 
163930f712c9SDave Chinner 	xfs_btree_del_cursor(icur, XFS_BTREE_NOERROR);
164030f712c9SDave Chinner 	xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
164130f712c9SDave Chinner 	xfs_perag_put(pag);
164230f712c9SDave Chinner 	*inop = ino;
164330f712c9SDave Chinner 	return 0;
164430f712c9SDave Chinner 
164530f712c9SDave Chinner error_icur:
164630f712c9SDave Chinner 	xfs_btree_del_cursor(icur, XFS_BTREE_ERROR);
164730f712c9SDave Chinner error_cur:
164830f712c9SDave Chinner 	xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
164930f712c9SDave Chinner 	xfs_perag_put(pag);
165030f712c9SDave Chinner 	return error;
165130f712c9SDave Chinner }
165230f712c9SDave Chinner 
165330f712c9SDave Chinner /*
165430f712c9SDave Chinner  * Allocate an inode on disk.
165530f712c9SDave Chinner  *
165630f712c9SDave Chinner  * Mode is used to tell whether the new inode will need space, and whether it
165730f712c9SDave Chinner  * is a directory.
165830f712c9SDave Chinner  *
165930f712c9SDave Chinner  * This function is designed to be called twice if it has to do an allocation
166030f712c9SDave Chinner  * to make more free inodes.  On the first call, *IO_agbp should be set to NULL.
166130f712c9SDave Chinner  * If an inode is available without having to performn an allocation, an inode
166230f712c9SDave Chinner  * number is returned.  In this case, *IO_agbp is set to NULL.  If an allocation
166330f712c9SDave Chinner  * needs to be done, xfs_dialloc returns the current AGI buffer in *IO_agbp.
166430f712c9SDave Chinner  * The caller should then commit the current transaction, allocate a
166530f712c9SDave Chinner  * new transaction, and call xfs_dialloc() again, passing in the previous value
166630f712c9SDave Chinner  * of *IO_agbp.  IO_agbp should be held across the transactions. Since the AGI
166730f712c9SDave Chinner  * buffer is locked across the two calls, the second call is guaranteed to have
166830f712c9SDave Chinner  * a free inode available.
166930f712c9SDave Chinner  *
167030f712c9SDave Chinner  * Once we successfully pick an inode its number is returned and the on-disk
167130f712c9SDave Chinner  * data structures are updated.  The inode itself is not read in, since doing so
167230f712c9SDave Chinner  * would break ordering constraints with xfs_reclaim.
167330f712c9SDave Chinner  */
167430f712c9SDave Chinner int
167530f712c9SDave Chinner xfs_dialloc(
167630f712c9SDave Chinner 	struct xfs_trans	*tp,
167730f712c9SDave Chinner 	xfs_ino_t		parent,
167830f712c9SDave Chinner 	umode_t			mode,
167930f712c9SDave Chinner 	struct xfs_buf		**IO_agbp,
168030f712c9SDave Chinner 	xfs_ino_t		*inop)
168130f712c9SDave Chinner {
168230f712c9SDave Chinner 	struct xfs_mount	*mp = tp->t_mountp;
168330f712c9SDave Chinner 	struct xfs_buf		*agbp;
168430f712c9SDave Chinner 	xfs_agnumber_t		agno;
168530f712c9SDave Chinner 	int			error;
168630f712c9SDave Chinner 	int			ialloced;
168730f712c9SDave Chinner 	int			noroom = 0;
168830f712c9SDave Chinner 	xfs_agnumber_t		start_agno;
168930f712c9SDave Chinner 	struct xfs_perag	*pag;
1690f59cf5c2SChristoph Hellwig 	int			okalloc = 1;
169130f712c9SDave Chinner 
169230f712c9SDave Chinner 	if (*IO_agbp) {
169330f712c9SDave Chinner 		/*
169430f712c9SDave Chinner 		 * If the caller passes in a pointer to the AGI buffer,
169530f712c9SDave Chinner 		 * continue where we left off before.  In this case, we
169630f712c9SDave Chinner 		 * know that the allocation group has free inodes.
169730f712c9SDave Chinner 		 */
169830f712c9SDave Chinner 		agbp = *IO_agbp;
169930f712c9SDave Chinner 		goto out_alloc;
170030f712c9SDave Chinner 	}
170130f712c9SDave Chinner 
170230f712c9SDave Chinner 	/*
170330f712c9SDave Chinner 	 * We do not have an agbp, so select an initial allocation
170430f712c9SDave Chinner 	 * group for inode allocation.
170530f712c9SDave Chinner 	 */
1706f59cf5c2SChristoph Hellwig 	start_agno = xfs_ialloc_ag_select(tp, parent, mode);
170730f712c9SDave Chinner 	if (start_agno == NULLAGNUMBER) {
170830f712c9SDave Chinner 		*inop = NULLFSINO;
170930f712c9SDave Chinner 		return 0;
171030f712c9SDave Chinner 	}
171130f712c9SDave Chinner 
171230f712c9SDave Chinner 	/*
171330f712c9SDave Chinner 	 * If we have already hit the ceiling of inode blocks then clear
171430f712c9SDave Chinner 	 * okalloc so we scan all available agi structures for a free
171530f712c9SDave Chinner 	 * inode.
171674f9ce1cSGeorge Wang 	 *
171774f9ce1cSGeorge Wang 	 * Read rough value of mp->m_icount by percpu_counter_read_positive,
171874f9ce1cSGeorge Wang 	 * which will sacrifice the preciseness but improve the performance.
171930f712c9SDave Chinner 	 */
172030f712c9SDave Chinner 	if (mp->m_maxicount &&
172174f9ce1cSGeorge Wang 	    percpu_counter_read_positive(&mp->m_icount) + mp->m_ialloc_inos
172274f9ce1cSGeorge Wang 							> mp->m_maxicount) {
172330f712c9SDave Chinner 		noroom = 1;
172430f712c9SDave Chinner 		okalloc = 0;
172530f712c9SDave Chinner 	}
172630f712c9SDave Chinner 
172730f712c9SDave Chinner 	/*
172830f712c9SDave Chinner 	 * Loop until we find an allocation group that either has free inodes
172930f712c9SDave Chinner 	 * or in which we can allocate some inodes.  Iterate through the
173030f712c9SDave Chinner 	 * allocation groups upward, wrapping at the end.
173130f712c9SDave Chinner 	 */
173230f712c9SDave Chinner 	agno = start_agno;
173330f712c9SDave Chinner 	for (;;) {
173430f712c9SDave Chinner 		pag = xfs_perag_get(mp, agno);
173530f712c9SDave Chinner 		if (!pag->pagi_inodeok) {
173630f712c9SDave Chinner 			xfs_ialloc_next_ag(mp);
173730f712c9SDave Chinner 			goto nextag;
173830f712c9SDave Chinner 		}
173930f712c9SDave Chinner 
174030f712c9SDave Chinner 		if (!pag->pagi_init) {
174130f712c9SDave Chinner 			error = xfs_ialloc_pagi_init(mp, tp, agno);
174230f712c9SDave Chinner 			if (error)
174330f712c9SDave Chinner 				goto out_error;
174430f712c9SDave Chinner 		}
174530f712c9SDave Chinner 
174630f712c9SDave Chinner 		/*
174730f712c9SDave Chinner 		 * Do a first racy fast path check if this AG is usable.
174830f712c9SDave Chinner 		 */
174930f712c9SDave Chinner 		if (!pag->pagi_freecount && !okalloc)
175030f712c9SDave Chinner 			goto nextag;
175130f712c9SDave Chinner 
175230f712c9SDave Chinner 		/*
175330f712c9SDave Chinner 		 * Then read in the AGI buffer and recheck with the AGI buffer
175430f712c9SDave Chinner 		 * lock held.
175530f712c9SDave Chinner 		 */
175630f712c9SDave Chinner 		error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
175730f712c9SDave Chinner 		if (error)
175830f712c9SDave Chinner 			goto out_error;
175930f712c9SDave Chinner 
176030f712c9SDave Chinner 		if (pag->pagi_freecount) {
176130f712c9SDave Chinner 			xfs_perag_put(pag);
176230f712c9SDave Chinner 			goto out_alloc;
176330f712c9SDave Chinner 		}
176430f712c9SDave Chinner 
176530f712c9SDave Chinner 		if (!okalloc)
176630f712c9SDave Chinner 			goto nextag_relse_buffer;
176730f712c9SDave Chinner 
176830f712c9SDave Chinner 
176930f712c9SDave Chinner 		error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced);
177030f712c9SDave Chinner 		if (error) {
177130f712c9SDave Chinner 			xfs_trans_brelse(tp, agbp);
177230f712c9SDave Chinner 
17732451337dSDave Chinner 			if (error != -ENOSPC)
177430f712c9SDave Chinner 				goto out_error;
177530f712c9SDave Chinner 
177630f712c9SDave Chinner 			xfs_perag_put(pag);
177730f712c9SDave Chinner 			*inop = NULLFSINO;
177830f712c9SDave Chinner 			return 0;
177930f712c9SDave Chinner 		}
178030f712c9SDave Chinner 
178130f712c9SDave Chinner 		if (ialloced) {
178230f712c9SDave Chinner 			/*
178330f712c9SDave Chinner 			 * We successfully allocated some inodes, return
178430f712c9SDave Chinner 			 * the current context to the caller so that it
178530f712c9SDave Chinner 			 * can commit the current transaction and call
178630f712c9SDave Chinner 			 * us again where we left off.
178730f712c9SDave Chinner 			 */
178830f712c9SDave Chinner 			ASSERT(pag->pagi_freecount > 0);
178930f712c9SDave Chinner 			xfs_perag_put(pag);
179030f712c9SDave Chinner 
179130f712c9SDave Chinner 			*IO_agbp = agbp;
179230f712c9SDave Chinner 			*inop = NULLFSINO;
179330f712c9SDave Chinner 			return 0;
179430f712c9SDave Chinner 		}
179530f712c9SDave Chinner 
179630f712c9SDave Chinner nextag_relse_buffer:
179730f712c9SDave Chinner 		xfs_trans_brelse(tp, agbp);
179830f712c9SDave Chinner nextag:
179930f712c9SDave Chinner 		xfs_perag_put(pag);
180030f712c9SDave Chinner 		if (++agno == mp->m_sb.sb_agcount)
180130f712c9SDave Chinner 			agno = 0;
180230f712c9SDave Chinner 		if (agno == start_agno) {
180330f712c9SDave Chinner 			*inop = NULLFSINO;
18042451337dSDave Chinner 			return noroom ? -ENOSPC : 0;
180530f712c9SDave Chinner 		}
180630f712c9SDave Chinner 	}
180730f712c9SDave Chinner 
180830f712c9SDave Chinner out_alloc:
180930f712c9SDave Chinner 	*IO_agbp = NULL;
181030f712c9SDave Chinner 	return xfs_dialloc_ag(tp, agbp, parent, inop);
181130f712c9SDave Chinner out_error:
181230f712c9SDave Chinner 	xfs_perag_put(pag);
181330f712c9SDave Chinner 	return error;
181430f712c9SDave Chinner }
181530f712c9SDave Chinner 
181610ae3dc7SBrian Foster /*
181710ae3dc7SBrian Foster  * Free the blocks of an inode chunk. We must consider that the inode chunk
181810ae3dc7SBrian Foster  * might be sparse and only free the regions that are allocated as part of the
181910ae3dc7SBrian Foster  * chunk.
182010ae3dc7SBrian Foster  */
182110ae3dc7SBrian Foster STATIC void
182210ae3dc7SBrian Foster xfs_difree_inode_chunk(
182310ae3dc7SBrian Foster 	struct xfs_mount		*mp,
182410ae3dc7SBrian Foster 	xfs_agnumber_t			agno,
182510ae3dc7SBrian Foster 	struct xfs_inobt_rec_incore	*rec,
18262c3234d1SDarrick J. Wong 	struct xfs_defer_ops		*dfops)
182710ae3dc7SBrian Foster {
182810ae3dc7SBrian Foster 	xfs_agblock_t	sagbno = XFS_AGINO_TO_AGBNO(mp, rec->ir_startino);
182910ae3dc7SBrian Foster 	int		startidx, endidx;
183010ae3dc7SBrian Foster 	int		nextbit;
183110ae3dc7SBrian Foster 	xfs_agblock_t	agbno;
183210ae3dc7SBrian Foster 	int		contigblk;
1833340785ccSDarrick J. Wong 	struct xfs_owner_info	oinfo;
183410ae3dc7SBrian Foster 	DECLARE_BITMAP(holemask, XFS_INOBT_HOLEMASK_BITS);
1835340785ccSDarrick J. Wong 	xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_INODES);
183610ae3dc7SBrian Foster 
183710ae3dc7SBrian Foster 	if (!xfs_inobt_issparse(rec->ir_holemask)) {
183810ae3dc7SBrian Foster 		/* not sparse, calculate extent info directly */
18392c3234d1SDarrick J. Wong 		xfs_bmap_add_free(mp, dfops, XFS_AGB_TO_FSB(mp, agno, sagbno),
1840340785ccSDarrick J. Wong 				  mp->m_ialloc_blks, &oinfo);
184110ae3dc7SBrian Foster 		return;
184210ae3dc7SBrian Foster 	}
184310ae3dc7SBrian Foster 
184410ae3dc7SBrian Foster 	/* holemask is only 16-bits (fits in an unsigned long) */
184510ae3dc7SBrian Foster 	ASSERT(sizeof(rec->ir_holemask) <= sizeof(holemask[0]));
184610ae3dc7SBrian Foster 	holemask[0] = rec->ir_holemask;
184710ae3dc7SBrian Foster 
184810ae3dc7SBrian Foster 	/*
184910ae3dc7SBrian Foster 	 * Find contiguous ranges of zeroes (i.e., allocated regions) in the
185010ae3dc7SBrian Foster 	 * holemask and convert the start/end index of each range to an extent.
185110ae3dc7SBrian Foster 	 * We start with the start and end index both pointing at the first 0 in
185210ae3dc7SBrian Foster 	 * the mask.
185310ae3dc7SBrian Foster 	 */
185410ae3dc7SBrian Foster 	startidx = endidx = find_first_zero_bit(holemask,
185510ae3dc7SBrian Foster 						XFS_INOBT_HOLEMASK_BITS);
185610ae3dc7SBrian Foster 	nextbit = startidx + 1;
185710ae3dc7SBrian Foster 	while (startidx < XFS_INOBT_HOLEMASK_BITS) {
185810ae3dc7SBrian Foster 		nextbit = find_next_zero_bit(holemask, XFS_INOBT_HOLEMASK_BITS,
185910ae3dc7SBrian Foster 					     nextbit);
186010ae3dc7SBrian Foster 		/*
186110ae3dc7SBrian Foster 		 * If the next zero bit is contiguous, update the end index of
186210ae3dc7SBrian Foster 		 * the current range and continue.
186310ae3dc7SBrian Foster 		 */
186410ae3dc7SBrian Foster 		if (nextbit != XFS_INOBT_HOLEMASK_BITS &&
186510ae3dc7SBrian Foster 		    nextbit == endidx + 1) {
186610ae3dc7SBrian Foster 			endidx = nextbit;
186710ae3dc7SBrian Foster 			goto next;
186810ae3dc7SBrian Foster 		}
186910ae3dc7SBrian Foster 
187010ae3dc7SBrian Foster 		/*
187110ae3dc7SBrian Foster 		 * nextbit is not contiguous with the current end index. Convert
187210ae3dc7SBrian Foster 		 * the current start/end to an extent and add it to the free
187310ae3dc7SBrian Foster 		 * list.
187410ae3dc7SBrian Foster 		 */
187510ae3dc7SBrian Foster 		agbno = sagbno + (startidx * XFS_INODES_PER_HOLEMASK_BIT) /
187610ae3dc7SBrian Foster 				  mp->m_sb.sb_inopblock;
187710ae3dc7SBrian Foster 		contigblk = ((endidx - startidx + 1) *
187810ae3dc7SBrian Foster 			     XFS_INODES_PER_HOLEMASK_BIT) /
187910ae3dc7SBrian Foster 			    mp->m_sb.sb_inopblock;
188010ae3dc7SBrian Foster 
188110ae3dc7SBrian Foster 		ASSERT(agbno % mp->m_sb.sb_spino_align == 0);
188210ae3dc7SBrian Foster 		ASSERT(contigblk % mp->m_sb.sb_spino_align == 0);
18832c3234d1SDarrick J. Wong 		xfs_bmap_add_free(mp, dfops, XFS_AGB_TO_FSB(mp, agno, agbno),
1884340785ccSDarrick J. Wong 				  contigblk, &oinfo);
188510ae3dc7SBrian Foster 
188610ae3dc7SBrian Foster 		/* reset range to current bit and carry on... */
188710ae3dc7SBrian Foster 		startidx = endidx = nextbit;
188810ae3dc7SBrian Foster 
188910ae3dc7SBrian Foster next:
189010ae3dc7SBrian Foster 		nextbit++;
189110ae3dc7SBrian Foster 	}
189210ae3dc7SBrian Foster }
189310ae3dc7SBrian Foster 
189430f712c9SDave Chinner STATIC int
189530f712c9SDave Chinner xfs_difree_inobt(
189630f712c9SDave Chinner 	struct xfs_mount		*mp,
189730f712c9SDave Chinner 	struct xfs_trans		*tp,
189830f712c9SDave Chinner 	struct xfs_buf			*agbp,
189930f712c9SDave Chinner 	xfs_agino_t			agino,
19002c3234d1SDarrick J. Wong 	struct xfs_defer_ops		*dfops,
190109b56604SBrian Foster 	struct xfs_icluster		*xic,
190230f712c9SDave Chinner 	struct xfs_inobt_rec_incore	*orec)
190330f712c9SDave Chinner {
190430f712c9SDave Chinner 	struct xfs_agi			*agi = XFS_BUF_TO_AGI(agbp);
190530f712c9SDave Chinner 	xfs_agnumber_t			agno = be32_to_cpu(agi->agi_seqno);
190630f712c9SDave Chinner 	struct xfs_perag		*pag;
190730f712c9SDave Chinner 	struct xfs_btree_cur		*cur;
190830f712c9SDave Chinner 	struct xfs_inobt_rec_incore	rec;
190930f712c9SDave Chinner 	int				ilen;
191030f712c9SDave Chinner 	int				error;
191130f712c9SDave Chinner 	int				i;
191230f712c9SDave Chinner 	int				off;
191330f712c9SDave Chinner 
191430f712c9SDave Chinner 	ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC));
191530f712c9SDave Chinner 	ASSERT(XFS_AGINO_TO_AGBNO(mp, agino) < be32_to_cpu(agi->agi_length));
191630f712c9SDave Chinner 
191730f712c9SDave Chinner 	/*
191830f712c9SDave Chinner 	 * Initialize the cursor.
191930f712c9SDave Chinner 	 */
192030f712c9SDave Chinner 	cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
192130f712c9SDave Chinner 
192230f712c9SDave Chinner 	error = xfs_check_agi_freecount(cur, agi);
192330f712c9SDave Chinner 	if (error)
192430f712c9SDave Chinner 		goto error0;
192530f712c9SDave Chinner 
192630f712c9SDave Chinner 	/*
192730f712c9SDave Chinner 	 * Look for the entry describing this inode.
192830f712c9SDave Chinner 	 */
192930f712c9SDave Chinner 	if ((error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i))) {
193030f712c9SDave Chinner 		xfs_warn(mp, "%s: xfs_inobt_lookup() returned error %d.",
193130f712c9SDave Chinner 			__func__, error);
193230f712c9SDave Chinner 		goto error0;
193330f712c9SDave Chinner 	}
1934c29aad41SEric Sandeen 	XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
193530f712c9SDave Chinner 	error = xfs_inobt_get_rec(cur, &rec, &i);
193630f712c9SDave Chinner 	if (error) {
193730f712c9SDave Chinner 		xfs_warn(mp, "%s: xfs_inobt_get_rec() returned error %d.",
193830f712c9SDave Chinner 			__func__, error);
193930f712c9SDave Chinner 		goto error0;
194030f712c9SDave Chinner 	}
1941c29aad41SEric Sandeen 	XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
194230f712c9SDave Chinner 	/*
194330f712c9SDave Chinner 	 * Get the offset in the inode chunk.
194430f712c9SDave Chinner 	 */
194530f712c9SDave Chinner 	off = agino - rec.ir_startino;
194630f712c9SDave Chinner 	ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK);
194730f712c9SDave Chinner 	ASSERT(!(rec.ir_free & XFS_INOBT_MASK(off)));
194830f712c9SDave Chinner 	/*
194930f712c9SDave Chinner 	 * Mark the inode free & increment the count.
195030f712c9SDave Chinner 	 */
195130f712c9SDave Chinner 	rec.ir_free |= XFS_INOBT_MASK(off);
195230f712c9SDave Chinner 	rec.ir_freecount++;
195330f712c9SDave Chinner 
195430f712c9SDave Chinner 	/*
1955999633d3SBrian Foster 	 * When an inode chunk is free, it becomes eligible for removal. Don't
1956999633d3SBrian Foster 	 * remove the chunk if the block size is large enough for multiple inode
1957999633d3SBrian Foster 	 * chunks (that might not be free).
195830f712c9SDave Chinner 	 */
195930f712c9SDave Chinner 	if (!(mp->m_flags & XFS_MOUNT_IKEEP) &&
1960999633d3SBrian Foster 	    rec.ir_free == XFS_INOBT_ALL_FREE &&
1961999633d3SBrian Foster 	    mp->m_sb.sb_inopblock <= XFS_INODES_PER_CHUNK) {
1962749f24f3SThomas Meyer 		xic->deleted = true;
196309b56604SBrian Foster 		xic->first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino);
196409b56604SBrian Foster 		xic->alloc = xfs_inobt_irec_to_allocmask(&rec);
196530f712c9SDave Chinner 
196630f712c9SDave Chinner 		/*
196730f712c9SDave Chinner 		 * Remove the inode cluster from the AGI B+Tree, adjust the
196830f712c9SDave Chinner 		 * AGI and Superblock inode counts, and mark the disk space
196930f712c9SDave Chinner 		 * to be freed when the transaction is committed.
197030f712c9SDave Chinner 		 */
1971999633d3SBrian Foster 		ilen = rec.ir_freecount;
197230f712c9SDave Chinner 		be32_add_cpu(&agi->agi_count, -ilen);
197330f712c9SDave Chinner 		be32_add_cpu(&agi->agi_freecount, -(ilen - 1));
197430f712c9SDave Chinner 		xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT);
197530f712c9SDave Chinner 		pag = xfs_perag_get(mp, agno);
197630f712c9SDave Chinner 		pag->pagi_freecount -= ilen - 1;
197730f712c9SDave Chinner 		xfs_perag_put(pag);
197830f712c9SDave Chinner 		xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen);
197930f712c9SDave Chinner 		xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1));
198030f712c9SDave Chinner 
198130f712c9SDave Chinner 		if ((error = xfs_btree_delete(cur, &i))) {
198230f712c9SDave Chinner 			xfs_warn(mp, "%s: xfs_btree_delete returned error %d.",
198330f712c9SDave Chinner 				__func__, error);
198430f712c9SDave Chinner 			goto error0;
198530f712c9SDave Chinner 		}
198630f712c9SDave Chinner 
19872c3234d1SDarrick J. Wong 		xfs_difree_inode_chunk(mp, agno, &rec, dfops);
198830f712c9SDave Chinner 	} else {
1989749f24f3SThomas Meyer 		xic->deleted = false;
199030f712c9SDave Chinner 
199130f712c9SDave Chinner 		error = xfs_inobt_update(cur, &rec);
199230f712c9SDave Chinner 		if (error) {
199330f712c9SDave Chinner 			xfs_warn(mp, "%s: xfs_inobt_update returned error %d.",
199430f712c9SDave Chinner 				__func__, error);
199530f712c9SDave Chinner 			goto error0;
199630f712c9SDave Chinner 		}
199730f712c9SDave Chinner 
199830f712c9SDave Chinner 		/*
199930f712c9SDave Chinner 		 * Change the inode free counts and log the ag/sb changes.
200030f712c9SDave Chinner 		 */
200130f712c9SDave Chinner 		be32_add_cpu(&agi->agi_freecount, 1);
200230f712c9SDave Chinner 		xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
200330f712c9SDave Chinner 		pag = xfs_perag_get(mp, agno);
200430f712c9SDave Chinner 		pag->pagi_freecount++;
200530f712c9SDave Chinner 		xfs_perag_put(pag);
200630f712c9SDave Chinner 		xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1);
200730f712c9SDave Chinner 	}
200830f712c9SDave Chinner 
200930f712c9SDave Chinner 	error = xfs_check_agi_freecount(cur, agi);
201030f712c9SDave Chinner 	if (error)
201130f712c9SDave Chinner 		goto error0;
201230f712c9SDave Chinner 
201330f712c9SDave Chinner 	*orec = rec;
201430f712c9SDave Chinner 	xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
201530f712c9SDave Chinner 	return 0;
201630f712c9SDave Chinner 
201730f712c9SDave Chinner error0:
201830f712c9SDave Chinner 	xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
201930f712c9SDave Chinner 	return error;
202030f712c9SDave Chinner }
202130f712c9SDave Chinner 
202230f712c9SDave Chinner /*
202330f712c9SDave Chinner  * Free an inode in the free inode btree.
202430f712c9SDave Chinner  */
202530f712c9SDave Chinner STATIC int
202630f712c9SDave Chinner xfs_difree_finobt(
202730f712c9SDave Chinner 	struct xfs_mount		*mp,
202830f712c9SDave Chinner 	struct xfs_trans		*tp,
202930f712c9SDave Chinner 	struct xfs_buf			*agbp,
203030f712c9SDave Chinner 	xfs_agino_t			agino,
203130f712c9SDave Chinner 	struct xfs_inobt_rec_incore	*ibtrec) /* inobt record */
203230f712c9SDave Chinner {
203330f712c9SDave Chinner 	struct xfs_agi			*agi = XFS_BUF_TO_AGI(agbp);
203430f712c9SDave Chinner 	xfs_agnumber_t			agno = be32_to_cpu(agi->agi_seqno);
203530f712c9SDave Chinner 	struct xfs_btree_cur		*cur;
203630f712c9SDave Chinner 	struct xfs_inobt_rec_incore	rec;
203730f712c9SDave Chinner 	int				offset = agino - ibtrec->ir_startino;
203830f712c9SDave Chinner 	int				error;
203930f712c9SDave Chinner 	int				i;
204030f712c9SDave Chinner 
204130f712c9SDave Chinner 	cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_FINO);
204230f712c9SDave Chinner 
204330f712c9SDave Chinner 	error = xfs_inobt_lookup(cur, ibtrec->ir_startino, XFS_LOOKUP_EQ, &i);
204430f712c9SDave Chinner 	if (error)
204530f712c9SDave Chinner 		goto error;
204630f712c9SDave Chinner 	if (i == 0) {
204730f712c9SDave Chinner 		/*
204830f712c9SDave Chinner 		 * If the record does not exist in the finobt, we must have just
204930f712c9SDave Chinner 		 * freed an inode in a previously fully allocated chunk. If not,
205030f712c9SDave Chinner 		 * something is out of sync.
205130f712c9SDave Chinner 		 */
2052c29aad41SEric Sandeen 		XFS_WANT_CORRUPTED_GOTO(mp, ibtrec->ir_freecount == 1, error);
205330f712c9SDave Chinner 
20545419040fSBrian Foster 		error = xfs_inobt_insert_rec(cur, ibtrec->ir_holemask,
20555419040fSBrian Foster 					     ibtrec->ir_count,
20565419040fSBrian Foster 					     ibtrec->ir_freecount,
205730f712c9SDave Chinner 					     ibtrec->ir_free, &i);
205830f712c9SDave Chinner 		if (error)
205930f712c9SDave Chinner 			goto error;
206030f712c9SDave Chinner 		ASSERT(i == 1);
206130f712c9SDave Chinner 
206230f712c9SDave Chinner 		goto out;
206330f712c9SDave Chinner 	}
206430f712c9SDave Chinner 
206530f712c9SDave Chinner 	/*
206630f712c9SDave Chinner 	 * Read and update the existing record. We could just copy the ibtrec
206730f712c9SDave Chinner 	 * across here, but that would defeat the purpose of having redundant
206830f712c9SDave Chinner 	 * metadata. By making the modifications independently, we can catch
206930f712c9SDave Chinner 	 * corruptions that we wouldn't see if we just copied from one record
207030f712c9SDave Chinner 	 * to another.
207130f712c9SDave Chinner 	 */
207230f712c9SDave Chinner 	error = xfs_inobt_get_rec(cur, &rec, &i);
207330f712c9SDave Chinner 	if (error)
207430f712c9SDave Chinner 		goto error;
2075c29aad41SEric Sandeen 	XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error);
207630f712c9SDave Chinner 
207730f712c9SDave Chinner 	rec.ir_free |= XFS_INOBT_MASK(offset);
207830f712c9SDave Chinner 	rec.ir_freecount++;
207930f712c9SDave Chinner 
2080c29aad41SEric Sandeen 	XFS_WANT_CORRUPTED_GOTO(mp, (rec.ir_free == ibtrec->ir_free) &&
208130f712c9SDave Chinner 				(rec.ir_freecount == ibtrec->ir_freecount),
208230f712c9SDave Chinner 				error);
208330f712c9SDave Chinner 
208430f712c9SDave Chinner 	/*
208530f712c9SDave Chinner 	 * The content of inobt records should always match between the inobt
208630f712c9SDave Chinner 	 * and finobt. The lifecycle of records in the finobt is different from
208730f712c9SDave Chinner 	 * the inobt in that the finobt only tracks records with at least one
208830f712c9SDave Chinner 	 * free inode. Hence, if all of the inodes are free and we aren't
208930f712c9SDave Chinner 	 * keeping inode chunks permanently on disk, remove the record.
209030f712c9SDave Chinner 	 * Otherwise, update the record with the new information.
2091999633d3SBrian Foster 	 *
2092999633d3SBrian Foster 	 * Note that we currently can't free chunks when the block size is large
2093999633d3SBrian Foster 	 * enough for multiple chunks. Leave the finobt record to remain in sync
2094999633d3SBrian Foster 	 * with the inobt.
209530f712c9SDave Chinner 	 */
2096999633d3SBrian Foster 	if (rec.ir_free == XFS_INOBT_ALL_FREE &&
2097999633d3SBrian Foster 	    mp->m_sb.sb_inopblock <= XFS_INODES_PER_CHUNK &&
209830f712c9SDave Chinner 	    !(mp->m_flags & XFS_MOUNT_IKEEP)) {
209930f712c9SDave Chinner 		error = xfs_btree_delete(cur, &i);
210030f712c9SDave Chinner 		if (error)
210130f712c9SDave Chinner 			goto error;
210230f712c9SDave Chinner 		ASSERT(i == 1);
210330f712c9SDave Chinner 	} else {
210430f712c9SDave Chinner 		error = xfs_inobt_update(cur, &rec);
210530f712c9SDave Chinner 		if (error)
210630f712c9SDave Chinner 			goto error;
210730f712c9SDave Chinner 	}
210830f712c9SDave Chinner 
210930f712c9SDave Chinner out:
211030f712c9SDave Chinner 	error = xfs_check_agi_freecount(cur, agi);
211130f712c9SDave Chinner 	if (error)
211230f712c9SDave Chinner 		goto error;
211330f712c9SDave Chinner 
211430f712c9SDave Chinner 	xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
211530f712c9SDave Chinner 	return 0;
211630f712c9SDave Chinner 
211730f712c9SDave Chinner error:
211830f712c9SDave Chinner 	xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
211930f712c9SDave Chinner 	return error;
212030f712c9SDave Chinner }
212130f712c9SDave Chinner 
212230f712c9SDave Chinner /*
212330f712c9SDave Chinner  * Free disk inode.  Carefully avoids touching the incore inode, all
212430f712c9SDave Chinner  * manipulations incore are the caller's responsibility.
212530f712c9SDave Chinner  * The on-disk inode is not changed by this operation, only the
212630f712c9SDave Chinner  * btree (free inode mask) is changed.
212730f712c9SDave Chinner  */
212830f712c9SDave Chinner int
212930f712c9SDave Chinner xfs_difree(
213030f712c9SDave Chinner 	struct xfs_trans	*tp,		/* transaction pointer */
213130f712c9SDave Chinner 	xfs_ino_t		inode,		/* inode to be freed */
21322c3234d1SDarrick J. Wong 	struct xfs_defer_ops	*dfops,		/* extents to free */
213309b56604SBrian Foster 	struct xfs_icluster	*xic)	/* cluster info if deleted */
213430f712c9SDave Chinner {
213530f712c9SDave Chinner 	/* REFERENCED */
213630f712c9SDave Chinner 	xfs_agblock_t		agbno;	/* block number containing inode */
213730f712c9SDave Chinner 	struct xfs_buf		*agbp;	/* buffer for allocation group header */
213830f712c9SDave Chinner 	xfs_agino_t		agino;	/* allocation group inode number */
213930f712c9SDave Chinner 	xfs_agnumber_t		agno;	/* allocation group number */
214030f712c9SDave Chinner 	int			error;	/* error return value */
214130f712c9SDave Chinner 	struct xfs_mount	*mp;	/* mount structure for filesystem */
214230f712c9SDave Chinner 	struct xfs_inobt_rec_incore rec;/* btree record */
214330f712c9SDave Chinner 
214430f712c9SDave Chinner 	mp = tp->t_mountp;
214530f712c9SDave Chinner 
214630f712c9SDave Chinner 	/*
214730f712c9SDave Chinner 	 * Break up inode number into its components.
214830f712c9SDave Chinner 	 */
214930f712c9SDave Chinner 	agno = XFS_INO_TO_AGNO(mp, inode);
215030f712c9SDave Chinner 	if (agno >= mp->m_sb.sb_agcount)  {
215130f712c9SDave Chinner 		xfs_warn(mp, "%s: agno >= mp->m_sb.sb_agcount (%d >= %d).",
215230f712c9SDave Chinner 			__func__, agno, mp->m_sb.sb_agcount);
215330f712c9SDave Chinner 		ASSERT(0);
21542451337dSDave Chinner 		return -EINVAL;
215530f712c9SDave Chinner 	}
215630f712c9SDave Chinner 	agino = XFS_INO_TO_AGINO(mp, inode);
215730f712c9SDave Chinner 	if (inode != XFS_AGINO_TO_INO(mp, agno, agino))  {
215830f712c9SDave Chinner 		xfs_warn(mp, "%s: inode != XFS_AGINO_TO_INO() (%llu != %llu).",
215930f712c9SDave Chinner 			__func__, (unsigned long long)inode,
216030f712c9SDave Chinner 			(unsigned long long)XFS_AGINO_TO_INO(mp, agno, agino));
216130f712c9SDave Chinner 		ASSERT(0);
21622451337dSDave Chinner 		return -EINVAL;
216330f712c9SDave Chinner 	}
216430f712c9SDave Chinner 	agbno = XFS_AGINO_TO_AGBNO(mp, agino);
216530f712c9SDave Chinner 	if (agbno >= mp->m_sb.sb_agblocks)  {
216630f712c9SDave Chinner 		xfs_warn(mp, "%s: agbno >= mp->m_sb.sb_agblocks (%d >= %d).",
216730f712c9SDave Chinner 			__func__, agbno, mp->m_sb.sb_agblocks);
216830f712c9SDave Chinner 		ASSERT(0);
21692451337dSDave Chinner 		return -EINVAL;
217030f712c9SDave Chinner 	}
217130f712c9SDave Chinner 	/*
217230f712c9SDave Chinner 	 * Get the allocation group header.
217330f712c9SDave Chinner 	 */
217430f712c9SDave Chinner 	error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
217530f712c9SDave Chinner 	if (error) {
217630f712c9SDave Chinner 		xfs_warn(mp, "%s: xfs_ialloc_read_agi() returned error %d.",
217730f712c9SDave Chinner 			__func__, error);
217830f712c9SDave Chinner 		return error;
217930f712c9SDave Chinner 	}
218030f712c9SDave Chinner 
218130f712c9SDave Chinner 	/*
218230f712c9SDave Chinner 	 * Fix up the inode allocation btree.
218330f712c9SDave Chinner 	 */
21842c3234d1SDarrick J. Wong 	error = xfs_difree_inobt(mp, tp, agbp, agino, dfops, xic, &rec);
218530f712c9SDave Chinner 	if (error)
218630f712c9SDave Chinner 		goto error0;
218730f712c9SDave Chinner 
218830f712c9SDave Chinner 	/*
218930f712c9SDave Chinner 	 * Fix up the free inode btree.
219030f712c9SDave Chinner 	 */
219130f712c9SDave Chinner 	if (xfs_sb_version_hasfinobt(&mp->m_sb)) {
219230f712c9SDave Chinner 		error = xfs_difree_finobt(mp, tp, agbp, agino, &rec);
219330f712c9SDave Chinner 		if (error)
219430f712c9SDave Chinner 			goto error0;
219530f712c9SDave Chinner 	}
219630f712c9SDave Chinner 
219730f712c9SDave Chinner 	return 0;
219830f712c9SDave Chinner 
219930f712c9SDave Chinner error0:
220030f712c9SDave Chinner 	return error;
220130f712c9SDave Chinner }
220230f712c9SDave Chinner 
220330f712c9SDave Chinner STATIC int
220430f712c9SDave Chinner xfs_imap_lookup(
220530f712c9SDave Chinner 	struct xfs_mount	*mp,
220630f712c9SDave Chinner 	struct xfs_trans	*tp,
220730f712c9SDave Chinner 	xfs_agnumber_t		agno,
220830f712c9SDave Chinner 	xfs_agino_t		agino,
220930f712c9SDave Chinner 	xfs_agblock_t		agbno,
221030f712c9SDave Chinner 	xfs_agblock_t		*chunk_agbno,
221130f712c9SDave Chinner 	xfs_agblock_t		*offset_agbno,
221230f712c9SDave Chinner 	int			flags)
221330f712c9SDave Chinner {
221430f712c9SDave Chinner 	struct xfs_inobt_rec_incore rec;
221530f712c9SDave Chinner 	struct xfs_btree_cur	*cur;
221630f712c9SDave Chinner 	struct xfs_buf		*agbp;
221730f712c9SDave Chinner 	int			error;
221830f712c9SDave Chinner 	int			i;
221930f712c9SDave Chinner 
222030f712c9SDave Chinner 	error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
222130f712c9SDave Chinner 	if (error) {
222230f712c9SDave Chinner 		xfs_alert(mp,
222330f712c9SDave Chinner 			"%s: xfs_ialloc_read_agi() returned error %d, agno %d",
222430f712c9SDave Chinner 			__func__, error, agno);
222530f712c9SDave Chinner 		return error;
222630f712c9SDave Chinner 	}
222730f712c9SDave Chinner 
222830f712c9SDave Chinner 	/*
222930f712c9SDave Chinner 	 * Lookup the inode record for the given agino. If the record cannot be
223030f712c9SDave Chinner 	 * found, then it's an invalid inode number and we should abort. Once
223130f712c9SDave Chinner 	 * we have a record, we need to ensure it contains the inode number
223230f712c9SDave Chinner 	 * we are looking up.
223330f712c9SDave Chinner 	 */
223430f712c9SDave Chinner 	cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
223530f712c9SDave Chinner 	error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i);
223630f712c9SDave Chinner 	if (!error) {
223730f712c9SDave Chinner 		if (i)
223830f712c9SDave Chinner 			error = xfs_inobt_get_rec(cur, &rec, &i);
223930f712c9SDave Chinner 		if (!error && i == 0)
22402451337dSDave Chinner 			error = -EINVAL;
224130f712c9SDave Chinner 	}
224230f712c9SDave Chinner 
224330f712c9SDave Chinner 	xfs_trans_brelse(tp, agbp);
2244f307080aSBrian Foster 	xfs_btree_del_cursor(cur, error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
224530f712c9SDave Chinner 	if (error)
224630f712c9SDave Chinner 		return error;
224730f712c9SDave Chinner 
224830f712c9SDave Chinner 	/* check that the returned record contains the required inode */
224930f712c9SDave Chinner 	if (rec.ir_startino > agino ||
225030f712c9SDave Chinner 	    rec.ir_startino + mp->m_ialloc_inos <= agino)
22512451337dSDave Chinner 		return -EINVAL;
225230f712c9SDave Chinner 
225330f712c9SDave Chinner 	/* for untrusted inodes check it is allocated first */
225430f712c9SDave Chinner 	if ((flags & XFS_IGET_UNTRUSTED) &&
225530f712c9SDave Chinner 	    (rec.ir_free & XFS_INOBT_MASK(agino - rec.ir_startino)))
22562451337dSDave Chinner 		return -EINVAL;
225730f712c9SDave Chinner 
225830f712c9SDave Chinner 	*chunk_agbno = XFS_AGINO_TO_AGBNO(mp, rec.ir_startino);
225930f712c9SDave Chinner 	*offset_agbno = agbno - *chunk_agbno;
226030f712c9SDave Chinner 	return 0;
226130f712c9SDave Chinner }
226230f712c9SDave Chinner 
226330f712c9SDave Chinner /*
226430f712c9SDave Chinner  * Return the location of the inode in imap, for mapping it into a buffer.
226530f712c9SDave Chinner  */
226630f712c9SDave Chinner int
226730f712c9SDave Chinner xfs_imap(
226830f712c9SDave Chinner 	xfs_mount_t	 *mp,	/* file system mount structure */
226930f712c9SDave Chinner 	xfs_trans_t	 *tp,	/* transaction pointer */
227030f712c9SDave Chinner 	xfs_ino_t	ino,	/* inode to locate */
227130f712c9SDave Chinner 	struct xfs_imap	*imap,	/* location map structure */
227230f712c9SDave Chinner 	uint		flags)	/* flags for inode btree lookup */
227330f712c9SDave Chinner {
227430f712c9SDave Chinner 	xfs_agblock_t	agbno;	/* block number of inode in the alloc group */
227530f712c9SDave Chinner 	xfs_agino_t	agino;	/* inode number within alloc group */
227630f712c9SDave Chinner 	xfs_agnumber_t	agno;	/* allocation group number */
227730f712c9SDave Chinner 	int		blks_per_cluster; /* num blocks per inode cluster */
227830f712c9SDave Chinner 	xfs_agblock_t	chunk_agbno;	/* first block in inode chunk */
227930f712c9SDave Chinner 	xfs_agblock_t	cluster_agbno;	/* first block in inode cluster */
228030f712c9SDave Chinner 	int		error;	/* error code */
228130f712c9SDave Chinner 	int		offset;	/* index of inode in its buffer */
228230f712c9SDave Chinner 	xfs_agblock_t	offset_agbno;	/* blks from chunk start to inode */
228330f712c9SDave Chinner 
228430f712c9SDave Chinner 	ASSERT(ino != NULLFSINO);
228530f712c9SDave Chinner 
228630f712c9SDave Chinner 	/*
228730f712c9SDave Chinner 	 * Split up the inode number into its parts.
228830f712c9SDave Chinner 	 */
228930f712c9SDave Chinner 	agno = XFS_INO_TO_AGNO(mp, ino);
229030f712c9SDave Chinner 	agino = XFS_INO_TO_AGINO(mp, ino);
229130f712c9SDave Chinner 	agbno = XFS_AGINO_TO_AGBNO(mp, agino);
229230f712c9SDave Chinner 	if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks ||
229330f712c9SDave Chinner 	    ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
229430f712c9SDave Chinner #ifdef DEBUG
229530f712c9SDave Chinner 		/*
229630f712c9SDave Chinner 		 * Don't output diagnostic information for untrusted inodes
229730f712c9SDave Chinner 		 * as they can be invalid without implying corruption.
229830f712c9SDave Chinner 		 */
229930f712c9SDave Chinner 		if (flags & XFS_IGET_UNTRUSTED)
23002451337dSDave Chinner 			return -EINVAL;
230130f712c9SDave Chinner 		if (agno >= mp->m_sb.sb_agcount) {
230230f712c9SDave Chinner 			xfs_alert(mp,
230330f712c9SDave Chinner 				"%s: agno (%d) >= mp->m_sb.sb_agcount (%d)",
230430f712c9SDave Chinner 				__func__, agno, mp->m_sb.sb_agcount);
230530f712c9SDave Chinner 		}
230630f712c9SDave Chinner 		if (agbno >= mp->m_sb.sb_agblocks) {
230730f712c9SDave Chinner 			xfs_alert(mp,
230830f712c9SDave Chinner 		"%s: agbno (0x%llx) >= mp->m_sb.sb_agblocks (0x%lx)",
230930f712c9SDave Chinner 				__func__, (unsigned long long)agbno,
231030f712c9SDave Chinner 				(unsigned long)mp->m_sb.sb_agblocks);
231130f712c9SDave Chinner 		}
231230f712c9SDave Chinner 		if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
231330f712c9SDave Chinner 			xfs_alert(mp,
231430f712c9SDave Chinner 		"%s: ino (0x%llx) != XFS_AGINO_TO_INO() (0x%llx)",
231530f712c9SDave Chinner 				__func__, ino,
231630f712c9SDave Chinner 				XFS_AGINO_TO_INO(mp, agno, agino));
231730f712c9SDave Chinner 		}
231830f712c9SDave Chinner 		xfs_stack_trace();
231930f712c9SDave Chinner #endif /* DEBUG */
23202451337dSDave Chinner 		return -EINVAL;
232130f712c9SDave Chinner 	}
232230f712c9SDave Chinner 
232330f712c9SDave Chinner 	blks_per_cluster = xfs_icluster_size_fsb(mp);
232430f712c9SDave Chinner 
232530f712c9SDave Chinner 	/*
232630f712c9SDave Chinner 	 * For bulkstat and handle lookups, we have an untrusted inode number
232730f712c9SDave Chinner 	 * that we have to verify is valid. We cannot do this just by reading
232830f712c9SDave Chinner 	 * the inode buffer as it may have been unlinked and removed leaving
232930f712c9SDave Chinner 	 * inodes in stale state on disk. Hence we have to do a btree lookup
233030f712c9SDave Chinner 	 * in all cases where an untrusted inode number is passed.
233130f712c9SDave Chinner 	 */
233230f712c9SDave Chinner 	if (flags & XFS_IGET_UNTRUSTED) {
233330f712c9SDave Chinner 		error = xfs_imap_lookup(mp, tp, agno, agino, agbno,
233430f712c9SDave Chinner 					&chunk_agbno, &offset_agbno, flags);
233530f712c9SDave Chinner 		if (error)
233630f712c9SDave Chinner 			return error;
233730f712c9SDave Chinner 		goto out_map;
233830f712c9SDave Chinner 	}
233930f712c9SDave Chinner 
234030f712c9SDave Chinner 	/*
234130f712c9SDave Chinner 	 * If the inode cluster size is the same as the blocksize or
234230f712c9SDave Chinner 	 * smaller we get to the buffer by simple arithmetics.
234330f712c9SDave Chinner 	 */
234430f712c9SDave Chinner 	if (blks_per_cluster == 1) {
234530f712c9SDave Chinner 		offset = XFS_INO_TO_OFFSET(mp, ino);
234630f712c9SDave Chinner 		ASSERT(offset < mp->m_sb.sb_inopblock);
234730f712c9SDave Chinner 
234830f712c9SDave Chinner 		imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, agbno);
234930f712c9SDave Chinner 		imap->im_len = XFS_FSB_TO_BB(mp, 1);
2350755c7bf5SDarrick J. Wong 		imap->im_boffset = (unsigned short)(offset <<
2351755c7bf5SDarrick J. Wong 							mp->m_sb.sb_inodelog);
235230f712c9SDave Chinner 		return 0;
235330f712c9SDave Chinner 	}
235430f712c9SDave Chinner 
235530f712c9SDave Chinner 	/*
235630f712c9SDave Chinner 	 * If the inode chunks are aligned then use simple maths to
235730f712c9SDave Chinner 	 * find the location. Otherwise we have to do a btree
235830f712c9SDave Chinner 	 * lookup to find the location.
235930f712c9SDave Chinner 	 */
236030f712c9SDave Chinner 	if (mp->m_inoalign_mask) {
236130f712c9SDave Chinner 		offset_agbno = agbno & mp->m_inoalign_mask;
236230f712c9SDave Chinner 		chunk_agbno = agbno - offset_agbno;
236330f712c9SDave Chinner 	} else {
236430f712c9SDave Chinner 		error = xfs_imap_lookup(mp, tp, agno, agino, agbno,
236530f712c9SDave Chinner 					&chunk_agbno, &offset_agbno, flags);
236630f712c9SDave Chinner 		if (error)
236730f712c9SDave Chinner 			return error;
236830f712c9SDave Chinner 	}
236930f712c9SDave Chinner 
237030f712c9SDave Chinner out_map:
237130f712c9SDave Chinner 	ASSERT(agbno >= chunk_agbno);
237230f712c9SDave Chinner 	cluster_agbno = chunk_agbno +
237330f712c9SDave Chinner 		((offset_agbno / blks_per_cluster) * blks_per_cluster);
237430f712c9SDave Chinner 	offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
237530f712c9SDave Chinner 		XFS_INO_TO_OFFSET(mp, ino);
237630f712c9SDave Chinner 
237730f712c9SDave Chinner 	imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, cluster_agbno);
237830f712c9SDave Chinner 	imap->im_len = XFS_FSB_TO_BB(mp, blks_per_cluster);
2379755c7bf5SDarrick J. Wong 	imap->im_boffset = (unsigned short)(offset << mp->m_sb.sb_inodelog);
238030f712c9SDave Chinner 
238130f712c9SDave Chinner 	/*
238230f712c9SDave Chinner 	 * If the inode number maps to a block outside the bounds
238330f712c9SDave Chinner 	 * of the file system then return NULL rather than calling
238430f712c9SDave Chinner 	 * read_buf and panicing when we get an error from the
238530f712c9SDave Chinner 	 * driver.
238630f712c9SDave Chinner 	 */
238730f712c9SDave Chinner 	if ((imap->im_blkno + imap->im_len) >
238830f712c9SDave Chinner 	    XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) {
238930f712c9SDave Chinner 		xfs_alert(mp,
239030f712c9SDave Chinner 	"%s: (im_blkno (0x%llx) + im_len (0x%llx)) > sb_dblocks (0x%llx)",
239130f712c9SDave Chinner 			__func__, (unsigned long long) imap->im_blkno,
239230f712c9SDave Chinner 			(unsigned long long) imap->im_len,
239330f712c9SDave Chinner 			XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks));
23942451337dSDave Chinner 		return -EINVAL;
239530f712c9SDave Chinner 	}
239630f712c9SDave Chinner 	return 0;
239730f712c9SDave Chinner }
239830f712c9SDave Chinner 
239930f712c9SDave Chinner /*
240030f712c9SDave Chinner  * Compute and fill in value of m_in_maxlevels.
240130f712c9SDave Chinner  */
240230f712c9SDave Chinner void
240330f712c9SDave Chinner xfs_ialloc_compute_maxlevels(
240430f712c9SDave Chinner 	xfs_mount_t	*mp)		/* file system mount structure */
240530f712c9SDave Chinner {
240619b54ee6SDarrick J. Wong 	uint		inodes;
240730f712c9SDave Chinner 
240819b54ee6SDarrick J. Wong 	inodes = (1LL << XFS_INO_AGINO_BITS(mp)) >> XFS_INODES_PER_CHUNK_LOG;
240919b54ee6SDarrick J. Wong 	mp->m_in_maxlevels = xfs_btree_compute_maxlevels(mp, mp->m_inobt_mnr,
241019b54ee6SDarrick J. Wong 							 inodes);
241130f712c9SDave Chinner }
241230f712c9SDave Chinner 
241330f712c9SDave Chinner /*
241430f712c9SDave Chinner  * Log specified fields for the ag hdr (inode section). The growth of the agi
241530f712c9SDave Chinner  * structure over time requires that we interpret the buffer as two logical
241630f712c9SDave Chinner  * regions delineated by the end of the unlinked list. This is due to the size
241730f712c9SDave Chinner  * of the hash table and its location in the middle of the agi.
241830f712c9SDave Chinner  *
241930f712c9SDave Chinner  * For example, a request to log a field before agi_unlinked and a field after
242030f712c9SDave Chinner  * agi_unlinked could cause us to log the entire hash table and use an excessive
242130f712c9SDave Chinner  * amount of log space. To avoid this behavior, log the region up through
242230f712c9SDave Chinner  * agi_unlinked in one call and the region after agi_unlinked through the end of
242330f712c9SDave Chinner  * the structure in another.
242430f712c9SDave Chinner  */
242530f712c9SDave Chinner void
242630f712c9SDave Chinner xfs_ialloc_log_agi(
242730f712c9SDave Chinner 	xfs_trans_t	*tp,		/* transaction pointer */
242830f712c9SDave Chinner 	xfs_buf_t	*bp,		/* allocation group header buffer */
242930f712c9SDave Chinner 	int		fields)		/* bitmask of fields to log */
243030f712c9SDave Chinner {
243130f712c9SDave Chinner 	int			first;		/* first byte number */
243230f712c9SDave Chinner 	int			last;		/* last byte number */
243330f712c9SDave Chinner 	static const short	offsets[] = {	/* field starting offsets */
243430f712c9SDave Chinner 					/* keep in sync with bit definitions */
243530f712c9SDave Chinner 		offsetof(xfs_agi_t, agi_magicnum),
243630f712c9SDave Chinner 		offsetof(xfs_agi_t, agi_versionnum),
243730f712c9SDave Chinner 		offsetof(xfs_agi_t, agi_seqno),
243830f712c9SDave Chinner 		offsetof(xfs_agi_t, agi_length),
243930f712c9SDave Chinner 		offsetof(xfs_agi_t, agi_count),
244030f712c9SDave Chinner 		offsetof(xfs_agi_t, agi_root),
244130f712c9SDave Chinner 		offsetof(xfs_agi_t, agi_level),
244230f712c9SDave Chinner 		offsetof(xfs_agi_t, agi_freecount),
244330f712c9SDave Chinner 		offsetof(xfs_agi_t, agi_newino),
244430f712c9SDave Chinner 		offsetof(xfs_agi_t, agi_dirino),
244530f712c9SDave Chinner 		offsetof(xfs_agi_t, agi_unlinked),
244630f712c9SDave Chinner 		offsetof(xfs_agi_t, agi_free_root),
244730f712c9SDave Chinner 		offsetof(xfs_agi_t, agi_free_level),
244830f712c9SDave Chinner 		sizeof(xfs_agi_t)
244930f712c9SDave Chinner 	};
245030f712c9SDave Chinner #ifdef DEBUG
245130f712c9SDave Chinner 	xfs_agi_t		*agi;	/* allocation group header */
245230f712c9SDave Chinner 
245330f712c9SDave Chinner 	agi = XFS_BUF_TO_AGI(bp);
245430f712c9SDave Chinner 	ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC));
245530f712c9SDave Chinner #endif
245630f712c9SDave Chinner 
245730f712c9SDave Chinner 	/*
245830f712c9SDave Chinner 	 * Compute byte offsets for the first and last fields in the first
245930f712c9SDave Chinner 	 * region and log the agi buffer. This only logs up through
246030f712c9SDave Chinner 	 * agi_unlinked.
246130f712c9SDave Chinner 	 */
246230f712c9SDave Chinner 	if (fields & XFS_AGI_ALL_BITS_R1) {
246330f712c9SDave Chinner 		xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R1,
246430f712c9SDave Chinner 				  &first, &last);
246530f712c9SDave Chinner 		xfs_trans_log_buf(tp, bp, first, last);
246630f712c9SDave Chinner 	}
246730f712c9SDave Chinner 
246830f712c9SDave Chinner 	/*
246930f712c9SDave Chinner 	 * Mask off the bits in the first region and calculate the first and
247030f712c9SDave Chinner 	 * last field offsets for any bits in the second region.
247130f712c9SDave Chinner 	 */
247230f712c9SDave Chinner 	fields &= ~XFS_AGI_ALL_BITS_R1;
247330f712c9SDave Chinner 	if (fields) {
247430f712c9SDave Chinner 		xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R2,
247530f712c9SDave Chinner 				  &first, &last);
247630f712c9SDave Chinner 		xfs_trans_log_buf(tp, bp, first, last);
247730f712c9SDave Chinner 	}
247830f712c9SDave Chinner }
247930f712c9SDave Chinner 
248030f712c9SDave Chinner #ifdef DEBUG
248130f712c9SDave Chinner STATIC void
248230f712c9SDave Chinner xfs_check_agi_unlinked(
248330f712c9SDave Chinner 	struct xfs_agi		*agi)
248430f712c9SDave Chinner {
248530f712c9SDave Chinner 	int			i;
248630f712c9SDave Chinner 
248730f712c9SDave Chinner 	for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++)
248830f712c9SDave Chinner 		ASSERT(agi->agi_unlinked[i]);
248930f712c9SDave Chinner }
249030f712c9SDave Chinner #else
249130f712c9SDave Chinner #define xfs_check_agi_unlinked(agi)
249230f712c9SDave Chinner #endif
249330f712c9SDave Chinner 
2494a6a781a5SDarrick J. Wong static xfs_failaddr_t
249530f712c9SDave Chinner xfs_agi_verify(
249630f712c9SDave Chinner 	struct xfs_buf	*bp)
249730f712c9SDave Chinner {
249830f712c9SDave Chinner 	struct xfs_mount *mp = bp->b_target->bt_mount;
249930f712c9SDave Chinner 	struct xfs_agi	*agi = XFS_BUF_TO_AGI(bp);
250030f712c9SDave Chinner 
2501a45086e2SBrian Foster 	if (xfs_sb_version_hascrc(&mp->m_sb)) {
2502a45086e2SBrian Foster 		if (!uuid_equal(&agi->agi_uuid, &mp->m_sb.sb_meta_uuid))
2503a6a781a5SDarrick J. Wong 			return __this_address;
2504a45086e2SBrian Foster 		if (!xfs_log_check_lsn(mp,
2505a45086e2SBrian Foster 				be64_to_cpu(XFS_BUF_TO_AGI(bp)->agi_lsn)))
2506a6a781a5SDarrick J. Wong 			return __this_address;
2507a45086e2SBrian Foster 	}
2508a45086e2SBrian Foster 
250930f712c9SDave Chinner 	/*
251030f712c9SDave Chinner 	 * Validate the magic number of the agi block.
251130f712c9SDave Chinner 	 */
251230f712c9SDave Chinner 	if (agi->agi_magicnum != cpu_to_be32(XFS_AGI_MAGIC))
2513a6a781a5SDarrick J. Wong 		return __this_address;
251430f712c9SDave Chinner 	if (!XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum)))
2515a6a781a5SDarrick J. Wong 		return __this_address;
251630f712c9SDave Chinner 
2517d2a047f3SDarrick J. Wong 	if (be32_to_cpu(agi->agi_level) < 1 ||
2518d2a047f3SDarrick J. Wong 	    be32_to_cpu(agi->agi_level) > XFS_BTREE_MAXLEVELS)
2519a6a781a5SDarrick J. Wong 		return __this_address;
2520d2a047f3SDarrick J. Wong 
2521d2a047f3SDarrick J. Wong 	if (xfs_sb_version_hasfinobt(&mp->m_sb) &&
2522d2a047f3SDarrick J. Wong 	    (be32_to_cpu(agi->agi_free_level) < 1 ||
2523d2a047f3SDarrick J. Wong 	     be32_to_cpu(agi->agi_free_level) > XFS_BTREE_MAXLEVELS))
2524a6a781a5SDarrick J. Wong 		return __this_address;
2525d2a047f3SDarrick J. Wong 
252630f712c9SDave Chinner 	/*
252730f712c9SDave Chinner 	 * during growfs operations, the perag is not fully initialised,
252830f712c9SDave Chinner 	 * so we can't use it for any useful checking. growfs ensures we can't
252930f712c9SDave Chinner 	 * use it by using uncached buffers that don't have the perag attached
253030f712c9SDave Chinner 	 * so we can detect and avoid this problem.
253130f712c9SDave Chinner 	 */
253230f712c9SDave Chinner 	if (bp->b_pag && be32_to_cpu(agi->agi_seqno) != bp->b_pag->pag_agno)
2533a6a781a5SDarrick J. Wong 		return __this_address;
253430f712c9SDave Chinner 
253530f712c9SDave Chinner 	xfs_check_agi_unlinked(agi);
2536a6a781a5SDarrick J. Wong 	return NULL;
253730f712c9SDave Chinner }
253830f712c9SDave Chinner 
253930f712c9SDave Chinner static void
254030f712c9SDave Chinner xfs_agi_read_verify(
254130f712c9SDave Chinner 	struct xfs_buf	*bp)
254230f712c9SDave Chinner {
254330f712c9SDave Chinner 	struct xfs_mount *mp = bp->b_target->bt_mount;
2544*bc1a09b8SDarrick J. Wong 	xfs_failaddr_t	fa;
254530f712c9SDave Chinner 
254630f712c9SDave Chinner 	if (xfs_sb_version_hascrc(&mp->m_sb) &&
254730f712c9SDave Chinner 	    !xfs_buf_verify_cksum(bp, XFS_AGI_CRC_OFF))
2548*bc1a09b8SDarrick J. Wong 		xfs_verifier_error(bp, -EFSBADCRC, __this_address);
2549*bc1a09b8SDarrick J. Wong 	else {
2550*bc1a09b8SDarrick J. Wong 		fa = xfs_agi_verify(bp);
2551*bc1a09b8SDarrick J. Wong 		if (XFS_TEST_ERROR(fa, mp, XFS_ERRTAG_IALLOC_READ_AGI))
2552*bc1a09b8SDarrick J. Wong 			xfs_verifier_error(bp, -EFSCORRUPTED, fa);
2553*bc1a09b8SDarrick J. Wong 	}
255430f712c9SDave Chinner }
255530f712c9SDave Chinner 
255630f712c9SDave Chinner static void
255730f712c9SDave Chinner xfs_agi_write_verify(
255830f712c9SDave Chinner 	struct xfs_buf	*bp)
255930f712c9SDave Chinner {
256030f712c9SDave Chinner 	struct xfs_mount *mp = bp->b_target->bt_mount;
256130f712c9SDave Chinner 	struct xfs_buf_log_item	*bip = bp->b_fspriv;
2562*bc1a09b8SDarrick J. Wong 	xfs_failaddr_t		fa;
256330f712c9SDave Chinner 
2564*bc1a09b8SDarrick J. Wong 	fa = xfs_agi_verify(bp);
2565*bc1a09b8SDarrick J. Wong 	if (fa) {
2566*bc1a09b8SDarrick J. Wong 		xfs_verifier_error(bp, -EFSCORRUPTED, fa);
256730f712c9SDave Chinner 		return;
256830f712c9SDave Chinner 	}
256930f712c9SDave Chinner 
257030f712c9SDave Chinner 	if (!xfs_sb_version_hascrc(&mp->m_sb))
257130f712c9SDave Chinner 		return;
257230f712c9SDave Chinner 
257330f712c9SDave Chinner 	if (bip)
257430f712c9SDave Chinner 		XFS_BUF_TO_AGI(bp)->agi_lsn = cpu_to_be64(bip->bli_item.li_lsn);
257530f712c9SDave Chinner 	xfs_buf_update_cksum(bp, XFS_AGI_CRC_OFF);
257630f712c9SDave Chinner }
257730f712c9SDave Chinner 
257830f712c9SDave Chinner const struct xfs_buf_ops xfs_agi_buf_ops = {
2579233135b7SEric Sandeen 	.name = "xfs_agi",
258030f712c9SDave Chinner 	.verify_read = xfs_agi_read_verify,
258130f712c9SDave Chinner 	.verify_write = xfs_agi_write_verify,
258230f712c9SDave Chinner };
258330f712c9SDave Chinner 
258430f712c9SDave Chinner /*
258530f712c9SDave Chinner  * Read in the allocation group header (inode allocation section)
258630f712c9SDave Chinner  */
258730f712c9SDave Chinner int
258830f712c9SDave Chinner xfs_read_agi(
258930f712c9SDave Chinner 	struct xfs_mount	*mp,	/* file system mount structure */
259030f712c9SDave Chinner 	struct xfs_trans	*tp,	/* transaction pointer */
259130f712c9SDave Chinner 	xfs_agnumber_t		agno,	/* allocation group number */
259230f712c9SDave Chinner 	struct xfs_buf		**bpp)	/* allocation group hdr buf */
259330f712c9SDave Chinner {
259430f712c9SDave Chinner 	int			error;
259530f712c9SDave Chinner 
259630f712c9SDave Chinner 	trace_xfs_read_agi(mp, agno);
259730f712c9SDave Chinner 
259830f712c9SDave Chinner 	ASSERT(agno != NULLAGNUMBER);
259930f712c9SDave Chinner 	error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
260030f712c9SDave Chinner 			XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
260130f712c9SDave Chinner 			XFS_FSS_TO_BB(mp, 1), 0, bpp, &xfs_agi_buf_ops);
260230f712c9SDave Chinner 	if (error)
260330f712c9SDave Chinner 		return error;
2604200237d6SEric Sandeen 	if (tp)
2605200237d6SEric Sandeen 		xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_AGI_BUF);
260630f712c9SDave Chinner 
260730f712c9SDave Chinner 	xfs_buf_set_ref(*bpp, XFS_AGI_REF);
260830f712c9SDave Chinner 	return 0;
260930f712c9SDave Chinner }
261030f712c9SDave Chinner 
261130f712c9SDave Chinner int
261230f712c9SDave Chinner xfs_ialloc_read_agi(
261330f712c9SDave Chinner 	struct xfs_mount	*mp,	/* file system mount structure */
261430f712c9SDave Chinner 	struct xfs_trans	*tp,	/* transaction pointer */
261530f712c9SDave Chinner 	xfs_agnumber_t		agno,	/* allocation group number */
261630f712c9SDave Chinner 	struct xfs_buf		**bpp)	/* allocation group hdr buf */
261730f712c9SDave Chinner {
261830f712c9SDave Chinner 	struct xfs_agi		*agi;	/* allocation group header */
261930f712c9SDave Chinner 	struct xfs_perag	*pag;	/* per allocation group data */
262030f712c9SDave Chinner 	int			error;
262130f712c9SDave Chinner 
262230f712c9SDave Chinner 	trace_xfs_ialloc_read_agi(mp, agno);
262330f712c9SDave Chinner 
262430f712c9SDave Chinner 	error = xfs_read_agi(mp, tp, agno, bpp);
262530f712c9SDave Chinner 	if (error)
262630f712c9SDave Chinner 		return error;
262730f712c9SDave Chinner 
262830f712c9SDave Chinner 	agi = XFS_BUF_TO_AGI(*bpp);
262930f712c9SDave Chinner 	pag = xfs_perag_get(mp, agno);
263030f712c9SDave Chinner 	if (!pag->pagi_init) {
263130f712c9SDave Chinner 		pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
263230f712c9SDave Chinner 		pag->pagi_count = be32_to_cpu(agi->agi_count);
263330f712c9SDave Chinner 		pag->pagi_init = 1;
263430f712c9SDave Chinner 	}
263530f712c9SDave Chinner 
263630f712c9SDave Chinner 	/*
263730f712c9SDave Chinner 	 * It's possible for these to be out of sync if
263830f712c9SDave Chinner 	 * we are in the middle of a forced shutdown.
263930f712c9SDave Chinner 	 */
264030f712c9SDave Chinner 	ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) ||
264130f712c9SDave Chinner 		XFS_FORCED_SHUTDOWN(mp));
264230f712c9SDave Chinner 	xfs_perag_put(pag);
264330f712c9SDave Chinner 	return 0;
264430f712c9SDave Chinner }
264530f712c9SDave Chinner 
264630f712c9SDave Chinner /*
264730f712c9SDave Chinner  * Read in the agi to initialise the per-ag data in the mount structure
264830f712c9SDave Chinner  */
264930f712c9SDave Chinner int
265030f712c9SDave Chinner xfs_ialloc_pagi_init(
265130f712c9SDave Chinner 	xfs_mount_t	*mp,		/* file system mount structure */
265230f712c9SDave Chinner 	xfs_trans_t	*tp,		/* transaction pointer */
265330f712c9SDave Chinner 	xfs_agnumber_t	agno)		/* allocation group number */
265430f712c9SDave Chinner {
265530f712c9SDave Chinner 	xfs_buf_t	*bp = NULL;
265630f712c9SDave Chinner 	int		error;
265730f712c9SDave Chinner 
265830f712c9SDave Chinner 	error = xfs_ialloc_read_agi(mp, tp, agno, &bp);
265930f712c9SDave Chinner 	if (error)
266030f712c9SDave Chinner 		return error;
266130f712c9SDave Chinner 	if (bp)
266230f712c9SDave Chinner 		xfs_trans_brelse(tp, bp);
266330f712c9SDave Chinner 	return 0;
266430f712c9SDave Chinner }
266591fb9afcSDarrick J. Wong 
266691fb9afcSDarrick J. Wong /* Calculate the first and last possible inode number in an AG. */
266791fb9afcSDarrick J. Wong void
266891fb9afcSDarrick J. Wong xfs_ialloc_agino_range(
266991fb9afcSDarrick J. Wong 	struct xfs_mount	*mp,
267091fb9afcSDarrick J. Wong 	xfs_agnumber_t		agno,
267191fb9afcSDarrick J. Wong 	xfs_agino_t		*first,
267291fb9afcSDarrick J. Wong 	xfs_agino_t		*last)
267391fb9afcSDarrick J. Wong {
267491fb9afcSDarrick J. Wong 	xfs_agblock_t		bno;
267591fb9afcSDarrick J. Wong 	xfs_agblock_t		eoag;
267691fb9afcSDarrick J. Wong 
267791fb9afcSDarrick J. Wong 	eoag = xfs_ag_block_count(mp, agno);
267891fb9afcSDarrick J. Wong 
267991fb9afcSDarrick J. Wong 	/*
268091fb9afcSDarrick J. Wong 	 * Calculate the first inode, which will be in the first
268191fb9afcSDarrick J. Wong 	 * cluster-aligned block after the AGFL.
268291fb9afcSDarrick J. Wong 	 */
268391fb9afcSDarrick J. Wong 	bno = round_up(XFS_AGFL_BLOCK(mp) + 1,
268491fb9afcSDarrick J. Wong 			xfs_ialloc_cluster_alignment(mp));
268591fb9afcSDarrick J. Wong 	*first = XFS_OFFBNO_TO_AGINO(mp, bno, 0);
268691fb9afcSDarrick J. Wong 
268791fb9afcSDarrick J. Wong 	/*
268891fb9afcSDarrick J. Wong 	 * Calculate the last inode, which will be at the end of the
268991fb9afcSDarrick J. Wong 	 * last (aligned) cluster that can be allocated in the AG.
269091fb9afcSDarrick J. Wong 	 */
269191fb9afcSDarrick J. Wong 	bno = round_down(eoag, xfs_ialloc_cluster_alignment(mp));
269291fb9afcSDarrick J. Wong 	*last = XFS_OFFBNO_TO_AGINO(mp, bno, 0) - 1;
269391fb9afcSDarrick J. Wong }
269491fb9afcSDarrick J. Wong 
269591fb9afcSDarrick J. Wong /*
269691fb9afcSDarrick J. Wong  * Verify that an AG inode number pointer neither points outside the AG
269791fb9afcSDarrick J. Wong  * nor points at static metadata.
269891fb9afcSDarrick J. Wong  */
269991fb9afcSDarrick J. Wong bool
270091fb9afcSDarrick J. Wong xfs_verify_agino(
270191fb9afcSDarrick J. Wong 	struct xfs_mount	*mp,
270291fb9afcSDarrick J. Wong 	xfs_agnumber_t		agno,
270391fb9afcSDarrick J. Wong 	xfs_agino_t		agino)
270491fb9afcSDarrick J. Wong {
270591fb9afcSDarrick J. Wong 	xfs_agino_t		first;
270691fb9afcSDarrick J. Wong 	xfs_agino_t		last;
270791fb9afcSDarrick J. Wong 
270891fb9afcSDarrick J. Wong 	xfs_ialloc_agino_range(mp, agno, &first, &last);
270991fb9afcSDarrick J. Wong 	return agino >= first && agino <= last;
271091fb9afcSDarrick J. Wong }
271191fb9afcSDarrick J. Wong 
271291fb9afcSDarrick J. Wong /*
271391fb9afcSDarrick J. Wong  * Verify that an FS inode number pointer neither points outside the
271491fb9afcSDarrick J. Wong  * filesystem nor points at static AG metadata.
271591fb9afcSDarrick J. Wong  */
271691fb9afcSDarrick J. Wong bool
271791fb9afcSDarrick J. Wong xfs_verify_ino(
271891fb9afcSDarrick J. Wong 	struct xfs_mount	*mp,
271991fb9afcSDarrick J. Wong 	xfs_ino_t		ino)
272091fb9afcSDarrick J. Wong {
272191fb9afcSDarrick J. Wong 	xfs_agnumber_t		agno = XFS_INO_TO_AGNO(mp, ino);
272291fb9afcSDarrick J. Wong 	xfs_agino_t		agino = XFS_INO_TO_AGINO(mp, ino);
272391fb9afcSDarrick J. Wong 
272491fb9afcSDarrick J. Wong 	if (agno >= mp->m_sb.sb_agcount)
272591fb9afcSDarrick J. Wong 		return false;
272691fb9afcSDarrick J. Wong 	if (XFS_AGINO_TO_INO(mp, agno, agino) != ino)
272791fb9afcSDarrick J. Wong 		return false;
272891fb9afcSDarrick J. Wong 	return xfs_verify_agino(mp, agno, agino);
272991fb9afcSDarrick J. Wong }
273091fb9afcSDarrick J. Wong 
273191fb9afcSDarrick J. Wong /* Is this an internal inode number? */
273291fb9afcSDarrick J. Wong bool
273391fb9afcSDarrick J. Wong xfs_internal_inum(
273491fb9afcSDarrick J. Wong 	struct xfs_mount	*mp,
273591fb9afcSDarrick J. Wong 	xfs_ino_t		ino)
273691fb9afcSDarrick J. Wong {
273791fb9afcSDarrick J. Wong 	return ino == mp->m_sb.sb_rbmino || ino == mp->m_sb.sb_rsumino ||
273891fb9afcSDarrick J. Wong 		(xfs_sb_version_hasquota(&mp->m_sb) &&
273991fb9afcSDarrick J. Wong 		 xfs_is_quota_inode(&mp->m_sb, ino));
274091fb9afcSDarrick J. Wong }
274191fb9afcSDarrick J. Wong 
274291fb9afcSDarrick J. Wong /*
274391fb9afcSDarrick J. Wong  * Verify that a directory entry's inode number doesn't point at an internal
274491fb9afcSDarrick J. Wong  * inode, empty space, or static AG metadata.
274591fb9afcSDarrick J. Wong  */
274691fb9afcSDarrick J. Wong bool
274791fb9afcSDarrick J. Wong xfs_verify_dir_ino(
274891fb9afcSDarrick J. Wong 	struct xfs_mount	*mp,
274991fb9afcSDarrick J. Wong 	xfs_ino_t		ino)
275091fb9afcSDarrick J. Wong {
275191fb9afcSDarrick J. Wong 	if (xfs_internal_inum(mp, ino))
275291fb9afcSDarrick J. Wong 		return false;
275391fb9afcSDarrick J. Wong 	return xfs_verify_ino(mp, ino);
275491fb9afcSDarrick J. Wong }
2755