xref: /openbmc/linux/fs/xfs/libxfs/xfs_ialloc.c (revision 6e2985c938e8b765b3de299c561d87f98330c546)
10b61f8a4SDave Chinner // SPDX-License-Identifier: GPL-2.0
230f712c9SDave Chinner /*
330f712c9SDave Chinner  * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
430f712c9SDave Chinner  * All Rights Reserved.
530f712c9SDave Chinner  */
630f712c9SDave Chinner #include "xfs.h"
730f712c9SDave Chinner #include "xfs_fs.h"
830f712c9SDave Chinner #include "xfs_shared.h"
930f712c9SDave Chinner #include "xfs_format.h"
1030f712c9SDave Chinner #include "xfs_log_format.h"
1130f712c9SDave Chinner #include "xfs_trans_resv.h"
1230f712c9SDave Chinner #include "xfs_bit.h"
1330f712c9SDave Chinner #include "xfs_mount.h"
1430f712c9SDave Chinner #include "xfs_inode.h"
1530f712c9SDave Chinner #include "xfs_btree.h"
1630f712c9SDave Chinner #include "xfs_ialloc.h"
1730f712c9SDave Chinner #include "xfs_ialloc_btree.h"
1830f712c9SDave Chinner #include "xfs_alloc.h"
19e9e899a2SDarrick J. Wong #include "xfs_errortag.h"
2030f712c9SDave Chinner #include "xfs_error.h"
2130f712c9SDave Chinner #include "xfs_bmap.h"
2230f712c9SDave Chinner #include "xfs_trans.h"
2330f712c9SDave Chinner #include "xfs_buf_item.h"
2430f712c9SDave Chinner #include "xfs_icreate_item.h"
2530f712c9SDave Chinner #include "xfs_icache.h"
2630f712c9SDave Chinner #include "xfs_trace.h"
27a45086e2SBrian Foster #include "xfs_log.h"
28340785ccSDarrick J. Wong #include "xfs_rmap.h"
299bbafc71SDave Chinner #include "xfs_ag.h"
3030f712c9SDave Chinner 
3130f712c9SDave Chinner /*
3230f712c9SDave Chinner  * Lookup a record by ino in the btree given by cur.
3330f712c9SDave Chinner  */
3430f712c9SDave Chinner int					/* error */
3530f712c9SDave Chinner xfs_inobt_lookup(
3630f712c9SDave Chinner 	struct xfs_btree_cur	*cur,	/* btree cursor */
3730f712c9SDave Chinner 	xfs_agino_t		ino,	/* starting inode of chunk */
3830f712c9SDave Chinner 	xfs_lookup_t		dir,	/* <=, >=, == */
3930f712c9SDave Chinner 	int			*stat)	/* success/failure */
4030f712c9SDave Chinner {
4130f712c9SDave Chinner 	cur->bc_rec.i.ir_startino = ino;
425419040fSBrian Foster 	cur->bc_rec.i.ir_holemask = 0;
435419040fSBrian Foster 	cur->bc_rec.i.ir_count = 0;
4430f712c9SDave Chinner 	cur->bc_rec.i.ir_freecount = 0;
4530f712c9SDave Chinner 	cur->bc_rec.i.ir_free = 0;
4630f712c9SDave Chinner 	return xfs_btree_lookup(cur, dir, stat);
4730f712c9SDave Chinner }
4830f712c9SDave Chinner 
4930f712c9SDave Chinner /*
5030f712c9SDave Chinner  * Update the record referred to by cur to the value given.
5130f712c9SDave Chinner  * This either works (return 0) or gets an EFSCORRUPTED error.
5230f712c9SDave Chinner  */
5330f712c9SDave Chinner STATIC int				/* error */
5430f712c9SDave Chinner xfs_inobt_update(
5530f712c9SDave Chinner 	struct xfs_btree_cur	*cur,	/* btree cursor */
5630f712c9SDave Chinner 	xfs_inobt_rec_incore_t	*irec)	/* btree record */
5730f712c9SDave Chinner {
5830f712c9SDave Chinner 	union xfs_btree_rec	rec;
5930f712c9SDave Chinner 
6030f712c9SDave Chinner 	rec.inobt.ir_startino = cpu_to_be32(irec->ir_startino);
6138c26bfdSDave Chinner 	if (xfs_has_sparseinodes(cur->bc_mp)) {
625419040fSBrian Foster 		rec.inobt.ir_u.sp.ir_holemask = cpu_to_be16(irec->ir_holemask);
635419040fSBrian Foster 		rec.inobt.ir_u.sp.ir_count = irec->ir_count;
645419040fSBrian Foster 		rec.inobt.ir_u.sp.ir_freecount = irec->ir_freecount;
655419040fSBrian Foster 	} else {
665419040fSBrian Foster 		/* ir_holemask/ir_count not supported on-disk */
675419040fSBrian Foster 		rec.inobt.ir_u.f.ir_freecount = cpu_to_be32(irec->ir_freecount);
685419040fSBrian Foster 	}
6930f712c9SDave Chinner 	rec.inobt.ir_free = cpu_to_be64(irec->ir_free);
7030f712c9SDave Chinner 	return xfs_btree_update(cur, &rec);
7130f712c9SDave Chinner }
7230f712c9SDave Chinner 
73e936945eSDarrick J. Wong /* Convert on-disk btree record to incore inobt record. */
74e936945eSDarrick J. Wong void
75e936945eSDarrick J. Wong xfs_inobt_btrec_to_irec(
76e936945eSDarrick J. Wong 	struct xfs_mount		*mp,
77159eb69dSDarrick J. Wong 	const union xfs_btree_rec	*rec,
78e936945eSDarrick J. Wong 	struct xfs_inobt_rec_incore	*irec)
7930f712c9SDave Chinner {
805419040fSBrian Foster 	irec->ir_startino = be32_to_cpu(rec->inobt.ir_startino);
8138c26bfdSDave Chinner 	if (xfs_has_sparseinodes(mp)) {
825419040fSBrian Foster 		irec->ir_holemask = be16_to_cpu(rec->inobt.ir_u.sp.ir_holemask);
835419040fSBrian Foster 		irec->ir_count = rec->inobt.ir_u.sp.ir_count;
845419040fSBrian Foster 		irec->ir_freecount = rec->inobt.ir_u.sp.ir_freecount;
855419040fSBrian Foster 	} else {
865419040fSBrian Foster 		/*
875419040fSBrian Foster 		 * ir_holemask/ir_count not supported on-disk. Fill in hardcoded
885419040fSBrian Foster 		 * values for full inode chunks.
895419040fSBrian Foster 		 */
905419040fSBrian Foster 		irec->ir_holemask = XFS_INOBT_HOLEMASK_FULL;
915419040fSBrian Foster 		irec->ir_count = XFS_INODES_PER_CHUNK;
925419040fSBrian Foster 		irec->ir_freecount =
935419040fSBrian Foster 				be32_to_cpu(rec->inobt.ir_u.f.ir_freecount);
945419040fSBrian Foster 	}
955419040fSBrian Foster 	irec->ir_free = be64_to_cpu(rec->inobt.ir_free);
96e936945eSDarrick J. Wong }
97e936945eSDarrick J. Wong 
98e936945eSDarrick J. Wong /*
99e936945eSDarrick J. Wong  * Get the data from the pointed-to record.
100e936945eSDarrick J. Wong  */
101e936945eSDarrick J. Wong int
102e936945eSDarrick J. Wong xfs_inobt_get_rec(
103e936945eSDarrick J. Wong 	struct xfs_btree_cur		*cur,
104e936945eSDarrick J. Wong 	struct xfs_inobt_rec_incore	*irec,
105e936945eSDarrick J. Wong 	int				*stat)
106e936945eSDarrick J. Wong {
1079e6c08d4SDave Chinner 	struct xfs_mount		*mp = cur->bc_mp;
108e936945eSDarrick J. Wong 	union xfs_btree_rec		*rec;
109e936945eSDarrick J. Wong 	int				error;
1109e6c08d4SDave Chinner 	uint64_t			realfree;
111e936945eSDarrick J. Wong 
112e936945eSDarrick J. Wong 	error = xfs_btree_get_rec(cur, &rec, stat);
113e936945eSDarrick J. Wong 	if (error || *stat == 0)
114e936945eSDarrick J. Wong 		return error;
115e936945eSDarrick J. Wong 
1169e6c08d4SDave Chinner 	xfs_inobt_btrec_to_irec(mp, rec, irec);
1179e6c08d4SDave Chinner 
1182d6ca832SDave Chinner 	if (!xfs_verify_agino(cur->bc_ag.pag, irec->ir_startino))
1199e6c08d4SDave Chinner 		goto out_bad_rec;
1209e6c08d4SDave Chinner 	if (irec->ir_count < XFS_INODES_PER_HOLEMASK_BIT ||
1219e6c08d4SDave Chinner 	    irec->ir_count > XFS_INODES_PER_CHUNK)
1229e6c08d4SDave Chinner 		goto out_bad_rec;
1239e6c08d4SDave Chinner 	if (irec->ir_freecount > XFS_INODES_PER_CHUNK)
1249e6c08d4SDave Chinner 		goto out_bad_rec;
1259e6c08d4SDave Chinner 
1269e6c08d4SDave Chinner 	/* if there are no holes, return the first available offset */
1279e6c08d4SDave Chinner 	if (!xfs_inobt_issparse(irec->ir_holemask))
1289e6c08d4SDave Chinner 		realfree = irec->ir_free;
1299e6c08d4SDave Chinner 	else
1309e6c08d4SDave Chinner 		realfree = irec->ir_free & xfs_inobt_irec_to_allocmask(irec);
1319e6c08d4SDave Chinner 	if (hweight64(realfree) != irec->ir_freecount)
1329e6c08d4SDave Chinner 		goto out_bad_rec;
1335419040fSBrian Foster 
1345419040fSBrian Foster 	return 0;
1359e6c08d4SDave Chinner 
1369e6c08d4SDave Chinner out_bad_rec:
1379e6c08d4SDave Chinner 	xfs_warn(mp,
1389e6c08d4SDave Chinner 		"%s Inode BTree record corruption in AG %d detected!",
1392d6ca832SDave Chinner 		cur->bc_btnum == XFS_BTNUM_INO ? "Used" : "Free",
1402d6ca832SDave Chinner 		cur->bc_ag.pag->pag_agno);
1419e6c08d4SDave Chinner 	xfs_warn(mp,
1429e6c08d4SDave Chinner "start inode 0x%x, count 0x%x, free 0x%x freemask 0x%llx, holemask 0x%x",
1439e6c08d4SDave Chinner 		irec->ir_startino, irec->ir_count, irec->ir_freecount,
1449e6c08d4SDave Chinner 		irec->ir_free, irec->ir_holemask);
1459e6c08d4SDave Chinner 	return -EFSCORRUPTED;
14630f712c9SDave Chinner }
14730f712c9SDave Chinner 
14830f712c9SDave Chinner /*
14930f712c9SDave Chinner  * Insert a single inobt record. Cursor must already point to desired location.
15030f712c9SDave Chinner  */
1517f8f1313SDarrick J. Wong int
15230f712c9SDave Chinner xfs_inobt_insert_rec(
15330f712c9SDave Chinner 	struct xfs_btree_cur	*cur,
154c8ce540dSDarrick J. Wong 	uint16_t		holemask,
155c8ce540dSDarrick J. Wong 	uint8_t			count,
156c8ce540dSDarrick J. Wong 	int32_t			freecount,
15730f712c9SDave Chinner 	xfs_inofree_t		free,
15830f712c9SDave Chinner 	int			*stat)
15930f712c9SDave Chinner {
1605419040fSBrian Foster 	cur->bc_rec.i.ir_holemask = holemask;
1615419040fSBrian Foster 	cur->bc_rec.i.ir_count = count;
16230f712c9SDave Chinner 	cur->bc_rec.i.ir_freecount = freecount;
16330f712c9SDave Chinner 	cur->bc_rec.i.ir_free = free;
16430f712c9SDave Chinner 	return xfs_btree_insert(cur, stat);
16530f712c9SDave Chinner }
16630f712c9SDave Chinner 
16730f712c9SDave Chinner /*
16830f712c9SDave Chinner  * Insert records describing a newly allocated inode chunk into the inobt.
16930f712c9SDave Chinner  */
17030f712c9SDave Chinner STATIC int
17130f712c9SDave Chinner xfs_inobt_insert(
172dedab3e4SDave Chinner 	struct xfs_perag	*pag,
17330f712c9SDave Chinner 	struct xfs_trans	*tp,
17430f712c9SDave Chinner 	struct xfs_buf		*agbp,
17530f712c9SDave Chinner 	xfs_agino_t		newino,
17630f712c9SDave Chinner 	xfs_agino_t		newlen,
17730f712c9SDave Chinner 	xfs_btnum_t		btnum)
17830f712c9SDave Chinner {
17930f712c9SDave Chinner 	struct xfs_btree_cur	*cur;
18030f712c9SDave Chinner 	xfs_agino_t		thisino;
18130f712c9SDave Chinner 	int			i;
18230f712c9SDave Chinner 	int			error;
18330f712c9SDave Chinner 
184bab8b795SDave Chinner 	cur = xfs_inobt_init_cursor(pag, tp, agbp, btnum);
18530f712c9SDave Chinner 
18630f712c9SDave Chinner 	for (thisino = newino;
18730f712c9SDave Chinner 	     thisino < newino + newlen;
18830f712c9SDave Chinner 	     thisino += XFS_INODES_PER_CHUNK) {
18930f712c9SDave Chinner 		error = xfs_inobt_lookup(cur, thisino, XFS_LOOKUP_EQ, &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 == 0);
19530f712c9SDave Chinner 
1965419040fSBrian Foster 		error = xfs_inobt_insert_rec(cur, XFS_INOBT_HOLEMASK_FULL,
1975419040fSBrian Foster 					     XFS_INODES_PER_CHUNK,
1985419040fSBrian Foster 					     XFS_INODES_PER_CHUNK,
19930f712c9SDave Chinner 					     XFS_INOBT_ALL_FREE, &i);
20030f712c9SDave Chinner 		if (error) {
20130f712c9SDave Chinner 			xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
20230f712c9SDave Chinner 			return error;
20330f712c9SDave Chinner 		}
20430f712c9SDave Chinner 		ASSERT(i == 1);
20530f712c9SDave Chinner 	}
20630f712c9SDave Chinner 
20730f712c9SDave Chinner 	xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
20830f712c9SDave Chinner 
20930f712c9SDave Chinner 	return 0;
21030f712c9SDave Chinner }
21130f712c9SDave Chinner 
21230f712c9SDave Chinner /*
21330f712c9SDave Chinner  * Verify that the number of free inodes in the AGI is correct.
21430f712c9SDave Chinner  */
21530f712c9SDave Chinner #ifdef DEBUG
2169ba0889eSDave Chinner static int
21730f712c9SDave Chinner xfs_check_agi_freecount(
2189ba0889eSDave Chinner 	struct xfs_btree_cur	*cur)
21930f712c9SDave Chinner {
22030f712c9SDave Chinner 	if (cur->bc_nlevels == 1) {
22130f712c9SDave Chinner 		xfs_inobt_rec_incore_t rec;
22230f712c9SDave Chinner 		int		freecount = 0;
22330f712c9SDave Chinner 		int		error;
22430f712c9SDave Chinner 		int		i;
22530f712c9SDave Chinner 
22630f712c9SDave Chinner 		error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
22730f712c9SDave Chinner 		if (error)
22830f712c9SDave Chinner 			return error;
22930f712c9SDave Chinner 
23030f712c9SDave Chinner 		do {
23130f712c9SDave Chinner 			error = xfs_inobt_get_rec(cur, &rec, &i);
23230f712c9SDave Chinner 			if (error)
23330f712c9SDave Chinner 				return error;
23430f712c9SDave Chinner 
23530f712c9SDave Chinner 			if (i) {
23630f712c9SDave Chinner 				freecount += rec.ir_freecount;
23730f712c9SDave Chinner 				error = xfs_btree_increment(cur, 0, &i);
23830f712c9SDave Chinner 				if (error)
23930f712c9SDave Chinner 					return error;
24030f712c9SDave Chinner 			}
24130f712c9SDave Chinner 		} while (i == 1);
24230f712c9SDave Chinner 
24375c8c50fSDave Chinner 		if (!xfs_is_shutdown(cur->bc_mp))
2449ba0889eSDave Chinner 			ASSERT(freecount == cur->bc_ag.pag->pagi_freecount);
24530f712c9SDave Chinner 	}
24630f712c9SDave Chinner 	return 0;
24730f712c9SDave Chinner }
24830f712c9SDave Chinner #else
2499ba0889eSDave Chinner #define xfs_check_agi_freecount(cur)	0
25030f712c9SDave Chinner #endif
25130f712c9SDave Chinner 
25230f712c9SDave Chinner /*
25330f712c9SDave Chinner  * Initialise a new set of inodes. When called without a transaction context
25430f712c9SDave Chinner  * (e.g. from recovery) we initiate a delayed write of the inode buffers rather
25530f712c9SDave Chinner  * than logging them (which in a transaction context puts them into the AIL
25630f712c9SDave Chinner  * for writeback rather than the xfsbufd queue).
25730f712c9SDave Chinner  */
25830f712c9SDave Chinner int
25930f712c9SDave Chinner xfs_ialloc_inode_init(
26030f712c9SDave Chinner 	struct xfs_mount	*mp,
26130f712c9SDave Chinner 	struct xfs_trans	*tp,
26230f712c9SDave Chinner 	struct list_head	*buffer_list,
263463958afSBrian Foster 	int			icount,
26430f712c9SDave Chinner 	xfs_agnumber_t		agno,
26530f712c9SDave Chinner 	xfs_agblock_t		agbno,
26630f712c9SDave Chinner 	xfs_agblock_t		length,
26730f712c9SDave Chinner 	unsigned int		gen)
26830f712c9SDave Chinner {
26930f712c9SDave Chinner 	struct xfs_buf		*fbuf;
27030f712c9SDave Chinner 	struct xfs_dinode	*free;
27183dcdb44SDarrick J. Wong 	int			nbufs;
27230f712c9SDave Chinner 	int			version;
27330f712c9SDave Chinner 	int			i, j;
27430f712c9SDave Chinner 	xfs_daddr_t		d;
27530f712c9SDave Chinner 	xfs_ino_t		ino = 0;
276ce92464cSDarrick J. Wong 	int			error;
27730f712c9SDave Chinner 
27830f712c9SDave Chinner 	/*
27930f712c9SDave Chinner 	 * Loop over the new block(s), filling in the inodes.  For small block
28030f712c9SDave Chinner 	 * sizes, manipulate the inodes in buffers  which are multiples of the
28130f712c9SDave Chinner 	 * blocks size.
28230f712c9SDave Chinner 	 */
283ef325959SDarrick J. Wong 	nbufs = length / M_IGEO(mp)->blocks_per_cluster;
28430f712c9SDave Chinner 
28530f712c9SDave Chinner 	/*
28630f712c9SDave Chinner 	 * Figure out what version number to use in the inodes we create.  If
28730f712c9SDave Chinner 	 * the superblock version has caught up to the one that supports the new
28830f712c9SDave Chinner 	 * inode format, then use the new inode version.  Otherwise use the old
28930f712c9SDave Chinner 	 * version so that old kernels will continue to be able to use the file
29030f712c9SDave Chinner 	 * system.
29130f712c9SDave Chinner 	 *
29230f712c9SDave Chinner 	 * For v3 inodes, we also need to write the inode number into the inode,
29330f712c9SDave Chinner 	 * so calculate the first inode number of the chunk here as
29443004b2aSDarrick J. Wong 	 * XFS_AGB_TO_AGINO() only works within a filesystem block, not
29530f712c9SDave Chinner 	 * across multiple filesystem blocks (such as a cluster) and so cannot
29630f712c9SDave Chinner 	 * be used in the cluster buffer loop below.
29730f712c9SDave Chinner 	 *
29830f712c9SDave Chinner 	 * Further, because we are writing the inode directly into the buffer
29930f712c9SDave Chinner 	 * and calculating a CRC on the entire inode, we have ot log the entire
30030f712c9SDave Chinner 	 * inode so that the entire range the CRC covers is present in the log.
30130f712c9SDave Chinner 	 * That means for v3 inode we log the entire buffer rather than just the
30230f712c9SDave Chinner 	 * inode cores.
30330f712c9SDave Chinner 	 */
304ebd9027dSDave Chinner 	if (xfs_has_v3inodes(mp)) {
30530f712c9SDave Chinner 		version = 3;
30643004b2aSDarrick J. Wong 		ino = XFS_AGINO_TO_INO(mp, agno, XFS_AGB_TO_AGINO(mp, agbno));
30730f712c9SDave Chinner 
30830f712c9SDave Chinner 		/*
30930f712c9SDave Chinner 		 * log the initialisation that is about to take place as an
31030f712c9SDave Chinner 		 * logical operation. This means the transaction does not
31130f712c9SDave Chinner 		 * need to log the physical changes to the inode buffers as log
31230f712c9SDave Chinner 		 * recovery will know what initialisation is actually needed.
31330f712c9SDave Chinner 		 * Hence we only need to log the buffers as "ordered" buffers so
31430f712c9SDave Chinner 		 * they track in the AIL as if they were physically logged.
31530f712c9SDave Chinner 		 */
31630f712c9SDave Chinner 		if (tp)
317463958afSBrian Foster 			xfs_icreate_log(tp, agno, agbno, icount,
31830f712c9SDave Chinner 					mp->m_sb.sb_inodesize, length, gen);
31930f712c9SDave Chinner 	} else
32030f712c9SDave Chinner 		version = 2;
32130f712c9SDave Chinner 
32230f712c9SDave Chinner 	for (j = 0; j < nbufs; j++) {
32330f712c9SDave Chinner 		/*
32430f712c9SDave Chinner 		 * Get the block.
32530f712c9SDave Chinner 		 */
32683dcdb44SDarrick J. Wong 		d = XFS_AGB_TO_DADDR(mp, agno, agbno +
327ef325959SDarrick J. Wong 				(j * M_IGEO(mp)->blocks_per_cluster));
328ce92464cSDarrick J. Wong 		error = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
329ce92464cSDarrick J. Wong 				mp->m_bsize * M_IGEO(mp)->blocks_per_cluster,
330ce92464cSDarrick J. Wong 				XBF_UNMAPPED, &fbuf);
331ce92464cSDarrick J. Wong 		if (error)
332ce92464cSDarrick J. Wong 			return error;
33330f712c9SDave Chinner 
33430f712c9SDave Chinner 		/* Initialize the inode buffers and log them appropriately. */
33530f712c9SDave Chinner 		fbuf->b_ops = &xfs_inode_buf_ops;
33630f712c9SDave Chinner 		xfs_buf_zero(fbuf, 0, BBTOB(fbuf->b_length));
337ef325959SDarrick J. Wong 		for (i = 0; i < M_IGEO(mp)->inodes_per_cluster; i++) {
33830f712c9SDave Chinner 			int	ioffset = i << mp->m_sb.sb_inodelog;
33930f712c9SDave Chinner 
34030f712c9SDave Chinner 			free = xfs_make_iptr(mp, fbuf, i);
34130f712c9SDave Chinner 			free->di_magic = cpu_to_be16(XFS_DINODE_MAGIC);
34230f712c9SDave Chinner 			free->di_version = version;
34330f712c9SDave Chinner 			free->di_gen = cpu_to_be32(gen);
34430f712c9SDave Chinner 			free->di_next_unlinked = cpu_to_be32(NULLAGINO);
34530f712c9SDave Chinner 
34630f712c9SDave Chinner 			if (version == 3) {
34730f712c9SDave Chinner 				free->di_ino = cpu_to_be64(ino);
34830f712c9SDave Chinner 				ino++;
349ce748eaaSEric Sandeen 				uuid_copy(&free->di_uuid,
350ce748eaaSEric Sandeen 					  &mp->m_sb.sb_meta_uuid);
35130f712c9SDave Chinner 				xfs_dinode_calc_crc(mp, free);
35230f712c9SDave Chinner 			} else if (tp) {
35330f712c9SDave Chinner 				/* just log the inode core */
35430f712c9SDave Chinner 				xfs_trans_log_buf(tp, fbuf, ioffset,
355cf28e17cSDave Chinner 					  ioffset + XFS_DINODE_SIZE(mp) - 1);
35630f712c9SDave Chinner 			}
35730f712c9SDave Chinner 		}
35830f712c9SDave Chinner 
35930f712c9SDave Chinner 		if (tp) {
36030f712c9SDave Chinner 			/*
36130f712c9SDave Chinner 			 * Mark the buffer as an inode allocation buffer so it
36230f712c9SDave Chinner 			 * sticks in AIL at the point of this allocation
36330f712c9SDave Chinner 			 * transaction. This ensures the they are on disk before
36430f712c9SDave Chinner 			 * the tail of the log can be moved past this
36530f712c9SDave Chinner 			 * transaction (i.e. by preventing relogging from moving
36630f712c9SDave Chinner 			 * it forward in the log).
36730f712c9SDave Chinner 			 */
36830f712c9SDave Chinner 			xfs_trans_inode_alloc_buf(tp, fbuf);
36930f712c9SDave Chinner 			if (version == 3) {
37030f712c9SDave Chinner 				/*
37130f712c9SDave Chinner 				 * Mark the buffer as ordered so that they are
37230f712c9SDave Chinner 				 * not physically logged in the transaction but
37330f712c9SDave Chinner 				 * still tracked in the AIL as part of the
37430f712c9SDave Chinner 				 * transaction and pin the log appropriately.
37530f712c9SDave Chinner 				 */
37630f712c9SDave Chinner 				xfs_trans_ordered_buf(tp, fbuf);
37730f712c9SDave Chinner 			}
37830f712c9SDave Chinner 		} else {
37930f712c9SDave Chinner 			fbuf->b_flags |= XBF_DONE;
38030f712c9SDave Chinner 			xfs_buf_delwri_queue(fbuf, buffer_list);
38130f712c9SDave Chinner 			xfs_buf_relse(fbuf);
38230f712c9SDave Chinner 		}
38330f712c9SDave Chinner 	}
38430f712c9SDave Chinner 	return 0;
38530f712c9SDave Chinner }
38630f712c9SDave Chinner 
38730f712c9SDave Chinner /*
38856d1115cSBrian Foster  * Align startino and allocmask for a recently allocated sparse chunk such that
38956d1115cSBrian Foster  * they are fit for insertion (or merge) into the on-disk inode btrees.
39056d1115cSBrian Foster  *
39156d1115cSBrian Foster  * Background:
39256d1115cSBrian Foster  *
39356d1115cSBrian Foster  * When enabled, sparse inode support increases the inode alignment from cluster
39456d1115cSBrian Foster  * size to inode chunk size. This means that the minimum range between two
39556d1115cSBrian Foster  * non-adjacent inode records in the inobt is large enough for a full inode
39656d1115cSBrian Foster  * record. This allows for cluster sized, cluster aligned block allocation
39756d1115cSBrian Foster  * without need to worry about whether the resulting inode record overlaps with
39856d1115cSBrian Foster  * another record in the tree. Without this basic rule, we would have to deal
39956d1115cSBrian Foster  * with the consequences of overlap by potentially undoing recent allocations in
40056d1115cSBrian Foster  * the inode allocation codepath.
40156d1115cSBrian Foster  *
40256d1115cSBrian Foster  * Because of this alignment rule (which is enforced on mount), there are two
40356d1115cSBrian Foster  * inobt possibilities for newly allocated sparse chunks. One is that the
40456d1115cSBrian Foster  * aligned inode record for the chunk covers a range of inodes not already
40556d1115cSBrian Foster  * covered in the inobt (i.e., it is safe to insert a new sparse record). The
40656d1115cSBrian Foster  * other is that a record already exists at the aligned startino that considers
40756d1115cSBrian Foster  * the newly allocated range as sparse. In the latter case, record content is
40856d1115cSBrian Foster  * merged in hope that sparse inode chunks fill to full chunks over time.
40956d1115cSBrian Foster  */
41056d1115cSBrian Foster STATIC void
41156d1115cSBrian Foster xfs_align_sparse_ino(
41256d1115cSBrian Foster 	struct xfs_mount		*mp,
41356d1115cSBrian Foster 	xfs_agino_t			*startino,
41456d1115cSBrian Foster 	uint16_t			*allocmask)
41556d1115cSBrian Foster {
41656d1115cSBrian Foster 	xfs_agblock_t			agbno;
41756d1115cSBrian Foster 	xfs_agblock_t			mod;
41856d1115cSBrian Foster 	int				offset;
41956d1115cSBrian Foster 
42056d1115cSBrian Foster 	agbno = XFS_AGINO_TO_AGBNO(mp, *startino);
42156d1115cSBrian Foster 	mod = agbno % mp->m_sb.sb_inoalignmt;
42256d1115cSBrian Foster 	if (!mod)
42356d1115cSBrian Foster 		return;
42456d1115cSBrian Foster 
42556d1115cSBrian Foster 	/* calculate the inode offset and align startino */
42643004b2aSDarrick J. Wong 	offset = XFS_AGB_TO_AGINO(mp, mod);
42756d1115cSBrian Foster 	*startino -= offset;
42856d1115cSBrian Foster 
42956d1115cSBrian Foster 	/*
43056d1115cSBrian Foster 	 * Since startino has been aligned down, left shift allocmask such that
43156d1115cSBrian Foster 	 * it continues to represent the same physical inodes relative to the
43256d1115cSBrian Foster 	 * new startino.
43356d1115cSBrian Foster 	 */
43456d1115cSBrian Foster 	*allocmask <<= offset / XFS_INODES_PER_HOLEMASK_BIT;
43556d1115cSBrian Foster }
43656d1115cSBrian Foster 
43756d1115cSBrian Foster /*
43856d1115cSBrian Foster  * Determine whether the source inode record can merge into the target. Both
43956d1115cSBrian Foster  * records must be sparse, the inode ranges must match and there must be no
44056d1115cSBrian Foster  * allocation overlap between the records.
44156d1115cSBrian Foster  */
44256d1115cSBrian Foster STATIC bool
44356d1115cSBrian Foster __xfs_inobt_can_merge(
44456d1115cSBrian Foster 	struct xfs_inobt_rec_incore	*trec,	/* tgt record */
44556d1115cSBrian Foster 	struct xfs_inobt_rec_incore	*srec)	/* src record */
44656d1115cSBrian Foster {
44756d1115cSBrian Foster 	uint64_t			talloc;
44856d1115cSBrian Foster 	uint64_t			salloc;
44956d1115cSBrian Foster 
45056d1115cSBrian Foster 	/* records must cover the same inode range */
45156d1115cSBrian Foster 	if (trec->ir_startino != srec->ir_startino)
45256d1115cSBrian Foster 		return false;
45356d1115cSBrian Foster 
45456d1115cSBrian Foster 	/* both records must be sparse */
45556d1115cSBrian Foster 	if (!xfs_inobt_issparse(trec->ir_holemask) ||
45656d1115cSBrian Foster 	    !xfs_inobt_issparse(srec->ir_holemask))
45756d1115cSBrian Foster 		return false;
45856d1115cSBrian Foster 
45956d1115cSBrian Foster 	/* both records must track some inodes */
46056d1115cSBrian Foster 	if (!trec->ir_count || !srec->ir_count)
46156d1115cSBrian Foster 		return false;
46256d1115cSBrian Foster 
46356d1115cSBrian Foster 	/* can't exceed capacity of a full record */
46456d1115cSBrian Foster 	if (trec->ir_count + srec->ir_count > XFS_INODES_PER_CHUNK)
46556d1115cSBrian Foster 		return false;
46656d1115cSBrian Foster 
46756d1115cSBrian Foster 	/* verify there is no allocation overlap */
46856d1115cSBrian Foster 	talloc = xfs_inobt_irec_to_allocmask(trec);
46956d1115cSBrian Foster 	salloc = xfs_inobt_irec_to_allocmask(srec);
47056d1115cSBrian Foster 	if (talloc & salloc)
47156d1115cSBrian Foster 		return false;
47256d1115cSBrian Foster 
47356d1115cSBrian Foster 	return true;
47456d1115cSBrian Foster }
47556d1115cSBrian Foster 
47656d1115cSBrian Foster /*
47756d1115cSBrian Foster  * Merge the source inode record into the target. The caller must call
47856d1115cSBrian Foster  * __xfs_inobt_can_merge() to ensure the merge is valid.
47956d1115cSBrian Foster  */
48056d1115cSBrian Foster STATIC void
48156d1115cSBrian Foster __xfs_inobt_rec_merge(
48256d1115cSBrian Foster 	struct xfs_inobt_rec_incore	*trec,	/* target */
48356d1115cSBrian Foster 	struct xfs_inobt_rec_incore	*srec)	/* src */
48456d1115cSBrian Foster {
48556d1115cSBrian Foster 	ASSERT(trec->ir_startino == srec->ir_startino);
48656d1115cSBrian Foster 
48756d1115cSBrian Foster 	/* combine the counts */
48856d1115cSBrian Foster 	trec->ir_count += srec->ir_count;
48956d1115cSBrian Foster 	trec->ir_freecount += srec->ir_freecount;
49056d1115cSBrian Foster 
49156d1115cSBrian Foster 	/*
49256d1115cSBrian Foster 	 * Merge the holemask and free mask. For both fields, 0 bits refer to
49356d1115cSBrian Foster 	 * allocated inodes. We combine the allocated ranges with bitwise AND.
49456d1115cSBrian Foster 	 */
49556d1115cSBrian Foster 	trec->ir_holemask &= srec->ir_holemask;
49656d1115cSBrian Foster 	trec->ir_free &= srec->ir_free;
49756d1115cSBrian Foster }
49856d1115cSBrian Foster 
49956d1115cSBrian Foster /*
50056d1115cSBrian Foster  * Insert a new sparse inode chunk into the associated inode btree. The inode
50156d1115cSBrian Foster  * record for the sparse chunk is pre-aligned to a startino that should match
50256d1115cSBrian Foster  * any pre-existing sparse inode record in the tree. This allows sparse chunks
50356d1115cSBrian Foster  * to fill over time.
50456d1115cSBrian Foster  *
50556d1115cSBrian Foster  * This function supports two modes of handling preexisting records depending on
50656d1115cSBrian Foster  * the merge flag. If merge is true, the provided record is merged with the
50756d1115cSBrian Foster  * existing record and updated in place. The merged record is returned in nrec.
50856d1115cSBrian Foster  * If merge is false, an existing record is replaced with the provided record.
50956d1115cSBrian Foster  * If no preexisting record exists, the provided record is always inserted.
51056d1115cSBrian Foster  *
51156d1115cSBrian Foster  * It is considered corruption if a merge is requested and not possible. Given
51256d1115cSBrian Foster  * the sparse inode alignment constraints, this should never happen.
51356d1115cSBrian Foster  */
51456d1115cSBrian Foster STATIC int
51556d1115cSBrian Foster xfs_inobt_insert_sprec(
516dedab3e4SDave Chinner 	struct xfs_perag		*pag,
51756d1115cSBrian Foster 	struct xfs_trans		*tp,
51856d1115cSBrian Foster 	struct xfs_buf			*agbp,
51956d1115cSBrian Foster 	int				btnum,
52056d1115cSBrian Foster 	struct xfs_inobt_rec_incore	*nrec,	/* in/out: new/merged rec. */
52156d1115cSBrian Foster 	bool				merge)	/* merge or replace */
52256d1115cSBrian Foster {
523dedab3e4SDave Chinner 	struct xfs_mount		*mp = pag->pag_mount;
52456d1115cSBrian Foster 	struct xfs_btree_cur		*cur;
52556d1115cSBrian Foster 	int				error;
52656d1115cSBrian Foster 	int				i;
52756d1115cSBrian Foster 	struct xfs_inobt_rec_incore	rec;
52856d1115cSBrian Foster 
529bab8b795SDave Chinner 	cur = xfs_inobt_init_cursor(pag, tp, agbp, btnum);
53056d1115cSBrian Foster 
53156d1115cSBrian Foster 	/* the new record is pre-aligned so we know where to look */
53256d1115cSBrian Foster 	error = xfs_inobt_lookup(cur, nrec->ir_startino, XFS_LOOKUP_EQ, &i);
53356d1115cSBrian Foster 	if (error)
53456d1115cSBrian Foster 		goto error;
53556d1115cSBrian Foster 	/* if nothing there, insert a new record and return */
53656d1115cSBrian Foster 	if (i == 0) {
53756d1115cSBrian Foster 		error = xfs_inobt_insert_rec(cur, nrec->ir_holemask,
53856d1115cSBrian Foster 					     nrec->ir_count, nrec->ir_freecount,
53956d1115cSBrian Foster 					     nrec->ir_free, &i);
54056d1115cSBrian Foster 		if (error)
54156d1115cSBrian Foster 			goto error;
542f9e03706SDarrick J. Wong 		if (XFS_IS_CORRUPT(mp, i != 1)) {
543f9e03706SDarrick J. Wong 			error = -EFSCORRUPTED;
544f9e03706SDarrick J. Wong 			goto error;
545f9e03706SDarrick J. Wong 		}
54656d1115cSBrian Foster 
54756d1115cSBrian Foster 		goto out;
54856d1115cSBrian Foster 	}
54956d1115cSBrian Foster 
55056d1115cSBrian Foster 	/*
55156d1115cSBrian Foster 	 * A record exists at this startino. Merge or replace the record
55256d1115cSBrian Foster 	 * depending on what we've been asked to do.
55356d1115cSBrian Foster 	 */
55456d1115cSBrian Foster 	if (merge) {
55556d1115cSBrian Foster 		error = xfs_inobt_get_rec(cur, &rec, &i);
55656d1115cSBrian Foster 		if (error)
55756d1115cSBrian Foster 			goto error;
558f9e03706SDarrick J. Wong 		if (XFS_IS_CORRUPT(mp, i != 1)) {
559f9e03706SDarrick J. Wong 			error = -EFSCORRUPTED;
560f9e03706SDarrick J. Wong 			goto error;
561f9e03706SDarrick J. Wong 		}
562f9e03706SDarrick J. Wong 		if (XFS_IS_CORRUPT(mp, rec.ir_startino != nrec->ir_startino)) {
563f9e03706SDarrick J. Wong 			error = -EFSCORRUPTED;
564f9e03706SDarrick J. Wong 			goto error;
565f9e03706SDarrick J. Wong 		}
56656d1115cSBrian Foster 
56756d1115cSBrian Foster 		/*
56856d1115cSBrian Foster 		 * This should never fail. If we have coexisting records that
56956d1115cSBrian Foster 		 * cannot merge, something is seriously wrong.
57056d1115cSBrian Foster 		 */
571f9e03706SDarrick J. Wong 		if (XFS_IS_CORRUPT(mp, !__xfs_inobt_can_merge(nrec, &rec))) {
572f9e03706SDarrick J. Wong 			error = -EFSCORRUPTED;
573f9e03706SDarrick J. Wong 			goto error;
574f9e03706SDarrick J. Wong 		}
57556d1115cSBrian Foster 
5767b13c515SDave Chinner 		trace_xfs_irec_merge_pre(mp, pag->pag_agno, rec.ir_startino,
57756d1115cSBrian Foster 					 rec.ir_holemask, nrec->ir_startino,
57856d1115cSBrian Foster 					 nrec->ir_holemask);
57956d1115cSBrian Foster 
58056d1115cSBrian Foster 		/* merge to nrec to output the updated record */
58156d1115cSBrian Foster 		__xfs_inobt_rec_merge(nrec, &rec);
58256d1115cSBrian Foster 
5837b13c515SDave Chinner 		trace_xfs_irec_merge_post(mp, pag->pag_agno, nrec->ir_startino,
58456d1115cSBrian Foster 					  nrec->ir_holemask);
58556d1115cSBrian Foster 
58656d1115cSBrian Foster 		error = xfs_inobt_rec_check_count(mp, nrec);
58756d1115cSBrian Foster 		if (error)
58856d1115cSBrian Foster 			goto error;
58956d1115cSBrian Foster 	}
59056d1115cSBrian Foster 
59156d1115cSBrian Foster 	error = xfs_inobt_update(cur, nrec);
59256d1115cSBrian Foster 	if (error)
59356d1115cSBrian Foster 		goto error;
59456d1115cSBrian Foster 
59556d1115cSBrian Foster out:
59656d1115cSBrian Foster 	xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
59756d1115cSBrian Foster 	return 0;
59856d1115cSBrian Foster error:
59956d1115cSBrian Foster 	xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
60056d1115cSBrian Foster 	return error;
60156d1115cSBrian Foster }
60256d1115cSBrian Foster 
60356d1115cSBrian Foster /*
6048237fbf5SDave Chinner  * Allocate new inodes in the allocation group specified by agbp.  Returns 0 if
6058237fbf5SDave Chinner  * inodes were allocated in this AG; -EAGAIN if there was no space in this AG so
6068237fbf5SDave Chinner  * the caller knows it can try another AG, a hard -ENOSPC when over the maximum
6078237fbf5SDave Chinner  * inode count threshold, or the usual negative error code for other errors.
60830f712c9SDave Chinner  */
609ef325959SDarrick J. Wong STATIC int
61030f712c9SDave Chinner xfs_ialloc_ag_alloc(
611dedab3e4SDave Chinner 	struct xfs_perag	*pag,
612ef325959SDarrick J. Wong 	struct xfs_trans	*tp,
613dedab3e4SDave Chinner 	struct xfs_buf		*agbp)
61430f712c9SDave Chinner {
615ef325959SDarrick J. Wong 	struct xfs_agi		*agi;
616ef325959SDarrick J. Wong 	struct xfs_alloc_arg	args;
61730f712c9SDave Chinner 	int			error;
61830f712c9SDave Chinner 	xfs_agino_t		newino;		/* new first inode's number */
61930f712c9SDave Chinner 	xfs_agino_t		newlen;		/* new number of inodes */
620ef325959SDarrick J. Wong 	int			isaligned = 0;	/* inode allocation at stripe */
621ef325959SDarrick J. Wong 						/* unit boundary */
622ef325959SDarrick J. Wong 	/* init. to full chunk */
62356d1115cSBrian Foster 	struct xfs_inobt_rec_incore rec;
624ef325959SDarrick J. Wong 	struct xfs_ino_geometry	*igeo = M_IGEO(tp->t_mountp);
6257b13c515SDave Chinner 	uint16_t		allocmask = (uint16_t) -1;
6261cdadee1SBrian Foster 	int			do_sparse = 0;
6271cdadee1SBrian Foster 
62830f712c9SDave Chinner 	memset(&args, 0, sizeof(args));
62930f712c9SDave Chinner 	args.tp = tp;
63030f712c9SDave Chinner 	args.mp = tp->t_mountp;
6311cdadee1SBrian Foster 	args.fsbno = NULLFSBLOCK;
6327280fedaSDarrick J. Wong 	args.oinfo = XFS_RMAP_OINFO_INODES;
63374c36a86SDave Chinner 	args.pag = pag;
63430f712c9SDave Chinner 
63546fc58daSBrian Foster #ifdef DEBUG
63646fc58daSBrian Foster 	/* randomly do sparse inode allocations */
637ebd9027dSDave Chinner 	if (xfs_has_sparseinodes(tp->t_mountp) &&
638ef325959SDarrick J. Wong 	    igeo->ialloc_min_blks < igeo->ialloc_blks)
6398032bf12SJason A. Donenfeld 		do_sparse = get_random_u32_below(2);
64046fc58daSBrian Foster #endif
64146fc58daSBrian Foster 
64230f712c9SDave Chinner 	/*
64330f712c9SDave Chinner 	 * Locking will ensure that we don't have two callers in here
64430f712c9SDave Chinner 	 * at one time.
64530f712c9SDave Chinner 	 */
646ef325959SDarrick J. Wong 	newlen = igeo->ialloc_inos;
647ef325959SDarrick J. Wong 	if (igeo->maxicount &&
64874f9ce1cSGeorge Wang 	    percpu_counter_read_positive(&args.mp->m_icount) + newlen >
649ef325959SDarrick J. Wong 							igeo->maxicount)
6502451337dSDave Chinner 		return -ENOSPC;
651ef325959SDarrick J. Wong 	args.minlen = args.maxlen = igeo->ialloc_blks;
65230f712c9SDave Chinner 	/*
65330f712c9SDave Chinner 	 * First try to allocate inodes contiguous with the last-allocated
65430f712c9SDave Chinner 	 * chunk of inodes.  If the filesystem is striped, this will fill
65530f712c9SDave Chinner 	 * an entire stripe unit with inodes.
65630f712c9SDave Chinner 	 */
657370c782bSChristoph Hellwig 	agi = agbp->b_addr;
65830f712c9SDave Chinner 	newino = be32_to_cpu(agi->agi_newino);
65930f712c9SDave Chinner 	args.agbno = XFS_AGINO_TO_AGBNO(args.mp, newino) +
660ef325959SDarrick J. Wong 		     igeo->ialloc_blks;
6611cdadee1SBrian Foster 	if (do_sparse)
6621cdadee1SBrian Foster 		goto sparse_alloc;
66330f712c9SDave Chinner 	if (likely(newino != NULLAGINO &&
66430f712c9SDave Chinner 		  (args.agbno < be32_to_cpu(agi->agi_length)))) {
66530f712c9SDave Chinner 		args.prod = 1;
66630f712c9SDave Chinner 
66730f712c9SDave Chinner 		/*
66830f712c9SDave Chinner 		 * We need to take into account alignment here to ensure that
66930f712c9SDave Chinner 		 * we don't modify the free list if we fail to have an exact
67030f712c9SDave Chinner 		 * block. If we don't have an exact match, and every oher
67130f712c9SDave Chinner 		 * attempt allocation attempt fails, we'll end up cancelling
67230f712c9SDave Chinner 		 * a dirty transaction and shutting down.
67330f712c9SDave Chinner 		 *
67430f712c9SDave Chinner 		 * For an exact allocation, alignment must be 1,
67530f712c9SDave Chinner 		 * however we need to take cluster alignment into account when
67630f712c9SDave Chinner 		 * fixing up the freelist. Use the minalignslop field to
67730f712c9SDave Chinner 		 * indicate that extra blocks might be required for alignment,
67830f712c9SDave Chinner 		 * but not to use them in the actual exact allocation.
67930f712c9SDave Chinner 		 */
68030f712c9SDave Chinner 		args.alignment = 1;
681ef325959SDarrick J. Wong 		args.minalignslop = igeo->cluster_align - 1;
68230f712c9SDave Chinner 
68330f712c9SDave Chinner 		/* Allow space for the inode btree to split. */
684657f1019SBrian Foster 		args.minleft = igeo->inobt_maxlevels;
6855f36b2ceSDave Chinner 		error = xfs_alloc_vextent_exact_bno(&args,
6865f36b2ceSDave Chinner 				XFS_AGB_TO_FSB(args.mp, pag->pag_agno,
6875f36b2ceSDave Chinner 						args.agbno));
68874c36a86SDave Chinner 		if (error)
68930f712c9SDave Chinner 			return error;
69030f712c9SDave Chinner 
69130f712c9SDave Chinner 		/*
69230f712c9SDave Chinner 		 * This request might have dirtied the transaction if the AG can
69330f712c9SDave Chinner 		 * satisfy the request, but the exact block was not available.
69430f712c9SDave Chinner 		 * If the allocation did fail, subsequent requests will relax
69530f712c9SDave Chinner 		 * the exact agbno requirement and increase the alignment
69630f712c9SDave Chinner 		 * instead. It is critical that the total size of the request
69730f712c9SDave Chinner 		 * (len + alignment + slop) does not increase from this point
69830f712c9SDave Chinner 		 * on, so reset minalignslop to ensure it is not included in
69930f712c9SDave Chinner 		 * subsequent requests.
70030f712c9SDave Chinner 		 */
70130f712c9SDave Chinner 		args.minalignslop = 0;
7021cdadee1SBrian Foster 	}
70330f712c9SDave Chinner 
70430f712c9SDave Chinner 	if (unlikely(args.fsbno == NULLFSBLOCK)) {
70530f712c9SDave Chinner 		/*
70630f712c9SDave Chinner 		 * Set the alignment for the allocation.
70730f712c9SDave Chinner 		 * If stripe alignment is turned on then align at stripe unit
70830f712c9SDave Chinner 		 * boundary.
70930f712c9SDave Chinner 		 * If the cluster size is smaller than a filesystem block
71030f712c9SDave Chinner 		 * then we're doing I/O for inodes in filesystem block size
71130f712c9SDave Chinner 		 * pieces, so don't need alignment anyway.
71230f712c9SDave Chinner 		 */
71330f712c9SDave Chinner 		isaligned = 0;
714ef325959SDarrick J. Wong 		if (igeo->ialloc_align) {
7150560f31aSDave Chinner 			ASSERT(!xfs_has_noalign(args.mp));
71630f712c9SDave Chinner 			args.alignment = args.mp->m_dalign;
71730f712c9SDave Chinner 			isaligned = 1;
71830f712c9SDave Chinner 		} else
719ef325959SDarrick J. Wong 			args.alignment = igeo->cluster_align;
72030f712c9SDave Chinner 		/*
72130f712c9SDave Chinner 		 * Allocate a fixed-size extent of inodes.
72230f712c9SDave Chinner 		 */
72330f712c9SDave Chinner 		args.prod = 1;
72430f712c9SDave Chinner 		/*
72530f712c9SDave Chinner 		 * Allow space for the inode btree to split.
72630f712c9SDave Chinner 		 */
727657f1019SBrian Foster 		args.minleft = igeo->inobt_maxlevels;
728db4710fdSDave Chinner 		error = xfs_alloc_vextent_near_bno(&args,
729db4710fdSDave Chinner 				XFS_AGB_TO_FSB(args.mp, pag->pag_agno,
730db4710fdSDave Chinner 						be32_to_cpu(agi->agi_root)));
73174c36a86SDave Chinner 		if (error)
73230f712c9SDave Chinner 			return error;
73330f712c9SDave Chinner 	}
73430f712c9SDave Chinner 
73530f712c9SDave Chinner 	/*
73630f712c9SDave Chinner 	 * If stripe alignment is turned on, then try again with cluster
73730f712c9SDave Chinner 	 * alignment.
73830f712c9SDave Chinner 	 */
73930f712c9SDave Chinner 	if (isaligned && args.fsbno == NULLFSBLOCK) {
740ef325959SDarrick J. Wong 		args.alignment = igeo->cluster_align;
741db4710fdSDave Chinner 		error = xfs_alloc_vextent_near_bno(&args,
742db4710fdSDave Chinner 				XFS_AGB_TO_FSB(args.mp, pag->pag_agno,
743db4710fdSDave Chinner 						be32_to_cpu(agi->agi_root)));
744db4710fdSDave Chinner 		if (error)
74530f712c9SDave Chinner 			return error;
74630f712c9SDave Chinner 	}
74730f712c9SDave Chinner 
74856d1115cSBrian Foster 	/*
74956d1115cSBrian Foster 	 * Finally, try a sparse allocation if the filesystem supports it and
75056d1115cSBrian Foster 	 * the sparse allocation length is smaller than a full chunk.
75156d1115cSBrian Foster 	 */
752ebd9027dSDave Chinner 	if (xfs_has_sparseinodes(args.mp) &&
753ef325959SDarrick J. Wong 	    igeo->ialloc_min_blks < igeo->ialloc_blks &&
75456d1115cSBrian Foster 	    args.fsbno == NULLFSBLOCK) {
7551cdadee1SBrian Foster sparse_alloc:
75656d1115cSBrian Foster 		args.alignment = args.mp->m_sb.sb_spino_align;
75756d1115cSBrian Foster 		args.prod = 1;
75856d1115cSBrian Foster 
759ef325959SDarrick J. Wong 		args.minlen = igeo->ialloc_min_blks;
76056d1115cSBrian Foster 		args.maxlen = args.minlen;
76156d1115cSBrian Foster 
76256d1115cSBrian Foster 		/*
76356d1115cSBrian Foster 		 * The inode record will be aligned to full chunk size. We must
76456d1115cSBrian Foster 		 * prevent sparse allocation from AG boundaries that result in
76556d1115cSBrian Foster 		 * invalid inode records, such as records that start at agbno 0
76656d1115cSBrian Foster 		 * or extend beyond the AG.
76756d1115cSBrian Foster 		 *
76856d1115cSBrian Foster 		 * Set min agbno to the first aligned, non-zero agbno and max to
76956d1115cSBrian Foster 		 * the last aligned agbno that is at least one full chunk from
77056d1115cSBrian Foster 		 * the end of the AG.
77156d1115cSBrian Foster 		 */
77256d1115cSBrian Foster 		args.min_agbno = args.mp->m_sb.sb_inoalignmt;
77356d1115cSBrian Foster 		args.max_agbno = round_down(args.mp->m_sb.sb_agblocks,
77456d1115cSBrian Foster 					    args.mp->m_sb.sb_inoalignmt) -
775ef325959SDarrick J. Wong 				 igeo->ialloc_blks;
77656d1115cSBrian Foster 
777db4710fdSDave Chinner 		error = xfs_alloc_vextent_near_bno(&args,
778db4710fdSDave Chinner 				XFS_AGB_TO_FSB(args.mp, pag->pag_agno,
779db4710fdSDave Chinner 						be32_to_cpu(agi->agi_root)));
78056d1115cSBrian Foster 		if (error)
78156d1115cSBrian Foster 			return error;
78256d1115cSBrian Foster 
78343004b2aSDarrick J. Wong 		newlen = XFS_AGB_TO_AGINO(args.mp, args.len);
78446fc58daSBrian Foster 		ASSERT(newlen <= XFS_INODES_PER_CHUNK);
78556d1115cSBrian Foster 		allocmask = (1 << (newlen / XFS_INODES_PER_HOLEMASK_BIT)) - 1;
78656d1115cSBrian Foster 	}
78756d1115cSBrian Foster 
7883937493cSGao Xiang 	if (args.fsbno == NULLFSBLOCK)
7898237fbf5SDave Chinner 		return -EAGAIN;
7903937493cSGao Xiang 
79130f712c9SDave Chinner 	ASSERT(args.len == args.minlen);
79230f712c9SDave Chinner 
79330f712c9SDave Chinner 	/*
79430f712c9SDave Chinner 	 * Stamp and write the inode buffers.
79530f712c9SDave Chinner 	 *
79630f712c9SDave Chinner 	 * Seed the new inode cluster with a random generation number. This
79730f712c9SDave Chinner 	 * prevents short-term reuse of generation numbers if a chunk is
79830f712c9SDave Chinner 	 * freed and then immediately reallocated. We use random numbers
79930f712c9SDave Chinner 	 * rather than a linear progression to prevent the next generation
80030f712c9SDave Chinner 	 * number from being easily guessable.
80130f712c9SDave Chinner 	 */
8027b13c515SDave Chinner 	error = xfs_ialloc_inode_init(args.mp, tp, NULL, newlen, pag->pag_agno,
803a251c17aSJason A. Donenfeld 			args.agbno, args.len, get_random_u32());
80430f712c9SDave Chinner 
80530f712c9SDave Chinner 	if (error)
80630f712c9SDave Chinner 		return error;
80730f712c9SDave Chinner 	/*
80830f712c9SDave Chinner 	 * Convert the results.
80930f712c9SDave Chinner 	 */
81043004b2aSDarrick J. Wong 	newino = XFS_AGB_TO_AGINO(args.mp, args.agbno);
81156d1115cSBrian Foster 
81256d1115cSBrian Foster 	if (xfs_inobt_issparse(~allocmask)) {
81356d1115cSBrian Foster 		/*
81456d1115cSBrian Foster 		 * We've allocated a sparse chunk. Align the startino and mask.
81556d1115cSBrian Foster 		 */
81656d1115cSBrian Foster 		xfs_align_sparse_ino(args.mp, &newino, &allocmask);
81756d1115cSBrian Foster 
81856d1115cSBrian Foster 		rec.ir_startino = newino;
81956d1115cSBrian Foster 		rec.ir_holemask = ~allocmask;
82056d1115cSBrian Foster 		rec.ir_count = newlen;
82156d1115cSBrian Foster 		rec.ir_freecount = newlen;
82256d1115cSBrian Foster 		rec.ir_free = XFS_INOBT_ALL_FREE;
82356d1115cSBrian Foster 
82456d1115cSBrian Foster 		/*
82556d1115cSBrian Foster 		 * Insert the sparse record into the inobt and allow for a merge
82656d1115cSBrian Foster 		 * if necessary. If a merge does occur, rec is updated to the
82756d1115cSBrian Foster 		 * merged record.
82856d1115cSBrian Foster 		 */
829dedab3e4SDave Chinner 		error = xfs_inobt_insert_sprec(pag, tp, agbp,
8307b13c515SDave Chinner 				XFS_BTNUM_INO, &rec, true);
83156d1115cSBrian Foster 		if (error == -EFSCORRUPTED) {
83256d1115cSBrian Foster 			xfs_alert(args.mp,
83356d1115cSBrian Foster 	"invalid sparse inode record: ino 0x%llx holemask 0x%x count %u",
8347b13c515SDave Chinner 				  XFS_AGINO_TO_INO(args.mp, pag->pag_agno,
83556d1115cSBrian Foster 						   rec.ir_startino),
83656d1115cSBrian Foster 				  rec.ir_holemask, rec.ir_count);
83756d1115cSBrian Foster 			xfs_force_shutdown(args.mp, SHUTDOWN_CORRUPT_INCORE);
83856d1115cSBrian Foster 		}
83956d1115cSBrian Foster 		if (error)
84056d1115cSBrian Foster 			return error;
84156d1115cSBrian Foster 
84256d1115cSBrian Foster 		/*
84356d1115cSBrian Foster 		 * We can't merge the part we've just allocated as for the inobt
84456d1115cSBrian Foster 		 * due to finobt semantics. The original record may or may not
84556d1115cSBrian Foster 		 * exist independent of whether physical inodes exist in this
84656d1115cSBrian Foster 		 * sparse chunk.
84756d1115cSBrian Foster 		 *
84856d1115cSBrian Foster 		 * We must update the finobt record based on the inobt record.
84956d1115cSBrian Foster 		 * rec contains the fully merged and up to date inobt record
85056d1115cSBrian Foster 		 * from the previous call. Set merge false to replace any
85156d1115cSBrian Foster 		 * existing record with this one.
85256d1115cSBrian Foster 		 */
853ebd9027dSDave Chinner 		if (xfs_has_finobt(args.mp)) {
854dedab3e4SDave Chinner 			error = xfs_inobt_insert_sprec(pag, tp, agbp,
8557b13c515SDave Chinner 				       XFS_BTNUM_FINO, &rec, false);
85656d1115cSBrian Foster 			if (error)
85756d1115cSBrian Foster 				return error;
85856d1115cSBrian Foster 		}
85956d1115cSBrian Foster 	} else {
86056d1115cSBrian Foster 		/* full chunk - insert new records to both btrees */
861dedab3e4SDave Chinner 		error = xfs_inobt_insert(pag, tp, agbp, newino, newlen,
86256d1115cSBrian Foster 					 XFS_BTNUM_INO);
86356d1115cSBrian Foster 		if (error)
86456d1115cSBrian Foster 			return error;
86556d1115cSBrian Foster 
866ebd9027dSDave Chinner 		if (xfs_has_finobt(args.mp)) {
867dedab3e4SDave Chinner 			error = xfs_inobt_insert(pag, tp, agbp, newino,
86856d1115cSBrian Foster 						 newlen, XFS_BTNUM_FINO);
86956d1115cSBrian Foster 			if (error)
87056d1115cSBrian Foster 				return error;
87156d1115cSBrian Foster 		}
87256d1115cSBrian Foster 	}
87356d1115cSBrian Foster 
87456d1115cSBrian Foster 	/*
87556d1115cSBrian Foster 	 * Update AGI counts and newino.
87656d1115cSBrian Foster 	 */
87730f712c9SDave Chinner 	be32_add_cpu(&agi->agi_count, newlen);
87830f712c9SDave Chinner 	be32_add_cpu(&agi->agi_freecount, newlen);
87930f712c9SDave Chinner 	pag->pagi_freecount += newlen;
88089e9b5c0SDarrick J. Wong 	pag->pagi_count += newlen;
88130f712c9SDave Chinner 	agi->agi_newino = cpu_to_be32(newino);
88230f712c9SDave Chinner 
88330f712c9SDave Chinner 	/*
88430f712c9SDave Chinner 	 * Log allocation group header fields
88530f712c9SDave Chinner 	 */
88630f712c9SDave Chinner 	xfs_ialloc_log_agi(tp, agbp,
88730f712c9SDave Chinner 		XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO);
88830f712c9SDave Chinner 	/*
88930f712c9SDave Chinner 	 * Modify/log superblock values for inode count and inode free count.
89030f712c9SDave Chinner 	 */
89130f712c9SDave Chinner 	xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen);
89230f712c9SDave Chinner 	xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen);
89330f712c9SDave Chinner 	return 0;
89430f712c9SDave Chinner }
89530f712c9SDave Chinner 
89630f712c9SDave Chinner /*
89730f712c9SDave Chinner  * Try to retrieve the next record to the left/right from the current one.
89830f712c9SDave Chinner  */
89930f712c9SDave Chinner STATIC int
90030f712c9SDave Chinner xfs_ialloc_next_rec(
90130f712c9SDave Chinner 	struct xfs_btree_cur	*cur,
90230f712c9SDave Chinner 	xfs_inobt_rec_incore_t	*rec,
90330f712c9SDave Chinner 	int			*done,
90430f712c9SDave Chinner 	int			left)
90530f712c9SDave Chinner {
90630f712c9SDave Chinner 	int                     error;
90730f712c9SDave Chinner 	int			i;
90830f712c9SDave Chinner 
90930f712c9SDave Chinner 	if (left)
91030f712c9SDave Chinner 		error = xfs_btree_decrement(cur, 0, &i);
91130f712c9SDave Chinner 	else
91230f712c9SDave Chinner 		error = xfs_btree_increment(cur, 0, &i);
91330f712c9SDave Chinner 
91430f712c9SDave Chinner 	if (error)
91530f712c9SDave Chinner 		return error;
91630f712c9SDave Chinner 	*done = !i;
91730f712c9SDave Chinner 	if (i) {
91830f712c9SDave Chinner 		error = xfs_inobt_get_rec(cur, rec, &i);
91930f712c9SDave Chinner 		if (error)
92030f712c9SDave Chinner 			return error;
921f9e03706SDarrick J. Wong 		if (XFS_IS_CORRUPT(cur->bc_mp, i != 1))
922f9e03706SDarrick J. Wong 			return -EFSCORRUPTED;
92330f712c9SDave Chinner 	}
92430f712c9SDave Chinner 
92530f712c9SDave Chinner 	return 0;
92630f712c9SDave Chinner }
92730f712c9SDave Chinner 
92830f712c9SDave Chinner STATIC int
92930f712c9SDave Chinner xfs_ialloc_get_rec(
93030f712c9SDave Chinner 	struct xfs_btree_cur	*cur,
93130f712c9SDave Chinner 	xfs_agino_t		agino,
93230f712c9SDave Chinner 	xfs_inobt_rec_incore_t	*rec,
93330f712c9SDave Chinner 	int			*done)
93430f712c9SDave Chinner {
93530f712c9SDave Chinner 	int                     error;
93630f712c9SDave Chinner 	int			i;
93730f712c9SDave Chinner 
93830f712c9SDave Chinner 	error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_EQ, &i);
93930f712c9SDave Chinner 	if (error)
94030f712c9SDave Chinner 		return error;
94130f712c9SDave Chinner 	*done = !i;
94230f712c9SDave Chinner 	if (i) {
94330f712c9SDave Chinner 		error = xfs_inobt_get_rec(cur, rec, &i);
94430f712c9SDave Chinner 		if (error)
94530f712c9SDave Chinner 			return error;
946f9e03706SDarrick J. Wong 		if (XFS_IS_CORRUPT(cur->bc_mp, i != 1))
947f9e03706SDarrick J. Wong 			return -EFSCORRUPTED;
94830f712c9SDave Chinner 	}
94930f712c9SDave Chinner 
95030f712c9SDave Chinner 	return 0;
95130f712c9SDave Chinner }
95230f712c9SDave Chinner 
95330f712c9SDave Chinner /*
95426dd5217SBrian Foster  * Return the offset of the first free inode in the record. If the inode chunk
95526dd5217SBrian Foster  * is sparsely allocated, we convert the record holemask to inode granularity
95626dd5217SBrian Foster  * and mask off the unallocated regions from the inode free mask.
957d4cc540bSBrian Foster  */
958d4cc540bSBrian Foster STATIC int
959d4cc540bSBrian Foster xfs_inobt_first_free_inode(
960d4cc540bSBrian Foster 	struct xfs_inobt_rec_incore	*rec)
961d4cc540bSBrian Foster {
96226dd5217SBrian Foster 	xfs_inofree_t			realfree;
96326dd5217SBrian Foster 
96426dd5217SBrian Foster 	/* if there are no holes, return the first available offset */
96526dd5217SBrian Foster 	if (!xfs_inobt_issparse(rec->ir_holemask))
966d4cc540bSBrian Foster 		return xfs_lowbit64(rec->ir_free);
96726dd5217SBrian Foster 
96826dd5217SBrian Foster 	realfree = xfs_inobt_irec_to_allocmask(rec);
96926dd5217SBrian Foster 	realfree &= rec->ir_free;
97026dd5217SBrian Foster 
97126dd5217SBrian Foster 	return xfs_lowbit64(realfree);
972d4cc540bSBrian Foster }
973d4cc540bSBrian Foster 
974d4cc540bSBrian Foster /*
97530f712c9SDave Chinner  * Allocate an inode using the inobt-only algorithm.
97630f712c9SDave Chinner  */
97730f712c9SDave Chinner STATIC int
97830f712c9SDave Chinner xfs_dialloc_ag_inobt(
979dedab3e4SDave Chinner 	struct xfs_perag	*pag,
98030f712c9SDave Chinner 	struct xfs_trans	*tp,
98130f712c9SDave Chinner 	struct xfs_buf		*agbp,
98230f712c9SDave Chinner 	xfs_ino_t		parent,
98330f712c9SDave Chinner 	xfs_ino_t		*inop)
98430f712c9SDave Chinner {
98530f712c9SDave Chinner 	struct xfs_mount	*mp = tp->t_mountp;
986370c782bSChristoph Hellwig 	struct xfs_agi		*agi = agbp->b_addr;
98730f712c9SDave Chinner 	xfs_agnumber_t		pagno = XFS_INO_TO_AGNO(mp, parent);
98830f712c9SDave Chinner 	xfs_agino_t		pagino = XFS_INO_TO_AGINO(mp, parent);
98930f712c9SDave Chinner 	struct xfs_btree_cur	*cur, *tcur;
99030f712c9SDave Chinner 	struct xfs_inobt_rec_incore rec, trec;
99130f712c9SDave Chinner 	xfs_ino_t		ino;
99230f712c9SDave Chinner 	int			error;
99330f712c9SDave Chinner 	int			offset;
99430f712c9SDave Chinner 	int			i, j;
9952d32311cSCarlos Maiolino 	int			searchdistance = 10;
99630f712c9SDave Chinner 
9977ac2ff8bSDave Chinner 	ASSERT(xfs_perag_initialised_agi(pag));
9987ac2ff8bSDave Chinner 	ASSERT(xfs_perag_allows_inodes(pag));
99930f712c9SDave Chinner 	ASSERT(pag->pagi_freecount > 0);
100030f712c9SDave Chinner 
100130f712c9SDave Chinner  restart_pagno:
1002bab8b795SDave Chinner 	cur = xfs_inobt_init_cursor(pag, tp, agbp, XFS_BTNUM_INO);
100330f712c9SDave Chinner 	/*
100430f712c9SDave Chinner 	 * If pagino is 0 (this is the root inode allocation) use newino.
100530f712c9SDave Chinner 	 * This must work because we've just allocated some.
100630f712c9SDave Chinner 	 */
100730f712c9SDave Chinner 	if (!pagino)
100830f712c9SDave Chinner 		pagino = be32_to_cpu(agi->agi_newino);
100930f712c9SDave Chinner 
10109ba0889eSDave Chinner 	error = xfs_check_agi_freecount(cur);
101130f712c9SDave Chinner 	if (error)
101230f712c9SDave Chinner 		goto error0;
101330f712c9SDave Chinner 
101430f712c9SDave Chinner 	/*
101530f712c9SDave Chinner 	 * If in the same AG as the parent, try to get near the parent.
101630f712c9SDave Chinner 	 */
10177b13c515SDave Chinner 	if (pagno == pag->pag_agno) {
101830f712c9SDave Chinner 		int		doneleft;	/* done, to the left */
101930f712c9SDave Chinner 		int		doneright;	/* done, to the right */
102030f712c9SDave Chinner 
102130f712c9SDave Chinner 		error = xfs_inobt_lookup(cur, pagino, XFS_LOOKUP_LE, &i);
102230f712c9SDave Chinner 		if (error)
102330f712c9SDave Chinner 			goto error0;
1024f9e03706SDarrick J. Wong 		if (XFS_IS_CORRUPT(mp, i != 1)) {
1025f9e03706SDarrick J. Wong 			error = -EFSCORRUPTED;
1026f9e03706SDarrick J. Wong 			goto error0;
1027f9e03706SDarrick J. Wong 		}
102830f712c9SDave Chinner 
102930f712c9SDave Chinner 		error = xfs_inobt_get_rec(cur, &rec, &j);
103030f712c9SDave Chinner 		if (error)
103130f712c9SDave Chinner 			goto error0;
1032f9e03706SDarrick J. Wong 		if (XFS_IS_CORRUPT(mp, j != 1)) {
1033f9e03706SDarrick J. Wong 			error = -EFSCORRUPTED;
1034f9e03706SDarrick J. Wong 			goto error0;
1035f9e03706SDarrick J. Wong 		}
103630f712c9SDave Chinner 
103730f712c9SDave Chinner 		if (rec.ir_freecount > 0) {
103830f712c9SDave Chinner 			/*
103930f712c9SDave Chinner 			 * Found a free inode in the same chunk
104030f712c9SDave Chinner 			 * as the parent, done.
104130f712c9SDave Chinner 			 */
104230f712c9SDave Chinner 			goto alloc_inode;
104330f712c9SDave Chinner 		}
104430f712c9SDave Chinner 
104530f712c9SDave Chinner 
104630f712c9SDave Chinner 		/*
104730f712c9SDave Chinner 		 * In the same AG as parent, but parent's chunk is full.
104830f712c9SDave Chinner 		 */
104930f712c9SDave Chinner 
105030f712c9SDave Chinner 		/* duplicate the cursor, search left & right simultaneously */
105130f712c9SDave Chinner 		error = xfs_btree_dup_cursor(cur, &tcur);
105230f712c9SDave Chinner 		if (error)
105330f712c9SDave Chinner 			goto error0;
105430f712c9SDave Chinner 
105530f712c9SDave Chinner 		/*
105630f712c9SDave Chinner 		 * Skip to last blocks looked up if same parent inode.
105730f712c9SDave Chinner 		 */
105830f712c9SDave Chinner 		if (pagino != NULLAGINO &&
105930f712c9SDave Chinner 		    pag->pagl_pagino == pagino &&
106030f712c9SDave Chinner 		    pag->pagl_leftrec != NULLAGINO &&
106130f712c9SDave Chinner 		    pag->pagl_rightrec != NULLAGINO) {
106230f712c9SDave Chinner 			error = xfs_ialloc_get_rec(tcur, pag->pagl_leftrec,
106330f712c9SDave Chinner 						   &trec, &doneleft);
106430f712c9SDave Chinner 			if (error)
106530f712c9SDave Chinner 				goto error1;
106630f712c9SDave Chinner 
106730f712c9SDave Chinner 			error = xfs_ialloc_get_rec(cur, pag->pagl_rightrec,
106830f712c9SDave Chinner 						   &rec, &doneright);
106930f712c9SDave Chinner 			if (error)
107030f712c9SDave Chinner 				goto error1;
107130f712c9SDave Chinner 		} else {
107230f712c9SDave Chinner 			/* search left with tcur, back up 1 record */
107330f712c9SDave Chinner 			error = xfs_ialloc_next_rec(tcur, &trec, &doneleft, 1);
107430f712c9SDave Chinner 			if (error)
107530f712c9SDave Chinner 				goto error1;
107630f712c9SDave Chinner 
107730f712c9SDave Chinner 			/* search right with cur, go forward 1 record. */
107830f712c9SDave Chinner 			error = xfs_ialloc_next_rec(cur, &rec, &doneright, 0);
107930f712c9SDave Chinner 			if (error)
108030f712c9SDave Chinner 				goto error1;
108130f712c9SDave Chinner 		}
108230f712c9SDave Chinner 
108330f712c9SDave Chinner 		/*
108430f712c9SDave Chinner 		 * Loop until we find an inode chunk with a free inode.
108530f712c9SDave Chinner 		 */
10862d32311cSCarlos Maiolino 		while (--searchdistance > 0 && (!doneleft || !doneright)) {
108730f712c9SDave Chinner 			int	useleft;  /* using left inode chunk this time */
108830f712c9SDave Chinner 
108930f712c9SDave Chinner 			/* figure out the closer block if both are valid. */
109030f712c9SDave Chinner 			if (!doneleft && !doneright) {
109130f712c9SDave Chinner 				useleft = pagino -
109230f712c9SDave Chinner 				 (trec.ir_startino + XFS_INODES_PER_CHUNK - 1) <
109330f712c9SDave Chinner 				  rec.ir_startino - pagino;
109430f712c9SDave Chinner 			} else {
109530f712c9SDave Chinner 				useleft = !doneleft;
109630f712c9SDave Chinner 			}
109730f712c9SDave Chinner 
109830f712c9SDave Chinner 			/* free inodes to the left? */
109930f712c9SDave Chinner 			if (useleft && trec.ir_freecount) {
110030f712c9SDave Chinner 				xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
110130f712c9SDave Chinner 				cur = tcur;
110230f712c9SDave Chinner 
110330f712c9SDave Chinner 				pag->pagl_leftrec = trec.ir_startino;
110430f712c9SDave Chinner 				pag->pagl_rightrec = rec.ir_startino;
110530f712c9SDave Chinner 				pag->pagl_pagino = pagino;
1106c44245b3SOmar Sandoval 				rec = trec;
110730f712c9SDave Chinner 				goto alloc_inode;
110830f712c9SDave Chinner 			}
110930f712c9SDave Chinner 
111030f712c9SDave Chinner 			/* free inodes to the right? */
111130f712c9SDave Chinner 			if (!useleft && rec.ir_freecount) {
111230f712c9SDave Chinner 				xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
111330f712c9SDave Chinner 
111430f712c9SDave Chinner 				pag->pagl_leftrec = trec.ir_startino;
111530f712c9SDave Chinner 				pag->pagl_rightrec = rec.ir_startino;
111630f712c9SDave Chinner 				pag->pagl_pagino = pagino;
111730f712c9SDave Chinner 				goto alloc_inode;
111830f712c9SDave Chinner 			}
111930f712c9SDave Chinner 
112030f712c9SDave Chinner 			/* get next record to check */
112130f712c9SDave Chinner 			if (useleft) {
112230f712c9SDave Chinner 				error = xfs_ialloc_next_rec(tcur, &trec,
112330f712c9SDave Chinner 								 &doneleft, 1);
112430f712c9SDave Chinner 			} else {
112530f712c9SDave Chinner 				error = xfs_ialloc_next_rec(cur, &rec,
112630f712c9SDave Chinner 								 &doneright, 0);
112730f712c9SDave Chinner 			}
112830f712c9SDave Chinner 			if (error)
112930f712c9SDave Chinner 				goto error1;
113030f712c9SDave Chinner 		}
113130f712c9SDave Chinner 
11322d32311cSCarlos Maiolino 		if (searchdistance <= 0) {
11332d32311cSCarlos Maiolino 			/*
11342d32311cSCarlos Maiolino 			 * Not in range - save last search
11352d32311cSCarlos Maiolino 			 * location and allocate a new inode
11362d32311cSCarlos Maiolino 			 */
11372d32311cSCarlos Maiolino 			xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
11382d32311cSCarlos Maiolino 			pag->pagl_leftrec = trec.ir_startino;
11392d32311cSCarlos Maiolino 			pag->pagl_rightrec = rec.ir_startino;
11402d32311cSCarlos Maiolino 			pag->pagl_pagino = pagino;
11412d32311cSCarlos Maiolino 
11422d32311cSCarlos Maiolino 		} else {
114330f712c9SDave Chinner 			/*
114430f712c9SDave Chinner 			 * We've reached the end of the btree. because
114530f712c9SDave Chinner 			 * we are only searching a small chunk of the
114630f712c9SDave Chinner 			 * btree each search, there is obviously free
114730f712c9SDave Chinner 			 * inodes closer to the parent inode than we
114830f712c9SDave Chinner 			 * are now. restart the search again.
114930f712c9SDave Chinner 			 */
115030f712c9SDave Chinner 			pag->pagl_pagino = NULLAGINO;
115130f712c9SDave Chinner 			pag->pagl_leftrec = NULLAGINO;
115230f712c9SDave Chinner 			pag->pagl_rightrec = NULLAGINO;
115330f712c9SDave Chinner 			xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
115430f712c9SDave Chinner 			xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
115530f712c9SDave Chinner 			goto restart_pagno;
115630f712c9SDave Chinner 		}
11572d32311cSCarlos Maiolino 	}
115830f712c9SDave Chinner 
115930f712c9SDave Chinner 	/*
116030f712c9SDave Chinner 	 * In a different AG from the parent.
116130f712c9SDave Chinner 	 * See if the most recently allocated block has any free.
116230f712c9SDave Chinner 	 */
116330f712c9SDave Chinner 	if (agi->agi_newino != cpu_to_be32(NULLAGINO)) {
116430f712c9SDave Chinner 		error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino),
116530f712c9SDave Chinner 					 XFS_LOOKUP_EQ, &i);
116630f712c9SDave Chinner 		if (error)
116730f712c9SDave Chinner 			goto error0;
116830f712c9SDave Chinner 
116930f712c9SDave Chinner 		if (i == 1) {
117030f712c9SDave Chinner 			error = xfs_inobt_get_rec(cur, &rec, &j);
117130f712c9SDave Chinner 			if (error)
117230f712c9SDave Chinner 				goto error0;
117330f712c9SDave Chinner 
117430f712c9SDave Chinner 			if (j == 1 && rec.ir_freecount > 0) {
117530f712c9SDave Chinner 				/*
117630f712c9SDave Chinner 				 * The last chunk allocated in the group
117730f712c9SDave Chinner 				 * still has a free inode.
117830f712c9SDave Chinner 				 */
117930f712c9SDave Chinner 				goto alloc_inode;
118030f712c9SDave Chinner 			}
118130f712c9SDave Chinner 		}
118230f712c9SDave Chinner 	}
118330f712c9SDave Chinner 
118430f712c9SDave Chinner 	/*
118530f712c9SDave Chinner 	 * None left in the last group, search the whole AG
118630f712c9SDave Chinner 	 */
118730f712c9SDave Chinner 	error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
118830f712c9SDave Chinner 	if (error)
118930f712c9SDave Chinner 		goto error0;
1190f9e03706SDarrick J. Wong 	if (XFS_IS_CORRUPT(mp, i != 1)) {
1191f9e03706SDarrick J. Wong 		error = -EFSCORRUPTED;
1192f9e03706SDarrick J. Wong 		goto error0;
1193f9e03706SDarrick J. Wong 	}
119430f712c9SDave Chinner 
119530f712c9SDave Chinner 	for (;;) {
119630f712c9SDave Chinner 		error = xfs_inobt_get_rec(cur, &rec, &i);
119730f712c9SDave Chinner 		if (error)
119830f712c9SDave Chinner 			goto error0;
1199f9e03706SDarrick J. Wong 		if (XFS_IS_CORRUPT(mp, i != 1)) {
1200f9e03706SDarrick J. Wong 			error = -EFSCORRUPTED;
1201f9e03706SDarrick J. Wong 			goto error0;
1202f9e03706SDarrick J. Wong 		}
120330f712c9SDave Chinner 		if (rec.ir_freecount > 0)
120430f712c9SDave Chinner 			break;
120530f712c9SDave Chinner 		error = xfs_btree_increment(cur, 0, &i);
120630f712c9SDave Chinner 		if (error)
120730f712c9SDave Chinner 			goto error0;
1208f9e03706SDarrick J. Wong 		if (XFS_IS_CORRUPT(mp, i != 1)) {
1209f9e03706SDarrick J. Wong 			error = -EFSCORRUPTED;
1210f9e03706SDarrick J. Wong 			goto error0;
1211f9e03706SDarrick J. Wong 		}
121230f712c9SDave Chinner 	}
121330f712c9SDave Chinner 
121430f712c9SDave Chinner alloc_inode:
1215d4cc540bSBrian Foster 	offset = xfs_inobt_first_free_inode(&rec);
121630f712c9SDave Chinner 	ASSERT(offset >= 0);
121730f712c9SDave Chinner 	ASSERT(offset < XFS_INODES_PER_CHUNK);
121830f712c9SDave Chinner 	ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
121930f712c9SDave Chinner 				   XFS_INODES_PER_CHUNK) == 0);
12207b13c515SDave Chinner 	ino = XFS_AGINO_TO_INO(mp, pag->pag_agno, rec.ir_startino + offset);
122130f712c9SDave Chinner 	rec.ir_free &= ~XFS_INOBT_MASK(offset);
122230f712c9SDave Chinner 	rec.ir_freecount--;
122330f712c9SDave Chinner 	error = xfs_inobt_update(cur, &rec);
122430f712c9SDave Chinner 	if (error)
122530f712c9SDave Chinner 		goto error0;
122630f712c9SDave Chinner 	be32_add_cpu(&agi->agi_freecount, -1);
122730f712c9SDave Chinner 	xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
122830f712c9SDave Chinner 	pag->pagi_freecount--;
122930f712c9SDave Chinner 
12309ba0889eSDave Chinner 	error = xfs_check_agi_freecount(cur);
123130f712c9SDave Chinner 	if (error)
123230f712c9SDave Chinner 		goto error0;
123330f712c9SDave Chinner 
123430f712c9SDave Chinner 	xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
123530f712c9SDave Chinner 	xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
123630f712c9SDave Chinner 	*inop = ino;
123730f712c9SDave Chinner 	return 0;
123830f712c9SDave Chinner error1:
123930f712c9SDave Chinner 	xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
124030f712c9SDave Chinner error0:
124130f712c9SDave Chinner 	xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
124230f712c9SDave Chinner 	return error;
124330f712c9SDave Chinner }
124430f712c9SDave Chinner 
124530f712c9SDave Chinner /*
124630f712c9SDave Chinner  * Use the free inode btree to allocate an inode based on distance from the
124730f712c9SDave Chinner  * parent. Note that the provided cursor may be deleted and replaced.
124830f712c9SDave Chinner  */
124930f712c9SDave Chinner STATIC int
125030f712c9SDave Chinner xfs_dialloc_ag_finobt_near(
125130f712c9SDave Chinner 	xfs_agino_t			pagino,
125230f712c9SDave Chinner 	struct xfs_btree_cur		**ocur,
125330f712c9SDave Chinner 	struct xfs_inobt_rec_incore	*rec)
125430f712c9SDave Chinner {
125530f712c9SDave Chinner 	struct xfs_btree_cur		*lcur = *ocur;	/* left search cursor */
125630f712c9SDave Chinner 	struct xfs_btree_cur		*rcur;	/* right search cursor */
125730f712c9SDave Chinner 	struct xfs_inobt_rec_incore	rrec;
125830f712c9SDave Chinner 	int				error;
125930f712c9SDave Chinner 	int				i, j;
126030f712c9SDave Chinner 
126130f712c9SDave Chinner 	error = xfs_inobt_lookup(lcur, pagino, XFS_LOOKUP_LE, &i);
126230f712c9SDave Chinner 	if (error)
126330f712c9SDave Chinner 		return error;
126430f712c9SDave Chinner 
126530f712c9SDave Chinner 	if (i == 1) {
126630f712c9SDave Chinner 		error = xfs_inobt_get_rec(lcur, rec, &i);
126730f712c9SDave Chinner 		if (error)
126830f712c9SDave Chinner 			return error;
1269f9e03706SDarrick J. Wong 		if (XFS_IS_CORRUPT(lcur->bc_mp, i != 1))
1270f9e03706SDarrick J. Wong 			return -EFSCORRUPTED;
127130f712c9SDave Chinner 
127230f712c9SDave Chinner 		/*
127330f712c9SDave Chinner 		 * See if we've landed in the parent inode record. The finobt
127430f712c9SDave Chinner 		 * only tracks chunks with at least one free inode, so record
127530f712c9SDave Chinner 		 * existence is enough.
127630f712c9SDave Chinner 		 */
127730f712c9SDave Chinner 		if (pagino >= rec->ir_startino &&
127830f712c9SDave Chinner 		    pagino < (rec->ir_startino + XFS_INODES_PER_CHUNK))
127930f712c9SDave Chinner 			return 0;
128030f712c9SDave Chinner 	}
128130f712c9SDave Chinner 
128230f712c9SDave Chinner 	error = xfs_btree_dup_cursor(lcur, &rcur);
128330f712c9SDave Chinner 	if (error)
128430f712c9SDave Chinner 		return error;
128530f712c9SDave Chinner 
128630f712c9SDave Chinner 	error = xfs_inobt_lookup(rcur, pagino, XFS_LOOKUP_GE, &j);
128730f712c9SDave Chinner 	if (error)
128830f712c9SDave Chinner 		goto error_rcur;
128930f712c9SDave Chinner 	if (j == 1) {
129030f712c9SDave Chinner 		error = xfs_inobt_get_rec(rcur, &rrec, &j);
129130f712c9SDave Chinner 		if (error)
129230f712c9SDave Chinner 			goto error_rcur;
1293f9e03706SDarrick J. Wong 		if (XFS_IS_CORRUPT(lcur->bc_mp, j != 1)) {
1294f9e03706SDarrick J. Wong 			error = -EFSCORRUPTED;
1295f9e03706SDarrick J. Wong 			goto error_rcur;
1296f9e03706SDarrick J. Wong 		}
129730f712c9SDave Chinner 	}
129830f712c9SDave Chinner 
1299f9e03706SDarrick J. Wong 	if (XFS_IS_CORRUPT(lcur->bc_mp, i != 1 && j != 1)) {
1300f9e03706SDarrick J. Wong 		error = -EFSCORRUPTED;
1301f9e03706SDarrick J. Wong 		goto error_rcur;
1302f9e03706SDarrick J. Wong 	}
130330f712c9SDave Chinner 	if (i == 1 && j == 1) {
130430f712c9SDave Chinner 		/*
130530f712c9SDave Chinner 		 * Both the left and right records are valid. Choose the closer
130630f712c9SDave Chinner 		 * inode chunk to the target.
130730f712c9SDave Chinner 		 */
130830f712c9SDave Chinner 		if ((pagino - rec->ir_startino + XFS_INODES_PER_CHUNK - 1) >
130930f712c9SDave Chinner 		    (rrec.ir_startino - pagino)) {
131030f712c9SDave Chinner 			*rec = rrec;
131130f712c9SDave Chinner 			xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR);
131230f712c9SDave Chinner 			*ocur = rcur;
131330f712c9SDave Chinner 		} else {
131430f712c9SDave Chinner 			xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR);
131530f712c9SDave Chinner 		}
131630f712c9SDave Chinner 	} else if (j == 1) {
131730f712c9SDave Chinner 		/* only the right record is valid */
131830f712c9SDave Chinner 		*rec = rrec;
131930f712c9SDave Chinner 		xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR);
132030f712c9SDave Chinner 		*ocur = rcur;
132130f712c9SDave Chinner 	} else if (i == 1) {
132230f712c9SDave Chinner 		/* only the left record is valid */
132330f712c9SDave Chinner 		xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR);
132430f712c9SDave Chinner 	}
132530f712c9SDave Chinner 
132630f712c9SDave Chinner 	return 0;
132730f712c9SDave Chinner 
132830f712c9SDave Chinner error_rcur:
132930f712c9SDave Chinner 	xfs_btree_del_cursor(rcur, XFS_BTREE_ERROR);
133030f712c9SDave Chinner 	return error;
133130f712c9SDave Chinner }
133230f712c9SDave Chinner 
133330f712c9SDave Chinner /*
133430f712c9SDave Chinner  * Use the free inode btree to find a free inode based on a newino hint. If
133530f712c9SDave Chinner  * the hint is NULL, find the first free inode in the AG.
133630f712c9SDave Chinner  */
133730f712c9SDave Chinner STATIC int
133830f712c9SDave Chinner xfs_dialloc_ag_finobt_newino(
133930f712c9SDave Chinner 	struct xfs_agi			*agi,
134030f712c9SDave Chinner 	struct xfs_btree_cur		*cur,
134130f712c9SDave Chinner 	struct xfs_inobt_rec_incore	*rec)
134230f712c9SDave Chinner {
134330f712c9SDave Chinner 	int error;
134430f712c9SDave Chinner 	int i;
134530f712c9SDave Chinner 
134630f712c9SDave Chinner 	if (agi->agi_newino != cpu_to_be32(NULLAGINO)) {
1347e68ed775SDave Chinner 		error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino),
1348e68ed775SDave Chinner 					 XFS_LOOKUP_EQ, &i);
134930f712c9SDave Chinner 		if (error)
135030f712c9SDave Chinner 			return error;
135130f712c9SDave Chinner 		if (i == 1) {
135230f712c9SDave Chinner 			error = xfs_inobt_get_rec(cur, rec, &i);
135330f712c9SDave Chinner 			if (error)
135430f712c9SDave Chinner 				return error;
1355f9e03706SDarrick J. Wong 			if (XFS_IS_CORRUPT(cur->bc_mp, i != 1))
1356f9e03706SDarrick J. Wong 				return -EFSCORRUPTED;
135730f712c9SDave Chinner 			return 0;
135830f712c9SDave Chinner 		}
135930f712c9SDave Chinner 	}
136030f712c9SDave Chinner 
136130f712c9SDave Chinner 	/*
136230f712c9SDave Chinner 	 * Find the first inode available in the AG.
136330f712c9SDave Chinner 	 */
136430f712c9SDave Chinner 	error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
136530f712c9SDave Chinner 	if (error)
136630f712c9SDave Chinner 		return error;
1367f9e03706SDarrick J. Wong 	if (XFS_IS_CORRUPT(cur->bc_mp, i != 1))
1368f9e03706SDarrick J. Wong 		return -EFSCORRUPTED;
136930f712c9SDave Chinner 
137030f712c9SDave Chinner 	error = xfs_inobt_get_rec(cur, rec, &i);
137130f712c9SDave Chinner 	if (error)
137230f712c9SDave Chinner 		return error;
1373f9e03706SDarrick J. Wong 	if (XFS_IS_CORRUPT(cur->bc_mp, i != 1))
1374f9e03706SDarrick J. Wong 		return -EFSCORRUPTED;
137530f712c9SDave Chinner 
137630f712c9SDave Chinner 	return 0;
137730f712c9SDave Chinner }
137830f712c9SDave Chinner 
137930f712c9SDave Chinner /*
138030f712c9SDave Chinner  * Update the inobt based on a modification made to the finobt. Also ensure that
138130f712c9SDave Chinner  * the records from both trees are equivalent post-modification.
138230f712c9SDave Chinner  */
138330f712c9SDave Chinner STATIC int
138430f712c9SDave Chinner xfs_dialloc_ag_update_inobt(
138530f712c9SDave Chinner 	struct xfs_btree_cur		*cur,	/* inobt cursor */
138630f712c9SDave Chinner 	struct xfs_inobt_rec_incore	*frec,	/* finobt record */
138730f712c9SDave Chinner 	int				offset) /* inode offset */
138830f712c9SDave Chinner {
138930f712c9SDave Chinner 	struct xfs_inobt_rec_incore	rec;
139030f712c9SDave Chinner 	int				error;
139130f712c9SDave Chinner 	int				i;
139230f712c9SDave Chinner 
139330f712c9SDave Chinner 	error = xfs_inobt_lookup(cur, frec->ir_startino, XFS_LOOKUP_EQ, &i);
139430f712c9SDave Chinner 	if (error)
139530f712c9SDave Chinner 		return error;
1396f9e03706SDarrick J. Wong 	if (XFS_IS_CORRUPT(cur->bc_mp, i != 1))
1397f9e03706SDarrick J. Wong 		return -EFSCORRUPTED;
139830f712c9SDave Chinner 
139930f712c9SDave Chinner 	error = xfs_inobt_get_rec(cur, &rec, &i);
140030f712c9SDave Chinner 	if (error)
140130f712c9SDave Chinner 		return error;
1402f9e03706SDarrick J. Wong 	if (XFS_IS_CORRUPT(cur->bc_mp, i != 1))
1403f9e03706SDarrick J. Wong 		return -EFSCORRUPTED;
140430f712c9SDave Chinner 	ASSERT((XFS_AGINO_TO_OFFSET(cur->bc_mp, rec.ir_startino) %
140530f712c9SDave Chinner 				   XFS_INODES_PER_CHUNK) == 0);
140630f712c9SDave Chinner 
140730f712c9SDave Chinner 	rec.ir_free &= ~XFS_INOBT_MASK(offset);
140830f712c9SDave Chinner 	rec.ir_freecount--;
140930f712c9SDave Chinner 
1410f9e03706SDarrick J. Wong 	if (XFS_IS_CORRUPT(cur->bc_mp,
1411f9e03706SDarrick J. Wong 			   rec.ir_free != frec->ir_free ||
1412f9e03706SDarrick J. Wong 			   rec.ir_freecount != frec->ir_freecount))
1413f9e03706SDarrick J. Wong 		return -EFSCORRUPTED;
141430f712c9SDave Chinner 
1415b72091f2Skbuild test robot 	return xfs_inobt_update(cur, &rec);
141630f712c9SDave Chinner }
141730f712c9SDave Chinner 
141830f712c9SDave Chinner /*
141930f712c9SDave Chinner  * Allocate an inode using the free inode btree, if available. Otherwise, fall
142030f712c9SDave Chinner  * back to the inobt search algorithm.
142130f712c9SDave Chinner  *
142230f712c9SDave Chinner  * The caller selected an AG for us, and made sure that free inodes are
142330f712c9SDave Chinner  * available.
142430f712c9SDave Chinner  */
1425b652afd9SDave Chinner static int
142630f712c9SDave Chinner xfs_dialloc_ag(
1427dedab3e4SDave Chinner 	struct xfs_perag	*pag,
142830f712c9SDave Chinner 	struct xfs_trans	*tp,
142930f712c9SDave Chinner 	struct xfs_buf		*agbp,
143030f712c9SDave Chinner 	xfs_ino_t		parent,
143130f712c9SDave Chinner 	xfs_ino_t		*inop)
143230f712c9SDave Chinner {
143330f712c9SDave Chinner 	struct xfs_mount		*mp = tp->t_mountp;
1434370c782bSChristoph Hellwig 	struct xfs_agi			*agi = agbp->b_addr;
143530f712c9SDave Chinner 	xfs_agnumber_t			pagno = XFS_INO_TO_AGNO(mp, parent);
143630f712c9SDave Chinner 	xfs_agino_t			pagino = XFS_INO_TO_AGINO(mp, parent);
143730f712c9SDave Chinner 	struct xfs_btree_cur		*cur;	/* finobt cursor */
143830f712c9SDave Chinner 	struct xfs_btree_cur		*icur;	/* inobt cursor */
143930f712c9SDave Chinner 	struct xfs_inobt_rec_incore	rec;
144030f712c9SDave Chinner 	xfs_ino_t			ino;
144130f712c9SDave Chinner 	int				error;
144230f712c9SDave Chinner 	int				offset;
144330f712c9SDave Chinner 	int				i;
144430f712c9SDave Chinner 
1445ebd9027dSDave Chinner 	if (!xfs_has_finobt(mp))
1446dedab3e4SDave Chinner 		return xfs_dialloc_ag_inobt(pag, tp, agbp, parent, inop);
144730f712c9SDave Chinner 
144830f712c9SDave Chinner 	/*
144930f712c9SDave Chinner 	 * If pagino is 0 (this is the root inode allocation) use newino.
145030f712c9SDave Chinner 	 * This must work because we've just allocated some.
145130f712c9SDave Chinner 	 */
145230f712c9SDave Chinner 	if (!pagino)
145330f712c9SDave Chinner 		pagino = be32_to_cpu(agi->agi_newino);
145430f712c9SDave Chinner 
1455bab8b795SDave Chinner 	cur = xfs_inobt_init_cursor(pag, tp, agbp, XFS_BTNUM_FINO);
145630f712c9SDave Chinner 
14579ba0889eSDave Chinner 	error = xfs_check_agi_freecount(cur);
145830f712c9SDave Chinner 	if (error)
145930f712c9SDave Chinner 		goto error_cur;
146030f712c9SDave Chinner 
146130f712c9SDave Chinner 	/*
146230f712c9SDave Chinner 	 * The search algorithm depends on whether we're in the same AG as the
146330f712c9SDave Chinner 	 * parent. If so, find the closest available inode to the parent. If
146430f712c9SDave Chinner 	 * not, consider the agi hint or find the first free inode in the AG.
146530f712c9SDave Chinner 	 */
14667b13c515SDave Chinner 	if (pag->pag_agno == pagno)
146730f712c9SDave Chinner 		error = xfs_dialloc_ag_finobt_near(pagino, &cur, &rec);
146830f712c9SDave Chinner 	else
146930f712c9SDave Chinner 		error = xfs_dialloc_ag_finobt_newino(agi, cur, &rec);
147030f712c9SDave Chinner 	if (error)
147130f712c9SDave Chinner 		goto error_cur;
147230f712c9SDave Chinner 
1473d4cc540bSBrian Foster 	offset = xfs_inobt_first_free_inode(&rec);
147430f712c9SDave Chinner 	ASSERT(offset >= 0);
147530f712c9SDave Chinner 	ASSERT(offset < XFS_INODES_PER_CHUNK);
147630f712c9SDave Chinner 	ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
147730f712c9SDave Chinner 				   XFS_INODES_PER_CHUNK) == 0);
14787b13c515SDave Chinner 	ino = XFS_AGINO_TO_INO(mp, pag->pag_agno, rec.ir_startino + offset);
147930f712c9SDave Chinner 
148030f712c9SDave Chinner 	/*
148130f712c9SDave Chinner 	 * Modify or remove the finobt record.
148230f712c9SDave Chinner 	 */
148330f712c9SDave Chinner 	rec.ir_free &= ~XFS_INOBT_MASK(offset);
148430f712c9SDave Chinner 	rec.ir_freecount--;
148530f712c9SDave Chinner 	if (rec.ir_freecount)
148630f712c9SDave Chinner 		error = xfs_inobt_update(cur, &rec);
148730f712c9SDave Chinner 	else
148830f712c9SDave Chinner 		error = xfs_btree_delete(cur, &i);
148930f712c9SDave Chinner 	if (error)
149030f712c9SDave Chinner 		goto error_cur;
149130f712c9SDave Chinner 
149230f712c9SDave Chinner 	/*
149330f712c9SDave Chinner 	 * The finobt has now been updated appropriately. We haven't updated the
149430f712c9SDave Chinner 	 * agi and superblock yet, so we can create an inobt cursor and validate
149530f712c9SDave Chinner 	 * the original freecount. If all is well, make the equivalent update to
149630f712c9SDave Chinner 	 * the inobt using the finobt record and offset information.
149730f712c9SDave Chinner 	 */
1498bab8b795SDave Chinner 	icur = xfs_inobt_init_cursor(pag, tp, agbp, XFS_BTNUM_INO);
149930f712c9SDave Chinner 
15009ba0889eSDave Chinner 	error = xfs_check_agi_freecount(icur);
150130f712c9SDave Chinner 	if (error)
150230f712c9SDave Chinner 		goto error_icur;
150330f712c9SDave Chinner 
150430f712c9SDave Chinner 	error = xfs_dialloc_ag_update_inobt(icur, &rec, offset);
150530f712c9SDave Chinner 	if (error)
150630f712c9SDave Chinner 		goto error_icur;
150730f712c9SDave Chinner 
150830f712c9SDave Chinner 	/*
150930f712c9SDave Chinner 	 * Both trees have now been updated. We must update the perag and
151030f712c9SDave Chinner 	 * superblock before we can check the freecount for each btree.
151130f712c9SDave Chinner 	 */
151230f712c9SDave Chinner 	be32_add_cpu(&agi->agi_freecount, -1);
151330f712c9SDave Chinner 	xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
15147b13c515SDave Chinner 	pag->pagi_freecount--;
151530f712c9SDave Chinner 
151630f712c9SDave Chinner 	xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
151730f712c9SDave Chinner 
15189ba0889eSDave Chinner 	error = xfs_check_agi_freecount(icur);
151930f712c9SDave Chinner 	if (error)
152030f712c9SDave Chinner 		goto error_icur;
15219ba0889eSDave Chinner 	error = xfs_check_agi_freecount(cur);
152230f712c9SDave Chinner 	if (error)
152330f712c9SDave Chinner 		goto error_icur;
152430f712c9SDave Chinner 
152530f712c9SDave Chinner 	xfs_btree_del_cursor(icur, XFS_BTREE_NOERROR);
152630f712c9SDave Chinner 	xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
152730f712c9SDave Chinner 	*inop = ino;
152830f712c9SDave Chinner 	return 0;
152930f712c9SDave Chinner 
153030f712c9SDave Chinner error_icur:
153130f712c9SDave Chinner 	xfs_btree_del_cursor(icur, XFS_BTREE_ERROR);
153230f712c9SDave Chinner error_cur:
153330f712c9SDave Chinner 	xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
153430f712c9SDave Chinner 	return error;
153530f712c9SDave Chinner }
153630f712c9SDave Chinner 
1537f3bf6e0fSDave Chinner static int
1538aececc9fSDave Chinner xfs_dialloc_roll(
1539aececc9fSDave Chinner 	struct xfs_trans	**tpp,
1540aececc9fSDave Chinner 	struct xfs_buf		*agibp)
1541aececc9fSDave Chinner {
1542aececc9fSDave Chinner 	struct xfs_trans	*tp = *tpp;
1543aececc9fSDave Chinner 	struct xfs_dquot_acct	*dqinfo;
1544aececc9fSDave Chinner 	int			error;
1545aececc9fSDave Chinner 
1546aececc9fSDave Chinner 	/*
1547aececc9fSDave Chinner 	 * Hold to on to the agibp across the commit so no other allocation can
1548aececc9fSDave Chinner 	 * come in and take the free inodes we just allocated for our caller.
1549aececc9fSDave Chinner 	 */
1550aececc9fSDave Chinner 	xfs_trans_bhold(tp, agibp);
1551aececc9fSDave Chinner 
1552aececc9fSDave Chinner 	/*
1553aececc9fSDave Chinner 	 * We want the quota changes to be associated with the next transaction,
1554aececc9fSDave Chinner 	 * NOT this one. So, detach the dqinfo from this and attach it to the
1555aececc9fSDave Chinner 	 * next transaction.
1556aececc9fSDave Chinner 	 */
1557aececc9fSDave Chinner 	dqinfo = tp->t_dqinfo;
1558aececc9fSDave Chinner 	tp->t_dqinfo = NULL;
1559aececc9fSDave Chinner 
1560aececc9fSDave Chinner 	error = xfs_trans_roll(&tp);
1561aececc9fSDave Chinner 
1562aececc9fSDave Chinner 	/* Re-attach the quota info that we detached from prev trx. */
1563aececc9fSDave Chinner 	tp->t_dqinfo = dqinfo;
1564aececc9fSDave Chinner 
15658237fbf5SDave Chinner 	/*
15668237fbf5SDave Chinner 	 * Join the buffer even on commit error so that the buffer is released
15678237fbf5SDave Chinner 	 * when the caller cancels the transaction and doesn't have to handle
15688237fbf5SDave Chinner 	 * this error case specially.
15698237fbf5SDave Chinner 	 */
1570aececc9fSDave Chinner 	xfs_trans_bjoin(tp, agibp);
15718237fbf5SDave Chinner 	*tpp = tp;
15728237fbf5SDave Chinner 	return error;
1573aececc9fSDave Chinner }
1574aececc9fSDave Chinner 
15758237fbf5SDave Chinner static bool
15768237fbf5SDave Chinner xfs_dialloc_good_ag(
15778237fbf5SDave Chinner 	struct xfs_perag	*pag,
1578dedab3e4SDave Chinner 	struct xfs_trans	*tp,
15798237fbf5SDave Chinner 	umode_t			mode,
15808237fbf5SDave Chinner 	int			flags,
15818237fbf5SDave Chinner 	bool			ok_alloc)
15828237fbf5SDave Chinner {
15838237fbf5SDave Chinner 	struct xfs_mount	*mp = tp->t_mountp;
15848237fbf5SDave Chinner 	xfs_extlen_t		ineed;
15858237fbf5SDave Chinner 	xfs_extlen_t		longest = 0;
15868237fbf5SDave Chinner 	int			needspace;
15878237fbf5SDave Chinner 	int			error;
15888237fbf5SDave Chinner 
1589dedab3e4SDave Chinner 	if (!pag)
1590dedab3e4SDave Chinner 		return false;
15917ac2ff8bSDave Chinner 	if (!xfs_perag_allows_inodes(pag))
15928237fbf5SDave Chinner 		return false;
15938237fbf5SDave Chinner 
15947ac2ff8bSDave Chinner 	if (!xfs_perag_initialised_agi(pag)) {
159599b13c7fSDave Chinner 		error = xfs_ialloc_read_agi(pag, tp, NULL);
15968237fbf5SDave Chinner 		if (error)
15978237fbf5SDave Chinner 			return false;
15988237fbf5SDave Chinner 	}
15998237fbf5SDave Chinner 
16008237fbf5SDave Chinner 	if (pag->pagi_freecount)
16018237fbf5SDave Chinner 		return true;
16028237fbf5SDave Chinner 	if (!ok_alloc)
16038237fbf5SDave Chinner 		return false;
16048237fbf5SDave Chinner 
16057ac2ff8bSDave Chinner 	if (!xfs_perag_initialised_agf(pag)) {
160608d3e84fSDave Chinner 		error = xfs_alloc_read_agf(pag, tp, flags, NULL);
16078237fbf5SDave Chinner 		if (error)
16088237fbf5SDave Chinner 			return false;
16098237fbf5SDave Chinner 	}
16108237fbf5SDave Chinner 
161130f712c9SDave Chinner 	/*
16128237fbf5SDave Chinner 	 * Check that there is enough free space for the file plus a chunk of
16138237fbf5SDave Chinner 	 * inodes if we need to allocate some. If this is the first pass across
16148237fbf5SDave Chinner 	 * the AGs, take into account the potential space needed for alignment
16158237fbf5SDave Chinner 	 * of inode chunks when checking the longest contiguous free space in
16168237fbf5SDave Chinner 	 * the AG - this prevents us from getting ENOSPC because we have free
16178237fbf5SDave Chinner 	 * space larger than ialloc_blks but alignment constraints prevent us
16188237fbf5SDave Chinner 	 * from using it.
16198237fbf5SDave Chinner 	 *
16208237fbf5SDave Chinner 	 * If we can't find an AG with space for full alignment slack to be
16218237fbf5SDave Chinner 	 * taken into account, we must be near ENOSPC in all AGs.  Hence we
16228237fbf5SDave Chinner 	 * don't include alignment for the second pass and so if we fail
16238237fbf5SDave Chinner 	 * allocation due to alignment issues then it is most likely a real
16248237fbf5SDave Chinner 	 * ENOSPC condition.
16258237fbf5SDave Chinner 	 *
16268237fbf5SDave Chinner 	 * XXX(dgc): this calculation is now bogus thanks to the per-ag
16278237fbf5SDave Chinner 	 * reservations that xfs_alloc_fix_freelist() now does via
16288237fbf5SDave Chinner 	 * xfs_alloc_space_available(). When the AG fills up, pagf_freeblks will
16298237fbf5SDave Chinner 	 * be more than large enough for the check below to succeed, but
16308237fbf5SDave Chinner 	 * xfs_alloc_space_available() will fail because of the non-zero
16318237fbf5SDave Chinner 	 * metadata reservation and hence we won't actually be able to allocate
16328237fbf5SDave Chinner 	 * more inodes in this AG. We do soooo much unnecessary work near ENOSPC
16338237fbf5SDave Chinner 	 * because of this.
16348237fbf5SDave Chinner 	 */
16358237fbf5SDave Chinner 	ineed = M_IGEO(mp)->ialloc_min_blks;
16368237fbf5SDave Chinner 	if (flags && ineed > 1)
16378237fbf5SDave Chinner 		ineed += M_IGEO(mp)->cluster_align;
16388237fbf5SDave Chinner 	longest = pag->pagf_longest;
16398237fbf5SDave Chinner 	if (!longest)
16408237fbf5SDave Chinner 		longest = pag->pagf_flcount > 0;
16418237fbf5SDave Chinner 	needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode);
16428237fbf5SDave Chinner 
16438237fbf5SDave Chinner 	if (pag->pagf_freeblks < needspace + ineed || longest < ineed)
16448237fbf5SDave Chinner 		return false;
16458237fbf5SDave Chinner 	return true;
16468237fbf5SDave Chinner }
16478237fbf5SDave Chinner 
16488237fbf5SDave Chinner static int
16498237fbf5SDave Chinner xfs_dialloc_try_ag(
16508237fbf5SDave Chinner 	struct xfs_perag	*pag,
1651dedab3e4SDave Chinner 	struct xfs_trans	**tpp,
16528237fbf5SDave Chinner 	xfs_ino_t		parent,
16538237fbf5SDave Chinner 	xfs_ino_t		*new_ino,
16548237fbf5SDave Chinner 	bool			ok_alloc)
16558237fbf5SDave Chinner {
16568237fbf5SDave Chinner 	struct xfs_buf		*agbp;
16578237fbf5SDave Chinner 	xfs_ino_t		ino;
16588237fbf5SDave Chinner 	int			error;
16598237fbf5SDave Chinner 
16608237fbf5SDave Chinner 	/*
16618237fbf5SDave Chinner 	 * Then read in the AGI buffer and recheck with the AGI buffer
16628237fbf5SDave Chinner 	 * lock held.
16638237fbf5SDave Chinner 	 */
166499b13c7fSDave Chinner 	error = xfs_ialloc_read_agi(pag, *tpp, &agbp);
16658237fbf5SDave Chinner 	if (error)
16668237fbf5SDave Chinner 		return error;
16678237fbf5SDave Chinner 
16688237fbf5SDave Chinner 	if (!pag->pagi_freecount) {
16698237fbf5SDave Chinner 		if (!ok_alloc) {
16708237fbf5SDave Chinner 			error = -EAGAIN;
16718237fbf5SDave Chinner 			goto out_release;
16728237fbf5SDave Chinner 		}
16738237fbf5SDave Chinner 
1674dedab3e4SDave Chinner 		error = xfs_ialloc_ag_alloc(pag, *tpp, agbp);
16758237fbf5SDave Chinner 		if (error < 0)
16768237fbf5SDave Chinner 			goto out_release;
16778237fbf5SDave Chinner 
16788237fbf5SDave Chinner 		/*
16798237fbf5SDave Chinner 		 * We successfully allocated space for an inode cluster in this
16808237fbf5SDave Chinner 		 * AG.  Roll the transaction so that we can allocate one of the
16818237fbf5SDave Chinner 		 * new inodes.
16828237fbf5SDave Chinner 		 */
16838237fbf5SDave Chinner 		ASSERT(pag->pagi_freecount > 0);
16848237fbf5SDave Chinner 		error = xfs_dialloc_roll(tpp, agbp);
16858237fbf5SDave Chinner 		if (error)
16868237fbf5SDave Chinner 			goto out_release;
16878237fbf5SDave Chinner 	}
16888237fbf5SDave Chinner 
16898237fbf5SDave Chinner 	/* Allocate an inode in the found AG */
1690dedab3e4SDave Chinner 	error = xfs_dialloc_ag(pag, *tpp, agbp, parent, &ino);
16918237fbf5SDave Chinner 	if (!error)
16928237fbf5SDave Chinner 		*new_ino = ino;
16938237fbf5SDave Chinner 	return error;
16948237fbf5SDave Chinner 
16958237fbf5SDave Chinner out_release:
16968237fbf5SDave Chinner 	xfs_trans_brelse(*tpp, agbp);
16978237fbf5SDave Chinner 	return error;
16988237fbf5SDave Chinner }
16998237fbf5SDave Chinner 
17008237fbf5SDave Chinner /*
17018237fbf5SDave Chinner  * Allocate an on-disk inode.
170230f712c9SDave Chinner  *
17038d822dc3SDave Chinner  * Mode is used to tell whether the new inode is a directory and hence where to
17048237fbf5SDave Chinner  * locate it. The on-disk inode that is allocated will be returned in @new_ino
17058237fbf5SDave Chinner  * on success, otherwise an error will be set to indicate the failure (e.g.
17068237fbf5SDave Chinner  * -ENOSPC).
170730f712c9SDave Chinner  */
170830f712c9SDave Chinner int
1709b652afd9SDave Chinner xfs_dialloc(
1710f3bf6e0fSDave Chinner 	struct xfs_trans	**tpp,
171130f712c9SDave Chinner 	xfs_ino_t		parent,
171230f712c9SDave Chinner 	umode_t			mode,
1713b652afd9SDave Chinner 	xfs_ino_t		*new_ino)
171430f712c9SDave Chinner {
1715f3bf6e0fSDave Chinner 	struct xfs_mount	*mp = (*tpp)->t_mountp;
171630f712c9SDave Chinner 	xfs_agnumber_t		agno;
1717b652afd9SDave Chinner 	int			error = 0;
171830f712c9SDave Chinner 	xfs_agnumber_t		start_agno;
171930f712c9SDave Chinner 	struct xfs_perag	*pag;
1720ef325959SDarrick J. Wong 	struct xfs_ino_geometry	*igeo = M_IGEO(mp);
17218237fbf5SDave Chinner 	bool			ok_alloc = true;
1722f08f984cSDave Chinner 	bool			low_space = false;
172389b1f55aSDave Chinner 	int			flags;
172476257a15SDave Chinner 	xfs_ino_t		ino = NULLFSINO;
17258d822dc3SDave Chinner 
172630f712c9SDave Chinner 	/*
172789b1f55aSDave Chinner 	 * Directories, symlinks, and regular files frequently allocate at least
172889b1f55aSDave Chinner 	 * one block, so factor that potential expansion when we examine whether
172989b1f55aSDave Chinner 	 * an AG has enough space for file creation.
173030f712c9SDave Chinner 	 */
173189b1f55aSDave Chinner 	if (S_ISDIR(mode))
1732*6e2985c9SDarrick J. Wong 		start_agno = (atomic_inc_return(&mp->m_agirotor) - 1) %
1733*6e2985c9SDarrick J. Wong 				mp->m_maxagi;
173489b1f55aSDave Chinner 	else {
173589b1f55aSDave Chinner 		start_agno = XFS_INO_TO_AGNO(mp, parent);
173689b1f55aSDave Chinner 		if (start_agno >= mp->m_maxagi)
173789b1f55aSDave Chinner 			start_agno = 0;
173889b1f55aSDave Chinner 	}
173930f712c9SDave Chinner 
174030f712c9SDave Chinner 	/*
174130f712c9SDave Chinner 	 * If we have already hit the ceiling of inode blocks then clear
17428237fbf5SDave Chinner 	 * ok_alloc so we scan all available agi structures for a free
174330f712c9SDave Chinner 	 * inode.
174474f9ce1cSGeorge Wang 	 *
174574f9ce1cSGeorge Wang 	 * Read rough value of mp->m_icount by percpu_counter_read_positive,
174674f9ce1cSGeorge Wang 	 * which will sacrifice the preciseness but improve the performance.
174730f712c9SDave Chinner 	 */
1748ef325959SDarrick J. Wong 	if (igeo->maxicount &&
1749ef325959SDarrick J. Wong 	    percpu_counter_read_positive(&mp->m_icount) + igeo->ialloc_inos
1750ef325959SDarrick J. Wong 							> igeo->maxicount) {
17518237fbf5SDave Chinner 		ok_alloc = false;
175230f712c9SDave Chinner 	}
175330f712c9SDave Chinner 
175430f712c9SDave Chinner 	/*
1755f08f984cSDave Chinner 	 * If we are near to ENOSPC, we want to prefer allocation from AGs that
1756f08f984cSDave Chinner 	 * have free inodes in them rather than use up free space allocating new
1757f08f984cSDave Chinner 	 * inode chunks. Hence we turn off allocation for the first non-blocking
1758f08f984cSDave Chinner 	 * pass through the AGs if we are near ENOSPC to consume free inodes
1759f08f984cSDave Chinner 	 * that we can immediately allocate, but then we allow allocation on the
1760f08f984cSDave Chinner 	 * second pass if we fail to find an AG with free inodes in it.
1761f08f984cSDave Chinner 	 */
1762f08f984cSDave Chinner 	if (percpu_counter_read_positive(&mp->m_fdblocks) <
1763f08f984cSDave Chinner 			mp->m_low_space[XFS_LOWSP_1_PCNT]) {
1764f08f984cSDave Chinner 		ok_alloc = false;
1765f08f984cSDave Chinner 		low_space = true;
1766f08f984cSDave Chinner 	}
1767f08f984cSDave Chinner 
1768f08f984cSDave Chinner 	/*
176930f712c9SDave Chinner 	 * Loop until we find an allocation group that either has free inodes
177030f712c9SDave Chinner 	 * or in which we can allocate some inodes.  Iterate through the
177130f712c9SDave Chinner 	 * allocation groups upward, wrapping at the end.
177230f712c9SDave Chinner 	 */
177389b1f55aSDave Chinner 	flags = XFS_ALLOC_FLAG_TRYLOCK;
177476257a15SDave Chinner retry:
177576257a15SDave Chinner 	for_each_perag_wrap_at(mp, start_agno, mp->m_maxagi, agno, pag) {
1776dedab3e4SDave Chinner 		if (xfs_dialloc_good_ag(pag, *tpp, mode, flags, ok_alloc)) {
1777dedab3e4SDave Chinner 			error = xfs_dialloc_try_ag(pag, tpp, parent,
17788237fbf5SDave Chinner 					&ino, ok_alloc);
17798237fbf5SDave Chinner 			if (error != -EAGAIN)
17808d822dc3SDave Chinner 				break;
178176257a15SDave Chinner 			error = 0;
178230f712c9SDave Chinner 		}
178330f712c9SDave Chinner 
178475c8c50fSDave Chinner 		if (xfs_is_shutdown(mp)) {
178589b1f55aSDave Chinner 			error = -EFSCORRUPTED;
178642685473SDave Chinner 			break;
178789b1f55aSDave Chinner 		}
17888237fbf5SDave Chinner 	}
178976257a15SDave Chinner 	if (pag)
179076257a15SDave Chinner 		xfs_perag_rele(pag);
179176257a15SDave Chinner 	if (error)
179276257a15SDave Chinner 		return error;
179376257a15SDave Chinner 	if (ino == NULLFSINO) {
179476257a15SDave Chinner 		if (flags) {
179589b1f55aSDave Chinner 			flags = 0;
1796f08f984cSDave Chinner 			if (low_space)
1797f08f984cSDave Chinner 				ok_alloc = true;
179876257a15SDave Chinner 			goto retry;
179989b1f55aSDave Chinner 		}
180076257a15SDave Chinner 		return -ENOSPC;
180130f712c9SDave Chinner 	}
1802b652afd9SDave Chinner 	*new_ino = ino;
180376257a15SDave Chinner 	return 0;
180430f712c9SDave Chinner }
180530f712c9SDave Chinner 
180610ae3dc7SBrian Foster /*
180710ae3dc7SBrian Foster  * Free the blocks of an inode chunk. We must consider that the inode chunk
180810ae3dc7SBrian Foster  * might be sparse and only free the regions that are allocated as part of the
180910ae3dc7SBrian Foster  * chunk.
181010ae3dc7SBrian Foster  */
181110ae3dc7SBrian Foster STATIC void
181210ae3dc7SBrian Foster xfs_difree_inode_chunk(
18130f37d178SBrian Foster 	struct xfs_trans		*tp,
181410ae3dc7SBrian Foster 	xfs_agnumber_t			agno,
18150f37d178SBrian Foster 	struct xfs_inobt_rec_incore	*rec)
181610ae3dc7SBrian Foster {
18170f37d178SBrian Foster 	struct xfs_mount		*mp = tp->t_mountp;
18180f37d178SBrian Foster 	xfs_agblock_t			sagbno = XFS_AGINO_TO_AGBNO(mp,
18190f37d178SBrian Foster 							rec->ir_startino);
182010ae3dc7SBrian Foster 	int				startidx, endidx;
182110ae3dc7SBrian Foster 	int				nextbit;
182210ae3dc7SBrian Foster 	xfs_agblock_t			agbno;
182310ae3dc7SBrian Foster 	int				contigblk;
182410ae3dc7SBrian Foster 	DECLARE_BITMAP(holemask, XFS_INOBT_HOLEMASK_BITS);
182510ae3dc7SBrian Foster 
182610ae3dc7SBrian Foster 	if (!xfs_inobt_issparse(rec->ir_holemask)) {
182710ae3dc7SBrian Foster 		/* not sparse, calculate extent info directly */
1828c201d9caSDarrick J. Wong 		xfs_free_extent_later(tp, XFS_AGB_TO_FSB(mp, agno, sagbno),
1829ef325959SDarrick J. Wong 				  M_IGEO(mp)->ialloc_blks,
1830ef325959SDarrick J. Wong 				  &XFS_RMAP_OINFO_INODES);
183110ae3dc7SBrian Foster 		return;
183210ae3dc7SBrian Foster 	}
183310ae3dc7SBrian Foster 
183410ae3dc7SBrian Foster 	/* holemask is only 16-bits (fits in an unsigned long) */
183510ae3dc7SBrian Foster 	ASSERT(sizeof(rec->ir_holemask) <= sizeof(holemask[0]));
183610ae3dc7SBrian Foster 	holemask[0] = rec->ir_holemask;
183710ae3dc7SBrian Foster 
183810ae3dc7SBrian Foster 	/*
183910ae3dc7SBrian Foster 	 * Find contiguous ranges of zeroes (i.e., allocated regions) in the
184010ae3dc7SBrian Foster 	 * holemask and convert the start/end index of each range to an extent.
184110ae3dc7SBrian Foster 	 * We start with the start and end index both pointing at the first 0 in
184210ae3dc7SBrian Foster 	 * the mask.
184310ae3dc7SBrian Foster 	 */
184410ae3dc7SBrian Foster 	startidx = endidx = find_first_zero_bit(holemask,
184510ae3dc7SBrian Foster 						XFS_INOBT_HOLEMASK_BITS);
184610ae3dc7SBrian Foster 	nextbit = startidx + 1;
184710ae3dc7SBrian Foster 	while (startidx < XFS_INOBT_HOLEMASK_BITS) {
184810ae3dc7SBrian Foster 		nextbit = find_next_zero_bit(holemask, XFS_INOBT_HOLEMASK_BITS,
184910ae3dc7SBrian Foster 					     nextbit);
185010ae3dc7SBrian Foster 		/*
185110ae3dc7SBrian Foster 		 * If the next zero bit is contiguous, update the end index of
185210ae3dc7SBrian Foster 		 * the current range and continue.
185310ae3dc7SBrian Foster 		 */
185410ae3dc7SBrian Foster 		if (nextbit != XFS_INOBT_HOLEMASK_BITS &&
185510ae3dc7SBrian Foster 		    nextbit == endidx + 1) {
185610ae3dc7SBrian Foster 			endidx = nextbit;
185710ae3dc7SBrian Foster 			goto next;
185810ae3dc7SBrian Foster 		}
185910ae3dc7SBrian Foster 
186010ae3dc7SBrian Foster 		/*
186110ae3dc7SBrian Foster 		 * nextbit is not contiguous with the current end index. Convert
186210ae3dc7SBrian Foster 		 * the current start/end to an extent and add it to the free
186310ae3dc7SBrian Foster 		 * list.
186410ae3dc7SBrian Foster 		 */
186510ae3dc7SBrian Foster 		agbno = sagbno + (startidx * XFS_INODES_PER_HOLEMASK_BIT) /
186610ae3dc7SBrian Foster 				  mp->m_sb.sb_inopblock;
186710ae3dc7SBrian Foster 		contigblk = ((endidx - startidx + 1) *
186810ae3dc7SBrian Foster 			     XFS_INODES_PER_HOLEMASK_BIT) /
186910ae3dc7SBrian Foster 			    mp->m_sb.sb_inopblock;
187010ae3dc7SBrian Foster 
187110ae3dc7SBrian Foster 		ASSERT(agbno % mp->m_sb.sb_spino_align == 0);
187210ae3dc7SBrian Foster 		ASSERT(contigblk % mp->m_sb.sb_spino_align == 0);
1873c201d9caSDarrick J. Wong 		xfs_free_extent_later(tp, XFS_AGB_TO_FSB(mp, agno, agbno),
18747280fedaSDarrick J. Wong 				  contigblk, &XFS_RMAP_OINFO_INODES);
187510ae3dc7SBrian Foster 
187610ae3dc7SBrian Foster 		/* reset range to current bit and carry on... */
187710ae3dc7SBrian Foster 		startidx = endidx = nextbit;
187810ae3dc7SBrian Foster 
187910ae3dc7SBrian Foster next:
188010ae3dc7SBrian Foster 		nextbit++;
188110ae3dc7SBrian Foster 	}
188210ae3dc7SBrian Foster }
188310ae3dc7SBrian Foster 
188430f712c9SDave Chinner STATIC int
188530f712c9SDave Chinner xfs_difree_inobt(
1886dedab3e4SDave Chinner 	struct xfs_perag		*pag,
188730f712c9SDave Chinner 	struct xfs_trans		*tp,
188830f712c9SDave Chinner 	struct xfs_buf			*agbp,
188930f712c9SDave Chinner 	xfs_agino_t			agino,
189009b56604SBrian Foster 	struct xfs_icluster		*xic,
189130f712c9SDave Chinner 	struct xfs_inobt_rec_incore	*orec)
189230f712c9SDave Chinner {
1893dedab3e4SDave Chinner 	struct xfs_mount		*mp = pag->pag_mount;
1894370c782bSChristoph Hellwig 	struct xfs_agi			*agi = agbp->b_addr;
189530f712c9SDave Chinner 	struct xfs_btree_cur		*cur;
189630f712c9SDave Chinner 	struct xfs_inobt_rec_incore	rec;
189730f712c9SDave Chinner 	int				ilen;
189830f712c9SDave Chinner 	int				error;
189930f712c9SDave Chinner 	int				i;
190030f712c9SDave Chinner 	int				off;
190130f712c9SDave Chinner 
190230f712c9SDave Chinner 	ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC));
190330f712c9SDave Chinner 	ASSERT(XFS_AGINO_TO_AGBNO(mp, agino) < be32_to_cpu(agi->agi_length));
190430f712c9SDave Chinner 
190530f712c9SDave Chinner 	/*
190630f712c9SDave Chinner 	 * Initialize the cursor.
190730f712c9SDave Chinner 	 */
1908bab8b795SDave Chinner 	cur = xfs_inobt_init_cursor(pag, tp, agbp, XFS_BTNUM_INO);
190930f712c9SDave Chinner 
19109ba0889eSDave Chinner 	error = xfs_check_agi_freecount(cur);
191130f712c9SDave Chinner 	if (error)
191230f712c9SDave Chinner 		goto error0;
191330f712c9SDave Chinner 
191430f712c9SDave Chinner 	/*
191530f712c9SDave Chinner 	 * Look for the entry describing this inode.
191630f712c9SDave Chinner 	 */
191730f712c9SDave Chinner 	if ((error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i))) {
191830f712c9SDave Chinner 		xfs_warn(mp, "%s: xfs_inobt_lookup() returned error %d.",
191930f712c9SDave Chinner 			__func__, error);
192030f712c9SDave Chinner 		goto error0;
192130f712c9SDave Chinner 	}
1922f9e03706SDarrick J. Wong 	if (XFS_IS_CORRUPT(mp, i != 1)) {
1923f9e03706SDarrick J. Wong 		error = -EFSCORRUPTED;
1924f9e03706SDarrick J. Wong 		goto error0;
1925f9e03706SDarrick J. Wong 	}
192630f712c9SDave Chinner 	error = xfs_inobt_get_rec(cur, &rec, &i);
192730f712c9SDave Chinner 	if (error) {
192830f712c9SDave Chinner 		xfs_warn(mp, "%s: xfs_inobt_get_rec() returned error %d.",
192930f712c9SDave Chinner 			__func__, error);
193030f712c9SDave Chinner 		goto error0;
193130f712c9SDave Chinner 	}
1932f9e03706SDarrick J. Wong 	if (XFS_IS_CORRUPT(mp, i != 1)) {
1933f9e03706SDarrick J. Wong 		error = -EFSCORRUPTED;
1934f9e03706SDarrick J. Wong 		goto error0;
1935f9e03706SDarrick J. Wong 	}
193630f712c9SDave Chinner 	/*
193730f712c9SDave Chinner 	 * Get the offset in the inode chunk.
193830f712c9SDave Chinner 	 */
193930f712c9SDave Chinner 	off = agino - rec.ir_startino;
194030f712c9SDave Chinner 	ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK);
194130f712c9SDave Chinner 	ASSERT(!(rec.ir_free & XFS_INOBT_MASK(off)));
194230f712c9SDave Chinner 	/*
194330f712c9SDave Chinner 	 * Mark the inode free & increment the count.
194430f712c9SDave Chinner 	 */
194530f712c9SDave Chinner 	rec.ir_free |= XFS_INOBT_MASK(off);
194630f712c9SDave Chinner 	rec.ir_freecount++;
194730f712c9SDave Chinner 
194830f712c9SDave Chinner 	/*
1949999633d3SBrian Foster 	 * When an inode chunk is free, it becomes eligible for removal. Don't
1950999633d3SBrian Foster 	 * remove the chunk if the block size is large enough for multiple inode
1951999633d3SBrian Foster 	 * chunks (that might not be free).
195230f712c9SDave Chinner 	 */
19530560f31aSDave Chinner 	if (!xfs_has_ikeep(mp) && rec.ir_free == XFS_INOBT_ALL_FREE &&
1954999633d3SBrian Foster 	    mp->m_sb.sb_inopblock <= XFS_INODES_PER_CHUNK) {
195592a00544SGao Xiang 		struct xfs_perag	*pag = agbp->b_pag;
195692a00544SGao Xiang 
1957749f24f3SThomas Meyer 		xic->deleted = true;
19587b13c515SDave Chinner 		xic->first_ino = XFS_AGINO_TO_INO(mp, pag->pag_agno,
19597b13c515SDave Chinner 				rec.ir_startino);
196009b56604SBrian Foster 		xic->alloc = xfs_inobt_irec_to_allocmask(&rec);
196130f712c9SDave Chinner 
196230f712c9SDave Chinner 		/*
196330f712c9SDave Chinner 		 * Remove the inode cluster from the AGI B+Tree, adjust the
196430f712c9SDave Chinner 		 * AGI and Superblock inode counts, and mark the disk space
196530f712c9SDave Chinner 		 * to be freed when the transaction is committed.
196630f712c9SDave Chinner 		 */
1967999633d3SBrian Foster 		ilen = rec.ir_freecount;
196830f712c9SDave Chinner 		be32_add_cpu(&agi->agi_count, -ilen);
196930f712c9SDave Chinner 		be32_add_cpu(&agi->agi_freecount, -(ilen - 1));
197030f712c9SDave Chinner 		xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT);
197130f712c9SDave Chinner 		pag->pagi_freecount -= ilen - 1;
197289e9b5c0SDarrick J. Wong 		pag->pagi_count -= ilen;
197330f712c9SDave Chinner 		xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen);
197430f712c9SDave Chinner 		xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1));
197530f712c9SDave Chinner 
197630f712c9SDave Chinner 		if ((error = xfs_btree_delete(cur, &i))) {
197730f712c9SDave Chinner 			xfs_warn(mp, "%s: xfs_btree_delete returned error %d.",
197830f712c9SDave Chinner 				__func__, error);
197930f712c9SDave Chinner 			goto error0;
198030f712c9SDave Chinner 		}
198130f712c9SDave Chinner 
19827b13c515SDave Chinner 		xfs_difree_inode_chunk(tp, pag->pag_agno, &rec);
198330f712c9SDave Chinner 	} else {
1984749f24f3SThomas Meyer 		xic->deleted = false;
198530f712c9SDave Chinner 
198630f712c9SDave Chinner 		error = xfs_inobt_update(cur, &rec);
198730f712c9SDave Chinner 		if (error) {
198830f712c9SDave Chinner 			xfs_warn(mp, "%s: xfs_inobt_update returned error %d.",
198930f712c9SDave Chinner 				__func__, error);
199030f712c9SDave Chinner 			goto error0;
199130f712c9SDave Chinner 		}
199230f712c9SDave Chinner 
199330f712c9SDave Chinner 		/*
199430f712c9SDave Chinner 		 * Change the inode free counts and log the ag/sb changes.
199530f712c9SDave Chinner 		 */
199630f712c9SDave Chinner 		be32_add_cpu(&agi->agi_freecount, 1);
199730f712c9SDave Chinner 		xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
19987b13c515SDave Chinner 		pag->pagi_freecount++;
199930f712c9SDave Chinner 		xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1);
200030f712c9SDave Chinner 	}
200130f712c9SDave Chinner 
20029ba0889eSDave Chinner 	error = xfs_check_agi_freecount(cur);
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(
2020dedab3e4SDave Chinner 	struct xfs_perag		*pag,
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 {
2026dedab3e4SDave Chinner 	struct xfs_mount		*mp = pag->pag_mount;
202730f712c9SDave Chinner 	struct xfs_btree_cur		*cur;
202830f712c9SDave Chinner 	struct xfs_inobt_rec_incore	rec;
202930f712c9SDave Chinner 	int				offset = agino - ibtrec->ir_startino;
203030f712c9SDave Chinner 	int				error;
203130f712c9SDave Chinner 	int				i;
203230f712c9SDave Chinner 
2033bab8b795SDave Chinner 	cur = xfs_inobt_init_cursor(pag, tp, agbp, XFS_BTNUM_FINO);
203430f712c9SDave Chinner 
203530f712c9SDave Chinner 	error = xfs_inobt_lookup(cur, ibtrec->ir_startino, XFS_LOOKUP_EQ, &i);
203630f712c9SDave Chinner 	if (error)
203730f712c9SDave Chinner 		goto error;
203830f712c9SDave Chinner 	if (i == 0) {
203930f712c9SDave Chinner 		/*
204030f712c9SDave Chinner 		 * If the record does not exist in the finobt, we must have just
204130f712c9SDave Chinner 		 * freed an inode in a previously fully allocated chunk. If not,
204230f712c9SDave Chinner 		 * something is out of sync.
204330f712c9SDave Chinner 		 */
2044f9e03706SDarrick J. Wong 		if (XFS_IS_CORRUPT(mp, ibtrec->ir_freecount != 1)) {
2045f9e03706SDarrick J. Wong 			error = -EFSCORRUPTED;
2046f9e03706SDarrick J. Wong 			goto error;
2047f9e03706SDarrick J. Wong 		}
204830f712c9SDave Chinner 
20495419040fSBrian Foster 		error = xfs_inobt_insert_rec(cur, ibtrec->ir_holemask,
20505419040fSBrian Foster 					     ibtrec->ir_count,
20515419040fSBrian Foster 					     ibtrec->ir_freecount,
205230f712c9SDave Chinner 					     ibtrec->ir_free, &i);
205330f712c9SDave Chinner 		if (error)
205430f712c9SDave Chinner 			goto error;
205530f712c9SDave Chinner 		ASSERT(i == 1);
205630f712c9SDave Chinner 
205730f712c9SDave Chinner 		goto out;
205830f712c9SDave Chinner 	}
205930f712c9SDave Chinner 
206030f712c9SDave Chinner 	/*
206130f712c9SDave Chinner 	 * Read and update the existing record. We could just copy the ibtrec
206230f712c9SDave Chinner 	 * across here, but that would defeat the purpose of having redundant
206330f712c9SDave Chinner 	 * metadata. By making the modifications independently, we can catch
206430f712c9SDave Chinner 	 * corruptions that we wouldn't see if we just copied from one record
206530f712c9SDave Chinner 	 * to another.
206630f712c9SDave Chinner 	 */
206730f712c9SDave Chinner 	error = xfs_inobt_get_rec(cur, &rec, &i);
206830f712c9SDave Chinner 	if (error)
206930f712c9SDave Chinner 		goto error;
2070f9e03706SDarrick J. Wong 	if (XFS_IS_CORRUPT(mp, i != 1)) {
2071f9e03706SDarrick J. Wong 		error = -EFSCORRUPTED;
2072f9e03706SDarrick J. Wong 		goto error;
2073f9e03706SDarrick J. Wong 	}
207430f712c9SDave Chinner 
207530f712c9SDave Chinner 	rec.ir_free |= XFS_INOBT_MASK(offset);
207630f712c9SDave Chinner 	rec.ir_freecount++;
207730f712c9SDave Chinner 
2078f9e03706SDarrick J. Wong 	if (XFS_IS_CORRUPT(mp,
2079f9e03706SDarrick J. Wong 			   rec.ir_free != ibtrec->ir_free ||
2080f9e03706SDarrick J. Wong 			   rec.ir_freecount != ibtrec->ir_freecount)) {
2081f9e03706SDarrick J. Wong 		error = -EFSCORRUPTED;
2082f9e03706SDarrick J. Wong 		goto error;
2083f9e03706SDarrick J. Wong 	}
208430f712c9SDave Chinner 
208530f712c9SDave Chinner 	/*
208630f712c9SDave Chinner 	 * The content of inobt records should always match between the inobt
208730f712c9SDave Chinner 	 * and finobt. The lifecycle of records in the finobt is different from
208830f712c9SDave Chinner 	 * the inobt in that the finobt only tracks records with at least one
208930f712c9SDave Chinner 	 * free inode. Hence, if all of the inodes are free and we aren't
209030f712c9SDave Chinner 	 * keeping inode chunks permanently on disk, remove the record.
209130f712c9SDave Chinner 	 * Otherwise, update the record with the new information.
2092999633d3SBrian Foster 	 *
2093999633d3SBrian Foster 	 * Note that we currently can't free chunks when the block size is large
2094999633d3SBrian Foster 	 * enough for multiple chunks. Leave the finobt record to remain in sync
2095999633d3SBrian Foster 	 * with the inobt.
209630f712c9SDave Chinner 	 */
20970560f31aSDave Chinner 	if (!xfs_has_ikeep(mp) && rec.ir_free == XFS_INOBT_ALL_FREE &&
20980560f31aSDave Chinner 	    mp->m_sb.sb_inopblock <= XFS_INODES_PER_CHUNK) {
209930f712c9SDave Chinner 		error = xfs_btree_delete(cur, &i);
210030f712c9SDave Chinner 		if (error)
210130f712c9SDave Chinner 			goto error;
210230f712c9SDave Chinner 		ASSERT(i == 1);
210330f712c9SDave Chinner 	} else {
210430f712c9SDave Chinner 		error = xfs_inobt_update(cur, &rec);
210530f712c9SDave Chinner 		if (error)
210630f712c9SDave Chinner 			goto error;
210730f712c9SDave Chinner 	}
210830f712c9SDave Chinner 
210930f712c9SDave Chinner out:
21109ba0889eSDave Chinner 	error = xfs_check_agi_freecount(cur);
211130f712c9SDave Chinner 	if (error)
211230f712c9SDave Chinner 		goto error;
211330f712c9SDave Chinner 
211430f712c9SDave Chinner 	xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
211530f712c9SDave Chinner 	return 0;
211630f712c9SDave Chinner 
211730f712c9SDave Chinner error:
211830f712c9SDave Chinner 	xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
211930f712c9SDave Chinner 	return error;
212030f712c9SDave Chinner }
212130f712c9SDave Chinner 
212230f712c9SDave Chinner /*
212330f712c9SDave Chinner  * Free disk inode.  Carefully avoids touching the incore inode, all
212430f712c9SDave Chinner  * manipulations incore are the caller's responsibility.
212530f712c9SDave Chinner  * The on-disk inode is not changed by this operation, only the
212630f712c9SDave Chinner  * btree (free inode mask) is changed.
212730f712c9SDave Chinner  */
212830f712c9SDave Chinner int
212930f712c9SDave Chinner xfs_difree(
2130f40aadb2SDave Chinner 	struct xfs_trans	*tp,
2131f40aadb2SDave Chinner 	struct xfs_perag	*pag,
2132f40aadb2SDave Chinner 	xfs_ino_t		inode,
2133f40aadb2SDave Chinner 	struct xfs_icluster	*xic)
213430f712c9SDave Chinner {
213530f712c9SDave Chinner 	/* REFERENCED */
213630f712c9SDave Chinner 	xfs_agblock_t		agbno;	/* block number containing inode */
213730f712c9SDave Chinner 	struct xfs_buf		*agbp;	/* buffer for allocation group header */
213830f712c9SDave Chinner 	xfs_agino_t		agino;	/* allocation group inode number */
213930f712c9SDave Chinner 	int			error;	/* error return value */
21407b13c515SDave Chinner 	struct xfs_mount	*mp = tp->t_mountp;
214130f712c9SDave Chinner 	struct xfs_inobt_rec_incore rec;/* btree record */
214230f712c9SDave Chinner 
214330f712c9SDave Chinner 	/*
214430f712c9SDave Chinner 	 * Break up inode number into its components.
214530f712c9SDave Chinner 	 */
2146f40aadb2SDave Chinner 	if (pag->pag_agno != XFS_INO_TO_AGNO(mp, inode)) {
2147f40aadb2SDave Chinner 		xfs_warn(mp, "%s: agno != pag->pag_agno (%d != %d).",
2148f40aadb2SDave Chinner 			__func__, XFS_INO_TO_AGNO(mp, inode), pag->pag_agno);
214930f712c9SDave Chinner 		ASSERT(0);
21502451337dSDave Chinner 		return -EINVAL;
215130f712c9SDave Chinner 	}
215230f712c9SDave Chinner 	agino = XFS_INO_TO_AGINO(mp, inode);
2153f40aadb2SDave Chinner 	if (inode != XFS_AGINO_TO_INO(mp, pag->pag_agno, agino))  {
215430f712c9SDave Chinner 		xfs_warn(mp, "%s: inode != XFS_AGINO_TO_INO() (%llu != %llu).",
215530f712c9SDave Chinner 			__func__, (unsigned long long)inode,
2156f40aadb2SDave Chinner 			(unsigned long long)XFS_AGINO_TO_INO(mp, pag->pag_agno, agino));
215730f712c9SDave Chinner 		ASSERT(0);
21582451337dSDave Chinner 		return -EINVAL;
215930f712c9SDave Chinner 	}
216030f712c9SDave Chinner 	agbno = XFS_AGINO_TO_AGBNO(mp, agino);
216130f712c9SDave Chinner 	if (agbno >= mp->m_sb.sb_agblocks)  {
216230f712c9SDave Chinner 		xfs_warn(mp, "%s: agbno >= mp->m_sb.sb_agblocks (%d >= %d).",
216330f712c9SDave Chinner 			__func__, agbno, mp->m_sb.sb_agblocks);
216430f712c9SDave Chinner 		ASSERT(0);
21652451337dSDave Chinner 		return -EINVAL;
216630f712c9SDave Chinner 	}
216730f712c9SDave Chinner 	/*
216830f712c9SDave Chinner 	 * Get the allocation group header.
216930f712c9SDave Chinner 	 */
217099b13c7fSDave Chinner 	error = xfs_ialloc_read_agi(pag, tp, &agbp);
217130f712c9SDave Chinner 	if (error) {
217230f712c9SDave Chinner 		xfs_warn(mp, "%s: xfs_ialloc_read_agi() returned error %d.",
217330f712c9SDave Chinner 			__func__, error);
217430f712c9SDave Chinner 		return error;
217530f712c9SDave Chinner 	}
217630f712c9SDave Chinner 
217730f712c9SDave Chinner 	/*
217830f712c9SDave Chinner 	 * Fix up the inode allocation btree.
217930f712c9SDave Chinner 	 */
2180dedab3e4SDave Chinner 	error = xfs_difree_inobt(pag, tp, agbp, agino, xic, &rec);
218130f712c9SDave Chinner 	if (error)
218230f712c9SDave Chinner 		goto error0;
218330f712c9SDave Chinner 
218430f712c9SDave Chinner 	/*
218530f712c9SDave Chinner 	 * Fix up the free inode btree.
218630f712c9SDave Chinner 	 */
2187ebd9027dSDave Chinner 	if (xfs_has_finobt(mp)) {
2188dedab3e4SDave Chinner 		error = xfs_difree_finobt(pag, tp, agbp, agino, &rec);
218930f712c9SDave Chinner 		if (error)
219030f712c9SDave Chinner 			goto error0;
219130f712c9SDave Chinner 	}
219230f712c9SDave Chinner 
219330f712c9SDave Chinner 	return 0;
219430f712c9SDave Chinner 
219530f712c9SDave Chinner error0:
219630f712c9SDave Chinner 	return error;
219730f712c9SDave Chinner }
219830f712c9SDave Chinner 
219930f712c9SDave Chinner STATIC int
220030f712c9SDave Chinner xfs_imap_lookup(
22017b13c515SDave Chinner 	struct xfs_perag	*pag,
2202498f0adbSDave Chinner 	struct xfs_trans	*tp,
220330f712c9SDave Chinner 	xfs_agino_t		agino,
220430f712c9SDave Chinner 	xfs_agblock_t		agbno,
220530f712c9SDave Chinner 	xfs_agblock_t		*chunk_agbno,
220630f712c9SDave Chinner 	xfs_agblock_t		*offset_agbno,
220730f712c9SDave Chinner 	int			flags)
220830f712c9SDave Chinner {
2209498f0adbSDave Chinner 	struct xfs_mount	*mp = pag->pag_mount;
221030f712c9SDave Chinner 	struct xfs_inobt_rec_incore rec;
221130f712c9SDave Chinner 	struct xfs_btree_cur	*cur;
221230f712c9SDave Chinner 	struct xfs_buf		*agbp;
221330f712c9SDave Chinner 	int			error;
221430f712c9SDave Chinner 	int			i;
221530f712c9SDave Chinner 
221699b13c7fSDave Chinner 	error = xfs_ialloc_read_agi(pag, tp, &agbp);
221730f712c9SDave Chinner 	if (error) {
221830f712c9SDave Chinner 		xfs_alert(mp,
221930f712c9SDave Chinner 			"%s: xfs_ialloc_read_agi() returned error %d, agno %d",
22207b13c515SDave Chinner 			__func__, error, pag->pag_agno);
222130f712c9SDave Chinner 		return error;
222230f712c9SDave Chinner 	}
222330f712c9SDave Chinner 
222430f712c9SDave Chinner 	/*
222530f712c9SDave Chinner 	 * Lookup the inode record for the given agino. If the record cannot be
222630f712c9SDave Chinner 	 * found, then it's an invalid inode number and we should abort. Once
222730f712c9SDave Chinner 	 * we have a record, we need to ensure it contains the inode number
222830f712c9SDave Chinner 	 * we are looking up.
222930f712c9SDave Chinner 	 */
2230bab8b795SDave Chinner 	cur = xfs_inobt_init_cursor(pag, tp, agbp, XFS_BTNUM_INO);
223130f712c9SDave Chinner 	error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i);
223230f712c9SDave Chinner 	if (!error) {
223330f712c9SDave Chinner 		if (i)
223430f712c9SDave Chinner 			error = xfs_inobt_get_rec(cur, &rec, &i);
223530f712c9SDave Chinner 		if (!error && i == 0)
22362451337dSDave Chinner 			error = -EINVAL;
223730f712c9SDave Chinner 	}
223830f712c9SDave Chinner 
223930f712c9SDave Chinner 	xfs_trans_brelse(tp, agbp);
22400b04b6b8SDarrick J. Wong 	xfs_btree_del_cursor(cur, error);
224130f712c9SDave Chinner 	if (error)
224230f712c9SDave Chinner 		return error;
224330f712c9SDave Chinner 
224430f712c9SDave Chinner 	/* check that the returned record contains the required inode */
224530f712c9SDave Chinner 	if (rec.ir_startino > agino ||
2246ef325959SDarrick J. Wong 	    rec.ir_startino + M_IGEO(mp)->ialloc_inos <= agino)
22472451337dSDave Chinner 		return -EINVAL;
224830f712c9SDave Chinner 
224930f712c9SDave Chinner 	/* for untrusted inodes check it is allocated first */
225030f712c9SDave Chinner 	if ((flags & XFS_IGET_UNTRUSTED) &&
225130f712c9SDave Chinner 	    (rec.ir_free & XFS_INOBT_MASK(agino - rec.ir_startino)))
22522451337dSDave Chinner 		return -EINVAL;
225330f712c9SDave Chinner 
225430f712c9SDave Chinner 	*chunk_agbno = XFS_AGINO_TO_AGBNO(mp, rec.ir_startino);
225530f712c9SDave Chinner 	*offset_agbno = agbno - *chunk_agbno;
225630f712c9SDave Chinner 	return 0;
225730f712c9SDave Chinner }
225830f712c9SDave Chinner 
225930f712c9SDave Chinner /*
226030f712c9SDave Chinner  * Return the location of the inode in imap, for mapping it into a buffer.
226130f712c9SDave Chinner  */
226230f712c9SDave Chinner int
226330f712c9SDave Chinner xfs_imap(
2264498f0adbSDave Chinner 	struct xfs_perag	*pag,
2265498f0adbSDave Chinner 	struct xfs_trans	*tp,
226630f712c9SDave Chinner 	xfs_ino_t		ino,	/* inode to locate */
226730f712c9SDave Chinner 	struct xfs_imap		*imap,	/* location map structure */
226830f712c9SDave Chinner 	uint			flags)	/* flags for inode btree lookup */
226930f712c9SDave Chinner {
2270498f0adbSDave Chinner 	struct xfs_mount	*mp = pag->pag_mount;
227130f712c9SDave Chinner 	xfs_agblock_t		agbno;	/* block number of inode in the alloc group */
227230f712c9SDave Chinner 	xfs_agino_t		agino;	/* inode number within alloc group */
227330f712c9SDave Chinner 	xfs_agblock_t		chunk_agbno;	/* first block in inode chunk */
227430f712c9SDave Chinner 	xfs_agblock_t		cluster_agbno;	/* first block in inode cluster */
227530f712c9SDave Chinner 	int			error;	/* error code */
227630f712c9SDave Chinner 	int			offset;	/* index of inode in its buffer */
227730f712c9SDave Chinner 	xfs_agblock_t		offset_agbno;	/* blks from chunk start to inode */
227830f712c9SDave Chinner 
227930f712c9SDave Chinner 	ASSERT(ino != NULLFSINO);
228030f712c9SDave Chinner 
228130f712c9SDave Chinner 	/*
228230f712c9SDave Chinner 	 * Split up the inode number into its parts.
228330f712c9SDave Chinner 	 */
228430f712c9SDave Chinner 	agino = XFS_INO_TO_AGINO(mp, ino);
228530f712c9SDave Chinner 	agbno = XFS_AGINO_TO_AGBNO(mp, agino);
2286498f0adbSDave Chinner 	if (agbno >= mp->m_sb.sb_agblocks ||
22877b13c515SDave Chinner 	    ino != XFS_AGINO_TO_INO(mp, pag->pag_agno, agino)) {
22887b13c515SDave Chinner 		error = -EINVAL;
228930f712c9SDave Chinner #ifdef DEBUG
229030f712c9SDave Chinner 		/*
229130f712c9SDave Chinner 		 * Don't output diagnostic information for untrusted inodes
229230f712c9SDave Chinner 		 * as they can be invalid without implying corruption.
229330f712c9SDave Chinner 		 */
229430f712c9SDave Chinner 		if (flags & XFS_IGET_UNTRUSTED)
2295498f0adbSDave Chinner 			return error;
229630f712c9SDave Chinner 		if (agbno >= mp->m_sb.sb_agblocks) {
229730f712c9SDave Chinner 			xfs_alert(mp,
229830f712c9SDave Chinner 		"%s: agbno (0x%llx) >= mp->m_sb.sb_agblocks (0x%lx)",
229930f712c9SDave Chinner 				__func__, (unsigned long long)agbno,
230030f712c9SDave Chinner 				(unsigned long)mp->m_sb.sb_agblocks);
230130f712c9SDave Chinner 		}
2302498f0adbSDave Chinner 		if (ino != XFS_AGINO_TO_INO(mp, pag->pag_agno, agino)) {
230330f712c9SDave Chinner 			xfs_alert(mp,
230430f712c9SDave Chinner 		"%s: ino (0x%llx) != XFS_AGINO_TO_INO() (0x%llx)",
230530f712c9SDave Chinner 				__func__, ino,
23067b13c515SDave Chinner 				XFS_AGINO_TO_INO(mp, pag->pag_agno, agino));
230730f712c9SDave Chinner 		}
230830f712c9SDave Chinner 		xfs_stack_trace();
230930f712c9SDave Chinner #endif /* DEBUG */
2310498f0adbSDave Chinner 		return error;
231130f712c9SDave Chinner 	}
231230f712c9SDave Chinner 
231330f712c9SDave Chinner 	/*
231430f712c9SDave Chinner 	 * For bulkstat and handle lookups, we have an untrusted inode number
231530f712c9SDave Chinner 	 * that we have to verify is valid. We cannot do this just by reading
231630f712c9SDave Chinner 	 * the inode buffer as it may have been unlinked and removed leaving
231730f712c9SDave Chinner 	 * inodes in stale state on disk. Hence we have to do a btree lookup
231830f712c9SDave Chinner 	 * in all cases where an untrusted inode number is passed.
231930f712c9SDave Chinner 	 */
232030f712c9SDave Chinner 	if (flags & XFS_IGET_UNTRUSTED) {
2321498f0adbSDave Chinner 		error = xfs_imap_lookup(pag, tp, agino, agbno,
232230f712c9SDave Chinner 					&chunk_agbno, &offset_agbno, flags);
232330f712c9SDave Chinner 		if (error)
2324498f0adbSDave Chinner 			return error;
232530f712c9SDave Chinner 		goto out_map;
232630f712c9SDave Chinner 	}
232730f712c9SDave Chinner 
232830f712c9SDave Chinner 	/*
232930f712c9SDave Chinner 	 * If the inode cluster size is the same as the blocksize or
233030f712c9SDave Chinner 	 * smaller we get to the buffer by simple arithmetics.
233130f712c9SDave Chinner 	 */
2332ef325959SDarrick J. Wong 	if (M_IGEO(mp)->blocks_per_cluster == 1) {
233330f712c9SDave Chinner 		offset = XFS_INO_TO_OFFSET(mp, ino);
233430f712c9SDave Chinner 		ASSERT(offset < mp->m_sb.sb_inopblock);
233530f712c9SDave Chinner 
23367b13c515SDave Chinner 		imap->im_blkno = XFS_AGB_TO_DADDR(mp, pag->pag_agno, agbno);
233730f712c9SDave Chinner 		imap->im_len = XFS_FSB_TO_BB(mp, 1);
2338755c7bf5SDarrick J. Wong 		imap->im_boffset = (unsigned short)(offset <<
2339755c7bf5SDarrick J. Wong 							mp->m_sb.sb_inodelog);
2340498f0adbSDave Chinner 		return 0;
234130f712c9SDave Chinner 	}
234230f712c9SDave Chinner 
234330f712c9SDave Chinner 	/*
234430f712c9SDave Chinner 	 * If the inode chunks are aligned then use simple maths to
234530f712c9SDave Chinner 	 * find the location. Otherwise we have to do a btree
234630f712c9SDave Chinner 	 * lookup to find the location.
234730f712c9SDave Chinner 	 */
2348ef325959SDarrick J. Wong 	if (M_IGEO(mp)->inoalign_mask) {
2349ef325959SDarrick J. Wong 		offset_agbno = agbno & M_IGEO(mp)->inoalign_mask;
235030f712c9SDave Chinner 		chunk_agbno = agbno - offset_agbno;
235130f712c9SDave Chinner 	} else {
2352498f0adbSDave Chinner 		error = xfs_imap_lookup(pag, tp, agino, agbno,
235330f712c9SDave Chinner 					&chunk_agbno, &offset_agbno, flags);
235430f712c9SDave Chinner 		if (error)
2355498f0adbSDave Chinner 			return error;
235630f712c9SDave Chinner 	}
235730f712c9SDave Chinner 
235830f712c9SDave Chinner out_map:
235930f712c9SDave Chinner 	ASSERT(agbno >= chunk_agbno);
236030f712c9SDave Chinner 	cluster_agbno = chunk_agbno +
2361ef325959SDarrick J. Wong 		((offset_agbno / M_IGEO(mp)->blocks_per_cluster) *
2362ef325959SDarrick J. Wong 		 M_IGEO(mp)->blocks_per_cluster);
236330f712c9SDave Chinner 	offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
236430f712c9SDave Chinner 		XFS_INO_TO_OFFSET(mp, ino);
236530f712c9SDave Chinner 
23667b13c515SDave Chinner 	imap->im_blkno = XFS_AGB_TO_DADDR(mp, pag->pag_agno, cluster_agbno);
2367ef325959SDarrick J. Wong 	imap->im_len = XFS_FSB_TO_BB(mp, M_IGEO(mp)->blocks_per_cluster);
2368755c7bf5SDarrick J. Wong 	imap->im_boffset = (unsigned short)(offset << mp->m_sb.sb_inodelog);
236930f712c9SDave Chinner 
237030f712c9SDave Chinner 	/*
237130f712c9SDave Chinner 	 * If the inode number maps to a block outside the bounds
237230f712c9SDave Chinner 	 * of the file system then return NULL rather than calling
237330f712c9SDave Chinner 	 * read_buf and panicing when we get an error from the
237430f712c9SDave Chinner 	 * driver.
237530f712c9SDave Chinner 	 */
237630f712c9SDave Chinner 	if ((imap->im_blkno + imap->im_len) >
237730f712c9SDave Chinner 	    XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) {
237830f712c9SDave Chinner 		xfs_alert(mp,
237930f712c9SDave Chinner 	"%s: (im_blkno (0x%llx) + im_len (0x%llx)) > sb_dblocks (0x%llx)",
238030f712c9SDave Chinner 			__func__, (unsigned long long) imap->im_blkno,
238130f712c9SDave Chinner 			(unsigned long long) imap->im_len,
238230f712c9SDave Chinner 			XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks));
2383498f0adbSDave Chinner 		return -EINVAL;
238430f712c9SDave Chinner 	}
2385498f0adbSDave Chinner 	return 0;
238630f712c9SDave Chinner }
238730f712c9SDave Chinner 
238830f712c9SDave Chinner /*
238930f712c9SDave Chinner  * Log specified fields for the ag hdr (inode section). The growth of the agi
239030f712c9SDave Chinner  * structure over time requires that we interpret the buffer as two logical
239130f712c9SDave Chinner  * regions delineated by the end of the unlinked list. This is due to the size
239230f712c9SDave Chinner  * of the hash table and its location in the middle of the agi.
239330f712c9SDave Chinner  *
239430f712c9SDave Chinner  * For example, a request to log a field before agi_unlinked and a field after
239530f712c9SDave Chinner  * agi_unlinked could cause us to log the entire hash table and use an excessive
239630f712c9SDave Chinner  * amount of log space. To avoid this behavior, log the region up through
239730f712c9SDave Chinner  * agi_unlinked in one call and the region after agi_unlinked through the end of
239830f712c9SDave Chinner  * the structure in another.
239930f712c9SDave Chinner  */
240030f712c9SDave Chinner void
240130f712c9SDave Chinner xfs_ialloc_log_agi(
24020d1b9769SDave Chinner 	struct xfs_trans	*tp,
24030d1b9769SDave Chinner 	struct xfs_buf		*bp,
24040d1b9769SDave Chinner 	uint32_t		fields)
240530f712c9SDave Chinner {
240630f712c9SDave Chinner 	int			first;		/* first byte number */
240730f712c9SDave Chinner 	int			last;		/* last byte number */
240830f712c9SDave Chinner 	static const short	offsets[] = {	/* field starting offsets */
240930f712c9SDave Chinner 					/* keep in sync with bit definitions */
241030f712c9SDave Chinner 		offsetof(xfs_agi_t, agi_magicnum),
241130f712c9SDave Chinner 		offsetof(xfs_agi_t, agi_versionnum),
241230f712c9SDave Chinner 		offsetof(xfs_agi_t, agi_seqno),
241330f712c9SDave Chinner 		offsetof(xfs_agi_t, agi_length),
241430f712c9SDave Chinner 		offsetof(xfs_agi_t, agi_count),
241530f712c9SDave Chinner 		offsetof(xfs_agi_t, agi_root),
241630f712c9SDave Chinner 		offsetof(xfs_agi_t, agi_level),
241730f712c9SDave Chinner 		offsetof(xfs_agi_t, agi_freecount),
241830f712c9SDave Chinner 		offsetof(xfs_agi_t, agi_newino),
241930f712c9SDave Chinner 		offsetof(xfs_agi_t, agi_dirino),
242030f712c9SDave Chinner 		offsetof(xfs_agi_t, agi_unlinked),
242130f712c9SDave Chinner 		offsetof(xfs_agi_t, agi_free_root),
242230f712c9SDave Chinner 		offsetof(xfs_agi_t, agi_free_level),
24232a39946cSDarrick J. Wong 		offsetof(xfs_agi_t, agi_iblocks),
242430f712c9SDave Chinner 		sizeof(xfs_agi_t)
242530f712c9SDave Chinner 	};
242630f712c9SDave Chinner #ifdef DEBUG
2427370c782bSChristoph Hellwig 	struct xfs_agi		*agi = bp->b_addr;
242830f712c9SDave Chinner 
242930f712c9SDave Chinner 	ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC));
243030f712c9SDave Chinner #endif
243130f712c9SDave Chinner 
243230f712c9SDave Chinner 	/*
243330f712c9SDave Chinner 	 * Compute byte offsets for the first and last fields in the first
243430f712c9SDave Chinner 	 * region and log the agi buffer. This only logs up through
243530f712c9SDave Chinner 	 * agi_unlinked.
243630f712c9SDave Chinner 	 */
243730f712c9SDave Chinner 	if (fields & XFS_AGI_ALL_BITS_R1) {
243830f712c9SDave Chinner 		xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R1,
243930f712c9SDave Chinner 				  &first, &last);
244030f712c9SDave Chinner 		xfs_trans_log_buf(tp, bp, first, last);
244130f712c9SDave Chinner 	}
244230f712c9SDave Chinner 
244330f712c9SDave Chinner 	/*
244430f712c9SDave Chinner 	 * Mask off the bits in the first region and calculate the first and
244530f712c9SDave Chinner 	 * last field offsets for any bits in the second region.
244630f712c9SDave Chinner 	 */
244730f712c9SDave Chinner 	fields &= ~XFS_AGI_ALL_BITS_R1;
244830f712c9SDave Chinner 	if (fields) {
244930f712c9SDave Chinner 		xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R2,
245030f712c9SDave Chinner 				  &first, &last);
245130f712c9SDave Chinner 		xfs_trans_log_buf(tp, bp, first, last);
245230f712c9SDave Chinner 	}
245330f712c9SDave Chinner }
245430f712c9SDave Chinner 
2455a6a781a5SDarrick J. Wong static xfs_failaddr_t
245630f712c9SDave Chinner xfs_agi_verify(
245730f712c9SDave Chinner 	struct xfs_buf	*bp)
245830f712c9SDave Chinner {
2459dbd329f1SChristoph Hellwig 	struct xfs_mount *mp = bp->b_mount;
2460370c782bSChristoph Hellwig 	struct xfs_agi	*agi = bp->b_addr;
24619f96cc95SDave Chinner 	int		i;
246230f712c9SDave Chinner 
246338c26bfdSDave Chinner 	if (xfs_has_crc(mp)) {
2464a45086e2SBrian Foster 		if (!uuid_equal(&agi->agi_uuid, &mp->m_sb.sb_meta_uuid))
2465a6a781a5SDarrick J. Wong 			return __this_address;
2466370c782bSChristoph Hellwig 		if (!xfs_log_check_lsn(mp, be64_to_cpu(agi->agi_lsn)))
2467a6a781a5SDarrick J. Wong 			return __this_address;
2468a45086e2SBrian Foster 	}
2469a45086e2SBrian Foster 
247030f712c9SDave Chinner 	/*
247130f712c9SDave Chinner 	 * Validate the magic number of the agi block.
247230f712c9SDave Chinner 	 */
247339708c20SBrian Foster 	if (!xfs_verify_magic(bp, agi->agi_magicnum))
2474a6a781a5SDarrick J. Wong 		return __this_address;
247530f712c9SDave Chinner 	if (!XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum)))
2476a6a781a5SDarrick J. Wong 		return __this_address;
247730f712c9SDave Chinner 
2478d2a047f3SDarrick J. Wong 	if (be32_to_cpu(agi->agi_level) < 1 ||
2479973975b7SDarrick J. Wong 	    be32_to_cpu(agi->agi_level) > M_IGEO(mp)->inobt_maxlevels)
2480a6a781a5SDarrick J. Wong 		return __this_address;
2481d2a047f3SDarrick J. Wong 
248238c26bfdSDave Chinner 	if (xfs_has_finobt(mp) &&
2483d2a047f3SDarrick J. Wong 	    (be32_to_cpu(agi->agi_free_level) < 1 ||
2484973975b7SDarrick J. Wong 	     be32_to_cpu(agi->agi_free_level) > M_IGEO(mp)->inobt_maxlevels))
2485a6a781a5SDarrick J. Wong 		return __this_address;
2486d2a047f3SDarrick J. Wong 
248730f712c9SDave Chinner 	/*
248830f712c9SDave Chinner 	 * during growfs operations, the perag is not fully initialised,
248930f712c9SDave Chinner 	 * so we can't use it for any useful checking. growfs ensures we can't
249030f712c9SDave Chinner 	 * use it by using uncached buffers that don't have the perag attached
249130f712c9SDave Chinner 	 * so we can detect and avoid this problem.
249230f712c9SDave Chinner 	 */
249330f712c9SDave Chinner 	if (bp->b_pag && be32_to_cpu(agi->agi_seqno) != bp->b_pag->pag_agno)
2494a6a781a5SDarrick J. Wong 		return __this_address;
249530f712c9SDave Chinner 
24969f96cc95SDave Chinner 	for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++) {
24975089eaffSCarlos Maiolino 		if (agi->agi_unlinked[i] == cpu_to_be32(NULLAGINO))
24989f96cc95SDave Chinner 			continue;
24999f96cc95SDave Chinner 		if (!xfs_verify_ino(mp, be32_to_cpu(agi->agi_unlinked[i])))
25009f96cc95SDave Chinner 			return __this_address;
25019f96cc95SDave Chinner 	}
25029f96cc95SDave Chinner 
2503a6a781a5SDarrick J. Wong 	return NULL;
250430f712c9SDave Chinner }
250530f712c9SDave Chinner 
250630f712c9SDave Chinner static void
250730f712c9SDave Chinner xfs_agi_read_verify(
250830f712c9SDave Chinner 	struct xfs_buf	*bp)
250930f712c9SDave Chinner {
2510dbd329f1SChristoph Hellwig 	struct xfs_mount *mp = bp->b_mount;
2511bc1a09b8SDarrick J. Wong 	xfs_failaddr_t	fa;
251230f712c9SDave Chinner 
251338c26bfdSDave Chinner 	if (xfs_has_crc(mp) &&
251430f712c9SDave Chinner 	    !xfs_buf_verify_cksum(bp, XFS_AGI_CRC_OFF))
2515bc1a09b8SDarrick J. Wong 		xfs_verifier_error(bp, -EFSBADCRC, __this_address);
2516bc1a09b8SDarrick J. Wong 	else {
2517bc1a09b8SDarrick J. Wong 		fa = xfs_agi_verify(bp);
2518bc1a09b8SDarrick J. Wong 		if (XFS_TEST_ERROR(fa, mp, XFS_ERRTAG_IALLOC_READ_AGI))
2519bc1a09b8SDarrick J. Wong 			xfs_verifier_error(bp, -EFSCORRUPTED, fa);
2520bc1a09b8SDarrick J. Wong 	}
252130f712c9SDave Chinner }
252230f712c9SDave Chinner 
252330f712c9SDave Chinner static void
252430f712c9SDave Chinner xfs_agi_write_verify(
252530f712c9SDave Chinner 	struct xfs_buf	*bp)
252630f712c9SDave Chinner {
2527dbd329f1SChristoph Hellwig 	struct xfs_mount	*mp = bp->b_mount;
2528fb1755a6SCarlos Maiolino 	struct xfs_buf_log_item	*bip = bp->b_log_item;
2529370c782bSChristoph Hellwig 	struct xfs_agi		*agi = bp->b_addr;
2530bc1a09b8SDarrick J. Wong 	xfs_failaddr_t		fa;
253130f712c9SDave Chinner 
2532bc1a09b8SDarrick J. Wong 	fa = xfs_agi_verify(bp);
2533bc1a09b8SDarrick J. Wong 	if (fa) {
2534bc1a09b8SDarrick J. Wong 		xfs_verifier_error(bp, -EFSCORRUPTED, fa);
253530f712c9SDave Chinner 		return;
253630f712c9SDave Chinner 	}
253730f712c9SDave Chinner 
253838c26bfdSDave Chinner 	if (!xfs_has_crc(mp))
253930f712c9SDave Chinner 		return;
254030f712c9SDave Chinner 
254130f712c9SDave Chinner 	if (bip)
2542370c782bSChristoph Hellwig 		agi->agi_lsn = cpu_to_be64(bip->bli_item.li_lsn);
254330f712c9SDave Chinner 	xfs_buf_update_cksum(bp, XFS_AGI_CRC_OFF);
254430f712c9SDave Chinner }
254530f712c9SDave Chinner 
254630f712c9SDave Chinner const struct xfs_buf_ops xfs_agi_buf_ops = {
2547233135b7SEric Sandeen 	.name = "xfs_agi",
254839708c20SBrian Foster 	.magic = { cpu_to_be32(XFS_AGI_MAGIC), cpu_to_be32(XFS_AGI_MAGIC) },
254930f712c9SDave Chinner 	.verify_read = xfs_agi_read_verify,
255030f712c9SDave Chinner 	.verify_write = xfs_agi_write_verify,
2551b5572597SDarrick J. Wong 	.verify_struct = xfs_agi_verify,
255230f712c9SDave Chinner };
255330f712c9SDave Chinner 
255430f712c9SDave Chinner /*
255530f712c9SDave Chinner  * Read in the allocation group header (inode allocation section)
255630f712c9SDave Chinner  */
255730f712c9SDave Chinner int
255830f712c9SDave Chinner xfs_read_agi(
255961021debSDave Chinner 	struct xfs_perag	*pag,
256061021debSDave Chinner 	struct xfs_trans	*tp,
256161021debSDave Chinner 	struct xfs_buf		**agibpp)
256230f712c9SDave Chinner {
256361021debSDave Chinner 	struct xfs_mount	*mp = pag->pag_mount;
256430f712c9SDave Chinner 	int			error;
256530f712c9SDave Chinner 
256661021debSDave Chinner 	trace_xfs_read_agi(pag->pag_mount, pag->pag_agno);
256730f712c9SDave Chinner 
256830f712c9SDave Chinner 	error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
256961021debSDave Chinner 			XFS_AG_DADDR(mp, pag->pag_agno, XFS_AGI_DADDR(mp)),
257061021debSDave Chinner 			XFS_FSS_TO_BB(mp, 1), 0, agibpp, &xfs_agi_buf_ops);
257130f712c9SDave Chinner 	if (error)
257230f712c9SDave Chinner 		return error;
2573200237d6SEric Sandeen 	if (tp)
257461021debSDave Chinner 		xfs_trans_buf_set_type(tp, *agibpp, XFS_BLFT_AGI_BUF);
257530f712c9SDave Chinner 
257661021debSDave Chinner 	xfs_buf_set_ref(*agibpp, XFS_AGI_REF);
257730f712c9SDave Chinner 	return 0;
257830f712c9SDave Chinner }
257930f712c9SDave Chinner 
2580a95fee40SDave Chinner /*
2581a95fee40SDave Chinner  * Read in the agi and initialise the per-ag data. If the caller supplies a
2582a95fee40SDave Chinner  * @agibpp, return the locked AGI buffer to them, otherwise release it.
2583a95fee40SDave Chinner  */
258430f712c9SDave Chinner int
258530f712c9SDave Chinner xfs_ialloc_read_agi(
258699b13c7fSDave Chinner 	struct xfs_perag	*pag,
258799b13c7fSDave Chinner 	struct xfs_trans	*tp,
2588a95fee40SDave Chinner 	struct xfs_buf		**agibpp)
258930f712c9SDave Chinner {
2590a95fee40SDave Chinner 	struct xfs_buf		*agibp;
259199b13c7fSDave Chinner 	struct xfs_agi		*agi;
259230f712c9SDave Chinner 	int			error;
259330f712c9SDave Chinner 
259499b13c7fSDave Chinner 	trace_xfs_ialloc_read_agi(pag->pag_mount, pag->pag_agno);
259530f712c9SDave Chinner 
259661021debSDave Chinner 	error = xfs_read_agi(pag, tp, &agibp);
259730f712c9SDave Chinner 	if (error)
259830f712c9SDave Chinner 		return error;
259930f712c9SDave Chinner 
2600a95fee40SDave Chinner 	agi = agibp->b_addr;
26017ac2ff8bSDave Chinner 	if (!xfs_perag_initialised_agi(pag)) {
260230f712c9SDave Chinner 		pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
260330f712c9SDave Chinner 		pag->pagi_count = be32_to_cpu(agi->agi_count);
26047ac2ff8bSDave Chinner 		set_bit(XFS_AGSTATE_AGI_INIT, &pag->pag_opstate);
260530f712c9SDave Chinner 	}
260630f712c9SDave Chinner 
260730f712c9SDave Chinner 	/*
260830f712c9SDave Chinner 	 * It's possible for these to be out of sync if
260930f712c9SDave Chinner 	 * we are in the middle of a forced shutdown.
261030f712c9SDave Chinner 	 */
261130f712c9SDave Chinner 	ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) ||
261299b13c7fSDave Chinner 		xfs_is_shutdown(pag->pag_mount));
2613a95fee40SDave Chinner 	if (agibpp)
2614a95fee40SDave Chinner 		*agibpp = agibp;
2615a95fee40SDave Chinner 	else
2616a95fee40SDave Chinner 		xfs_trans_brelse(tp, agibp);
261730f712c9SDave Chinner 	return 0;
261830f712c9SDave Chinner }
261991fb9afcSDarrick J. Wong 
26202e001266SDarrick J. Wong /* Is there an inode record covering a given range of inode numbers? */
26212e001266SDarrick J. Wong int
26222e001266SDarrick J. Wong xfs_ialloc_has_inode_record(
26232e001266SDarrick J. Wong 	struct xfs_btree_cur	*cur,
26242e001266SDarrick J. Wong 	xfs_agino_t		low,
26252e001266SDarrick J. Wong 	xfs_agino_t		high,
26262e001266SDarrick J. Wong 	bool			*exists)
26272e001266SDarrick J. Wong {
26282e001266SDarrick J. Wong 	struct xfs_inobt_rec_incore	irec;
26292e001266SDarrick J. Wong 	xfs_agino_t		agino;
26302e001266SDarrick J. Wong 	uint16_t		holemask;
26312e001266SDarrick J. Wong 	int			has_record;
26322e001266SDarrick J. Wong 	int			i;
26332e001266SDarrick J. Wong 	int			error;
26342e001266SDarrick J. Wong 
26352e001266SDarrick J. Wong 	*exists = false;
26362e001266SDarrick J. Wong 	error = xfs_inobt_lookup(cur, low, XFS_LOOKUP_LE, &has_record);
26372e001266SDarrick J. Wong 	while (error == 0 && has_record) {
26382e001266SDarrick J. Wong 		error = xfs_inobt_get_rec(cur, &irec, &has_record);
26392e001266SDarrick J. Wong 		if (error || irec.ir_startino > high)
26402e001266SDarrick J. Wong 			break;
26412e001266SDarrick J. Wong 
26422e001266SDarrick J. Wong 		agino = irec.ir_startino;
26432e001266SDarrick J. Wong 		holemask = irec.ir_holemask;
26442e001266SDarrick J. Wong 		for (i = 0; i < XFS_INOBT_HOLEMASK_BITS; holemask >>= 1,
26452e001266SDarrick J. Wong 				i++, agino += XFS_INODES_PER_HOLEMASK_BIT) {
26462e001266SDarrick J. Wong 			if (holemask & 1)
26472e001266SDarrick J. Wong 				continue;
26482e001266SDarrick J. Wong 			if (agino + XFS_INODES_PER_HOLEMASK_BIT > low &&
26492e001266SDarrick J. Wong 					agino <= high) {
26502e001266SDarrick J. Wong 				*exists = true;
26512e001266SDarrick J. Wong 				return 0;
26522e001266SDarrick J. Wong 			}
26532e001266SDarrick J. Wong 		}
26542e001266SDarrick J. Wong 
26552e001266SDarrick J. Wong 		error = xfs_btree_increment(cur, 0, &has_record);
26562e001266SDarrick J. Wong 	}
26572e001266SDarrick J. Wong 	return error;
26582e001266SDarrick J. Wong }
26592e001266SDarrick J. Wong 
26602e001266SDarrick J. Wong /* Is there an inode record covering a given extent? */
26612e001266SDarrick J. Wong int
26622e001266SDarrick J. Wong xfs_ialloc_has_inodes_at_extent(
26632e001266SDarrick J. Wong 	struct xfs_btree_cur	*cur,
26642e001266SDarrick J. Wong 	xfs_agblock_t		bno,
26652e001266SDarrick J. Wong 	xfs_extlen_t		len,
26662e001266SDarrick J. Wong 	bool			*exists)
26672e001266SDarrick J. Wong {
26682e001266SDarrick J. Wong 	xfs_agino_t		low;
26692e001266SDarrick J. Wong 	xfs_agino_t		high;
26702e001266SDarrick J. Wong 
267143004b2aSDarrick J. Wong 	low = XFS_AGB_TO_AGINO(cur->bc_mp, bno);
267243004b2aSDarrick J. Wong 	high = XFS_AGB_TO_AGINO(cur->bc_mp, bno + len) - 1;
26732e001266SDarrick J. Wong 
26742e001266SDarrick J. Wong 	return xfs_ialloc_has_inode_record(cur, low, high, exists);
26752e001266SDarrick J. Wong }
26762e001266SDarrick J. Wong 
26772e001266SDarrick J. Wong struct xfs_ialloc_count_inodes {
26782e001266SDarrick J. Wong 	xfs_agino_t			count;
26792e001266SDarrick J. Wong 	xfs_agino_t			freecount;
26802e001266SDarrick J. Wong };
26812e001266SDarrick J. Wong 
26822e001266SDarrick J. Wong /* Record inode counts across all inobt records. */
26832e001266SDarrick J. Wong STATIC int
26842e001266SDarrick J. Wong xfs_ialloc_count_inodes_rec(
26852e001266SDarrick J. Wong 	struct xfs_btree_cur		*cur,
2686159eb69dSDarrick J. Wong 	const union xfs_btree_rec	*rec,
26872e001266SDarrick J. Wong 	void				*priv)
26882e001266SDarrick J. Wong {
26892e001266SDarrick J. Wong 	struct xfs_inobt_rec_incore	irec;
26902e001266SDarrick J. Wong 	struct xfs_ialloc_count_inodes	*ci = priv;
26912e001266SDarrick J. Wong 
26922e001266SDarrick J. Wong 	xfs_inobt_btrec_to_irec(cur->bc_mp, rec, &irec);
26932e001266SDarrick J. Wong 	ci->count += irec.ir_count;
26942e001266SDarrick J. Wong 	ci->freecount += irec.ir_freecount;
26952e001266SDarrick J. Wong 
26962e001266SDarrick J. Wong 	return 0;
26972e001266SDarrick J. Wong }
26982e001266SDarrick J. Wong 
26992e001266SDarrick J. Wong /* Count allocated and free inodes under an inobt. */
27002e001266SDarrick J. Wong int
27012e001266SDarrick J. Wong xfs_ialloc_count_inodes(
27022e001266SDarrick J. Wong 	struct xfs_btree_cur		*cur,
27032e001266SDarrick J. Wong 	xfs_agino_t			*count,
27042e001266SDarrick J. Wong 	xfs_agino_t			*freecount)
27052e001266SDarrick J. Wong {
27062e001266SDarrick J. Wong 	struct xfs_ialloc_count_inodes	ci = {0};
27072e001266SDarrick J. Wong 	int				error;
27082e001266SDarrick J. Wong 
27092e001266SDarrick J. Wong 	ASSERT(cur->bc_btnum == XFS_BTNUM_INO);
27102e001266SDarrick J. Wong 	error = xfs_btree_query_all(cur, xfs_ialloc_count_inodes_rec, &ci);
27112e001266SDarrick J. Wong 	if (error)
27122e001266SDarrick J. Wong 		return error;
27132e001266SDarrick J. Wong 
27142e001266SDarrick J. Wong 	*count = ci.count;
27152e001266SDarrick J. Wong 	*freecount = ci.freecount;
27162e001266SDarrick J. Wong 	return 0;
27172e001266SDarrick J. Wong }
2718494dba7bSDarrick J. Wong 
2719494dba7bSDarrick J. Wong /*
2720494dba7bSDarrick J. Wong  * Initialize inode-related geometry information.
2721494dba7bSDarrick J. Wong  *
2722494dba7bSDarrick J. Wong  * Compute the inode btree min and max levels and set maxicount.
2723494dba7bSDarrick J. Wong  *
2724494dba7bSDarrick J. Wong  * Set the inode cluster size.  This may still be overridden by the file
2725494dba7bSDarrick J. Wong  * system block size if it is larger than the chosen cluster size.
2726494dba7bSDarrick J. Wong  *
2727494dba7bSDarrick J. Wong  * For v5 filesystems, scale the cluster size with the inode size to keep a
2728494dba7bSDarrick J. Wong  * constant ratio of inode per cluster buffer, but only if mkfs has set the
2729494dba7bSDarrick J. Wong  * inode alignment value appropriately for larger cluster sizes.
2730494dba7bSDarrick J. Wong  *
2731494dba7bSDarrick J. Wong  * Then compute the inode cluster alignment information.
2732494dba7bSDarrick J. Wong  */
2733494dba7bSDarrick J. Wong void
2734494dba7bSDarrick J. Wong xfs_ialloc_setup_geometry(
2735494dba7bSDarrick J. Wong 	struct xfs_mount	*mp)
2736494dba7bSDarrick J. Wong {
2737494dba7bSDarrick J. Wong 	struct xfs_sb		*sbp = &mp->m_sb;
2738494dba7bSDarrick J. Wong 	struct xfs_ino_geometry	*igeo = M_IGEO(mp);
2739494dba7bSDarrick J. Wong 	uint64_t		icount;
2740494dba7bSDarrick J. Wong 	uint			inodes;
2741494dba7bSDarrick J. Wong 
2742f93e5436SDarrick J. Wong 	igeo->new_diflags2 = 0;
2743ebd9027dSDave Chinner 	if (xfs_has_bigtime(mp))
2744f93e5436SDarrick J. Wong 		igeo->new_diflags2 |= XFS_DIFLAG2_BIGTIME;
27459b7d16e3SChandan Babu R 	if (xfs_has_large_extent_counts(mp))
27469b7d16e3SChandan Babu R 		igeo->new_diflags2 |= XFS_DIFLAG2_NREXT64;
2747f93e5436SDarrick J. Wong 
2748494dba7bSDarrick J. Wong 	/* Compute inode btree geometry. */
2749494dba7bSDarrick J. Wong 	igeo->agino_log = sbp->sb_inopblog + sbp->sb_agblklog;
2750494dba7bSDarrick J. Wong 	igeo->inobt_mxr[0] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 1);
2751494dba7bSDarrick J. Wong 	igeo->inobt_mxr[1] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 0);
2752494dba7bSDarrick J. Wong 	igeo->inobt_mnr[0] = igeo->inobt_mxr[0] / 2;
2753494dba7bSDarrick J. Wong 	igeo->inobt_mnr[1] = igeo->inobt_mxr[1] / 2;
2754494dba7bSDarrick J. Wong 
2755494dba7bSDarrick J. Wong 	igeo->ialloc_inos = max_t(uint16_t, XFS_INODES_PER_CHUNK,
2756494dba7bSDarrick J. Wong 			sbp->sb_inopblock);
2757494dba7bSDarrick J. Wong 	igeo->ialloc_blks = igeo->ialloc_inos >> sbp->sb_inopblog;
2758494dba7bSDarrick J. Wong 
2759494dba7bSDarrick J. Wong 	if (sbp->sb_spino_align)
2760494dba7bSDarrick J. Wong 		igeo->ialloc_min_blks = sbp->sb_spino_align;
2761494dba7bSDarrick J. Wong 	else
2762494dba7bSDarrick J. Wong 		igeo->ialloc_min_blks = igeo->ialloc_blks;
2763494dba7bSDarrick J. Wong 
2764494dba7bSDarrick J. Wong 	/* Compute and fill in value of m_ino_geo.inobt_maxlevels. */
2765494dba7bSDarrick J. Wong 	inodes = (1LL << XFS_INO_AGINO_BITS(mp)) >> XFS_INODES_PER_CHUNK_LOG;
2766494dba7bSDarrick J. Wong 	igeo->inobt_maxlevels = xfs_btree_compute_maxlevels(igeo->inobt_mnr,
2767494dba7bSDarrick J. Wong 			inodes);
27680ed5f735SDarrick J. Wong 	ASSERT(igeo->inobt_maxlevels <= xfs_iallocbt_maxlevels_ondisk());
2769494dba7bSDarrick J. Wong 
2770c94613feSDarrick J. Wong 	/*
2771c94613feSDarrick J. Wong 	 * Set the maximum inode count for this filesystem, being careful not
2772c94613feSDarrick J. Wong 	 * to use obviously garbage sb_inopblog/sb_inopblock values.  Regular
2773c94613feSDarrick J. Wong 	 * users should never get here due to failing sb verification, but
2774c94613feSDarrick J. Wong 	 * certain users (xfs_db) need to be usable even with corrupt metadata.
2775c94613feSDarrick J. Wong 	 */
2776c94613feSDarrick J. Wong 	if (sbp->sb_imax_pct && igeo->ialloc_blks) {
2777494dba7bSDarrick J. Wong 		/*
2778494dba7bSDarrick J. Wong 		 * Make sure the maximum inode count is a multiple
2779494dba7bSDarrick J. Wong 		 * of the units we allocate inodes in.
2780494dba7bSDarrick J. Wong 		 */
2781494dba7bSDarrick J. Wong 		icount = sbp->sb_dblocks * sbp->sb_imax_pct;
2782494dba7bSDarrick J. Wong 		do_div(icount, 100);
2783494dba7bSDarrick J. Wong 		do_div(icount, igeo->ialloc_blks);
2784494dba7bSDarrick J. Wong 		igeo->maxicount = XFS_FSB_TO_INO(mp,
2785494dba7bSDarrick J. Wong 				icount * igeo->ialloc_blks);
2786494dba7bSDarrick J. Wong 	} else {
2787494dba7bSDarrick J. Wong 		igeo->maxicount = 0;
2788494dba7bSDarrick J. Wong 	}
2789494dba7bSDarrick J. Wong 
2790490d451fSDarrick J. Wong 	/*
2791490d451fSDarrick J. Wong 	 * Compute the desired size of an inode cluster buffer size, which
2792490d451fSDarrick J. Wong 	 * starts at 8K and (on v5 filesystems) scales up with larger inode
2793490d451fSDarrick J. Wong 	 * sizes.
2794490d451fSDarrick J. Wong 	 *
2795490d451fSDarrick J. Wong 	 * Preserve the desired inode cluster size because the sparse inodes
2796490d451fSDarrick J. Wong 	 * feature uses that desired size (not the actual size) to compute the
2797490d451fSDarrick J. Wong 	 * sparse inode alignment.  The mount code validates this value, so we
2798490d451fSDarrick J. Wong 	 * cannot change the behavior.
2799490d451fSDarrick J. Wong 	 */
2800490d451fSDarrick J. Wong 	igeo->inode_cluster_size_raw = XFS_INODE_BIG_CLUSTER_SIZE;
2801ebd9027dSDave Chinner 	if (xfs_has_v3inodes(mp)) {
2802490d451fSDarrick J. Wong 		int	new_size = igeo->inode_cluster_size_raw;
2803494dba7bSDarrick J. Wong 
2804494dba7bSDarrick J. Wong 		new_size *= mp->m_sb.sb_inodesize / XFS_DINODE_MIN_SIZE;
2805494dba7bSDarrick J. Wong 		if (mp->m_sb.sb_inoalignmt >= XFS_B_TO_FSBT(mp, new_size))
2806490d451fSDarrick J. Wong 			igeo->inode_cluster_size_raw = new_size;
2807494dba7bSDarrick J. Wong 	}
2808494dba7bSDarrick J. Wong 
2809494dba7bSDarrick J. Wong 	/* Calculate inode cluster ratios. */
2810490d451fSDarrick J. Wong 	if (igeo->inode_cluster_size_raw > mp->m_sb.sb_blocksize)
2811494dba7bSDarrick J. Wong 		igeo->blocks_per_cluster = XFS_B_TO_FSBT(mp,
2812490d451fSDarrick J. Wong 				igeo->inode_cluster_size_raw);
2813494dba7bSDarrick J. Wong 	else
2814494dba7bSDarrick J. Wong 		igeo->blocks_per_cluster = 1;
2815490d451fSDarrick J. Wong 	igeo->inode_cluster_size = XFS_FSB_TO_B(mp, igeo->blocks_per_cluster);
2816494dba7bSDarrick J. Wong 	igeo->inodes_per_cluster = XFS_FSB_TO_INO(mp, igeo->blocks_per_cluster);
2817494dba7bSDarrick J. Wong 
2818494dba7bSDarrick J. Wong 	/* Calculate inode cluster alignment. */
2819ebd9027dSDave Chinner 	if (xfs_has_align(mp) &&
2820494dba7bSDarrick J. Wong 	    mp->m_sb.sb_inoalignmt >= igeo->blocks_per_cluster)
2821494dba7bSDarrick J. Wong 		igeo->cluster_align = mp->m_sb.sb_inoalignmt;
2822494dba7bSDarrick J. Wong 	else
2823494dba7bSDarrick J. Wong 		igeo->cluster_align = 1;
2824494dba7bSDarrick J. Wong 	igeo->inoalign_mask = igeo->cluster_align - 1;
2825494dba7bSDarrick J. Wong 	igeo->cluster_align_inodes = XFS_FSB_TO_INO(mp, igeo->cluster_align);
2826494dba7bSDarrick J. Wong 
2827494dba7bSDarrick J. Wong 	/*
2828494dba7bSDarrick J. Wong 	 * If we are using stripe alignment, check whether
2829494dba7bSDarrick J. Wong 	 * the stripe unit is a multiple of the inode alignment
2830494dba7bSDarrick J. Wong 	 */
2831494dba7bSDarrick J. Wong 	if (mp->m_dalign && igeo->inoalign_mask &&
2832494dba7bSDarrick J. Wong 	    !(mp->m_dalign & igeo->inoalign_mask))
2833494dba7bSDarrick J. Wong 		igeo->ialloc_align = mp->m_dalign;
2834494dba7bSDarrick J. Wong 	else
2835494dba7bSDarrick J. Wong 		igeo->ialloc_align = 0;
2836494dba7bSDarrick J. Wong }
283713eaec4bSDarrick J. Wong 
283813eaec4bSDarrick J. Wong /* Compute the location of the root directory inode that is laid out by mkfs. */
283913eaec4bSDarrick J. Wong xfs_ino_t
284013eaec4bSDarrick J. Wong xfs_ialloc_calc_rootino(
284113eaec4bSDarrick J. Wong 	struct xfs_mount	*mp,
284213eaec4bSDarrick J. Wong 	int			sunit)
284313eaec4bSDarrick J. Wong {
284413eaec4bSDarrick J. Wong 	struct xfs_ino_geometry	*igeo = M_IGEO(mp);
284513eaec4bSDarrick J. Wong 	xfs_agblock_t		first_bno;
284613eaec4bSDarrick J. Wong 
284713eaec4bSDarrick J. Wong 	/*
284813eaec4bSDarrick J. Wong 	 * Pre-calculate the geometry of AG 0.  We know what it looks like
284913eaec4bSDarrick J. Wong 	 * because libxfs knows how to create allocation groups now.
285013eaec4bSDarrick J. Wong 	 *
285113eaec4bSDarrick J. Wong 	 * first_bno is the first block in which mkfs could possibly have
285213eaec4bSDarrick J. Wong 	 * allocated the root directory inode, once we factor in the metadata
285313eaec4bSDarrick J. Wong 	 * that mkfs formats before it.  Namely, the four AG headers...
285413eaec4bSDarrick J. Wong 	 */
285513eaec4bSDarrick J. Wong 	first_bno = howmany(4 * mp->m_sb.sb_sectsize, mp->m_sb.sb_blocksize);
285613eaec4bSDarrick J. Wong 
285713eaec4bSDarrick J. Wong 	/* ...the two free space btree roots... */
285813eaec4bSDarrick J. Wong 	first_bno += 2;
285913eaec4bSDarrick J. Wong 
286013eaec4bSDarrick J. Wong 	/* ...the inode btree root... */
286113eaec4bSDarrick J. Wong 	first_bno += 1;
286213eaec4bSDarrick J. Wong 
286313eaec4bSDarrick J. Wong 	/* ...the initial AGFL... */
286413eaec4bSDarrick J. Wong 	first_bno += xfs_alloc_min_freelist(mp, NULL);
286513eaec4bSDarrick J. Wong 
286613eaec4bSDarrick J. Wong 	/* ...the free inode btree root... */
2867ebd9027dSDave Chinner 	if (xfs_has_finobt(mp))
286813eaec4bSDarrick J. Wong 		first_bno++;
286913eaec4bSDarrick J. Wong 
287013eaec4bSDarrick J. Wong 	/* ...the reverse mapping btree root... */
2871ebd9027dSDave Chinner 	if (xfs_has_rmapbt(mp))
287213eaec4bSDarrick J. Wong 		first_bno++;
287313eaec4bSDarrick J. Wong 
287413eaec4bSDarrick J. Wong 	/* ...the reference count btree... */
2875ebd9027dSDave Chinner 	if (xfs_has_reflink(mp))
287613eaec4bSDarrick J. Wong 		first_bno++;
287713eaec4bSDarrick J. Wong 
287813eaec4bSDarrick J. Wong 	/*
287913eaec4bSDarrick J. Wong 	 * ...and the log, if it is allocated in the first allocation group.
288013eaec4bSDarrick J. Wong 	 *
288113eaec4bSDarrick J. Wong 	 * This can happen with filesystems that only have a single
288213eaec4bSDarrick J. Wong 	 * allocation group, or very odd geometries created by old mkfs
288313eaec4bSDarrick J. Wong 	 * versions on very small filesystems.
288413eaec4bSDarrick J. Wong 	 */
288536029deeSDave Chinner 	if (xfs_ag_contains_log(mp, 0))
288613eaec4bSDarrick J. Wong 		 first_bno += mp->m_sb.sb_logblocks;
288713eaec4bSDarrick J. Wong 
288813eaec4bSDarrick J. Wong 	/*
288913eaec4bSDarrick J. Wong 	 * Now round first_bno up to whatever allocation alignment is given
289013eaec4bSDarrick J. Wong 	 * by the filesystem or was passed in.
289113eaec4bSDarrick J. Wong 	 */
2892ebd9027dSDave Chinner 	if (xfs_has_dalign(mp) && igeo->ialloc_align > 0)
289313eaec4bSDarrick J. Wong 		first_bno = roundup(first_bno, sunit);
2894ebd9027dSDave Chinner 	else if (xfs_has_align(mp) &&
289513eaec4bSDarrick J. Wong 			mp->m_sb.sb_inoalignmt > 1)
289613eaec4bSDarrick J. Wong 		first_bno = roundup(first_bno, mp->m_sb.sb_inoalignmt);
289713eaec4bSDarrick J. Wong 
289813eaec4bSDarrick J. Wong 	return XFS_AGINO_TO_INO(mp, 0, XFS_AGB_TO_AGINO(mp, first_bno));
289913eaec4bSDarrick J. Wong }
2900da062d16SDarrick J. Wong 
2901da062d16SDarrick J. Wong /*
2902da062d16SDarrick J. Wong  * Ensure there are not sparse inode clusters that cross the new EOAG.
2903da062d16SDarrick J. Wong  *
2904da062d16SDarrick J. Wong  * This is a no-op for non-spinode filesystems since clusters are always fully
2905da062d16SDarrick J. Wong  * allocated and checking the bnobt suffices.  However, a spinode filesystem
2906da062d16SDarrick J. Wong  * could have a record where the upper inodes are free blocks.  If those blocks
2907da062d16SDarrick J. Wong  * were removed from the filesystem, the inode record would extend beyond EOAG,
2908da062d16SDarrick J. Wong  * which will be flagged as corruption.
2909da062d16SDarrick J. Wong  */
2910da062d16SDarrick J. Wong int
2911da062d16SDarrick J. Wong xfs_ialloc_check_shrink(
2912dedab3e4SDave Chinner 	struct xfs_perag	*pag,
2913da062d16SDarrick J. Wong 	struct xfs_trans	*tp,
2914da062d16SDarrick J. Wong 	struct xfs_buf		*agibp,
2915da062d16SDarrick J. Wong 	xfs_agblock_t		new_length)
2916da062d16SDarrick J. Wong {
2917da062d16SDarrick J. Wong 	struct xfs_inobt_rec_incore rec;
2918da062d16SDarrick J. Wong 	struct xfs_btree_cur	*cur;
2919bab8b795SDave Chinner 	xfs_agino_t		agino;
2920da062d16SDarrick J. Wong 	int			has;
2921da062d16SDarrick J. Wong 	int			error;
2922da062d16SDarrick J. Wong 
2923bab8b795SDave Chinner 	if (!xfs_has_sparseinodes(pag->pag_mount))
2924da062d16SDarrick J. Wong 		return 0;
2925da062d16SDarrick J. Wong 
2926bab8b795SDave Chinner 	cur = xfs_inobt_init_cursor(pag, tp, agibp, XFS_BTNUM_INO);
2927da062d16SDarrick J. Wong 
2928da062d16SDarrick J. Wong 	/* Look up the inobt record that would correspond to the new EOFS. */
2929bab8b795SDave Chinner 	agino = XFS_AGB_TO_AGINO(pag->pag_mount, new_length);
2930da062d16SDarrick J. Wong 	error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &has);
2931da062d16SDarrick J. Wong 	if (error || !has)
2932da062d16SDarrick J. Wong 		goto out;
2933da062d16SDarrick J. Wong 
2934da062d16SDarrick J. Wong 	error = xfs_inobt_get_rec(cur, &rec, &has);
2935da062d16SDarrick J. Wong 	if (error)
2936da062d16SDarrick J. Wong 		goto out;
2937da062d16SDarrick J. Wong 
2938da062d16SDarrick J. Wong 	if (!has) {
2939da062d16SDarrick J. Wong 		error = -EFSCORRUPTED;
2940da062d16SDarrick J. Wong 		goto out;
2941da062d16SDarrick J. Wong 	}
2942da062d16SDarrick J. Wong 
2943da062d16SDarrick J. Wong 	/* If the record covers inodes that would be beyond EOFS, bail out. */
2944da062d16SDarrick J. Wong 	if (rec.ir_startino + XFS_INODES_PER_CHUNK > agino) {
2945da062d16SDarrick J. Wong 		error = -ENOSPC;
2946da062d16SDarrick J. Wong 		goto out;
2947da062d16SDarrick J. Wong 	}
2948da062d16SDarrick J. Wong out:
2949da062d16SDarrick J. Wong 	xfs_btree_del_cursor(cur, error);
2950da062d16SDarrick J. Wong 	return error;
2951da062d16SDarrick J. Wong }
2952