xref: /openbmc/linux/fs/xfs/xfs_itable.c (revision f7ca35227253dc8244fd908140b06010e67a31e5)
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"
241da177e4SLinus Torvalds #include "xfs_mount.h"
251da177e4SLinus Torvalds #include "xfs_inode.h"
26a4fbe6abSDave Chinner #include "xfs_btree.h"
271da177e4SLinus Torvalds #include "xfs_ialloc.h"
28a4fbe6abSDave Chinner #include "xfs_ialloc_btree.h"
291da177e4SLinus Torvalds #include "xfs_itable.h"
301da177e4SLinus Torvalds #include "xfs_error.h"
31f2d67614SChristoph Hellwig #include "xfs_trace.h"
3233479e05SDave Chinner #include "xfs_icache.h"
331da177e4SLinus Torvalds 
34d96f8f89SEric Sandeen STATIC int
356f1f2168SVlad Apostolov xfs_internal_inum(
366f1f2168SVlad Apostolov 	xfs_mount_t	*mp,
376f1f2168SVlad Apostolov 	xfs_ino_t	ino)
386f1f2168SVlad Apostolov {
396f1f2168SVlad Apostolov 	return (ino == mp->m_sb.sb_rbmino || ino == mp->m_sb.sb_rsumino ||
4062118709SEric Sandeen 		(xfs_sb_version_hasquota(&mp->m_sb) &&
419cad19d2SChandra Seetharaman 		 xfs_is_quota_inode(&mp->m_sb, ino)));
426f1f2168SVlad Apostolov }
436f1f2168SVlad Apostolov 
447dce11dbSChristoph Hellwig /*
457dce11dbSChristoph Hellwig  * Return stat information for one inode.
467dce11dbSChristoph Hellwig  * Return 0 if ok, else errno.
477dce11dbSChristoph Hellwig  */
487dce11dbSChristoph Hellwig int
497dce11dbSChristoph Hellwig xfs_bulkstat_one_int(
507dce11dbSChristoph Hellwig 	struct xfs_mount	*mp,		/* mount point for filesystem */
517dce11dbSChristoph Hellwig 	xfs_ino_t		ino,		/* inode to get data for */
527dce11dbSChristoph Hellwig 	void __user		*buffer,	/* buffer to place output in */
537dce11dbSChristoph Hellwig 	int			ubsize,		/* size of buffer */
547dce11dbSChristoph Hellwig 	bulkstat_one_fmt_pf	formatter,	/* formatter, copy to user */
557dce11dbSChristoph Hellwig 	int			*ubused,	/* bytes used by me */
561da177e4SLinus Torvalds 	int			*stat)		/* BULKSTAT_RV_... */
571da177e4SLinus Torvalds {
587dce11dbSChristoph Hellwig 	struct xfs_icdinode	*dic;		/* dinode core info pointer */
597dce11dbSChristoph Hellwig 	struct xfs_inode	*ip;		/* incore inode pointer */
603987848cSDave Chinner 	struct inode		*inode;
617dce11dbSChristoph Hellwig 	struct xfs_bstat	*buf;		/* return buffer */
627dce11dbSChristoph Hellwig 	int			error = 0;	/* error value */
637dce11dbSChristoph Hellwig 
647dce11dbSChristoph Hellwig 	*stat = BULKSTAT_RV_NOTHING;
657dce11dbSChristoph Hellwig 
667dce11dbSChristoph Hellwig 	if (!buffer || xfs_internal_inum(mp, ino))
672451337dSDave Chinner 		return -EINVAL;
687dce11dbSChristoph Hellwig 
69*f7ca3522SDarrick J. Wong 	buf = kmem_zalloc(sizeof(*buf), KM_SLEEP | KM_MAYFAIL);
707dce11dbSChristoph Hellwig 	if (!buf)
712451337dSDave Chinner 		return -ENOMEM;
721da177e4SLinus Torvalds 
73745b1f47SNathan Scott 	error = xfs_iget(mp, NULL, ino,
745132ba8fSDave Chinner 			 (XFS_IGET_DONTCACHE | XFS_IGET_UNTRUSTED),
755132ba8fSDave Chinner 			 XFS_ILOCK_SHARED, &ip);
768fe65776SJie Liu 	if (error)
777dce11dbSChristoph Hellwig 		goto out_free;
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds 	ASSERT(ip != NULL);
8092bfc6e7SChristoph Hellwig 	ASSERT(ip->i_imap.im_blkno != 0);
813987848cSDave Chinner 	inode = VFS_I(ip);
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds 	dic = &ip->i_d;
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds 	/* xfs_iget returns the following without needing
861da177e4SLinus Torvalds 	 * further change.
871da177e4SLinus Torvalds 	 */
886743099cSArkadiusz Mi?kiewicz 	buf->bs_projid_lo = dic->di_projid_lo;
896743099cSArkadiusz Mi?kiewicz 	buf->bs_projid_hi = dic->di_projid_hi;
901da177e4SLinus Torvalds 	buf->bs_ino = ino;
911da177e4SLinus Torvalds 	buf->bs_uid = dic->di_uid;
921da177e4SLinus Torvalds 	buf->bs_gid = dic->di_gid;
931da177e4SLinus Torvalds 	buf->bs_size = dic->di_size;
943987848cSDave Chinner 
9554d7b5c1SDave Chinner 	buf->bs_nlink = inode->i_nlink;
963987848cSDave Chinner 	buf->bs_atime.tv_sec = inode->i_atime.tv_sec;
973987848cSDave Chinner 	buf->bs_atime.tv_nsec = inode->i_atime.tv_nsec;
983987848cSDave Chinner 	buf->bs_mtime.tv_sec = inode->i_mtime.tv_sec;
993987848cSDave Chinner 	buf->bs_mtime.tv_nsec = inode->i_mtime.tv_nsec;
1003987848cSDave Chinner 	buf->bs_ctime.tv_sec = inode->i_ctime.tv_sec;
1013987848cSDave Chinner 	buf->bs_ctime.tv_nsec = inode->i_ctime.tv_nsec;
1029e9a2674SDave Chinner 	buf->bs_gen = inode->i_generation;
103c19b3b05SDave Chinner 	buf->bs_mode = inode->i_mode;
1043987848cSDave Chinner 
1051da177e4SLinus Torvalds 	buf->bs_xflags = xfs_ip2xflags(ip);
1061da177e4SLinus Torvalds 	buf->bs_extsize = dic->di_extsize << mp->m_sb.sb_blocklog;
1071da177e4SLinus Torvalds 	buf->bs_extents = dic->di_nextents;
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 
114*f7ca3522SDarrick J. Wong 	if (dic->di_version == 3) {
115*f7ca3522SDarrick J. Wong 		if (dic->di_flags2 & XFS_DIFLAG2_COWEXTSIZE)
116*f7ca3522SDarrick J. Wong 			buf->bs_cowextsize = dic->di_cowextsize <<
117*f7ca3522SDarrick J. Wong 					mp->m_sb.sb_blocklog;
118*f7ca3522SDarrick J. Wong 	}
119*f7ca3522SDarrick J. Wong 
1201da177e4SLinus Torvalds 	switch (dic->di_format) {
1211da177e4SLinus Torvalds 	case XFS_DINODE_FMT_DEV:
1221da177e4SLinus Torvalds 		buf->bs_rdev = ip->i_df.if_u2.if_rdev;
1231da177e4SLinus Torvalds 		buf->bs_blksize = BLKDEV_IOSIZE;
1241da177e4SLinus Torvalds 		buf->bs_blocks = 0;
1251da177e4SLinus Torvalds 		break;
1261da177e4SLinus Torvalds 	case XFS_DINODE_FMT_LOCAL:
1271da177e4SLinus Torvalds 	case XFS_DINODE_FMT_UUID:
1281da177e4SLinus Torvalds 		buf->bs_rdev = 0;
1291da177e4SLinus Torvalds 		buf->bs_blksize = mp->m_sb.sb_blocksize;
1301da177e4SLinus Torvalds 		buf->bs_blocks = 0;
1311da177e4SLinus Torvalds 		break;
1321da177e4SLinus Torvalds 	case XFS_DINODE_FMT_EXTENTS:
1331da177e4SLinus Torvalds 	case XFS_DINODE_FMT_BTREE:
1341da177e4SLinus Torvalds 		buf->bs_rdev = 0;
1351da177e4SLinus Torvalds 		buf->bs_blksize = mp->m_sb.sb_blocksize;
1361da177e4SLinus Torvalds 		buf->bs_blocks = dic->di_nblocks + ip->i_delayed_blks;
1371da177e4SLinus Torvalds 		break;
1381da177e4SLinus Torvalds 	}
139f2d67614SChristoph Hellwig 	xfs_iunlock(ip, XFS_ILOCK_SHARED);
140f2d67614SChristoph Hellwig 	IRELE(ip);
1417dce11dbSChristoph Hellwig 
1427dce11dbSChristoph Hellwig 	error = formatter(buffer, ubsize, ubused, buf);
1437dce11dbSChristoph Hellwig 	if (!error)
1447dce11dbSChristoph Hellwig 		*stat = BULKSTAT_RV_DIDONE;
1457dce11dbSChristoph Hellwig 
1467dce11dbSChristoph Hellwig  out_free:
1477dce11dbSChristoph Hellwig 	kmem_free(buf);
1481da177e4SLinus Torvalds 	return error;
1491da177e4SLinus Torvalds }
1501da177e4SLinus Torvalds 
15165fbaf24Ssandeen@sandeen.net /* Return 0 on success or positive error */
152faa63e95SMichal Marek STATIC int
153faa63e95SMichal Marek xfs_bulkstat_one_fmt(
154faa63e95SMichal Marek 	void			__user *ubuffer,
15565fbaf24Ssandeen@sandeen.net 	int			ubsize,
15665fbaf24Ssandeen@sandeen.net 	int			*ubused,
157faa63e95SMichal Marek 	const xfs_bstat_t	*buffer)
158faa63e95SMichal Marek {
15965fbaf24Ssandeen@sandeen.net 	if (ubsize < sizeof(*buffer))
1602451337dSDave Chinner 		return -ENOMEM;
161faa63e95SMichal Marek 	if (copy_to_user(ubuffer, buffer, sizeof(*buffer)))
1622451337dSDave Chinner 		return -EFAULT;
16365fbaf24Ssandeen@sandeen.net 	if (ubused)
16465fbaf24Ssandeen@sandeen.net 		*ubused = sizeof(*buffer);
16565fbaf24Ssandeen@sandeen.net 	return 0;
166faa63e95SMichal Marek }
167faa63e95SMichal Marek 
1682ee4fa5cSsandeen@sandeen.net int
1692ee4fa5cSsandeen@sandeen.net xfs_bulkstat_one(
1702ee4fa5cSsandeen@sandeen.net 	xfs_mount_t	*mp,		/* mount point for filesystem */
1712ee4fa5cSsandeen@sandeen.net 	xfs_ino_t	ino,		/* inode number to get data for */
1722ee4fa5cSsandeen@sandeen.net 	void		__user *buffer,	/* buffer to place output in */
1732ee4fa5cSsandeen@sandeen.net 	int		ubsize,		/* size of buffer */
1742ee4fa5cSsandeen@sandeen.net 	int		*ubused,	/* bytes used by me */
1752ee4fa5cSsandeen@sandeen.net 	int		*stat)		/* BULKSTAT_RV_... */
1762ee4fa5cSsandeen@sandeen.net {
1772ee4fa5cSsandeen@sandeen.net 	return xfs_bulkstat_one_int(mp, ino, buffer, ubsize,
1787b6259e7SDave Chinner 				    xfs_bulkstat_one_fmt, ubused, stat);
1798b56f083SNathan Scott }
1808b56f083SNathan Scott 
1814b8fdfecSJie Liu /*
1824b8fdfecSJie Liu  * Loop over all clusters in a chunk for a given incore inode allocation btree
1834b8fdfecSJie Liu  * record.  Do a readahead if there are any allocated inodes in that cluster.
1844b8fdfecSJie Liu  */
1854b8fdfecSJie Liu STATIC void
1864b8fdfecSJie Liu xfs_bulkstat_ichunk_ra(
1874b8fdfecSJie Liu 	struct xfs_mount		*mp,
1884b8fdfecSJie Liu 	xfs_agnumber_t			agno,
1894b8fdfecSJie Liu 	struct xfs_inobt_rec_incore	*irec)
1904b8fdfecSJie Liu {
1914b8fdfecSJie Liu 	xfs_agblock_t			agbno;
1924b8fdfecSJie Liu 	struct blk_plug			plug;
1934b8fdfecSJie Liu 	int				blks_per_cluster;
1944b8fdfecSJie Liu 	int				inodes_per_cluster;
1954b8fdfecSJie Liu 	int				i;	/* inode chunk index */
1964b8fdfecSJie Liu 
1974b8fdfecSJie Liu 	agbno = XFS_AGINO_TO_AGBNO(mp, irec->ir_startino);
1984b8fdfecSJie Liu 	blks_per_cluster = xfs_icluster_size_fsb(mp);
1994b8fdfecSJie Liu 	inodes_per_cluster = blks_per_cluster << mp->m_sb.sb_inopblog;
2004b8fdfecSJie Liu 
2014b8fdfecSJie Liu 	blk_start_plug(&plug);
2024b8fdfecSJie Liu 	for (i = 0; i < XFS_INODES_PER_CHUNK;
2034b8fdfecSJie Liu 	     i += inodes_per_cluster, agbno += blks_per_cluster) {
2044b8fdfecSJie Liu 		if (xfs_inobt_maskn(i, inodes_per_cluster) & ~irec->ir_free) {
2054b8fdfecSJie Liu 			xfs_btree_reada_bufs(mp, agno, agbno, blks_per_cluster,
2064b8fdfecSJie Liu 					     &xfs_inode_buf_ops);
2074b8fdfecSJie Liu 		}
2084b8fdfecSJie Liu 	}
2094b8fdfecSJie Liu 	blk_finish_plug(&plug);
2104b8fdfecSJie Liu }
2114b8fdfecSJie Liu 
212f3d1e587SJie Liu /*
213f3d1e587SJie Liu  * Lookup the inode chunk that the given inode lives in and then get the record
214f3d1e587SJie Liu  * if we found the chunk.  If the inode was not the last in the chunk and there
215f3d1e587SJie Liu  * are some left allocated, update the data for the pointed-to record as well as
216f3d1e587SJie Liu  * return the count of grabbed inodes.
217f3d1e587SJie Liu  */
218f3d1e587SJie Liu STATIC int
219f3d1e587SJie Liu xfs_bulkstat_grab_ichunk(
220f3d1e587SJie Liu 	struct xfs_btree_cur		*cur,	/* btree cursor */
221f3d1e587SJie Liu 	xfs_agino_t			agino,	/* starting inode of chunk */
222f3d1e587SJie Liu 	int				*icount,/* return # of inodes grabbed */
223f3d1e587SJie Liu 	struct xfs_inobt_rec_incore	*irec)	/* btree record */
224f3d1e587SJie Liu {
225f3d1e587SJie Liu 	int				idx;	/* index into inode chunk */
226f3d1e587SJie Liu 	int				stat;
227f3d1e587SJie Liu 	int				error = 0;
228f3d1e587SJie Liu 
229f3d1e587SJie Liu 	/* Lookup the inode chunk that this inode lives in */
230f3d1e587SJie Liu 	error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &stat);
231f3d1e587SJie Liu 	if (error)
232f3d1e587SJie Liu 		return error;
233f3d1e587SJie Liu 	if (!stat) {
234f3d1e587SJie Liu 		*icount = 0;
235f3d1e587SJie Liu 		return error;
236f3d1e587SJie Liu 	}
237f3d1e587SJie Liu 
238f3d1e587SJie Liu 	/* Get the record, should always work */
239f3d1e587SJie Liu 	error = xfs_inobt_get_rec(cur, irec, &stat);
240f3d1e587SJie Liu 	if (error)
241f3d1e587SJie Liu 		return error;
2425fb5aeeeSEric Sandeen 	XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, stat == 1);
243f3d1e587SJie Liu 
244f3d1e587SJie Liu 	/* Check if the record contains the inode in request */
245febe3cbeSDave Chinner 	if (irec->ir_startino + XFS_INODES_PER_CHUNK <= agino) {
246febe3cbeSDave Chinner 		*icount = 0;
247febe3cbeSDave Chinner 		return 0;
248febe3cbeSDave Chinner 	}
249f3d1e587SJie Liu 
250f3d1e587SJie Liu 	idx = agino - irec->ir_startino + 1;
251f3d1e587SJie Liu 	if (idx < XFS_INODES_PER_CHUNK &&
252f3d1e587SJie Liu 	    (xfs_inobt_maskn(idx, XFS_INODES_PER_CHUNK - idx) & ~irec->ir_free)) {
253f3d1e587SJie Liu 		int	i;
254f3d1e587SJie Liu 
255f3d1e587SJie Liu 		/* We got a right chunk with some left inodes allocated at it.
256f3d1e587SJie Liu 		 * Grab the chunk record.  Mark all the uninteresting inodes
257f3d1e587SJie Liu 		 * free -- because they're before our start point.
258f3d1e587SJie Liu 		 */
259f3d1e587SJie Liu 		for (i = 0; i < idx; i++) {
260f3d1e587SJie Liu 			if (XFS_INOBT_MASK(i) & ~irec->ir_free)
261f3d1e587SJie Liu 				irec->ir_freecount++;
262f3d1e587SJie Liu 		}
263f3d1e587SJie Liu 
264f3d1e587SJie Liu 		irec->ir_free |= xfs_inobt_maskn(0, idx);
26512d0714dSBrian Foster 		*icount = irec->ir_count - irec->ir_freecount;
266f3d1e587SJie Liu 	}
267f3d1e587SJie Liu 
268f3d1e587SJie Liu 	return 0;
269f3d1e587SJie Liu }
270f3d1e587SJie Liu 
271cd57e594SLachlan McIlroy #define XFS_BULKSTAT_UBLEFT(ubleft)	((ubleft) >= statstruct_size)
272cd57e594SLachlan McIlroy 
273bf4a5af2SDave Chinner struct xfs_bulkstat_agichunk {
274bf4a5af2SDave Chinner 	char		__user **ac_ubuffer;/* pointer into user's buffer */
275bf4a5af2SDave Chinner 	int		ac_ubleft;	/* bytes left in user's buffer */
276bf4a5af2SDave Chinner 	int		ac_ubelem;	/* spaces used in user's buffer */
277bf4a5af2SDave Chinner };
278bf4a5af2SDave Chinner 
2798b56f083SNathan Scott /*
2801e773c49SJie Liu  * Process inodes in chunk with a pointer to a formatter function
2811e773c49SJie Liu  * that will iget the inode and fill in the appropriate structure.
2821e773c49SJie Liu  */
283bf4a5af2SDave Chinner static int
2841e773c49SJie Liu xfs_bulkstat_ag_ichunk(
2851e773c49SJie Liu 	struct xfs_mount		*mp,
2861e773c49SJie Liu 	xfs_agnumber_t			agno,
2871e773c49SJie Liu 	struct xfs_inobt_rec_incore	*irbp,
2881e773c49SJie Liu 	bulkstat_one_pf			formatter,
2891e773c49SJie Liu 	size_t				statstruct_size,
290bf4a5af2SDave Chinner 	struct xfs_bulkstat_agichunk	*acp,
29100275899SDave Chinner 	xfs_agino_t			*last_agino)
2921e773c49SJie Liu {
2931e773c49SJie Liu 	char				__user **ubufp = acp->ac_ubuffer;
2942b831ac6SDave Chinner 	int				chunkidx;
2951e773c49SJie Liu 	int				error = 0;
29600275899SDave Chinner 	xfs_agino_t			agino = irbp->ir_startino;
2971e773c49SJie Liu 
2982b831ac6SDave Chinner 	for (chunkidx = 0; chunkidx < XFS_INODES_PER_CHUNK;
2992b831ac6SDave Chinner 	     chunkidx++, agino++) {
3002b831ac6SDave Chinner 		int		fmterror;
3011e773c49SJie Liu 		int		ubused;
30200275899SDave Chinner 
30300275899SDave Chinner 		/* inode won't fit in buffer, we are done */
30400275899SDave Chinner 		if (acp->ac_ubleft < statstruct_size)
30500275899SDave Chinner 			break;
3061e773c49SJie Liu 
3071e773c49SJie Liu 		/* Skip if this inode is free */
30800275899SDave Chinner 		if (XFS_INOBT_MASK(chunkidx) & irbp->ir_free)
3091e773c49SJie Liu 			continue;
3101e773c49SJie Liu 
3111e773c49SJie Liu 		/* Get the inode and fill in a single buffer */
3121e773c49SJie Liu 		ubused = statstruct_size;
31300275899SDave Chinner 		error = formatter(mp, XFS_AGINO_TO_INO(mp, agno, agino),
31400275899SDave Chinner 				  *ubufp, acp->ac_ubleft, &ubused, &fmterror);
31500275899SDave Chinner 
3162b831ac6SDave Chinner 		if (fmterror == BULKSTAT_RV_GIVEUP ||
3172b831ac6SDave Chinner 		    (error && error != -ENOENT && error != -EINVAL)) {
3182b831ac6SDave Chinner 			acp->ac_ubleft = 0;
3191e773c49SJie Liu 			ASSERT(error);
3201e773c49SJie Liu 			break;
3211e773c49SJie Liu 		}
3222b831ac6SDave Chinner 
3232b831ac6SDave Chinner 		/* be careful not to leak error if at end of chunk */
3242b831ac6SDave Chinner 		if (fmterror == BULKSTAT_RV_NOTHING || error) {
3252b831ac6SDave Chinner 			error = 0;
3262b831ac6SDave Chinner 			continue;
3271e773c49SJie Liu 		}
3281e773c49SJie Liu 
3292b831ac6SDave Chinner 		*ubufp += ubused;
3302b831ac6SDave Chinner 		acp->ac_ubleft -= ubused;
3312b831ac6SDave Chinner 		acp->ac_ubelem++;
3322b831ac6SDave Chinner 	}
3331e773c49SJie Liu 
33400275899SDave Chinner 	/*
33500275899SDave Chinner 	 * Post-update *last_agino. At this point, agino will always point one
33600275899SDave Chinner 	 * inode past the last inode we processed successfully. Hence we
33700275899SDave Chinner 	 * substract that inode when setting the *last_agino cursor so that we
33800275899SDave Chinner 	 * return the correct cookie to userspace. On the next bulkstat call,
33900275899SDave Chinner 	 * the inode under the lastino cookie will be skipped as we have already
34000275899SDave Chinner 	 * processed it here.
34100275899SDave Chinner 	 */
34200275899SDave Chinner 	*last_agino = agino - 1;
34300275899SDave Chinner 
3441e773c49SJie Liu 	return error;
3451e773c49SJie Liu }
3461e773c49SJie Liu 
3471e773c49SJie Liu /*
3481da177e4SLinus Torvalds  * Return stat information in bulk (by-inode) for the filesystem.
3491da177e4SLinus Torvalds  */
3501da177e4SLinus Torvalds int					/* error status */
3511da177e4SLinus Torvalds xfs_bulkstat(
3521da177e4SLinus Torvalds 	xfs_mount_t		*mp,	/* mount point for filesystem */
3531da177e4SLinus Torvalds 	xfs_ino_t		*lastinop, /* last inode returned */
3541da177e4SLinus Torvalds 	int			*ubcountp, /* size of buffer/count returned */
3551da177e4SLinus Torvalds 	bulkstat_one_pf		formatter, /* func that'd fill a single buf */
3561da177e4SLinus Torvalds 	size_t			statstruct_size, /* sizeof struct filling */
3571da177e4SLinus Torvalds 	char			__user *ubuffer, /* buffer with inode stats */
358c41564b5SNathan Scott 	int			*done)	/* 1 if there are more stats to get */
3591da177e4SLinus Torvalds {
3601da177e4SLinus Torvalds 	xfs_buf_t		*agbp;	/* agi header buffer */
3611da177e4SLinus Torvalds 	xfs_agino_t		agino;	/* inode # in allocation group */
3621da177e4SLinus Torvalds 	xfs_agnumber_t		agno;	/* allocation group number */
3631da177e4SLinus Torvalds 	xfs_btree_cur_t		*cur;	/* btree cursor for ialloc btree */
364215101c3SNathan Scott 	size_t			irbsize; /* size of irec buffer in bytes */
36526275093SNathan Scott 	xfs_inobt_rec_incore_t	*irbuf;	/* start of irec buffer */
3661da177e4SLinus Torvalds 	int			nirbuf;	/* size of irbuf */
3671da177e4SLinus Torvalds 	int			ubcount; /* size of user's buffer */
368bf4a5af2SDave Chinner 	struct xfs_bulkstat_agichunk ac;
3696e57c542SDave Chinner 	int			error = 0;
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds 	/*
3721da177e4SLinus Torvalds 	 * Get the last inode value, see if there's nothing to do.
3731da177e4SLinus Torvalds 	 */
37400275899SDave Chinner 	agno = XFS_INO_TO_AGNO(mp, *lastinop);
37500275899SDave Chinner 	agino = XFS_INO_TO_AGINO(mp, *lastinop);
3761da177e4SLinus Torvalds 	if (agno >= mp->m_sb.sb_agcount ||
37700275899SDave Chinner 	    *lastinop != XFS_AGINO_TO_INO(mp, agno, agino)) {
3781da177e4SLinus Torvalds 		*done = 1;
3791da177e4SLinus Torvalds 		*ubcountp = 0;
3801da177e4SLinus Torvalds 		return 0;
3811da177e4SLinus Torvalds 	}
382296dfd7fSJie Liu 
3831da177e4SLinus Torvalds 	ubcount = *ubcountp; /* statstruct's */
384bf4a5af2SDave Chinner 	ac.ac_ubuffer = &ubuffer;
385bf4a5af2SDave Chinner 	ac.ac_ubleft = ubcount * statstruct_size; /* bytes */;
386bf4a5af2SDave Chinner 	ac.ac_ubelem = 0;
387bf4a5af2SDave Chinner 
388bf4a5af2SDave Chinner 	*ubcountp = 0;
3891da177e4SLinus Torvalds 	*done = 0;
390bf4a5af2SDave Chinner 
391bdfb0430SChristoph Hellwig 	irbuf = kmem_zalloc_greedy(&irbsize, PAGE_SIZE, PAGE_SIZE * 4);
392bdfb0430SChristoph Hellwig 	if (!irbuf)
3932451337dSDave Chinner 		return -ENOMEM;
394bdfb0430SChristoph Hellwig 
395bb3c7d29SNathan Scott 	nirbuf = irbsize / sizeof(*irbuf);
396bb3c7d29SNathan Scott 
3971da177e4SLinus Torvalds 	/*
3981da177e4SLinus Torvalds 	 * Loop over the allocation groups, starting from the last
3991da177e4SLinus Torvalds 	 * inode returned; 0 means start of the allocation group.
4001da177e4SLinus Torvalds 	 */
4016e57c542SDave Chinner 	while (agno < mp->m_sb.sb_agcount) {
4026e57c542SDave Chinner 		struct xfs_inobt_rec_incore	*irbp = irbuf;
4036e57c542SDave Chinner 		struct xfs_inobt_rec_incore	*irbufend = irbuf + nirbuf;
4046e57c542SDave Chinner 		bool				end_of_ag = false;
4056e57c542SDave Chinner 		int				icount = 0;
4066e57c542SDave Chinner 		int				stat;
4076e57c542SDave Chinner 
4081da177e4SLinus Torvalds 		error = xfs_ialloc_read_agi(mp, NULL, agno, &agbp);
409d4c27348SJie Liu 		if (error)
410d4c27348SJie Liu 			break;
4111da177e4SLinus Torvalds 		/*
4121da177e4SLinus Torvalds 		 * Allocate and initialize a btree cursor for ialloc btree.
4131da177e4SLinus Torvalds 		 */
41457bd3dbeSBrian Foster 		cur = xfs_inobt_init_cursor(mp, NULL, agbp, agno,
41557bd3dbeSBrian Foster 					    XFS_BTNUM_INO);
4161da177e4SLinus Torvalds 		if (agino > 0) {
417f3d1e587SJie Liu 			/*
418f3d1e587SJie Liu 			 * In the middle of an allocation group, we need to get
419f3d1e587SJie Liu 			 * the remainder of the chunk we're in.
420f3d1e587SJie Liu 			 */
421f3d1e587SJie Liu 			struct xfs_inobt_rec_incore	r;
4222e287a73SChristoph Hellwig 
423f3d1e587SJie Liu 			error = xfs_bulkstat_grab_ichunk(cur, agino, &icount, &r);
424f3d1e587SJie Liu 			if (error)
425a6bbce54SDave Chinner 				goto del_cursor;
426f3d1e587SJie Liu 			if (icount) {
4272e287a73SChristoph Hellwig 				irbp->ir_startino = r.ir_startino;
42812d0714dSBrian Foster 				irbp->ir_holemask = r.ir_holemask;
42912d0714dSBrian Foster 				irbp->ir_count = r.ir_count;
4302e287a73SChristoph Hellwig 				irbp->ir_freecount = r.ir_freecount;
4312e287a73SChristoph Hellwig 				irbp->ir_free = r.ir_free;
4321da177e4SLinus Torvalds 				irbp++;
4331da177e4SLinus Torvalds 			}
434f3d1e587SJie Liu 			/* Increment to the next record */
435afa947cbSDave Chinner 			error = xfs_btree_increment(cur, 0, &stat);
4361da177e4SLinus Torvalds 		} else {
437f3d1e587SJie Liu 			/* Start of ag.  Lookup the first inode chunk */
438afa947cbSDave Chinner 			error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &stat);
4391da177e4SLinus Torvalds 		}
440afa947cbSDave Chinner 		if (error || stat == 0) {
4416e57c542SDave Chinner 			end_of_ag = true;
442a6bbce54SDave Chinner 			goto del_cursor;
443afa947cbSDave Chinner 		}
444d4c27348SJie Liu 
4451da177e4SLinus Torvalds 		/*
4461da177e4SLinus Torvalds 		 * Loop through inode btree records in this ag,
4471da177e4SLinus Torvalds 		 * until we run out of inodes or space in the buffer.
4481da177e4SLinus Torvalds 		 */
4491da177e4SLinus Torvalds 		while (irbp < irbufend && icount < ubcount) {
450d4c27348SJie Liu 			struct xfs_inobt_rec_incore	r;
4512e287a73SChristoph Hellwig 
452afa947cbSDave Chinner 			error = xfs_inobt_get_rec(cur, &r, &stat);
453afa947cbSDave Chinner 			if (error || stat == 0) {
4546e57c542SDave Chinner 				end_of_ag = true;
455a6bbce54SDave Chinner 				goto del_cursor;
4562e287a73SChristoph Hellwig 			}
4572e287a73SChristoph Hellwig 
4581da177e4SLinus Torvalds 			/*
4591da177e4SLinus Torvalds 			 * If this chunk has any allocated inodes, save it.
46026275093SNathan Scott 			 * Also start read-ahead now for this chunk.
4611da177e4SLinus Torvalds 			 */
46212d0714dSBrian Foster 			if (r.ir_freecount < r.ir_count) {
4634b8fdfecSJie Liu 				xfs_bulkstat_ichunk_ra(mp, agno, &r);
4642e287a73SChristoph Hellwig 				irbp->ir_startino = r.ir_startino;
46512d0714dSBrian Foster 				irbp->ir_holemask = r.ir_holemask;
46612d0714dSBrian Foster 				irbp->ir_count = r.ir_count;
4672e287a73SChristoph Hellwig 				irbp->ir_freecount = r.ir_freecount;
4682e287a73SChristoph Hellwig 				irbp->ir_free = r.ir_free;
4691da177e4SLinus Torvalds 				irbp++;
47012d0714dSBrian Foster 				icount += r.ir_count - r.ir_freecount;
4711da177e4SLinus Torvalds 			}
472afa947cbSDave Chinner 			error = xfs_btree_increment(cur, 0, &stat);
473afa947cbSDave Chinner 			if (error || stat == 0) {
4746e57c542SDave Chinner 				end_of_ag = true;
4757a19dee1SJan Kara 				goto del_cursor;
4767a19dee1SJan Kara 			}
477cd57e594SLachlan McIlroy 			cond_resched();
4781da177e4SLinus Torvalds 		}
479a6bbce54SDave Chinner 
4801da177e4SLinus Torvalds 		/*
481a6bbce54SDave Chinner 		 * Drop the btree buffers and the agi buffer as we can't hold any
482a6bbce54SDave Chinner 		 * of the locks these represent when calling iget. If there is a
483a6bbce54SDave Chinner 		 * pending error, then we are done.
4841da177e4SLinus Torvalds 		 */
485a6bbce54SDave Chinner del_cursor:
486f307080aSBrian Foster 		xfs_btree_del_cursor(cur, error ?
487f307080aSBrian Foster 					  XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4881da177e4SLinus Torvalds 		xfs_buf_relse(agbp);
489a6bbce54SDave Chinner 		if (error)
490a6bbce54SDave Chinner 			break;
4911da177e4SLinus Torvalds 		/*
49200275899SDave Chinner 		 * Now format all the good inodes into the user's buffer. The
49300275899SDave Chinner 		 * call to xfs_bulkstat_ag_ichunk() sets up the agino pointer
49400275899SDave Chinner 		 * for the next loop iteration.
4951da177e4SLinus Torvalds 		 */
4961da177e4SLinus Torvalds 		irbufend = irbp;
4971da177e4SLinus Torvalds 		for (irbp = irbuf;
4986e57c542SDave Chinner 		     irbp < irbufend && ac.ac_ubleft >= statstruct_size;
499bf4a5af2SDave Chinner 		     irbp++) {
5001e773c49SJie Liu 			error = xfs_bulkstat_ag_ichunk(mp, agno, irbp,
501bf4a5af2SDave Chinner 					formatter, statstruct_size, &ac,
50200275899SDave Chinner 					&agino);
5031e773c49SJie Liu 			if (error)
504febe3cbeSDave Chinner 				break;
5051da177e4SLinus Torvalds 
506cd57e594SLachlan McIlroy 			cond_resched();
5071da177e4SLinus Torvalds 		}
508bf4a5af2SDave Chinner 
509febe3cbeSDave Chinner 		/*
510febe3cbeSDave Chinner 		 * If we've run out of space or had a formatting error, we
511febe3cbeSDave Chinner 		 * are now done
512febe3cbeSDave Chinner 		 */
513febe3cbeSDave Chinner 		if (ac.ac_ubleft < statstruct_size || error)
5146e57c542SDave Chinner 			break;
5156e57c542SDave Chinner 
5161da177e4SLinus Torvalds 		if (end_of_ag) {
5171da177e4SLinus Torvalds 			agno++;
5181da177e4SLinus Torvalds 			agino = 0;
51900275899SDave Chinner 		}
5201da177e4SLinus Torvalds 	}
5211da177e4SLinus Torvalds 	/*
5221da177e4SLinus Torvalds 	 * Done, we're either out of filesystem or space to put the data.
5231da177e4SLinus Torvalds 	 */
524fdd3cceeSDave Chinner 	kmem_free(irbuf);
525bf4a5af2SDave Chinner 	*ubcountp = ac.ac_ubelem;
526febe3cbeSDave Chinner 
527cd57e594SLachlan McIlroy 	/*
528febe3cbeSDave Chinner 	 * We found some inodes, so clear the error status and return them.
529febe3cbeSDave Chinner 	 * The lastino pointer will point directly at the inode that triggered
530febe3cbeSDave Chinner 	 * any error that occurred, so on the next call the error will be
531febe3cbeSDave Chinner 	 * triggered again and propagated to userspace as there will be no
532febe3cbeSDave Chinner 	 * formatted inodes in the buffer.
533cd57e594SLachlan McIlroy 	 */
534bf4a5af2SDave Chinner 	if (ac.ac_ubelem)
535febe3cbeSDave Chinner 		error = 0;
536febe3cbeSDave Chinner 
5371da177e4SLinus Torvalds 	/*
53800275899SDave Chinner 	 * If we ran out of filesystem, lastino will point off the end of
53900275899SDave Chinner 	 * the filesystem so the next call will return immediately.
5401da177e4SLinus Torvalds 	 */
54100275899SDave Chinner 	*lastinop = XFS_AGINO_TO_INO(mp, agno, agino);
54200275899SDave Chinner 	if (agno >= mp->m_sb.sb_agcount)
5431da177e4SLinus Torvalds 		*done = 1;
5441da177e4SLinus Torvalds 
545febe3cbeSDave Chinner 	return error;
5461da177e4SLinus Torvalds }
5471da177e4SLinus Torvalds 
548faa63e95SMichal Marek int
549faa63e95SMichal Marek xfs_inumbers_fmt(
550faa63e95SMichal Marek 	void			__user *ubuffer, /* buffer to write to */
551549fa006SJie Liu 	const struct xfs_inogrp	*buffer,	/* buffer to read from */
552faa63e95SMichal Marek 	long			count,		/* # of elements to read */
553faa63e95SMichal Marek 	long			*written)	/* # of bytes written */
554faa63e95SMichal Marek {
555faa63e95SMichal Marek 	if (copy_to_user(ubuffer, buffer, count * sizeof(*buffer)))
556faa63e95SMichal Marek 		return -EFAULT;
557faa63e95SMichal Marek 	*written = count * sizeof(*buffer);
558faa63e95SMichal Marek 	return 0;
559faa63e95SMichal Marek }
560faa63e95SMichal Marek 
5611da177e4SLinus Torvalds /*
5621da177e4SLinus Torvalds  * Return inode number table for the filesystem.
5631da177e4SLinus Torvalds  */
5641da177e4SLinus Torvalds int					/* error status */
5651da177e4SLinus Torvalds xfs_inumbers(
566549fa006SJie Liu 	struct xfs_mount	*mp,/* mount point for filesystem */
5671da177e4SLinus Torvalds 	xfs_ino_t		*lastino,/* last inode returned */
5681da177e4SLinus Torvalds 	int			*count,/* size of buffer/count returned */
569faa63e95SMichal Marek 	void			__user *ubuffer,/* buffer with inode descriptions */
570faa63e95SMichal Marek 	inumbers_fmt_pf		formatter)
5711da177e4SLinus Torvalds {
572549fa006SJie Liu 	xfs_agnumber_t		agno = XFS_INO_TO_AGNO(mp, *lastino);
573549fa006SJie Liu 	xfs_agino_t		agino = XFS_INO_TO_AGINO(mp, *lastino);
574549fa006SJie Liu 	struct xfs_btree_cur	*cur = NULL;
575c7cb51dcSJie Liu 	struct xfs_buf		*agbp = NULL;
576549fa006SJie Liu 	struct xfs_inogrp	*buffer;
5771da177e4SLinus Torvalds 	int			bcount;
578549fa006SJie Liu 	int			left = *count;
579549fa006SJie Liu 	int			bufidx = 0;
580549fa006SJie Liu 	int			error = 0;
5811da177e4SLinus Torvalds 
5821da177e4SLinus Torvalds 	*count = 0;
583549fa006SJie Liu 	if (agno >= mp->m_sb.sb_agcount ||
584549fa006SJie Liu 	    *lastino != XFS_AGINO_TO_INO(mp, agno, agino))
585549fa006SJie Liu 		return error;
586549fa006SJie Liu 
587e6a4b37fSTim Shimmin 	bcount = MIN(left, (int)(PAGE_SIZE / sizeof(*buffer)));
5881da177e4SLinus Torvalds 	buffer = kmem_alloc(bcount * sizeof(*buffer), KM_SLEEP);
589c7cb51dcSJie Liu 	do {
590549fa006SJie Liu 		struct xfs_inobt_rec_incore	r;
591549fa006SJie Liu 		int				stat;
592549fa006SJie Liu 
593c7cb51dcSJie Liu 		if (!agbp) {
5941da177e4SLinus Torvalds 			error = xfs_ialloc_read_agi(mp, NULL, agno, &agbp);
595c7cb51dcSJie Liu 			if (error)
596c7cb51dcSJie Liu 				break;
597c7cb51dcSJie Liu 
59857bd3dbeSBrian Foster 			cur = xfs_inobt_init_cursor(mp, NULL, agbp, agno,
59957bd3dbeSBrian Foster 						    XFS_BTNUM_INO);
60021875505SChristoph Hellwig 			error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_GE,
601549fa006SJie Liu 						 &stat);
602c7cb51dcSJie Liu 			if (error)
603c7cb51dcSJie Liu 				break;
604c7cb51dcSJie Liu 			if (!stat)
605c7cb51dcSJie Liu 				goto next_ag;
6061da177e4SLinus Torvalds 		}
607c7cb51dcSJie Liu 
608549fa006SJie Liu 		error = xfs_inobt_get_rec(cur, &r, &stat);
609c7cb51dcSJie Liu 		if (error)
610c7cb51dcSJie Liu 			break;
611c7cb51dcSJie Liu 		if (!stat)
612c7cb51dcSJie Liu 			goto next_ag;
613c7cb51dcSJie Liu 
6142e287a73SChristoph Hellwig 		agino = r.ir_startino + XFS_INODES_PER_CHUNK - 1;
6152e287a73SChristoph Hellwig 		buffer[bufidx].xi_startino =
6162e287a73SChristoph Hellwig 			XFS_AGINO_TO_INO(mp, agno, r.ir_startino);
61712d0714dSBrian Foster 		buffer[bufidx].xi_alloccount = r.ir_count - r.ir_freecount;
6182e287a73SChristoph Hellwig 		buffer[bufidx].xi_allocmask = ~r.ir_free;
619c7cb51dcSJie Liu 		if (++bufidx == bcount) {
620faa63e95SMichal Marek 			long	written;
621c7cb51dcSJie Liu 
622549fa006SJie Liu 			error = formatter(ubuffer, buffer, bufidx, &written);
623549fa006SJie Liu 			if (error)
6241da177e4SLinus Torvalds 				break;
625faa63e95SMichal Marek 			ubuffer += written;
6261da177e4SLinus Torvalds 			*count += bufidx;
6271da177e4SLinus Torvalds 			bufidx = 0;
6281da177e4SLinus Torvalds 		}
629c7cb51dcSJie Liu 		if (!--left)
630c7cb51dcSJie Liu 			break;
631c7cb51dcSJie Liu 
632549fa006SJie Liu 		error = xfs_btree_increment(cur, 0, &stat);
633c7cb51dcSJie Liu 		if (error)
634c7cb51dcSJie Liu 			break;
635c7cb51dcSJie Liu 		if (stat)
636c7cb51dcSJie Liu 			continue;
637c7cb51dcSJie Liu 
638c7cb51dcSJie Liu next_ag:
6391da177e4SLinus Torvalds 		xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
6401da177e4SLinus Torvalds 		cur = NULL;
6411da177e4SLinus Torvalds 		xfs_buf_relse(agbp);
6421da177e4SLinus Torvalds 		agbp = NULL;
643c7cb51dcSJie Liu 		agino = 0;
644a8b1ee8bSEric Sandeen 		agno++;
645a8b1ee8bSEric Sandeen 	} while (agno < mp->m_sb.sb_agcount);
646c7cb51dcSJie Liu 
6471da177e4SLinus Torvalds 	if (!error) {
6481da177e4SLinus Torvalds 		if (bufidx) {
649faa63e95SMichal Marek 			long	written;
650c7cb51dcSJie Liu 
651549fa006SJie Liu 			error = formatter(ubuffer, buffer, bufidx, &written);
652549fa006SJie Liu 			if (!error)
6531da177e4SLinus Torvalds 				*count += bufidx;
6541da177e4SLinus Torvalds 		}
6551da177e4SLinus Torvalds 		*lastino = XFS_AGINO_TO_INO(mp, agno, agino);
6561da177e4SLinus Torvalds 	}
657c7cb51dcSJie Liu 
658f0e2d93cSDenys Vlasenko 	kmem_free(buffer);
6591da177e4SLinus Torvalds 	if (cur)
6601da177e4SLinus Torvalds 		xfs_btree_del_cursor(cur, (error ? XFS_BTREE_ERROR :
6611da177e4SLinus Torvalds 					   XFS_BTREE_NOERROR));
6621da177e4SLinus Torvalds 	if (agbp)
6631da177e4SLinus Torvalds 		xfs_buf_relse(agbp);
664c7cb51dcSJie Liu 
6651da177e4SLinus Torvalds 	return error;
6661da177e4SLinus Torvalds }
667