xref: /openbmc/linux/fs/xfs/xfs_itable.c (revision 7b6259e7a83647948fa33a736cc832310c8d85aa)
11da177e4SLinus Torvalds /*
27b718769SNathan Scott  * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
37b718769SNathan Scott  * All Rights Reserved.
41da177e4SLinus Torvalds  *
57b718769SNathan Scott  * This program is free software; you can redistribute it and/or
67b718769SNathan Scott  * modify it under the terms of the GNU General Public License as
71da177e4SLinus Torvalds  * published by the Free Software Foundation.
81da177e4SLinus Torvalds  *
97b718769SNathan Scott  * This program is distributed in the hope that it would be useful,
107b718769SNathan Scott  * but WITHOUT ANY WARRANTY; without even the implied warranty of
117b718769SNathan Scott  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
127b718769SNathan Scott  * GNU General Public License for more details.
131da177e4SLinus Torvalds  *
147b718769SNathan Scott  * You should have received a copy of the GNU General Public License
157b718769SNathan Scott  * along with this program; if not, write the Free Software Foundation,
167b718769SNathan Scott  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
171da177e4SLinus Torvalds  */
181da177e4SLinus Torvalds #include "xfs.h"
19a844f451SNathan Scott #include "xfs_fs.h"
201da177e4SLinus Torvalds #include "xfs_types.h"
21a844f451SNathan Scott #include "xfs_bit.h"
221da177e4SLinus Torvalds #include "xfs_log.h"
23a844f451SNathan Scott #include "xfs_inum.h"
241da177e4SLinus Torvalds #include "xfs_trans.h"
251da177e4SLinus Torvalds #include "xfs_sb.h"
26a844f451SNathan Scott #include "xfs_ag.h"
271da177e4SLinus Torvalds #include "xfs_dir2.h"
281da177e4SLinus Torvalds #include "xfs_dmapi.h"
291da177e4SLinus Torvalds #include "xfs_mount.h"
301da177e4SLinus Torvalds #include "xfs_bmap_btree.h"
31a844f451SNathan Scott #include "xfs_alloc_btree.h"
321da177e4SLinus Torvalds #include "xfs_ialloc_btree.h"
331da177e4SLinus Torvalds #include "xfs_dir2_sf.h"
34a844f451SNathan Scott #include "xfs_attr_sf.h"
351da177e4SLinus Torvalds #include "xfs_dinode.h"
361da177e4SLinus Torvalds #include "xfs_inode.h"
371da177e4SLinus Torvalds #include "xfs_ialloc.h"
381da177e4SLinus Torvalds #include "xfs_itable.h"
391da177e4SLinus Torvalds #include "xfs_error.h"
40a844f451SNathan Scott #include "xfs_btree.h"
411da177e4SLinus Torvalds 
42d96f8f89SEric Sandeen STATIC int
436f1f2168SVlad Apostolov xfs_internal_inum(
446f1f2168SVlad Apostolov 	xfs_mount_t	*mp,
456f1f2168SVlad Apostolov 	xfs_ino_t	ino)
466f1f2168SVlad Apostolov {
476f1f2168SVlad Apostolov 	return (ino == mp->m_sb.sb_rbmino || ino == mp->m_sb.sb_rsumino ||
4862118709SEric Sandeen 		(xfs_sb_version_hasquota(&mp->m_sb) &&
496f1f2168SVlad Apostolov 		 (ino == mp->m_sb.sb_uquotino || ino == mp->m_sb.sb_gquotino)));
506f1f2168SVlad Apostolov }
516f1f2168SVlad Apostolov 
527dce11dbSChristoph Hellwig /*
537dce11dbSChristoph Hellwig  * Return stat information for one inode.
547dce11dbSChristoph Hellwig  * Return 0 if ok, else errno.
557dce11dbSChristoph Hellwig  */
567dce11dbSChristoph Hellwig int
577dce11dbSChristoph Hellwig xfs_bulkstat_one_int(
587dce11dbSChristoph Hellwig 	struct xfs_mount	*mp,		/* mount point for filesystem */
597dce11dbSChristoph Hellwig 	xfs_ino_t		ino,		/* inode to get data for */
607dce11dbSChristoph Hellwig 	void __user		*buffer,	/* buffer to place output in */
617dce11dbSChristoph Hellwig 	int			ubsize,		/* size of buffer */
627dce11dbSChristoph Hellwig 	bulkstat_one_fmt_pf	formatter,	/* formatter, copy to user */
637dce11dbSChristoph Hellwig 	int			*ubused,	/* bytes used by me */
641da177e4SLinus Torvalds 	int			*stat)		/* BULKSTAT_RV_... */
651da177e4SLinus Torvalds {
667dce11dbSChristoph Hellwig 	struct xfs_icdinode	*dic;		/* dinode core info pointer */
677dce11dbSChristoph Hellwig 	struct xfs_inode	*ip;		/* incore inode pointer */
68f9581b14SChristoph Hellwig 	struct inode		*inode;
697dce11dbSChristoph Hellwig 	struct xfs_bstat	*buf;		/* return buffer */
707dce11dbSChristoph Hellwig 	int			error = 0;	/* error value */
717dce11dbSChristoph Hellwig 
727dce11dbSChristoph Hellwig 	*stat = BULKSTAT_RV_NOTHING;
737dce11dbSChristoph Hellwig 
747dce11dbSChristoph Hellwig 	if (!buffer || xfs_internal_inum(mp, ino))
757dce11dbSChristoph Hellwig 		return XFS_ERROR(EINVAL);
767dce11dbSChristoph Hellwig 
777dce11dbSChristoph Hellwig 	buf = kmem_alloc(sizeof(*buf), KM_SLEEP | KM_MAYFAIL);
787dce11dbSChristoph Hellwig 	if (!buf)
797dce11dbSChristoph Hellwig 		return XFS_ERROR(ENOMEM);
801da177e4SLinus Torvalds 
81745b1f47SNathan Scott 	error = xfs_iget(mp, NULL, ino,
82*7b6259e7SDave Chinner 			 XFS_IGET_UNTRUSTED, XFS_ILOCK_SHARED, &ip);
831da177e4SLinus Torvalds 	if (error) {
841da177e4SLinus Torvalds 		*stat = BULKSTAT_RV_NOTHING;
857dce11dbSChristoph Hellwig 		goto out_free;
861da177e4SLinus Torvalds 	}
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds 	ASSERT(ip != NULL);
8992bfc6e7SChristoph Hellwig 	ASSERT(ip->i_imap.im_blkno != 0);
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds 	dic = &ip->i_d;
92f9581b14SChristoph Hellwig 	inode = VFS_I(ip);
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds 	/* xfs_iget returns the following without needing
951da177e4SLinus Torvalds 	 * further change.
961da177e4SLinus Torvalds 	 */
971da177e4SLinus Torvalds 	buf->bs_nlink = dic->di_nlink;
981da177e4SLinus Torvalds 	buf->bs_projid = dic->di_projid;
991da177e4SLinus Torvalds 	buf->bs_ino = ino;
1001da177e4SLinus Torvalds 	buf->bs_mode = dic->di_mode;
1011da177e4SLinus Torvalds 	buf->bs_uid = dic->di_uid;
1021da177e4SLinus Torvalds 	buf->bs_gid = dic->di_gid;
1031da177e4SLinus Torvalds 	buf->bs_size = dic->di_size;
104f9581b14SChristoph Hellwig 
1058fab451eSChristoph Hellwig 	/*
106f9581b14SChristoph Hellwig 	 * We need to read the timestamps from the Linux inode because
107f9581b14SChristoph Hellwig 	 * the VFS keeps writing directly into the inode structure instead
108f9581b14SChristoph Hellwig 	 * of telling us about the updates.
1098fab451eSChristoph Hellwig 	 */
110f9581b14SChristoph Hellwig 	buf->bs_atime.tv_sec = inode->i_atime.tv_sec;
111f9581b14SChristoph Hellwig 	buf->bs_atime.tv_nsec = inode->i_atime.tv_nsec;
112f9581b14SChristoph Hellwig 	buf->bs_mtime.tv_sec = inode->i_mtime.tv_sec;
113f9581b14SChristoph Hellwig 	buf->bs_mtime.tv_nsec = inode->i_mtime.tv_nsec;
114f9581b14SChristoph Hellwig 	buf->bs_ctime.tv_sec = inode->i_ctime.tv_sec;
115f9581b14SChristoph Hellwig 	buf->bs_ctime.tv_nsec = inode->i_ctime.tv_nsec;
116f9581b14SChristoph Hellwig 
1171da177e4SLinus Torvalds 	buf->bs_xflags = xfs_ip2xflags(ip);
1181da177e4SLinus Torvalds 	buf->bs_extsize = dic->di_extsize << mp->m_sb.sb_blocklog;
1191da177e4SLinus Torvalds 	buf->bs_extents = dic->di_nextents;
1201da177e4SLinus Torvalds 	buf->bs_gen = dic->di_gen;
1211da177e4SLinus Torvalds 	memset(buf->bs_pad, 0, sizeof(buf->bs_pad));
1221da177e4SLinus Torvalds 	buf->bs_dmevmask = dic->di_dmevmask;
1231da177e4SLinus Torvalds 	buf->bs_dmstate = dic->di_dmstate;
1241da177e4SLinus Torvalds 	buf->bs_aextents = dic->di_anextents;
12507000ee6SDave Chinner 	buf->bs_forkoff = XFS_IFORK_BOFF(ip);
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds 	switch (dic->di_format) {
1281da177e4SLinus Torvalds 	case XFS_DINODE_FMT_DEV:
1291da177e4SLinus Torvalds 		buf->bs_rdev = ip->i_df.if_u2.if_rdev;
1301da177e4SLinus Torvalds 		buf->bs_blksize = BLKDEV_IOSIZE;
1311da177e4SLinus Torvalds 		buf->bs_blocks = 0;
1321da177e4SLinus Torvalds 		break;
1331da177e4SLinus Torvalds 	case XFS_DINODE_FMT_LOCAL:
1341da177e4SLinus Torvalds 	case XFS_DINODE_FMT_UUID:
1351da177e4SLinus Torvalds 		buf->bs_rdev = 0;
1361da177e4SLinus Torvalds 		buf->bs_blksize = mp->m_sb.sb_blocksize;
1371da177e4SLinus Torvalds 		buf->bs_blocks = 0;
1381da177e4SLinus Torvalds 		break;
1391da177e4SLinus Torvalds 	case XFS_DINODE_FMT_EXTENTS:
1401da177e4SLinus Torvalds 	case XFS_DINODE_FMT_BTREE:
1411da177e4SLinus Torvalds 		buf->bs_rdev = 0;
1421da177e4SLinus Torvalds 		buf->bs_blksize = mp->m_sb.sb_blocksize;
1431da177e4SLinus Torvalds 		buf->bs_blocks = dic->di_nblocks + ip->i_delayed_blks;
1441da177e4SLinus Torvalds 		break;
1451da177e4SLinus Torvalds 	}
1461da177e4SLinus Torvalds 	xfs_iput(ip, XFS_ILOCK_SHARED);
1477dce11dbSChristoph Hellwig 
1487dce11dbSChristoph Hellwig 	error = formatter(buffer, ubsize, ubused, buf);
1497dce11dbSChristoph Hellwig 
1507dce11dbSChristoph Hellwig 	if (!error)
1517dce11dbSChristoph Hellwig 		*stat = BULKSTAT_RV_DIDONE;
1527dce11dbSChristoph Hellwig 
1537dce11dbSChristoph Hellwig  out_free:
1547dce11dbSChristoph Hellwig 	kmem_free(buf);
1551da177e4SLinus Torvalds 	return error;
1561da177e4SLinus Torvalds }
1571da177e4SLinus Torvalds 
15865fbaf24Ssandeen@sandeen.net /* Return 0 on success or positive error */
159faa63e95SMichal Marek STATIC int
160faa63e95SMichal Marek xfs_bulkstat_one_fmt(
161faa63e95SMichal Marek 	void			__user *ubuffer,
16265fbaf24Ssandeen@sandeen.net 	int			ubsize,
16365fbaf24Ssandeen@sandeen.net 	int			*ubused,
164faa63e95SMichal Marek 	const xfs_bstat_t	*buffer)
165faa63e95SMichal Marek {
16665fbaf24Ssandeen@sandeen.net 	if (ubsize < sizeof(*buffer))
16765fbaf24Ssandeen@sandeen.net 		return XFS_ERROR(ENOMEM);
168faa63e95SMichal Marek 	if (copy_to_user(ubuffer, buffer, sizeof(*buffer)))
16965fbaf24Ssandeen@sandeen.net 		return XFS_ERROR(EFAULT);
17065fbaf24Ssandeen@sandeen.net 	if (ubused)
17165fbaf24Ssandeen@sandeen.net 		*ubused = sizeof(*buffer);
17265fbaf24Ssandeen@sandeen.net 	return 0;
173faa63e95SMichal Marek }
174faa63e95SMichal Marek 
1752ee4fa5cSsandeen@sandeen.net int
1762ee4fa5cSsandeen@sandeen.net xfs_bulkstat_one(
1772ee4fa5cSsandeen@sandeen.net 	xfs_mount_t	*mp,		/* mount point for filesystem */
1782ee4fa5cSsandeen@sandeen.net 	xfs_ino_t	ino,		/* inode number to get data for */
1792ee4fa5cSsandeen@sandeen.net 	void		__user *buffer,	/* buffer to place output in */
1802ee4fa5cSsandeen@sandeen.net 	int		ubsize,		/* size of buffer */
1812ee4fa5cSsandeen@sandeen.net 	int		*ubused,	/* bytes used by me */
1822ee4fa5cSsandeen@sandeen.net 	int		*stat)		/* BULKSTAT_RV_... */
1832ee4fa5cSsandeen@sandeen.net {
1842ee4fa5cSsandeen@sandeen.net 	return xfs_bulkstat_one_int(mp, ino, buffer, ubsize,
185*7b6259e7SDave Chinner 				    xfs_bulkstat_one_fmt, ubused, stat);
1868b56f083SNathan Scott }
1878b56f083SNathan Scott 
188cd57e594SLachlan McIlroy #define XFS_BULKSTAT_UBLEFT(ubleft)	((ubleft) >= statstruct_size)
189cd57e594SLachlan McIlroy 
1908b56f083SNathan Scott /*
1911da177e4SLinus Torvalds  * Return stat information in bulk (by-inode) for the filesystem.
1921da177e4SLinus Torvalds  */
1931da177e4SLinus Torvalds int					/* error status */
1941da177e4SLinus Torvalds xfs_bulkstat(
1951da177e4SLinus Torvalds 	xfs_mount_t		*mp,	/* mount point for filesystem */
1961da177e4SLinus Torvalds 	xfs_ino_t		*lastinop, /* last inode returned */
1971da177e4SLinus Torvalds 	int			*ubcountp, /* size of buffer/count returned */
1981da177e4SLinus Torvalds 	bulkstat_one_pf		formatter, /* func that'd fill a single buf */
1991da177e4SLinus Torvalds 	size_t			statstruct_size, /* sizeof struct filling */
2001da177e4SLinus Torvalds 	char			__user *ubuffer, /* buffer with inode stats */
201c41564b5SNathan Scott 	int			*done)	/* 1 if there are more stats to get */
2021da177e4SLinus Torvalds {
2031da177e4SLinus Torvalds 	xfs_agblock_t		agbno=0;/* allocation group block number */
2041da177e4SLinus Torvalds 	xfs_buf_t		*agbp;	/* agi header buffer */
2051da177e4SLinus Torvalds 	xfs_agi_t		*agi;	/* agi header data */
2061da177e4SLinus Torvalds 	xfs_agino_t		agino;	/* inode # in allocation group */
2071da177e4SLinus Torvalds 	xfs_agnumber_t		agno;	/* allocation group number */
2081da177e4SLinus Torvalds 	xfs_daddr_t		bno;	/* inode cluster start daddr */
2091da177e4SLinus Torvalds 	int			chunkidx; /* current index into inode chunk */
2101da177e4SLinus Torvalds 	int			clustidx; /* current index into inode cluster */
2111da177e4SLinus Torvalds 	xfs_btree_cur_t		*cur;	/* btree cursor for ialloc btree */
2121da177e4SLinus Torvalds 	int			end_of_ag; /* set if we've seen the ag end */
2131da177e4SLinus Torvalds 	int			error;	/* error code */
2141da177e4SLinus Torvalds 	int                     fmterror;/* bulkstat formatter result */
2151da177e4SLinus Torvalds 	int			i;	/* loop index */
2161da177e4SLinus Torvalds 	int			icount;	/* count of inodes good in irbuf */
217215101c3SNathan Scott 	size_t			irbsize; /* size of irec buffer in bytes */
2181da177e4SLinus Torvalds 	xfs_ino_t		ino;	/* inode number (filesystem) */
21926275093SNathan Scott 	xfs_inobt_rec_incore_t	*irbp;	/* current irec buffer pointer */
22026275093SNathan Scott 	xfs_inobt_rec_incore_t	*irbuf;	/* start of irec buffer */
22126275093SNathan Scott 	xfs_inobt_rec_incore_t	*irbufend; /* end of good irec buffer entries */
222cd57e594SLachlan McIlroy 	xfs_ino_t		lastino; /* last inode number returned */
2231da177e4SLinus Torvalds 	int			nbcluster; /* # of blocks in a cluster */
2241da177e4SLinus Torvalds 	int			nicluster; /* # of inodes in a cluster */
2251da177e4SLinus Torvalds 	int			nimask;	/* mask for inode clusters */
2261da177e4SLinus Torvalds 	int			nirbuf;	/* size of irbuf */
2271da177e4SLinus Torvalds 	int			rval;	/* return value error code */
2281da177e4SLinus Torvalds 	int			tmp;	/* result value from btree calls */
2291da177e4SLinus Torvalds 	int			ubcount; /* size of user's buffer */
2301da177e4SLinus Torvalds 	int			ubleft;	/* bytes left in user's buffer */
2311da177e4SLinus Torvalds 	char			__user *ubufp;	/* pointer into user's buffer */
2321da177e4SLinus Torvalds 	int			ubelem;	/* spaces used in user's buffer */
2331da177e4SLinus Torvalds 	int			ubused;	/* bytes used by formatter */
2341da177e4SLinus Torvalds 	xfs_buf_t		*bp;	/* ptr to on-disk inode cluster buf */
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds 	/*
2371da177e4SLinus Torvalds 	 * Get the last inode value, see if there's nothing to do.
2381da177e4SLinus Torvalds 	 */
2391da177e4SLinus Torvalds 	ino = (xfs_ino_t)*lastinop;
240cd57e594SLachlan McIlroy 	lastino = ino;
2411da177e4SLinus Torvalds 	agno = XFS_INO_TO_AGNO(mp, ino);
2421da177e4SLinus Torvalds 	agino = XFS_INO_TO_AGINO(mp, ino);
2431da177e4SLinus Torvalds 	if (agno >= mp->m_sb.sb_agcount ||
2441da177e4SLinus Torvalds 	    ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
2451da177e4SLinus Torvalds 		*done = 1;
2461da177e4SLinus Torvalds 		*ubcountp = 0;
2471da177e4SLinus Torvalds 		return 0;
2481da177e4SLinus Torvalds 	}
249cd57e594SLachlan McIlroy 	if (!ubcountp || *ubcountp <= 0) {
250cd57e594SLachlan McIlroy 		return EINVAL;
251cd57e594SLachlan McIlroy 	}
2521da177e4SLinus Torvalds 	ubcount = *ubcountp; /* statstruct's */
2531da177e4SLinus Torvalds 	ubleft = ubcount * statstruct_size; /* bytes */
2541da177e4SLinus Torvalds 	*ubcountp = ubelem = 0;
2551da177e4SLinus Torvalds 	*done = 0;
2561da177e4SLinus Torvalds 	fmterror = 0;
2571da177e4SLinus Torvalds 	ubufp = ubuffer;
2581da177e4SLinus Torvalds 	nicluster = mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp) ?
2591da177e4SLinus Torvalds 		mp->m_sb.sb_inopblock :
2601da177e4SLinus Torvalds 		(XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog);
2611da177e4SLinus Torvalds 	nimask = ~(nicluster - 1);
2621da177e4SLinus Torvalds 	nbcluster = nicluster >> mp->m_sb.sb_inopblog;
263bdfb0430SChristoph Hellwig 	irbuf = kmem_zalloc_greedy(&irbsize, PAGE_SIZE, PAGE_SIZE * 4);
264bdfb0430SChristoph Hellwig 	if (!irbuf)
265bdfb0430SChristoph Hellwig 		return ENOMEM;
266bdfb0430SChristoph Hellwig 
267bb3c7d29SNathan Scott 	nirbuf = irbsize / sizeof(*irbuf);
268bb3c7d29SNathan Scott 
2691da177e4SLinus Torvalds 	/*
2701da177e4SLinus Torvalds 	 * Loop over the allocation groups, starting from the last
2711da177e4SLinus Torvalds 	 * inode returned; 0 means start of the allocation group.
2721da177e4SLinus Torvalds 	 */
2731da177e4SLinus Torvalds 	rval = 0;
274cd57e594SLachlan McIlroy 	while (XFS_BULKSTAT_UBLEFT(ubleft) && agno < mp->m_sb.sb_agcount) {
275cd57e594SLachlan McIlroy 		cond_resched();
2761da177e4SLinus Torvalds 		bp = NULL;
2771da177e4SLinus Torvalds 		error = xfs_ialloc_read_agi(mp, NULL, agno, &agbp);
2781da177e4SLinus Torvalds 		if (error) {
2791da177e4SLinus Torvalds 			/*
2801da177e4SLinus Torvalds 			 * Skip this allocation group and go to the next one.
2811da177e4SLinus Torvalds 			 */
2821da177e4SLinus Torvalds 			agno++;
2831da177e4SLinus Torvalds 			agino = 0;
2841da177e4SLinus Torvalds 			continue;
2851da177e4SLinus Torvalds 		}
2861da177e4SLinus Torvalds 		agi = XFS_BUF_TO_AGI(agbp);
2871da177e4SLinus Torvalds 		/*
2881da177e4SLinus Torvalds 		 * Allocate and initialize a btree cursor for ialloc btree.
2891da177e4SLinus Torvalds 		 */
290561f7d17SChristoph Hellwig 		cur = xfs_inobt_init_cursor(mp, NULL, agbp, agno);
2911da177e4SLinus Torvalds 		irbp = irbuf;
2921da177e4SLinus Torvalds 		irbufend = irbuf + nirbuf;
2931da177e4SLinus Torvalds 		end_of_ag = 0;
2941da177e4SLinus Torvalds 		/*
2951da177e4SLinus Torvalds 		 * If we're returning in the middle of an allocation group,
2961da177e4SLinus Torvalds 		 * we need to get the remainder of the chunk we're in.
2971da177e4SLinus Torvalds 		 */
2981da177e4SLinus Torvalds 		if (agino > 0) {
2992e287a73SChristoph Hellwig 			xfs_inobt_rec_incore_t r;
3002e287a73SChristoph Hellwig 
3011da177e4SLinus Torvalds 			/*
3021da177e4SLinus Torvalds 			 * Lookup the inode chunk that this inode lives in.
3031da177e4SLinus Torvalds 			 */
30421875505SChristoph Hellwig 			error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE,
30521875505SChristoph Hellwig 						 &tmp);
3061da177e4SLinus Torvalds 			if (!error &&	/* no I/O error */
3071da177e4SLinus Torvalds 			    tmp &&	/* lookup succeeded */
3081da177e4SLinus Torvalds 					/* got the record, should always work */
3092e287a73SChristoph Hellwig 			    !(error = xfs_inobt_get_rec(cur, &r, &i)) &&
3101da177e4SLinus Torvalds 			    i == 1 &&
3111da177e4SLinus Torvalds 					/* this is the right chunk */
3122e287a73SChristoph Hellwig 			    agino < r.ir_startino + XFS_INODES_PER_CHUNK &&
3131da177e4SLinus Torvalds 					/* lastino was not last in chunk */
3142e287a73SChristoph Hellwig 			    (chunkidx = agino - r.ir_startino + 1) <
3151da177e4SLinus Torvalds 				    XFS_INODES_PER_CHUNK &&
3161da177e4SLinus Torvalds 					/* there are some left allocated */
3179d87c319SEric Sandeen 			    xfs_inobt_maskn(chunkidx,
3182e287a73SChristoph Hellwig 				    XFS_INODES_PER_CHUNK - chunkidx) &
3192e287a73SChristoph Hellwig 				    ~r.ir_free) {
3201da177e4SLinus Torvalds 				/*
3211da177e4SLinus Torvalds 				 * Grab the chunk record.  Mark all the
3221da177e4SLinus Torvalds 				 * uninteresting inodes (because they're
3231da177e4SLinus Torvalds 				 * before our start point) free.
3241da177e4SLinus Torvalds 				 */
3251da177e4SLinus Torvalds 				for (i = 0; i < chunkidx; i++) {
3262e287a73SChristoph Hellwig 					if (XFS_INOBT_MASK(i) & ~r.ir_free)
3272e287a73SChristoph Hellwig 						r.ir_freecount++;
3281da177e4SLinus Torvalds 				}
3292e287a73SChristoph Hellwig 				r.ir_free |= xfs_inobt_maskn(0, chunkidx);
3302e287a73SChristoph Hellwig 				irbp->ir_startino = r.ir_startino;
3312e287a73SChristoph Hellwig 				irbp->ir_freecount = r.ir_freecount;
3322e287a73SChristoph Hellwig 				irbp->ir_free = r.ir_free;
3331da177e4SLinus Torvalds 				irbp++;
3342e287a73SChristoph Hellwig 				agino = r.ir_startino + XFS_INODES_PER_CHUNK;
3352e287a73SChristoph Hellwig 				icount = XFS_INODES_PER_CHUNK - r.ir_freecount;
3361da177e4SLinus Torvalds 			} else {
3371da177e4SLinus Torvalds 				/*
3381da177e4SLinus Torvalds 				 * If any of those tests failed, bump the
3391da177e4SLinus Torvalds 				 * inode number (just in case).
3401da177e4SLinus Torvalds 				 */
3411da177e4SLinus Torvalds 				agino++;
3421da177e4SLinus Torvalds 				icount = 0;
3431da177e4SLinus Torvalds 			}
3441da177e4SLinus Torvalds 			/*
3451da177e4SLinus Torvalds 			 * In any case, increment to the next record.
3461da177e4SLinus Torvalds 			 */
3471da177e4SLinus Torvalds 			if (!error)
348637aa50fSChristoph Hellwig 				error = xfs_btree_increment(cur, 0, &tmp);
3491da177e4SLinus Torvalds 		} else {
3501da177e4SLinus Torvalds 			/*
3511da177e4SLinus Torvalds 			 * Start of ag.  Lookup the first inode chunk.
3521da177e4SLinus Torvalds 			 */
35321875505SChristoph Hellwig 			error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &tmp);
3541da177e4SLinus Torvalds 			icount = 0;
3551da177e4SLinus Torvalds 		}
3561da177e4SLinus Torvalds 		/*
3571da177e4SLinus Torvalds 		 * Loop through inode btree records in this ag,
3581da177e4SLinus Torvalds 		 * until we run out of inodes or space in the buffer.
3591da177e4SLinus Torvalds 		 */
3601da177e4SLinus Torvalds 		while (irbp < irbufend && icount < ubcount) {
3612e287a73SChristoph Hellwig 			xfs_inobt_rec_incore_t r;
3622e287a73SChristoph Hellwig 
3631da177e4SLinus Torvalds 			/*
3641da177e4SLinus Torvalds 			 * Loop as long as we're unable to read the
3651da177e4SLinus Torvalds 			 * inode btree.
3661da177e4SLinus Torvalds 			 */
3671da177e4SLinus Torvalds 			while (error) {
3681da177e4SLinus Torvalds 				agino += XFS_INODES_PER_CHUNK;
3691da177e4SLinus Torvalds 				if (XFS_AGINO_TO_AGBNO(mp, agino) >=
37016259e7dSChristoph Hellwig 						be32_to_cpu(agi->agi_length))
3711da177e4SLinus Torvalds 					break;
37221875505SChristoph Hellwig 				error = xfs_inobt_lookup(cur, agino,
37321875505SChristoph Hellwig 							 XFS_LOOKUP_GE, &tmp);
374cd57e594SLachlan McIlroy 				cond_resched();
3751da177e4SLinus Torvalds 			}
3761da177e4SLinus Torvalds 			/*
3771da177e4SLinus Torvalds 			 * If ran off the end of the ag either with an error,
3781da177e4SLinus Torvalds 			 * or the normal way, set end and stop collecting.
3791da177e4SLinus Torvalds 			 */
3802e287a73SChristoph Hellwig 			if (error) {
3811da177e4SLinus Torvalds 				end_of_ag = 1;
3821da177e4SLinus Torvalds 				break;
3831da177e4SLinus Torvalds 			}
3842e287a73SChristoph Hellwig 
3852e287a73SChristoph Hellwig 			error = xfs_inobt_get_rec(cur, &r, &i);
3862e287a73SChristoph Hellwig 			if (error || i == 0) {
3872e287a73SChristoph Hellwig 				end_of_ag = 1;
3882e287a73SChristoph Hellwig 				break;
3892e287a73SChristoph Hellwig 			}
3902e287a73SChristoph Hellwig 
3911da177e4SLinus Torvalds 			/*
3921da177e4SLinus Torvalds 			 * If this chunk has any allocated inodes, save it.
39326275093SNathan Scott 			 * Also start read-ahead now for this chunk.
3941da177e4SLinus Torvalds 			 */
3952e287a73SChristoph Hellwig 			if (r.ir_freecount < XFS_INODES_PER_CHUNK) {
39626275093SNathan Scott 				/*
39726275093SNathan Scott 				 * Loop over all clusters in the next chunk.
39826275093SNathan Scott 				 * Do a readahead if there are any allocated
39926275093SNathan Scott 				 * inodes in that cluster.
40026275093SNathan Scott 				 */
4012e287a73SChristoph Hellwig 				agbno = XFS_AGINO_TO_AGBNO(mp, r.ir_startino);
4022e287a73SChristoph Hellwig 				for (chunkidx = 0;
40326275093SNathan Scott 				     chunkidx < XFS_INODES_PER_CHUNK;
40426275093SNathan Scott 				     chunkidx += nicluster,
40526275093SNathan Scott 				     agbno += nbcluster) {
4062e287a73SChristoph Hellwig 					if (xfs_inobt_maskn(chunkidx, nicluster)
4072e287a73SChristoph Hellwig 							& ~r.ir_free)
40826275093SNathan Scott 						xfs_btree_reada_bufs(mp, agno,
40926275093SNathan Scott 							agbno, nbcluster);
41026275093SNathan Scott 				}
4112e287a73SChristoph Hellwig 				irbp->ir_startino = r.ir_startino;
4122e287a73SChristoph Hellwig 				irbp->ir_freecount = r.ir_freecount;
4132e287a73SChristoph Hellwig 				irbp->ir_free = r.ir_free;
4141da177e4SLinus Torvalds 				irbp++;
4152e287a73SChristoph Hellwig 				icount += XFS_INODES_PER_CHUNK - r.ir_freecount;
4161da177e4SLinus Torvalds 			}
4171da177e4SLinus Torvalds 			/*
4181da177e4SLinus Torvalds 			 * Set agino to after this chunk and bump the cursor.
4191da177e4SLinus Torvalds 			 */
4202e287a73SChristoph Hellwig 			agino = r.ir_startino + XFS_INODES_PER_CHUNK;
421637aa50fSChristoph Hellwig 			error = xfs_btree_increment(cur, 0, &tmp);
422cd57e594SLachlan McIlroy 			cond_resched();
4231da177e4SLinus Torvalds 		}
4241da177e4SLinus Torvalds 		/*
4251da177e4SLinus Torvalds 		 * Drop the btree buffers and the agi buffer.
4261da177e4SLinus Torvalds 		 * We can't hold any of the locks these represent
4271da177e4SLinus Torvalds 		 * when calling iget.
4281da177e4SLinus Torvalds 		 */
4291da177e4SLinus Torvalds 		xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
4301da177e4SLinus Torvalds 		xfs_buf_relse(agbp);
4311da177e4SLinus Torvalds 		/*
4321da177e4SLinus Torvalds 		 * Now format all the good inodes into the user's buffer.
4331da177e4SLinus Torvalds 		 */
4341da177e4SLinus Torvalds 		irbufend = irbp;
4351da177e4SLinus Torvalds 		for (irbp = irbuf;
436cd57e594SLachlan McIlroy 		     irbp < irbufend && XFS_BULKSTAT_UBLEFT(ubleft); irbp++) {
4371da177e4SLinus Torvalds 			/*
4381da177e4SLinus Torvalds 			 * Now process this chunk of inodes.
4391da177e4SLinus Torvalds 			 */
44026275093SNathan Scott 			for (agino = irbp->ir_startino, chunkidx = clustidx = 0;
441cd57e594SLachlan McIlroy 			     XFS_BULKSTAT_UBLEFT(ubleft) &&
44226275093SNathan Scott 				irbp->ir_freecount < XFS_INODES_PER_CHUNK;
4431da177e4SLinus Torvalds 			     chunkidx++, clustidx++, agino++) {
4441da177e4SLinus Torvalds 				ASSERT(chunkidx < XFS_INODES_PER_CHUNK);
4451da177e4SLinus Torvalds 				/*
4461da177e4SLinus Torvalds 				 * Recompute agbno if this is the
4471da177e4SLinus Torvalds 				 * first inode of the cluster.
4481da177e4SLinus Torvalds 				 *
4491da177e4SLinus Torvalds 				 * Careful with clustidx.   There can be
4509da096fdSMalcolm Parsons 				 * multiple clusters per chunk, a single
4511da177e4SLinus Torvalds 				 * cluster per chunk or a cluster that has
4521da177e4SLinus Torvalds 				 * inodes represented from several different
4531da177e4SLinus Torvalds 				 * chunks (if blocksize is large).
4541da177e4SLinus Torvalds 				 *
4551da177e4SLinus Torvalds 				 * Because of this, the starting clustidx is
4561da177e4SLinus Torvalds 				 * initialized to zero in this loop but must
4571da177e4SLinus Torvalds 				 * later be reset after reading in the cluster
4581da177e4SLinus Torvalds 				 * buffer.
4591da177e4SLinus Torvalds 				 */
4601da177e4SLinus Torvalds 				if ((chunkidx & (nicluster - 1)) == 0) {
4611da177e4SLinus Torvalds 					agbno = XFS_AGINO_TO_AGBNO(mp,
46226275093SNathan Scott 							irbp->ir_startino) +
4631da177e4SLinus Torvalds 						((chunkidx & nimask) >>
4641da177e4SLinus Torvalds 						 mp->m_sb.sb_inopblog);
4651da177e4SLinus Torvalds 				}
466c2cba57eSLachlan McIlroy 				ino = XFS_AGINO_TO_INO(mp, agno, agino);
467c2cba57eSLachlan McIlroy 				bno = XFS_AGB_TO_DADDR(mp, agno, agbno);
4681da177e4SLinus Torvalds 				/*
4691da177e4SLinus Torvalds 				 * Skip if this inode is free.
4701da177e4SLinus Torvalds 				 */
471c2cba57eSLachlan McIlroy 				if (XFS_INOBT_MASK(chunkidx) & irbp->ir_free) {
472c2cba57eSLachlan McIlroy 					lastino = ino;
4731da177e4SLinus Torvalds 					continue;
474c2cba57eSLachlan McIlroy 				}
4751da177e4SLinus Torvalds 				/*
4761da177e4SLinus Torvalds 				 * Count used inodes as free so we can tell
4771da177e4SLinus Torvalds 				 * when the chunk is used up.
4781da177e4SLinus Torvalds 				 */
47926275093SNathan Scott 				irbp->ir_freecount++;
4801da177e4SLinus Torvalds 
4811da177e4SLinus Torvalds 				/*
4821da177e4SLinus Torvalds 				 * Get the inode and fill in a single buffer.
4831da177e4SLinus Torvalds 				 */
4841da177e4SLinus Torvalds 				ubused = statstruct_size;
485*7b6259e7SDave Chinner 				error = formatter(mp, ino, ubufp, ubleft,
4867dce11dbSChristoph Hellwig 						  &ubused, &fmterror);
4871da177e4SLinus Torvalds 				if (fmterror == BULKSTAT_RV_NOTHING) {
488cd57e594SLachlan McIlroy 					if (error && error != ENOENT &&
489cd57e594SLachlan McIlroy 						error != EINVAL) {
49022de606aSVlad Apostolov 						ubleft = 0;
49122de606aSVlad Apostolov 						rval = error;
49222de606aSVlad Apostolov 						break;
49322de606aSVlad Apostolov 					}
4946e73b418SVlad Apostolov 					lastino = ino;
4951da177e4SLinus Torvalds 					continue;
4961da177e4SLinus Torvalds 				}
4971da177e4SLinus Torvalds 				if (fmterror == BULKSTAT_RV_GIVEUP) {
4981da177e4SLinus Torvalds 					ubleft = 0;
4991da177e4SLinus Torvalds 					ASSERT(error);
5001da177e4SLinus Torvalds 					rval = error;
5011da177e4SLinus Torvalds 					break;
5021da177e4SLinus Torvalds 				}
5031da177e4SLinus Torvalds 				if (ubufp)
5041da177e4SLinus Torvalds 					ubufp += ubused;
5051da177e4SLinus Torvalds 				ubleft -= ubused;
5061da177e4SLinus Torvalds 				ubelem++;
5071da177e4SLinus Torvalds 				lastino = ino;
5081da177e4SLinus Torvalds 			}
509cd57e594SLachlan McIlroy 
510cd57e594SLachlan McIlroy 			cond_resched();
5111da177e4SLinus Torvalds 		}
5121da177e4SLinus Torvalds 
5131da177e4SLinus Torvalds 		if (bp)
5141da177e4SLinus Torvalds 			xfs_buf_relse(bp);
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds 		/*
5171da177e4SLinus Torvalds 		 * Set up for the next loop iteration.
5181da177e4SLinus Torvalds 		 */
519cd57e594SLachlan McIlroy 		if (XFS_BULKSTAT_UBLEFT(ubleft)) {
5201da177e4SLinus Torvalds 			if (end_of_ag) {
5211da177e4SLinus Torvalds 				agno++;
5221da177e4SLinus Torvalds 				agino = 0;
523cd57e594SLachlan McIlroy 			} else
524cd57e594SLachlan McIlroy 				agino = XFS_INO_TO_AGINO(mp, lastino);
5251da177e4SLinus Torvalds 		} else
5261da177e4SLinus Torvalds 			break;
5271da177e4SLinus Torvalds 	}
5281da177e4SLinus Torvalds 	/*
5291da177e4SLinus Torvalds 	 * Done, we're either out of filesystem or space to put the data.
5301da177e4SLinus Torvalds 	 */
531bdfb0430SChristoph Hellwig 	kmem_free_large(irbuf);
5321da177e4SLinus Torvalds 	*ubcountp = ubelem;
533cd57e594SLachlan McIlroy 	/*
534cd57e594SLachlan McIlroy 	 * Found some inodes, return them now and return the error next time.
535cd57e594SLachlan McIlroy 	 */
536cd57e594SLachlan McIlroy 	if (ubelem)
537cd57e594SLachlan McIlroy 		rval = 0;
5381da177e4SLinus Torvalds 	if (agno >= mp->m_sb.sb_agcount) {
5391da177e4SLinus Torvalds 		/*
5401da177e4SLinus Torvalds 		 * If we ran out of filesystem, mark lastino as off
5411da177e4SLinus Torvalds 		 * the end of the filesystem, so the next call
5421da177e4SLinus Torvalds 		 * will return immediately.
5431da177e4SLinus Torvalds 		 */
5441da177e4SLinus Torvalds 		*lastinop = (xfs_ino_t)XFS_AGINO_TO_INO(mp, agno, 0);
5451da177e4SLinus Torvalds 		*done = 1;
5461da177e4SLinus Torvalds 	} else
5471da177e4SLinus Torvalds 		*lastinop = (xfs_ino_t)lastino;
5481da177e4SLinus Torvalds 
5491da177e4SLinus Torvalds 	return rval;
5501da177e4SLinus Torvalds }
5511da177e4SLinus Torvalds 
5521da177e4SLinus Torvalds /*
5531da177e4SLinus Torvalds  * Return stat information in bulk (by-inode) for the filesystem.
5541da177e4SLinus Torvalds  * Special case for non-sequential one inode bulkstat.
5551da177e4SLinus Torvalds  */
5561da177e4SLinus Torvalds int					/* error status */
5571da177e4SLinus Torvalds xfs_bulkstat_single(
5581da177e4SLinus Torvalds 	xfs_mount_t		*mp,	/* mount point for filesystem */
5591da177e4SLinus Torvalds 	xfs_ino_t		*lastinop, /* inode to return */
5601da177e4SLinus Torvalds 	char			__user *buffer, /* buffer with inode stats */
561c41564b5SNathan Scott 	int			*done)	/* 1 if there are more stats to get */
5621da177e4SLinus Torvalds {
5631da177e4SLinus Torvalds 	int			count;	/* count value for bulkstat call */
5641da177e4SLinus Torvalds 	int			error;	/* return value */
5651da177e4SLinus Torvalds 	xfs_ino_t		ino;	/* filesystem inode number */
5661da177e4SLinus Torvalds 	int			res;	/* result from bs1 */
5671da177e4SLinus Torvalds 
5681da177e4SLinus Torvalds 	/*
5691da177e4SLinus Torvalds 	 * note that requesting valid inode numbers which are not allocated
5701da177e4SLinus Torvalds 	 * to inodes will most likely cause xfs_itobp to generate warning
5711da177e4SLinus Torvalds 	 * messages about bad magic numbers. This is ok. The fact that
5721da177e4SLinus Torvalds 	 * the inode isn't actually an inode is handled by the
5731da177e4SLinus Torvalds 	 * error check below. Done this way to make the usual case faster
5741da177e4SLinus Torvalds 	 * at the expense of the error case.
5751da177e4SLinus Torvalds 	 */
5761da177e4SLinus Torvalds 
5771da177e4SLinus Torvalds 	ino = (xfs_ino_t)*lastinop;
578*7b6259e7SDave Chinner 	error = xfs_bulkstat_one(mp, ino, buffer, sizeof(xfs_bstat_t), 0, &res);
5791da177e4SLinus Torvalds 	if (error) {
5801da177e4SLinus Torvalds 		/*
5811da177e4SLinus Torvalds 		 * Special case way failed, do it the "long" way
5821da177e4SLinus Torvalds 		 * to see if that works.
5831da177e4SLinus Torvalds 		 */
5841da177e4SLinus Torvalds 		(*lastinop)--;
5851da177e4SLinus Torvalds 		count = 1;
5861da177e4SLinus Torvalds 		if (xfs_bulkstat(mp, lastinop, &count, xfs_bulkstat_one,
5877dce11dbSChristoph Hellwig 				sizeof(xfs_bstat_t), buffer, done))
5881da177e4SLinus Torvalds 			return error;
5891da177e4SLinus Torvalds 		if (count == 0 || (xfs_ino_t)*lastinop != ino)
5901da177e4SLinus Torvalds 			return error == EFSCORRUPTED ?
5911da177e4SLinus Torvalds 				XFS_ERROR(EINVAL) : error;
5921da177e4SLinus Torvalds 		else
5931da177e4SLinus Torvalds 			return 0;
5941da177e4SLinus Torvalds 	}
5951da177e4SLinus Torvalds 	*done = 0;
5961da177e4SLinus Torvalds 	return 0;
5971da177e4SLinus Torvalds }
5981da177e4SLinus Torvalds 
599faa63e95SMichal Marek int
600faa63e95SMichal Marek xfs_inumbers_fmt(
601faa63e95SMichal Marek 	void			__user *ubuffer, /* buffer to write to */
602faa63e95SMichal Marek 	const xfs_inogrp_t	*buffer,	/* buffer to read from */
603faa63e95SMichal Marek 	long			count,		/* # of elements to read */
604faa63e95SMichal Marek 	long			*written)	/* # of bytes written */
605faa63e95SMichal Marek {
606faa63e95SMichal Marek 	if (copy_to_user(ubuffer, buffer, count * sizeof(*buffer)))
607faa63e95SMichal Marek 		return -EFAULT;
608faa63e95SMichal Marek 	*written = count * sizeof(*buffer);
609faa63e95SMichal Marek 	return 0;
610faa63e95SMichal Marek }
611faa63e95SMichal Marek 
6121da177e4SLinus Torvalds /*
6131da177e4SLinus Torvalds  * Return inode number table for the filesystem.
6141da177e4SLinus Torvalds  */
6151da177e4SLinus Torvalds int					/* error status */
6161da177e4SLinus Torvalds xfs_inumbers(
6171da177e4SLinus Torvalds 	xfs_mount_t	*mp,		/* mount point for filesystem */
6181da177e4SLinus Torvalds 	xfs_ino_t	*lastino,	/* last inode returned */
6191da177e4SLinus Torvalds 	int		*count,		/* size of buffer/count returned */
620faa63e95SMichal Marek 	void		__user *ubuffer,/* buffer with inode descriptions */
621faa63e95SMichal Marek 	inumbers_fmt_pf	formatter)
6221da177e4SLinus Torvalds {
6231da177e4SLinus Torvalds 	xfs_buf_t	*agbp;
6241da177e4SLinus Torvalds 	xfs_agino_t	agino;
6251da177e4SLinus Torvalds 	xfs_agnumber_t	agno;
6261da177e4SLinus Torvalds 	int		bcount;
6271da177e4SLinus Torvalds 	xfs_inogrp_t	*buffer;
6281da177e4SLinus Torvalds 	int		bufidx;
6291da177e4SLinus Torvalds 	xfs_btree_cur_t	*cur;
6301da177e4SLinus Torvalds 	int		error;
6312e287a73SChristoph Hellwig 	xfs_inobt_rec_incore_t r;
6321da177e4SLinus Torvalds 	int		i;
6331da177e4SLinus Torvalds 	xfs_ino_t	ino;
6341da177e4SLinus Torvalds 	int		left;
6351da177e4SLinus Torvalds 	int		tmp;
6361da177e4SLinus Torvalds 
6371da177e4SLinus Torvalds 	ino = (xfs_ino_t)*lastino;
6381da177e4SLinus Torvalds 	agno = XFS_INO_TO_AGNO(mp, ino);
6391da177e4SLinus Torvalds 	agino = XFS_INO_TO_AGINO(mp, ino);
6401da177e4SLinus Torvalds 	left = *count;
6411da177e4SLinus Torvalds 	*count = 0;
642e6a4b37fSTim Shimmin 	bcount = MIN(left, (int)(PAGE_SIZE / sizeof(*buffer)));
6431da177e4SLinus Torvalds 	buffer = kmem_alloc(bcount * sizeof(*buffer), KM_SLEEP);
6441da177e4SLinus Torvalds 	error = bufidx = 0;
6451da177e4SLinus Torvalds 	cur = NULL;
6461da177e4SLinus Torvalds 	agbp = NULL;
6471da177e4SLinus Torvalds 	while (left > 0 && agno < mp->m_sb.sb_agcount) {
6481da177e4SLinus Torvalds 		if (agbp == NULL) {
6491da177e4SLinus Torvalds 			error = xfs_ialloc_read_agi(mp, NULL, agno, &agbp);
6501da177e4SLinus Torvalds 			if (error) {
6511da177e4SLinus Torvalds 				/*
6521da177e4SLinus Torvalds 				 * If we can't read the AGI of this ag,
6531da177e4SLinus Torvalds 				 * then just skip to the next one.
6541da177e4SLinus Torvalds 				 */
6551da177e4SLinus Torvalds 				ASSERT(cur == NULL);
6561da177e4SLinus Torvalds 				agbp = NULL;
6571da177e4SLinus Torvalds 				agno++;
6581da177e4SLinus Torvalds 				agino = 0;
6591da177e4SLinus Torvalds 				continue;
6601da177e4SLinus Torvalds 			}
661561f7d17SChristoph Hellwig 			cur = xfs_inobt_init_cursor(mp, NULL, agbp, agno);
66221875505SChristoph Hellwig 			error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_GE,
66321875505SChristoph Hellwig 						 &tmp);
6641da177e4SLinus Torvalds 			if (error) {
6651da177e4SLinus Torvalds 				xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
6661da177e4SLinus Torvalds 				cur = NULL;
6671da177e4SLinus Torvalds 				xfs_buf_relse(agbp);
6681da177e4SLinus Torvalds 				agbp = NULL;
6691da177e4SLinus Torvalds 				/*
67059c51591SMichael Opdenacker 				 * Move up the last inode in the current
6711da177e4SLinus Torvalds 				 * chunk.  The lookup_ge will always get
6721da177e4SLinus Torvalds 				 * us the first inode in the next chunk.
6731da177e4SLinus Torvalds 				 */
6741da177e4SLinus Torvalds 				agino += XFS_INODES_PER_CHUNK - 1;
6751da177e4SLinus Torvalds 				continue;
6761da177e4SLinus Torvalds 			}
6771da177e4SLinus Torvalds 		}
6782e287a73SChristoph Hellwig 		error = xfs_inobt_get_rec(cur, &r, &i);
6792e287a73SChristoph Hellwig 		if (error || i == 0) {
6801da177e4SLinus Torvalds 			xfs_buf_relse(agbp);
6811da177e4SLinus Torvalds 			agbp = NULL;
6821da177e4SLinus Torvalds 			xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
6831da177e4SLinus Torvalds 			cur = NULL;
6841da177e4SLinus Torvalds 			agno++;
6851da177e4SLinus Torvalds 			agino = 0;
6861da177e4SLinus Torvalds 			continue;
6871da177e4SLinus Torvalds 		}
6882e287a73SChristoph Hellwig 		agino = r.ir_startino + XFS_INODES_PER_CHUNK - 1;
6892e287a73SChristoph Hellwig 		buffer[bufidx].xi_startino =
6902e287a73SChristoph Hellwig 			XFS_AGINO_TO_INO(mp, agno, r.ir_startino);
6912e287a73SChristoph Hellwig 		buffer[bufidx].xi_alloccount =
6922e287a73SChristoph Hellwig 			XFS_INODES_PER_CHUNK - r.ir_freecount;
6932e287a73SChristoph Hellwig 		buffer[bufidx].xi_allocmask = ~r.ir_free;
6941da177e4SLinus Torvalds 		bufidx++;
6951da177e4SLinus Torvalds 		left--;
6961da177e4SLinus Torvalds 		if (bufidx == bcount) {
697faa63e95SMichal Marek 			long written;
698faa63e95SMichal Marek 			if (formatter(ubuffer, buffer, bufidx, &written)) {
6991da177e4SLinus Torvalds 				error = XFS_ERROR(EFAULT);
7001da177e4SLinus Torvalds 				break;
7011da177e4SLinus Torvalds 			}
702faa63e95SMichal Marek 			ubuffer += written;
7031da177e4SLinus Torvalds 			*count += bufidx;
7041da177e4SLinus Torvalds 			bufidx = 0;
7051da177e4SLinus Torvalds 		}
7061da177e4SLinus Torvalds 		if (left) {
707637aa50fSChristoph Hellwig 			error = xfs_btree_increment(cur, 0, &tmp);
7081da177e4SLinus Torvalds 			if (error) {
7091da177e4SLinus Torvalds 				xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
7101da177e4SLinus Torvalds 				cur = NULL;
7111da177e4SLinus Torvalds 				xfs_buf_relse(agbp);
7121da177e4SLinus Torvalds 				agbp = NULL;
7131da177e4SLinus Torvalds 				/*
7141da177e4SLinus Torvalds 				 * The agino value has already been bumped.
7151da177e4SLinus Torvalds 				 * Just try to skip up to it.
7161da177e4SLinus Torvalds 				 */
7171da177e4SLinus Torvalds 				agino += XFS_INODES_PER_CHUNK;
7181da177e4SLinus Torvalds 				continue;
7191da177e4SLinus Torvalds 			}
7201da177e4SLinus Torvalds 		}
7211da177e4SLinus Torvalds 	}
7221da177e4SLinus Torvalds 	if (!error) {
7231da177e4SLinus Torvalds 		if (bufidx) {
724faa63e95SMichal Marek 			long written;
725faa63e95SMichal Marek 			if (formatter(ubuffer, buffer, bufidx, &written))
7261da177e4SLinus Torvalds 				error = XFS_ERROR(EFAULT);
7271da177e4SLinus Torvalds 			else
7281da177e4SLinus Torvalds 				*count += bufidx;
7291da177e4SLinus Torvalds 		}
7301da177e4SLinus Torvalds 		*lastino = XFS_AGINO_TO_INO(mp, agno, agino);
7311da177e4SLinus Torvalds 	}
732f0e2d93cSDenys Vlasenko 	kmem_free(buffer);
7331da177e4SLinus Torvalds 	if (cur)
7341da177e4SLinus Torvalds 		xfs_btree_del_cursor(cur, (error ? XFS_BTREE_ERROR :
7351da177e4SLinus Torvalds 					   XFS_BTREE_NOERROR));
7361da177e4SLinus Torvalds 	if (agbp)
7371da177e4SLinus Torvalds 		xfs_buf_relse(agbp);
7381da177e4SLinus Torvalds 	return error;
7391da177e4SLinus Torvalds }
740