xref: /openbmc/linux/fs/xfs/xfs_itable.c (revision c7cb51dcb0a38624d42eeabb38502fa54a4d774b)
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"
2070a9883cSDave Chinner #include "xfs_shared.h"
21a4fbe6abSDave Chinner #include "xfs_format.h"
22239880efSDave Chinner #include "xfs_log_format.h"
23239880efSDave Chinner #include "xfs_trans_resv.h"
24a844f451SNathan Scott #include "xfs_inum.h"
251da177e4SLinus Torvalds #include "xfs_sb.h"
26a844f451SNathan Scott #include "xfs_ag.h"
271da177e4SLinus Torvalds #include "xfs_mount.h"
281da177e4SLinus Torvalds #include "xfs_inode.h"
29a4fbe6abSDave Chinner #include "xfs_btree.h"
301da177e4SLinus Torvalds #include "xfs_ialloc.h"
31a4fbe6abSDave Chinner #include "xfs_ialloc_btree.h"
321da177e4SLinus Torvalds #include "xfs_itable.h"
331da177e4SLinus Torvalds #include "xfs_error.h"
34f2d67614SChristoph Hellwig #include "xfs_trace.h"
3533479e05SDave Chinner #include "xfs_icache.h"
36a4fbe6abSDave Chinner #include "xfs_dinode.h"
371da177e4SLinus Torvalds 
38d96f8f89SEric Sandeen STATIC int
396f1f2168SVlad Apostolov xfs_internal_inum(
406f1f2168SVlad Apostolov 	xfs_mount_t	*mp,
416f1f2168SVlad Apostolov 	xfs_ino_t	ino)
426f1f2168SVlad Apostolov {
436f1f2168SVlad Apostolov 	return (ino == mp->m_sb.sb_rbmino || ino == mp->m_sb.sb_rsumino ||
4462118709SEric Sandeen 		(xfs_sb_version_hasquota(&mp->m_sb) &&
459cad19d2SChandra Seetharaman 		 xfs_is_quota_inode(&mp->m_sb, ino)));
466f1f2168SVlad Apostolov }
476f1f2168SVlad Apostolov 
487dce11dbSChristoph Hellwig /*
497dce11dbSChristoph Hellwig  * Return stat information for one inode.
507dce11dbSChristoph Hellwig  * Return 0 if ok, else errno.
517dce11dbSChristoph Hellwig  */
527dce11dbSChristoph Hellwig int
537dce11dbSChristoph Hellwig xfs_bulkstat_one_int(
547dce11dbSChristoph Hellwig 	struct xfs_mount	*mp,		/* mount point for filesystem */
557dce11dbSChristoph Hellwig 	xfs_ino_t		ino,		/* inode to get data for */
567dce11dbSChristoph Hellwig 	void __user		*buffer,	/* buffer to place output in */
577dce11dbSChristoph Hellwig 	int			ubsize,		/* size of buffer */
587dce11dbSChristoph Hellwig 	bulkstat_one_fmt_pf	formatter,	/* formatter, copy to user */
597dce11dbSChristoph Hellwig 	int			*ubused,	/* bytes used by me */
601da177e4SLinus Torvalds 	int			*stat)		/* BULKSTAT_RV_... */
611da177e4SLinus Torvalds {
627dce11dbSChristoph Hellwig 	struct xfs_icdinode	*dic;		/* dinode core info pointer */
637dce11dbSChristoph Hellwig 	struct xfs_inode	*ip;		/* incore inode pointer */
647dce11dbSChristoph Hellwig 	struct xfs_bstat	*buf;		/* return buffer */
657dce11dbSChristoph Hellwig 	int			error = 0;	/* error value */
667dce11dbSChristoph Hellwig 
677dce11dbSChristoph Hellwig 	*stat = BULKSTAT_RV_NOTHING;
687dce11dbSChristoph Hellwig 
697dce11dbSChristoph Hellwig 	if (!buffer || xfs_internal_inum(mp, ino))
702451337dSDave Chinner 		return -EINVAL;
717dce11dbSChristoph Hellwig 
727dce11dbSChristoph Hellwig 	buf = kmem_alloc(sizeof(*buf), KM_SLEEP | KM_MAYFAIL);
737dce11dbSChristoph Hellwig 	if (!buf)
742451337dSDave Chinner 		return -ENOMEM;
751da177e4SLinus Torvalds 
76745b1f47SNathan Scott 	error = xfs_iget(mp, NULL, ino,
775132ba8fSDave Chinner 			 (XFS_IGET_DONTCACHE | XFS_IGET_UNTRUSTED),
785132ba8fSDave Chinner 			 XFS_ILOCK_SHARED, &ip);
798fe65776SJie Liu 	if (error)
807dce11dbSChristoph Hellwig 		goto out_free;
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds 	ASSERT(ip != NULL);
8392bfc6e7SChristoph Hellwig 	ASSERT(ip->i_imap.im_blkno != 0);
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds 	dic = &ip->i_d;
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds 	/* xfs_iget returns the following without needing
881da177e4SLinus Torvalds 	 * further change.
891da177e4SLinus Torvalds 	 */
901da177e4SLinus Torvalds 	buf->bs_nlink = dic->di_nlink;
916743099cSArkadiusz Mi?kiewicz 	buf->bs_projid_lo = dic->di_projid_lo;
926743099cSArkadiusz Mi?kiewicz 	buf->bs_projid_hi = dic->di_projid_hi;
931da177e4SLinus Torvalds 	buf->bs_ino = ino;
941da177e4SLinus Torvalds 	buf->bs_mode = dic->di_mode;
951da177e4SLinus Torvalds 	buf->bs_uid = dic->di_uid;
961da177e4SLinus Torvalds 	buf->bs_gid = dic->di_gid;
971da177e4SLinus Torvalds 	buf->bs_size = dic->di_size;
988a9c9980SChristoph Hellwig 	buf->bs_atime.tv_sec = dic->di_atime.t_sec;
998a9c9980SChristoph Hellwig 	buf->bs_atime.tv_nsec = dic->di_atime.t_nsec;
1008a9c9980SChristoph Hellwig 	buf->bs_mtime.tv_sec = dic->di_mtime.t_sec;
1018a9c9980SChristoph Hellwig 	buf->bs_mtime.tv_nsec = dic->di_mtime.t_nsec;
1028a9c9980SChristoph Hellwig 	buf->bs_ctime.tv_sec = dic->di_ctime.t_sec;
1038a9c9980SChristoph Hellwig 	buf->bs_ctime.tv_nsec = dic->di_ctime.t_nsec;
1041da177e4SLinus Torvalds 	buf->bs_xflags = xfs_ip2xflags(ip);
1051da177e4SLinus Torvalds 	buf->bs_extsize = dic->di_extsize << mp->m_sb.sb_blocklog;
1061da177e4SLinus Torvalds 	buf->bs_extents = dic->di_nextents;
1071da177e4SLinus Torvalds 	buf->bs_gen = dic->di_gen;
1081da177e4SLinus Torvalds 	memset(buf->bs_pad, 0, sizeof(buf->bs_pad));
1091da177e4SLinus Torvalds 	buf->bs_dmevmask = dic->di_dmevmask;
1101da177e4SLinus Torvalds 	buf->bs_dmstate = dic->di_dmstate;
1111da177e4SLinus Torvalds 	buf->bs_aextents = dic->di_anextents;
11207000ee6SDave Chinner 	buf->bs_forkoff = XFS_IFORK_BOFF(ip);
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds 	switch (dic->di_format) {
1151da177e4SLinus Torvalds 	case XFS_DINODE_FMT_DEV:
1161da177e4SLinus Torvalds 		buf->bs_rdev = ip->i_df.if_u2.if_rdev;
1171da177e4SLinus Torvalds 		buf->bs_blksize = BLKDEV_IOSIZE;
1181da177e4SLinus Torvalds 		buf->bs_blocks = 0;
1191da177e4SLinus Torvalds 		break;
1201da177e4SLinus Torvalds 	case XFS_DINODE_FMT_LOCAL:
1211da177e4SLinus Torvalds 	case XFS_DINODE_FMT_UUID:
1221da177e4SLinus Torvalds 		buf->bs_rdev = 0;
1231da177e4SLinus Torvalds 		buf->bs_blksize = mp->m_sb.sb_blocksize;
1241da177e4SLinus Torvalds 		buf->bs_blocks = 0;
1251da177e4SLinus Torvalds 		break;
1261da177e4SLinus Torvalds 	case XFS_DINODE_FMT_EXTENTS:
1271da177e4SLinus Torvalds 	case XFS_DINODE_FMT_BTREE:
1281da177e4SLinus Torvalds 		buf->bs_rdev = 0;
1291da177e4SLinus Torvalds 		buf->bs_blksize = mp->m_sb.sb_blocksize;
1301da177e4SLinus Torvalds 		buf->bs_blocks = dic->di_nblocks + ip->i_delayed_blks;
1311da177e4SLinus Torvalds 		break;
1321da177e4SLinus Torvalds 	}
133f2d67614SChristoph Hellwig 	xfs_iunlock(ip, XFS_ILOCK_SHARED);
134f2d67614SChristoph Hellwig 	IRELE(ip);
1357dce11dbSChristoph Hellwig 
1367dce11dbSChristoph Hellwig 	error = formatter(buffer, ubsize, ubused, buf);
1377dce11dbSChristoph Hellwig 	if (!error)
1387dce11dbSChristoph Hellwig 		*stat = BULKSTAT_RV_DIDONE;
1397dce11dbSChristoph Hellwig 
1407dce11dbSChristoph Hellwig  out_free:
1417dce11dbSChristoph Hellwig 	kmem_free(buf);
1421da177e4SLinus Torvalds 	return error;
1431da177e4SLinus Torvalds }
1441da177e4SLinus Torvalds 
14565fbaf24Ssandeen@sandeen.net /* Return 0 on success or positive error */
146faa63e95SMichal Marek STATIC int
147faa63e95SMichal Marek xfs_bulkstat_one_fmt(
148faa63e95SMichal Marek 	void			__user *ubuffer,
14965fbaf24Ssandeen@sandeen.net 	int			ubsize,
15065fbaf24Ssandeen@sandeen.net 	int			*ubused,
151faa63e95SMichal Marek 	const xfs_bstat_t	*buffer)
152faa63e95SMichal Marek {
15365fbaf24Ssandeen@sandeen.net 	if (ubsize < sizeof(*buffer))
1542451337dSDave Chinner 		return -ENOMEM;
155faa63e95SMichal Marek 	if (copy_to_user(ubuffer, buffer, sizeof(*buffer)))
1562451337dSDave Chinner 		return -EFAULT;
15765fbaf24Ssandeen@sandeen.net 	if (ubused)
15865fbaf24Ssandeen@sandeen.net 		*ubused = sizeof(*buffer);
15965fbaf24Ssandeen@sandeen.net 	return 0;
160faa63e95SMichal Marek }
161faa63e95SMichal Marek 
1622ee4fa5cSsandeen@sandeen.net int
1632ee4fa5cSsandeen@sandeen.net xfs_bulkstat_one(
1642ee4fa5cSsandeen@sandeen.net 	xfs_mount_t	*mp,		/* mount point for filesystem */
1652ee4fa5cSsandeen@sandeen.net 	xfs_ino_t	ino,		/* inode number to get data for */
1662ee4fa5cSsandeen@sandeen.net 	void		__user *buffer,	/* buffer to place output in */
1672ee4fa5cSsandeen@sandeen.net 	int		ubsize,		/* size of buffer */
1682ee4fa5cSsandeen@sandeen.net 	int		*ubused,	/* bytes used by me */
1692ee4fa5cSsandeen@sandeen.net 	int		*stat)		/* BULKSTAT_RV_... */
1702ee4fa5cSsandeen@sandeen.net {
1712ee4fa5cSsandeen@sandeen.net 	return xfs_bulkstat_one_int(mp, ino, buffer, ubsize,
1727b6259e7SDave Chinner 				    xfs_bulkstat_one_fmt, ubused, stat);
1738b56f083SNathan Scott }
1748b56f083SNathan Scott 
175cd57e594SLachlan McIlroy #define XFS_BULKSTAT_UBLEFT(ubleft)	((ubleft) >= statstruct_size)
176cd57e594SLachlan McIlroy 
1778b56f083SNathan Scott /*
1781da177e4SLinus Torvalds  * Return stat information in bulk (by-inode) for the filesystem.
1791da177e4SLinus Torvalds  */
1801da177e4SLinus Torvalds int					/* error status */
1811da177e4SLinus Torvalds xfs_bulkstat(
1821da177e4SLinus Torvalds 	xfs_mount_t		*mp,	/* mount point for filesystem */
1831da177e4SLinus Torvalds 	xfs_ino_t		*lastinop, /* last inode returned */
1841da177e4SLinus Torvalds 	int			*ubcountp, /* size of buffer/count returned */
1851da177e4SLinus Torvalds 	bulkstat_one_pf		formatter, /* func that'd fill a single buf */
1861da177e4SLinus Torvalds 	size_t			statstruct_size, /* sizeof struct filling */
1871da177e4SLinus Torvalds 	char			__user *ubuffer, /* buffer with inode stats */
188c41564b5SNathan Scott 	int			*done)	/* 1 if there are more stats to get */
1891da177e4SLinus Torvalds {
1901da177e4SLinus Torvalds 	xfs_agblock_t		agbno=0;/* allocation group block number */
1911da177e4SLinus Torvalds 	xfs_buf_t		*agbp;	/* agi header buffer */
1921da177e4SLinus Torvalds 	xfs_agi_t		*agi;	/* agi header data */
1931da177e4SLinus Torvalds 	xfs_agino_t		agino;	/* inode # in allocation group */
1941da177e4SLinus Torvalds 	xfs_agnumber_t		agno;	/* allocation group number */
1951da177e4SLinus Torvalds 	int			chunkidx; /* current index into inode chunk */
1961da177e4SLinus Torvalds 	int			clustidx; /* current index into inode cluster */
1971da177e4SLinus Torvalds 	xfs_btree_cur_t		*cur;	/* btree cursor for ialloc btree */
1981da177e4SLinus Torvalds 	int			end_of_ag; /* set if we've seen the ag end */
1991da177e4SLinus Torvalds 	int			error;	/* error code */
2001da177e4SLinus Torvalds 	int                     fmterror;/* bulkstat formatter result */
2011da177e4SLinus Torvalds 	int			i;	/* loop index */
2021da177e4SLinus Torvalds 	int			icount;	/* count of inodes good in irbuf */
203215101c3SNathan Scott 	size_t			irbsize; /* size of irec buffer in bytes */
2041da177e4SLinus Torvalds 	xfs_ino_t		ino;	/* inode number (filesystem) */
20526275093SNathan Scott 	xfs_inobt_rec_incore_t	*irbp;	/* current irec buffer pointer */
20626275093SNathan Scott 	xfs_inobt_rec_incore_t	*irbuf;	/* start of irec buffer */
20726275093SNathan Scott 	xfs_inobt_rec_incore_t	*irbufend; /* end of good irec buffer entries */
208cd57e594SLachlan McIlroy 	xfs_ino_t		lastino; /* last inode number returned */
209a2ba07b2SJie Liu 	int			blks_per_cluster; /* # of blocks per cluster */
210a2ba07b2SJie Liu 	int			inodes_per_cluster;/* # of inodes per cluster */
2111da177e4SLinus Torvalds 	int			nirbuf;	/* size of irbuf */
2121da177e4SLinus Torvalds 	int			rval;	/* return value error code */
2131da177e4SLinus Torvalds 	int			tmp;	/* result value from btree calls */
2141da177e4SLinus Torvalds 	int			ubcount; /* size of user's buffer */
2151da177e4SLinus Torvalds 	int			ubleft;	/* bytes left in user's buffer */
2161da177e4SLinus Torvalds 	char			__user *ubufp;	/* pointer into user's buffer */
2171da177e4SLinus Torvalds 	int			ubelem;	/* spaces used in user's buffer */
2181da177e4SLinus Torvalds 	int			ubused;	/* bytes used by formatter */
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds 	/*
2211da177e4SLinus Torvalds 	 * Get the last inode value, see if there's nothing to do.
2221da177e4SLinus Torvalds 	 */
2231da177e4SLinus Torvalds 	ino = (xfs_ino_t)*lastinop;
224cd57e594SLachlan McIlroy 	lastino = ino;
2251da177e4SLinus Torvalds 	agno = XFS_INO_TO_AGNO(mp, ino);
2261da177e4SLinus Torvalds 	agino = XFS_INO_TO_AGINO(mp, ino);
2271da177e4SLinus Torvalds 	if (agno >= mp->m_sb.sb_agcount ||
2281da177e4SLinus Torvalds 	    ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
2291da177e4SLinus Torvalds 		*done = 1;
2301da177e4SLinus Torvalds 		*ubcountp = 0;
2311da177e4SLinus Torvalds 		return 0;
2321da177e4SLinus Torvalds 	}
233cd57e594SLachlan McIlroy 	if (!ubcountp || *ubcountp <= 0) {
2342451337dSDave Chinner 		return -EINVAL;
235cd57e594SLachlan McIlroy 	}
2361da177e4SLinus Torvalds 	ubcount = *ubcountp; /* statstruct's */
2371da177e4SLinus Torvalds 	ubleft = ubcount * statstruct_size; /* bytes */
2381da177e4SLinus Torvalds 	*ubcountp = ubelem = 0;
2391da177e4SLinus Torvalds 	*done = 0;
2401da177e4SLinus Torvalds 	fmterror = 0;
2411da177e4SLinus Torvalds 	ubufp = ubuffer;
242a2ba07b2SJie Liu 	blks_per_cluster = xfs_icluster_size_fsb(mp);
243a2ba07b2SJie Liu 	inodes_per_cluster = blks_per_cluster << mp->m_sb.sb_inopblog;
244bdfb0430SChristoph Hellwig 	irbuf = kmem_zalloc_greedy(&irbsize, PAGE_SIZE, PAGE_SIZE * 4);
245bdfb0430SChristoph Hellwig 	if (!irbuf)
2462451337dSDave Chinner 		return -ENOMEM;
247bdfb0430SChristoph Hellwig 
248bb3c7d29SNathan Scott 	nirbuf = irbsize / sizeof(*irbuf);
249bb3c7d29SNathan Scott 
2501da177e4SLinus Torvalds 	/*
2511da177e4SLinus Torvalds 	 * Loop over the allocation groups, starting from the last
2521da177e4SLinus Torvalds 	 * inode returned; 0 means start of the allocation group.
2531da177e4SLinus Torvalds 	 */
2541da177e4SLinus Torvalds 	rval = 0;
255cd57e594SLachlan McIlroy 	while (XFS_BULKSTAT_UBLEFT(ubleft) && agno < mp->m_sb.sb_agcount) {
256cd57e594SLachlan McIlroy 		cond_resched();
2571da177e4SLinus Torvalds 		error = xfs_ialloc_read_agi(mp, NULL, agno, &agbp);
2581da177e4SLinus Torvalds 		if (error) {
2591da177e4SLinus Torvalds 			/*
2601da177e4SLinus Torvalds 			 * Skip this allocation group and go to the next one.
2611da177e4SLinus Torvalds 			 */
2621da177e4SLinus Torvalds 			agno++;
2631da177e4SLinus Torvalds 			agino = 0;
2641da177e4SLinus Torvalds 			continue;
2651da177e4SLinus Torvalds 		}
2661da177e4SLinus Torvalds 		agi = XFS_BUF_TO_AGI(agbp);
2671da177e4SLinus Torvalds 		/*
2681da177e4SLinus Torvalds 		 * Allocate and initialize a btree cursor for ialloc btree.
2691da177e4SLinus Torvalds 		 */
27057bd3dbeSBrian Foster 		cur = xfs_inobt_init_cursor(mp, NULL, agbp, agno,
27157bd3dbeSBrian Foster 					    XFS_BTNUM_INO);
2721da177e4SLinus Torvalds 		irbp = irbuf;
2731da177e4SLinus Torvalds 		irbufend = irbuf + nirbuf;
2741da177e4SLinus Torvalds 		end_of_ag = 0;
2751da177e4SLinus Torvalds 		/*
2761da177e4SLinus Torvalds 		 * If we're returning in the middle of an allocation group,
2771da177e4SLinus Torvalds 		 * we need to get the remainder of the chunk we're in.
2781da177e4SLinus Torvalds 		 */
2791da177e4SLinus Torvalds 		if (agino > 0) {
2802e287a73SChristoph Hellwig 			xfs_inobt_rec_incore_t r;
2812e287a73SChristoph Hellwig 
2821da177e4SLinus Torvalds 			/*
2831da177e4SLinus Torvalds 			 * Lookup the inode chunk that this inode lives in.
2841da177e4SLinus Torvalds 			 */
28521875505SChristoph Hellwig 			error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE,
28621875505SChristoph Hellwig 						 &tmp);
2871da177e4SLinus Torvalds 			if (!error &&	/* no I/O error */
2881da177e4SLinus Torvalds 			    tmp &&	/* lookup succeeded */
2891da177e4SLinus Torvalds 					/* got the record, should always work */
2902e287a73SChristoph Hellwig 			    !(error = xfs_inobt_get_rec(cur, &r, &i)) &&
2911da177e4SLinus Torvalds 			    i == 1 &&
2921da177e4SLinus Torvalds 					/* this is the right chunk */
2932e287a73SChristoph Hellwig 			    agino < r.ir_startino + XFS_INODES_PER_CHUNK &&
2941da177e4SLinus Torvalds 					/* lastino was not last in chunk */
2952e287a73SChristoph Hellwig 			    (chunkidx = agino - r.ir_startino + 1) <
2961da177e4SLinus Torvalds 				    XFS_INODES_PER_CHUNK &&
2971da177e4SLinus Torvalds 					/* there are some left allocated */
2989d87c319SEric Sandeen 			    xfs_inobt_maskn(chunkidx,
2992e287a73SChristoph Hellwig 				    XFS_INODES_PER_CHUNK - chunkidx) &
3002e287a73SChristoph Hellwig 				    ~r.ir_free) {
3011da177e4SLinus Torvalds 				/*
3021da177e4SLinus Torvalds 				 * Grab the chunk record.  Mark all the
3031da177e4SLinus Torvalds 				 * uninteresting inodes (because they're
3041da177e4SLinus Torvalds 				 * before our start point) free.
3051da177e4SLinus Torvalds 				 */
3061da177e4SLinus Torvalds 				for (i = 0; i < chunkidx; i++) {
3072e287a73SChristoph Hellwig 					if (XFS_INOBT_MASK(i) & ~r.ir_free)
3082e287a73SChristoph Hellwig 						r.ir_freecount++;
3091da177e4SLinus Torvalds 				}
3102e287a73SChristoph Hellwig 				r.ir_free |= xfs_inobt_maskn(0, chunkidx);
3112e287a73SChristoph Hellwig 				irbp->ir_startino = r.ir_startino;
3122e287a73SChristoph Hellwig 				irbp->ir_freecount = r.ir_freecount;
3132e287a73SChristoph Hellwig 				irbp->ir_free = r.ir_free;
3141da177e4SLinus Torvalds 				irbp++;
3152e287a73SChristoph Hellwig 				agino = r.ir_startino + XFS_INODES_PER_CHUNK;
3162e287a73SChristoph Hellwig 				icount = XFS_INODES_PER_CHUNK - r.ir_freecount;
3171da177e4SLinus Torvalds 			} else {
3181da177e4SLinus Torvalds 				/*
3191da177e4SLinus Torvalds 				 * If any of those tests failed, bump the
3201da177e4SLinus Torvalds 				 * inode number (just in case).
3211da177e4SLinus Torvalds 				 */
3221da177e4SLinus Torvalds 				agino++;
3231da177e4SLinus Torvalds 				icount = 0;
3241da177e4SLinus Torvalds 			}
3251da177e4SLinus Torvalds 			/*
3261da177e4SLinus Torvalds 			 * In any case, increment to the next record.
3271da177e4SLinus Torvalds 			 */
3281da177e4SLinus Torvalds 			if (!error)
329637aa50fSChristoph Hellwig 				error = xfs_btree_increment(cur, 0, &tmp);
3301da177e4SLinus Torvalds 		} else {
3311da177e4SLinus Torvalds 			/*
3321da177e4SLinus Torvalds 			 * Start of ag.  Lookup the first inode chunk.
3331da177e4SLinus Torvalds 			 */
33421875505SChristoph Hellwig 			error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &tmp);
3351da177e4SLinus Torvalds 			icount = 0;
3361da177e4SLinus Torvalds 		}
3371da177e4SLinus Torvalds 		/*
3381da177e4SLinus Torvalds 		 * Loop through inode btree records in this ag,
3391da177e4SLinus Torvalds 		 * until we run out of inodes or space in the buffer.
3401da177e4SLinus Torvalds 		 */
3411da177e4SLinus Torvalds 		while (irbp < irbufend && icount < ubcount) {
3422e287a73SChristoph Hellwig 			xfs_inobt_rec_incore_t r;
3432e287a73SChristoph Hellwig 
3441da177e4SLinus Torvalds 			/*
3451da177e4SLinus Torvalds 			 * Loop as long as we're unable to read the
3461da177e4SLinus Torvalds 			 * inode btree.
3471da177e4SLinus Torvalds 			 */
3481da177e4SLinus Torvalds 			while (error) {
3491da177e4SLinus Torvalds 				agino += XFS_INODES_PER_CHUNK;
3501da177e4SLinus Torvalds 				if (XFS_AGINO_TO_AGBNO(mp, agino) >=
35116259e7dSChristoph Hellwig 						be32_to_cpu(agi->agi_length))
3521da177e4SLinus Torvalds 					break;
35321875505SChristoph Hellwig 				error = xfs_inobt_lookup(cur, agino,
35421875505SChristoph Hellwig 							 XFS_LOOKUP_GE, &tmp);
355cd57e594SLachlan McIlroy 				cond_resched();
3561da177e4SLinus Torvalds 			}
3571da177e4SLinus Torvalds 			/*
3581da177e4SLinus Torvalds 			 * If ran off the end of the ag either with an error,
3591da177e4SLinus Torvalds 			 * or the normal way, set end and stop collecting.
3601da177e4SLinus Torvalds 			 */
3612e287a73SChristoph Hellwig 			if (error) {
3621da177e4SLinus Torvalds 				end_of_ag = 1;
3631da177e4SLinus Torvalds 				break;
3641da177e4SLinus Torvalds 			}
3652e287a73SChristoph Hellwig 
3662e287a73SChristoph Hellwig 			error = xfs_inobt_get_rec(cur, &r, &i);
3672e287a73SChristoph Hellwig 			if (error || i == 0) {
3682e287a73SChristoph Hellwig 				end_of_ag = 1;
3692e287a73SChristoph Hellwig 				break;
3702e287a73SChristoph Hellwig 			}
3712e287a73SChristoph Hellwig 
3721da177e4SLinus Torvalds 			/*
3731da177e4SLinus Torvalds 			 * If this chunk has any allocated inodes, save it.
37426275093SNathan Scott 			 * Also start read-ahead now for this chunk.
3751da177e4SLinus Torvalds 			 */
3762e287a73SChristoph Hellwig 			if (r.ir_freecount < XFS_INODES_PER_CHUNK) {
377cbb2864aSDave Chinner 				struct blk_plug	plug;
37826275093SNathan Scott 				/*
37926275093SNathan Scott 				 * Loop over all clusters in the next chunk.
38026275093SNathan Scott 				 * Do a readahead if there are any allocated
38126275093SNathan Scott 				 * inodes in that cluster.
38226275093SNathan Scott 				 */
383cbb2864aSDave Chinner 				blk_start_plug(&plug);
3842e287a73SChristoph Hellwig 				agbno = XFS_AGINO_TO_AGBNO(mp, r.ir_startino);
3852e287a73SChristoph Hellwig 				for (chunkidx = 0;
38626275093SNathan Scott 				     chunkidx < XFS_INODES_PER_CHUNK;
387a2ba07b2SJie Liu 				     chunkidx += inodes_per_cluster,
388a2ba07b2SJie Liu 				     agbno += blks_per_cluster) {
389a2ba07b2SJie Liu 					if (xfs_inobt_maskn(chunkidx,
390a2ba07b2SJie Liu 					    inodes_per_cluster) & ~r.ir_free)
39126275093SNathan Scott 						xfs_btree_reada_bufs(mp, agno,
392a2ba07b2SJie Liu 							agbno, blks_per_cluster,
3931813dd64SDave Chinner 							&xfs_inode_buf_ops);
39426275093SNathan Scott 				}
395cbb2864aSDave Chinner 				blk_finish_plug(&plug);
3962e287a73SChristoph Hellwig 				irbp->ir_startino = r.ir_startino;
3972e287a73SChristoph Hellwig 				irbp->ir_freecount = r.ir_freecount;
3982e287a73SChristoph Hellwig 				irbp->ir_free = r.ir_free;
3991da177e4SLinus Torvalds 				irbp++;
4002e287a73SChristoph Hellwig 				icount += XFS_INODES_PER_CHUNK - r.ir_freecount;
4011da177e4SLinus Torvalds 			}
4021da177e4SLinus Torvalds 			/*
4031da177e4SLinus Torvalds 			 * Set agino to after this chunk and bump the cursor.
4041da177e4SLinus Torvalds 			 */
4052e287a73SChristoph Hellwig 			agino = r.ir_startino + XFS_INODES_PER_CHUNK;
406637aa50fSChristoph Hellwig 			error = xfs_btree_increment(cur, 0, &tmp);
407cd57e594SLachlan McIlroy 			cond_resched();
4081da177e4SLinus Torvalds 		}
4091da177e4SLinus Torvalds 		/*
4101da177e4SLinus Torvalds 		 * Drop the btree buffers and the agi buffer.
4111da177e4SLinus Torvalds 		 * We can't hold any of the locks these represent
4121da177e4SLinus Torvalds 		 * when calling iget.
4131da177e4SLinus Torvalds 		 */
4141da177e4SLinus Torvalds 		xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
4151da177e4SLinus Torvalds 		xfs_buf_relse(agbp);
4161da177e4SLinus Torvalds 		/*
4171da177e4SLinus Torvalds 		 * Now format all the good inodes into the user's buffer.
4181da177e4SLinus Torvalds 		 */
4191da177e4SLinus Torvalds 		irbufend = irbp;
4201da177e4SLinus Torvalds 		for (irbp = irbuf;
421cd57e594SLachlan McIlroy 		     irbp < irbufend && XFS_BULKSTAT_UBLEFT(ubleft); irbp++) {
4221da177e4SLinus Torvalds 			/*
4231da177e4SLinus Torvalds 			 * Now process this chunk of inodes.
4241da177e4SLinus Torvalds 			 */
42526275093SNathan Scott 			for (agino = irbp->ir_startino, chunkidx = clustidx = 0;
426cd57e594SLachlan McIlroy 			     XFS_BULKSTAT_UBLEFT(ubleft) &&
42726275093SNathan Scott 				irbp->ir_freecount < XFS_INODES_PER_CHUNK;
4281da177e4SLinus Torvalds 			     chunkidx++, clustidx++, agino++) {
4291da177e4SLinus Torvalds 				ASSERT(chunkidx < XFS_INODES_PER_CHUNK);
4309cee4c5bSJie Liu 
431c2cba57eSLachlan McIlroy 				ino = XFS_AGINO_TO_INO(mp, agno, agino);
4321da177e4SLinus Torvalds 				/*
4331da177e4SLinus Torvalds 				 * Skip if this inode is free.
4341da177e4SLinus Torvalds 				 */
435c2cba57eSLachlan McIlroy 				if (XFS_INOBT_MASK(chunkidx) & irbp->ir_free) {
436c2cba57eSLachlan McIlroy 					lastino = ino;
4371da177e4SLinus Torvalds 					continue;
438c2cba57eSLachlan McIlroy 				}
4391da177e4SLinus Torvalds 				/*
4401da177e4SLinus Torvalds 				 * Count used inodes as free so we can tell
4411da177e4SLinus Torvalds 				 * when the chunk is used up.
4421da177e4SLinus Torvalds 				 */
44326275093SNathan Scott 				irbp->ir_freecount++;
4441da177e4SLinus Torvalds 
4451da177e4SLinus Torvalds 				/*
4461da177e4SLinus Torvalds 				 * Get the inode and fill in a single buffer.
4471da177e4SLinus Torvalds 				 */
4481da177e4SLinus Torvalds 				ubused = statstruct_size;
4497b6259e7SDave Chinner 				error = formatter(mp, ino, ubufp, ubleft,
4507dce11dbSChristoph Hellwig 						  &ubused, &fmterror);
4511da177e4SLinus Torvalds 				if (fmterror == BULKSTAT_RV_NOTHING) {
4522451337dSDave Chinner 					if (error && error != -ENOENT &&
4532451337dSDave Chinner 						error != -EINVAL) {
45422de606aSVlad Apostolov 						ubleft = 0;
45522de606aSVlad Apostolov 						rval = error;
45622de606aSVlad Apostolov 						break;
45722de606aSVlad Apostolov 					}
4586e73b418SVlad Apostolov 					lastino = ino;
4591da177e4SLinus Torvalds 					continue;
4601da177e4SLinus Torvalds 				}
4611da177e4SLinus Torvalds 				if (fmterror == BULKSTAT_RV_GIVEUP) {
4621da177e4SLinus Torvalds 					ubleft = 0;
4631da177e4SLinus Torvalds 					ASSERT(error);
4641da177e4SLinus Torvalds 					rval = error;
4651da177e4SLinus Torvalds 					break;
4661da177e4SLinus Torvalds 				}
4671da177e4SLinus Torvalds 				if (ubufp)
4681da177e4SLinus Torvalds 					ubufp += ubused;
4691da177e4SLinus Torvalds 				ubleft -= ubused;
4701da177e4SLinus Torvalds 				ubelem++;
4711da177e4SLinus Torvalds 				lastino = ino;
4721da177e4SLinus Torvalds 			}
473cd57e594SLachlan McIlroy 
474cd57e594SLachlan McIlroy 			cond_resched();
4751da177e4SLinus Torvalds 		}
4761da177e4SLinus Torvalds 		/*
4771da177e4SLinus Torvalds 		 * Set up for the next loop iteration.
4781da177e4SLinus Torvalds 		 */
479cd57e594SLachlan McIlroy 		if (XFS_BULKSTAT_UBLEFT(ubleft)) {
4801da177e4SLinus Torvalds 			if (end_of_ag) {
4811da177e4SLinus Torvalds 				agno++;
4821da177e4SLinus Torvalds 				agino = 0;
483cd57e594SLachlan McIlroy 			} else
484cd57e594SLachlan McIlroy 				agino = XFS_INO_TO_AGINO(mp, lastino);
4851da177e4SLinus Torvalds 		} else
4861da177e4SLinus Torvalds 			break;
4871da177e4SLinus Torvalds 	}
4881da177e4SLinus Torvalds 	/*
4891da177e4SLinus Torvalds 	 * Done, we're either out of filesystem or space to put the data.
4901da177e4SLinus Torvalds 	 */
491fdd3cceeSDave Chinner 	kmem_free(irbuf);
4921da177e4SLinus Torvalds 	*ubcountp = ubelem;
493cd57e594SLachlan McIlroy 	/*
494cd57e594SLachlan McIlroy 	 * Found some inodes, return them now and return the error next time.
495cd57e594SLachlan McIlroy 	 */
496cd57e594SLachlan McIlroy 	if (ubelem)
497cd57e594SLachlan McIlroy 		rval = 0;
4981da177e4SLinus Torvalds 	if (agno >= mp->m_sb.sb_agcount) {
4991da177e4SLinus Torvalds 		/*
5001da177e4SLinus Torvalds 		 * If we ran out of filesystem, mark lastino as off
5011da177e4SLinus Torvalds 		 * the end of the filesystem, so the next call
5021da177e4SLinus Torvalds 		 * will return immediately.
5031da177e4SLinus Torvalds 		 */
5041da177e4SLinus Torvalds 		*lastinop = (xfs_ino_t)XFS_AGINO_TO_INO(mp, agno, 0);
5051da177e4SLinus Torvalds 		*done = 1;
5061da177e4SLinus Torvalds 	} else
5071da177e4SLinus Torvalds 		*lastinop = (xfs_ino_t)lastino;
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds 	return rval;
5101da177e4SLinus Torvalds }
5111da177e4SLinus Torvalds 
512faa63e95SMichal Marek int
513faa63e95SMichal Marek xfs_inumbers_fmt(
514faa63e95SMichal Marek 	void			__user *ubuffer, /* buffer to write to */
515549fa006SJie Liu 	const struct xfs_inogrp	*buffer,	/* buffer to read from */
516faa63e95SMichal Marek 	long			count,		/* # of elements to read */
517faa63e95SMichal Marek 	long			*written)	/* # of bytes written */
518faa63e95SMichal Marek {
519faa63e95SMichal Marek 	if (copy_to_user(ubuffer, buffer, count * sizeof(*buffer)))
520faa63e95SMichal Marek 		return -EFAULT;
521faa63e95SMichal Marek 	*written = count * sizeof(*buffer);
522faa63e95SMichal Marek 	return 0;
523faa63e95SMichal Marek }
524faa63e95SMichal Marek 
5251da177e4SLinus Torvalds /*
5261da177e4SLinus Torvalds  * Return inode number table for the filesystem.
5271da177e4SLinus Torvalds  */
5281da177e4SLinus Torvalds int					/* error status */
5291da177e4SLinus Torvalds xfs_inumbers(
530549fa006SJie Liu 	struct xfs_mount	*mp,/* mount point for filesystem */
5311da177e4SLinus Torvalds 	xfs_ino_t		*lastino,/* last inode returned */
5321da177e4SLinus Torvalds 	int			*count,/* size of buffer/count returned */
533faa63e95SMichal Marek 	void			__user *ubuffer,/* buffer with inode descriptions */
534faa63e95SMichal Marek 	inumbers_fmt_pf		formatter)
5351da177e4SLinus Torvalds {
536549fa006SJie Liu 	xfs_agnumber_t		agno = XFS_INO_TO_AGNO(mp, *lastino);
537549fa006SJie Liu 	xfs_agino_t		agino = XFS_INO_TO_AGINO(mp, *lastino);
538549fa006SJie Liu 	struct xfs_btree_cur	*cur = NULL;
539*c7cb51dcSJie Liu 	struct xfs_buf		*agbp = NULL;
540549fa006SJie Liu 	struct xfs_inogrp	*buffer;
5411da177e4SLinus Torvalds 	int			bcount;
542549fa006SJie Liu 	int			left = *count;
543549fa006SJie Liu 	int			bufidx = 0;
544549fa006SJie Liu 	int			error = 0;
5451da177e4SLinus Torvalds 
5461da177e4SLinus Torvalds 	*count = 0;
547549fa006SJie Liu 	if (agno >= mp->m_sb.sb_agcount ||
548549fa006SJie Liu 	    *lastino != XFS_AGINO_TO_INO(mp, agno, agino))
549549fa006SJie Liu 		return error;
550549fa006SJie Liu 
551e6a4b37fSTim Shimmin 	bcount = MIN(left, (int)(PAGE_SIZE / sizeof(*buffer)));
5521da177e4SLinus Torvalds 	buffer = kmem_alloc(bcount * sizeof(*buffer), KM_SLEEP);
553*c7cb51dcSJie Liu 	do {
554549fa006SJie Liu 		struct xfs_inobt_rec_incore	r;
555549fa006SJie Liu 		int				stat;
556549fa006SJie Liu 
557*c7cb51dcSJie Liu 		if (!agbp) {
5581da177e4SLinus Torvalds 			error = xfs_ialloc_read_agi(mp, NULL, agno, &agbp);
559*c7cb51dcSJie Liu 			if (error)
560*c7cb51dcSJie Liu 				break;
561*c7cb51dcSJie Liu 
56257bd3dbeSBrian Foster 			cur = xfs_inobt_init_cursor(mp, NULL, agbp, agno,
56357bd3dbeSBrian Foster 						    XFS_BTNUM_INO);
56421875505SChristoph Hellwig 			error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_GE,
565549fa006SJie Liu 						 &stat);
566*c7cb51dcSJie Liu 			if (error)
567*c7cb51dcSJie Liu 				break;
568*c7cb51dcSJie Liu 			if (!stat)
569*c7cb51dcSJie Liu 				goto next_ag;
5701da177e4SLinus Torvalds 		}
571*c7cb51dcSJie Liu 
572549fa006SJie Liu 		error = xfs_inobt_get_rec(cur, &r, &stat);
573*c7cb51dcSJie Liu 		if (error)
574*c7cb51dcSJie Liu 			break;
575*c7cb51dcSJie Liu 		if (!stat)
576*c7cb51dcSJie Liu 			goto next_ag;
577*c7cb51dcSJie Liu 
5782e287a73SChristoph Hellwig 		agino = r.ir_startino + XFS_INODES_PER_CHUNK - 1;
5792e287a73SChristoph Hellwig 		buffer[bufidx].xi_startino =
5802e287a73SChristoph Hellwig 			XFS_AGINO_TO_INO(mp, agno, r.ir_startino);
5812e287a73SChristoph Hellwig 		buffer[bufidx].xi_alloccount =
5822e287a73SChristoph Hellwig 			XFS_INODES_PER_CHUNK - r.ir_freecount;
5832e287a73SChristoph Hellwig 		buffer[bufidx].xi_allocmask = ~r.ir_free;
584*c7cb51dcSJie Liu 		if (++bufidx == bcount) {
585faa63e95SMichal Marek 			long	written;
586*c7cb51dcSJie Liu 
587549fa006SJie Liu 			error = formatter(ubuffer, buffer, bufidx, &written);
588549fa006SJie Liu 			if (error)
5891da177e4SLinus Torvalds 				break;
590faa63e95SMichal Marek 			ubuffer += written;
5911da177e4SLinus Torvalds 			*count += bufidx;
5921da177e4SLinus Torvalds 			bufidx = 0;
5931da177e4SLinus Torvalds 		}
594*c7cb51dcSJie Liu 		if (!--left)
595*c7cb51dcSJie Liu 			break;
596*c7cb51dcSJie Liu 
597549fa006SJie Liu 		error = xfs_btree_increment(cur, 0, &stat);
598*c7cb51dcSJie Liu 		if (error)
599*c7cb51dcSJie Liu 			break;
600*c7cb51dcSJie Liu 		if (stat)
601*c7cb51dcSJie Liu 			continue;
602*c7cb51dcSJie Liu 
603*c7cb51dcSJie Liu next_ag:
6041da177e4SLinus Torvalds 		xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
6051da177e4SLinus Torvalds 		cur = NULL;
6061da177e4SLinus Torvalds 		xfs_buf_relse(agbp);
6071da177e4SLinus Torvalds 		agbp = NULL;
608*c7cb51dcSJie Liu 		agino = 0;
609*c7cb51dcSJie Liu 	} while (++agno < mp->m_sb.sb_agcount);
610*c7cb51dcSJie Liu 
6111da177e4SLinus Torvalds 	if (!error) {
6121da177e4SLinus Torvalds 		if (bufidx) {
613faa63e95SMichal Marek 			long	written;
614*c7cb51dcSJie Liu 
615549fa006SJie Liu 			error = formatter(ubuffer, buffer, bufidx, &written);
616549fa006SJie Liu 			if (!error)
6171da177e4SLinus Torvalds 				*count += bufidx;
6181da177e4SLinus Torvalds 		}
6191da177e4SLinus Torvalds 		*lastino = XFS_AGINO_TO_INO(mp, agno, agino);
6201da177e4SLinus Torvalds 	}
621*c7cb51dcSJie Liu 
622f0e2d93cSDenys Vlasenko 	kmem_free(buffer);
6231da177e4SLinus Torvalds 	if (cur)
6241da177e4SLinus Torvalds 		xfs_btree_del_cursor(cur, (error ? XFS_BTREE_ERROR :
6251da177e4SLinus Torvalds 					   XFS_BTREE_NOERROR));
6261da177e4SLinus Torvalds 	if (agbp)
6271da177e4SLinus Torvalds 		xfs_buf_relse(agbp);
628*c7cb51dcSJie Liu 
6291da177e4SLinus Torvalds 	return error;
6301da177e4SLinus Torvalds }
631