xref: /openbmc/linux/fs/xfs/libxfs/xfs_ialloc.c (revision 233135b763db7c64d07b728a9c66745fb0376275)
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"
41a45086e2SBrian Foster #include "xfs_log.h"
4230f712c9SDave Chinner 
4330f712c9SDave Chinner 
4430f712c9SDave Chinner /*
4530f712c9SDave Chinner  * Allocation group level functions.
4630f712c9SDave Chinner  */
4730f712c9SDave Chinner static inline int
4830f712c9SDave Chinner xfs_ialloc_cluster_alignment(
497a1df156SDave Chinner 	struct xfs_mount	*mp)
5030f712c9SDave Chinner {
517a1df156SDave Chinner 	if (xfs_sb_version_hasalign(&mp->m_sb) &&
527a1df156SDave Chinner 	    mp->m_sb.sb_inoalignmt >=
537a1df156SDave Chinner 			XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size))
547a1df156SDave Chinner 		return mp->m_sb.sb_inoalignmt;
5530f712c9SDave Chinner 	return 1;
5630f712c9SDave Chinner }
5730f712c9SDave Chinner 
5830f712c9SDave Chinner /*
5930f712c9SDave Chinner  * Lookup a record by ino in the btree given by cur.
6030f712c9SDave Chinner  */
6130f712c9SDave Chinner int					/* error */
6230f712c9SDave Chinner xfs_inobt_lookup(
6330f712c9SDave Chinner 	struct xfs_btree_cur	*cur,	/* btree cursor */
6430f712c9SDave Chinner 	xfs_agino_t		ino,	/* starting inode of chunk */
6530f712c9SDave Chinner 	xfs_lookup_t		dir,	/* <=, >=, == */
6630f712c9SDave Chinner 	int			*stat)	/* success/failure */
6730f712c9SDave Chinner {
6830f712c9SDave Chinner 	cur->bc_rec.i.ir_startino = ino;
695419040fSBrian Foster 	cur->bc_rec.i.ir_holemask = 0;
705419040fSBrian Foster 	cur->bc_rec.i.ir_count = 0;
7130f712c9SDave Chinner 	cur->bc_rec.i.ir_freecount = 0;
7230f712c9SDave Chinner 	cur->bc_rec.i.ir_free = 0;
7330f712c9SDave Chinner 	return xfs_btree_lookup(cur, dir, stat);
7430f712c9SDave Chinner }
7530f712c9SDave Chinner 
7630f712c9SDave Chinner /*
7730f712c9SDave Chinner  * Update the record referred to by cur to the value given.
7830f712c9SDave Chinner  * This either works (return 0) or gets an EFSCORRUPTED error.
7930f712c9SDave Chinner  */
8030f712c9SDave Chinner STATIC int				/* error */
8130f712c9SDave Chinner xfs_inobt_update(
8230f712c9SDave Chinner 	struct xfs_btree_cur	*cur,	/* btree cursor */
8330f712c9SDave Chinner 	xfs_inobt_rec_incore_t	*irec)	/* btree record */
8430f712c9SDave Chinner {
8530f712c9SDave Chinner 	union xfs_btree_rec	rec;
8630f712c9SDave Chinner 
8730f712c9SDave Chinner 	rec.inobt.ir_startino = cpu_to_be32(irec->ir_startino);
885419040fSBrian Foster 	if (xfs_sb_version_hassparseinodes(&cur->bc_mp->m_sb)) {
895419040fSBrian Foster 		rec.inobt.ir_u.sp.ir_holemask = cpu_to_be16(irec->ir_holemask);
905419040fSBrian Foster 		rec.inobt.ir_u.sp.ir_count = irec->ir_count;
915419040fSBrian Foster 		rec.inobt.ir_u.sp.ir_freecount = irec->ir_freecount;
925419040fSBrian Foster 	} else {
935419040fSBrian Foster 		/* ir_holemask/ir_count not supported on-disk */
945419040fSBrian Foster 		rec.inobt.ir_u.f.ir_freecount = cpu_to_be32(irec->ir_freecount);
955419040fSBrian Foster 	}
9630f712c9SDave Chinner 	rec.inobt.ir_free = cpu_to_be64(irec->ir_free);
9730f712c9SDave Chinner 	return xfs_btree_update(cur, &rec);
9830f712c9SDave Chinner }
9930f712c9SDave Chinner 
10030f712c9SDave Chinner /*
10130f712c9SDave Chinner  * Get the data from the pointed-to record.
10230f712c9SDave Chinner  */
10330f712c9SDave Chinner int					/* error */
10430f712c9SDave Chinner xfs_inobt_get_rec(
10530f712c9SDave Chinner 	struct xfs_btree_cur	*cur,	/* btree cursor */
10630f712c9SDave Chinner 	xfs_inobt_rec_incore_t	*irec,	/* btree record */
10730f712c9SDave Chinner 	int			*stat)	/* output: success/failure */
10830f712c9SDave Chinner {
10930f712c9SDave Chinner 	union xfs_btree_rec	*rec;
11030f712c9SDave Chinner 	int			error;
11130f712c9SDave Chinner 
11230f712c9SDave Chinner 	error = xfs_btree_get_rec(cur, &rec, stat);
1135419040fSBrian Foster 	if (error || *stat == 0)
11430f712c9SDave Chinner 		return error;
1155419040fSBrian Foster 
1165419040fSBrian Foster 	irec->ir_startino = be32_to_cpu(rec->inobt.ir_startino);
1175419040fSBrian Foster 	if (xfs_sb_version_hassparseinodes(&cur->bc_mp->m_sb)) {
1185419040fSBrian Foster 		irec->ir_holemask = be16_to_cpu(rec->inobt.ir_u.sp.ir_holemask);
1195419040fSBrian Foster 		irec->ir_count = rec->inobt.ir_u.sp.ir_count;
1205419040fSBrian Foster 		irec->ir_freecount = rec->inobt.ir_u.sp.ir_freecount;
1215419040fSBrian Foster 	} else {
1225419040fSBrian Foster 		/*
1235419040fSBrian Foster 		 * ir_holemask/ir_count not supported on-disk. Fill in hardcoded
1245419040fSBrian Foster 		 * values for full inode chunks.
1255419040fSBrian Foster 		 */
1265419040fSBrian Foster 		irec->ir_holemask = XFS_INOBT_HOLEMASK_FULL;
1275419040fSBrian Foster 		irec->ir_count = XFS_INODES_PER_CHUNK;
1285419040fSBrian Foster 		irec->ir_freecount =
1295419040fSBrian Foster 				be32_to_cpu(rec->inobt.ir_u.f.ir_freecount);
1305419040fSBrian Foster 	}
1315419040fSBrian Foster 	irec->ir_free = be64_to_cpu(rec->inobt.ir_free);
1325419040fSBrian Foster 
1335419040fSBrian Foster 	return 0;
13430f712c9SDave Chinner }
13530f712c9SDave Chinner 
13630f712c9SDave Chinner /*
13730f712c9SDave Chinner  * Insert a single inobt record. Cursor must already point to desired location.
13830f712c9SDave Chinner  */
13930f712c9SDave Chinner STATIC int
14030f712c9SDave Chinner xfs_inobt_insert_rec(
14130f712c9SDave Chinner 	struct xfs_btree_cur	*cur,
1425419040fSBrian Foster 	__uint16_t		holemask,
1435419040fSBrian Foster 	__uint8_t		count,
14430f712c9SDave Chinner 	__int32_t		freecount,
14530f712c9SDave Chinner 	xfs_inofree_t		free,
14630f712c9SDave Chinner 	int			*stat)
14730f712c9SDave Chinner {
1485419040fSBrian Foster 	cur->bc_rec.i.ir_holemask = holemask;
1495419040fSBrian Foster 	cur->bc_rec.i.ir_count = count;
15030f712c9SDave Chinner 	cur->bc_rec.i.ir_freecount = freecount;
15130f712c9SDave Chinner 	cur->bc_rec.i.ir_free = free;
15230f712c9SDave Chinner 	return xfs_btree_insert(cur, stat);
15330f712c9SDave Chinner }
15430f712c9SDave Chinner 
15530f712c9SDave Chinner /*
15630f712c9SDave Chinner  * Insert records describing a newly allocated inode chunk into the inobt.
15730f712c9SDave Chinner  */
15830f712c9SDave Chinner STATIC int
15930f712c9SDave Chinner xfs_inobt_insert(
16030f712c9SDave Chinner 	struct xfs_mount	*mp,
16130f712c9SDave Chinner 	struct xfs_trans	*tp,
16230f712c9SDave Chinner 	struct xfs_buf		*agbp,
16330f712c9SDave Chinner 	xfs_agino_t		newino,
16430f712c9SDave Chinner 	xfs_agino_t		newlen,
16530f712c9SDave Chinner 	xfs_btnum_t		btnum)
16630f712c9SDave Chinner {
16730f712c9SDave Chinner 	struct xfs_btree_cur	*cur;
16830f712c9SDave Chinner 	struct xfs_agi		*agi = XFS_BUF_TO_AGI(agbp);
16930f712c9SDave Chinner 	xfs_agnumber_t		agno = be32_to_cpu(agi->agi_seqno);
17030f712c9SDave Chinner 	xfs_agino_t		thisino;
17130f712c9SDave Chinner 	int			i;
17230f712c9SDave Chinner 	int			error;
17330f712c9SDave Chinner 
17430f712c9SDave Chinner 	cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, btnum);
17530f712c9SDave Chinner 
17630f712c9SDave Chinner 	for (thisino = newino;
17730f712c9SDave Chinner 	     thisino < newino + newlen;
17830f712c9SDave Chinner 	     thisino += XFS_INODES_PER_CHUNK) {
17930f712c9SDave Chinner 		error = xfs_inobt_lookup(cur, thisino, XFS_LOOKUP_EQ, &i);
18030f712c9SDave Chinner 		if (error) {
18130f712c9SDave Chinner 			xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
18230f712c9SDave Chinner 			return error;
18330f712c9SDave Chinner 		}
18430f712c9SDave Chinner 		ASSERT(i == 0);
18530f712c9SDave Chinner 
1865419040fSBrian Foster 		error = xfs_inobt_insert_rec(cur, XFS_INOBT_HOLEMASK_FULL,
1875419040fSBrian Foster 					     XFS_INODES_PER_CHUNK,
1885419040fSBrian Foster 					     XFS_INODES_PER_CHUNK,
18930f712c9SDave Chinner 					     XFS_INOBT_ALL_FREE, &i);
19030f712c9SDave Chinner 		if (error) {
19130f712c9SDave Chinner 			xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
19230f712c9SDave Chinner 			return error;
19330f712c9SDave Chinner 		}
19430f712c9SDave Chinner 		ASSERT(i == 1);
19530f712c9SDave Chinner 	}
19630f712c9SDave Chinner 
19730f712c9SDave Chinner 	xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
19830f712c9SDave Chinner 
19930f712c9SDave Chinner 	return 0;
20030f712c9SDave Chinner }
20130f712c9SDave Chinner 
20230f712c9SDave Chinner /*
20330f712c9SDave Chinner  * Verify that the number of free inodes in the AGI is correct.
20430f712c9SDave Chinner  */
20530f712c9SDave Chinner #ifdef DEBUG
20630f712c9SDave Chinner STATIC int
20730f712c9SDave Chinner xfs_check_agi_freecount(
20830f712c9SDave Chinner 	struct xfs_btree_cur	*cur,
20930f712c9SDave Chinner 	struct xfs_agi		*agi)
21030f712c9SDave Chinner {
21130f712c9SDave Chinner 	if (cur->bc_nlevels == 1) {
21230f712c9SDave Chinner 		xfs_inobt_rec_incore_t rec;
21330f712c9SDave Chinner 		int		freecount = 0;
21430f712c9SDave Chinner 		int		error;
21530f712c9SDave Chinner 		int		i;
21630f712c9SDave Chinner 
21730f712c9SDave Chinner 		error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
21830f712c9SDave Chinner 		if (error)
21930f712c9SDave Chinner 			return error;
22030f712c9SDave Chinner 
22130f712c9SDave Chinner 		do {
22230f712c9SDave Chinner 			error = xfs_inobt_get_rec(cur, &rec, &i);
22330f712c9SDave Chinner 			if (error)
22430f712c9SDave Chinner 				return error;
22530f712c9SDave Chinner 
22630f712c9SDave Chinner 			if (i) {
22730f712c9SDave Chinner 				freecount += rec.ir_freecount;
22830f712c9SDave Chinner 				error = xfs_btree_increment(cur, 0, &i);
22930f712c9SDave Chinner 				if (error)
23030f712c9SDave Chinner 					return error;
23130f712c9SDave Chinner 			}
23230f712c9SDave Chinner 		} while (i == 1);
23330f712c9SDave Chinner 
23430f712c9SDave Chinner 		if (!XFS_FORCED_SHUTDOWN(cur->bc_mp))
23530f712c9SDave Chinner 			ASSERT(freecount == be32_to_cpu(agi->agi_freecount));
23630f712c9SDave Chinner 	}
23730f712c9SDave Chinner 	return 0;
23830f712c9SDave Chinner }
23930f712c9SDave Chinner #else
24030f712c9SDave Chinner #define xfs_check_agi_freecount(cur, agi)	0
24130f712c9SDave Chinner #endif
24230f712c9SDave Chinner 
24330f712c9SDave Chinner /*
24430f712c9SDave Chinner  * Initialise a new set of inodes. When called without a transaction context
24530f712c9SDave Chinner  * (e.g. from recovery) we initiate a delayed write of the inode buffers rather
24630f712c9SDave Chinner  * than logging them (which in a transaction context puts them into the AIL
24730f712c9SDave Chinner  * for writeback rather than the xfsbufd queue).
24830f712c9SDave Chinner  */
24930f712c9SDave Chinner int
25030f712c9SDave Chinner xfs_ialloc_inode_init(
25130f712c9SDave Chinner 	struct xfs_mount	*mp,
25230f712c9SDave Chinner 	struct xfs_trans	*tp,
25330f712c9SDave Chinner 	struct list_head	*buffer_list,
254463958afSBrian Foster 	int			icount,
25530f712c9SDave Chinner 	xfs_agnumber_t		agno,
25630f712c9SDave Chinner 	xfs_agblock_t		agbno,
25730f712c9SDave Chinner 	xfs_agblock_t		length,
25830f712c9SDave Chinner 	unsigned int		gen)
25930f712c9SDave Chinner {
26030f712c9SDave Chinner 	struct xfs_buf		*fbuf;
26130f712c9SDave Chinner 	struct xfs_dinode	*free;
26230f712c9SDave Chinner 	int			nbufs, blks_per_cluster, inodes_per_cluster;
26330f712c9SDave Chinner 	int			version;
26430f712c9SDave Chinner 	int			i, j;
26530f712c9SDave Chinner 	xfs_daddr_t		d;
26630f712c9SDave Chinner 	xfs_ino_t		ino = 0;
26730f712c9SDave Chinner 
26830f712c9SDave Chinner 	/*
26930f712c9SDave Chinner 	 * Loop over the new block(s), filling in the inodes.  For small block
27030f712c9SDave Chinner 	 * sizes, manipulate the inodes in buffers  which are multiples of the
27130f712c9SDave Chinner 	 * blocks size.
27230f712c9SDave Chinner 	 */
27330f712c9SDave Chinner 	blks_per_cluster = xfs_icluster_size_fsb(mp);
27430f712c9SDave Chinner 	inodes_per_cluster = blks_per_cluster << mp->m_sb.sb_inopblog;
27530f712c9SDave Chinner 	nbufs = length / blks_per_cluster;
27630f712c9SDave Chinner 
27730f712c9SDave Chinner 	/*
27830f712c9SDave Chinner 	 * Figure out what version number to use in the inodes we create.  If
27930f712c9SDave Chinner 	 * the superblock version has caught up to the one that supports the new
28030f712c9SDave Chinner 	 * inode format, then use the new inode version.  Otherwise use the old
28130f712c9SDave Chinner 	 * version so that old kernels will continue to be able to use the file
28230f712c9SDave Chinner 	 * system.
28330f712c9SDave Chinner 	 *
28430f712c9SDave Chinner 	 * For v3 inodes, we also need to write the inode number into the inode,
28530f712c9SDave Chinner 	 * so calculate the first inode number of the chunk here as
28630f712c9SDave Chinner 	 * XFS_OFFBNO_TO_AGINO() only works within a filesystem block, not
28730f712c9SDave Chinner 	 * across multiple filesystem blocks (such as a cluster) and so cannot
28830f712c9SDave Chinner 	 * be used in the cluster buffer loop below.
28930f712c9SDave Chinner 	 *
29030f712c9SDave Chinner 	 * Further, because we are writing the inode directly into the buffer
29130f712c9SDave Chinner 	 * and calculating a CRC on the entire inode, we have ot log the entire
29230f712c9SDave Chinner 	 * inode so that the entire range the CRC covers is present in the log.
29330f712c9SDave Chinner 	 * That means for v3 inode we log the entire buffer rather than just the
29430f712c9SDave Chinner 	 * inode cores.
29530f712c9SDave Chinner 	 */
29630f712c9SDave Chinner 	if (xfs_sb_version_hascrc(&mp->m_sb)) {
29730f712c9SDave Chinner 		version = 3;
29830f712c9SDave Chinner 		ino = XFS_AGINO_TO_INO(mp, agno,
29930f712c9SDave Chinner 				       XFS_OFFBNO_TO_AGINO(mp, agbno, 0));
30030f712c9SDave Chinner 
30130f712c9SDave Chinner 		/*
30230f712c9SDave Chinner 		 * log the initialisation that is about to take place as an
30330f712c9SDave Chinner 		 * logical operation. This means the transaction does not
30430f712c9SDave Chinner 		 * need to log the physical changes to the inode buffers as log
30530f712c9SDave Chinner 		 * recovery will know what initialisation is actually needed.
30630f712c9SDave Chinner 		 * Hence we only need to log the buffers as "ordered" buffers so
30730f712c9SDave Chinner 		 * they track in the AIL as if they were physically logged.
30830f712c9SDave Chinner 		 */
30930f712c9SDave Chinner 		if (tp)
310463958afSBrian Foster 			xfs_icreate_log(tp, agno, agbno, icount,
31130f712c9SDave Chinner 					mp->m_sb.sb_inodesize, length, gen);
31230f712c9SDave Chinner 	} else
31330f712c9SDave Chinner 		version = 2;
31430f712c9SDave Chinner 
31530f712c9SDave Chinner 	for (j = 0; j < nbufs; j++) {
31630f712c9SDave Chinner 		/*
31730f712c9SDave Chinner 		 * Get the block.
31830f712c9SDave Chinner 		 */
31930f712c9SDave Chinner 		d = XFS_AGB_TO_DADDR(mp, agno, agbno + (j * blks_per_cluster));
32030f712c9SDave Chinner 		fbuf = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
32130f712c9SDave Chinner 					 mp->m_bsize * blks_per_cluster,
32230f712c9SDave Chinner 					 XBF_UNMAPPED);
32330f712c9SDave Chinner 		if (!fbuf)
3242451337dSDave Chinner 			return -ENOMEM;
32530f712c9SDave Chinner 
32630f712c9SDave Chinner 		/* Initialize the inode buffers and log them appropriately. */
32730f712c9SDave Chinner 		fbuf->b_ops = &xfs_inode_buf_ops;
32830f712c9SDave Chinner 		xfs_buf_zero(fbuf, 0, BBTOB(fbuf->b_length));
32930f712c9SDave Chinner 		for (i = 0; i < inodes_per_cluster; i++) {
33030f712c9SDave Chinner 			int	ioffset = i << mp->m_sb.sb_inodelog;
33130f712c9SDave Chinner 			uint	isize = xfs_dinode_size(version);
33230f712c9SDave Chinner 
33330f712c9SDave Chinner 			free = xfs_make_iptr(mp, fbuf, i);
33430f712c9SDave Chinner 			free->di_magic = cpu_to_be16(XFS_DINODE_MAGIC);
33530f712c9SDave Chinner 			free->di_version = version;
33630f712c9SDave Chinner 			free->di_gen = cpu_to_be32(gen);
33730f712c9SDave Chinner 			free->di_next_unlinked = cpu_to_be32(NULLAGINO);
33830f712c9SDave Chinner 
33930f712c9SDave Chinner 			if (version == 3) {
34030f712c9SDave Chinner 				free->di_ino = cpu_to_be64(ino);
34130f712c9SDave Chinner 				ino++;
342ce748eaaSEric Sandeen 				uuid_copy(&free->di_uuid,
343ce748eaaSEric Sandeen 					  &mp->m_sb.sb_meta_uuid);
34430f712c9SDave Chinner 				xfs_dinode_calc_crc(mp, free);
34530f712c9SDave Chinner 			} else if (tp) {
34630f712c9SDave Chinner 				/* just log the inode core */
34730f712c9SDave Chinner 				xfs_trans_log_buf(tp, fbuf, ioffset,
34830f712c9SDave Chinner 						  ioffset + isize - 1);
34930f712c9SDave Chinner 			}
35030f712c9SDave Chinner 		}
35130f712c9SDave Chinner 
35230f712c9SDave Chinner 		if (tp) {
35330f712c9SDave Chinner 			/*
35430f712c9SDave Chinner 			 * Mark the buffer as an inode allocation buffer so it
35530f712c9SDave Chinner 			 * sticks in AIL at the point of this allocation
35630f712c9SDave Chinner 			 * transaction. This ensures the they are on disk before
35730f712c9SDave Chinner 			 * the tail of the log can be moved past this
35830f712c9SDave Chinner 			 * transaction (i.e. by preventing relogging from moving
35930f712c9SDave Chinner 			 * it forward in the log).
36030f712c9SDave Chinner 			 */
36130f712c9SDave Chinner 			xfs_trans_inode_alloc_buf(tp, fbuf);
36230f712c9SDave Chinner 			if (version == 3) {
36330f712c9SDave Chinner 				/*
36430f712c9SDave Chinner 				 * Mark the buffer as ordered so that they are
36530f712c9SDave Chinner 				 * not physically logged in the transaction but
36630f712c9SDave Chinner 				 * still tracked in the AIL as part of the
36730f712c9SDave Chinner 				 * transaction and pin the log appropriately.
36830f712c9SDave Chinner 				 */
36930f712c9SDave Chinner 				xfs_trans_ordered_buf(tp, fbuf);
37030f712c9SDave Chinner 				xfs_trans_log_buf(tp, fbuf, 0,
37130f712c9SDave Chinner 						  BBTOB(fbuf->b_length) - 1);
37230f712c9SDave Chinner 			}
37330f712c9SDave Chinner 		} else {
37430f712c9SDave Chinner 			fbuf->b_flags |= XBF_DONE;
37530f712c9SDave Chinner 			xfs_buf_delwri_queue(fbuf, buffer_list);
37630f712c9SDave Chinner 			xfs_buf_relse(fbuf);
37730f712c9SDave Chinner 		}
37830f712c9SDave Chinner 	}
37930f712c9SDave Chinner 	return 0;
38030f712c9SDave Chinner }
38130f712c9SDave Chinner 
38230f712c9SDave Chinner /*
38356d1115cSBrian Foster  * Align startino and allocmask for a recently allocated sparse chunk such that
38456d1115cSBrian Foster  * they are fit for insertion (or merge) into the on-disk inode btrees.
38556d1115cSBrian Foster  *
38656d1115cSBrian Foster  * Background:
38756d1115cSBrian Foster  *
38856d1115cSBrian Foster  * When enabled, sparse inode support increases the inode alignment from cluster
38956d1115cSBrian Foster  * size to inode chunk size. This means that the minimum range between two
39056d1115cSBrian Foster  * non-adjacent inode records in the inobt is large enough for a full inode
39156d1115cSBrian Foster  * record. This allows for cluster sized, cluster aligned block allocation
39256d1115cSBrian Foster  * without need to worry about whether the resulting inode record overlaps with
39356d1115cSBrian Foster  * another record in the tree. Without this basic rule, we would have to deal
39456d1115cSBrian Foster  * with the consequences of overlap by potentially undoing recent allocations in
39556d1115cSBrian Foster  * the inode allocation codepath.
39656d1115cSBrian Foster  *
39756d1115cSBrian Foster  * Because of this alignment rule (which is enforced on mount), there are two
39856d1115cSBrian Foster  * inobt possibilities for newly allocated sparse chunks. One is that the
39956d1115cSBrian Foster  * aligned inode record for the chunk covers a range of inodes not already
40056d1115cSBrian Foster  * covered in the inobt (i.e., it is safe to insert a new sparse record). The
40156d1115cSBrian Foster  * other is that a record already exists at the aligned startino that considers
40256d1115cSBrian Foster  * the newly allocated range as sparse. In the latter case, record content is
40356d1115cSBrian Foster  * merged in hope that sparse inode chunks fill to full chunks over time.
40456d1115cSBrian Foster  */
40556d1115cSBrian Foster STATIC void
40656d1115cSBrian Foster xfs_align_sparse_ino(
40756d1115cSBrian Foster 	struct xfs_mount		*mp,
40856d1115cSBrian Foster 	xfs_agino_t			*startino,
40956d1115cSBrian Foster 	uint16_t			*allocmask)
41056d1115cSBrian Foster {
41156d1115cSBrian Foster 	xfs_agblock_t			agbno;
41256d1115cSBrian Foster 	xfs_agblock_t			mod;
41356d1115cSBrian Foster 	int				offset;
41456d1115cSBrian Foster 
41556d1115cSBrian Foster 	agbno = XFS_AGINO_TO_AGBNO(mp, *startino);
41656d1115cSBrian Foster 	mod = agbno % mp->m_sb.sb_inoalignmt;
41756d1115cSBrian Foster 	if (!mod)
41856d1115cSBrian Foster 		return;
41956d1115cSBrian Foster 
42056d1115cSBrian Foster 	/* calculate the inode offset and align startino */
42156d1115cSBrian Foster 	offset = mod << mp->m_sb.sb_inopblog;
42256d1115cSBrian Foster 	*startino -= offset;
42356d1115cSBrian Foster 
42456d1115cSBrian Foster 	/*
42556d1115cSBrian Foster 	 * Since startino has been aligned down, left shift allocmask such that
42656d1115cSBrian Foster 	 * it continues to represent the same physical inodes relative to the
42756d1115cSBrian Foster 	 * new startino.
42856d1115cSBrian Foster 	 */
42956d1115cSBrian Foster 	*allocmask <<= offset / XFS_INODES_PER_HOLEMASK_BIT;
43056d1115cSBrian Foster }
43156d1115cSBrian Foster 
43256d1115cSBrian Foster /*
43356d1115cSBrian Foster  * Determine whether the source inode record can merge into the target. Both
43456d1115cSBrian Foster  * records must be sparse, the inode ranges must match and there must be no
43556d1115cSBrian Foster  * allocation overlap between the records.
43656d1115cSBrian Foster  */
43756d1115cSBrian Foster STATIC bool
43856d1115cSBrian Foster __xfs_inobt_can_merge(
43956d1115cSBrian Foster 	struct xfs_inobt_rec_incore	*trec,	/* tgt record */
44056d1115cSBrian Foster 	struct xfs_inobt_rec_incore	*srec)	/* src record */
44156d1115cSBrian Foster {
44256d1115cSBrian Foster 	uint64_t			talloc;
44356d1115cSBrian Foster 	uint64_t			salloc;
44456d1115cSBrian Foster 
44556d1115cSBrian Foster 	/* records must cover the same inode range */
44656d1115cSBrian Foster 	if (trec->ir_startino != srec->ir_startino)
44756d1115cSBrian Foster 		return false;
44856d1115cSBrian Foster 
44956d1115cSBrian Foster 	/* both records must be sparse */
45056d1115cSBrian Foster 	if (!xfs_inobt_issparse(trec->ir_holemask) ||
45156d1115cSBrian Foster 	    !xfs_inobt_issparse(srec->ir_holemask))
45256d1115cSBrian Foster 		return false;
45356d1115cSBrian Foster 
45456d1115cSBrian Foster 	/* both records must track some inodes */
45556d1115cSBrian Foster 	if (!trec->ir_count || !srec->ir_count)
45656d1115cSBrian Foster 		return false;
45756d1115cSBrian Foster 
45856d1115cSBrian Foster 	/* can't exceed capacity of a full record */
45956d1115cSBrian Foster 	if (trec->ir_count + srec->ir_count > XFS_INODES_PER_CHUNK)
46056d1115cSBrian Foster 		return false;
46156d1115cSBrian Foster 
46256d1115cSBrian Foster 	/* verify there is no allocation overlap */
46356d1115cSBrian Foster 	talloc = xfs_inobt_irec_to_allocmask(trec);
46456d1115cSBrian Foster 	salloc = xfs_inobt_irec_to_allocmask(srec);
46556d1115cSBrian Foster 	if (talloc & salloc)
46656d1115cSBrian Foster 		return false;
46756d1115cSBrian Foster 
46856d1115cSBrian Foster 	return true;
46956d1115cSBrian Foster }
47056d1115cSBrian Foster 
47156d1115cSBrian Foster /*
47256d1115cSBrian Foster  * Merge the source inode record into the target. The caller must call
47356d1115cSBrian Foster  * __xfs_inobt_can_merge() to ensure the merge is valid.
47456d1115cSBrian Foster  */
47556d1115cSBrian Foster STATIC void
47656d1115cSBrian Foster __xfs_inobt_rec_merge(
47756d1115cSBrian Foster 	struct xfs_inobt_rec_incore	*trec,	/* target */
47856d1115cSBrian Foster 	struct xfs_inobt_rec_incore	*srec)	/* src */
47956d1115cSBrian Foster {
48056d1115cSBrian Foster 	ASSERT(trec->ir_startino == srec->ir_startino);
48156d1115cSBrian Foster 
48256d1115cSBrian Foster 	/* combine the counts */
48356d1115cSBrian Foster 	trec->ir_count += srec->ir_count;
48456d1115cSBrian Foster 	trec->ir_freecount += srec->ir_freecount;
48556d1115cSBrian Foster 
48656d1115cSBrian Foster 	/*
48756d1115cSBrian Foster 	 * Merge the holemask and free mask. For both fields, 0 bits refer to
48856d1115cSBrian Foster 	 * allocated inodes. We combine the allocated ranges with bitwise AND.
48956d1115cSBrian Foster 	 */
49056d1115cSBrian Foster 	trec->ir_holemask &= srec->ir_holemask;
49156d1115cSBrian Foster 	trec->ir_free &= srec->ir_free;
49256d1115cSBrian Foster }
49356d1115cSBrian Foster 
49456d1115cSBrian Foster /*
49556d1115cSBrian Foster  * Insert a new sparse inode chunk into the associated inode btree. The inode
49656d1115cSBrian Foster  * record for the sparse chunk is pre-aligned to a startino that should match
49756d1115cSBrian Foster  * any pre-existing sparse inode record in the tree. This allows sparse chunks
49856d1115cSBrian Foster  * to fill over time.
49956d1115cSBrian Foster  *
50056d1115cSBrian Foster  * This function supports two modes of handling preexisting records depending on
50156d1115cSBrian Foster  * the merge flag. If merge is true, the provided record is merged with the
50256d1115cSBrian Foster  * existing record and updated in place. The merged record is returned in nrec.
50356d1115cSBrian Foster  * If merge is false, an existing record is replaced with the provided record.
50456d1115cSBrian Foster  * If no preexisting record exists, the provided record is always inserted.
50556d1115cSBrian Foster  *
50656d1115cSBrian Foster  * It is considered corruption if a merge is requested and not possible. Given
50756d1115cSBrian Foster  * the sparse inode alignment constraints, this should never happen.
50856d1115cSBrian Foster  */
50956d1115cSBrian Foster STATIC int
51056d1115cSBrian Foster xfs_inobt_insert_sprec(
51156d1115cSBrian Foster 	struct xfs_mount		*mp,
51256d1115cSBrian Foster 	struct xfs_trans		*tp,
51356d1115cSBrian Foster 	struct xfs_buf			*agbp,
51456d1115cSBrian Foster 	int				btnum,
51556d1115cSBrian Foster 	struct xfs_inobt_rec_incore	*nrec,	/* in/out: new/merged rec. */
51656d1115cSBrian Foster 	bool				merge)	/* merge or replace */
51756d1115cSBrian Foster {
51856d1115cSBrian Foster 	struct xfs_btree_cur		*cur;
51956d1115cSBrian Foster 	struct xfs_agi			*agi = XFS_BUF_TO_AGI(agbp);
52056d1115cSBrian Foster 	xfs_agnumber_t			agno = be32_to_cpu(agi->agi_seqno);
52156d1115cSBrian Foster 	int				error;
52256d1115cSBrian Foster 	int				i;
52356d1115cSBrian Foster 	struct xfs_inobt_rec_incore	rec;
52456d1115cSBrian Foster 
52556d1115cSBrian Foster 	cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, btnum);
52656d1115cSBrian Foster 
52756d1115cSBrian Foster 	/* the new record is pre-aligned so we know where to look */
52856d1115cSBrian Foster 	error = xfs_inobt_lookup(cur, nrec->ir_startino, XFS_LOOKUP_EQ, &i);
52956d1115cSBrian Foster 	if (error)
53056d1115cSBrian Foster 		goto error;
53156d1115cSBrian Foster 	/* if nothing there, insert a new record and return */
53256d1115cSBrian Foster 	if (i == 0) {
53356d1115cSBrian Foster 		error = xfs_inobt_insert_rec(cur, nrec->ir_holemask,
53456d1115cSBrian Foster 					     nrec->ir_count, nrec->ir_freecount,
53556d1115cSBrian Foster 					     nrec->ir_free, &i);
53656d1115cSBrian Foster 		if (error)
53756d1115cSBrian Foster 			goto error;
53856d1115cSBrian Foster 		XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error);
53956d1115cSBrian Foster 
54056d1115cSBrian Foster 		goto out;
54156d1115cSBrian Foster 	}
54256d1115cSBrian Foster 
54356d1115cSBrian Foster 	/*
54456d1115cSBrian Foster 	 * A record exists at this startino. Merge or replace the record
54556d1115cSBrian Foster 	 * depending on what we've been asked to do.
54656d1115cSBrian Foster 	 */
54756d1115cSBrian Foster 	if (merge) {
54856d1115cSBrian Foster 		error = xfs_inobt_get_rec(cur, &rec, &i);
54956d1115cSBrian Foster 		if (error)
55056d1115cSBrian Foster 			goto error;
55156d1115cSBrian Foster 		XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error);
55256d1115cSBrian Foster 		XFS_WANT_CORRUPTED_GOTO(mp,
55356d1115cSBrian Foster 					rec.ir_startino == nrec->ir_startino,
55456d1115cSBrian Foster 					error);
55556d1115cSBrian Foster 
55656d1115cSBrian Foster 		/*
55756d1115cSBrian Foster 		 * This should never fail. If we have coexisting records that
55856d1115cSBrian Foster 		 * cannot merge, something is seriously wrong.
55956d1115cSBrian Foster 		 */
56056d1115cSBrian Foster 		XFS_WANT_CORRUPTED_GOTO(mp, __xfs_inobt_can_merge(nrec, &rec),
56156d1115cSBrian Foster 					error);
56256d1115cSBrian Foster 
56356d1115cSBrian Foster 		trace_xfs_irec_merge_pre(mp, agno, rec.ir_startino,
56456d1115cSBrian Foster 					 rec.ir_holemask, nrec->ir_startino,
56556d1115cSBrian Foster 					 nrec->ir_holemask);
56656d1115cSBrian Foster 
56756d1115cSBrian Foster 		/* merge to nrec to output the updated record */
56856d1115cSBrian Foster 		__xfs_inobt_rec_merge(nrec, &rec);
56956d1115cSBrian Foster 
57056d1115cSBrian Foster 		trace_xfs_irec_merge_post(mp, agno, nrec->ir_startino,
57156d1115cSBrian Foster 					  nrec->ir_holemask);
57256d1115cSBrian Foster 
57356d1115cSBrian Foster 		error = xfs_inobt_rec_check_count(mp, nrec);
57456d1115cSBrian Foster 		if (error)
57556d1115cSBrian Foster 			goto error;
57656d1115cSBrian Foster 	}
57756d1115cSBrian Foster 
57856d1115cSBrian Foster 	error = xfs_inobt_update(cur, nrec);
57956d1115cSBrian Foster 	if (error)
58056d1115cSBrian Foster 		goto error;
58156d1115cSBrian Foster 
58256d1115cSBrian Foster out:
58356d1115cSBrian Foster 	xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
58456d1115cSBrian Foster 	return 0;
58556d1115cSBrian Foster error:
58656d1115cSBrian Foster 	xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
58756d1115cSBrian Foster 	return error;
58856d1115cSBrian Foster }
58956d1115cSBrian Foster 
59056d1115cSBrian Foster /*
59130f712c9SDave Chinner  * Allocate new inodes in the allocation group specified by agbp.
59230f712c9SDave Chinner  * Return 0 for success, else error code.
59330f712c9SDave Chinner  */
59430f712c9SDave Chinner STATIC int				/* error code or 0 */
59530f712c9SDave Chinner xfs_ialloc_ag_alloc(
59630f712c9SDave Chinner 	xfs_trans_t	*tp,		/* transaction pointer */
59730f712c9SDave Chinner 	xfs_buf_t	*agbp,		/* alloc group buffer */
59830f712c9SDave Chinner 	int		*alloc)
59930f712c9SDave Chinner {
60030f712c9SDave Chinner 	xfs_agi_t	*agi;		/* allocation group header */
60130f712c9SDave Chinner 	xfs_alloc_arg_t	args;		/* allocation argument structure */
60230f712c9SDave Chinner 	xfs_agnumber_t	agno;
60330f712c9SDave Chinner 	int		error;
60430f712c9SDave Chinner 	xfs_agino_t	newino;		/* new first inode's number */
60530f712c9SDave Chinner 	xfs_agino_t	newlen;		/* new number of inodes */
60630f712c9SDave Chinner 	int		isaligned = 0;	/* inode allocation at stripe unit */
60730f712c9SDave Chinner 					/* boundary */
60856d1115cSBrian Foster 	uint16_t	allocmask = (uint16_t) -1; /* init. to full chunk */
60956d1115cSBrian Foster 	struct xfs_inobt_rec_incore rec;
61030f712c9SDave Chinner 	struct xfs_perag *pag;
6111cdadee1SBrian Foster 	int		do_sparse = 0;
6121cdadee1SBrian Foster 
61330f712c9SDave Chinner 	memset(&args, 0, sizeof(args));
61430f712c9SDave Chinner 	args.tp = tp;
61530f712c9SDave Chinner 	args.mp = tp->t_mountp;
6161cdadee1SBrian Foster 	args.fsbno = NULLFSBLOCK;
61730f712c9SDave Chinner 
61846fc58daSBrian Foster #ifdef DEBUG
61946fc58daSBrian Foster 	/* randomly do sparse inode allocations */
62046fc58daSBrian Foster 	if (xfs_sb_version_hassparseinodes(&tp->t_mountp->m_sb) &&
62146fc58daSBrian Foster 	    args.mp->m_ialloc_min_blks < args.mp->m_ialloc_blks)
62246fc58daSBrian Foster 		do_sparse = prandom_u32() & 1;
62346fc58daSBrian Foster #endif
62446fc58daSBrian Foster 
62530f712c9SDave Chinner 	/*
62630f712c9SDave Chinner 	 * Locking will ensure that we don't have two callers in here
62730f712c9SDave Chinner 	 * at one time.
62830f712c9SDave Chinner 	 */
62930f712c9SDave Chinner 	newlen = args.mp->m_ialloc_inos;
63030f712c9SDave Chinner 	if (args.mp->m_maxicount &&
63174f9ce1cSGeorge Wang 	    percpu_counter_read_positive(&args.mp->m_icount) + newlen >
632501ab323SDave Chinner 							args.mp->m_maxicount)
6332451337dSDave Chinner 		return -ENOSPC;
63430f712c9SDave Chinner 	args.minlen = args.maxlen = args.mp->m_ialloc_blks;
63530f712c9SDave Chinner 	/*
63630f712c9SDave Chinner 	 * First try to allocate inodes contiguous with the last-allocated
63730f712c9SDave Chinner 	 * chunk of inodes.  If the filesystem is striped, this will fill
63830f712c9SDave Chinner 	 * an entire stripe unit with inodes.
63930f712c9SDave Chinner 	 */
64030f712c9SDave Chinner 	agi = XFS_BUF_TO_AGI(agbp);
64130f712c9SDave Chinner 	newino = be32_to_cpu(agi->agi_newino);
64230f712c9SDave Chinner 	agno = be32_to_cpu(agi->agi_seqno);
64330f712c9SDave Chinner 	args.agbno = XFS_AGINO_TO_AGBNO(args.mp, newino) +
64430f712c9SDave Chinner 		     args.mp->m_ialloc_blks;
6451cdadee1SBrian Foster 	if (do_sparse)
6461cdadee1SBrian Foster 		goto sparse_alloc;
64730f712c9SDave Chinner 	if (likely(newino != NULLAGINO &&
64830f712c9SDave Chinner 		  (args.agbno < be32_to_cpu(agi->agi_length)))) {
64930f712c9SDave Chinner 		args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
65030f712c9SDave Chinner 		args.type = XFS_ALLOCTYPE_THIS_BNO;
65130f712c9SDave Chinner 		args.prod = 1;
65230f712c9SDave Chinner 
65330f712c9SDave Chinner 		/*
65430f712c9SDave Chinner 		 * We need to take into account alignment here to ensure that
65530f712c9SDave Chinner 		 * we don't modify the free list if we fail to have an exact
65630f712c9SDave Chinner 		 * block. If we don't have an exact match, and every oher
65730f712c9SDave Chinner 		 * attempt allocation attempt fails, we'll end up cancelling
65830f712c9SDave Chinner 		 * a dirty transaction and shutting down.
65930f712c9SDave Chinner 		 *
66030f712c9SDave Chinner 		 * For an exact allocation, alignment must be 1,
66130f712c9SDave Chinner 		 * however we need to take cluster alignment into account when
66230f712c9SDave Chinner 		 * fixing up the freelist. Use the minalignslop field to
66330f712c9SDave Chinner 		 * indicate that extra blocks might be required for alignment,
66430f712c9SDave Chinner 		 * but not to use them in the actual exact allocation.
66530f712c9SDave Chinner 		 */
66630f712c9SDave Chinner 		args.alignment = 1;
6677a1df156SDave Chinner 		args.minalignslop = xfs_ialloc_cluster_alignment(args.mp) - 1;
66830f712c9SDave Chinner 
66930f712c9SDave Chinner 		/* Allow space for the inode btree to split. */
67030f712c9SDave Chinner 		args.minleft = args.mp->m_in_maxlevels - 1;
67130f712c9SDave Chinner 		if ((error = xfs_alloc_vextent(&args)))
67230f712c9SDave Chinner 			return error;
67330f712c9SDave Chinner 
67430f712c9SDave Chinner 		/*
67530f712c9SDave Chinner 		 * This request might have dirtied the transaction if the AG can
67630f712c9SDave Chinner 		 * satisfy the request, but the exact block was not available.
67730f712c9SDave Chinner 		 * If the allocation did fail, subsequent requests will relax
67830f712c9SDave Chinner 		 * the exact agbno requirement and increase the alignment
67930f712c9SDave Chinner 		 * instead. It is critical that the total size of the request
68030f712c9SDave Chinner 		 * (len + alignment + slop) does not increase from this point
68130f712c9SDave Chinner 		 * on, so reset minalignslop to ensure it is not included in
68230f712c9SDave Chinner 		 * subsequent requests.
68330f712c9SDave Chinner 		 */
68430f712c9SDave Chinner 		args.minalignslop = 0;
6851cdadee1SBrian Foster 	}
68630f712c9SDave Chinner 
68730f712c9SDave Chinner 	if (unlikely(args.fsbno == NULLFSBLOCK)) {
68830f712c9SDave Chinner 		/*
68930f712c9SDave Chinner 		 * Set the alignment for the allocation.
69030f712c9SDave Chinner 		 * If stripe alignment is turned on then align at stripe unit
69130f712c9SDave Chinner 		 * boundary.
69230f712c9SDave Chinner 		 * If the cluster size is smaller than a filesystem block
69330f712c9SDave Chinner 		 * then we're doing I/O for inodes in filesystem block size
69430f712c9SDave Chinner 		 * pieces, so don't need alignment anyway.
69530f712c9SDave Chinner 		 */
69630f712c9SDave Chinner 		isaligned = 0;
69730f712c9SDave Chinner 		if (args.mp->m_sinoalign) {
69830f712c9SDave Chinner 			ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN));
69930f712c9SDave Chinner 			args.alignment = args.mp->m_dalign;
70030f712c9SDave Chinner 			isaligned = 1;
70130f712c9SDave Chinner 		} else
7027a1df156SDave Chinner 			args.alignment = xfs_ialloc_cluster_alignment(args.mp);
70330f712c9SDave Chinner 		/*
70430f712c9SDave Chinner 		 * Need to figure out where to allocate the inode blocks.
70530f712c9SDave Chinner 		 * Ideally they should be spaced out through the a.g.
70630f712c9SDave Chinner 		 * For now, just allocate blocks up front.
70730f712c9SDave Chinner 		 */
70830f712c9SDave Chinner 		args.agbno = be32_to_cpu(agi->agi_root);
70930f712c9SDave Chinner 		args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
71030f712c9SDave Chinner 		/*
71130f712c9SDave Chinner 		 * Allocate a fixed-size extent of inodes.
71230f712c9SDave Chinner 		 */
71330f712c9SDave Chinner 		args.type = XFS_ALLOCTYPE_NEAR_BNO;
71430f712c9SDave Chinner 		args.prod = 1;
71530f712c9SDave Chinner 		/*
71630f712c9SDave Chinner 		 * Allow space for the inode btree to split.
71730f712c9SDave Chinner 		 */
71830f712c9SDave Chinner 		args.minleft = args.mp->m_in_maxlevels - 1;
71930f712c9SDave Chinner 		if ((error = xfs_alloc_vextent(&args)))
72030f712c9SDave Chinner 			return error;
72130f712c9SDave Chinner 	}
72230f712c9SDave Chinner 
72330f712c9SDave Chinner 	/*
72430f712c9SDave Chinner 	 * If stripe alignment is turned on, then try again with cluster
72530f712c9SDave Chinner 	 * alignment.
72630f712c9SDave Chinner 	 */
72730f712c9SDave Chinner 	if (isaligned && args.fsbno == NULLFSBLOCK) {
72830f712c9SDave Chinner 		args.type = XFS_ALLOCTYPE_NEAR_BNO;
72930f712c9SDave Chinner 		args.agbno = be32_to_cpu(agi->agi_root);
73030f712c9SDave Chinner 		args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
7317a1df156SDave Chinner 		args.alignment = xfs_ialloc_cluster_alignment(args.mp);
73230f712c9SDave Chinner 		if ((error = xfs_alloc_vextent(&args)))
73330f712c9SDave Chinner 			return error;
73430f712c9SDave Chinner 	}
73530f712c9SDave Chinner 
73656d1115cSBrian Foster 	/*
73756d1115cSBrian Foster 	 * Finally, try a sparse allocation if the filesystem supports it and
73856d1115cSBrian Foster 	 * the sparse allocation length is smaller than a full chunk.
73956d1115cSBrian Foster 	 */
74056d1115cSBrian Foster 	if (xfs_sb_version_hassparseinodes(&args.mp->m_sb) &&
74156d1115cSBrian Foster 	    args.mp->m_ialloc_min_blks < args.mp->m_ialloc_blks &&
74256d1115cSBrian Foster 	    args.fsbno == NULLFSBLOCK) {
7431cdadee1SBrian Foster sparse_alloc:
74456d1115cSBrian Foster 		args.type = XFS_ALLOCTYPE_NEAR_BNO;
74556d1115cSBrian Foster 		args.agbno = be32_to_cpu(agi->agi_root);
74656d1115cSBrian Foster 		args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
74756d1115cSBrian Foster 		args.alignment = args.mp->m_sb.sb_spino_align;
74856d1115cSBrian Foster 		args.prod = 1;
74956d1115cSBrian Foster 
75056d1115cSBrian Foster 		args.minlen = args.mp->m_ialloc_min_blks;
75156d1115cSBrian Foster 		args.maxlen = args.minlen;
75256d1115cSBrian Foster 
75356d1115cSBrian Foster 		/*
75456d1115cSBrian Foster 		 * The inode record will be aligned to full chunk size. We must
75556d1115cSBrian Foster 		 * prevent sparse allocation from AG boundaries that result in
75656d1115cSBrian Foster 		 * invalid inode records, such as records that start at agbno 0
75756d1115cSBrian Foster 		 * or extend beyond the AG.
75856d1115cSBrian Foster 		 *
75956d1115cSBrian Foster 		 * Set min agbno to the first aligned, non-zero agbno and max to
76056d1115cSBrian Foster 		 * the last aligned agbno that is at least one full chunk from
76156d1115cSBrian Foster 		 * the end of the AG.
76256d1115cSBrian Foster 		 */
76356d1115cSBrian Foster 		args.min_agbno = args.mp->m_sb.sb_inoalignmt;
76456d1115cSBrian Foster 		args.max_agbno = round_down(args.mp->m_sb.sb_agblocks,
76556d1115cSBrian Foster 					    args.mp->m_sb.sb_inoalignmt) -
76656d1115cSBrian Foster 				 args.mp->m_ialloc_blks;
76756d1115cSBrian Foster 
76856d1115cSBrian Foster 		error = xfs_alloc_vextent(&args);
76956d1115cSBrian Foster 		if (error)
77056d1115cSBrian Foster 			return error;
77156d1115cSBrian Foster 
77256d1115cSBrian Foster 		newlen = args.len << args.mp->m_sb.sb_inopblog;
77346fc58daSBrian Foster 		ASSERT(newlen <= XFS_INODES_PER_CHUNK);
77456d1115cSBrian Foster 		allocmask = (1 << (newlen / XFS_INODES_PER_HOLEMASK_BIT)) - 1;
77556d1115cSBrian Foster 	}
77656d1115cSBrian Foster 
77730f712c9SDave Chinner 	if (args.fsbno == NULLFSBLOCK) {
77830f712c9SDave Chinner 		*alloc = 0;
77930f712c9SDave Chinner 		return 0;
78030f712c9SDave Chinner 	}
78130f712c9SDave Chinner 	ASSERT(args.len == args.minlen);
78230f712c9SDave Chinner 
78330f712c9SDave Chinner 	/*
78430f712c9SDave Chinner 	 * Stamp and write the inode buffers.
78530f712c9SDave Chinner 	 *
78630f712c9SDave Chinner 	 * Seed the new inode cluster with a random generation number. This
78730f712c9SDave Chinner 	 * prevents short-term reuse of generation numbers if a chunk is
78830f712c9SDave Chinner 	 * freed and then immediately reallocated. We use random numbers
78930f712c9SDave Chinner 	 * rather than a linear progression to prevent the next generation
79030f712c9SDave Chinner 	 * number from being easily guessable.
79130f712c9SDave Chinner 	 */
792463958afSBrian Foster 	error = xfs_ialloc_inode_init(args.mp, tp, NULL, newlen, agno,
793463958afSBrian Foster 			args.agbno, args.len, prandom_u32());
79430f712c9SDave Chinner 
79530f712c9SDave Chinner 	if (error)
79630f712c9SDave Chinner 		return error;
79730f712c9SDave Chinner 	/*
79830f712c9SDave Chinner 	 * Convert the results.
79930f712c9SDave Chinner 	 */
80030f712c9SDave Chinner 	newino = XFS_OFFBNO_TO_AGINO(args.mp, args.agbno, 0);
80156d1115cSBrian Foster 
80256d1115cSBrian Foster 	if (xfs_inobt_issparse(~allocmask)) {
80356d1115cSBrian Foster 		/*
80456d1115cSBrian Foster 		 * We've allocated a sparse chunk. Align the startino and mask.
80556d1115cSBrian Foster 		 */
80656d1115cSBrian Foster 		xfs_align_sparse_ino(args.mp, &newino, &allocmask);
80756d1115cSBrian Foster 
80856d1115cSBrian Foster 		rec.ir_startino = newino;
80956d1115cSBrian Foster 		rec.ir_holemask = ~allocmask;
81056d1115cSBrian Foster 		rec.ir_count = newlen;
81156d1115cSBrian Foster 		rec.ir_freecount = newlen;
81256d1115cSBrian Foster 		rec.ir_free = XFS_INOBT_ALL_FREE;
81356d1115cSBrian Foster 
81456d1115cSBrian Foster 		/*
81556d1115cSBrian Foster 		 * Insert the sparse record into the inobt and allow for a merge
81656d1115cSBrian Foster 		 * if necessary. If a merge does occur, rec is updated to the
81756d1115cSBrian Foster 		 * merged record.
81856d1115cSBrian Foster 		 */
81956d1115cSBrian Foster 		error = xfs_inobt_insert_sprec(args.mp, tp, agbp, XFS_BTNUM_INO,
82056d1115cSBrian Foster 					       &rec, true);
82156d1115cSBrian Foster 		if (error == -EFSCORRUPTED) {
82256d1115cSBrian Foster 			xfs_alert(args.mp,
82356d1115cSBrian Foster 	"invalid sparse inode record: ino 0x%llx holemask 0x%x count %u",
82456d1115cSBrian Foster 				  XFS_AGINO_TO_INO(args.mp, agno,
82556d1115cSBrian Foster 						   rec.ir_startino),
82656d1115cSBrian Foster 				  rec.ir_holemask, rec.ir_count);
82756d1115cSBrian Foster 			xfs_force_shutdown(args.mp, SHUTDOWN_CORRUPT_INCORE);
82856d1115cSBrian Foster 		}
82956d1115cSBrian Foster 		if (error)
83056d1115cSBrian Foster 			return error;
83156d1115cSBrian Foster 
83256d1115cSBrian Foster 		/*
83356d1115cSBrian Foster 		 * We can't merge the part we've just allocated as for the inobt
83456d1115cSBrian Foster 		 * due to finobt semantics. The original record may or may not
83556d1115cSBrian Foster 		 * exist independent of whether physical inodes exist in this
83656d1115cSBrian Foster 		 * sparse chunk.
83756d1115cSBrian Foster 		 *
83856d1115cSBrian Foster 		 * We must update the finobt record based on the inobt record.
83956d1115cSBrian Foster 		 * rec contains the fully merged and up to date inobt record
84056d1115cSBrian Foster 		 * from the previous call. Set merge false to replace any
84156d1115cSBrian Foster 		 * existing record with this one.
84256d1115cSBrian Foster 		 */
84356d1115cSBrian Foster 		if (xfs_sb_version_hasfinobt(&args.mp->m_sb)) {
84456d1115cSBrian Foster 			error = xfs_inobt_insert_sprec(args.mp, tp, agbp,
84556d1115cSBrian Foster 						       XFS_BTNUM_FINO, &rec,
84656d1115cSBrian Foster 						       false);
84756d1115cSBrian Foster 			if (error)
84856d1115cSBrian Foster 				return error;
84956d1115cSBrian Foster 		}
85056d1115cSBrian Foster 	} else {
85156d1115cSBrian Foster 		/* full chunk - insert new records to both btrees */
85256d1115cSBrian Foster 		error = xfs_inobt_insert(args.mp, tp, agbp, newino, newlen,
85356d1115cSBrian Foster 					 XFS_BTNUM_INO);
85456d1115cSBrian Foster 		if (error)
85556d1115cSBrian Foster 			return error;
85656d1115cSBrian Foster 
85756d1115cSBrian Foster 		if (xfs_sb_version_hasfinobt(&args.mp->m_sb)) {
85856d1115cSBrian Foster 			error = xfs_inobt_insert(args.mp, tp, agbp, newino,
85956d1115cSBrian Foster 						 newlen, XFS_BTNUM_FINO);
86056d1115cSBrian Foster 			if (error)
86156d1115cSBrian Foster 				return error;
86256d1115cSBrian Foster 		}
86356d1115cSBrian Foster 	}
86456d1115cSBrian Foster 
86556d1115cSBrian Foster 	/*
86656d1115cSBrian Foster 	 * Update AGI counts and newino.
86756d1115cSBrian Foster 	 */
86830f712c9SDave Chinner 	be32_add_cpu(&agi->agi_count, newlen);
86930f712c9SDave Chinner 	be32_add_cpu(&agi->agi_freecount, newlen);
87030f712c9SDave Chinner 	pag = xfs_perag_get(args.mp, agno);
87130f712c9SDave Chinner 	pag->pagi_freecount += newlen;
87230f712c9SDave Chinner 	xfs_perag_put(pag);
87330f712c9SDave Chinner 	agi->agi_newino = cpu_to_be32(newino);
87430f712c9SDave Chinner 
87530f712c9SDave Chinner 	/*
87630f712c9SDave Chinner 	 * Log allocation group header fields
87730f712c9SDave Chinner 	 */
87830f712c9SDave Chinner 	xfs_ialloc_log_agi(tp, agbp,
87930f712c9SDave Chinner 		XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO);
88030f712c9SDave Chinner 	/*
88130f712c9SDave Chinner 	 * Modify/log superblock values for inode count and inode free count.
88230f712c9SDave Chinner 	 */
88330f712c9SDave Chinner 	xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen);
88430f712c9SDave Chinner 	xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen);
88530f712c9SDave Chinner 	*alloc = 1;
88630f712c9SDave Chinner 	return 0;
88730f712c9SDave Chinner }
88830f712c9SDave Chinner 
88930f712c9SDave Chinner STATIC xfs_agnumber_t
89030f712c9SDave Chinner xfs_ialloc_next_ag(
89130f712c9SDave Chinner 	xfs_mount_t	*mp)
89230f712c9SDave Chinner {
89330f712c9SDave Chinner 	xfs_agnumber_t	agno;
89430f712c9SDave Chinner 
89530f712c9SDave Chinner 	spin_lock(&mp->m_agirotor_lock);
89630f712c9SDave Chinner 	agno = mp->m_agirotor;
89730f712c9SDave Chinner 	if (++mp->m_agirotor >= mp->m_maxagi)
89830f712c9SDave Chinner 		mp->m_agirotor = 0;
89930f712c9SDave Chinner 	spin_unlock(&mp->m_agirotor_lock);
90030f712c9SDave Chinner 
90130f712c9SDave Chinner 	return agno;
90230f712c9SDave Chinner }
90330f712c9SDave Chinner 
90430f712c9SDave Chinner /*
90530f712c9SDave Chinner  * Select an allocation group to look for a free inode in, based on the parent
90630f712c9SDave Chinner  * inode and the mode.  Return the allocation group buffer.
90730f712c9SDave Chinner  */
90830f712c9SDave Chinner STATIC xfs_agnumber_t
90930f712c9SDave Chinner xfs_ialloc_ag_select(
91030f712c9SDave Chinner 	xfs_trans_t	*tp,		/* transaction pointer */
91130f712c9SDave Chinner 	xfs_ino_t	parent,		/* parent directory inode number */
91230f712c9SDave Chinner 	umode_t		mode,		/* bits set to indicate file type */
91330f712c9SDave Chinner 	int		okalloc)	/* ok to allocate more space */
91430f712c9SDave Chinner {
91530f712c9SDave Chinner 	xfs_agnumber_t	agcount;	/* number of ag's in the filesystem */
91630f712c9SDave Chinner 	xfs_agnumber_t	agno;		/* current ag number */
91730f712c9SDave Chinner 	int		flags;		/* alloc buffer locking flags */
91830f712c9SDave Chinner 	xfs_extlen_t	ineed;		/* blocks needed for inode allocation */
91930f712c9SDave Chinner 	xfs_extlen_t	longest = 0;	/* longest extent available */
92030f712c9SDave Chinner 	xfs_mount_t	*mp;		/* mount point structure */
92130f712c9SDave Chinner 	int		needspace;	/* file mode implies space allocated */
92230f712c9SDave Chinner 	xfs_perag_t	*pag;		/* per allocation group data */
92330f712c9SDave Chinner 	xfs_agnumber_t	pagno;		/* parent (starting) ag number */
92430f712c9SDave Chinner 	int		error;
92530f712c9SDave Chinner 
92630f712c9SDave Chinner 	/*
92730f712c9SDave Chinner 	 * Files of these types need at least one block if length > 0
92830f712c9SDave Chinner 	 * (and they won't fit in the inode, but that's hard to figure out).
92930f712c9SDave Chinner 	 */
93030f712c9SDave Chinner 	needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode);
93130f712c9SDave Chinner 	mp = tp->t_mountp;
93230f712c9SDave Chinner 	agcount = mp->m_maxagi;
93330f712c9SDave Chinner 	if (S_ISDIR(mode))
93430f712c9SDave Chinner 		pagno = xfs_ialloc_next_ag(mp);
93530f712c9SDave Chinner 	else {
93630f712c9SDave Chinner 		pagno = XFS_INO_TO_AGNO(mp, parent);
93730f712c9SDave Chinner 		if (pagno >= agcount)
93830f712c9SDave Chinner 			pagno = 0;
93930f712c9SDave Chinner 	}
94030f712c9SDave Chinner 
94130f712c9SDave Chinner 	ASSERT(pagno < agcount);
94230f712c9SDave Chinner 
94330f712c9SDave Chinner 	/*
94430f712c9SDave Chinner 	 * Loop through allocation groups, looking for one with a little
94530f712c9SDave Chinner 	 * free space in it.  Note we don't look for free inodes, exactly.
94630f712c9SDave Chinner 	 * Instead, we include whether there is a need to allocate inodes
94730f712c9SDave Chinner 	 * to mean that blocks must be allocated for them,
94830f712c9SDave Chinner 	 * if none are currently free.
94930f712c9SDave Chinner 	 */
95030f712c9SDave Chinner 	agno = pagno;
95130f712c9SDave Chinner 	flags = XFS_ALLOC_FLAG_TRYLOCK;
95230f712c9SDave Chinner 	for (;;) {
95330f712c9SDave Chinner 		pag = xfs_perag_get(mp, agno);
95430f712c9SDave Chinner 		if (!pag->pagi_inodeok) {
95530f712c9SDave Chinner 			xfs_ialloc_next_ag(mp);
95630f712c9SDave Chinner 			goto nextag;
95730f712c9SDave Chinner 		}
95830f712c9SDave Chinner 
95930f712c9SDave Chinner 		if (!pag->pagi_init) {
96030f712c9SDave Chinner 			error = xfs_ialloc_pagi_init(mp, tp, agno);
96130f712c9SDave Chinner 			if (error)
96230f712c9SDave Chinner 				goto nextag;
96330f712c9SDave Chinner 		}
96430f712c9SDave Chinner 
96530f712c9SDave Chinner 		if (pag->pagi_freecount) {
96630f712c9SDave Chinner 			xfs_perag_put(pag);
96730f712c9SDave Chinner 			return agno;
96830f712c9SDave Chinner 		}
96930f712c9SDave Chinner 
97030f712c9SDave Chinner 		if (!okalloc)
97130f712c9SDave Chinner 			goto nextag;
97230f712c9SDave Chinner 
97330f712c9SDave Chinner 		if (!pag->pagf_init) {
97430f712c9SDave Chinner 			error = xfs_alloc_pagf_init(mp, tp, agno, flags);
97530f712c9SDave Chinner 			if (error)
97630f712c9SDave Chinner 				goto nextag;
97730f712c9SDave Chinner 		}
97830f712c9SDave Chinner 
97930f712c9SDave Chinner 		/*
9807a1df156SDave Chinner 		 * Check that there is enough free space for the file plus a
9817a1df156SDave Chinner 		 * chunk of inodes if we need to allocate some. If this is the
9827a1df156SDave Chinner 		 * first pass across the AGs, take into account the potential
9837a1df156SDave Chinner 		 * space needed for alignment of inode chunks when checking the
9847a1df156SDave Chinner 		 * longest contiguous free space in the AG - this prevents us
9857a1df156SDave Chinner 		 * from getting ENOSPC because we have free space larger than
9867a1df156SDave Chinner 		 * m_ialloc_blks but alignment constraints prevent us from using
9877a1df156SDave Chinner 		 * it.
9887a1df156SDave Chinner 		 *
9897a1df156SDave Chinner 		 * If we can't find an AG with space for full alignment slack to
9907a1df156SDave Chinner 		 * be taken into account, we must be near ENOSPC in all AGs.
9917a1df156SDave Chinner 		 * Hence we don't include alignment for the second pass and so
9927a1df156SDave Chinner 		 * if we fail allocation due to alignment issues then it is most
9937a1df156SDave Chinner 		 * likely a real ENOSPC condition.
99430f712c9SDave Chinner 		 */
995066a1884SBrian Foster 		ineed = mp->m_ialloc_min_blks;
9967a1df156SDave Chinner 		if (flags && ineed > 1)
9977a1df156SDave Chinner 			ineed += xfs_ialloc_cluster_alignment(mp);
99830f712c9SDave Chinner 		longest = pag->pagf_longest;
99930f712c9SDave Chinner 		if (!longest)
100030f712c9SDave Chinner 			longest = pag->pagf_flcount > 0;
100130f712c9SDave Chinner 
100230f712c9SDave Chinner 		if (pag->pagf_freeblks >= needspace + ineed &&
100330f712c9SDave Chinner 		    longest >= ineed) {
100430f712c9SDave Chinner 			xfs_perag_put(pag);
100530f712c9SDave Chinner 			return agno;
100630f712c9SDave Chinner 		}
100730f712c9SDave Chinner nextag:
100830f712c9SDave Chinner 		xfs_perag_put(pag);
100930f712c9SDave Chinner 		/*
101030f712c9SDave Chinner 		 * No point in iterating over the rest, if we're shutting
101130f712c9SDave Chinner 		 * down.
101230f712c9SDave Chinner 		 */
101330f712c9SDave Chinner 		if (XFS_FORCED_SHUTDOWN(mp))
101430f712c9SDave Chinner 			return NULLAGNUMBER;
101530f712c9SDave Chinner 		agno++;
101630f712c9SDave Chinner 		if (agno >= agcount)
101730f712c9SDave Chinner 			agno = 0;
101830f712c9SDave Chinner 		if (agno == pagno) {
101930f712c9SDave Chinner 			if (flags == 0)
102030f712c9SDave Chinner 				return NULLAGNUMBER;
102130f712c9SDave Chinner 			flags = 0;
102230f712c9SDave Chinner 		}
102330f712c9SDave Chinner 	}
102430f712c9SDave Chinner }
102530f712c9SDave Chinner 
102630f712c9SDave Chinner /*
102730f712c9SDave Chinner  * Try to retrieve the next record to the left/right from the current one.
102830f712c9SDave Chinner  */
102930f712c9SDave Chinner STATIC int
103030f712c9SDave Chinner xfs_ialloc_next_rec(
103130f712c9SDave Chinner 	struct xfs_btree_cur	*cur,
103230f712c9SDave Chinner 	xfs_inobt_rec_incore_t	*rec,
103330f712c9SDave Chinner 	int			*done,
103430f712c9SDave Chinner 	int			left)
103530f712c9SDave Chinner {
103630f712c9SDave Chinner 	int                     error;
103730f712c9SDave Chinner 	int			i;
103830f712c9SDave Chinner 
103930f712c9SDave Chinner 	if (left)
104030f712c9SDave Chinner 		error = xfs_btree_decrement(cur, 0, &i);
104130f712c9SDave Chinner 	else
104230f712c9SDave Chinner 		error = xfs_btree_increment(cur, 0, &i);
104330f712c9SDave Chinner 
104430f712c9SDave Chinner 	if (error)
104530f712c9SDave Chinner 		return error;
104630f712c9SDave Chinner 	*done = !i;
104730f712c9SDave Chinner 	if (i) {
104830f712c9SDave Chinner 		error = xfs_inobt_get_rec(cur, rec, &i);
104930f712c9SDave Chinner 		if (error)
105030f712c9SDave Chinner 			return error;
10515fb5aeeeSEric Sandeen 		XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
105230f712c9SDave Chinner 	}
105330f712c9SDave Chinner 
105430f712c9SDave Chinner 	return 0;
105530f712c9SDave Chinner }
105630f712c9SDave Chinner 
105730f712c9SDave Chinner STATIC int
105830f712c9SDave Chinner xfs_ialloc_get_rec(
105930f712c9SDave Chinner 	struct xfs_btree_cur	*cur,
106030f712c9SDave Chinner 	xfs_agino_t		agino,
106130f712c9SDave Chinner 	xfs_inobt_rec_incore_t	*rec,
106230f712c9SDave Chinner 	int			*done)
106330f712c9SDave Chinner {
106430f712c9SDave Chinner 	int                     error;
106530f712c9SDave Chinner 	int			i;
106630f712c9SDave Chinner 
106730f712c9SDave Chinner 	error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_EQ, &i);
106830f712c9SDave Chinner 	if (error)
106930f712c9SDave Chinner 		return error;
107030f712c9SDave Chinner 	*done = !i;
107130f712c9SDave Chinner 	if (i) {
107230f712c9SDave Chinner 		error = xfs_inobt_get_rec(cur, rec, &i);
107330f712c9SDave Chinner 		if (error)
107430f712c9SDave Chinner 			return error;
10755fb5aeeeSEric Sandeen 		XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
107630f712c9SDave Chinner 	}
107730f712c9SDave Chinner 
107830f712c9SDave Chinner 	return 0;
107930f712c9SDave Chinner }
108030f712c9SDave Chinner 
108130f712c9SDave Chinner /*
108226dd5217SBrian Foster  * Return the offset of the first free inode in the record. If the inode chunk
108326dd5217SBrian Foster  * is sparsely allocated, we convert the record holemask to inode granularity
108426dd5217SBrian Foster  * and mask off the unallocated regions from the inode free mask.
1085d4cc540bSBrian Foster  */
1086d4cc540bSBrian Foster STATIC int
1087d4cc540bSBrian Foster xfs_inobt_first_free_inode(
1088d4cc540bSBrian Foster 	struct xfs_inobt_rec_incore	*rec)
1089d4cc540bSBrian Foster {
109026dd5217SBrian Foster 	xfs_inofree_t			realfree;
109126dd5217SBrian Foster 
109226dd5217SBrian Foster 	/* if there are no holes, return the first available offset */
109326dd5217SBrian Foster 	if (!xfs_inobt_issparse(rec->ir_holemask))
1094d4cc540bSBrian Foster 		return xfs_lowbit64(rec->ir_free);
109526dd5217SBrian Foster 
109626dd5217SBrian Foster 	realfree = xfs_inobt_irec_to_allocmask(rec);
109726dd5217SBrian Foster 	realfree &= rec->ir_free;
109826dd5217SBrian Foster 
109926dd5217SBrian Foster 	return xfs_lowbit64(realfree);
1100d4cc540bSBrian Foster }
1101d4cc540bSBrian Foster 
1102d4cc540bSBrian Foster /*
110330f712c9SDave Chinner  * Allocate an inode using the inobt-only algorithm.
110430f712c9SDave Chinner  */
110530f712c9SDave Chinner STATIC int
110630f712c9SDave Chinner xfs_dialloc_ag_inobt(
110730f712c9SDave Chinner 	struct xfs_trans	*tp,
110830f712c9SDave Chinner 	struct xfs_buf		*agbp,
110930f712c9SDave Chinner 	xfs_ino_t		parent,
111030f712c9SDave Chinner 	xfs_ino_t		*inop)
111130f712c9SDave Chinner {
111230f712c9SDave Chinner 	struct xfs_mount	*mp = tp->t_mountp;
111330f712c9SDave Chinner 	struct xfs_agi		*agi = XFS_BUF_TO_AGI(agbp);
111430f712c9SDave Chinner 	xfs_agnumber_t		agno = be32_to_cpu(agi->agi_seqno);
111530f712c9SDave Chinner 	xfs_agnumber_t		pagno = XFS_INO_TO_AGNO(mp, parent);
111630f712c9SDave Chinner 	xfs_agino_t		pagino = XFS_INO_TO_AGINO(mp, parent);
111730f712c9SDave Chinner 	struct xfs_perag	*pag;
111830f712c9SDave Chinner 	struct xfs_btree_cur	*cur, *tcur;
111930f712c9SDave Chinner 	struct xfs_inobt_rec_incore rec, trec;
112030f712c9SDave Chinner 	xfs_ino_t		ino;
112130f712c9SDave Chinner 	int			error;
112230f712c9SDave Chinner 	int			offset;
112330f712c9SDave Chinner 	int			i, j;
112430f712c9SDave Chinner 
112530f712c9SDave Chinner 	pag = xfs_perag_get(mp, agno);
112630f712c9SDave Chinner 
112730f712c9SDave Chinner 	ASSERT(pag->pagi_init);
112830f712c9SDave Chinner 	ASSERT(pag->pagi_inodeok);
112930f712c9SDave Chinner 	ASSERT(pag->pagi_freecount > 0);
113030f712c9SDave Chinner 
113130f712c9SDave Chinner  restart_pagno:
113230f712c9SDave Chinner 	cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
113330f712c9SDave Chinner 	/*
113430f712c9SDave Chinner 	 * If pagino is 0 (this is the root inode allocation) use newino.
113530f712c9SDave Chinner 	 * This must work because we've just allocated some.
113630f712c9SDave Chinner 	 */
113730f712c9SDave Chinner 	if (!pagino)
113830f712c9SDave Chinner 		pagino = be32_to_cpu(agi->agi_newino);
113930f712c9SDave Chinner 
114030f712c9SDave Chinner 	error = xfs_check_agi_freecount(cur, agi);
114130f712c9SDave Chinner 	if (error)
114230f712c9SDave Chinner 		goto error0;
114330f712c9SDave Chinner 
114430f712c9SDave Chinner 	/*
114530f712c9SDave Chinner 	 * If in the same AG as the parent, try to get near the parent.
114630f712c9SDave Chinner 	 */
114730f712c9SDave Chinner 	if (pagno == agno) {
114830f712c9SDave Chinner 		int		doneleft;	/* done, to the left */
114930f712c9SDave Chinner 		int		doneright;	/* done, to the right */
115030f712c9SDave Chinner 		int		searchdistance = 10;
115130f712c9SDave Chinner 
115230f712c9SDave Chinner 		error = xfs_inobt_lookup(cur, pagino, XFS_LOOKUP_LE, &i);
115330f712c9SDave Chinner 		if (error)
115430f712c9SDave Chinner 			goto error0;
1155c29aad41SEric Sandeen 		XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
115630f712c9SDave Chinner 
115730f712c9SDave Chinner 		error = xfs_inobt_get_rec(cur, &rec, &j);
115830f712c9SDave Chinner 		if (error)
115930f712c9SDave Chinner 			goto error0;
1160c29aad41SEric Sandeen 		XFS_WANT_CORRUPTED_GOTO(mp, j == 1, error0);
116130f712c9SDave Chinner 
116230f712c9SDave Chinner 		if (rec.ir_freecount > 0) {
116330f712c9SDave Chinner 			/*
116430f712c9SDave Chinner 			 * Found a free inode in the same chunk
116530f712c9SDave Chinner 			 * as the parent, done.
116630f712c9SDave Chinner 			 */
116730f712c9SDave Chinner 			goto alloc_inode;
116830f712c9SDave Chinner 		}
116930f712c9SDave Chinner 
117030f712c9SDave Chinner 
117130f712c9SDave Chinner 		/*
117230f712c9SDave Chinner 		 * In the same AG as parent, but parent's chunk is full.
117330f712c9SDave Chinner 		 */
117430f712c9SDave Chinner 
117530f712c9SDave Chinner 		/* duplicate the cursor, search left & right simultaneously */
117630f712c9SDave Chinner 		error = xfs_btree_dup_cursor(cur, &tcur);
117730f712c9SDave Chinner 		if (error)
117830f712c9SDave Chinner 			goto error0;
117930f712c9SDave Chinner 
118030f712c9SDave Chinner 		/*
118130f712c9SDave Chinner 		 * Skip to last blocks looked up if same parent inode.
118230f712c9SDave Chinner 		 */
118330f712c9SDave Chinner 		if (pagino != NULLAGINO &&
118430f712c9SDave Chinner 		    pag->pagl_pagino == pagino &&
118530f712c9SDave Chinner 		    pag->pagl_leftrec != NULLAGINO &&
118630f712c9SDave Chinner 		    pag->pagl_rightrec != NULLAGINO) {
118730f712c9SDave Chinner 			error = xfs_ialloc_get_rec(tcur, pag->pagl_leftrec,
118830f712c9SDave Chinner 						   &trec, &doneleft);
118930f712c9SDave Chinner 			if (error)
119030f712c9SDave Chinner 				goto error1;
119130f712c9SDave Chinner 
119230f712c9SDave Chinner 			error = xfs_ialloc_get_rec(cur, pag->pagl_rightrec,
119330f712c9SDave Chinner 						   &rec, &doneright);
119430f712c9SDave Chinner 			if (error)
119530f712c9SDave Chinner 				goto error1;
119630f712c9SDave Chinner 		} else {
119730f712c9SDave Chinner 			/* search left with tcur, back up 1 record */
119830f712c9SDave Chinner 			error = xfs_ialloc_next_rec(tcur, &trec, &doneleft, 1);
119930f712c9SDave Chinner 			if (error)
120030f712c9SDave Chinner 				goto error1;
120130f712c9SDave Chinner 
120230f712c9SDave Chinner 			/* search right with cur, go forward 1 record. */
120330f712c9SDave Chinner 			error = xfs_ialloc_next_rec(cur, &rec, &doneright, 0);
120430f712c9SDave Chinner 			if (error)
120530f712c9SDave Chinner 				goto error1;
120630f712c9SDave Chinner 		}
120730f712c9SDave Chinner 
120830f712c9SDave Chinner 		/*
120930f712c9SDave Chinner 		 * Loop until we find an inode chunk with a free inode.
121030f712c9SDave Chinner 		 */
121130f712c9SDave Chinner 		while (!doneleft || !doneright) {
121230f712c9SDave Chinner 			int	useleft;  /* using left inode chunk this time */
121330f712c9SDave Chinner 
121430f712c9SDave Chinner 			if (!--searchdistance) {
121530f712c9SDave Chinner 				/*
121630f712c9SDave Chinner 				 * Not in range - save last search
121730f712c9SDave Chinner 				 * location and allocate a new inode
121830f712c9SDave Chinner 				 */
121930f712c9SDave Chinner 				xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
122030f712c9SDave Chinner 				pag->pagl_leftrec = trec.ir_startino;
122130f712c9SDave Chinner 				pag->pagl_rightrec = rec.ir_startino;
122230f712c9SDave Chinner 				pag->pagl_pagino = pagino;
122330f712c9SDave Chinner 				goto newino;
122430f712c9SDave Chinner 			}
122530f712c9SDave Chinner 
122630f712c9SDave Chinner 			/* figure out the closer block if both are valid. */
122730f712c9SDave Chinner 			if (!doneleft && !doneright) {
122830f712c9SDave Chinner 				useleft = pagino -
122930f712c9SDave Chinner 				 (trec.ir_startino + XFS_INODES_PER_CHUNK - 1) <
123030f712c9SDave Chinner 				  rec.ir_startino - pagino;
123130f712c9SDave Chinner 			} else {
123230f712c9SDave Chinner 				useleft = !doneleft;
123330f712c9SDave Chinner 			}
123430f712c9SDave Chinner 
123530f712c9SDave Chinner 			/* free inodes to the left? */
123630f712c9SDave Chinner 			if (useleft && trec.ir_freecount) {
123730f712c9SDave Chinner 				rec = trec;
123830f712c9SDave Chinner 				xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
123930f712c9SDave Chinner 				cur = tcur;
124030f712c9SDave Chinner 
124130f712c9SDave Chinner 				pag->pagl_leftrec = trec.ir_startino;
124230f712c9SDave Chinner 				pag->pagl_rightrec = rec.ir_startino;
124330f712c9SDave Chinner 				pag->pagl_pagino = pagino;
124430f712c9SDave Chinner 				goto alloc_inode;
124530f712c9SDave Chinner 			}
124630f712c9SDave Chinner 
124730f712c9SDave Chinner 			/* free inodes to the right? */
124830f712c9SDave Chinner 			if (!useleft && rec.ir_freecount) {
124930f712c9SDave Chinner 				xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
125030f712c9SDave Chinner 
125130f712c9SDave Chinner 				pag->pagl_leftrec = trec.ir_startino;
125230f712c9SDave Chinner 				pag->pagl_rightrec = rec.ir_startino;
125330f712c9SDave Chinner 				pag->pagl_pagino = pagino;
125430f712c9SDave Chinner 				goto alloc_inode;
125530f712c9SDave Chinner 			}
125630f712c9SDave Chinner 
125730f712c9SDave Chinner 			/* get next record to check */
125830f712c9SDave Chinner 			if (useleft) {
125930f712c9SDave Chinner 				error = xfs_ialloc_next_rec(tcur, &trec,
126030f712c9SDave Chinner 								 &doneleft, 1);
126130f712c9SDave Chinner 			} else {
126230f712c9SDave Chinner 				error = xfs_ialloc_next_rec(cur, &rec,
126330f712c9SDave Chinner 								 &doneright, 0);
126430f712c9SDave Chinner 			}
126530f712c9SDave Chinner 			if (error)
126630f712c9SDave Chinner 				goto error1;
126730f712c9SDave Chinner 		}
126830f712c9SDave Chinner 
126930f712c9SDave Chinner 		/*
127030f712c9SDave Chinner 		 * We've reached the end of the btree. because
127130f712c9SDave Chinner 		 * we are only searching a small chunk of the
127230f712c9SDave Chinner 		 * btree each search, there is obviously free
127330f712c9SDave Chinner 		 * inodes closer to the parent inode than we
127430f712c9SDave Chinner 		 * are now. restart the search again.
127530f712c9SDave Chinner 		 */
127630f712c9SDave Chinner 		pag->pagl_pagino = NULLAGINO;
127730f712c9SDave Chinner 		pag->pagl_leftrec = NULLAGINO;
127830f712c9SDave Chinner 		pag->pagl_rightrec = NULLAGINO;
127930f712c9SDave Chinner 		xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
128030f712c9SDave Chinner 		xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
128130f712c9SDave Chinner 		goto restart_pagno;
128230f712c9SDave Chinner 	}
128330f712c9SDave Chinner 
128430f712c9SDave Chinner 	/*
128530f712c9SDave Chinner 	 * In a different AG from the parent.
128630f712c9SDave Chinner 	 * See if the most recently allocated block has any free.
128730f712c9SDave Chinner 	 */
128830f712c9SDave Chinner newino:
128930f712c9SDave Chinner 	if (agi->agi_newino != cpu_to_be32(NULLAGINO)) {
129030f712c9SDave Chinner 		error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino),
129130f712c9SDave Chinner 					 XFS_LOOKUP_EQ, &i);
129230f712c9SDave Chinner 		if (error)
129330f712c9SDave Chinner 			goto error0;
129430f712c9SDave Chinner 
129530f712c9SDave Chinner 		if (i == 1) {
129630f712c9SDave Chinner 			error = xfs_inobt_get_rec(cur, &rec, &j);
129730f712c9SDave Chinner 			if (error)
129830f712c9SDave Chinner 				goto error0;
129930f712c9SDave Chinner 
130030f712c9SDave Chinner 			if (j == 1 && rec.ir_freecount > 0) {
130130f712c9SDave Chinner 				/*
130230f712c9SDave Chinner 				 * The last chunk allocated in the group
130330f712c9SDave Chinner 				 * still has a free inode.
130430f712c9SDave Chinner 				 */
130530f712c9SDave Chinner 				goto alloc_inode;
130630f712c9SDave Chinner 			}
130730f712c9SDave Chinner 		}
130830f712c9SDave Chinner 	}
130930f712c9SDave Chinner 
131030f712c9SDave Chinner 	/*
131130f712c9SDave Chinner 	 * None left in the last group, search the whole AG
131230f712c9SDave Chinner 	 */
131330f712c9SDave Chinner 	error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
131430f712c9SDave Chinner 	if (error)
131530f712c9SDave Chinner 		goto error0;
1316c29aad41SEric Sandeen 	XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
131730f712c9SDave Chinner 
131830f712c9SDave Chinner 	for (;;) {
131930f712c9SDave Chinner 		error = xfs_inobt_get_rec(cur, &rec, &i);
132030f712c9SDave Chinner 		if (error)
132130f712c9SDave Chinner 			goto error0;
1322c29aad41SEric Sandeen 		XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
132330f712c9SDave Chinner 		if (rec.ir_freecount > 0)
132430f712c9SDave Chinner 			break;
132530f712c9SDave Chinner 		error = xfs_btree_increment(cur, 0, &i);
132630f712c9SDave Chinner 		if (error)
132730f712c9SDave Chinner 			goto error0;
1328c29aad41SEric Sandeen 		XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
132930f712c9SDave Chinner 	}
133030f712c9SDave Chinner 
133130f712c9SDave Chinner alloc_inode:
1332d4cc540bSBrian Foster 	offset = xfs_inobt_first_free_inode(&rec);
133330f712c9SDave Chinner 	ASSERT(offset >= 0);
133430f712c9SDave Chinner 	ASSERT(offset < XFS_INODES_PER_CHUNK);
133530f712c9SDave Chinner 	ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
133630f712c9SDave Chinner 				   XFS_INODES_PER_CHUNK) == 0);
133730f712c9SDave Chinner 	ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
133830f712c9SDave Chinner 	rec.ir_free &= ~XFS_INOBT_MASK(offset);
133930f712c9SDave Chinner 	rec.ir_freecount--;
134030f712c9SDave Chinner 	error = xfs_inobt_update(cur, &rec);
134130f712c9SDave Chinner 	if (error)
134230f712c9SDave Chinner 		goto error0;
134330f712c9SDave Chinner 	be32_add_cpu(&agi->agi_freecount, -1);
134430f712c9SDave Chinner 	xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
134530f712c9SDave Chinner 	pag->pagi_freecount--;
134630f712c9SDave Chinner 
134730f712c9SDave Chinner 	error = xfs_check_agi_freecount(cur, agi);
134830f712c9SDave Chinner 	if (error)
134930f712c9SDave Chinner 		goto error0;
135030f712c9SDave Chinner 
135130f712c9SDave Chinner 	xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
135230f712c9SDave Chinner 	xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
135330f712c9SDave Chinner 	xfs_perag_put(pag);
135430f712c9SDave Chinner 	*inop = ino;
135530f712c9SDave Chinner 	return 0;
135630f712c9SDave Chinner error1:
135730f712c9SDave Chinner 	xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
135830f712c9SDave Chinner error0:
135930f712c9SDave Chinner 	xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
136030f712c9SDave Chinner 	xfs_perag_put(pag);
136130f712c9SDave Chinner 	return error;
136230f712c9SDave Chinner }
136330f712c9SDave Chinner 
136430f712c9SDave Chinner /*
136530f712c9SDave Chinner  * Use the free inode btree to allocate an inode based on distance from the
136630f712c9SDave Chinner  * parent. Note that the provided cursor may be deleted and replaced.
136730f712c9SDave Chinner  */
136830f712c9SDave Chinner STATIC int
136930f712c9SDave Chinner xfs_dialloc_ag_finobt_near(
137030f712c9SDave Chinner 	xfs_agino_t			pagino,
137130f712c9SDave Chinner 	struct xfs_btree_cur		**ocur,
137230f712c9SDave Chinner 	struct xfs_inobt_rec_incore	*rec)
137330f712c9SDave Chinner {
137430f712c9SDave Chinner 	struct xfs_btree_cur		*lcur = *ocur;	/* left search cursor */
137530f712c9SDave Chinner 	struct xfs_btree_cur		*rcur;	/* right search cursor */
137630f712c9SDave Chinner 	struct xfs_inobt_rec_incore	rrec;
137730f712c9SDave Chinner 	int				error;
137830f712c9SDave Chinner 	int				i, j;
137930f712c9SDave Chinner 
138030f712c9SDave Chinner 	error = xfs_inobt_lookup(lcur, pagino, XFS_LOOKUP_LE, &i);
138130f712c9SDave Chinner 	if (error)
138230f712c9SDave Chinner 		return error;
138330f712c9SDave Chinner 
138430f712c9SDave Chinner 	if (i == 1) {
138530f712c9SDave Chinner 		error = xfs_inobt_get_rec(lcur, rec, &i);
138630f712c9SDave Chinner 		if (error)
138730f712c9SDave Chinner 			return error;
13885fb5aeeeSEric Sandeen 		XFS_WANT_CORRUPTED_RETURN(lcur->bc_mp, i == 1);
138930f712c9SDave Chinner 
139030f712c9SDave Chinner 		/*
139130f712c9SDave Chinner 		 * See if we've landed in the parent inode record. The finobt
139230f712c9SDave Chinner 		 * only tracks chunks with at least one free inode, so record
139330f712c9SDave Chinner 		 * existence is enough.
139430f712c9SDave Chinner 		 */
139530f712c9SDave Chinner 		if (pagino >= rec->ir_startino &&
139630f712c9SDave Chinner 		    pagino < (rec->ir_startino + XFS_INODES_PER_CHUNK))
139730f712c9SDave Chinner 			return 0;
139830f712c9SDave Chinner 	}
139930f712c9SDave Chinner 
140030f712c9SDave Chinner 	error = xfs_btree_dup_cursor(lcur, &rcur);
140130f712c9SDave Chinner 	if (error)
140230f712c9SDave Chinner 		return error;
140330f712c9SDave Chinner 
140430f712c9SDave Chinner 	error = xfs_inobt_lookup(rcur, pagino, XFS_LOOKUP_GE, &j);
140530f712c9SDave Chinner 	if (error)
140630f712c9SDave Chinner 		goto error_rcur;
140730f712c9SDave Chinner 	if (j == 1) {
140830f712c9SDave Chinner 		error = xfs_inobt_get_rec(rcur, &rrec, &j);
140930f712c9SDave Chinner 		if (error)
141030f712c9SDave Chinner 			goto error_rcur;
1411c29aad41SEric Sandeen 		XFS_WANT_CORRUPTED_GOTO(lcur->bc_mp, j == 1, error_rcur);
141230f712c9SDave Chinner 	}
141330f712c9SDave Chinner 
1414c29aad41SEric Sandeen 	XFS_WANT_CORRUPTED_GOTO(lcur->bc_mp, i == 1 || j == 1, error_rcur);
141530f712c9SDave Chinner 	if (i == 1 && j == 1) {
141630f712c9SDave Chinner 		/*
141730f712c9SDave Chinner 		 * Both the left and right records are valid. Choose the closer
141830f712c9SDave Chinner 		 * inode chunk to the target.
141930f712c9SDave Chinner 		 */
142030f712c9SDave Chinner 		if ((pagino - rec->ir_startino + XFS_INODES_PER_CHUNK - 1) >
142130f712c9SDave Chinner 		    (rrec.ir_startino - pagino)) {
142230f712c9SDave Chinner 			*rec = rrec;
142330f712c9SDave Chinner 			xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR);
142430f712c9SDave Chinner 			*ocur = rcur;
142530f712c9SDave Chinner 		} else {
142630f712c9SDave Chinner 			xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR);
142730f712c9SDave Chinner 		}
142830f712c9SDave Chinner 	} else if (j == 1) {
142930f712c9SDave Chinner 		/* only the right record is valid */
143030f712c9SDave Chinner 		*rec = rrec;
143130f712c9SDave Chinner 		xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR);
143230f712c9SDave Chinner 		*ocur = rcur;
143330f712c9SDave Chinner 	} else if (i == 1) {
143430f712c9SDave Chinner 		/* only the left record is valid */
143530f712c9SDave Chinner 		xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR);
143630f712c9SDave Chinner 	}
143730f712c9SDave Chinner 
143830f712c9SDave Chinner 	return 0;
143930f712c9SDave Chinner 
144030f712c9SDave Chinner error_rcur:
144130f712c9SDave Chinner 	xfs_btree_del_cursor(rcur, XFS_BTREE_ERROR);
144230f712c9SDave Chinner 	return error;
144330f712c9SDave Chinner }
144430f712c9SDave Chinner 
144530f712c9SDave Chinner /*
144630f712c9SDave Chinner  * Use the free inode btree to find a free inode based on a newino hint. If
144730f712c9SDave Chinner  * the hint is NULL, find the first free inode in the AG.
144830f712c9SDave Chinner  */
144930f712c9SDave Chinner STATIC int
145030f712c9SDave Chinner xfs_dialloc_ag_finobt_newino(
145130f712c9SDave Chinner 	struct xfs_agi			*agi,
145230f712c9SDave Chinner 	struct xfs_btree_cur		*cur,
145330f712c9SDave Chinner 	struct xfs_inobt_rec_incore	*rec)
145430f712c9SDave Chinner {
145530f712c9SDave Chinner 	int error;
145630f712c9SDave Chinner 	int i;
145730f712c9SDave Chinner 
145830f712c9SDave Chinner 	if (agi->agi_newino != cpu_to_be32(NULLAGINO)) {
1459e68ed775SDave Chinner 		error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino),
1460e68ed775SDave Chinner 					 XFS_LOOKUP_EQ, &i);
146130f712c9SDave Chinner 		if (error)
146230f712c9SDave Chinner 			return error;
146330f712c9SDave Chinner 		if (i == 1) {
146430f712c9SDave Chinner 			error = xfs_inobt_get_rec(cur, rec, &i);
146530f712c9SDave Chinner 			if (error)
146630f712c9SDave Chinner 				return error;
14675fb5aeeeSEric Sandeen 			XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
146830f712c9SDave Chinner 			return 0;
146930f712c9SDave Chinner 		}
147030f712c9SDave Chinner 	}
147130f712c9SDave Chinner 
147230f712c9SDave Chinner 	/*
147330f712c9SDave Chinner 	 * Find the first inode available in the AG.
147430f712c9SDave Chinner 	 */
147530f712c9SDave Chinner 	error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
147630f712c9SDave Chinner 	if (error)
147730f712c9SDave Chinner 		return error;
14785fb5aeeeSEric Sandeen 	XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
147930f712c9SDave Chinner 
148030f712c9SDave Chinner 	error = xfs_inobt_get_rec(cur, rec, &i);
148130f712c9SDave Chinner 	if (error)
148230f712c9SDave Chinner 		return error;
14835fb5aeeeSEric Sandeen 	XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
148430f712c9SDave Chinner 
148530f712c9SDave Chinner 	return 0;
148630f712c9SDave Chinner }
148730f712c9SDave Chinner 
148830f712c9SDave Chinner /*
148930f712c9SDave Chinner  * Update the inobt based on a modification made to the finobt. Also ensure that
149030f712c9SDave Chinner  * the records from both trees are equivalent post-modification.
149130f712c9SDave Chinner  */
149230f712c9SDave Chinner STATIC int
149330f712c9SDave Chinner xfs_dialloc_ag_update_inobt(
149430f712c9SDave Chinner 	struct xfs_btree_cur		*cur,	/* inobt cursor */
149530f712c9SDave Chinner 	struct xfs_inobt_rec_incore	*frec,	/* finobt record */
149630f712c9SDave Chinner 	int				offset) /* inode offset */
149730f712c9SDave Chinner {
149830f712c9SDave Chinner 	struct xfs_inobt_rec_incore	rec;
149930f712c9SDave Chinner 	int				error;
150030f712c9SDave Chinner 	int				i;
150130f712c9SDave Chinner 
150230f712c9SDave Chinner 	error = xfs_inobt_lookup(cur, frec->ir_startino, XFS_LOOKUP_EQ, &i);
150330f712c9SDave Chinner 	if (error)
150430f712c9SDave Chinner 		return error;
15055fb5aeeeSEric Sandeen 	XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
150630f712c9SDave Chinner 
150730f712c9SDave Chinner 	error = xfs_inobt_get_rec(cur, &rec, &i);
150830f712c9SDave Chinner 	if (error)
150930f712c9SDave Chinner 		return error;
15105fb5aeeeSEric Sandeen 	XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
151130f712c9SDave Chinner 	ASSERT((XFS_AGINO_TO_OFFSET(cur->bc_mp, rec.ir_startino) %
151230f712c9SDave Chinner 				   XFS_INODES_PER_CHUNK) == 0);
151330f712c9SDave Chinner 
151430f712c9SDave Chinner 	rec.ir_free &= ~XFS_INOBT_MASK(offset);
151530f712c9SDave Chinner 	rec.ir_freecount--;
151630f712c9SDave Chinner 
15175fb5aeeeSEric Sandeen 	XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, (rec.ir_free == frec->ir_free) &&
151830f712c9SDave Chinner 				  (rec.ir_freecount == frec->ir_freecount));
151930f712c9SDave Chinner 
1520b72091f2Skbuild test robot 	return xfs_inobt_update(cur, &rec);
152130f712c9SDave Chinner }
152230f712c9SDave Chinner 
152330f712c9SDave Chinner /*
152430f712c9SDave Chinner  * Allocate an inode using the free inode btree, if available. Otherwise, fall
152530f712c9SDave Chinner  * back to the inobt search algorithm.
152630f712c9SDave Chinner  *
152730f712c9SDave Chinner  * The caller selected an AG for us, and made sure that free inodes are
152830f712c9SDave Chinner  * available.
152930f712c9SDave Chinner  */
153030f712c9SDave Chinner STATIC int
153130f712c9SDave Chinner xfs_dialloc_ag(
153230f712c9SDave Chinner 	struct xfs_trans	*tp,
153330f712c9SDave Chinner 	struct xfs_buf		*agbp,
153430f712c9SDave Chinner 	xfs_ino_t		parent,
153530f712c9SDave Chinner 	xfs_ino_t		*inop)
153630f712c9SDave Chinner {
153730f712c9SDave Chinner 	struct xfs_mount		*mp = tp->t_mountp;
153830f712c9SDave Chinner 	struct xfs_agi			*agi = XFS_BUF_TO_AGI(agbp);
153930f712c9SDave Chinner 	xfs_agnumber_t			agno = be32_to_cpu(agi->agi_seqno);
154030f712c9SDave Chinner 	xfs_agnumber_t			pagno = XFS_INO_TO_AGNO(mp, parent);
154130f712c9SDave Chinner 	xfs_agino_t			pagino = XFS_INO_TO_AGINO(mp, parent);
154230f712c9SDave Chinner 	struct xfs_perag		*pag;
154330f712c9SDave Chinner 	struct xfs_btree_cur		*cur;	/* finobt cursor */
154430f712c9SDave Chinner 	struct xfs_btree_cur		*icur;	/* inobt cursor */
154530f712c9SDave Chinner 	struct xfs_inobt_rec_incore	rec;
154630f712c9SDave Chinner 	xfs_ino_t			ino;
154730f712c9SDave Chinner 	int				error;
154830f712c9SDave Chinner 	int				offset;
154930f712c9SDave Chinner 	int				i;
155030f712c9SDave Chinner 
155130f712c9SDave Chinner 	if (!xfs_sb_version_hasfinobt(&mp->m_sb))
155230f712c9SDave Chinner 		return xfs_dialloc_ag_inobt(tp, agbp, parent, inop);
155330f712c9SDave Chinner 
155430f712c9SDave Chinner 	pag = xfs_perag_get(mp, agno);
155530f712c9SDave Chinner 
155630f712c9SDave Chinner 	/*
155730f712c9SDave Chinner 	 * If pagino is 0 (this is the root inode allocation) use newino.
155830f712c9SDave Chinner 	 * This must work because we've just allocated some.
155930f712c9SDave Chinner 	 */
156030f712c9SDave Chinner 	if (!pagino)
156130f712c9SDave Chinner 		pagino = be32_to_cpu(agi->agi_newino);
156230f712c9SDave Chinner 
156330f712c9SDave Chinner 	cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_FINO);
156430f712c9SDave Chinner 
156530f712c9SDave Chinner 	error = xfs_check_agi_freecount(cur, agi);
156630f712c9SDave Chinner 	if (error)
156730f712c9SDave Chinner 		goto error_cur;
156830f712c9SDave Chinner 
156930f712c9SDave Chinner 	/*
157030f712c9SDave Chinner 	 * The search algorithm depends on whether we're in the same AG as the
157130f712c9SDave Chinner 	 * parent. If so, find the closest available inode to the parent. If
157230f712c9SDave Chinner 	 * not, consider the agi hint or find the first free inode in the AG.
157330f712c9SDave Chinner 	 */
157430f712c9SDave Chinner 	if (agno == pagno)
157530f712c9SDave Chinner 		error = xfs_dialloc_ag_finobt_near(pagino, &cur, &rec);
157630f712c9SDave Chinner 	else
157730f712c9SDave Chinner 		error = xfs_dialloc_ag_finobt_newino(agi, cur, &rec);
157830f712c9SDave Chinner 	if (error)
157930f712c9SDave Chinner 		goto error_cur;
158030f712c9SDave Chinner 
1581d4cc540bSBrian Foster 	offset = xfs_inobt_first_free_inode(&rec);
158230f712c9SDave Chinner 	ASSERT(offset >= 0);
158330f712c9SDave Chinner 	ASSERT(offset < XFS_INODES_PER_CHUNK);
158430f712c9SDave Chinner 	ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
158530f712c9SDave Chinner 				   XFS_INODES_PER_CHUNK) == 0);
158630f712c9SDave Chinner 	ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
158730f712c9SDave Chinner 
158830f712c9SDave Chinner 	/*
158930f712c9SDave Chinner 	 * Modify or remove the finobt record.
159030f712c9SDave Chinner 	 */
159130f712c9SDave Chinner 	rec.ir_free &= ~XFS_INOBT_MASK(offset);
159230f712c9SDave Chinner 	rec.ir_freecount--;
159330f712c9SDave Chinner 	if (rec.ir_freecount)
159430f712c9SDave Chinner 		error = xfs_inobt_update(cur, &rec);
159530f712c9SDave Chinner 	else
159630f712c9SDave Chinner 		error = xfs_btree_delete(cur, &i);
159730f712c9SDave Chinner 	if (error)
159830f712c9SDave Chinner 		goto error_cur;
159930f712c9SDave Chinner 
160030f712c9SDave Chinner 	/*
160130f712c9SDave Chinner 	 * The finobt has now been updated appropriately. We haven't updated the
160230f712c9SDave Chinner 	 * agi and superblock yet, so we can create an inobt cursor and validate
160330f712c9SDave Chinner 	 * the original freecount. If all is well, make the equivalent update to
160430f712c9SDave Chinner 	 * the inobt using the finobt record and offset information.
160530f712c9SDave Chinner 	 */
160630f712c9SDave Chinner 	icur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
160730f712c9SDave Chinner 
160830f712c9SDave Chinner 	error = xfs_check_agi_freecount(icur, agi);
160930f712c9SDave Chinner 	if (error)
161030f712c9SDave Chinner 		goto error_icur;
161130f712c9SDave Chinner 
161230f712c9SDave Chinner 	error = xfs_dialloc_ag_update_inobt(icur, &rec, offset);
161330f712c9SDave Chinner 	if (error)
161430f712c9SDave Chinner 		goto error_icur;
161530f712c9SDave Chinner 
161630f712c9SDave Chinner 	/*
161730f712c9SDave Chinner 	 * Both trees have now been updated. We must update the perag and
161830f712c9SDave Chinner 	 * superblock before we can check the freecount for each btree.
161930f712c9SDave Chinner 	 */
162030f712c9SDave Chinner 	be32_add_cpu(&agi->agi_freecount, -1);
162130f712c9SDave Chinner 	xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
162230f712c9SDave Chinner 	pag->pagi_freecount--;
162330f712c9SDave Chinner 
162430f712c9SDave Chinner 	xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
162530f712c9SDave Chinner 
162630f712c9SDave Chinner 	error = xfs_check_agi_freecount(icur, agi);
162730f712c9SDave Chinner 	if (error)
162830f712c9SDave Chinner 		goto error_icur;
162930f712c9SDave Chinner 	error = xfs_check_agi_freecount(cur, agi);
163030f712c9SDave Chinner 	if (error)
163130f712c9SDave Chinner 		goto error_icur;
163230f712c9SDave Chinner 
163330f712c9SDave Chinner 	xfs_btree_del_cursor(icur, XFS_BTREE_NOERROR);
163430f712c9SDave Chinner 	xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
163530f712c9SDave Chinner 	xfs_perag_put(pag);
163630f712c9SDave Chinner 	*inop = ino;
163730f712c9SDave Chinner 	return 0;
163830f712c9SDave Chinner 
163930f712c9SDave Chinner error_icur:
164030f712c9SDave Chinner 	xfs_btree_del_cursor(icur, XFS_BTREE_ERROR);
164130f712c9SDave Chinner error_cur:
164230f712c9SDave Chinner 	xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
164330f712c9SDave Chinner 	xfs_perag_put(pag);
164430f712c9SDave Chinner 	return error;
164530f712c9SDave Chinner }
164630f712c9SDave Chinner 
164730f712c9SDave Chinner /*
164830f712c9SDave Chinner  * Allocate an inode on disk.
164930f712c9SDave Chinner  *
165030f712c9SDave Chinner  * Mode is used to tell whether the new inode will need space, and whether it
165130f712c9SDave Chinner  * is a directory.
165230f712c9SDave Chinner  *
165330f712c9SDave Chinner  * This function is designed to be called twice if it has to do an allocation
165430f712c9SDave Chinner  * to make more free inodes.  On the first call, *IO_agbp should be set to NULL.
165530f712c9SDave Chinner  * If an inode is available without having to performn an allocation, an inode
165630f712c9SDave Chinner  * number is returned.  In this case, *IO_agbp is set to NULL.  If an allocation
165730f712c9SDave Chinner  * needs to be done, xfs_dialloc returns the current AGI buffer in *IO_agbp.
165830f712c9SDave Chinner  * The caller should then commit the current transaction, allocate a
165930f712c9SDave Chinner  * new transaction, and call xfs_dialloc() again, passing in the previous value
166030f712c9SDave Chinner  * of *IO_agbp.  IO_agbp should be held across the transactions. Since the AGI
166130f712c9SDave Chinner  * buffer is locked across the two calls, the second call is guaranteed to have
166230f712c9SDave Chinner  * a free inode available.
166330f712c9SDave Chinner  *
166430f712c9SDave Chinner  * Once we successfully pick an inode its number is returned and the on-disk
166530f712c9SDave Chinner  * data structures are updated.  The inode itself is not read in, since doing so
166630f712c9SDave Chinner  * would break ordering constraints with xfs_reclaim.
166730f712c9SDave Chinner  */
166830f712c9SDave Chinner int
166930f712c9SDave Chinner xfs_dialloc(
167030f712c9SDave Chinner 	struct xfs_trans	*tp,
167130f712c9SDave Chinner 	xfs_ino_t		parent,
167230f712c9SDave Chinner 	umode_t			mode,
167330f712c9SDave Chinner 	int			okalloc,
167430f712c9SDave Chinner 	struct xfs_buf		**IO_agbp,
167530f712c9SDave Chinner 	xfs_ino_t		*inop)
167630f712c9SDave Chinner {
167730f712c9SDave Chinner 	struct xfs_mount	*mp = tp->t_mountp;
167830f712c9SDave Chinner 	struct xfs_buf		*agbp;
167930f712c9SDave Chinner 	xfs_agnumber_t		agno;
168030f712c9SDave Chinner 	int			error;
168130f712c9SDave Chinner 	int			ialloced;
168230f712c9SDave Chinner 	int			noroom = 0;
168330f712c9SDave Chinner 	xfs_agnumber_t		start_agno;
168430f712c9SDave Chinner 	struct xfs_perag	*pag;
168530f712c9SDave Chinner 
168630f712c9SDave Chinner 	if (*IO_agbp) {
168730f712c9SDave Chinner 		/*
168830f712c9SDave Chinner 		 * If the caller passes in a pointer to the AGI buffer,
168930f712c9SDave Chinner 		 * continue where we left off before.  In this case, we
169030f712c9SDave Chinner 		 * know that the allocation group has free inodes.
169130f712c9SDave Chinner 		 */
169230f712c9SDave Chinner 		agbp = *IO_agbp;
169330f712c9SDave Chinner 		goto out_alloc;
169430f712c9SDave Chinner 	}
169530f712c9SDave Chinner 
169630f712c9SDave Chinner 	/*
169730f712c9SDave Chinner 	 * We do not have an agbp, so select an initial allocation
169830f712c9SDave Chinner 	 * group for inode allocation.
169930f712c9SDave Chinner 	 */
170030f712c9SDave Chinner 	start_agno = xfs_ialloc_ag_select(tp, parent, mode, okalloc);
170130f712c9SDave Chinner 	if (start_agno == NULLAGNUMBER) {
170230f712c9SDave Chinner 		*inop = NULLFSINO;
170330f712c9SDave Chinner 		return 0;
170430f712c9SDave Chinner 	}
170530f712c9SDave Chinner 
170630f712c9SDave Chinner 	/*
170730f712c9SDave Chinner 	 * If we have already hit the ceiling of inode blocks then clear
170830f712c9SDave Chinner 	 * okalloc so we scan all available agi structures for a free
170930f712c9SDave Chinner 	 * inode.
171074f9ce1cSGeorge Wang 	 *
171174f9ce1cSGeorge Wang 	 * Read rough value of mp->m_icount by percpu_counter_read_positive,
171274f9ce1cSGeorge Wang 	 * which will sacrifice the preciseness but improve the performance.
171330f712c9SDave Chinner 	 */
171430f712c9SDave Chinner 	if (mp->m_maxicount &&
171574f9ce1cSGeorge Wang 	    percpu_counter_read_positive(&mp->m_icount) + mp->m_ialloc_inos
171674f9ce1cSGeorge Wang 							> mp->m_maxicount) {
171730f712c9SDave Chinner 		noroom = 1;
171830f712c9SDave Chinner 		okalloc = 0;
171930f712c9SDave Chinner 	}
172030f712c9SDave Chinner 
172130f712c9SDave Chinner 	/*
172230f712c9SDave Chinner 	 * Loop until we find an allocation group that either has free inodes
172330f712c9SDave Chinner 	 * or in which we can allocate some inodes.  Iterate through the
172430f712c9SDave Chinner 	 * allocation groups upward, wrapping at the end.
172530f712c9SDave Chinner 	 */
172630f712c9SDave Chinner 	agno = start_agno;
172730f712c9SDave Chinner 	for (;;) {
172830f712c9SDave Chinner 		pag = xfs_perag_get(mp, agno);
172930f712c9SDave Chinner 		if (!pag->pagi_inodeok) {
173030f712c9SDave Chinner 			xfs_ialloc_next_ag(mp);
173130f712c9SDave Chinner 			goto nextag;
173230f712c9SDave Chinner 		}
173330f712c9SDave Chinner 
173430f712c9SDave Chinner 		if (!pag->pagi_init) {
173530f712c9SDave Chinner 			error = xfs_ialloc_pagi_init(mp, tp, agno);
173630f712c9SDave Chinner 			if (error)
173730f712c9SDave Chinner 				goto out_error;
173830f712c9SDave Chinner 		}
173930f712c9SDave Chinner 
174030f712c9SDave Chinner 		/*
174130f712c9SDave Chinner 		 * Do a first racy fast path check if this AG is usable.
174230f712c9SDave Chinner 		 */
174330f712c9SDave Chinner 		if (!pag->pagi_freecount && !okalloc)
174430f712c9SDave Chinner 			goto nextag;
174530f712c9SDave Chinner 
174630f712c9SDave Chinner 		/*
174730f712c9SDave Chinner 		 * Then read in the AGI buffer and recheck with the AGI buffer
174830f712c9SDave Chinner 		 * lock held.
174930f712c9SDave Chinner 		 */
175030f712c9SDave Chinner 		error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
175130f712c9SDave Chinner 		if (error)
175230f712c9SDave Chinner 			goto out_error;
175330f712c9SDave Chinner 
175430f712c9SDave Chinner 		if (pag->pagi_freecount) {
175530f712c9SDave Chinner 			xfs_perag_put(pag);
175630f712c9SDave Chinner 			goto out_alloc;
175730f712c9SDave Chinner 		}
175830f712c9SDave Chinner 
175930f712c9SDave Chinner 		if (!okalloc)
176030f712c9SDave Chinner 			goto nextag_relse_buffer;
176130f712c9SDave Chinner 
176230f712c9SDave Chinner 
176330f712c9SDave Chinner 		error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced);
176430f712c9SDave Chinner 		if (error) {
176530f712c9SDave Chinner 			xfs_trans_brelse(tp, agbp);
176630f712c9SDave Chinner 
17672451337dSDave Chinner 			if (error != -ENOSPC)
176830f712c9SDave Chinner 				goto out_error;
176930f712c9SDave Chinner 
177030f712c9SDave Chinner 			xfs_perag_put(pag);
177130f712c9SDave Chinner 			*inop = NULLFSINO;
177230f712c9SDave Chinner 			return 0;
177330f712c9SDave Chinner 		}
177430f712c9SDave Chinner 
177530f712c9SDave Chinner 		if (ialloced) {
177630f712c9SDave Chinner 			/*
177730f712c9SDave Chinner 			 * We successfully allocated some inodes, return
177830f712c9SDave Chinner 			 * the current context to the caller so that it
177930f712c9SDave Chinner 			 * can commit the current transaction and call
178030f712c9SDave Chinner 			 * us again where we left off.
178130f712c9SDave Chinner 			 */
178230f712c9SDave Chinner 			ASSERT(pag->pagi_freecount > 0);
178330f712c9SDave Chinner 			xfs_perag_put(pag);
178430f712c9SDave Chinner 
178530f712c9SDave Chinner 			*IO_agbp = agbp;
178630f712c9SDave Chinner 			*inop = NULLFSINO;
178730f712c9SDave Chinner 			return 0;
178830f712c9SDave Chinner 		}
178930f712c9SDave Chinner 
179030f712c9SDave Chinner nextag_relse_buffer:
179130f712c9SDave Chinner 		xfs_trans_brelse(tp, agbp);
179230f712c9SDave Chinner nextag:
179330f712c9SDave Chinner 		xfs_perag_put(pag);
179430f712c9SDave Chinner 		if (++agno == mp->m_sb.sb_agcount)
179530f712c9SDave Chinner 			agno = 0;
179630f712c9SDave Chinner 		if (agno == start_agno) {
179730f712c9SDave Chinner 			*inop = NULLFSINO;
17982451337dSDave Chinner 			return noroom ? -ENOSPC : 0;
179930f712c9SDave Chinner 		}
180030f712c9SDave Chinner 	}
180130f712c9SDave Chinner 
180230f712c9SDave Chinner out_alloc:
180330f712c9SDave Chinner 	*IO_agbp = NULL;
180430f712c9SDave Chinner 	return xfs_dialloc_ag(tp, agbp, parent, inop);
180530f712c9SDave Chinner out_error:
180630f712c9SDave Chinner 	xfs_perag_put(pag);
180730f712c9SDave Chinner 	return error;
180830f712c9SDave Chinner }
180930f712c9SDave Chinner 
181010ae3dc7SBrian Foster /*
181110ae3dc7SBrian Foster  * Free the blocks of an inode chunk. We must consider that the inode chunk
181210ae3dc7SBrian Foster  * might be sparse and only free the regions that are allocated as part of the
181310ae3dc7SBrian Foster  * chunk.
181410ae3dc7SBrian Foster  */
181510ae3dc7SBrian Foster STATIC void
181610ae3dc7SBrian Foster xfs_difree_inode_chunk(
181710ae3dc7SBrian Foster 	struct xfs_mount		*mp,
181810ae3dc7SBrian Foster 	xfs_agnumber_t			agno,
181910ae3dc7SBrian Foster 	struct xfs_inobt_rec_incore	*rec,
182010ae3dc7SBrian Foster 	struct xfs_bmap_free		*flist)
182110ae3dc7SBrian Foster {
182210ae3dc7SBrian Foster 	xfs_agblock_t	sagbno = XFS_AGINO_TO_AGBNO(mp, rec->ir_startino);
182310ae3dc7SBrian Foster 	int		startidx, endidx;
182410ae3dc7SBrian Foster 	int		nextbit;
182510ae3dc7SBrian Foster 	xfs_agblock_t	agbno;
182610ae3dc7SBrian Foster 	int		contigblk;
182710ae3dc7SBrian Foster 	DECLARE_BITMAP(holemask, XFS_INOBT_HOLEMASK_BITS);
182810ae3dc7SBrian Foster 
182910ae3dc7SBrian Foster 	if (!xfs_inobt_issparse(rec->ir_holemask)) {
183010ae3dc7SBrian Foster 		/* not sparse, calculate extent info directly */
183110ae3dc7SBrian Foster 		xfs_bmap_add_free(XFS_AGB_TO_FSB(mp, agno,
183210ae3dc7SBrian Foster 				  XFS_AGINO_TO_AGBNO(mp, rec->ir_startino)),
183310ae3dc7SBrian Foster 				  mp->m_ialloc_blks, flist, mp);
183410ae3dc7SBrian Foster 		return;
183510ae3dc7SBrian Foster 	}
183610ae3dc7SBrian Foster 
183710ae3dc7SBrian Foster 	/* holemask is only 16-bits (fits in an unsigned long) */
183810ae3dc7SBrian Foster 	ASSERT(sizeof(rec->ir_holemask) <= sizeof(holemask[0]));
183910ae3dc7SBrian Foster 	holemask[0] = rec->ir_holemask;
184010ae3dc7SBrian Foster 
184110ae3dc7SBrian Foster 	/*
184210ae3dc7SBrian Foster 	 * Find contiguous ranges of zeroes (i.e., allocated regions) in the
184310ae3dc7SBrian Foster 	 * holemask and convert the start/end index of each range to an extent.
184410ae3dc7SBrian Foster 	 * We start with the start and end index both pointing at the first 0 in
184510ae3dc7SBrian Foster 	 * the mask.
184610ae3dc7SBrian Foster 	 */
184710ae3dc7SBrian Foster 	startidx = endidx = find_first_zero_bit(holemask,
184810ae3dc7SBrian Foster 						XFS_INOBT_HOLEMASK_BITS);
184910ae3dc7SBrian Foster 	nextbit = startidx + 1;
185010ae3dc7SBrian Foster 	while (startidx < XFS_INOBT_HOLEMASK_BITS) {
185110ae3dc7SBrian Foster 		nextbit = find_next_zero_bit(holemask, XFS_INOBT_HOLEMASK_BITS,
185210ae3dc7SBrian Foster 					     nextbit);
185310ae3dc7SBrian Foster 		/*
185410ae3dc7SBrian Foster 		 * If the next zero bit is contiguous, update the end index of
185510ae3dc7SBrian Foster 		 * the current range and continue.
185610ae3dc7SBrian Foster 		 */
185710ae3dc7SBrian Foster 		if (nextbit != XFS_INOBT_HOLEMASK_BITS &&
185810ae3dc7SBrian Foster 		    nextbit == endidx + 1) {
185910ae3dc7SBrian Foster 			endidx = nextbit;
186010ae3dc7SBrian Foster 			goto next;
186110ae3dc7SBrian Foster 		}
186210ae3dc7SBrian Foster 
186310ae3dc7SBrian Foster 		/*
186410ae3dc7SBrian Foster 		 * nextbit is not contiguous with the current end index. Convert
186510ae3dc7SBrian Foster 		 * the current start/end to an extent and add it to the free
186610ae3dc7SBrian Foster 		 * list.
186710ae3dc7SBrian Foster 		 */
186810ae3dc7SBrian Foster 		agbno = sagbno + (startidx * XFS_INODES_PER_HOLEMASK_BIT) /
186910ae3dc7SBrian Foster 				  mp->m_sb.sb_inopblock;
187010ae3dc7SBrian Foster 		contigblk = ((endidx - startidx + 1) *
187110ae3dc7SBrian Foster 			     XFS_INODES_PER_HOLEMASK_BIT) /
187210ae3dc7SBrian Foster 			    mp->m_sb.sb_inopblock;
187310ae3dc7SBrian Foster 
187410ae3dc7SBrian Foster 		ASSERT(agbno % mp->m_sb.sb_spino_align == 0);
187510ae3dc7SBrian Foster 		ASSERT(contigblk % mp->m_sb.sb_spino_align == 0);
187610ae3dc7SBrian Foster 		xfs_bmap_add_free(XFS_AGB_TO_FSB(mp, agno, agbno), contigblk,
187710ae3dc7SBrian Foster 				  flist, mp);
187810ae3dc7SBrian Foster 
187910ae3dc7SBrian Foster 		/* reset range to current bit and carry on... */
188010ae3dc7SBrian Foster 		startidx = endidx = nextbit;
188110ae3dc7SBrian Foster 
188210ae3dc7SBrian Foster next:
188310ae3dc7SBrian Foster 		nextbit++;
188410ae3dc7SBrian Foster 	}
188510ae3dc7SBrian Foster }
188610ae3dc7SBrian Foster 
188730f712c9SDave Chinner STATIC int
188830f712c9SDave Chinner xfs_difree_inobt(
188930f712c9SDave Chinner 	struct xfs_mount		*mp,
189030f712c9SDave Chinner 	struct xfs_trans		*tp,
189130f712c9SDave Chinner 	struct xfs_buf			*agbp,
189230f712c9SDave Chinner 	xfs_agino_t			agino,
189330f712c9SDave Chinner 	struct xfs_bmap_free		*flist,
189409b56604SBrian Foster 	struct xfs_icluster		*xic,
189530f712c9SDave Chinner 	struct xfs_inobt_rec_incore	*orec)
189630f712c9SDave Chinner {
189730f712c9SDave Chinner 	struct xfs_agi			*agi = XFS_BUF_TO_AGI(agbp);
189830f712c9SDave Chinner 	xfs_agnumber_t			agno = be32_to_cpu(agi->agi_seqno);
189930f712c9SDave Chinner 	struct xfs_perag		*pag;
190030f712c9SDave Chinner 	struct xfs_btree_cur		*cur;
190130f712c9SDave Chinner 	struct xfs_inobt_rec_incore	rec;
190230f712c9SDave Chinner 	int				ilen;
190330f712c9SDave Chinner 	int				error;
190430f712c9SDave Chinner 	int				i;
190530f712c9SDave Chinner 	int				off;
190630f712c9SDave Chinner 
190730f712c9SDave Chinner 	ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC));
190830f712c9SDave Chinner 	ASSERT(XFS_AGINO_TO_AGBNO(mp, agino) < be32_to_cpu(agi->agi_length));
190930f712c9SDave Chinner 
191030f712c9SDave Chinner 	/*
191130f712c9SDave Chinner 	 * Initialize the cursor.
191230f712c9SDave Chinner 	 */
191330f712c9SDave Chinner 	cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
191430f712c9SDave Chinner 
191530f712c9SDave Chinner 	error = xfs_check_agi_freecount(cur, agi);
191630f712c9SDave Chinner 	if (error)
191730f712c9SDave Chinner 		goto error0;
191830f712c9SDave Chinner 
191930f712c9SDave Chinner 	/*
192030f712c9SDave Chinner 	 * Look for the entry describing this inode.
192130f712c9SDave Chinner 	 */
192230f712c9SDave Chinner 	if ((error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i))) {
192330f712c9SDave Chinner 		xfs_warn(mp, "%s: xfs_inobt_lookup() returned error %d.",
192430f712c9SDave Chinner 			__func__, error);
192530f712c9SDave Chinner 		goto error0;
192630f712c9SDave Chinner 	}
1927c29aad41SEric Sandeen 	XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
192830f712c9SDave Chinner 	error = xfs_inobt_get_rec(cur, &rec, &i);
192930f712c9SDave Chinner 	if (error) {
193030f712c9SDave Chinner 		xfs_warn(mp, "%s: xfs_inobt_get_rec() returned error %d.",
193130f712c9SDave Chinner 			__func__, error);
193230f712c9SDave Chinner 		goto error0;
193330f712c9SDave Chinner 	}
1934c29aad41SEric Sandeen 	XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
193530f712c9SDave Chinner 	/*
193630f712c9SDave Chinner 	 * Get the offset in the inode chunk.
193730f712c9SDave Chinner 	 */
193830f712c9SDave Chinner 	off = agino - rec.ir_startino;
193930f712c9SDave Chinner 	ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK);
194030f712c9SDave Chinner 	ASSERT(!(rec.ir_free & XFS_INOBT_MASK(off)));
194130f712c9SDave Chinner 	/*
194230f712c9SDave Chinner 	 * Mark the inode free & increment the count.
194330f712c9SDave Chinner 	 */
194430f712c9SDave Chinner 	rec.ir_free |= XFS_INOBT_MASK(off);
194530f712c9SDave Chinner 	rec.ir_freecount++;
194630f712c9SDave Chinner 
194730f712c9SDave Chinner 	/*
1948999633d3SBrian Foster 	 * When an inode chunk is free, it becomes eligible for removal. Don't
1949999633d3SBrian Foster 	 * remove the chunk if the block size is large enough for multiple inode
1950999633d3SBrian Foster 	 * chunks (that might not be free).
195130f712c9SDave Chinner 	 */
195230f712c9SDave Chinner 	if (!(mp->m_flags & XFS_MOUNT_IKEEP) &&
1953999633d3SBrian Foster 	    rec.ir_free == XFS_INOBT_ALL_FREE &&
1954999633d3SBrian Foster 	    mp->m_sb.sb_inopblock <= XFS_INODES_PER_CHUNK) {
195509b56604SBrian Foster 		xic->deleted = 1;
195609b56604SBrian Foster 		xic->first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino);
195709b56604SBrian Foster 		xic->alloc = xfs_inobt_irec_to_allocmask(&rec);
195830f712c9SDave Chinner 
195930f712c9SDave Chinner 		/*
196030f712c9SDave Chinner 		 * Remove the inode cluster from the AGI B+Tree, adjust the
196130f712c9SDave Chinner 		 * AGI and Superblock inode counts, and mark the disk space
196230f712c9SDave Chinner 		 * to be freed when the transaction is committed.
196330f712c9SDave Chinner 		 */
1964999633d3SBrian Foster 		ilen = rec.ir_freecount;
196530f712c9SDave Chinner 		be32_add_cpu(&agi->agi_count, -ilen);
196630f712c9SDave Chinner 		be32_add_cpu(&agi->agi_freecount, -(ilen - 1));
196730f712c9SDave Chinner 		xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT);
196830f712c9SDave Chinner 		pag = xfs_perag_get(mp, agno);
196930f712c9SDave Chinner 		pag->pagi_freecount -= ilen - 1;
197030f712c9SDave Chinner 		xfs_perag_put(pag);
197130f712c9SDave Chinner 		xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen);
197230f712c9SDave Chinner 		xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1));
197330f712c9SDave Chinner 
197430f712c9SDave Chinner 		if ((error = xfs_btree_delete(cur, &i))) {
197530f712c9SDave Chinner 			xfs_warn(mp, "%s: xfs_btree_delete returned error %d.",
197630f712c9SDave Chinner 				__func__, error);
197730f712c9SDave Chinner 			goto error0;
197830f712c9SDave Chinner 		}
197930f712c9SDave Chinner 
198010ae3dc7SBrian Foster 		xfs_difree_inode_chunk(mp, agno, &rec, flist);
198130f712c9SDave Chinner 	} else {
198209b56604SBrian Foster 		xic->deleted = 0;
198330f712c9SDave Chinner 
198430f712c9SDave Chinner 		error = xfs_inobt_update(cur, &rec);
198530f712c9SDave Chinner 		if (error) {
198630f712c9SDave Chinner 			xfs_warn(mp, "%s: xfs_inobt_update returned error %d.",
198730f712c9SDave Chinner 				__func__, error);
198830f712c9SDave Chinner 			goto error0;
198930f712c9SDave Chinner 		}
199030f712c9SDave Chinner 
199130f712c9SDave Chinner 		/*
199230f712c9SDave Chinner 		 * Change the inode free counts and log the ag/sb changes.
199330f712c9SDave Chinner 		 */
199430f712c9SDave Chinner 		be32_add_cpu(&agi->agi_freecount, 1);
199530f712c9SDave Chinner 		xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
199630f712c9SDave Chinner 		pag = xfs_perag_get(mp, agno);
199730f712c9SDave Chinner 		pag->pagi_freecount++;
199830f712c9SDave Chinner 		xfs_perag_put(pag);
199930f712c9SDave Chinner 		xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1);
200030f712c9SDave Chinner 	}
200130f712c9SDave Chinner 
200230f712c9SDave Chinner 	error = xfs_check_agi_freecount(cur, agi);
200330f712c9SDave Chinner 	if (error)
200430f712c9SDave Chinner 		goto error0;
200530f712c9SDave Chinner 
200630f712c9SDave Chinner 	*orec = rec;
200730f712c9SDave Chinner 	xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
200830f712c9SDave Chinner 	return 0;
200930f712c9SDave Chinner 
201030f712c9SDave Chinner error0:
201130f712c9SDave Chinner 	xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
201230f712c9SDave Chinner 	return error;
201330f712c9SDave Chinner }
201430f712c9SDave Chinner 
201530f712c9SDave Chinner /*
201630f712c9SDave Chinner  * Free an inode in the free inode btree.
201730f712c9SDave Chinner  */
201830f712c9SDave Chinner STATIC int
201930f712c9SDave Chinner xfs_difree_finobt(
202030f712c9SDave Chinner 	struct xfs_mount		*mp,
202130f712c9SDave Chinner 	struct xfs_trans		*tp,
202230f712c9SDave Chinner 	struct xfs_buf			*agbp,
202330f712c9SDave Chinner 	xfs_agino_t			agino,
202430f712c9SDave Chinner 	struct xfs_inobt_rec_incore	*ibtrec) /* inobt record */
202530f712c9SDave Chinner {
202630f712c9SDave Chinner 	struct xfs_agi			*agi = XFS_BUF_TO_AGI(agbp);
202730f712c9SDave Chinner 	xfs_agnumber_t			agno = be32_to_cpu(agi->agi_seqno);
202830f712c9SDave Chinner 	struct xfs_btree_cur		*cur;
202930f712c9SDave Chinner 	struct xfs_inobt_rec_incore	rec;
203030f712c9SDave Chinner 	int				offset = agino - ibtrec->ir_startino;
203130f712c9SDave Chinner 	int				error;
203230f712c9SDave Chinner 	int				i;
203330f712c9SDave Chinner 
203430f712c9SDave Chinner 	cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_FINO);
203530f712c9SDave Chinner 
203630f712c9SDave Chinner 	error = xfs_inobt_lookup(cur, ibtrec->ir_startino, XFS_LOOKUP_EQ, &i);
203730f712c9SDave Chinner 	if (error)
203830f712c9SDave Chinner 		goto error;
203930f712c9SDave Chinner 	if (i == 0) {
204030f712c9SDave Chinner 		/*
204130f712c9SDave Chinner 		 * If the record does not exist in the finobt, we must have just
204230f712c9SDave Chinner 		 * freed an inode in a previously fully allocated chunk. If not,
204330f712c9SDave Chinner 		 * something is out of sync.
204430f712c9SDave Chinner 		 */
2045c29aad41SEric Sandeen 		XFS_WANT_CORRUPTED_GOTO(mp, ibtrec->ir_freecount == 1, error);
204630f712c9SDave Chinner 
20475419040fSBrian Foster 		error = xfs_inobt_insert_rec(cur, ibtrec->ir_holemask,
20485419040fSBrian Foster 					     ibtrec->ir_count,
20495419040fSBrian Foster 					     ibtrec->ir_freecount,
205030f712c9SDave Chinner 					     ibtrec->ir_free, &i);
205130f712c9SDave Chinner 		if (error)
205230f712c9SDave Chinner 			goto error;
205330f712c9SDave Chinner 		ASSERT(i == 1);
205430f712c9SDave Chinner 
205530f712c9SDave Chinner 		goto out;
205630f712c9SDave Chinner 	}
205730f712c9SDave Chinner 
205830f712c9SDave Chinner 	/*
205930f712c9SDave Chinner 	 * Read and update the existing record. We could just copy the ibtrec
206030f712c9SDave Chinner 	 * across here, but that would defeat the purpose of having redundant
206130f712c9SDave Chinner 	 * metadata. By making the modifications independently, we can catch
206230f712c9SDave Chinner 	 * corruptions that we wouldn't see if we just copied from one record
206330f712c9SDave Chinner 	 * to another.
206430f712c9SDave Chinner 	 */
206530f712c9SDave Chinner 	error = xfs_inobt_get_rec(cur, &rec, &i);
206630f712c9SDave Chinner 	if (error)
206730f712c9SDave Chinner 		goto error;
2068c29aad41SEric Sandeen 	XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error);
206930f712c9SDave Chinner 
207030f712c9SDave Chinner 	rec.ir_free |= XFS_INOBT_MASK(offset);
207130f712c9SDave Chinner 	rec.ir_freecount++;
207230f712c9SDave Chinner 
2073c29aad41SEric Sandeen 	XFS_WANT_CORRUPTED_GOTO(mp, (rec.ir_free == ibtrec->ir_free) &&
207430f712c9SDave Chinner 				(rec.ir_freecount == ibtrec->ir_freecount),
207530f712c9SDave Chinner 				error);
207630f712c9SDave Chinner 
207730f712c9SDave Chinner 	/*
207830f712c9SDave Chinner 	 * The content of inobt records should always match between the inobt
207930f712c9SDave Chinner 	 * and finobt. The lifecycle of records in the finobt is different from
208030f712c9SDave Chinner 	 * the inobt in that the finobt only tracks records with at least one
208130f712c9SDave Chinner 	 * free inode. Hence, if all of the inodes are free and we aren't
208230f712c9SDave Chinner 	 * keeping inode chunks permanently on disk, remove the record.
208330f712c9SDave Chinner 	 * Otherwise, update the record with the new information.
2084999633d3SBrian Foster 	 *
2085999633d3SBrian Foster 	 * Note that we currently can't free chunks when the block size is large
2086999633d3SBrian Foster 	 * enough for multiple chunks. Leave the finobt record to remain in sync
2087999633d3SBrian Foster 	 * with the inobt.
208830f712c9SDave Chinner 	 */
2089999633d3SBrian Foster 	if (rec.ir_free == XFS_INOBT_ALL_FREE &&
2090999633d3SBrian Foster 	    mp->m_sb.sb_inopblock <= XFS_INODES_PER_CHUNK &&
209130f712c9SDave Chinner 	    !(mp->m_flags & XFS_MOUNT_IKEEP)) {
209230f712c9SDave Chinner 		error = xfs_btree_delete(cur, &i);
209330f712c9SDave Chinner 		if (error)
209430f712c9SDave Chinner 			goto error;
209530f712c9SDave Chinner 		ASSERT(i == 1);
209630f712c9SDave Chinner 	} else {
209730f712c9SDave Chinner 		error = xfs_inobt_update(cur, &rec);
209830f712c9SDave Chinner 		if (error)
209930f712c9SDave Chinner 			goto error;
210030f712c9SDave Chinner 	}
210130f712c9SDave Chinner 
210230f712c9SDave Chinner out:
210330f712c9SDave Chinner 	error = xfs_check_agi_freecount(cur, agi);
210430f712c9SDave Chinner 	if (error)
210530f712c9SDave Chinner 		goto error;
210630f712c9SDave Chinner 
210730f712c9SDave Chinner 	xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
210830f712c9SDave Chinner 	return 0;
210930f712c9SDave Chinner 
211030f712c9SDave Chinner error:
211130f712c9SDave Chinner 	xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
211230f712c9SDave Chinner 	return error;
211330f712c9SDave Chinner }
211430f712c9SDave Chinner 
211530f712c9SDave Chinner /*
211630f712c9SDave Chinner  * Free disk inode.  Carefully avoids touching the incore inode, all
211730f712c9SDave Chinner  * manipulations incore are the caller's responsibility.
211830f712c9SDave Chinner  * The on-disk inode is not changed by this operation, only the
211930f712c9SDave Chinner  * btree (free inode mask) is changed.
212030f712c9SDave Chinner  */
212130f712c9SDave Chinner int
212230f712c9SDave Chinner xfs_difree(
212330f712c9SDave Chinner 	struct xfs_trans	*tp,		/* transaction pointer */
212430f712c9SDave Chinner 	xfs_ino_t		inode,		/* inode to be freed */
212530f712c9SDave Chinner 	struct xfs_bmap_free	*flist,		/* extents to free */
212609b56604SBrian Foster 	struct xfs_icluster	*xic)	/* cluster info if deleted */
212730f712c9SDave Chinner {
212830f712c9SDave Chinner 	/* REFERENCED */
212930f712c9SDave Chinner 	xfs_agblock_t		agbno;	/* block number containing inode */
213030f712c9SDave Chinner 	struct xfs_buf		*agbp;	/* buffer for allocation group header */
213130f712c9SDave Chinner 	xfs_agino_t		agino;	/* allocation group inode number */
213230f712c9SDave Chinner 	xfs_agnumber_t		agno;	/* allocation group number */
213330f712c9SDave Chinner 	int			error;	/* error return value */
213430f712c9SDave Chinner 	struct xfs_mount	*mp;	/* mount structure for filesystem */
213530f712c9SDave Chinner 	struct xfs_inobt_rec_incore rec;/* btree record */
213630f712c9SDave Chinner 
213730f712c9SDave Chinner 	mp = tp->t_mountp;
213830f712c9SDave Chinner 
213930f712c9SDave Chinner 	/*
214030f712c9SDave Chinner 	 * Break up inode number into its components.
214130f712c9SDave Chinner 	 */
214230f712c9SDave Chinner 	agno = XFS_INO_TO_AGNO(mp, inode);
214330f712c9SDave Chinner 	if (agno >= mp->m_sb.sb_agcount)  {
214430f712c9SDave Chinner 		xfs_warn(mp, "%s: agno >= mp->m_sb.sb_agcount (%d >= %d).",
214530f712c9SDave Chinner 			__func__, agno, mp->m_sb.sb_agcount);
214630f712c9SDave Chinner 		ASSERT(0);
21472451337dSDave Chinner 		return -EINVAL;
214830f712c9SDave Chinner 	}
214930f712c9SDave Chinner 	agino = XFS_INO_TO_AGINO(mp, inode);
215030f712c9SDave Chinner 	if (inode != XFS_AGINO_TO_INO(mp, agno, agino))  {
215130f712c9SDave Chinner 		xfs_warn(mp, "%s: inode != XFS_AGINO_TO_INO() (%llu != %llu).",
215230f712c9SDave Chinner 			__func__, (unsigned long long)inode,
215330f712c9SDave Chinner 			(unsigned long long)XFS_AGINO_TO_INO(mp, agno, agino));
215430f712c9SDave Chinner 		ASSERT(0);
21552451337dSDave Chinner 		return -EINVAL;
215630f712c9SDave Chinner 	}
215730f712c9SDave Chinner 	agbno = XFS_AGINO_TO_AGBNO(mp, agino);
215830f712c9SDave Chinner 	if (agbno >= mp->m_sb.sb_agblocks)  {
215930f712c9SDave Chinner 		xfs_warn(mp, "%s: agbno >= mp->m_sb.sb_agblocks (%d >= %d).",
216030f712c9SDave Chinner 			__func__, agbno, mp->m_sb.sb_agblocks);
216130f712c9SDave Chinner 		ASSERT(0);
21622451337dSDave Chinner 		return -EINVAL;
216330f712c9SDave Chinner 	}
216430f712c9SDave Chinner 	/*
216530f712c9SDave Chinner 	 * Get the allocation group header.
216630f712c9SDave Chinner 	 */
216730f712c9SDave Chinner 	error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
216830f712c9SDave Chinner 	if (error) {
216930f712c9SDave Chinner 		xfs_warn(mp, "%s: xfs_ialloc_read_agi() returned error %d.",
217030f712c9SDave Chinner 			__func__, error);
217130f712c9SDave Chinner 		return error;
217230f712c9SDave Chinner 	}
217330f712c9SDave Chinner 
217430f712c9SDave Chinner 	/*
217530f712c9SDave Chinner 	 * Fix up the inode allocation btree.
217630f712c9SDave Chinner 	 */
217709b56604SBrian Foster 	error = xfs_difree_inobt(mp, tp, agbp, agino, flist, xic, &rec);
217830f712c9SDave Chinner 	if (error)
217930f712c9SDave Chinner 		goto error0;
218030f712c9SDave Chinner 
218130f712c9SDave Chinner 	/*
218230f712c9SDave Chinner 	 * Fix up the free inode btree.
218330f712c9SDave Chinner 	 */
218430f712c9SDave Chinner 	if (xfs_sb_version_hasfinobt(&mp->m_sb)) {
218530f712c9SDave Chinner 		error = xfs_difree_finobt(mp, tp, agbp, agino, &rec);
218630f712c9SDave Chinner 		if (error)
218730f712c9SDave Chinner 			goto error0;
218830f712c9SDave Chinner 	}
218930f712c9SDave Chinner 
219030f712c9SDave Chinner 	return 0;
219130f712c9SDave Chinner 
219230f712c9SDave Chinner error0:
219330f712c9SDave Chinner 	return error;
219430f712c9SDave Chinner }
219530f712c9SDave Chinner 
219630f712c9SDave Chinner STATIC int
219730f712c9SDave Chinner xfs_imap_lookup(
219830f712c9SDave Chinner 	struct xfs_mount	*mp,
219930f712c9SDave Chinner 	struct xfs_trans	*tp,
220030f712c9SDave Chinner 	xfs_agnumber_t		agno,
220130f712c9SDave Chinner 	xfs_agino_t		agino,
220230f712c9SDave Chinner 	xfs_agblock_t		agbno,
220330f712c9SDave Chinner 	xfs_agblock_t		*chunk_agbno,
220430f712c9SDave Chinner 	xfs_agblock_t		*offset_agbno,
220530f712c9SDave Chinner 	int			flags)
220630f712c9SDave Chinner {
220730f712c9SDave Chinner 	struct xfs_inobt_rec_incore rec;
220830f712c9SDave Chinner 	struct xfs_btree_cur	*cur;
220930f712c9SDave Chinner 	struct xfs_buf		*agbp;
221030f712c9SDave Chinner 	int			error;
221130f712c9SDave Chinner 	int			i;
221230f712c9SDave Chinner 
221330f712c9SDave Chinner 	error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
221430f712c9SDave Chinner 	if (error) {
221530f712c9SDave Chinner 		xfs_alert(mp,
221630f712c9SDave Chinner 			"%s: xfs_ialloc_read_agi() returned error %d, agno %d",
221730f712c9SDave Chinner 			__func__, error, agno);
221830f712c9SDave Chinner 		return error;
221930f712c9SDave Chinner 	}
222030f712c9SDave Chinner 
222130f712c9SDave Chinner 	/*
222230f712c9SDave Chinner 	 * Lookup the inode record for the given agino. If the record cannot be
222330f712c9SDave Chinner 	 * found, then it's an invalid inode number and we should abort. Once
222430f712c9SDave Chinner 	 * we have a record, we need to ensure it contains the inode number
222530f712c9SDave Chinner 	 * we are looking up.
222630f712c9SDave Chinner 	 */
222730f712c9SDave Chinner 	cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
222830f712c9SDave Chinner 	error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i);
222930f712c9SDave Chinner 	if (!error) {
223030f712c9SDave Chinner 		if (i)
223130f712c9SDave Chinner 			error = xfs_inobt_get_rec(cur, &rec, &i);
223230f712c9SDave Chinner 		if (!error && i == 0)
22332451337dSDave Chinner 			error = -EINVAL;
223430f712c9SDave Chinner 	}
223530f712c9SDave Chinner 
223630f712c9SDave Chinner 	xfs_trans_brelse(tp, agbp);
2237f307080aSBrian Foster 	xfs_btree_del_cursor(cur, error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
223830f712c9SDave Chinner 	if (error)
223930f712c9SDave Chinner 		return error;
224030f712c9SDave Chinner 
224130f712c9SDave Chinner 	/* check that the returned record contains the required inode */
224230f712c9SDave Chinner 	if (rec.ir_startino > agino ||
224330f712c9SDave Chinner 	    rec.ir_startino + mp->m_ialloc_inos <= agino)
22442451337dSDave Chinner 		return -EINVAL;
224530f712c9SDave Chinner 
224630f712c9SDave Chinner 	/* for untrusted inodes check it is allocated first */
224730f712c9SDave Chinner 	if ((flags & XFS_IGET_UNTRUSTED) &&
224830f712c9SDave Chinner 	    (rec.ir_free & XFS_INOBT_MASK(agino - rec.ir_startino)))
22492451337dSDave Chinner 		return -EINVAL;
225030f712c9SDave Chinner 
225130f712c9SDave Chinner 	*chunk_agbno = XFS_AGINO_TO_AGBNO(mp, rec.ir_startino);
225230f712c9SDave Chinner 	*offset_agbno = agbno - *chunk_agbno;
225330f712c9SDave Chinner 	return 0;
225430f712c9SDave Chinner }
225530f712c9SDave Chinner 
225630f712c9SDave Chinner /*
225730f712c9SDave Chinner  * Return the location of the inode in imap, for mapping it into a buffer.
225830f712c9SDave Chinner  */
225930f712c9SDave Chinner int
226030f712c9SDave Chinner xfs_imap(
226130f712c9SDave Chinner 	xfs_mount_t	 *mp,	/* file system mount structure */
226230f712c9SDave Chinner 	xfs_trans_t	 *tp,	/* transaction pointer */
226330f712c9SDave Chinner 	xfs_ino_t	ino,	/* inode to locate */
226430f712c9SDave Chinner 	struct xfs_imap	*imap,	/* location map structure */
226530f712c9SDave Chinner 	uint		flags)	/* flags for inode btree lookup */
226630f712c9SDave Chinner {
226730f712c9SDave Chinner 	xfs_agblock_t	agbno;	/* block number of inode in the alloc group */
226830f712c9SDave Chinner 	xfs_agino_t	agino;	/* inode number within alloc group */
226930f712c9SDave Chinner 	xfs_agnumber_t	agno;	/* allocation group number */
227030f712c9SDave Chinner 	int		blks_per_cluster; /* num blocks per inode cluster */
227130f712c9SDave Chinner 	xfs_agblock_t	chunk_agbno;	/* first block in inode chunk */
227230f712c9SDave Chinner 	xfs_agblock_t	cluster_agbno;	/* first block in inode cluster */
227330f712c9SDave Chinner 	int		error;	/* error code */
227430f712c9SDave Chinner 	int		offset;	/* index of inode in its buffer */
227530f712c9SDave Chinner 	xfs_agblock_t	offset_agbno;	/* blks from chunk start to inode */
227630f712c9SDave Chinner 
227730f712c9SDave Chinner 	ASSERT(ino != NULLFSINO);
227830f712c9SDave Chinner 
227930f712c9SDave Chinner 	/*
228030f712c9SDave Chinner 	 * Split up the inode number into its parts.
228130f712c9SDave Chinner 	 */
228230f712c9SDave Chinner 	agno = XFS_INO_TO_AGNO(mp, ino);
228330f712c9SDave Chinner 	agino = XFS_INO_TO_AGINO(mp, ino);
228430f712c9SDave Chinner 	agbno = XFS_AGINO_TO_AGBNO(mp, agino);
228530f712c9SDave Chinner 	if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks ||
228630f712c9SDave Chinner 	    ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
228730f712c9SDave Chinner #ifdef DEBUG
228830f712c9SDave Chinner 		/*
228930f712c9SDave Chinner 		 * Don't output diagnostic information for untrusted inodes
229030f712c9SDave Chinner 		 * as they can be invalid without implying corruption.
229130f712c9SDave Chinner 		 */
229230f712c9SDave Chinner 		if (flags & XFS_IGET_UNTRUSTED)
22932451337dSDave Chinner 			return -EINVAL;
229430f712c9SDave Chinner 		if (agno >= mp->m_sb.sb_agcount) {
229530f712c9SDave Chinner 			xfs_alert(mp,
229630f712c9SDave Chinner 				"%s: agno (%d) >= mp->m_sb.sb_agcount (%d)",
229730f712c9SDave Chinner 				__func__, agno, mp->m_sb.sb_agcount);
229830f712c9SDave Chinner 		}
229930f712c9SDave Chinner 		if (agbno >= mp->m_sb.sb_agblocks) {
230030f712c9SDave Chinner 			xfs_alert(mp,
230130f712c9SDave Chinner 		"%s: agbno (0x%llx) >= mp->m_sb.sb_agblocks (0x%lx)",
230230f712c9SDave Chinner 				__func__, (unsigned long long)agbno,
230330f712c9SDave Chinner 				(unsigned long)mp->m_sb.sb_agblocks);
230430f712c9SDave Chinner 		}
230530f712c9SDave Chinner 		if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
230630f712c9SDave Chinner 			xfs_alert(mp,
230730f712c9SDave Chinner 		"%s: ino (0x%llx) != XFS_AGINO_TO_INO() (0x%llx)",
230830f712c9SDave Chinner 				__func__, ino,
230930f712c9SDave Chinner 				XFS_AGINO_TO_INO(mp, agno, agino));
231030f712c9SDave Chinner 		}
231130f712c9SDave Chinner 		xfs_stack_trace();
231230f712c9SDave Chinner #endif /* DEBUG */
23132451337dSDave Chinner 		return -EINVAL;
231430f712c9SDave Chinner 	}
231530f712c9SDave Chinner 
231630f712c9SDave Chinner 	blks_per_cluster = xfs_icluster_size_fsb(mp);
231730f712c9SDave Chinner 
231830f712c9SDave Chinner 	/*
231930f712c9SDave Chinner 	 * For bulkstat and handle lookups, we have an untrusted inode number
232030f712c9SDave Chinner 	 * that we have to verify is valid. We cannot do this just by reading
232130f712c9SDave Chinner 	 * the inode buffer as it may have been unlinked and removed leaving
232230f712c9SDave Chinner 	 * inodes in stale state on disk. Hence we have to do a btree lookup
232330f712c9SDave Chinner 	 * in all cases where an untrusted inode number is passed.
232430f712c9SDave Chinner 	 */
232530f712c9SDave Chinner 	if (flags & XFS_IGET_UNTRUSTED) {
232630f712c9SDave Chinner 		error = xfs_imap_lookup(mp, tp, agno, agino, agbno,
232730f712c9SDave Chinner 					&chunk_agbno, &offset_agbno, flags);
232830f712c9SDave Chinner 		if (error)
232930f712c9SDave Chinner 			return error;
233030f712c9SDave Chinner 		goto out_map;
233130f712c9SDave Chinner 	}
233230f712c9SDave Chinner 
233330f712c9SDave Chinner 	/*
233430f712c9SDave Chinner 	 * If the inode cluster size is the same as the blocksize or
233530f712c9SDave Chinner 	 * smaller we get to the buffer by simple arithmetics.
233630f712c9SDave Chinner 	 */
233730f712c9SDave Chinner 	if (blks_per_cluster == 1) {
233830f712c9SDave Chinner 		offset = XFS_INO_TO_OFFSET(mp, ino);
233930f712c9SDave Chinner 		ASSERT(offset < mp->m_sb.sb_inopblock);
234030f712c9SDave Chinner 
234130f712c9SDave Chinner 		imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, agbno);
234230f712c9SDave Chinner 		imap->im_len = XFS_FSB_TO_BB(mp, 1);
234330f712c9SDave Chinner 		imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog);
234430f712c9SDave Chinner 		return 0;
234530f712c9SDave Chinner 	}
234630f712c9SDave Chinner 
234730f712c9SDave Chinner 	/*
234830f712c9SDave Chinner 	 * If the inode chunks are aligned then use simple maths to
234930f712c9SDave Chinner 	 * find the location. Otherwise we have to do a btree
235030f712c9SDave Chinner 	 * lookup to find the location.
235130f712c9SDave Chinner 	 */
235230f712c9SDave Chinner 	if (mp->m_inoalign_mask) {
235330f712c9SDave Chinner 		offset_agbno = agbno & mp->m_inoalign_mask;
235430f712c9SDave Chinner 		chunk_agbno = agbno - offset_agbno;
235530f712c9SDave Chinner 	} else {
235630f712c9SDave Chinner 		error = xfs_imap_lookup(mp, tp, agno, agino, agbno,
235730f712c9SDave Chinner 					&chunk_agbno, &offset_agbno, flags);
235830f712c9SDave Chinner 		if (error)
235930f712c9SDave Chinner 			return error;
236030f712c9SDave Chinner 	}
236130f712c9SDave Chinner 
236230f712c9SDave Chinner out_map:
236330f712c9SDave Chinner 	ASSERT(agbno >= chunk_agbno);
236430f712c9SDave Chinner 	cluster_agbno = chunk_agbno +
236530f712c9SDave Chinner 		((offset_agbno / blks_per_cluster) * blks_per_cluster);
236630f712c9SDave Chinner 	offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
236730f712c9SDave Chinner 		XFS_INO_TO_OFFSET(mp, ino);
236830f712c9SDave Chinner 
236930f712c9SDave Chinner 	imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, cluster_agbno);
237030f712c9SDave Chinner 	imap->im_len = XFS_FSB_TO_BB(mp, blks_per_cluster);
237130f712c9SDave Chinner 	imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog);
237230f712c9SDave Chinner 
237330f712c9SDave Chinner 	/*
237430f712c9SDave Chinner 	 * If the inode number maps to a block outside the bounds
237530f712c9SDave Chinner 	 * of the file system then return NULL rather than calling
237630f712c9SDave Chinner 	 * read_buf and panicing when we get an error from the
237730f712c9SDave Chinner 	 * driver.
237830f712c9SDave Chinner 	 */
237930f712c9SDave Chinner 	if ((imap->im_blkno + imap->im_len) >
238030f712c9SDave Chinner 	    XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) {
238130f712c9SDave Chinner 		xfs_alert(mp,
238230f712c9SDave Chinner 	"%s: (im_blkno (0x%llx) + im_len (0x%llx)) > sb_dblocks (0x%llx)",
238330f712c9SDave Chinner 			__func__, (unsigned long long) imap->im_blkno,
238430f712c9SDave Chinner 			(unsigned long long) imap->im_len,
238530f712c9SDave Chinner 			XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks));
23862451337dSDave Chinner 		return -EINVAL;
238730f712c9SDave Chinner 	}
238830f712c9SDave Chinner 	return 0;
238930f712c9SDave Chinner }
239030f712c9SDave Chinner 
239130f712c9SDave Chinner /*
239230f712c9SDave Chinner  * Compute and fill in value of m_in_maxlevels.
239330f712c9SDave Chinner  */
239430f712c9SDave Chinner void
239530f712c9SDave Chinner xfs_ialloc_compute_maxlevels(
239630f712c9SDave Chinner 	xfs_mount_t	*mp)		/* file system mount structure */
239730f712c9SDave Chinner {
239830f712c9SDave Chinner 	int		level;
239930f712c9SDave Chinner 	uint		maxblocks;
240030f712c9SDave Chinner 	uint		maxleafents;
240130f712c9SDave Chinner 	int		minleafrecs;
240230f712c9SDave Chinner 	int		minnoderecs;
240330f712c9SDave Chinner 
240430f712c9SDave Chinner 	maxleafents = (1LL << XFS_INO_AGINO_BITS(mp)) >>
240530f712c9SDave Chinner 		XFS_INODES_PER_CHUNK_LOG;
240630f712c9SDave Chinner 	minleafrecs = mp->m_alloc_mnr[0];
240730f712c9SDave Chinner 	minnoderecs = mp->m_alloc_mnr[1];
240830f712c9SDave Chinner 	maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
240930f712c9SDave Chinner 	for (level = 1; maxblocks > 1; level++)
241030f712c9SDave Chinner 		maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
241130f712c9SDave Chinner 	mp->m_in_maxlevels = level;
241230f712c9SDave Chinner }
241330f712c9SDave Chinner 
241430f712c9SDave Chinner /*
241530f712c9SDave Chinner  * Log specified fields for the ag hdr (inode section). The growth of the agi
241630f712c9SDave Chinner  * structure over time requires that we interpret the buffer as two logical
241730f712c9SDave Chinner  * regions delineated by the end of the unlinked list. This is due to the size
241830f712c9SDave Chinner  * of the hash table and its location in the middle of the agi.
241930f712c9SDave Chinner  *
242030f712c9SDave Chinner  * For example, a request to log a field before agi_unlinked and a field after
242130f712c9SDave Chinner  * agi_unlinked could cause us to log the entire hash table and use an excessive
242230f712c9SDave Chinner  * amount of log space. To avoid this behavior, log the region up through
242330f712c9SDave Chinner  * agi_unlinked in one call and the region after agi_unlinked through the end of
242430f712c9SDave Chinner  * the structure in another.
242530f712c9SDave Chinner  */
242630f712c9SDave Chinner void
242730f712c9SDave Chinner xfs_ialloc_log_agi(
242830f712c9SDave Chinner 	xfs_trans_t	*tp,		/* transaction pointer */
242930f712c9SDave Chinner 	xfs_buf_t	*bp,		/* allocation group header buffer */
243030f712c9SDave Chinner 	int		fields)		/* bitmask of fields to log */
243130f712c9SDave Chinner {
243230f712c9SDave Chinner 	int			first;		/* first byte number */
243330f712c9SDave Chinner 	int			last;		/* last byte number */
243430f712c9SDave Chinner 	static const short	offsets[] = {	/* field starting offsets */
243530f712c9SDave Chinner 					/* keep in sync with bit definitions */
243630f712c9SDave Chinner 		offsetof(xfs_agi_t, agi_magicnum),
243730f712c9SDave Chinner 		offsetof(xfs_agi_t, agi_versionnum),
243830f712c9SDave Chinner 		offsetof(xfs_agi_t, agi_seqno),
243930f712c9SDave Chinner 		offsetof(xfs_agi_t, agi_length),
244030f712c9SDave Chinner 		offsetof(xfs_agi_t, agi_count),
244130f712c9SDave Chinner 		offsetof(xfs_agi_t, agi_root),
244230f712c9SDave Chinner 		offsetof(xfs_agi_t, agi_level),
244330f712c9SDave Chinner 		offsetof(xfs_agi_t, agi_freecount),
244430f712c9SDave Chinner 		offsetof(xfs_agi_t, agi_newino),
244530f712c9SDave Chinner 		offsetof(xfs_agi_t, agi_dirino),
244630f712c9SDave Chinner 		offsetof(xfs_agi_t, agi_unlinked),
244730f712c9SDave Chinner 		offsetof(xfs_agi_t, agi_free_root),
244830f712c9SDave Chinner 		offsetof(xfs_agi_t, agi_free_level),
244930f712c9SDave Chinner 		sizeof(xfs_agi_t)
245030f712c9SDave Chinner 	};
245130f712c9SDave Chinner #ifdef DEBUG
245230f712c9SDave Chinner 	xfs_agi_t		*agi;	/* allocation group header */
245330f712c9SDave Chinner 
245430f712c9SDave Chinner 	agi = XFS_BUF_TO_AGI(bp);
245530f712c9SDave Chinner 	ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC));
245630f712c9SDave Chinner #endif
245730f712c9SDave Chinner 
245830f712c9SDave Chinner 	xfs_trans_buf_set_type(tp, bp, XFS_BLFT_AGI_BUF);
245930f712c9SDave Chinner 
246030f712c9SDave Chinner 	/*
246130f712c9SDave Chinner 	 * Compute byte offsets for the first and last fields in the first
246230f712c9SDave Chinner 	 * region and log the agi buffer. This only logs up through
246330f712c9SDave Chinner 	 * agi_unlinked.
246430f712c9SDave Chinner 	 */
246530f712c9SDave Chinner 	if (fields & XFS_AGI_ALL_BITS_R1) {
246630f712c9SDave Chinner 		xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R1,
246730f712c9SDave Chinner 				  &first, &last);
246830f712c9SDave Chinner 		xfs_trans_log_buf(tp, bp, first, last);
246930f712c9SDave Chinner 	}
247030f712c9SDave Chinner 
247130f712c9SDave Chinner 	/*
247230f712c9SDave Chinner 	 * Mask off the bits in the first region and calculate the first and
247330f712c9SDave Chinner 	 * last field offsets for any bits in the second region.
247430f712c9SDave Chinner 	 */
247530f712c9SDave Chinner 	fields &= ~XFS_AGI_ALL_BITS_R1;
247630f712c9SDave Chinner 	if (fields) {
247730f712c9SDave Chinner 		xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R2,
247830f712c9SDave Chinner 				  &first, &last);
247930f712c9SDave Chinner 		xfs_trans_log_buf(tp, bp, first, last);
248030f712c9SDave Chinner 	}
248130f712c9SDave Chinner }
248230f712c9SDave Chinner 
248330f712c9SDave Chinner #ifdef DEBUG
248430f712c9SDave Chinner STATIC void
248530f712c9SDave Chinner xfs_check_agi_unlinked(
248630f712c9SDave Chinner 	struct xfs_agi		*agi)
248730f712c9SDave Chinner {
248830f712c9SDave Chinner 	int			i;
248930f712c9SDave Chinner 
249030f712c9SDave Chinner 	for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++)
249130f712c9SDave Chinner 		ASSERT(agi->agi_unlinked[i]);
249230f712c9SDave Chinner }
249330f712c9SDave Chinner #else
249430f712c9SDave Chinner #define xfs_check_agi_unlinked(agi)
249530f712c9SDave Chinner #endif
249630f712c9SDave Chinner 
249730f712c9SDave Chinner static bool
249830f712c9SDave Chinner xfs_agi_verify(
249930f712c9SDave Chinner 	struct xfs_buf	*bp)
250030f712c9SDave Chinner {
250130f712c9SDave Chinner 	struct xfs_mount *mp = bp->b_target->bt_mount;
250230f712c9SDave Chinner 	struct xfs_agi	*agi = XFS_BUF_TO_AGI(bp);
250330f712c9SDave Chinner 
2504a45086e2SBrian Foster 	if (xfs_sb_version_hascrc(&mp->m_sb)) {
2505a45086e2SBrian Foster 		if (!uuid_equal(&agi->agi_uuid, &mp->m_sb.sb_meta_uuid))
250630f712c9SDave Chinner 			return false;
2507a45086e2SBrian Foster 		if (!xfs_log_check_lsn(mp,
2508a45086e2SBrian Foster 				be64_to_cpu(XFS_BUF_TO_AGI(bp)->agi_lsn)))
2509a45086e2SBrian Foster 			return false;
2510a45086e2SBrian Foster 	}
2511a45086e2SBrian Foster 
251230f712c9SDave Chinner 	/*
251330f712c9SDave Chinner 	 * Validate the magic number of the agi block.
251430f712c9SDave Chinner 	 */
251530f712c9SDave Chinner 	if (agi->agi_magicnum != cpu_to_be32(XFS_AGI_MAGIC))
251630f712c9SDave Chinner 		return false;
251730f712c9SDave Chinner 	if (!XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum)))
251830f712c9SDave Chinner 		return false;
251930f712c9SDave Chinner 
2520e1b05723SEric Sandeen 	if (be32_to_cpu(agi->agi_level) > XFS_BTREE_MAXLEVELS)
2521e1b05723SEric Sandeen 		return false;
252230f712c9SDave Chinner 	/*
252330f712c9SDave Chinner 	 * during growfs operations, the perag is not fully initialised,
252430f712c9SDave Chinner 	 * so we can't use it for any useful checking. growfs ensures we can't
252530f712c9SDave Chinner 	 * use it by using uncached buffers that don't have the perag attached
252630f712c9SDave Chinner 	 * so we can detect and avoid this problem.
252730f712c9SDave Chinner 	 */
252830f712c9SDave Chinner 	if (bp->b_pag && be32_to_cpu(agi->agi_seqno) != bp->b_pag->pag_agno)
252930f712c9SDave Chinner 		return false;
253030f712c9SDave Chinner 
253130f712c9SDave Chinner 	xfs_check_agi_unlinked(agi);
253230f712c9SDave Chinner 	return true;
253330f712c9SDave Chinner }
253430f712c9SDave Chinner 
253530f712c9SDave Chinner static void
253630f712c9SDave Chinner xfs_agi_read_verify(
253730f712c9SDave Chinner 	struct xfs_buf	*bp)
253830f712c9SDave Chinner {
253930f712c9SDave Chinner 	struct xfs_mount *mp = bp->b_target->bt_mount;
254030f712c9SDave Chinner 
254130f712c9SDave Chinner 	if (xfs_sb_version_hascrc(&mp->m_sb) &&
254230f712c9SDave Chinner 	    !xfs_buf_verify_cksum(bp, XFS_AGI_CRC_OFF))
25432451337dSDave Chinner 		xfs_buf_ioerror(bp, -EFSBADCRC);
254430f712c9SDave Chinner 	else if (XFS_TEST_ERROR(!xfs_agi_verify(bp), mp,
254530f712c9SDave Chinner 				XFS_ERRTAG_IALLOC_READ_AGI,
254630f712c9SDave Chinner 				XFS_RANDOM_IALLOC_READ_AGI))
25472451337dSDave Chinner 		xfs_buf_ioerror(bp, -EFSCORRUPTED);
254830f712c9SDave Chinner 
254930f712c9SDave Chinner 	if (bp->b_error)
255030f712c9SDave Chinner 		xfs_verifier_error(bp);
255130f712c9SDave Chinner }
255230f712c9SDave Chinner 
255330f712c9SDave Chinner static void
255430f712c9SDave Chinner xfs_agi_write_verify(
255530f712c9SDave Chinner 	struct xfs_buf	*bp)
255630f712c9SDave Chinner {
255730f712c9SDave Chinner 	struct xfs_mount *mp = bp->b_target->bt_mount;
255830f712c9SDave Chinner 	struct xfs_buf_log_item	*bip = bp->b_fspriv;
255930f712c9SDave Chinner 
256030f712c9SDave Chinner 	if (!xfs_agi_verify(bp)) {
25612451337dSDave Chinner 		xfs_buf_ioerror(bp, -EFSCORRUPTED);
256230f712c9SDave Chinner 		xfs_verifier_error(bp);
256330f712c9SDave Chinner 		return;
256430f712c9SDave Chinner 	}
256530f712c9SDave Chinner 
256630f712c9SDave Chinner 	if (!xfs_sb_version_hascrc(&mp->m_sb))
256730f712c9SDave Chinner 		return;
256830f712c9SDave Chinner 
256930f712c9SDave Chinner 	if (bip)
257030f712c9SDave Chinner 		XFS_BUF_TO_AGI(bp)->agi_lsn = cpu_to_be64(bip->bli_item.li_lsn);
257130f712c9SDave Chinner 	xfs_buf_update_cksum(bp, XFS_AGI_CRC_OFF);
257230f712c9SDave Chinner }
257330f712c9SDave Chinner 
257430f712c9SDave Chinner const struct xfs_buf_ops xfs_agi_buf_ops = {
2575*233135b7SEric Sandeen 	.name = "xfs_agi",
257630f712c9SDave Chinner 	.verify_read = xfs_agi_read_verify,
257730f712c9SDave Chinner 	.verify_write = xfs_agi_write_verify,
257830f712c9SDave Chinner };
257930f712c9SDave Chinner 
258030f712c9SDave Chinner /*
258130f712c9SDave Chinner  * Read in the allocation group header (inode allocation section)
258230f712c9SDave Chinner  */
258330f712c9SDave Chinner int
258430f712c9SDave Chinner xfs_read_agi(
258530f712c9SDave Chinner 	struct xfs_mount	*mp,	/* file system mount structure */
258630f712c9SDave Chinner 	struct xfs_trans	*tp,	/* transaction pointer */
258730f712c9SDave Chinner 	xfs_agnumber_t		agno,	/* allocation group number */
258830f712c9SDave Chinner 	struct xfs_buf		**bpp)	/* allocation group hdr buf */
258930f712c9SDave Chinner {
259030f712c9SDave Chinner 	int			error;
259130f712c9SDave Chinner 
259230f712c9SDave Chinner 	trace_xfs_read_agi(mp, agno);
259330f712c9SDave Chinner 
259430f712c9SDave Chinner 	ASSERT(agno != NULLAGNUMBER);
259530f712c9SDave Chinner 	error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
259630f712c9SDave Chinner 			XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
259730f712c9SDave Chinner 			XFS_FSS_TO_BB(mp, 1), 0, bpp, &xfs_agi_buf_ops);
259830f712c9SDave Chinner 	if (error)
259930f712c9SDave Chinner 		return error;
260030f712c9SDave Chinner 
260130f712c9SDave Chinner 	xfs_buf_set_ref(*bpp, XFS_AGI_REF);
260230f712c9SDave Chinner 	return 0;
260330f712c9SDave Chinner }
260430f712c9SDave Chinner 
260530f712c9SDave Chinner int
260630f712c9SDave Chinner xfs_ialloc_read_agi(
260730f712c9SDave Chinner 	struct xfs_mount	*mp,	/* file system mount structure */
260830f712c9SDave Chinner 	struct xfs_trans	*tp,	/* transaction pointer */
260930f712c9SDave Chinner 	xfs_agnumber_t		agno,	/* allocation group number */
261030f712c9SDave Chinner 	struct xfs_buf		**bpp)	/* allocation group hdr buf */
261130f712c9SDave Chinner {
261230f712c9SDave Chinner 	struct xfs_agi		*agi;	/* allocation group header */
261330f712c9SDave Chinner 	struct xfs_perag	*pag;	/* per allocation group data */
261430f712c9SDave Chinner 	int			error;
261530f712c9SDave Chinner 
261630f712c9SDave Chinner 	trace_xfs_ialloc_read_agi(mp, agno);
261730f712c9SDave Chinner 
261830f712c9SDave Chinner 	error = xfs_read_agi(mp, tp, agno, bpp);
261930f712c9SDave Chinner 	if (error)
262030f712c9SDave Chinner 		return error;
262130f712c9SDave Chinner 
262230f712c9SDave Chinner 	agi = XFS_BUF_TO_AGI(*bpp);
262330f712c9SDave Chinner 	pag = xfs_perag_get(mp, agno);
262430f712c9SDave Chinner 	if (!pag->pagi_init) {
262530f712c9SDave Chinner 		pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
262630f712c9SDave Chinner 		pag->pagi_count = be32_to_cpu(agi->agi_count);
262730f712c9SDave Chinner 		pag->pagi_init = 1;
262830f712c9SDave Chinner 	}
262930f712c9SDave Chinner 
263030f712c9SDave Chinner 	/*
263130f712c9SDave Chinner 	 * It's possible for these to be out of sync if
263230f712c9SDave Chinner 	 * we are in the middle of a forced shutdown.
263330f712c9SDave Chinner 	 */
263430f712c9SDave Chinner 	ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) ||
263530f712c9SDave Chinner 		XFS_FORCED_SHUTDOWN(mp));
263630f712c9SDave Chinner 	xfs_perag_put(pag);
263730f712c9SDave Chinner 	return 0;
263830f712c9SDave Chinner }
263930f712c9SDave Chinner 
264030f712c9SDave Chinner /*
264130f712c9SDave Chinner  * Read in the agi to initialise the per-ag data in the mount structure
264230f712c9SDave Chinner  */
264330f712c9SDave Chinner int
264430f712c9SDave Chinner xfs_ialloc_pagi_init(
264530f712c9SDave Chinner 	xfs_mount_t	*mp,		/* file system mount structure */
264630f712c9SDave Chinner 	xfs_trans_t	*tp,		/* transaction pointer */
264730f712c9SDave Chinner 	xfs_agnumber_t	agno)		/* allocation group number */
264830f712c9SDave Chinner {
264930f712c9SDave Chinner 	xfs_buf_t	*bp = NULL;
265030f712c9SDave Chinner 	int		error;
265130f712c9SDave Chinner 
265230f712c9SDave Chinner 	error = xfs_ialloc_read_agi(mp, tp, agno, &bp);
265330f712c9SDave Chinner 	if (error)
265430f712c9SDave Chinner 		return error;
265530f712c9SDave Chinner 	if (bp)
265630f712c9SDave Chinner 		xfs_trans_brelse(tp, bp);
265730f712c9SDave Chinner 	return 0;
265830f712c9SDave Chinner }
2659