xref: /openbmc/linux/fs/xfs/libxfs/xfs_ialloc.c (revision 463958af5c92d876fd2fe3c756f18bd0ce70b713)
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"
2730f712c9SDave Chinner #include "xfs_inode.h"
2830f712c9SDave Chinner #include "xfs_btree.h"
2930f712c9SDave Chinner #include "xfs_ialloc.h"
3030f712c9SDave Chinner #include "xfs_ialloc_btree.h"
3130f712c9SDave Chinner #include "xfs_alloc.h"
3230f712c9SDave Chinner #include "xfs_rtalloc.h"
3330f712c9SDave Chinner #include "xfs_error.h"
3430f712c9SDave Chinner #include "xfs_bmap.h"
3530f712c9SDave Chinner #include "xfs_cksum.h"
3630f712c9SDave Chinner #include "xfs_trans.h"
3730f712c9SDave Chinner #include "xfs_buf_item.h"
3830f712c9SDave Chinner #include "xfs_icreate_item.h"
3930f712c9SDave Chinner #include "xfs_icache.h"
4030f712c9SDave Chinner #include "xfs_trace.h"
4130f712c9SDave Chinner 
4230f712c9SDave Chinner 
4330f712c9SDave Chinner /*
4430f712c9SDave Chinner  * Allocation group level functions.
4530f712c9SDave Chinner  */
4630f712c9SDave Chinner static inline int
4730f712c9SDave Chinner xfs_ialloc_cluster_alignment(
487a1df156SDave Chinner 	struct xfs_mount	*mp)
4930f712c9SDave Chinner {
507a1df156SDave Chinner 	if (xfs_sb_version_hasalign(&mp->m_sb) &&
517a1df156SDave Chinner 	    mp->m_sb.sb_inoalignmt >=
527a1df156SDave Chinner 			XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size))
537a1df156SDave Chinner 		return mp->m_sb.sb_inoalignmt;
5430f712c9SDave Chinner 	return 1;
5530f712c9SDave Chinner }
5630f712c9SDave Chinner 
5730f712c9SDave Chinner /*
5830f712c9SDave Chinner  * Lookup a record by ino in the btree given by cur.
5930f712c9SDave Chinner  */
6030f712c9SDave Chinner int					/* error */
6130f712c9SDave Chinner xfs_inobt_lookup(
6230f712c9SDave Chinner 	struct xfs_btree_cur	*cur,	/* btree cursor */
6330f712c9SDave Chinner 	xfs_agino_t		ino,	/* starting inode of chunk */
6430f712c9SDave Chinner 	xfs_lookup_t		dir,	/* <=, >=, == */
6530f712c9SDave Chinner 	int			*stat)	/* success/failure */
6630f712c9SDave Chinner {
6730f712c9SDave Chinner 	cur->bc_rec.i.ir_startino = ino;
685419040fSBrian Foster 	cur->bc_rec.i.ir_holemask = 0;
695419040fSBrian Foster 	cur->bc_rec.i.ir_count = 0;
7030f712c9SDave Chinner 	cur->bc_rec.i.ir_freecount = 0;
7130f712c9SDave Chinner 	cur->bc_rec.i.ir_free = 0;
7230f712c9SDave Chinner 	return xfs_btree_lookup(cur, dir, stat);
7330f712c9SDave Chinner }
7430f712c9SDave Chinner 
7530f712c9SDave Chinner /*
7630f712c9SDave Chinner  * Update the record referred to by cur to the value given.
7730f712c9SDave Chinner  * This either works (return 0) or gets an EFSCORRUPTED error.
7830f712c9SDave Chinner  */
7930f712c9SDave Chinner STATIC int				/* error */
8030f712c9SDave Chinner xfs_inobt_update(
8130f712c9SDave Chinner 	struct xfs_btree_cur	*cur,	/* btree cursor */
8230f712c9SDave Chinner 	xfs_inobt_rec_incore_t	*irec)	/* btree record */
8330f712c9SDave Chinner {
8430f712c9SDave Chinner 	union xfs_btree_rec	rec;
8530f712c9SDave Chinner 
8630f712c9SDave Chinner 	rec.inobt.ir_startino = cpu_to_be32(irec->ir_startino);
875419040fSBrian Foster 	if (xfs_sb_version_hassparseinodes(&cur->bc_mp->m_sb)) {
885419040fSBrian Foster 		rec.inobt.ir_u.sp.ir_holemask = cpu_to_be16(irec->ir_holemask);
895419040fSBrian Foster 		rec.inobt.ir_u.sp.ir_count = irec->ir_count;
905419040fSBrian Foster 		rec.inobt.ir_u.sp.ir_freecount = irec->ir_freecount;
915419040fSBrian Foster 	} else {
925419040fSBrian Foster 		/* ir_holemask/ir_count not supported on-disk */
935419040fSBrian Foster 		rec.inobt.ir_u.f.ir_freecount = cpu_to_be32(irec->ir_freecount);
945419040fSBrian Foster 	}
9530f712c9SDave Chinner 	rec.inobt.ir_free = cpu_to_be64(irec->ir_free);
9630f712c9SDave Chinner 	return xfs_btree_update(cur, &rec);
9730f712c9SDave Chinner }
9830f712c9SDave Chinner 
9930f712c9SDave Chinner /*
10030f712c9SDave Chinner  * Get the data from the pointed-to record.
10130f712c9SDave Chinner  */
10230f712c9SDave Chinner int					/* error */
10330f712c9SDave Chinner xfs_inobt_get_rec(
10430f712c9SDave Chinner 	struct xfs_btree_cur	*cur,	/* btree cursor */
10530f712c9SDave Chinner 	xfs_inobt_rec_incore_t	*irec,	/* btree record */
10630f712c9SDave Chinner 	int			*stat)	/* output: success/failure */
10730f712c9SDave Chinner {
10830f712c9SDave Chinner 	union xfs_btree_rec	*rec;
10930f712c9SDave Chinner 	int			error;
11030f712c9SDave Chinner 
11130f712c9SDave Chinner 	error = xfs_btree_get_rec(cur, &rec, stat);
1125419040fSBrian Foster 	if (error || *stat == 0)
11330f712c9SDave Chinner 		return error;
1145419040fSBrian Foster 
1155419040fSBrian Foster 	irec->ir_startino = be32_to_cpu(rec->inobt.ir_startino);
1165419040fSBrian Foster 	if (xfs_sb_version_hassparseinodes(&cur->bc_mp->m_sb)) {
1175419040fSBrian Foster 		irec->ir_holemask = be16_to_cpu(rec->inobt.ir_u.sp.ir_holemask);
1185419040fSBrian Foster 		irec->ir_count = rec->inobt.ir_u.sp.ir_count;
1195419040fSBrian Foster 		irec->ir_freecount = rec->inobt.ir_u.sp.ir_freecount;
1205419040fSBrian Foster 	} else {
1215419040fSBrian Foster 		/*
1225419040fSBrian Foster 		 * ir_holemask/ir_count not supported on-disk. Fill in hardcoded
1235419040fSBrian Foster 		 * values for full inode chunks.
1245419040fSBrian Foster 		 */
1255419040fSBrian Foster 		irec->ir_holemask = XFS_INOBT_HOLEMASK_FULL;
1265419040fSBrian Foster 		irec->ir_count = XFS_INODES_PER_CHUNK;
1275419040fSBrian Foster 		irec->ir_freecount =
1285419040fSBrian Foster 				be32_to_cpu(rec->inobt.ir_u.f.ir_freecount);
1295419040fSBrian Foster 	}
1305419040fSBrian Foster 	irec->ir_free = be64_to_cpu(rec->inobt.ir_free);
1315419040fSBrian Foster 
1325419040fSBrian Foster 	return 0;
13330f712c9SDave Chinner }
13430f712c9SDave Chinner 
13530f712c9SDave Chinner /*
13630f712c9SDave Chinner  * Insert a single inobt record. Cursor must already point to desired location.
13730f712c9SDave Chinner  */
13830f712c9SDave Chinner STATIC int
13930f712c9SDave Chinner xfs_inobt_insert_rec(
14030f712c9SDave Chinner 	struct xfs_btree_cur	*cur,
1415419040fSBrian Foster 	__uint16_t		holemask,
1425419040fSBrian Foster 	__uint8_t		count,
14330f712c9SDave Chinner 	__int32_t		freecount,
14430f712c9SDave Chinner 	xfs_inofree_t		free,
14530f712c9SDave Chinner 	int			*stat)
14630f712c9SDave Chinner {
1475419040fSBrian Foster 	cur->bc_rec.i.ir_holemask = holemask;
1485419040fSBrian Foster 	cur->bc_rec.i.ir_count = count;
14930f712c9SDave Chinner 	cur->bc_rec.i.ir_freecount = freecount;
15030f712c9SDave Chinner 	cur->bc_rec.i.ir_free = free;
15130f712c9SDave Chinner 	return xfs_btree_insert(cur, stat);
15230f712c9SDave Chinner }
15330f712c9SDave Chinner 
15430f712c9SDave Chinner /*
15530f712c9SDave Chinner  * Insert records describing a newly allocated inode chunk into the inobt.
15630f712c9SDave Chinner  */
15730f712c9SDave Chinner STATIC int
15830f712c9SDave Chinner xfs_inobt_insert(
15930f712c9SDave Chinner 	struct xfs_mount	*mp,
16030f712c9SDave Chinner 	struct xfs_trans	*tp,
16130f712c9SDave Chinner 	struct xfs_buf		*agbp,
16230f712c9SDave Chinner 	xfs_agino_t		newino,
16330f712c9SDave Chinner 	xfs_agino_t		newlen,
16430f712c9SDave Chinner 	xfs_btnum_t		btnum)
16530f712c9SDave Chinner {
16630f712c9SDave Chinner 	struct xfs_btree_cur	*cur;
16730f712c9SDave Chinner 	struct xfs_agi		*agi = XFS_BUF_TO_AGI(agbp);
16830f712c9SDave Chinner 	xfs_agnumber_t		agno = be32_to_cpu(agi->agi_seqno);
16930f712c9SDave Chinner 	xfs_agino_t		thisino;
17030f712c9SDave Chinner 	int			i;
17130f712c9SDave Chinner 	int			error;
17230f712c9SDave Chinner 
17330f712c9SDave Chinner 	cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, btnum);
17430f712c9SDave Chinner 
17530f712c9SDave Chinner 	for (thisino = newino;
17630f712c9SDave Chinner 	     thisino < newino + newlen;
17730f712c9SDave Chinner 	     thisino += XFS_INODES_PER_CHUNK) {
17830f712c9SDave Chinner 		error = xfs_inobt_lookup(cur, thisino, XFS_LOOKUP_EQ, &i);
17930f712c9SDave Chinner 		if (error) {
18030f712c9SDave Chinner 			xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
18130f712c9SDave Chinner 			return error;
18230f712c9SDave Chinner 		}
18330f712c9SDave Chinner 		ASSERT(i == 0);
18430f712c9SDave Chinner 
1855419040fSBrian Foster 		error = xfs_inobt_insert_rec(cur, XFS_INOBT_HOLEMASK_FULL,
1865419040fSBrian Foster 					     XFS_INODES_PER_CHUNK,
1875419040fSBrian Foster 					     XFS_INODES_PER_CHUNK,
18830f712c9SDave Chinner 					     XFS_INOBT_ALL_FREE, &i);
18930f712c9SDave Chinner 		if (error) {
19030f712c9SDave Chinner 			xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
19130f712c9SDave Chinner 			return error;
19230f712c9SDave Chinner 		}
19330f712c9SDave Chinner 		ASSERT(i == 1);
19430f712c9SDave Chinner 	}
19530f712c9SDave Chinner 
19630f712c9SDave Chinner 	xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
19730f712c9SDave Chinner 
19830f712c9SDave Chinner 	return 0;
19930f712c9SDave Chinner }
20030f712c9SDave Chinner 
20130f712c9SDave Chinner /*
20230f712c9SDave Chinner  * Verify that the number of free inodes in the AGI is correct.
20330f712c9SDave Chinner  */
20430f712c9SDave Chinner #ifdef DEBUG
20530f712c9SDave Chinner STATIC int
20630f712c9SDave Chinner xfs_check_agi_freecount(
20730f712c9SDave Chinner 	struct xfs_btree_cur	*cur,
20830f712c9SDave Chinner 	struct xfs_agi		*agi)
20930f712c9SDave Chinner {
21030f712c9SDave Chinner 	if (cur->bc_nlevels == 1) {
21130f712c9SDave Chinner 		xfs_inobt_rec_incore_t rec;
21230f712c9SDave Chinner 		int		freecount = 0;
21330f712c9SDave Chinner 		int		error;
21430f712c9SDave Chinner 		int		i;
21530f712c9SDave Chinner 
21630f712c9SDave Chinner 		error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
21730f712c9SDave Chinner 		if (error)
21830f712c9SDave Chinner 			return error;
21930f712c9SDave Chinner 
22030f712c9SDave Chinner 		do {
22130f712c9SDave Chinner 			error = xfs_inobt_get_rec(cur, &rec, &i);
22230f712c9SDave Chinner 			if (error)
22330f712c9SDave Chinner 				return error;
22430f712c9SDave Chinner 
22530f712c9SDave Chinner 			if (i) {
22630f712c9SDave Chinner 				freecount += rec.ir_freecount;
22730f712c9SDave Chinner 				error = xfs_btree_increment(cur, 0, &i);
22830f712c9SDave Chinner 				if (error)
22930f712c9SDave Chinner 					return error;
23030f712c9SDave Chinner 			}
23130f712c9SDave Chinner 		} while (i == 1);
23230f712c9SDave Chinner 
23330f712c9SDave Chinner 		if (!XFS_FORCED_SHUTDOWN(cur->bc_mp))
23430f712c9SDave Chinner 			ASSERT(freecount == be32_to_cpu(agi->agi_freecount));
23530f712c9SDave Chinner 	}
23630f712c9SDave Chinner 	return 0;
23730f712c9SDave Chinner }
23830f712c9SDave Chinner #else
23930f712c9SDave Chinner #define xfs_check_agi_freecount(cur, agi)	0
24030f712c9SDave Chinner #endif
24130f712c9SDave Chinner 
24230f712c9SDave Chinner /*
24330f712c9SDave Chinner  * Initialise a new set of inodes. When called without a transaction context
24430f712c9SDave Chinner  * (e.g. from recovery) we initiate a delayed write of the inode buffers rather
24530f712c9SDave Chinner  * than logging them (which in a transaction context puts them into the AIL
24630f712c9SDave Chinner  * for writeback rather than the xfsbufd queue).
24730f712c9SDave Chinner  */
24830f712c9SDave Chinner int
24930f712c9SDave Chinner xfs_ialloc_inode_init(
25030f712c9SDave Chinner 	struct xfs_mount	*mp,
25130f712c9SDave Chinner 	struct xfs_trans	*tp,
25230f712c9SDave Chinner 	struct list_head	*buffer_list,
253*463958afSBrian Foster 	int			icount,
25430f712c9SDave Chinner 	xfs_agnumber_t		agno,
25530f712c9SDave Chinner 	xfs_agblock_t		agbno,
25630f712c9SDave Chinner 	xfs_agblock_t		length,
25730f712c9SDave Chinner 	unsigned int		gen)
25830f712c9SDave Chinner {
25930f712c9SDave Chinner 	struct xfs_buf		*fbuf;
26030f712c9SDave Chinner 	struct xfs_dinode	*free;
26130f712c9SDave Chinner 	int			nbufs, blks_per_cluster, inodes_per_cluster;
26230f712c9SDave Chinner 	int			version;
26330f712c9SDave Chinner 	int			i, j;
26430f712c9SDave Chinner 	xfs_daddr_t		d;
26530f712c9SDave Chinner 	xfs_ino_t		ino = 0;
26630f712c9SDave Chinner 
26730f712c9SDave Chinner 	/*
26830f712c9SDave Chinner 	 * Loop over the new block(s), filling in the inodes.  For small block
26930f712c9SDave Chinner 	 * sizes, manipulate the inodes in buffers  which are multiples of the
27030f712c9SDave Chinner 	 * blocks size.
27130f712c9SDave Chinner 	 */
27230f712c9SDave Chinner 	blks_per_cluster = xfs_icluster_size_fsb(mp);
27330f712c9SDave Chinner 	inodes_per_cluster = blks_per_cluster << mp->m_sb.sb_inopblog;
27430f712c9SDave Chinner 	nbufs = length / blks_per_cluster;
27530f712c9SDave Chinner 
27630f712c9SDave Chinner 	/*
27730f712c9SDave Chinner 	 * Figure out what version number to use in the inodes we create.  If
27830f712c9SDave Chinner 	 * the superblock version has caught up to the one that supports the new
27930f712c9SDave Chinner 	 * inode format, then use the new inode version.  Otherwise use the old
28030f712c9SDave Chinner 	 * version so that old kernels will continue to be able to use the file
28130f712c9SDave Chinner 	 * system.
28230f712c9SDave Chinner 	 *
28330f712c9SDave Chinner 	 * For v3 inodes, we also need to write the inode number into the inode,
28430f712c9SDave Chinner 	 * so calculate the first inode number of the chunk here as
28530f712c9SDave Chinner 	 * XFS_OFFBNO_TO_AGINO() only works within a filesystem block, not
28630f712c9SDave Chinner 	 * across multiple filesystem blocks (such as a cluster) and so cannot
28730f712c9SDave Chinner 	 * be used in the cluster buffer loop below.
28830f712c9SDave Chinner 	 *
28930f712c9SDave Chinner 	 * Further, because we are writing the inode directly into the buffer
29030f712c9SDave Chinner 	 * and calculating a CRC on the entire inode, we have ot log the entire
29130f712c9SDave Chinner 	 * inode so that the entire range the CRC covers is present in the log.
29230f712c9SDave Chinner 	 * That means for v3 inode we log the entire buffer rather than just the
29330f712c9SDave Chinner 	 * inode cores.
29430f712c9SDave Chinner 	 */
29530f712c9SDave Chinner 	if (xfs_sb_version_hascrc(&mp->m_sb)) {
29630f712c9SDave Chinner 		version = 3;
29730f712c9SDave Chinner 		ino = XFS_AGINO_TO_INO(mp, agno,
29830f712c9SDave Chinner 				       XFS_OFFBNO_TO_AGINO(mp, agbno, 0));
29930f712c9SDave Chinner 
30030f712c9SDave Chinner 		/*
30130f712c9SDave Chinner 		 * log the initialisation that is about to take place as an
30230f712c9SDave Chinner 		 * logical operation. This means the transaction does not
30330f712c9SDave Chinner 		 * need to log the physical changes to the inode buffers as log
30430f712c9SDave Chinner 		 * recovery will know what initialisation is actually needed.
30530f712c9SDave Chinner 		 * Hence we only need to log the buffers as "ordered" buffers so
30630f712c9SDave Chinner 		 * they track in the AIL as if they were physically logged.
30730f712c9SDave Chinner 		 */
30830f712c9SDave Chinner 		if (tp)
309*463958afSBrian Foster 			xfs_icreate_log(tp, agno, agbno, icount,
31030f712c9SDave Chinner 					mp->m_sb.sb_inodesize, length, gen);
31130f712c9SDave Chinner 	} else
31230f712c9SDave Chinner 		version = 2;
31330f712c9SDave Chinner 
31430f712c9SDave Chinner 	for (j = 0; j < nbufs; j++) {
31530f712c9SDave Chinner 		/*
31630f712c9SDave Chinner 		 * Get the block.
31730f712c9SDave Chinner 		 */
31830f712c9SDave Chinner 		d = XFS_AGB_TO_DADDR(mp, agno, agbno + (j * blks_per_cluster));
31930f712c9SDave Chinner 		fbuf = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
32030f712c9SDave Chinner 					 mp->m_bsize * blks_per_cluster,
32130f712c9SDave Chinner 					 XBF_UNMAPPED);
32230f712c9SDave Chinner 		if (!fbuf)
3232451337dSDave Chinner 			return -ENOMEM;
32430f712c9SDave Chinner 
32530f712c9SDave Chinner 		/* Initialize the inode buffers and log them appropriately. */
32630f712c9SDave Chinner 		fbuf->b_ops = &xfs_inode_buf_ops;
32730f712c9SDave Chinner 		xfs_buf_zero(fbuf, 0, BBTOB(fbuf->b_length));
32830f712c9SDave Chinner 		for (i = 0; i < inodes_per_cluster; i++) {
32930f712c9SDave Chinner 			int	ioffset = i << mp->m_sb.sb_inodelog;
33030f712c9SDave Chinner 			uint	isize = xfs_dinode_size(version);
33130f712c9SDave Chinner 
33230f712c9SDave Chinner 			free = xfs_make_iptr(mp, fbuf, i);
33330f712c9SDave Chinner 			free->di_magic = cpu_to_be16(XFS_DINODE_MAGIC);
33430f712c9SDave Chinner 			free->di_version = version;
33530f712c9SDave Chinner 			free->di_gen = cpu_to_be32(gen);
33630f712c9SDave Chinner 			free->di_next_unlinked = cpu_to_be32(NULLAGINO);
33730f712c9SDave Chinner 
33830f712c9SDave Chinner 			if (version == 3) {
33930f712c9SDave Chinner 				free->di_ino = cpu_to_be64(ino);
34030f712c9SDave Chinner 				ino++;
34130f712c9SDave Chinner 				uuid_copy(&free->di_uuid, &mp->m_sb.sb_uuid);
34230f712c9SDave Chinner 				xfs_dinode_calc_crc(mp, free);
34330f712c9SDave Chinner 			} else if (tp) {
34430f712c9SDave Chinner 				/* just log the inode core */
34530f712c9SDave Chinner 				xfs_trans_log_buf(tp, fbuf, ioffset,
34630f712c9SDave Chinner 						  ioffset + isize - 1);
34730f712c9SDave Chinner 			}
34830f712c9SDave Chinner 		}
34930f712c9SDave Chinner 
35030f712c9SDave Chinner 		if (tp) {
35130f712c9SDave Chinner 			/*
35230f712c9SDave Chinner 			 * Mark the buffer as an inode allocation buffer so it
35330f712c9SDave Chinner 			 * sticks in AIL at the point of this allocation
35430f712c9SDave Chinner 			 * transaction. This ensures the they are on disk before
35530f712c9SDave Chinner 			 * the tail of the log can be moved past this
35630f712c9SDave Chinner 			 * transaction (i.e. by preventing relogging from moving
35730f712c9SDave Chinner 			 * it forward in the log).
35830f712c9SDave Chinner 			 */
35930f712c9SDave Chinner 			xfs_trans_inode_alloc_buf(tp, fbuf);
36030f712c9SDave Chinner 			if (version == 3) {
36130f712c9SDave Chinner 				/*
36230f712c9SDave Chinner 				 * Mark the buffer as ordered so that they are
36330f712c9SDave Chinner 				 * not physically logged in the transaction but
36430f712c9SDave Chinner 				 * still tracked in the AIL as part of the
36530f712c9SDave Chinner 				 * transaction and pin the log appropriately.
36630f712c9SDave Chinner 				 */
36730f712c9SDave Chinner 				xfs_trans_ordered_buf(tp, fbuf);
36830f712c9SDave Chinner 				xfs_trans_log_buf(tp, fbuf, 0,
36930f712c9SDave Chinner 						  BBTOB(fbuf->b_length) - 1);
37030f712c9SDave Chinner 			}
37130f712c9SDave Chinner 		} else {
37230f712c9SDave Chinner 			fbuf->b_flags |= XBF_DONE;
37330f712c9SDave Chinner 			xfs_buf_delwri_queue(fbuf, buffer_list);
37430f712c9SDave Chinner 			xfs_buf_relse(fbuf);
37530f712c9SDave Chinner 		}
37630f712c9SDave Chinner 	}
37730f712c9SDave Chinner 	return 0;
37830f712c9SDave Chinner }
37930f712c9SDave Chinner 
38030f712c9SDave Chinner /*
38130f712c9SDave Chinner  * Allocate new inodes in the allocation group specified by agbp.
38230f712c9SDave Chinner  * Return 0 for success, else error code.
38330f712c9SDave Chinner  */
38430f712c9SDave Chinner STATIC int				/* error code or 0 */
38530f712c9SDave Chinner xfs_ialloc_ag_alloc(
38630f712c9SDave Chinner 	xfs_trans_t	*tp,		/* transaction pointer */
38730f712c9SDave Chinner 	xfs_buf_t	*agbp,		/* alloc group buffer */
38830f712c9SDave Chinner 	int		*alloc)
38930f712c9SDave Chinner {
39030f712c9SDave Chinner 	xfs_agi_t	*agi;		/* allocation group header */
39130f712c9SDave Chinner 	xfs_alloc_arg_t	args;		/* allocation argument structure */
39230f712c9SDave Chinner 	xfs_agnumber_t	agno;
39330f712c9SDave Chinner 	int		error;
39430f712c9SDave Chinner 	xfs_agino_t	newino;		/* new first inode's number */
39530f712c9SDave Chinner 	xfs_agino_t	newlen;		/* new number of inodes */
39630f712c9SDave Chinner 	int		isaligned = 0;	/* inode allocation at stripe unit */
39730f712c9SDave Chinner 					/* boundary */
39830f712c9SDave Chinner 	struct xfs_perag *pag;
39930f712c9SDave Chinner 
40030f712c9SDave Chinner 	memset(&args, 0, sizeof(args));
40130f712c9SDave Chinner 	args.tp = tp;
40230f712c9SDave Chinner 	args.mp = tp->t_mountp;
40330f712c9SDave Chinner 
40430f712c9SDave Chinner 	/*
40530f712c9SDave Chinner 	 * Locking will ensure that we don't have two callers in here
40630f712c9SDave Chinner 	 * at one time.
40730f712c9SDave Chinner 	 */
40830f712c9SDave Chinner 	newlen = args.mp->m_ialloc_inos;
40930f712c9SDave Chinner 	if (args.mp->m_maxicount &&
410501ab323SDave Chinner 	    percpu_counter_read(&args.mp->m_icount) + newlen >
411501ab323SDave Chinner 							args.mp->m_maxicount)
4122451337dSDave Chinner 		return -ENOSPC;
41330f712c9SDave Chinner 	args.minlen = args.maxlen = args.mp->m_ialloc_blks;
41430f712c9SDave Chinner 	/*
41530f712c9SDave Chinner 	 * First try to allocate inodes contiguous with the last-allocated
41630f712c9SDave Chinner 	 * chunk of inodes.  If the filesystem is striped, this will fill
41730f712c9SDave Chinner 	 * an entire stripe unit with inodes.
41830f712c9SDave Chinner 	 */
41930f712c9SDave Chinner 	agi = XFS_BUF_TO_AGI(agbp);
42030f712c9SDave Chinner 	newino = be32_to_cpu(agi->agi_newino);
42130f712c9SDave Chinner 	agno = be32_to_cpu(agi->agi_seqno);
42230f712c9SDave Chinner 	args.agbno = XFS_AGINO_TO_AGBNO(args.mp, newino) +
42330f712c9SDave Chinner 		     args.mp->m_ialloc_blks;
42430f712c9SDave Chinner 	if (likely(newino != NULLAGINO &&
42530f712c9SDave Chinner 		  (args.agbno < be32_to_cpu(agi->agi_length)))) {
42630f712c9SDave Chinner 		args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
42730f712c9SDave Chinner 		args.type = XFS_ALLOCTYPE_THIS_BNO;
42830f712c9SDave Chinner 		args.prod = 1;
42930f712c9SDave Chinner 
43030f712c9SDave Chinner 		/*
43130f712c9SDave Chinner 		 * We need to take into account alignment here to ensure that
43230f712c9SDave Chinner 		 * we don't modify the free list if we fail to have an exact
43330f712c9SDave Chinner 		 * block. If we don't have an exact match, and every oher
43430f712c9SDave Chinner 		 * attempt allocation attempt fails, we'll end up cancelling
43530f712c9SDave Chinner 		 * a dirty transaction and shutting down.
43630f712c9SDave Chinner 		 *
43730f712c9SDave Chinner 		 * For an exact allocation, alignment must be 1,
43830f712c9SDave Chinner 		 * however we need to take cluster alignment into account when
43930f712c9SDave Chinner 		 * fixing up the freelist. Use the minalignslop field to
44030f712c9SDave Chinner 		 * indicate that extra blocks might be required for alignment,
44130f712c9SDave Chinner 		 * but not to use them in the actual exact allocation.
44230f712c9SDave Chinner 		 */
44330f712c9SDave Chinner 		args.alignment = 1;
4447a1df156SDave Chinner 		args.minalignslop = xfs_ialloc_cluster_alignment(args.mp) - 1;
44530f712c9SDave Chinner 
44630f712c9SDave Chinner 		/* Allow space for the inode btree to split. */
44730f712c9SDave Chinner 		args.minleft = args.mp->m_in_maxlevels - 1;
44830f712c9SDave Chinner 		if ((error = xfs_alloc_vextent(&args)))
44930f712c9SDave Chinner 			return error;
45030f712c9SDave Chinner 
45130f712c9SDave Chinner 		/*
45230f712c9SDave Chinner 		 * This request might have dirtied the transaction if the AG can
45330f712c9SDave Chinner 		 * satisfy the request, but the exact block was not available.
45430f712c9SDave Chinner 		 * If the allocation did fail, subsequent requests will relax
45530f712c9SDave Chinner 		 * the exact agbno requirement and increase the alignment
45630f712c9SDave Chinner 		 * instead. It is critical that the total size of the request
45730f712c9SDave Chinner 		 * (len + alignment + slop) does not increase from this point
45830f712c9SDave Chinner 		 * on, so reset minalignslop to ensure it is not included in
45930f712c9SDave Chinner 		 * subsequent requests.
46030f712c9SDave Chinner 		 */
46130f712c9SDave Chinner 		args.minalignslop = 0;
46230f712c9SDave Chinner 	} else
46330f712c9SDave Chinner 		args.fsbno = NULLFSBLOCK;
46430f712c9SDave Chinner 
46530f712c9SDave Chinner 	if (unlikely(args.fsbno == NULLFSBLOCK)) {
46630f712c9SDave Chinner 		/*
46730f712c9SDave Chinner 		 * Set the alignment for the allocation.
46830f712c9SDave Chinner 		 * If stripe alignment is turned on then align at stripe unit
46930f712c9SDave Chinner 		 * boundary.
47030f712c9SDave Chinner 		 * If the cluster size is smaller than a filesystem block
47130f712c9SDave Chinner 		 * then we're doing I/O for inodes in filesystem block size
47230f712c9SDave Chinner 		 * pieces, so don't need alignment anyway.
47330f712c9SDave Chinner 		 */
47430f712c9SDave Chinner 		isaligned = 0;
47530f712c9SDave Chinner 		if (args.mp->m_sinoalign) {
47630f712c9SDave Chinner 			ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN));
47730f712c9SDave Chinner 			args.alignment = args.mp->m_dalign;
47830f712c9SDave Chinner 			isaligned = 1;
47930f712c9SDave Chinner 		} else
4807a1df156SDave Chinner 			args.alignment = xfs_ialloc_cluster_alignment(args.mp);
48130f712c9SDave Chinner 		/*
48230f712c9SDave Chinner 		 * Need to figure out where to allocate the inode blocks.
48330f712c9SDave Chinner 		 * Ideally they should be spaced out through the a.g.
48430f712c9SDave Chinner 		 * For now, just allocate blocks up front.
48530f712c9SDave Chinner 		 */
48630f712c9SDave Chinner 		args.agbno = be32_to_cpu(agi->agi_root);
48730f712c9SDave Chinner 		args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
48830f712c9SDave Chinner 		/*
48930f712c9SDave Chinner 		 * Allocate a fixed-size extent of inodes.
49030f712c9SDave Chinner 		 */
49130f712c9SDave Chinner 		args.type = XFS_ALLOCTYPE_NEAR_BNO;
49230f712c9SDave Chinner 		args.prod = 1;
49330f712c9SDave Chinner 		/*
49430f712c9SDave Chinner 		 * Allow space for the inode btree to split.
49530f712c9SDave Chinner 		 */
49630f712c9SDave Chinner 		args.minleft = args.mp->m_in_maxlevels - 1;
49730f712c9SDave Chinner 		if ((error = xfs_alloc_vextent(&args)))
49830f712c9SDave Chinner 			return error;
49930f712c9SDave Chinner 	}
50030f712c9SDave Chinner 
50130f712c9SDave Chinner 	/*
50230f712c9SDave Chinner 	 * If stripe alignment is turned on, then try again with cluster
50330f712c9SDave Chinner 	 * alignment.
50430f712c9SDave Chinner 	 */
50530f712c9SDave Chinner 	if (isaligned && args.fsbno == NULLFSBLOCK) {
50630f712c9SDave Chinner 		args.type = XFS_ALLOCTYPE_NEAR_BNO;
50730f712c9SDave Chinner 		args.agbno = be32_to_cpu(agi->agi_root);
50830f712c9SDave Chinner 		args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
5097a1df156SDave Chinner 		args.alignment = xfs_ialloc_cluster_alignment(args.mp);
51030f712c9SDave Chinner 		if ((error = xfs_alloc_vextent(&args)))
51130f712c9SDave Chinner 			return error;
51230f712c9SDave Chinner 	}
51330f712c9SDave Chinner 
51430f712c9SDave Chinner 	if (args.fsbno == NULLFSBLOCK) {
51530f712c9SDave Chinner 		*alloc = 0;
51630f712c9SDave Chinner 		return 0;
51730f712c9SDave Chinner 	}
51830f712c9SDave Chinner 	ASSERT(args.len == args.minlen);
51930f712c9SDave Chinner 
52030f712c9SDave Chinner 	/*
52130f712c9SDave Chinner 	 * Stamp and write the inode buffers.
52230f712c9SDave Chinner 	 *
52330f712c9SDave Chinner 	 * Seed the new inode cluster with a random generation number. This
52430f712c9SDave Chinner 	 * prevents short-term reuse of generation numbers if a chunk is
52530f712c9SDave Chinner 	 * freed and then immediately reallocated. We use random numbers
52630f712c9SDave Chinner 	 * rather than a linear progression to prevent the next generation
52730f712c9SDave Chinner 	 * number from being easily guessable.
52830f712c9SDave Chinner 	 */
529*463958afSBrian Foster 	error = xfs_ialloc_inode_init(args.mp, tp, NULL, newlen, agno,
530*463958afSBrian Foster 			args.agbno, args.len, prandom_u32());
53130f712c9SDave Chinner 
53230f712c9SDave Chinner 	if (error)
53330f712c9SDave Chinner 		return error;
53430f712c9SDave Chinner 	/*
53530f712c9SDave Chinner 	 * Convert the results.
53630f712c9SDave Chinner 	 */
53730f712c9SDave Chinner 	newino = XFS_OFFBNO_TO_AGINO(args.mp, args.agbno, 0);
53830f712c9SDave Chinner 	be32_add_cpu(&agi->agi_count, newlen);
53930f712c9SDave Chinner 	be32_add_cpu(&agi->agi_freecount, newlen);
54030f712c9SDave Chinner 	pag = xfs_perag_get(args.mp, agno);
54130f712c9SDave Chinner 	pag->pagi_freecount += newlen;
54230f712c9SDave Chinner 	xfs_perag_put(pag);
54330f712c9SDave Chinner 	agi->agi_newino = cpu_to_be32(newino);
54430f712c9SDave Chinner 
54530f712c9SDave Chinner 	/*
54630f712c9SDave Chinner 	 * Insert records describing the new inode chunk into the btrees.
54730f712c9SDave Chinner 	 */
54830f712c9SDave Chinner 	error = xfs_inobt_insert(args.mp, tp, agbp, newino, newlen,
54930f712c9SDave Chinner 				 XFS_BTNUM_INO);
55030f712c9SDave Chinner 	if (error)
55130f712c9SDave Chinner 		return error;
55230f712c9SDave Chinner 
55330f712c9SDave Chinner 	if (xfs_sb_version_hasfinobt(&args.mp->m_sb)) {
55430f712c9SDave Chinner 		error = xfs_inobt_insert(args.mp, tp, agbp, newino, newlen,
55530f712c9SDave Chinner 					 XFS_BTNUM_FINO);
55630f712c9SDave Chinner 		if (error)
55730f712c9SDave Chinner 			return error;
55830f712c9SDave Chinner 	}
55930f712c9SDave Chinner 	/*
56030f712c9SDave Chinner 	 * Log allocation group header fields
56130f712c9SDave Chinner 	 */
56230f712c9SDave Chinner 	xfs_ialloc_log_agi(tp, agbp,
56330f712c9SDave Chinner 		XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO);
56430f712c9SDave Chinner 	/*
56530f712c9SDave Chinner 	 * Modify/log superblock values for inode count and inode free count.
56630f712c9SDave Chinner 	 */
56730f712c9SDave Chinner 	xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen);
56830f712c9SDave Chinner 	xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen);
56930f712c9SDave Chinner 	*alloc = 1;
57030f712c9SDave Chinner 	return 0;
57130f712c9SDave Chinner }
57230f712c9SDave Chinner 
57330f712c9SDave Chinner STATIC xfs_agnumber_t
57430f712c9SDave Chinner xfs_ialloc_next_ag(
57530f712c9SDave Chinner 	xfs_mount_t	*mp)
57630f712c9SDave Chinner {
57730f712c9SDave Chinner 	xfs_agnumber_t	agno;
57830f712c9SDave Chinner 
57930f712c9SDave Chinner 	spin_lock(&mp->m_agirotor_lock);
58030f712c9SDave Chinner 	agno = mp->m_agirotor;
58130f712c9SDave Chinner 	if (++mp->m_agirotor >= mp->m_maxagi)
58230f712c9SDave Chinner 		mp->m_agirotor = 0;
58330f712c9SDave Chinner 	spin_unlock(&mp->m_agirotor_lock);
58430f712c9SDave Chinner 
58530f712c9SDave Chinner 	return agno;
58630f712c9SDave Chinner }
58730f712c9SDave Chinner 
58830f712c9SDave Chinner /*
58930f712c9SDave Chinner  * Select an allocation group to look for a free inode in, based on the parent
59030f712c9SDave Chinner  * inode and the mode.  Return the allocation group buffer.
59130f712c9SDave Chinner  */
59230f712c9SDave Chinner STATIC xfs_agnumber_t
59330f712c9SDave Chinner xfs_ialloc_ag_select(
59430f712c9SDave Chinner 	xfs_trans_t	*tp,		/* transaction pointer */
59530f712c9SDave Chinner 	xfs_ino_t	parent,		/* parent directory inode number */
59630f712c9SDave Chinner 	umode_t		mode,		/* bits set to indicate file type */
59730f712c9SDave Chinner 	int		okalloc)	/* ok to allocate more space */
59830f712c9SDave Chinner {
59930f712c9SDave Chinner 	xfs_agnumber_t	agcount;	/* number of ag's in the filesystem */
60030f712c9SDave Chinner 	xfs_agnumber_t	agno;		/* current ag number */
60130f712c9SDave Chinner 	int		flags;		/* alloc buffer locking flags */
60230f712c9SDave Chinner 	xfs_extlen_t	ineed;		/* blocks needed for inode allocation */
60330f712c9SDave Chinner 	xfs_extlen_t	longest = 0;	/* longest extent available */
60430f712c9SDave Chinner 	xfs_mount_t	*mp;		/* mount point structure */
60530f712c9SDave Chinner 	int		needspace;	/* file mode implies space allocated */
60630f712c9SDave Chinner 	xfs_perag_t	*pag;		/* per allocation group data */
60730f712c9SDave Chinner 	xfs_agnumber_t	pagno;		/* parent (starting) ag number */
60830f712c9SDave Chinner 	int		error;
60930f712c9SDave Chinner 
61030f712c9SDave Chinner 	/*
61130f712c9SDave Chinner 	 * Files of these types need at least one block if length > 0
61230f712c9SDave Chinner 	 * (and they won't fit in the inode, but that's hard to figure out).
61330f712c9SDave Chinner 	 */
61430f712c9SDave Chinner 	needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode);
61530f712c9SDave Chinner 	mp = tp->t_mountp;
61630f712c9SDave Chinner 	agcount = mp->m_maxagi;
61730f712c9SDave Chinner 	if (S_ISDIR(mode))
61830f712c9SDave Chinner 		pagno = xfs_ialloc_next_ag(mp);
61930f712c9SDave Chinner 	else {
62030f712c9SDave Chinner 		pagno = XFS_INO_TO_AGNO(mp, parent);
62130f712c9SDave Chinner 		if (pagno >= agcount)
62230f712c9SDave Chinner 			pagno = 0;
62330f712c9SDave Chinner 	}
62430f712c9SDave Chinner 
62530f712c9SDave Chinner 	ASSERT(pagno < agcount);
62630f712c9SDave Chinner 
62730f712c9SDave Chinner 	/*
62830f712c9SDave Chinner 	 * Loop through allocation groups, looking for one with a little
62930f712c9SDave Chinner 	 * free space in it.  Note we don't look for free inodes, exactly.
63030f712c9SDave Chinner 	 * Instead, we include whether there is a need to allocate inodes
63130f712c9SDave Chinner 	 * to mean that blocks must be allocated for them,
63230f712c9SDave Chinner 	 * if none are currently free.
63330f712c9SDave Chinner 	 */
63430f712c9SDave Chinner 	agno = pagno;
63530f712c9SDave Chinner 	flags = XFS_ALLOC_FLAG_TRYLOCK;
63630f712c9SDave Chinner 	for (;;) {
63730f712c9SDave Chinner 		pag = xfs_perag_get(mp, agno);
63830f712c9SDave Chinner 		if (!pag->pagi_inodeok) {
63930f712c9SDave Chinner 			xfs_ialloc_next_ag(mp);
64030f712c9SDave Chinner 			goto nextag;
64130f712c9SDave Chinner 		}
64230f712c9SDave Chinner 
64330f712c9SDave Chinner 		if (!pag->pagi_init) {
64430f712c9SDave Chinner 			error = xfs_ialloc_pagi_init(mp, tp, agno);
64530f712c9SDave Chinner 			if (error)
64630f712c9SDave Chinner 				goto nextag;
64730f712c9SDave Chinner 		}
64830f712c9SDave Chinner 
64930f712c9SDave Chinner 		if (pag->pagi_freecount) {
65030f712c9SDave Chinner 			xfs_perag_put(pag);
65130f712c9SDave Chinner 			return agno;
65230f712c9SDave Chinner 		}
65330f712c9SDave Chinner 
65430f712c9SDave Chinner 		if (!okalloc)
65530f712c9SDave Chinner 			goto nextag;
65630f712c9SDave Chinner 
65730f712c9SDave Chinner 		if (!pag->pagf_init) {
65830f712c9SDave Chinner 			error = xfs_alloc_pagf_init(mp, tp, agno, flags);
65930f712c9SDave Chinner 			if (error)
66030f712c9SDave Chinner 				goto nextag;
66130f712c9SDave Chinner 		}
66230f712c9SDave Chinner 
66330f712c9SDave Chinner 		/*
6647a1df156SDave Chinner 		 * Check that there is enough free space for the file plus a
6657a1df156SDave Chinner 		 * chunk of inodes if we need to allocate some. If this is the
6667a1df156SDave Chinner 		 * first pass across the AGs, take into account the potential
6677a1df156SDave Chinner 		 * space needed for alignment of inode chunks when checking the
6687a1df156SDave Chinner 		 * longest contiguous free space in the AG - this prevents us
6697a1df156SDave Chinner 		 * from getting ENOSPC because we have free space larger than
6707a1df156SDave Chinner 		 * m_ialloc_blks but alignment constraints prevent us from using
6717a1df156SDave Chinner 		 * it.
6727a1df156SDave Chinner 		 *
6737a1df156SDave Chinner 		 * If we can't find an AG with space for full alignment slack to
6747a1df156SDave Chinner 		 * be taken into account, we must be near ENOSPC in all AGs.
6757a1df156SDave Chinner 		 * Hence we don't include alignment for the second pass and so
6767a1df156SDave Chinner 		 * if we fail allocation due to alignment issues then it is most
6777a1df156SDave Chinner 		 * likely a real ENOSPC condition.
67830f712c9SDave Chinner 		 */
679066a1884SBrian Foster 		ineed = mp->m_ialloc_min_blks;
6807a1df156SDave Chinner 		if (flags && ineed > 1)
6817a1df156SDave Chinner 			ineed += xfs_ialloc_cluster_alignment(mp);
68230f712c9SDave Chinner 		longest = pag->pagf_longest;
68330f712c9SDave Chinner 		if (!longest)
68430f712c9SDave Chinner 			longest = pag->pagf_flcount > 0;
68530f712c9SDave Chinner 
68630f712c9SDave Chinner 		if (pag->pagf_freeblks >= needspace + ineed &&
68730f712c9SDave Chinner 		    longest >= ineed) {
68830f712c9SDave Chinner 			xfs_perag_put(pag);
68930f712c9SDave Chinner 			return agno;
69030f712c9SDave Chinner 		}
69130f712c9SDave Chinner nextag:
69230f712c9SDave Chinner 		xfs_perag_put(pag);
69330f712c9SDave Chinner 		/*
69430f712c9SDave Chinner 		 * No point in iterating over the rest, if we're shutting
69530f712c9SDave Chinner 		 * down.
69630f712c9SDave Chinner 		 */
69730f712c9SDave Chinner 		if (XFS_FORCED_SHUTDOWN(mp))
69830f712c9SDave Chinner 			return NULLAGNUMBER;
69930f712c9SDave Chinner 		agno++;
70030f712c9SDave Chinner 		if (agno >= agcount)
70130f712c9SDave Chinner 			agno = 0;
70230f712c9SDave Chinner 		if (agno == pagno) {
70330f712c9SDave Chinner 			if (flags == 0)
70430f712c9SDave Chinner 				return NULLAGNUMBER;
70530f712c9SDave Chinner 			flags = 0;
70630f712c9SDave Chinner 		}
70730f712c9SDave Chinner 	}
70830f712c9SDave Chinner }
70930f712c9SDave Chinner 
71030f712c9SDave Chinner /*
71130f712c9SDave Chinner  * Try to retrieve the next record to the left/right from the current one.
71230f712c9SDave Chinner  */
71330f712c9SDave Chinner STATIC int
71430f712c9SDave Chinner xfs_ialloc_next_rec(
71530f712c9SDave Chinner 	struct xfs_btree_cur	*cur,
71630f712c9SDave Chinner 	xfs_inobt_rec_incore_t	*rec,
71730f712c9SDave Chinner 	int			*done,
71830f712c9SDave Chinner 	int			left)
71930f712c9SDave Chinner {
72030f712c9SDave Chinner 	int                     error;
72130f712c9SDave Chinner 	int			i;
72230f712c9SDave Chinner 
72330f712c9SDave Chinner 	if (left)
72430f712c9SDave Chinner 		error = xfs_btree_decrement(cur, 0, &i);
72530f712c9SDave Chinner 	else
72630f712c9SDave Chinner 		error = xfs_btree_increment(cur, 0, &i);
72730f712c9SDave Chinner 
72830f712c9SDave Chinner 	if (error)
72930f712c9SDave Chinner 		return error;
73030f712c9SDave Chinner 	*done = !i;
73130f712c9SDave Chinner 	if (i) {
73230f712c9SDave Chinner 		error = xfs_inobt_get_rec(cur, rec, &i);
73330f712c9SDave Chinner 		if (error)
73430f712c9SDave Chinner 			return error;
7355fb5aeeeSEric Sandeen 		XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
73630f712c9SDave Chinner 	}
73730f712c9SDave Chinner 
73830f712c9SDave Chinner 	return 0;
73930f712c9SDave Chinner }
74030f712c9SDave Chinner 
74130f712c9SDave Chinner STATIC int
74230f712c9SDave Chinner xfs_ialloc_get_rec(
74330f712c9SDave Chinner 	struct xfs_btree_cur	*cur,
74430f712c9SDave Chinner 	xfs_agino_t		agino,
74530f712c9SDave Chinner 	xfs_inobt_rec_incore_t	*rec,
74630f712c9SDave Chinner 	int			*done)
74730f712c9SDave Chinner {
74830f712c9SDave Chinner 	int                     error;
74930f712c9SDave Chinner 	int			i;
75030f712c9SDave Chinner 
75130f712c9SDave Chinner 	error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_EQ, &i);
75230f712c9SDave Chinner 	if (error)
75330f712c9SDave Chinner 		return error;
75430f712c9SDave Chinner 	*done = !i;
75530f712c9SDave Chinner 	if (i) {
75630f712c9SDave Chinner 		error = xfs_inobt_get_rec(cur, rec, &i);
75730f712c9SDave Chinner 		if (error)
75830f712c9SDave Chinner 			return error;
7595fb5aeeeSEric Sandeen 		XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
76030f712c9SDave Chinner 	}
76130f712c9SDave Chinner 
76230f712c9SDave Chinner 	return 0;
76330f712c9SDave Chinner }
76430f712c9SDave Chinner 
76530f712c9SDave Chinner /*
766d4cc540bSBrian Foster  * Return the offset of the first free inode in the record.
767d4cc540bSBrian Foster  */
768d4cc540bSBrian Foster STATIC int
769d4cc540bSBrian Foster xfs_inobt_first_free_inode(
770d4cc540bSBrian Foster 	struct xfs_inobt_rec_incore	*rec)
771d4cc540bSBrian Foster {
772d4cc540bSBrian Foster 	return xfs_lowbit64(rec->ir_free);
773d4cc540bSBrian Foster }
774d4cc540bSBrian Foster 
775d4cc540bSBrian Foster /*
77630f712c9SDave Chinner  * Allocate an inode using the inobt-only algorithm.
77730f712c9SDave Chinner  */
77830f712c9SDave Chinner STATIC int
77930f712c9SDave Chinner xfs_dialloc_ag_inobt(
78030f712c9SDave Chinner 	struct xfs_trans	*tp,
78130f712c9SDave Chinner 	struct xfs_buf		*agbp,
78230f712c9SDave Chinner 	xfs_ino_t		parent,
78330f712c9SDave Chinner 	xfs_ino_t		*inop)
78430f712c9SDave Chinner {
78530f712c9SDave Chinner 	struct xfs_mount	*mp = tp->t_mountp;
78630f712c9SDave Chinner 	struct xfs_agi		*agi = XFS_BUF_TO_AGI(agbp);
78730f712c9SDave Chinner 	xfs_agnumber_t		agno = be32_to_cpu(agi->agi_seqno);
78830f712c9SDave Chinner 	xfs_agnumber_t		pagno = XFS_INO_TO_AGNO(mp, parent);
78930f712c9SDave Chinner 	xfs_agino_t		pagino = XFS_INO_TO_AGINO(mp, parent);
79030f712c9SDave Chinner 	struct xfs_perag	*pag;
79130f712c9SDave Chinner 	struct xfs_btree_cur	*cur, *tcur;
79230f712c9SDave Chinner 	struct xfs_inobt_rec_incore rec, trec;
79330f712c9SDave Chinner 	xfs_ino_t		ino;
79430f712c9SDave Chinner 	int			error;
79530f712c9SDave Chinner 	int			offset;
79630f712c9SDave Chinner 	int			i, j;
79730f712c9SDave Chinner 
79830f712c9SDave Chinner 	pag = xfs_perag_get(mp, agno);
79930f712c9SDave Chinner 
80030f712c9SDave Chinner 	ASSERT(pag->pagi_init);
80130f712c9SDave Chinner 	ASSERT(pag->pagi_inodeok);
80230f712c9SDave Chinner 	ASSERT(pag->pagi_freecount > 0);
80330f712c9SDave Chinner 
80430f712c9SDave Chinner  restart_pagno:
80530f712c9SDave Chinner 	cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
80630f712c9SDave Chinner 	/*
80730f712c9SDave Chinner 	 * If pagino is 0 (this is the root inode allocation) use newino.
80830f712c9SDave Chinner 	 * This must work because we've just allocated some.
80930f712c9SDave Chinner 	 */
81030f712c9SDave Chinner 	if (!pagino)
81130f712c9SDave Chinner 		pagino = be32_to_cpu(agi->agi_newino);
81230f712c9SDave Chinner 
81330f712c9SDave Chinner 	error = xfs_check_agi_freecount(cur, agi);
81430f712c9SDave Chinner 	if (error)
81530f712c9SDave Chinner 		goto error0;
81630f712c9SDave Chinner 
81730f712c9SDave Chinner 	/*
81830f712c9SDave Chinner 	 * If in the same AG as the parent, try to get near the parent.
81930f712c9SDave Chinner 	 */
82030f712c9SDave Chinner 	if (pagno == agno) {
82130f712c9SDave Chinner 		int		doneleft;	/* done, to the left */
82230f712c9SDave Chinner 		int		doneright;	/* done, to the right */
82330f712c9SDave Chinner 		int		searchdistance = 10;
82430f712c9SDave Chinner 
82530f712c9SDave Chinner 		error = xfs_inobt_lookup(cur, pagino, XFS_LOOKUP_LE, &i);
82630f712c9SDave Chinner 		if (error)
82730f712c9SDave Chinner 			goto error0;
828c29aad41SEric Sandeen 		XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
82930f712c9SDave Chinner 
83030f712c9SDave Chinner 		error = xfs_inobt_get_rec(cur, &rec, &j);
83130f712c9SDave Chinner 		if (error)
83230f712c9SDave Chinner 			goto error0;
833c29aad41SEric Sandeen 		XFS_WANT_CORRUPTED_GOTO(mp, j == 1, error0);
83430f712c9SDave Chinner 
83530f712c9SDave Chinner 		if (rec.ir_freecount > 0) {
83630f712c9SDave Chinner 			/*
83730f712c9SDave Chinner 			 * Found a free inode in the same chunk
83830f712c9SDave Chinner 			 * as the parent, done.
83930f712c9SDave Chinner 			 */
84030f712c9SDave Chinner 			goto alloc_inode;
84130f712c9SDave Chinner 		}
84230f712c9SDave Chinner 
84330f712c9SDave Chinner 
84430f712c9SDave Chinner 		/*
84530f712c9SDave Chinner 		 * In the same AG as parent, but parent's chunk is full.
84630f712c9SDave Chinner 		 */
84730f712c9SDave Chinner 
84830f712c9SDave Chinner 		/* duplicate the cursor, search left & right simultaneously */
84930f712c9SDave Chinner 		error = xfs_btree_dup_cursor(cur, &tcur);
85030f712c9SDave Chinner 		if (error)
85130f712c9SDave Chinner 			goto error0;
85230f712c9SDave Chinner 
85330f712c9SDave Chinner 		/*
85430f712c9SDave Chinner 		 * Skip to last blocks looked up if same parent inode.
85530f712c9SDave Chinner 		 */
85630f712c9SDave Chinner 		if (pagino != NULLAGINO &&
85730f712c9SDave Chinner 		    pag->pagl_pagino == pagino &&
85830f712c9SDave Chinner 		    pag->pagl_leftrec != NULLAGINO &&
85930f712c9SDave Chinner 		    pag->pagl_rightrec != NULLAGINO) {
86030f712c9SDave Chinner 			error = xfs_ialloc_get_rec(tcur, pag->pagl_leftrec,
86130f712c9SDave Chinner 						   &trec, &doneleft);
86230f712c9SDave Chinner 			if (error)
86330f712c9SDave Chinner 				goto error1;
86430f712c9SDave Chinner 
86530f712c9SDave Chinner 			error = xfs_ialloc_get_rec(cur, pag->pagl_rightrec,
86630f712c9SDave Chinner 						   &rec, &doneright);
86730f712c9SDave Chinner 			if (error)
86830f712c9SDave Chinner 				goto error1;
86930f712c9SDave Chinner 		} else {
87030f712c9SDave Chinner 			/* search left with tcur, back up 1 record */
87130f712c9SDave Chinner 			error = xfs_ialloc_next_rec(tcur, &trec, &doneleft, 1);
87230f712c9SDave Chinner 			if (error)
87330f712c9SDave Chinner 				goto error1;
87430f712c9SDave Chinner 
87530f712c9SDave Chinner 			/* search right with cur, go forward 1 record. */
87630f712c9SDave Chinner 			error = xfs_ialloc_next_rec(cur, &rec, &doneright, 0);
87730f712c9SDave Chinner 			if (error)
87830f712c9SDave Chinner 				goto error1;
87930f712c9SDave Chinner 		}
88030f712c9SDave Chinner 
88130f712c9SDave Chinner 		/*
88230f712c9SDave Chinner 		 * Loop until we find an inode chunk with a free inode.
88330f712c9SDave Chinner 		 */
88430f712c9SDave Chinner 		while (!doneleft || !doneright) {
88530f712c9SDave Chinner 			int	useleft;  /* using left inode chunk this time */
88630f712c9SDave Chinner 
88730f712c9SDave Chinner 			if (!--searchdistance) {
88830f712c9SDave Chinner 				/*
88930f712c9SDave Chinner 				 * Not in range - save last search
89030f712c9SDave Chinner 				 * location and allocate a new inode
89130f712c9SDave Chinner 				 */
89230f712c9SDave Chinner 				xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
89330f712c9SDave Chinner 				pag->pagl_leftrec = trec.ir_startino;
89430f712c9SDave Chinner 				pag->pagl_rightrec = rec.ir_startino;
89530f712c9SDave Chinner 				pag->pagl_pagino = pagino;
89630f712c9SDave Chinner 				goto newino;
89730f712c9SDave Chinner 			}
89830f712c9SDave Chinner 
89930f712c9SDave Chinner 			/* figure out the closer block if both are valid. */
90030f712c9SDave Chinner 			if (!doneleft && !doneright) {
90130f712c9SDave Chinner 				useleft = pagino -
90230f712c9SDave Chinner 				 (trec.ir_startino + XFS_INODES_PER_CHUNK - 1) <
90330f712c9SDave Chinner 				  rec.ir_startino - pagino;
90430f712c9SDave Chinner 			} else {
90530f712c9SDave Chinner 				useleft = !doneleft;
90630f712c9SDave Chinner 			}
90730f712c9SDave Chinner 
90830f712c9SDave Chinner 			/* free inodes to the left? */
90930f712c9SDave Chinner 			if (useleft && trec.ir_freecount) {
91030f712c9SDave Chinner 				rec = trec;
91130f712c9SDave Chinner 				xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
91230f712c9SDave Chinner 				cur = tcur;
91330f712c9SDave Chinner 
91430f712c9SDave Chinner 				pag->pagl_leftrec = trec.ir_startino;
91530f712c9SDave Chinner 				pag->pagl_rightrec = rec.ir_startino;
91630f712c9SDave Chinner 				pag->pagl_pagino = pagino;
91730f712c9SDave Chinner 				goto alloc_inode;
91830f712c9SDave Chinner 			}
91930f712c9SDave Chinner 
92030f712c9SDave Chinner 			/* free inodes to the right? */
92130f712c9SDave Chinner 			if (!useleft && rec.ir_freecount) {
92230f712c9SDave Chinner 				xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
92330f712c9SDave Chinner 
92430f712c9SDave Chinner 				pag->pagl_leftrec = trec.ir_startino;
92530f712c9SDave Chinner 				pag->pagl_rightrec = rec.ir_startino;
92630f712c9SDave Chinner 				pag->pagl_pagino = pagino;
92730f712c9SDave Chinner 				goto alloc_inode;
92830f712c9SDave Chinner 			}
92930f712c9SDave Chinner 
93030f712c9SDave Chinner 			/* get next record to check */
93130f712c9SDave Chinner 			if (useleft) {
93230f712c9SDave Chinner 				error = xfs_ialloc_next_rec(tcur, &trec,
93330f712c9SDave Chinner 								 &doneleft, 1);
93430f712c9SDave Chinner 			} else {
93530f712c9SDave Chinner 				error = xfs_ialloc_next_rec(cur, &rec,
93630f712c9SDave Chinner 								 &doneright, 0);
93730f712c9SDave Chinner 			}
93830f712c9SDave Chinner 			if (error)
93930f712c9SDave Chinner 				goto error1;
94030f712c9SDave Chinner 		}
94130f712c9SDave Chinner 
94230f712c9SDave Chinner 		/*
94330f712c9SDave Chinner 		 * We've reached the end of the btree. because
94430f712c9SDave Chinner 		 * we are only searching a small chunk of the
94530f712c9SDave Chinner 		 * btree each search, there is obviously free
94630f712c9SDave Chinner 		 * inodes closer to the parent inode than we
94730f712c9SDave Chinner 		 * are now. restart the search again.
94830f712c9SDave Chinner 		 */
94930f712c9SDave Chinner 		pag->pagl_pagino = NULLAGINO;
95030f712c9SDave Chinner 		pag->pagl_leftrec = NULLAGINO;
95130f712c9SDave Chinner 		pag->pagl_rightrec = NULLAGINO;
95230f712c9SDave Chinner 		xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
95330f712c9SDave Chinner 		xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
95430f712c9SDave Chinner 		goto restart_pagno;
95530f712c9SDave Chinner 	}
95630f712c9SDave Chinner 
95730f712c9SDave Chinner 	/*
95830f712c9SDave Chinner 	 * In a different AG from the parent.
95930f712c9SDave Chinner 	 * See if the most recently allocated block has any free.
96030f712c9SDave Chinner 	 */
96130f712c9SDave Chinner newino:
96230f712c9SDave Chinner 	if (agi->agi_newino != cpu_to_be32(NULLAGINO)) {
96330f712c9SDave Chinner 		error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino),
96430f712c9SDave Chinner 					 XFS_LOOKUP_EQ, &i);
96530f712c9SDave Chinner 		if (error)
96630f712c9SDave Chinner 			goto error0;
96730f712c9SDave Chinner 
96830f712c9SDave Chinner 		if (i == 1) {
96930f712c9SDave Chinner 			error = xfs_inobt_get_rec(cur, &rec, &j);
97030f712c9SDave Chinner 			if (error)
97130f712c9SDave Chinner 				goto error0;
97230f712c9SDave Chinner 
97330f712c9SDave Chinner 			if (j == 1 && rec.ir_freecount > 0) {
97430f712c9SDave Chinner 				/*
97530f712c9SDave Chinner 				 * The last chunk allocated in the group
97630f712c9SDave Chinner 				 * still has a free inode.
97730f712c9SDave Chinner 				 */
97830f712c9SDave Chinner 				goto alloc_inode;
97930f712c9SDave Chinner 			}
98030f712c9SDave Chinner 		}
98130f712c9SDave Chinner 	}
98230f712c9SDave Chinner 
98330f712c9SDave Chinner 	/*
98430f712c9SDave Chinner 	 * None left in the last group, search the whole AG
98530f712c9SDave Chinner 	 */
98630f712c9SDave Chinner 	error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
98730f712c9SDave Chinner 	if (error)
98830f712c9SDave Chinner 		goto error0;
989c29aad41SEric Sandeen 	XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
99030f712c9SDave Chinner 
99130f712c9SDave Chinner 	for (;;) {
99230f712c9SDave Chinner 		error = xfs_inobt_get_rec(cur, &rec, &i);
99330f712c9SDave Chinner 		if (error)
99430f712c9SDave Chinner 			goto error0;
995c29aad41SEric Sandeen 		XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
99630f712c9SDave Chinner 		if (rec.ir_freecount > 0)
99730f712c9SDave Chinner 			break;
99830f712c9SDave Chinner 		error = xfs_btree_increment(cur, 0, &i);
99930f712c9SDave Chinner 		if (error)
100030f712c9SDave Chinner 			goto error0;
1001c29aad41SEric Sandeen 		XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
100230f712c9SDave Chinner 	}
100330f712c9SDave Chinner 
100430f712c9SDave Chinner alloc_inode:
1005d4cc540bSBrian Foster 	offset = xfs_inobt_first_free_inode(&rec);
100630f712c9SDave Chinner 	ASSERT(offset >= 0);
100730f712c9SDave Chinner 	ASSERT(offset < XFS_INODES_PER_CHUNK);
100830f712c9SDave Chinner 	ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
100930f712c9SDave Chinner 				   XFS_INODES_PER_CHUNK) == 0);
101030f712c9SDave Chinner 	ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
101130f712c9SDave Chinner 	rec.ir_free &= ~XFS_INOBT_MASK(offset);
101230f712c9SDave Chinner 	rec.ir_freecount--;
101330f712c9SDave Chinner 	error = xfs_inobt_update(cur, &rec);
101430f712c9SDave Chinner 	if (error)
101530f712c9SDave Chinner 		goto error0;
101630f712c9SDave Chinner 	be32_add_cpu(&agi->agi_freecount, -1);
101730f712c9SDave Chinner 	xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
101830f712c9SDave Chinner 	pag->pagi_freecount--;
101930f712c9SDave Chinner 
102030f712c9SDave Chinner 	error = xfs_check_agi_freecount(cur, agi);
102130f712c9SDave Chinner 	if (error)
102230f712c9SDave Chinner 		goto error0;
102330f712c9SDave Chinner 
102430f712c9SDave Chinner 	xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
102530f712c9SDave Chinner 	xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
102630f712c9SDave Chinner 	xfs_perag_put(pag);
102730f712c9SDave Chinner 	*inop = ino;
102830f712c9SDave Chinner 	return 0;
102930f712c9SDave Chinner error1:
103030f712c9SDave Chinner 	xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
103130f712c9SDave Chinner error0:
103230f712c9SDave Chinner 	xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
103330f712c9SDave Chinner 	xfs_perag_put(pag);
103430f712c9SDave Chinner 	return error;
103530f712c9SDave Chinner }
103630f712c9SDave Chinner 
103730f712c9SDave Chinner /*
103830f712c9SDave Chinner  * Use the free inode btree to allocate an inode based on distance from the
103930f712c9SDave Chinner  * parent. Note that the provided cursor may be deleted and replaced.
104030f712c9SDave Chinner  */
104130f712c9SDave Chinner STATIC int
104230f712c9SDave Chinner xfs_dialloc_ag_finobt_near(
104330f712c9SDave Chinner 	xfs_agino_t			pagino,
104430f712c9SDave Chinner 	struct xfs_btree_cur		**ocur,
104530f712c9SDave Chinner 	struct xfs_inobt_rec_incore	*rec)
104630f712c9SDave Chinner {
104730f712c9SDave Chinner 	struct xfs_btree_cur		*lcur = *ocur;	/* left search cursor */
104830f712c9SDave Chinner 	struct xfs_btree_cur		*rcur;	/* right search cursor */
104930f712c9SDave Chinner 	struct xfs_inobt_rec_incore	rrec;
105030f712c9SDave Chinner 	int				error;
105130f712c9SDave Chinner 	int				i, j;
105230f712c9SDave Chinner 
105330f712c9SDave Chinner 	error = xfs_inobt_lookup(lcur, pagino, XFS_LOOKUP_LE, &i);
105430f712c9SDave Chinner 	if (error)
105530f712c9SDave Chinner 		return error;
105630f712c9SDave Chinner 
105730f712c9SDave Chinner 	if (i == 1) {
105830f712c9SDave Chinner 		error = xfs_inobt_get_rec(lcur, rec, &i);
105930f712c9SDave Chinner 		if (error)
106030f712c9SDave Chinner 			return error;
10615fb5aeeeSEric Sandeen 		XFS_WANT_CORRUPTED_RETURN(lcur->bc_mp, i == 1);
106230f712c9SDave Chinner 
106330f712c9SDave Chinner 		/*
106430f712c9SDave Chinner 		 * See if we've landed in the parent inode record. The finobt
106530f712c9SDave Chinner 		 * only tracks chunks with at least one free inode, so record
106630f712c9SDave Chinner 		 * existence is enough.
106730f712c9SDave Chinner 		 */
106830f712c9SDave Chinner 		if (pagino >= rec->ir_startino &&
106930f712c9SDave Chinner 		    pagino < (rec->ir_startino + XFS_INODES_PER_CHUNK))
107030f712c9SDave Chinner 			return 0;
107130f712c9SDave Chinner 	}
107230f712c9SDave Chinner 
107330f712c9SDave Chinner 	error = xfs_btree_dup_cursor(lcur, &rcur);
107430f712c9SDave Chinner 	if (error)
107530f712c9SDave Chinner 		return error;
107630f712c9SDave Chinner 
107730f712c9SDave Chinner 	error = xfs_inobt_lookup(rcur, pagino, XFS_LOOKUP_GE, &j);
107830f712c9SDave Chinner 	if (error)
107930f712c9SDave Chinner 		goto error_rcur;
108030f712c9SDave Chinner 	if (j == 1) {
108130f712c9SDave Chinner 		error = xfs_inobt_get_rec(rcur, &rrec, &j);
108230f712c9SDave Chinner 		if (error)
108330f712c9SDave Chinner 			goto error_rcur;
1084c29aad41SEric Sandeen 		XFS_WANT_CORRUPTED_GOTO(lcur->bc_mp, j == 1, error_rcur);
108530f712c9SDave Chinner 	}
108630f712c9SDave Chinner 
1087c29aad41SEric Sandeen 	XFS_WANT_CORRUPTED_GOTO(lcur->bc_mp, i == 1 || j == 1, error_rcur);
108830f712c9SDave Chinner 	if (i == 1 && j == 1) {
108930f712c9SDave Chinner 		/*
109030f712c9SDave Chinner 		 * Both the left and right records are valid. Choose the closer
109130f712c9SDave Chinner 		 * inode chunk to the target.
109230f712c9SDave Chinner 		 */
109330f712c9SDave Chinner 		if ((pagino - rec->ir_startino + XFS_INODES_PER_CHUNK - 1) >
109430f712c9SDave Chinner 		    (rrec.ir_startino - pagino)) {
109530f712c9SDave Chinner 			*rec = rrec;
109630f712c9SDave Chinner 			xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR);
109730f712c9SDave Chinner 			*ocur = rcur;
109830f712c9SDave Chinner 		} else {
109930f712c9SDave Chinner 			xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR);
110030f712c9SDave Chinner 		}
110130f712c9SDave Chinner 	} else if (j == 1) {
110230f712c9SDave Chinner 		/* only the right record is valid */
110330f712c9SDave Chinner 		*rec = rrec;
110430f712c9SDave Chinner 		xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR);
110530f712c9SDave Chinner 		*ocur = rcur;
110630f712c9SDave Chinner 	} else if (i == 1) {
110730f712c9SDave Chinner 		/* only the left record is valid */
110830f712c9SDave Chinner 		xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR);
110930f712c9SDave Chinner 	}
111030f712c9SDave Chinner 
111130f712c9SDave Chinner 	return 0;
111230f712c9SDave Chinner 
111330f712c9SDave Chinner error_rcur:
111430f712c9SDave Chinner 	xfs_btree_del_cursor(rcur, XFS_BTREE_ERROR);
111530f712c9SDave Chinner 	return error;
111630f712c9SDave Chinner }
111730f712c9SDave Chinner 
111830f712c9SDave Chinner /*
111930f712c9SDave Chinner  * Use the free inode btree to find a free inode based on a newino hint. If
112030f712c9SDave Chinner  * the hint is NULL, find the first free inode in the AG.
112130f712c9SDave Chinner  */
112230f712c9SDave Chinner STATIC int
112330f712c9SDave Chinner xfs_dialloc_ag_finobt_newino(
112430f712c9SDave Chinner 	struct xfs_agi			*agi,
112530f712c9SDave Chinner 	struct xfs_btree_cur		*cur,
112630f712c9SDave Chinner 	struct xfs_inobt_rec_incore	*rec)
112730f712c9SDave Chinner {
112830f712c9SDave Chinner 	int error;
112930f712c9SDave Chinner 	int i;
113030f712c9SDave Chinner 
113130f712c9SDave Chinner 	if (agi->agi_newino != cpu_to_be32(NULLAGINO)) {
1132e68ed775SDave Chinner 		error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino),
1133e68ed775SDave Chinner 					 XFS_LOOKUP_EQ, &i);
113430f712c9SDave Chinner 		if (error)
113530f712c9SDave Chinner 			return error;
113630f712c9SDave Chinner 		if (i == 1) {
113730f712c9SDave Chinner 			error = xfs_inobt_get_rec(cur, rec, &i);
113830f712c9SDave Chinner 			if (error)
113930f712c9SDave Chinner 				return error;
11405fb5aeeeSEric Sandeen 			XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
114130f712c9SDave Chinner 			return 0;
114230f712c9SDave Chinner 		}
114330f712c9SDave Chinner 	}
114430f712c9SDave Chinner 
114530f712c9SDave Chinner 	/*
114630f712c9SDave Chinner 	 * Find the first inode available in the AG.
114730f712c9SDave Chinner 	 */
114830f712c9SDave Chinner 	error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
114930f712c9SDave Chinner 	if (error)
115030f712c9SDave Chinner 		return error;
11515fb5aeeeSEric Sandeen 	XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
115230f712c9SDave Chinner 
115330f712c9SDave Chinner 	error = xfs_inobt_get_rec(cur, rec, &i);
115430f712c9SDave Chinner 	if (error)
115530f712c9SDave Chinner 		return error;
11565fb5aeeeSEric Sandeen 	XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
115730f712c9SDave Chinner 
115830f712c9SDave Chinner 	return 0;
115930f712c9SDave Chinner }
116030f712c9SDave Chinner 
116130f712c9SDave Chinner /*
116230f712c9SDave Chinner  * Update the inobt based on a modification made to the finobt. Also ensure that
116330f712c9SDave Chinner  * the records from both trees are equivalent post-modification.
116430f712c9SDave Chinner  */
116530f712c9SDave Chinner STATIC int
116630f712c9SDave Chinner xfs_dialloc_ag_update_inobt(
116730f712c9SDave Chinner 	struct xfs_btree_cur		*cur,	/* inobt cursor */
116830f712c9SDave Chinner 	struct xfs_inobt_rec_incore	*frec,	/* finobt record */
116930f712c9SDave Chinner 	int				offset) /* inode offset */
117030f712c9SDave Chinner {
117130f712c9SDave Chinner 	struct xfs_inobt_rec_incore	rec;
117230f712c9SDave Chinner 	int				error;
117330f712c9SDave Chinner 	int				i;
117430f712c9SDave Chinner 
117530f712c9SDave Chinner 	error = xfs_inobt_lookup(cur, frec->ir_startino, XFS_LOOKUP_EQ, &i);
117630f712c9SDave Chinner 	if (error)
117730f712c9SDave Chinner 		return error;
11785fb5aeeeSEric Sandeen 	XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
117930f712c9SDave Chinner 
118030f712c9SDave Chinner 	error = xfs_inobt_get_rec(cur, &rec, &i);
118130f712c9SDave Chinner 	if (error)
118230f712c9SDave Chinner 		return error;
11835fb5aeeeSEric Sandeen 	XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
118430f712c9SDave Chinner 	ASSERT((XFS_AGINO_TO_OFFSET(cur->bc_mp, rec.ir_startino) %
118530f712c9SDave Chinner 				   XFS_INODES_PER_CHUNK) == 0);
118630f712c9SDave Chinner 
118730f712c9SDave Chinner 	rec.ir_free &= ~XFS_INOBT_MASK(offset);
118830f712c9SDave Chinner 	rec.ir_freecount--;
118930f712c9SDave Chinner 
11905fb5aeeeSEric Sandeen 	XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, (rec.ir_free == frec->ir_free) &&
119130f712c9SDave Chinner 				  (rec.ir_freecount == frec->ir_freecount));
119230f712c9SDave Chinner 
1193b72091f2Skbuild test robot 	return xfs_inobt_update(cur, &rec);
119430f712c9SDave Chinner }
119530f712c9SDave Chinner 
119630f712c9SDave Chinner /*
119730f712c9SDave Chinner  * Allocate an inode using the free inode btree, if available. Otherwise, fall
119830f712c9SDave Chinner  * back to the inobt search algorithm.
119930f712c9SDave Chinner  *
120030f712c9SDave Chinner  * The caller selected an AG for us, and made sure that free inodes are
120130f712c9SDave Chinner  * available.
120230f712c9SDave Chinner  */
120330f712c9SDave Chinner STATIC int
120430f712c9SDave Chinner xfs_dialloc_ag(
120530f712c9SDave Chinner 	struct xfs_trans	*tp,
120630f712c9SDave Chinner 	struct xfs_buf		*agbp,
120730f712c9SDave Chinner 	xfs_ino_t		parent,
120830f712c9SDave Chinner 	xfs_ino_t		*inop)
120930f712c9SDave Chinner {
121030f712c9SDave Chinner 	struct xfs_mount		*mp = tp->t_mountp;
121130f712c9SDave Chinner 	struct xfs_agi			*agi = XFS_BUF_TO_AGI(agbp);
121230f712c9SDave Chinner 	xfs_agnumber_t			agno = be32_to_cpu(agi->agi_seqno);
121330f712c9SDave Chinner 	xfs_agnumber_t			pagno = XFS_INO_TO_AGNO(mp, parent);
121430f712c9SDave Chinner 	xfs_agino_t			pagino = XFS_INO_TO_AGINO(mp, parent);
121530f712c9SDave Chinner 	struct xfs_perag		*pag;
121630f712c9SDave Chinner 	struct xfs_btree_cur		*cur;	/* finobt cursor */
121730f712c9SDave Chinner 	struct xfs_btree_cur		*icur;	/* inobt cursor */
121830f712c9SDave Chinner 	struct xfs_inobt_rec_incore	rec;
121930f712c9SDave Chinner 	xfs_ino_t			ino;
122030f712c9SDave Chinner 	int				error;
122130f712c9SDave Chinner 	int				offset;
122230f712c9SDave Chinner 	int				i;
122330f712c9SDave Chinner 
122430f712c9SDave Chinner 	if (!xfs_sb_version_hasfinobt(&mp->m_sb))
122530f712c9SDave Chinner 		return xfs_dialloc_ag_inobt(tp, agbp, parent, inop);
122630f712c9SDave Chinner 
122730f712c9SDave Chinner 	pag = xfs_perag_get(mp, agno);
122830f712c9SDave Chinner 
122930f712c9SDave Chinner 	/*
123030f712c9SDave Chinner 	 * If pagino is 0 (this is the root inode allocation) use newino.
123130f712c9SDave Chinner 	 * This must work because we've just allocated some.
123230f712c9SDave Chinner 	 */
123330f712c9SDave Chinner 	if (!pagino)
123430f712c9SDave Chinner 		pagino = be32_to_cpu(agi->agi_newino);
123530f712c9SDave Chinner 
123630f712c9SDave Chinner 	cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_FINO);
123730f712c9SDave Chinner 
123830f712c9SDave Chinner 	error = xfs_check_agi_freecount(cur, agi);
123930f712c9SDave Chinner 	if (error)
124030f712c9SDave Chinner 		goto error_cur;
124130f712c9SDave Chinner 
124230f712c9SDave Chinner 	/*
124330f712c9SDave Chinner 	 * The search algorithm depends on whether we're in the same AG as the
124430f712c9SDave Chinner 	 * parent. If so, find the closest available inode to the parent. If
124530f712c9SDave Chinner 	 * not, consider the agi hint or find the first free inode in the AG.
124630f712c9SDave Chinner 	 */
124730f712c9SDave Chinner 	if (agno == pagno)
124830f712c9SDave Chinner 		error = xfs_dialloc_ag_finobt_near(pagino, &cur, &rec);
124930f712c9SDave Chinner 	else
125030f712c9SDave Chinner 		error = xfs_dialloc_ag_finobt_newino(agi, cur, &rec);
125130f712c9SDave Chinner 	if (error)
125230f712c9SDave Chinner 		goto error_cur;
125330f712c9SDave Chinner 
1254d4cc540bSBrian Foster 	offset = xfs_inobt_first_free_inode(&rec);
125530f712c9SDave Chinner 	ASSERT(offset >= 0);
125630f712c9SDave Chinner 	ASSERT(offset < XFS_INODES_PER_CHUNK);
125730f712c9SDave Chinner 	ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
125830f712c9SDave Chinner 				   XFS_INODES_PER_CHUNK) == 0);
125930f712c9SDave Chinner 	ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
126030f712c9SDave Chinner 
126130f712c9SDave Chinner 	/*
126230f712c9SDave Chinner 	 * Modify or remove the finobt record.
126330f712c9SDave Chinner 	 */
126430f712c9SDave Chinner 	rec.ir_free &= ~XFS_INOBT_MASK(offset);
126530f712c9SDave Chinner 	rec.ir_freecount--;
126630f712c9SDave Chinner 	if (rec.ir_freecount)
126730f712c9SDave Chinner 		error = xfs_inobt_update(cur, &rec);
126830f712c9SDave Chinner 	else
126930f712c9SDave Chinner 		error = xfs_btree_delete(cur, &i);
127030f712c9SDave Chinner 	if (error)
127130f712c9SDave Chinner 		goto error_cur;
127230f712c9SDave Chinner 
127330f712c9SDave Chinner 	/*
127430f712c9SDave Chinner 	 * The finobt has now been updated appropriately. We haven't updated the
127530f712c9SDave Chinner 	 * agi and superblock yet, so we can create an inobt cursor and validate
127630f712c9SDave Chinner 	 * the original freecount. If all is well, make the equivalent update to
127730f712c9SDave Chinner 	 * the inobt using the finobt record and offset information.
127830f712c9SDave Chinner 	 */
127930f712c9SDave Chinner 	icur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
128030f712c9SDave Chinner 
128130f712c9SDave Chinner 	error = xfs_check_agi_freecount(icur, agi);
128230f712c9SDave Chinner 	if (error)
128330f712c9SDave Chinner 		goto error_icur;
128430f712c9SDave Chinner 
128530f712c9SDave Chinner 	error = xfs_dialloc_ag_update_inobt(icur, &rec, offset);
128630f712c9SDave Chinner 	if (error)
128730f712c9SDave Chinner 		goto error_icur;
128830f712c9SDave Chinner 
128930f712c9SDave Chinner 	/*
129030f712c9SDave Chinner 	 * Both trees have now been updated. We must update the perag and
129130f712c9SDave Chinner 	 * superblock before we can check the freecount for each btree.
129230f712c9SDave Chinner 	 */
129330f712c9SDave Chinner 	be32_add_cpu(&agi->agi_freecount, -1);
129430f712c9SDave Chinner 	xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
129530f712c9SDave Chinner 	pag->pagi_freecount--;
129630f712c9SDave Chinner 
129730f712c9SDave Chinner 	xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
129830f712c9SDave Chinner 
129930f712c9SDave Chinner 	error = xfs_check_agi_freecount(icur, agi);
130030f712c9SDave Chinner 	if (error)
130130f712c9SDave Chinner 		goto error_icur;
130230f712c9SDave Chinner 	error = xfs_check_agi_freecount(cur, agi);
130330f712c9SDave Chinner 	if (error)
130430f712c9SDave Chinner 		goto error_icur;
130530f712c9SDave Chinner 
130630f712c9SDave Chinner 	xfs_btree_del_cursor(icur, XFS_BTREE_NOERROR);
130730f712c9SDave Chinner 	xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
130830f712c9SDave Chinner 	xfs_perag_put(pag);
130930f712c9SDave Chinner 	*inop = ino;
131030f712c9SDave Chinner 	return 0;
131130f712c9SDave Chinner 
131230f712c9SDave Chinner error_icur:
131330f712c9SDave Chinner 	xfs_btree_del_cursor(icur, XFS_BTREE_ERROR);
131430f712c9SDave Chinner error_cur:
131530f712c9SDave Chinner 	xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
131630f712c9SDave Chinner 	xfs_perag_put(pag);
131730f712c9SDave Chinner 	return error;
131830f712c9SDave Chinner }
131930f712c9SDave Chinner 
132030f712c9SDave Chinner /*
132130f712c9SDave Chinner  * Allocate an inode on disk.
132230f712c9SDave Chinner  *
132330f712c9SDave Chinner  * Mode is used to tell whether the new inode will need space, and whether it
132430f712c9SDave Chinner  * is a directory.
132530f712c9SDave Chinner  *
132630f712c9SDave Chinner  * This function is designed to be called twice if it has to do an allocation
132730f712c9SDave Chinner  * to make more free inodes.  On the first call, *IO_agbp should be set to NULL.
132830f712c9SDave Chinner  * If an inode is available without having to performn an allocation, an inode
132930f712c9SDave Chinner  * number is returned.  In this case, *IO_agbp is set to NULL.  If an allocation
133030f712c9SDave Chinner  * needs to be done, xfs_dialloc returns the current AGI buffer in *IO_agbp.
133130f712c9SDave Chinner  * The caller should then commit the current transaction, allocate a
133230f712c9SDave Chinner  * new transaction, and call xfs_dialloc() again, passing in the previous value
133330f712c9SDave Chinner  * of *IO_agbp.  IO_agbp should be held across the transactions. Since the AGI
133430f712c9SDave Chinner  * buffer is locked across the two calls, the second call is guaranteed to have
133530f712c9SDave Chinner  * a free inode available.
133630f712c9SDave Chinner  *
133730f712c9SDave Chinner  * Once we successfully pick an inode its number is returned and the on-disk
133830f712c9SDave Chinner  * data structures are updated.  The inode itself is not read in, since doing so
133930f712c9SDave Chinner  * would break ordering constraints with xfs_reclaim.
134030f712c9SDave Chinner  */
134130f712c9SDave Chinner int
134230f712c9SDave Chinner xfs_dialloc(
134330f712c9SDave Chinner 	struct xfs_trans	*tp,
134430f712c9SDave Chinner 	xfs_ino_t		parent,
134530f712c9SDave Chinner 	umode_t			mode,
134630f712c9SDave Chinner 	int			okalloc,
134730f712c9SDave Chinner 	struct xfs_buf		**IO_agbp,
134830f712c9SDave Chinner 	xfs_ino_t		*inop)
134930f712c9SDave Chinner {
135030f712c9SDave Chinner 	struct xfs_mount	*mp = tp->t_mountp;
135130f712c9SDave Chinner 	struct xfs_buf		*agbp;
135230f712c9SDave Chinner 	xfs_agnumber_t		agno;
135330f712c9SDave Chinner 	int			error;
135430f712c9SDave Chinner 	int			ialloced;
135530f712c9SDave Chinner 	int			noroom = 0;
135630f712c9SDave Chinner 	xfs_agnumber_t		start_agno;
135730f712c9SDave Chinner 	struct xfs_perag	*pag;
135830f712c9SDave Chinner 
135930f712c9SDave Chinner 	if (*IO_agbp) {
136030f712c9SDave Chinner 		/*
136130f712c9SDave Chinner 		 * If the caller passes in a pointer to the AGI buffer,
136230f712c9SDave Chinner 		 * continue where we left off before.  In this case, we
136330f712c9SDave Chinner 		 * know that the allocation group has free inodes.
136430f712c9SDave Chinner 		 */
136530f712c9SDave Chinner 		agbp = *IO_agbp;
136630f712c9SDave Chinner 		goto out_alloc;
136730f712c9SDave Chinner 	}
136830f712c9SDave Chinner 
136930f712c9SDave Chinner 	/*
137030f712c9SDave Chinner 	 * We do not have an agbp, so select an initial allocation
137130f712c9SDave Chinner 	 * group for inode allocation.
137230f712c9SDave Chinner 	 */
137330f712c9SDave Chinner 	start_agno = xfs_ialloc_ag_select(tp, parent, mode, okalloc);
137430f712c9SDave Chinner 	if (start_agno == NULLAGNUMBER) {
137530f712c9SDave Chinner 		*inop = NULLFSINO;
137630f712c9SDave Chinner 		return 0;
137730f712c9SDave Chinner 	}
137830f712c9SDave Chinner 
137930f712c9SDave Chinner 	/*
138030f712c9SDave Chinner 	 * If we have already hit the ceiling of inode blocks then clear
138130f712c9SDave Chinner 	 * okalloc so we scan all available agi structures for a free
138230f712c9SDave Chinner 	 * inode.
138330f712c9SDave Chinner 	 */
138430f712c9SDave Chinner 	if (mp->m_maxicount &&
1385501ab323SDave Chinner 	    percpu_counter_read(&mp->m_icount) + mp->m_ialloc_inos >
1386501ab323SDave Chinner 							mp->m_maxicount) {
138730f712c9SDave Chinner 		noroom = 1;
138830f712c9SDave Chinner 		okalloc = 0;
138930f712c9SDave Chinner 	}
139030f712c9SDave Chinner 
139130f712c9SDave Chinner 	/*
139230f712c9SDave Chinner 	 * Loop until we find an allocation group that either has free inodes
139330f712c9SDave Chinner 	 * or in which we can allocate some inodes.  Iterate through the
139430f712c9SDave Chinner 	 * allocation groups upward, wrapping at the end.
139530f712c9SDave Chinner 	 */
139630f712c9SDave Chinner 	agno = start_agno;
139730f712c9SDave Chinner 	for (;;) {
139830f712c9SDave Chinner 		pag = xfs_perag_get(mp, agno);
139930f712c9SDave Chinner 		if (!pag->pagi_inodeok) {
140030f712c9SDave Chinner 			xfs_ialloc_next_ag(mp);
140130f712c9SDave Chinner 			goto nextag;
140230f712c9SDave Chinner 		}
140330f712c9SDave Chinner 
140430f712c9SDave Chinner 		if (!pag->pagi_init) {
140530f712c9SDave Chinner 			error = xfs_ialloc_pagi_init(mp, tp, agno);
140630f712c9SDave Chinner 			if (error)
140730f712c9SDave Chinner 				goto out_error;
140830f712c9SDave Chinner 		}
140930f712c9SDave Chinner 
141030f712c9SDave Chinner 		/*
141130f712c9SDave Chinner 		 * Do a first racy fast path check if this AG is usable.
141230f712c9SDave Chinner 		 */
141330f712c9SDave Chinner 		if (!pag->pagi_freecount && !okalloc)
141430f712c9SDave Chinner 			goto nextag;
141530f712c9SDave Chinner 
141630f712c9SDave Chinner 		/*
141730f712c9SDave Chinner 		 * Then read in the AGI buffer and recheck with the AGI buffer
141830f712c9SDave Chinner 		 * lock held.
141930f712c9SDave Chinner 		 */
142030f712c9SDave Chinner 		error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
142130f712c9SDave Chinner 		if (error)
142230f712c9SDave Chinner 			goto out_error;
142330f712c9SDave Chinner 
142430f712c9SDave Chinner 		if (pag->pagi_freecount) {
142530f712c9SDave Chinner 			xfs_perag_put(pag);
142630f712c9SDave Chinner 			goto out_alloc;
142730f712c9SDave Chinner 		}
142830f712c9SDave Chinner 
142930f712c9SDave Chinner 		if (!okalloc)
143030f712c9SDave Chinner 			goto nextag_relse_buffer;
143130f712c9SDave Chinner 
143230f712c9SDave Chinner 
143330f712c9SDave Chinner 		error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced);
143430f712c9SDave Chinner 		if (error) {
143530f712c9SDave Chinner 			xfs_trans_brelse(tp, agbp);
143630f712c9SDave Chinner 
14372451337dSDave Chinner 			if (error != -ENOSPC)
143830f712c9SDave Chinner 				goto out_error;
143930f712c9SDave Chinner 
144030f712c9SDave Chinner 			xfs_perag_put(pag);
144130f712c9SDave Chinner 			*inop = NULLFSINO;
144230f712c9SDave Chinner 			return 0;
144330f712c9SDave Chinner 		}
144430f712c9SDave Chinner 
144530f712c9SDave Chinner 		if (ialloced) {
144630f712c9SDave Chinner 			/*
144730f712c9SDave Chinner 			 * We successfully allocated some inodes, return
144830f712c9SDave Chinner 			 * the current context to the caller so that it
144930f712c9SDave Chinner 			 * can commit the current transaction and call
145030f712c9SDave Chinner 			 * us again where we left off.
145130f712c9SDave Chinner 			 */
145230f712c9SDave Chinner 			ASSERT(pag->pagi_freecount > 0);
145330f712c9SDave Chinner 			xfs_perag_put(pag);
145430f712c9SDave Chinner 
145530f712c9SDave Chinner 			*IO_agbp = agbp;
145630f712c9SDave Chinner 			*inop = NULLFSINO;
145730f712c9SDave Chinner 			return 0;
145830f712c9SDave Chinner 		}
145930f712c9SDave Chinner 
146030f712c9SDave Chinner nextag_relse_buffer:
146130f712c9SDave Chinner 		xfs_trans_brelse(tp, agbp);
146230f712c9SDave Chinner nextag:
146330f712c9SDave Chinner 		xfs_perag_put(pag);
146430f712c9SDave Chinner 		if (++agno == mp->m_sb.sb_agcount)
146530f712c9SDave Chinner 			agno = 0;
146630f712c9SDave Chinner 		if (agno == start_agno) {
146730f712c9SDave Chinner 			*inop = NULLFSINO;
14682451337dSDave Chinner 			return noroom ? -ENOSPC : 0;
146930f712c9SDave Chinner 		}
147030f712c9SDave Chinner 	}
147130f712c9SDave Chinner 
147230f712c9SDave Chinner out_alloc:
147330f712c9SDave Chinner 	*IO_agbp = NULL;
147430f712c9SDave Chinner 	return xfs_dialloc_ag(tp, agbp, parent, inop);
147530f712c9SDave Chinner out_error:
147630f712c9SDave Chinner 	xfs_perag_put(pag);
147730f712c9SDave Chinner 	return error;
147830f712c9SDave Chinner }
147930f712c9SDave Chinner 
148030f712c9SDave Chinner STATIC int
148130f712c9SDave Chinner xfs_difree_inobt(
148230f712c9SDave Chinner 	struct xfs_mount		*mp,
148330f712c9SDave Chinner 	struct xfs_trans		*tp,
148430f712c9SDave Chinner 	struct xfs_buf			*agbp,
148530f712c9SDave Chinner 	xfs_agino_t			agino,
148630f712c9SDave Chinner 	struct xfs_bmap_free		*flist,
148730f712c9SDave Chinner 	int				*deleted,
148830f712c9SDave Chinner 	xfs_ino_t			*first_ino,
148930f712c9SDave Chinner 	struct xfs_inobt_rec_incore	*orec)
149030f712c9SDave Chinner {
149130f712c9SDave Chinner 	struct xfs_agi			*agi = XFS_BUF_TO_AGI(agbp);
149230f712c9SDave Chinner 	xfs_agnumber_t			agno = be32_to_cpu(agi->agi_seqno);
149330f712c9SDave Chinner 	struct xfs_perag		*pag;
149430f712c9SDave Chinner 	struct xfs_btree_cur		*cur;
149530f712c9SDave Chinner 	struct xfs_inobt_rec_incore	rec;
149630f712c9SDave Chinner 	int				ilen;
149730f712c9SDave Chinner 	int				error;
149830f712c9SDave Chinner 	int				i;
149930f712c9SDave Chinner 	int				off;
150030f712c9SDave Chinner 
150130f712c9SDave Chinner 	ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC));
150230f712c9SDave Chinner 	ASSERT(XFS_AGINO_TO_AGBNO(mp, agino) < be32_to_cpu(agi->agi_length));
150330f712c9SDave Chinner 
150430f712c9SDave Chinner 	/*
150530f712c9SDave Chinner 	 * Initialize the cursor.
150630f712c9SDave Chinner 	 */
150730f712c9SDave Chinner 	cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
150830f712c9SDave Chinner 
150930f712c9SDave Chinner 	error = xfs_check_agi_freecount(cur, agi);
151030f712c9SDave Chinner 	if (error)
151130f712c9SDave Chinner 		goto error0;
151230f712c9SDave Chinner 
151330f712c9SDave Chinner 	/*
151430f712c9SDave Chinner 	 * Look for the entry describing this inode.
151530f712c9SDave Chinner 	 */
151630f712c9SDave Chinner 	if ((error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i))) {
151730f712c9SDave Chinner 		xfs_warn(mp, "%s: xfs_inobt_lookup() returned error %d.",
151830f712c9SDave Chinner 			__func__, error);
151930f712c9SDave Chinner 		goto error0;
152030f712c9SDave Chinner 	}
1521c29aad41SEric Sandeen 	XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
152230f712c9SDave Chinner 	error = xfs_inobt_get_rec(cur, &rec, &i);
152330f712c9SDave Chinner 	if (error) {
152430f712c9SDave Chinner 		xfs_warn(mp, "%s: xfs_inobt_get_rec() returned error %d.",
152530f712c9SDave Chinner 			__func__, error);
152630f712c9SDave Chinner 		goto error0;
152730f712c9SDave Chinner 	}
1528c29aad41SEric Sandeen 	XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
152930f712c9SDave Chinner 	/*
153030f712c9SDave Chinner 	 * Get the offset in the inode chunk.
153130f712c9SDave Chinner 	 */
153230f712c9SDave Chinner 	off = agino - rec.ir_startino;
153330f712c9SDave Chinner 	ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK);
153430f712c9SDave Chinner 	ASSERT(!(rec.ir_free & XFS_INOBT_MASK(off)));
153530f712c9SDave Chinner 	/*
153630f712c9SDave Chinner 	 * Mark the inode free & increment the count.
153730f712c9SDave Chinner 	 */
153830f712c9SDave Chinner 	rec.ir_free |= XFS_INOBT_MASK(off);
153930f712c9SDave Chinner 	rec.ir_freecount++;
154030f712c9SDave Chinner 
154130f712c9SDave Chinner 	/*
1542999633d3SBrian Foster 	 * When an inode chunk is free, it becomes eligible for removal. Don't
1543999633d3SBrian Foster 	 * remove the chunk if the block size is large enough for multiple inode
1544999633d3SBrian Foster 	 * chunks (that might not be free).
154530f712c9SDave Chinner 	 */
154630f712c9SDave Chinner 	if (!(mp->m_flags & XFS_MOUNT_IKEEP) &&
1547999633d3SBrian Foster 	    rec.ir_free == XFS_INOBT_ALL_FREE &&
1548999633d3SBrian Foster 	    mp->m_sb.sb_inopblock <= XFS_INODES_PER_CHUNK) {
154930f712c9SDave Chinner 
155030f712c9SDave Chinner 		*deleted = 1;
155130f712c9SDave Chinner 		*first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino);
155230f712c9SDave Chinner 
155330f712c9SDave Chinner 		/*
155430f712c9SDave Chinner 		 * Remove the inode cluster from the AGI B+Tree, adjust the
155530f712c9SDave Chinner 		 * AGI and Superblock inode counts, and mark the disk space
155630f712c9SDave Chinner 		 * to be freed when the transaction is committed.
155730f712c9SDave Chinner 		 */
1558999633d3SBrian Foster 		ilen = rec.ir_freecount;
155930f712c9SDave Chinner 		be32_add_cpu(&agi->agi_count, -ilen);
156030f712c9SDave Chinner 		be32_add_cpu(&agi->agi_freecount, -(ilen - 1));
156130f712c9SDave Chinner 		xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT);
156230f712c9SDave Chinner 		pag = xfs_perag_get(mp, agno);
156330f712c9SDave Chinner 		pag->pagi_freecount -= ilen - 1;
156430f712c9SDave Chinner 		xfs_perag_put(pag);
156530f712c9SDave Chinner 		xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen);
156630f712c9SDave Chinner 		xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1));
156730f712c9SDave Chinner 
156830f712c9SDave Chinner 		if ((error = xfs_btree_delete(cur, &i))) {
156930f712c9SDave Chinner 			xfs_warn(mp, "%s: xfs_btree_delete returned error %d.",
157030f712c9SDave Chinner 				__func__, error);
157130f712c9SDave Chinner 			goto error0;
157230f712c9SDave Chinner 		}
157330f712c9SDave Chinner 
157430f712c9SDave Chinner 		xfs_bmap_add_free(XFS_AGB_TO_FSB(mp, agno,
157530f712c9SDave Chinner 				  XFS_AGINO_TO_AGBNO(mp, rec.ir_startino)),
157630f712c9SDave Chinner 				  mp->m_ialloc_blks, flist, mp);
157730f712c9SDave Chinner 	} else {
157830f712c9SDave Chinner 		*deleted = 0;
157930f712c9SDave Chinner 
158030f712c9SDave Chinner 		error = xfs_inobt_update(cur, &rec);
158130f712c9SDave Chinner 		if (error) {
158230f712c9SDave Chinner 			xfs_warn(mp, "%s: xfs_inobt_update returned error %d.",
158330f712c9SDave Chinner 				__func__, error);
158430f712c9SDave Chinner 			goto error0;
158530f712c9SDave Chinner 		}
158630f712c9SDave Chinner 
158730f712c9SDave Chinner 		/*
158830f712c9SDave Chinner 		 * Change the inode free counts and log the ag/sb changes.
158930f712c9SDave Chinner 		 */
159030f712c9SDave Chinner 		be32_add_cpu(&agi->agi_freecount, 1);
159130f712c9SDave Chinner 		xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
159230f712c9SDave Chinner 		pag = xfs_perag_get(mp, agno);
159330f712c9SDave Chinner 		pag->pagi_freecount++;
159430f712c9SDave Chinner 		xfs_perag_put(pag);
159530f712c9SDave Chinner 		xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1);
159630f712c9SDave Chinner 	}
159730f712c9SDave Chinner 
159830f712c9SDave Chinner 	error = xfs_check_agi_freecount(cur, agi);
159930f712c9SDave Chinner 	if (error)
160030f712c9SDave Chinner 		goto error0;
160130f712c9SDave Chinner 
160230f712c9SDave Chinner 	*orec = rec;
160330f712c9SDave Chinner 	xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
160430f712c9SDave Chinner 	return 0;
160530f712c9SDave Chinner 
160630f712c9SDave Chinner error0:
160730f712c9SDave Chinner 	xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
160830f712c9SDave Chinner 	return error;
160930f712c9SDave Chinner }
161030f712c9SDave Chinner 
161130f712c9SDave Chinner /*
161230f712c9SDave Chinner  * Free an inode in the free inode btree.
161330f712c9SDave Chinner  */
161430f712c9SDave Chinner STATIC int
161530f712c9SDave Chinner xfs_difree_finobt(
161630f712c9SDave Chinner 	struct xfs_mount		*mp,
161730f712c9SDave Chinner 	struct xfs_trans		*tp,
161830f712c9SDave Chinner 	struct xfs_buf			*agbp,
161930f712c9SDave Chinner 	xfs_agino_t			agino,
162030f712c9SDave Chinner 	struct xfs_inobt_rec_incore	*ibtrec) /* inobt record */
162130f712c9SDave Chinner {
162230f712c9SDave Chinner 	struct xfs_agi			*agi = XFS_BUF_TO_AGI(agbp);
162330f712c9SDave Chinner 	xfs_agnumber_t			agno = be32_to_cpu(agi->agi_seqno);
162430f712c9SDave Chinner 	struct xfs_btree_cur		*cur;
162530f712c9SDave Chinner 	struct xfs_inobt_rec_incore	rec;
162630f712c9SDave Chinner 	int				offset = agino - ibtrec->ir_startino;
162730f712c9SDave Chinner 	int				error;
162830f712c9SDave Chinner 	int				i;
162930f712c9SDave Chinner 
163030f712c9SDave Chinner 	cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_FINO);
163130f712c9SDave Chinner 
163230f712c9SDave Chinner 	error = xfs_inobt_lookup(cur, ibtrec->ir_startino, XFS_LOOKUP_EQ, &i);
163330f712c9SDave Chinner 	if (error)
163430f712c9SDave Chinner 		goto error;
163530f712c9SDave Chinner 	if (i == 0) {
163630f712c9SDave Chinner 		/*
163730f712c9SDave Chinner 		 * If the record does not exist in the finobt, we must have just
163830f712c9SDave Chinner 		 * freed an inode in a previously fully allocated chunk. If not,
163930f712c9SDave Chinner 		 * something is out of sync.
164030f712c9SDave Chinner 		 */
1641c29aad41SEric Sandeen 		XFS_WANT_CORRUPTED_GOTO(mp, ibtrec->ir_freecount == 1, error);
164230f712c9SDave Chinner 
16435419040fSBrian Foster 		error = xfs_inobt_insert_rec(cur, ibtrec->ir_holemask,
16445419040fSBrian Foster 					     ibtrec->ir_count,
16455419040fSBrian Foster 					     ibtrec->ir_freecount,
164630f712c9SDave Chinner 					     ibtrec->ir_free, &i);
164730f712c9SDave Chinner 		if (error)
164830f712c9SDave Chinner 			goto error;
164930f712c9SDave Chinner 		ASSERT(i == 1);
165030f712c9SDave Chinner 
165130f712c9SDave Chinner 		goto out;
165230f712c9SDave Chinner 	}
165330f712c9SDave Chinner 
165430f712c9SDave Chinner 	/*
165530f712c9SDave Chinner 	 * Read and update the existing record. We could just copy the ibtrec
165630f712c9SDave Chinner 	 * across here, but that would defeat the purpose of having redundant
165730f712c9SDave Chinner 	 * metadata. By making the modifications independently, we can catch
165830f712c9SDave Chinner 	 * corruptions that we wouldn't see if we just copied from one record
165930f712c9SDave Chinner 	 * to another.
166030f712c9SDave Chinner 	 */
166130f712c9SDave Chinner 	error = xfs_inobt_get_rec(cur, &rec, &i);
166230f712c9SDave Chinner 	if (error)
166330f712c9SDave Chinner 		goto error;
1664c29aad41SEric Sandeen 	XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error);
166530f712c9SDave Chinner 
166630f712c9SDave Chinner 	rec.ir_free |= XFS_INOBT_MASK(offset);
166730f712c9SDave Chinner 	rec.ir_freecount++;
166830f712c9SDave Chinner 
1669c29aad41SEric Sandeen 	XFS_WANT_CORRUPTED_GOTO(mp, (rec.ir_free == ibtrec->ir_free) &&
167030f712c9SDave Chinner 				(rec.ir_freecount == ibtrec->ir_freecount),
167130f712c9SDave Chinner 				error);
167230f712c9SDave Chinner 
167330f712c9SDave Chinner 	/*
167430f712c9SDave Chinner 	 * The content of inobt records should always match between the inobt
167530f712c9SDave Chinner 	 * and finobt. The lifecycle of records in the finobt is different from
167630f712c9SDave Chinner 	 * the inobt in that the finobt only tracks records with at least one
167730f712c9SDave Chinner 	 * free inode. Hence, if all of the inodes are free and we aren't
167830f712c9SDave Chinner 	 * keeping inode chunks permanently on disk, remove the record.
167930f712c9SDave Chinner 	 * Otherwise, update the record with the new information.
1680999633d3SBrian Foster 	 *
1681999633d3SBrian Foster 	 * Note that we currently can't free chunks when the block size is large
1682999633d3SBrian Foster 	 * enough for multiple chunks. Leave the finobt record to remain in sync
1683999633d3SBrian Foster 	 * with the inobt.
168430f712c9SDave Chinner 	 */
1685999633d3SBrian Foster 	if (rec.ir_free == XFS_INOBT_ALL_FREE &&
1686999633d3SBrian Foster 	    mp->m_sb.sb_inopblock <= XFS_INODES_PER_CHUNK &&
168730f712c9SDave Chinner 	    !(mp->m_flags & XFS_MOUNT_IKEEP)) {
168830f712c9SDave Chinner 		error = xfs_btree_delete(cur, &i);
168930f712c9SDave Chinner 		if (error)
169030f712c9SDave Chinner 			goto error;
169130f712c9SDave Chinner 		ASSERT(i == 1);
169230f712c9SDave Chinner 	} else {
169330f712c9SDave Chinner 		error = xfs_inobt_update(cur, &rec);
169430f712c9SDave Chinner 		if (error)
169530f712c9SDave Chinner 			goto error;
169630f712c9SDave Chinner 	}
169730f712c9SDave Chinner 
169830f712c9SDave Chinner out:
169930f712c9SDave Chinner 	error = xfs_check_agi_freecount(cur, agi);
170030f712c9SDave Chinner 	if (error)
170130f712c9SDave Chinner 		goto error;
170230f712c9SDave Chinner 
170330f712c9SDave Chinner 	xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
170430f712c9SDave Chinner 	return 0;
170530f712c9SDave Chinner 
170630f712c9SDave Chinner error:
170730f712c9SDave Chinner 	xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
170830f712c9SDave Chinner 	return error;
170930f712c9SDave Chinner }
171030f712c9SDave Chinner 
171130f712c9SDave Chinner /*
171230f712c9SDave Chinner  * Free disk inode.  Carefully avoids touching the incore inode, all
171330f712c9SDave Chinner  * manipulations incore are the caller's responsibility.
171430f712c9SDave Chinner  * The on-disk inode is not changed by this operation, only the
171530f712c9SDave Chinner  * btree (free inode mask) is changed.
171630f712c9SDave Chinner  */
171730f712c9SDave Chinner int
171830f712c9SDave Chinner xfs_difree(
171930f712c9SDave Chinner 	struct xfs_trans	*tp,		/* transaction pointer */
172030f712c9SDave Chinner 	xfs_ino_t		inode,		/* inode to be freed */
172130f712c9SDave Chinner 	struct xfs_bmap_free	*flist,		/* extents to free */
172230f712c9SDave Chinner 	int			*deleted,/* set if inode cluster was deleted */
172330f712c9SDave Chinner 	xfs_ino_t		*first_ino)/* first inode in deleted cluster */
172430f712c9SDave Chinner {
172530f712c9SDave Chinner 	/* REFERENCED */
172630f712c9SDave Chinner 	xfs_agblock_t		agbno;	/* block number containing inode */
172730f712c9SDave Chinner 	struct xfs_buf		*agbp;	/* buffer for allocation group header */
172830f712c9SDave Chinner 	xfs_agino_t		agino;	/* allocation group inode number */
172930f712c9SDave Chinner 	xfs_agnumber_t		agno;	/* allocation group number */
173030f712c9SDave Chinner 	int			error;	/* error return value */
173130f712c9SDave Chinner 	struct xfs_mount	*mp;	/* mount structure for filesystem */
173230f712c9SDave Chinner 	struct xfs_inobt_rec_incore rec;/* btree record */
173330f712c9SDave Chinner 
173430f712c9SDave Chinner 	mp = tp->t_mountp;
173530f712c9SDave Chinner 
173630f712c9SDave Chinner 	/*
173730f712c9SDave Chinner 	 * Break up inode number into its components.
173830f712c9SDave Chinner 	 */
173930f712c9SDave Chinner 	agno = XFS_INO_TO_AGNO(mp, inode);
174030f712c9SDave Chinner 	if (agno >= mp->m_sb.sb_agcount)  {
174130f712c9SDave Chinner 		xfs_warn(mp, "%s: agno >= mp->m_sb.sb_agcount (%d >= %d).",
174230f712c9SDave Chinner 			__func__, agno, mp->m_sb.sb_agcount);
174330f712c9SDave Chinner 		ASSERT(0);
17442451337dSDave Chinner 		return -EINVAL;
174530f712c9SDave Chinner 	}
174630f712c9SDave Chinner 	agino = XFS_INO_TO_AGINO(mp, inode);
174730f712c9SDave Chinner 	if (inode != XFS_AGINO_TO_INO(mp, agno, agino))  {
174830f712c9SDave Chinner 		xfs_warn(mp, "%s: inode != XFS_AGINO_TO_INO() (%llu != %llu).",
174930f712c9SDave Chinner 			__func__, (unsigned long long)inode,
175030f712c9SDave Chinner 			(unsigned long long)XFS_AGINO_TO_INO(mp, agno, agino));
175130f712c9SDave Chinner 		ASSERT(0);
17522451337dSDave Chinner 		return -EINVAL;
175330f712c9SDave Chinner 	}
175430f712c9SDave Chinner 	agbno = XFS_AGINO_TO_AGBNO(mp, agino);
175530f712c9SDave Chinner 	if (agbno >= mp->m_sb.sb_agblocks)  {
175630f712c9SDave Chinner 		xfs_warn(mp, "%s: agbno >= mp->m_sb.sb_agblocks (%d >= %d).",
175730f712c9SDave Chinner 			__func__, agbno, mp->m_sb.sb_agblocks);
175830f712c9SDave Chinner 		ASSERT(0);
17592451337dSDave Chinner 		return -EINVAL;
176030f712c9SDave Chinner 	}
176130f712c9SDave Chinner 	/*
176230f712c9SDave Chinner 	 * Get the allocation group header.
176330f712c9SDave Chinner 	 */
176430f712c9SDave Chinner 	error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
176530f712c9SDave Chinner 	if (error) {
176630f712c9SDave Chinner 		xfs_warn(mp, "%s: xfs_ialloc_read_agi() returned error %d.",
176730f712c9SDave Chinner 			__func__, error);
176830f712c9SDave Chinner 		return error;
176930f712c9SDave Chinner 	}
177030f712c9SDave Chinner 
177130f712c9SDave Chinner 	/*
177230f712c9SDave Chinner 	 * Fix up the inode allocation btree.
177330f712c9SDave Chinner 	 */
177430f712c9SDave Chinner 	error = xfs_difree_inobt(mp, tp, agbp, agino, flist, deleted, first_ino,
177530f712c9SDave Chinner 				 &rec);
177630f712c9SDave Chinner 	if (error)
177730f712c9SDave Chinner 		goto error0;
177830f712c9SDave Chinner 
177930f712c9SDave Chinner 	/*
178030f712c9SDave Chinner 	 * Fix up the free inode btree.
178130f712c9SDave Chinner 	 */
178230f712c9SDave Chinner 	if (xfs_sb_version_hasfinobt(&mp->m_sb)) {
178330f712c9SDave Chinner 		error = xfs_difree_finobt(mp, tp, agbp, agino, &rec);
178430f712c9SDave Chinner 		if (error)
178530f712c9SDave Chinner 			goto error0;
178630f712c9SDave Chinner 	}
178730f712c9SDave Chinner 
178830f712c9SDave Chinner 	return 0;
178930f712c9SDave Chinner 
179030f712c9SDave Chinner error0:
179130f712c9SDave Chinner 	return error;
179230f712c9SDave Chinner }
179330f712c9SDave Chinner 
179430f712c9SDave Chinner STATIC int
179530f712c9SDave Chinner xfs_imap_lookup(
179630f712c9SDave Chinner 	struct xfs_mount	*mp,
179730f712c9SDave Chinner 	struct xfs_trans	*tp,
179830f712c9SDave Chinner 	xfs_agnumber_t		agno,
179930f712c9SDave Chinner 	xfs_agino_t		agino,
180030f712c9SDave Chinner 	xfs_agblock_t		agbno,
180130f712c9SDave Chinner 	xfs_agblock_t		*chunk_agbno,
180230f712c9SDave Chinner 	xfs_agblock_t		*offset_agbno,
180330f712c9SDave Chinner 	int			flags)
180430f712c9SDave Chinner {
180530f712c9SDave Chinner 	struct xfs_inobt_rec_incore rec;
180630f712c9SDave Chinner 	struct xfs_btree_cur	*cur;
180730f712c9SDave Chinner 	struct xfs_buf		*agbp;
180830f712c9SDave Chinner 	int			error;
180930f712c9SDave Chinner 	int			i;
181030f712c9SDave Chinner 
181130f712c9SDave Chinner 	error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
181230f712c9SDave Chinner 	if (error) {
181330f712c9SDave Chinner 		xfs_alert(mp,
181430f712c9SDave Chinner 			"%s: xfs_ialloc_read_agi() returned error %d, agno %d",
181530f712c9SDave Chinner 			__func__, error, agno);
181630f712c9SDave Chinner 		return error;
181730f712c9SDave Chinner 	}
181830f712c9SDave Chinner 
181930f712c9SDave Chinner 	/*
182030f712c9SDave Chinner 	 * Lookup the inode record for the given agino. If the record cannot be
182130f712c9SDave Chinner 	 * found, then it's an invalid inode number and we should abort. Once
182230f712c9SDave Chinner 	 * we have a record, we need to ensure it contains the inode number
182330f712c9SDave Chinner 	 * we are looking up.
182430f712c9SDave Chinner 	 */
182530f712c9SDave Chinner 	cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
182630f712c9SDave Chinner 	error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i);
182730f712c9SDave Chinner 	if (!error) {
182830f712c9SDave Chinner 		if (i)
182930f712c9SDave Chinner 			error = xfs_inobt_get_rec(cur, &rec, &i);
183030f712c9SDave Chinner 		if (!error && i == 0)
18312451337dSDave Chinner 			error = -EINVAL;
183230f712c9SDave Chinner 	}
183330f712c9SDave Chinner 
183430f712c9SDave Chinner 	xfs_trans_brelse(tp, agbp);
183530f712c9SDave Chinner 	xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
183630f712c9SDave Chinner 	if (error)
183730f712c9SDave Chinner 		return error;
183830f712c9SDave Chinner 
183930f712c9SDave Chinner 	/* check that the returned record contains the required inode */
184030f712c9SDave Chinner 	if (rec.ir_startino > agino ||
184130f712c9SDave Chinner 	    rec.ir_startino + mp->m_ialloc_inos <= agino)
18422451337dSDave Chinner 		return -EINVAL;
184330f712c9SDave Chinner 
184430f712c9SDave Chinner 	/* for untrusted inodes check it is allocated first */
184530f712c9SDave Chinner 	if ((flags & XFS_IGET_UNTRUSTED) &&
184630f712c9SDave Chinner 	    (rec.ir_free & XFS_INOBT_MASK(agino - rec.ir_startino)))
18472451337dSDave Chinner 		return -EINVAL;
184830f712c9SDave Chinner 
184930f712c9SDave Chinner 	*chunk_agbno = XFS_AGINO_TO_AGBNO(mp, rec.ir_startino);
185030f712c9SDave Chinner 	*offset_agbno = agbno - *chunk_agbno;
185130f712c9SDave Chinner 	return 0;
185230f712c9SDave Chinner }
185330f712c9SDave Chinner 
185430f712c9SDave Chinner /*
185530f712c9SDave Chinner  * Return the location of the inode in imap, for mapping it into a buffer.
185630f712c9SDave Chinner  */
185730f712c9SDave Chinner int
185830f712c9SDave Chinner xfs_imap(
185930f712c9SDave Chinner 	xfs_mount_t	 *mp,	/* file system mount structure */
186030f712c9SDave Chinner 	xfs_trans_t	 *tp,	/* transaction pointer */
186130f712c9SDave Chinner 	xfs_ino_t	ino,	/* inode to locate */
186230f712c9SDave Chinner 	struct xfs_imap	*imap,	/* location map structure */
186330f712c9SDave Chinner 	uint		flags)	/* flags for inode btree lookup */
186430f712c9SDave Chinner {
186530f712c9SDave Chinner 	xfs_agblock_t	agbno;	/* block number of inode in the alloc group */
186630f712c9SDave Chinner 	xfs_agino_t	agino;	/* inode number within alloc group */
186730f712c9SDave Chinner 	xfs_agnumber_t	agno;	/* allocation group number */
186830f712c9SDave Chinner 	int		blks_per_cluster; /* num blocks per inode cluster */
186930f712c9SDave Chinner 	xfs_agblock_t	chunk_agbno;	/* first block in inode chunk */
187030f712c9SDave Chinner 	xfs_agblock_t	cluster_agbno;	/* first block in inode cluster */
187130f712c9SDave Chinner 	int		error;	/* error code */
187230f712c9SDave Chinner 	int		offset;	/* index of inode in its buffer */
187330f712c9SDave Chinner 	xfs_agblock_t	offset_agbno;	/* blks from chunk start to inode */
187430f712c9SDave Chinner 
187530f712c9SDave Chinner 	ASSERT(ino != NULLFSINO);
187630f712c9SDave Chinner 
187730f712c9SDave Chinner 	/*
187830f712c9SDave Chinner 	 * Split up the inode number into its parts.
187930f712c9SDave Chinner 	 */
188030f712c9SDave Chinner 	agno = XFS_INO_TO_AGNO(mp, ino);
188130f712c9SDave Chinner 	agino = XFS_INO_TO_AGINO(mp, ino);
188230f712c9SDave Chinner 	agbno = XFS_AGINO_TO_AGBNO(mp, agino);
188330f712c9SDave Chinner 	if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks ||
188430f712c9SDave Chinner 	    ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
188530f712c9SDave Chinner #ifdef DEBUG
188630f712c9SDave Chinner 		/*
188730f712c9SDave Chinner 		 * Don't output diagnostic information for untrusted inodes
188830f712c9SDave Chinner 		 * as they can be invalid without implying corruption.
188930f712c9SDave Chinner 		 */
189030f712c9SDave Chinner 		if (flags & XFS_IGET_UNTRUSTED)
18912451337dSDave Chinner 			return -EINVAL;
189230f712c9SDave Chinner 		if (agno >= mp->m_sb.sb_agcount) {
189330f712c9SDave Chinner 			xfs_alert(mp,
189430f712c9SDave Chinner 				"%s: agno (%d) >= mp->m_sb.sb_agcount (%d)",
189530f712c9SDave Chinner 				__func__, agno, mp->m_sb.sb_agcount);
189630f712c9SDave Chinner 		}
189730f712c9SDave Chinner 		if (agbno >= mp->m_sb.sb_agblocks) {
189830f712c9SDave Chinner 			xfs_alert(mp,
189930f712c9SDave Chinner 		"%s: agbno (0x%llx) >= mp->m_sb.sb_agblocks (0x%lx)",
190030f712c9SDave Chinner 				__func__, (unsigned long long)agbno,
190130f712c9SDave Chinner 				(unsigned long)mp->m_sb.sb_agblocks);
190230f712c9SDave Chinner 		}
190330f712c9SDave Chinner 		if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
190430f712c9SDave Chinner 			xfs_alert(mp,
190530f712c9SDave Chinner 		"%s: ino (0x%llx) != XFS_AGINO_TO_INO() (0x%llx)",
190630f712c9SDave Chinner 				__func__, ino,
190730f712c9SDave Chinner 				XFS_AGINO_TO_INO(mp, agno, agino));
190830f712c9SDave Chinner 		}
190930f712c9SDave Chinner 		xfs_stack_trace();
191030f712c9SDave Chinner #endif /* DEBUG */
19112451337dSDave Chinner 		return -EINVAL;
191230f712c9SDave Chinner 	}
191330f712c9SDave Chinner 
191430f712c9SDave Chinner 	blks_per_cluster = xfs_icluster_size_fsb(mp);
191530f712c9SDave Chinner 
191630f712c9SDave Chinner 	/*
191730f712c9SDave Chinner 	 * For bulkstat and handle lookups, we have an untrusted inode number
191830f712c9SDave Chinner 	 * that we have to verify is valid. We cannot do this just by reading
191930f712c9SDave Chinner 	 * the inode buffer as it may have been unlinked and removed leaving
192030f712c9SDave Chinner 	 * inodes in stale state on disk. Hence we have to do a btree lookup
192130f712c9SDave Chinner 	 * in all cases where an untrusted inode number is passed.
192230f712c9SDave Chinner 	 */
192330f712c9SDave Chinner 	if (flags & XFS_IGET_UNTRUSTED) {
192430f712c9SDave Chinner 		error = xfs_imap_lookup(mp, tp, agno, agino, agbno,
192530f712c9SDave Chinner 					&chunk_agbno, &offset_agbno, flags);
192630f712c9SDave Chinner 		if (error)
192730f712c9SDave Chinner 			return error;
192830f712c9SDave Chinner 		goto out_map;
192930f712c9SDave Chinner 	}
193030f712c9SDave Chinner 
193130f712c9SDave Chinner 	/*
193230f712c9SDave Chinner 	 * If the inode cluster size is the same as the blocksize or
193330f712c9SDave Chinner 	 * smaller we get to the buffer by simple arithmetics.
193430f712c9SDave Chinner 	 */
193530f712c9SDave Chinner 	if (blks_per_cluster == 1) {
193630f712c9SDave Chinner 		offset = XFS_INO_TO_OFFSET(mp, ino);
193730f712c9SDave Chinner 		ASSERT(offset < mp->m_sb.sb_inopblock);
193830f712c9SDave Chinner 
193930f712c9SDave Chinner 		imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, agbno);
194030f712c9SDave Chinner 		imap->im_len = XFS_FSB_TO_BB(mp, 1);
194130f712c9SDave Chinner 		imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog);
194230f712c9SDave Chinner 		return 0;
194330f712c9SDave Chinner 	}
194430f712c9SDave Chinner 
194530f712c9SDave Chinner 	/*
194630f712c9SDave Chinner 	 * If the inode chunks are aligned then use simple maths to
194730f712c9SDave Chinner 	 * find the location. Otherwise we have to do a btree
194830f712c9SDave Chinner 	 * lookup to find the location.
194930f712c9SDave Chinner 	 */
195030f712c9SDave Chinner 	if (mp->m_inoalign_mask) {
195130f712c9SDave Chinner 		offset_agbno = agbno & mp->m_inoalign_mask;
195230f712c9SDave Chinner 		chunk_agbno = agbno - offset_agbno;
195330f712c9SDave Chinner 	} else {
195430f712c9SDave Chinner 		error = xfs_imap_lookup(mp, tp, agno, agino, agbno,
195530f712c9SDave Chinner 					&chunk_agbno, &offset_agbno, flags);
195630f712c9SDave Chinner 		if (error)
195730f712c9SDave Chinner 			return error;
195830f712c9SDave Chinner 	}
195930f712c9SDave Chinner 
196030f712c9SDave Chinner out_map:
196130f712c9SDave Chinner 	ASSERT(agbno >= chunk_agbno);
196230f712c9SDave Chinner 	cluster_agbno = chunk_agbno +
196330f712c9SDave Chinner 		((offset_agbno / blks_per_cluster) * blks_per_cluster);
196430f712c9SDave Chinner 	offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
196530f712c9SDave Chinner 		XFS_INO_TO_OFFSET(mp, ino);
196630f712c9SDave Chinner 
196730f712c9SDave Chinner 	imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, cluster_agbno);
196830f712c9SDave Chinner 	imap->im_len = XFS_FSB_TO_BB(mp, blks_per_cluster);
196930f712c9SDave Chinner 	imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog);
197030f712c9SDave Chinner 
197130f712c9SDave Chinner 	/*
197230f712c9SDave Chinner 	 * If the inode number maps to a block outside the bounds
197330f712c9SDave Chinner 	 * of the file system then return NULL rather than calling
197430f712c9SDave Chinner 	 * read_buf and panicing when we get an error from the
197530f712c9SDave Chinner 	 * driver.
197630f712c9SDave Chinner 	 */
197730f712c9SDave Chinner 	if ((imap->im_blkno + imap->im_len) >
197830f712c9SDave Chinner 	    XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) {
197930f712c9SDave Chinner 		xfs_alert(mp,
198030f712c9SDave Chinner 	"%s: (im_blkno (0x%llx) + im_len (0x%llx)) > sb_dblocks (0x%llx)",
198130f712c9SDave Chinner 			__func__, (unsigned long long) imap->im_blkno,
198230f712c9SDave Chinner 			(unsigned long long) imap->im_len,
198330f712c9SDave Chinner 			XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks));
19842451337dSDave Chinner 		return -EINVAL;
198530f712c9SDave Chinner 	}
198630f712c9SDave Chinner 	return 0;
198730f712c9SDave Chinner }
198830f712c9SDave Chinner 
198930f712c9SDave Chinner /*
199030f712c9SDave Chinner  * Compute and fill in value of m_in_maxlevels.
199130f712c9SDave Chinner  */
199230f712c9SDave Chinner void
199330f712c9SDave Chinner xfs_ialloc_compute_maxlevels(
199430f712c9SDave Chinner 	xfs_mount_t	*mp)		/* file system mount structure */
199530f712c9SDave Chinner {
199630f712c9SDave Chinner 	int		level;
199730f712c9SDave Chinner 	uint		maxblocks;
199830f712c9SDave Chinner 	uint		maxleafents;
199930f712c9SDave Chinner 	int		minleafrecs;
200030f712c9SDave Chinner 	int		minnoderecs;
200130f712c9SDave Chinner 
200230f712c9SDave Chinner 	maxleafents = (1LL << XFS_INO_AGINO_BITS(mp)) >>
200330f712c9SDave Chinner 		XFS_INODES_PER_CHUNK_LOG;
200430f712c9SDave Chinner 	minleafrecs = mp->m_alloc_mnr[0];
200530f712c9SDave Chinner 	minnoderecs = mp->m_alloc_mnr[1];
200630f712c9SDave Chinner 	maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
200730f712c9SDave Chinner 	for (level = 1; maxblocks > 1; level++)
200830f712c9SDave Chinner 		maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
200930f712c9SDave Chinner 	mp->m_in_maxlevels = level;
201030f712c9SDave Chinner }
201130f712c9SDave Chinner 
201230f712c9SDave Chinner /*
201330f712c9SDave Chinner  * Log specified fields for the ag hdr (inode section). The growth of the agi
201430f712c9SDave Chinner  * structure over time requires that we interpret the buffer as two logical
201530f712c9SDave Chinner  * regions delineated by the end of the unlinked list. This is due to the size
201630f712c9SDave Chinner  * of the hash table and its location in the middle of the agi.
201730f712c9SDave Chinner  *
201830f712c9SDave Chinner  * For example, a request to log a field before agi_unlinked and a field after
201930f712c9SDave Chinner  * agi_unlinked could cause us to log the entire hash table and use an excessive
202030f712c9SDave Chinner  * amount of log space. To avoid this behavior, log the region up through
202130f712c9SDave Chinner  * agi_unlinked in one call and the region after agi_unlinked through the end of
202230f712c9SDave Chinner  * the structure in another.
202330f712c9SDave Chinner  */
202430f712c9SDave Chinner void
202530f712c9SDave Chinner xfs_ialloc_log_agi(
202630f712c9SDave Chinner 	xfs_trans_t	*tp,		/* transaction pointer */
202730f712c9SDave Chinner 	xfs_buf_t	*bp,		/* allocation group header buffer */
202830f712c9SDave Chinner 	int		fields)		/* bitmask of fields to log */
202930f712c9SDave Chinner {
203030f712c9SDave Chinner 	int			first;		/* first byte number */
203130f712c9SDave Chinner 	int			last;		/* last byte number */
203230f712c9SDave Chinner 	static const short	offsets[] = {	/* field starting offsets */
203330f712c9SDave Chinner 					/* keep in sync with bit definitions */
203430f712c9SDave Chinner 		offsetof(xfs_agi_t, agi_magicnum),
203530f712c9SDave Chinner 		offsetof(xfs_agi_t, agi_versionnum),
203630f712c9SDave Chinner 		offsetof(xfs_agi_t, agi_seqno),
203730f712c9SDave Chinner 		offsetof(xfs_agi_t, agi_length),
203830f712c9SDave Chinner 		offsetof(xfs_agi_t, agi_count),
203930f712c9SDave Chinner 		offsetof(xfs_agi_t, agi_root),
204030f712c9SDave Chinner 		offsetof(xfs_agi_t, agi_level),
204130f712c9SDave Chinner 		offsetof(xfs_agi_t, agi_freecount),
204230f712c9SDave Chinner 		offsetof(xfs_agi_t, agi_newino),
204330f712c9SDave Chinner 		offsetof(xfs_agi_t, agi_dirino),
204430f712c9SDave Chinner 		offsetof(xfs_agi_t, agi_unlinked),
204530f712c9SDave Chinner 		offsetof(xfs_agi_t, agi_free_root),
204630f712c9SDave Chinner 		offsetof(xfs_agi_t, agi_free_level),
204730f712c9SDave Chinner 		sizeof(xfs_agi_t)
204830f712c9SDave Chinner 	};
204930f712c9SDave Chinner #ifdef DEBUG
205030f712c9SDave Chinner 	xfs_agi_t		*agi;	/* allocation group header */
205130f712c9SDave Chinner 
205230f712c9SDave Chinner 	agi = XFS_BUF_TO_AGI(bp);
205330f712c9SDave Chinner 	ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC));
205430f712c9SDave Chinner #endif
205530f712c9SDave Chinner 
205630f712c9SDave Chinner 	xfs_trans_buf_set_type(tp, bp, XFS_BLFT_AGI_BUF);
205730f712c9SDave Chinner 
205830f712c9SDave Chinner 	/*
205930f712c9SDave Chinner 	 * Compute byte offsets for the first and last fields in the first
206030f712c9SDave Chinner 	 * region and log the agi buffer. This only logs up through
206130f712c9SDave Chinner 	 * agi_unlinked.
206230f712c9SDave Chinner 	 */
206330f712c9SDave Chinner 	if (fields & XFS_AGI_ALL_BITS_R1) {
206430f712c9SDave Chinner 		xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R1,
206530f712c9SDave Chinner 				  &first, &last);
206630f712c9SDave Chinner 		xfs_trans_log_buf(tp, bp, first, last);
206730f712c9SDave Chinner 	}
206830f712c9SDave Chinner 
206930f712c9SDave Chinner 	/*
207030f712c9SDave Chinner 	 * Mask off the bits in the first region and calculate the first and
207130f712c9SDave Chinner 	 * last field offsets for any bits in the second region.
207230f712c9SDave Chinner 	 */
207330f712c9SDave Chinner 	fields &= ~XFS_AGI_ALL_BITS_R1;
207430f712c9SDave Chinner 	if (fields) {
207530f712c9SDave Chinner 		xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R2,
207630f712c9SDave Chinner 				  &first, &last);
207730f712c9SDave Chinner 		xfs_trans_log_buf(tp, bp, first, last);
207830f712c9SDave Chinner 	}
207930f712c9SDave Chinner }
208030f712c9SDave Chinner 
208130f712c9SDave Chinner #ifdef DEBUG
208230f712c9SDave Chinner STATIC void
208330f712c9SDave Chinner xfs_check_agi_unlinked(
208430f712c9SDave Chinner 	struct xfs_agi		*agi)
208530f712c9SDave Chinner {
208630f712c9SDave Chinner 	int			i;
208730f712c9SDave Chinner 
208830f712c9SDave Chinner 	for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++)
208930f712c9SDave Chinner 		ASSERT(agi->agi_unlinked[i]);
209030f712c9SDave Chinner }
209130f712c9SDave Chinner #else
209230f712c9SDave Chinner #define xfs_check_agi_unlinked(agi)
209330f712c9SDave Chinner #endif
209430f712c9SDave Chinner 
209530f712c9SDave Chinner static bool
209630f712c9SDave Chinner xfs_agi_verify(
209730f712c9SDave Chinner 	struct xfs_buf	*bp)
209830f712c9SDave Chinner {
209930f712c9SDave Chinner 	struct xfs_mount *mp = bp->b_target->bt_mount;
210030f712c9SDave Chinner 	struct xfs_agi	*agi = XFS_BUF_TO_AGI(bp);
210130f712c9SDave Chinner 
210230f712c9SDave Chinner 	if (xfs_sb_version_hascrc(&mp->m_sb) &&
210330f712c9SDave Chinner 	    !uuid_equal(&agi->agi_uuid, &mp->m_sb.sb_uuid))
210430f712c9SDave Chinner 			return false;
210530f712c9SDave Chinner 	/*
210630f712c9SDave Chinner 	 * Validate the magic number of the agi block.
210730f712c9SDave Chinner 	 */
210830f712c9SDave Chinner 	if (agi->agi_magicnum != cpu_to_be32(XFS_AGI_MAGIC))
210930f712c9SDave Chinner 		return false;
211030f712c9SDave Chinner 	if (!XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum)))
211130f712c9SDave Chinner 		return false;
211230f712c9SDave Chinner 
2113e1b05723SEric Sandeen 	if (be32_to_cpu(agi->agi_level) > XFS_BTREE_MAXLEVELS)
2114e1b05723SEric Sandeen 		return false;
211530f712c9SDave Chinner 	/*
211630f712c9SDave Chinner 	 * during growfs operations, the perag is not fully initialised,
211730f712c9SDave Chinner 	 * so we can't use it for any useful checking. growfs ensures we can't
211830f712c9SDave Chinner 	 * use it by using uncached buffers that don't have the perag attached
211930f712c9SDave Chinner 	 * so we can detect and avoid this problem.
212030f712c9SDave Chinner 	 */
212130f712c9SDave Chinner 	if (bp->b_pag && be32_to_cpu(agi->agi_seqno) != bp->b_pag->pag_agno)
212230f712c9SDave Chinner 		return false;
212330f712c9SDave Chinner 
212430f712c9SDave Chinner 	xfs_check_agi_unlinked(agi);
212530f712c9SDave Chinner 	return true;
212630f712c9SDave Chinner }
212730f712c9SDave Chinner 
212830f712c9SDave Chinner static void
212930f712c9SDave Chinner xfs_agi_read_verify(
213030f712c9SDave Chinner 	struct xfs_buf	*bp)
213130f712c9SDave Chinner {
213230f712c9SDave Chinner 	struct xfs_mount *mp = bp->b_target->bt_mount;
213330f712c9SDave Chinner 
213430f712c9SDave Chinner 	if (xfs_sb_version_hascrc(&mp->m_sb) &&
213530f712c9SDave Chinner 	    !xfs_buf_verify_cksum(bp, XFS_AGI_CRC_OFF))
21362451337dSDave Chinner 		xfs_buf_ioerror(bp, -EFSBADCRC);
213730f712c9SDave Chinner 	else if (XFS_TEST_ERROR(!xfs_agi_verify(bp), mp,
213830f712c9SDave Chinner 				XFS_ERRTAG_IALLOC_READ_AGI,
213930f712c9SDave Chinner 				XFS_RANDOM_IALLOC_READ_AGI))
21402451337dSDave Chinner 		xfs_buf_ioerror(bp, -EFSCORRUPTED);
214130f712c9SDave Chinner 
214230f712c9SDave Chinner 	if (bp->b_error)
214330f712c9SDave Chinner 		xfs_verifier_error(bp);
214430f712c9SDave Chinner }
214530f712c9SDave Chinner 
214630f712c9SDave Chinner static void
214730f712c9SDave Chinner xfs_agi_write_verify(
214830f712c9SDave Chinner 	struct xfs_buf	*bp)
214930f712c9SDave Chinner {
215030f712c9SDave Chinner 	struct xfs_mount *mp = bp->b_target->bt_mount;
215130f712c9SDave Chinner 	struct xfs_buf_log_item	*bip = bp->b_fspriv;
215230f712c9SDave Chinner 
215330f712c9SDave Chinner 	if (!xfs_agi_verify(bp)) {
21542451337dSDave Chinner 		xfs_buf_ioerror(bp, -EFSCORRUPTED);
215530f712c9SDave Chinner 		xfs_verifier_error(bp);
215630f712c9SDave Chinner 		return;
215730f712c9SDave Chinner 	}
215830f712c9SDave Chinner 
215930f712c9SDave Chinner 	if (!xfs_sb_version_hascrc(&mp->m_sb))
216030f712c9SDave Chinner 		return;
216130f712c9SDave Chinner 
216230f712c9SDave Chinner 	if (bip)
216330f712c9SDave Chinner 		XFS_BUF_TO_AGI(bp)->agi_lsn = cpu_to_be64(bip->bli_item.li_lsn);
216430f712c9SDave Chinner 	xfs_buf_update_cksum(bp, XFS_AGI_CRC_OFF);
216530f712c9SDave Chinner }
216630f712c9SDave Chinner 
216730f712c9SDave Chinner const struct xfs_buf_ops xfs_agi_buf_ops = {
216830f712c9SDave Chinner 	.verify_read = xfs_agi_read_verify,
216930f712c9SDave Chinner 	.verify_write = xfs_agi_write_verify,
217030f712c9SDave Chinner };
217130f712c9SDave Chinner 
217230f712c9SDave Chinner /*
217330f712c9SDave Chinner  * Read in the allocation group header (inode allocation section)
217430f712c9SDave Chinner  */
217530f712c9SDave Chinner int
217630f712c9SDave Chinner xfs_read_agi(
217730f712c9SDave Chinner 	struct xfs_mount	*mp,	/* file system mount structure */
217830f712c9SDave Chinner 	struct xfs_trans	*tp,	/* transaction pointer */
217930f712c9SDave Chinner 	xfs_agnumber_t		agno,	/* allocation group number */
218030f712c9SDave Chinner 	struct xfs_buf		**bpp)	/* allocation group hdr buf */
218130f712c9SDave Chinner {
218230f712c9SDave Chinner 	int			error;
218330f712c9SDave Chinner 
218430f712c9SDave Chinner 	trace_xfs_read_agi(mp, agno);
218530f712c9SDave Chinner 
218630f712c9SDave Chinner 	ASSERT(agno != NULLAGNUMBER);
218730f712c9SDave Chinner 	error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
218830f712c9SDave Chinner 			XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
218930f712c9SDave Chinner 			XFS_FSS_TO_BB(mp, 1), 0, bpp, &xfs_agi_buf_ops);
219030f712c9SDave Chinner 	if (error)
219130f712c9SDave Chinner 		return error;
219230f712c9SDave Chinner 
219330f712c9SDave Chinner 	xfs_buf_set_ref(*bpp, XFS_AGI_REF);
219430f712c9SDave Chinner 	return 0;
219530f712c9SDave Chinner }
219630f712c9SDave Chinner 
219730f712c9SDave Chinner int
219830f712c9SDave Chinner xfs_ialloc_read_agi(
219930f712c9SDave Chinner 	struct xfs_mount	*mp,	/* file system mount structure */
220030f712c9SDave Chinner 	struct xfs_trans	*tp,	/* transaction pointer */
220130f712c9SDave Chinner 	xfs_agnumber_t		agno,	/* allocation group number */
220230f712c9SDave Chinner 	struct xfs_buf		**bpp)	/* allocation group hdr buf */
220330f712c9SDave Chinner {
220430f712c9SDave Chinner 	struct xfs_agi		*agi;	/* allocation group header */
220530f712c9SDave Chinner 	struct xfs_perag	*pag;	/* per allocation group data */
220630f712c9SDave Chinner 	int			error;
220730f712c9SDave Chinner 
220830f712c9SDave Chinner 	trace_xfs_ialloc_read_agi(mp, agno);
220930f712c9SDave Chinner 
221030f712c9SDave Chinner 	error = xfs_read_agi(mp, tp, agno, bpp);
221130f712c9SDave Chinner 	if (error)
221230f712c9SDave Chinner 		return error;
221330f712c9SDave Chinner 
221430f712c9SDave Chinner 	agi = XFS_BUF_TO_AGI(*bpp);
221530f712c9SDave Chinner 	pag = xfs_perag_get(mp, agno);
221630f712c9SDave Chinner 	if (!pag->pagi_init) {
221730f712c9SDave Chinner 		pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
221830f712c9SDave Chinner 		pag->pagi_count = be32_to_cpu(agi->agi_count);
221930f712c9SDave Chinner 		pag->pagi_init = 1;
222030f712c9SDave Chinner 	}
222130f712c9SDave Chinner 
222230f712c9SDave Chinner 	/*
222330f712c9SDave Chinner 	 * It's possible for these to be out of sync if
222430f712c9SDave Chinner 	 * we are in the middle of a forced shutdown.
222530f712c9SDave Chinner 	 */
222630f712c9SDave Chinner 	ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) ||
222730f712c9SDave Chinner 		XFS_FORCED_SHUTDOWN(mp));
222830f712c9SDave Chinner 	xfs_perag_put(pag);
222930f712c9SDave Chinner 	return 0;
223030f712c9SDave Chinner }
223130f712c9SDave Chinner 
223230f712c9SDave Chinner /*
223330f712c9SDave Chinner  * Read in the agi to initialise the per-ag data in the mount structure
223430f712c9SDave Chinner  */
223530f712c9SDave Chinner int
223630f712c9SDave Chinner xfs_ialloc_pagi_init(
223730f712c9SDave Chinner 	xfs_mount_t	*mp,		/* file system mount structure */
223830f712c9SDave Chinner 	xfs_trans_t	*tp,		/* transaction pointer */
223930f712c9SDave Chinner 	xfs_agnumber_t	agno)		/* allocation group number */
224030f712c9SDave Chinner {
224130f712c9SDave Chinner 	xfs_buf_t	*bp = NULL;
224230f712c9SDave Chinner 	int		error;
224330f712c9SDave Chinner 
224430f712c9SDave Chinner 	error = xfs_ialloc_read_agi(mp, tp, agno, &bp);
224530f712c9SDave Chinner 	if (error)
224630f712c9SDave Chinner 		return error;
224730f712c9SDave Chinner 	if (bp)
224830f712c9SDave Chinner 		xfs_trans_brelse(tp, bp);
224930f712c9SDave Chinner 	return 0;
225030f712c9SDave Chinner }
2251