xref: /openbmc/linux/fs/xfs/xfs_itable.c (revision 10c0b6ba25a71d14f80586f6a795dbc47f5c6731)
10b61f8a4SDave Chinner // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
37b718769SNathan Scott  * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
47b718769SNathan Scott  * All Rights Reserved.
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds #include "xfs.h"
7a844f451SNathan Scott #include "xfs_fs.h"
870a9883cSDave Chinner #include "xfs_shared.h"
9a4fbe6abSDave Chinner #include "xfs_format.h"
10239880efSDave Chinner #include "xfs_log_format.h"
11239880efSDave Chinner #include "xfs_trans_resv.h"
121da177e4SLinus Torvalds #include "xfs_mount.h"
131da177e4SLinus Torvalds #include "xfs_inode.h"
14a4fbe6abSDave Chinner #include "xfs_btree.h"
151da177e4SLinus Torvalds #include "xfs_ialloc.h"
16a4fbe6abSDave Chinner #include "xfs_ialloc_btree.h"
172810bd68SDarrick J. Wong #include "xfs_iwalk.h"
181da177e4SLinus Torvalds #include "xfs_itable.h"
191da177e4SLinus Torvalds #include "xfs_error.h"
2033479e05SDave Chinner #include "xfs_icache.h"
2189d139d5SDarrick J. Wong #include "xfs_health.h"
22a6343e4dSDarrick J. Wong #include "xfs_trans.h"
231da177e4SLinus Torvalds 
247dce11dbSChristoph Hellwig /*
252810bd68SDarrick J. Wong  * Bulk Stat
262810bd68SDarrick J. Wong  * =========
272810bd68SDarrick J. Wong  *
287035f972SDarrick J. Wong  * Use the inode walking functions to fill out struct xfs_bulkstat for every
292810bd68SDarrick J. Wong  * allocated inode, then pass the stat information to some externally provided
302810bd68SDarrick J. Wong  * iteration function.
317dce11dbSChristoph Hellwig  */
322810bd68SDarrick J. Wong 
332810bd68SDarrick J. Wong struct xfs_bstat_chunk {
342810bd68SDarrick J. Wong 	bulkstat_one_fmt_pf	formatter;
352810bd68SDarrick J. Wong 	struct xfs_ibulk	*breq;
367035f972SDarrick J. Wong 	struct xfs_bulkstat	*buf;
372810bd68SDarrick J. Wong };
382810bd68SDarrick J. Wong 
392810bd68SDarrick J. Wong /*
402810bd68SDarrick J. Wong  * Fill out the bulkstat info for a single inode and report it somewhere.
412810bd68SDarrick J. Wong  *
422810bd68SDarrick J. Wong  * bc->breq->lastino is effectively the inode cursor as we walk through the
432810bd68SDarrick J. Wong  * filesystem.  Therefore, we update it any time we need to move the cursor
442810bd68SDarrick J. Wong  * forward, regardless of whether or not we're sending any bstat information
452810bd68SDarrick J. Wong  * back to userspace.  If the inode is internal metadata or, has been freed
462810bd68SDarrick J. Wong  * out from under us, we just simply keep going.
472810bd68SDarrick J. Wong  *
482810bd68SDarrick J. Wong  * However, if any other type of error happens we want to stop right where we
492810bd68SDarrick J. Wong  * are so that userspace will call back with exact number of the bad inode and
502810bd68SDarrick J. Wong  * we can send back an error code.
512810bd68SDarrick J. Wong  *
522810bd68SDarrick J. Wong  * Note that if the formatter tells us there's no space left in the buffer we
532810bd68SDarrick J. Wong  * move the cursor forward and abort the walk.
542810bd68SDarrick J. Wong  */
552810bd68SDarrick J. Wong STATIC int
xfs_bulkstat_one_int(struct xfs_mount * mp,struct mnt_idmap * idmap,struct xfs_trans * tp,xfs_ino_t ino,struct xfs_bstat_chunk * bc)567dce11dbSChristoph Hellwig xfs_bulkstat_one_int(
572810bd68SDarrick J. Wong 	struct xfs_mount	*mp,
58e67fe633SChristian Brauner 	struct mnt_idmap	*idmap,
592810bd68SDarrick J. Wong 	struct xfs_trans	*tp,
602810bd68SDarrick J. Wong 	xfs_ino_t		ino,
612810bd68SDarrick J. Wong 	struct xfs_bstat_chunk	*bc)
621da177e4SLinus Torvalds {
63f736d93dSChristoph Hellwig 	struct user_namespace	*sb_userns = mp->m_super->s_user_ns;
647dce11dbSChristoph Hellwig 	struct xfs_inode	*ip;		/* incore inode pointer */
653987848cSDave Chinner 	struct inode		*inode;
667035f972SDarrick J. Wong 	struct xfs_bulkstat	*buf = bc->buf;
67c3c4ecb5SChandan Babu R 	xfs_extnum_t		nextents;
682810bd68SDarrick J. Wong 	int			error = -EINVAL;
6942b7cc11SChristian Brauner 	vfsuid_t		vfsuid;
7042b7cc11SChristian Brauner 	vfsgid_t		vfsgid;
717dce11dbSChristoph Hellwig 
722810bd68SDarrick J. Wong 	if (xfs_internal_inum(mp, ino))
732810bd68SDarrick J. Wong 		goto out_advance;
747dce11dbSChristoph Hellwig 
752810bd68SDarrick J. Wong 	error = xfs_iget(mp, tp, ino,
765132ba8fSDave Chinner 			 (XFS_IGET_DONTCACHE | XFS_IGET_UNTRUSTED),
775132ba8fSDave Chinner 			 XFS_ILOCK_SHARED, &ip);
782810bd68SDarrick J. Wong 	if (error == -ENOENT || error == -EINVAL)
792810bd68SDarrick J. Wong 		goto out_advance;
808fe65776SJie Liu 	if (error)
812810bd68SDarrick J. Wong 		goto out;
821da177e4SLinus Torvalds 
83*537c013bSDarrick J. Wong 	/* Reload the incore unlinked list to avoid failure in inodegc. */
8483771c50SDarrick J. Wong 	if (xfs_inode_unlinked_incomplete(ip)) {
8583771c50SDarrick J. Wong 		error = xfs_inode_reload_unlinked_bucket(tp, ip);
8683771c50SDarrick J. Wong 		if (error) {
8783771c50SDarrick J. Wong 			xfs_iunlock(ip, XFS_ILOCK_SHARED);
88*537c013bSDarrick J. Wong 			xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
8983771c50SDarrick J. Wong 			xfs_irele(ip);
9083771c50SDarrick J. Wong 			return error;
9183771c50SDarrick J. Wong 		}
9283771c50SDarrick J. Wong 	}
9383771c50SDarrick J. Wong 
941da177e4SLinus Torvalds 	ASSERT(ip != NULL);
9592bfc6e7SChristoph Hellwig 	ASSERT(ip->i_imap.im_blkno != 0);
963987848cSDave Chinner 	inode = VFS_I(ip);
97e67fe633SChristian Brauner 	vfsuid = i_uid_into_vfsuid(idmap, inode);
98e67fe633SChristian Brauner 	vfsgid = i_gid_into_vfsgid(idmap, inode);
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds 	/* xfs_iget returns the following without needing
1011da177e4SLinus Torvalds 	 * further change.
1021da177e4SLinus Torvalds 	 */
103ceaf603cSChristoph Hellwig 	buf->bs_projectid = ip->i_projid;
1041da177e4SLinus Torvalds 	buf->bs_ino = ino;
10542b7cc11SChristian Brauner 	buf->bs_uid = from_kuid(sb_userns, vfsuid_into_kuid(vfsuid));
10642b7cc11SChristian Brauner 	buf->bs_gid = from_kgid(sb_userns, vfsgid_into_kgid(vfsgid));
10713d2c10bSChristoph Hellwig 	buf->bs_size = ip->i_disk_size;
1083987848cSDave Chinner 
10954d7b5c1SDave Chinner 	buf->bs_nlink = inode->i_nlink;
1107035f972SDarrick J. Wong 	buf->bs_atime = inode->i_atime.tv_sec;
1117035f972SDarrick J. Wong 	buf->bs_atime_nsec = inode->i_atime.tv_nsec;
1127035f972SDarrick J. Wong 	buf->bs_mtime = inode->i_mtime.tv_sec;
1137035f972SDarrick J. Wong 	buf->bs_mtime_nsec = inode->i_mtime.tv_nsec;
114a0a415e3SJeff Layton 	buf->bs_ctime = inode_get_ctime(inode).tv_sec;
115a0a415e3SJeff Layton 	buf->bs_ctime_nsec = inode_get_ctime(inode).tv_nsec;
1169e9a2674SDave Chinner 	buf->bs_gen = inode->i_generation;
117c19b3b05SDave Chinner 	buf->bs_mode = inode->i_mode;
1183987848cSDave Chinner 
1191da177e4SLinus Torvalds 	buf->bs_xflags = xfs_ip2xflags(ip);
120031474c2SChristoph Hellwig 	buf->bs_extsize_blks = ip->i_extsize;
121c3c4ecb5SChandan Babu R 
122c3c4ecb5SChandan Babu R 	nextents = xfs_ifork_nextents(&ip->i_df);
123c3c4ecb5SChandan Babu R 	if (!(bc->breq->flags & XFS_IBULK_NREXT64))
124c3c4ecb5SChandan Babu R 		buf->bs_extents = min(nextents, XFS_MAX_EXTCNT_DATA_FORK_SMALL);
125c3c4ecb5SChandan Babu R 	else
126c3c4ecb5SChandan Babu R 		buf->bs_extents64 = nextents;
127c3c4ecb5SChandan Babu R 
12889d139d5SDarrick J. Wong 	xfs_bulkstat_health(ip, buf);
1292ed5b09bSDarrick J. Wong 	buf->bs_aextents = xfs_ifork_nextents(&ip->i_af);
130c01147d9SDarrick J. Wong 	buf->bs_forkoff = xfs_inode_fork_boff(ip);
1317035f972SDarrick J. Wong 	buf->bs_version = XFS_BULKSTAT_VERSION_V5;
1321da177e4SLinus Torvalds 
13338c26bfdSDave Chinner 	if (xfs_has_v3inodes(mp)) {
134e98d5e88SChristoph Hellwig 		buf->bs_btime = ip->i_crtime.tv_sec;
135e98d5e88SChristoph Hellwig 		buf->bs_btime_nsec = ip->i_crtime.tv_nsec;
1363e09ab8fSChristoph Hellwig 		if (ip->i_diflags2 & XFS_DIFLAG2_COWEXTSIZE)
137b33ce57dSChristoph Hellwig 			buf->bs_cowextsize_blks = ip->i_cowextsize;
138f7ca3522SDarrick J. Wong 	}
139f7ca3522SDarrick J. Wong 
140f7e67b20SChristoph Hellwig 	switch (ip->i_df.if_format) {
1411da177e4SLinus Torvalds 	case XFS_DINODE_FMT_DEV:
14266f36464SChristoph Hellwig 		buf->bs_rdev = sysv_encode_dev(inode->i_rdev);
1431da177e4SLinus Torvalds 		buf->bs_blksize = BLKDEV_IOSIZE;
1441da177e4SLinus Torvalds 		buf->bs_blocks = 0;
1451da177e4SLinus Torvalds 		break;
1461da177e4SLinus Torvalds 	case XFS_DINODE_FMT_LOCAL:
1471da177e4SLinus Torvalds 		buf->bs_rdev = 0;
1481da177e4SLinus Torvalds 		buf->bs_blksize = mp->m_sb.sb_blocksize;
1491da177e4SLinus Torvalds 		buf->bs_blocks = 0;
1501da177e4SLinus Torvalds 		break;
1511da177e4SLinus Torvalds 	case XFS_DINODE_FMT_EXTENTS:
1521da177e4SLinus Torvalds 	case XFS_DINODE_FMT_BTREE:
1531da177e4SLinus Torvalds 		buf->bs_rdev = 0;
1541da177e4SLinus Torvalds 		buf->bs_blksize = mp->m_sb.sb_blocksize;
1556e73a545SChristoph Hellwig 		buf->bs_blocks = ip->i_nblocks + ip->i_delayed_blks;
1561da177e4SLinus Torvalds 		break;
1571da177e4SLinus Torvalds 	}
158f2d67614SChristoph Hellwig 	xfs_iunlock(ip, XFS_ILOCK_SHARED);
15944a8736bSDarrick J. Wong 	xfs_irele(ip);
1607dce11dbSChristoph Hellwig 
1612810bd68SDarrick J. Wong 	error = bc->formatter(bc->breq, buf);
162e7ee96dfSDarrick J. Wong 	if (error == -ECANCELED)
1632810bd68SDarrick J. Wong 		goto out_advance;
1642810bd68SDarrick J. Wong 	if (error)
1652810bd68SDarrick J. Wong 		goto out;
1667dce11dbSChristoph Hellwig 
1672810bd68SDarrick J. Wong out_advance:
1682810bd68SDarrick J. Wong 	/*
1692810bd68SDarrick J. Wong 	 * Advance the cursor to the inode that comes after the one we just
1702810bd68SDarrick J. Wong 	 * looked at.  We want the caller to move along if the bulkstat
1712810bd68SDarrick J. Wong 	 * information was copied successfully; if we tried to grab the inode
1722810bd68SDarrick J. Wong 	 * but it's no longer allocated; or if it's internal metadata.
1732810bd68SDarrick J. Wong 	 */
1742810bd68SDarrick J. Wong 	bc->breq->startino = ino + 1;
1752810bd68SDarrick J. Wong out:
1761da177e4SLinus Torvalds 	return error;
1771da177e4SLinus Torvalds }
1781da177e4SLinus Torvalds 
1792810bd68SDarrick J. Wong /* Bulkstat a single inode. */
1802ee4fa5cSsandeen@sandeen.net int
xfs_bulkstat_one(struct xfs_ibulk * breq,bulkstat_one_fmt_pf formatter)1812ee4fa5cSsandeen@sandeen.net xfs_bulkstat_one(
1822810bd68SDarrick J. Wong 	struct xfs_ibulk	*breq,
1832810bd68SDarrick J. Wong 	bulkstat_one_fmt_pf	formatter)
1842ee4fa5cSsandeen@sandeen.net {
1852810bd68SDarrick J. Wong 	struct xfs_bstat_chunk	bc = {
1862810bd68SDarrick J. Wong 		.formatter	= formatter,
1872810bd68SDarrick J. Wong 		.breq		= breq,
1882810bd68SDarrick J. Wong 	};
189a6343e4dSDarrick J. Wong 	struct xfs_trans	*tp;
1902810bd68SDarrick J. Wong 	int			error;
1912810bd68SDarrick J. Wong 
192e67fe633SChristian Brauner 	if (breq->idmap != &nop_mnt_idmap) {
1938723d5baSChristoph Hellwig 		xfs_warn_ratelimited(breq->mp,
1948723d5baSChristoph Hellwig 			"bulkstat not supported inside of idmapped mounts.");
1958723d5baSChristoph Hellwig 		return -EINVAL;
1968723d5baSChristoph Hellwig 	}
1978723d5baSChristoph Hellwig 
1982810bd68SDarrick J. Wong 	ASSERT(breq->icount == 1);
1992810bd68SDarrick J. Wong 
2007035f972SDarrick J. Wong 	bc.buf = kmem_zalloc(sizeof(struct xfs_bulkstat),
201707e0ddaSTetsuo Handa 			KM_MAYFAIL);
2022810bd68SDarrick J. Wong 	if (!bc.buf)
2032810bd68SDarrick J. Wong 		return -ENOMEM;
2042810bd68SDarrick J. Wong 
205a6343e4dSDarrick J. Wong 	/*
206a6343e4dSDarrick J. Wong 	 * Grab an empty transaction so that we can use its recursive buffer
207a6343e4dSDarrick J. Wong 	 * locking abilities to detect cycles in the inobt without deadlocking.
208a6343e4dSDarrick J. Wong 	 */
209a6343e4dSDarrick J. Wong 	error = xfs_trans_alloc_empty(breq->mp, &tp);
210a6343e4dSDarrick J. Wong 	if (error)
211a6343e4dSDarrick J. Wong 		goto out;
2122810bd68SDarrick J. Wong 
213e67fe633SChristian Brauner 	error = xfs_bulkstat_one_int(breq->mp, breq->idmap, tp,
214a6343e4dSDarrick J. Wong 			breq->startino, &bc);
215a6343e4dSDarrick J. Wong 	xfs_trans_cancel(tp);
216a6343e4dSDarrick J. Wong out:
2172810bd68SDarrick J. Wong 	kmem_free(bc.buf);
2182810bd68SDarrick J. Wong 
2192810bd68SDarrick J. Wong 	/*
2202810bd68SDarrick J. Wong 	 * If we reported one inode to userspace then we abort because we hit
2212810bd68SDarrick J. Wong 	 * the end of the buffer.  Don't leak that back to userspace.
2222810bd68SDarrick J. Wong 	 */
223e7ee96dfSDarrick J. Wong 	if (error == -ECANCELED)
2242810bd68SDarrick J. Wong 		error = 0;
2252810bd68SDarrick J. Wong 
2262810bd68SDarrick J. Wong 	return error;
2278b56f083SNathan Scott }
2288b56f083SNathan Scott 
229bf4a5af2SDave Chinner static int
xfs_bulkstat_iwalk(struct xfs_mount * mp,struct xfs_trans * tp,xfs_ino_t ino,void * data)2302810bd68SDarrick J. Wong xfs_bulkstat_iwalk(
2311e773c49SJie Liu 	struct xfs_mount	*mp,
2322810bd68SDarrick J. Wong 	struct xfs_trans	*tp,
2332810bd68SDarrick J. Wong 	xfs_ino_t		ino,
2342810bd68SDarrick J. Wong 	void			*data)
2351e773c49SJie Liu {
236f736d93dSChristoph Hellwig 	struct xfs_bstat_chunk	*bc = data;
2372810bd68SDarrick J. Wong 	int			error;
2381e773c49SJie Liu 
239e67fe633SChristian Brauner 	error = xfs_bulkstat_one_int(mp, bc->breq->idmap, tp, ino, data);
2402810bd68SDarrick J. Wong 	/* bulkstat just skips over missing inodes */
2412810bd68SDarrick J. Wong 	if (error == -ENOENT || error == -EINVAL)
2422810bd68SDarrick J. Wong 		return 0;
2431e773c49SJie Liu 	return error;
2441e773c49SJie Liu }
2451e773c49SJie Liu 
2461e773c49SJie Liu /*
2472810bd68SDarrick J. Wong  * Check the incoming lastino parameter.
2482810bd68SDarrick J. Wong  *
2492810bd68SDarrick J. Wong  * We allow any inode value that could map to physical space inside the
2502810bd68SDarrick J. Wong  * filesystem because if there are no inodes there, bulkstat moves on to the
2512810bd68SDarrick J. Wong  * next chunk.  In other words, the magic agino value of zero takes us to the
2522810bd68SDarrick J. Wong  * first chunk in the AG, and an agino value past the end of the AG takes us to
2532810bd68SDarrick J. Wong  * the first chunk in the next AG.
2542810bd68SDarrick J. Wong  *
2552810bd68SDarrick J. Wong  * Therefore we can end early if the requested inode is beyond the end of the
2562810bd68SDarrick J. Wong  * filesystem or doesn't map properly.
2571da177e4SLinus Torvalds  */
2582810bd68SDarrick J. Wong static inline bool
xfs_bulkstat_already_done(struct xfs_mount * mp,xfs_ino_t startino)2592810bd68SDarrick J. Wong xfs_bulkstat_already_done(
2602810bd68SDarrick J. Wong 	struct xfs_mount	*mp,
2612810bd68SDarrick J. Wong 	xfs_ino_t		startino)
2621da177e4SLinus Torvalds {
2632810bd68SDarrick J. Wong 	xfs_agnumber_t		agno = XFS_INO_TO_AGNO(mp, startino);
2642810bd68SDarrick J. Wong 	xfs_agino_t		agino = XFS_INO_TO_AGINO(mp, startino);
2651da177e4SLinus Torvalds 
2662810bd68SDarrick J. Wong 	return agno >= mp->m_sb.sb_agcount ||
2672810bd68SDarrick J. Wong 	       startino != XFS_AGINO_TO_INO(mp, agno, agino);
2682810bd68SDarrick J. Wong }
2692810bd68SDarrick J. Wong 
2702810bd68SDarrick J. Wong /* Return stat information in bulk (by-inode) for the filesystem. */
2712810bd68SDarrick J. Wong int
xfs_bulkstat(struct xfs_ibulk * breq,bulkstat_one_fmt_pf formatter)2722810bd68SDarrick J. Wong xfs_bulkstat(
2732810bd68SDarrick J. Wong 	struct xfs_ibulk	*breq,
2742810bd68SDarrick J. Wong 	bulkstat_one_fmt_pf	formatter)
2752810bd68SDarrick J. Wong {
2762810bd68SDarrick J. Wong 	struct xfs_bstat_chunk	bc = {
2772810bd68SDarrick J. Wong 		.formatter	= formatter,
2782810bd68SDarrick J. Wong 		.breq		= breq,
2792810bd68SDarrick J. Wong 	};
280a6343e4dSDarrick J. Wong 	struct xfs_trans	*tp;
2815b35d922SChandan Babu R 	unsigned int		iwalk_flags = 0;
2822810bd68SDarrick J. Wong 	int			error;
2832810bd68SDarrick J. Wong 
284e67fe633SChristian Brauner 	if (breq->idmap != &nop_mnt_idmap) {
285f736d93dSChristoph Hellwig 		xfs_warn_ratelimited(breq->mp,
286f736d93dSChristoph Hellwig 			"bulkstat not supported inside of idmapped mounts.");
287f736d93dSChristoph Hellwig 		return -EINVAL;
288f736d93dSChristoph Hellwig 	}
2892810bd68SDarrick J. Wong 	if (xfs_bulkstat_already_done(breq->mp, breq->startino))
2901da177e4SLinus Torvalds 		return 0;
291296dfd7fSJie Liu 
2927035f972SDarrick J. Wong 	bc.buf = kmem_zalloc(sizeof(struct xfs_bulkstat),
293707e0ddaSTetsuo Handa 			KM_MAYFAIL);
2942810bd68SDarrick J. Wong 	if (!bc.buf)
2952451337dSDave Chinner 		return -ENOMEM;
296bb3c7d29SNathan Scott 
297a6343e4dSDarrick J. Wong 	/*
298a6343e4dSDarrick J. Wong 	 * Grab an empty transaction so that we can use its recursive buffer
299a6343e4dSDarrick J. Wong 	 * locking abilities to detect cycles in the inobt without deadlocking.
300a6343e4dSDarrick J. Wong 	 */
301a6343e4dSDarrick J. Wong 	error = xfs_trans_alloc_empty(breq->mp, &tp);
302a6343e4dSDarrick J. Wong 	if (error)
303a6343e4dSDarrick J. Wong 		goto out;
3046e57c542SDave Chinner 
3055b35d922SChandan Babu R 	if (breq->flags & XFS_IBULK_SAME_AG)
3065b35d922SChandan Babu R 		iwalk_flags |= XFS_IWALK_SAME_AG;
3075b35d922SChandan Babu R 
3085b35d922SChandan Babu R 	error = xfs_iwalk(breq->mp, tp, breq->startino, iwalk_flags,
309a6343e4dSDarrick J. Wong 			xfs_bulkstat_iwalk, breq->icount, &bc);
310a6343e4dSDarrick J. Wong 	xfs_trans_cancel(tp);
311a6343e4dSDarrick J. Wong out:
3122810bd68SDarrick J. Wong 	kmem_free(bc.buf);
313febe3cbeSDave Chinner 
314cd57e594SLachlan McIlroy 	/*
315febe3cbeSDave Chinner 	 * We found some inodes, so clear the error status and return them.
316febe3cbeSDave Chinner 	 * The lastino pointer will point directly at the inode that triggered
317febe3cbeSDave Chinner 	 * any error that occurred, so on the next call the error will be
318febe3cbeSDave Chinner 	 * triggered again and propagated to userspace as there will be no
319febe3cbeSDave Chinner 	 * formatted inodes in the buffer.
320cd57e594SLachlan McIlroy 	 */
3212810bd68SDarrick J. Wong 	if (breq->ocount > 0)
322febe3cbeSDave Chinner 		error = 0;
323febe3cbeSDave Chinner 
324febe3cbeSDave Chinner 	return error;
3251da177e4SLinus Torvalds }
3261da177e4SLinus Torvalds 
3277035f972SDarrick J. Wong /* Convert bulkstat (v5) to bstat (v1). */
3287035f972SDarrick J. Wong void
xfs_bulkstat_to_bstat(struct xfs_mount * mp,struct xfs_bstat * bs1,const struct xfs_bulkstat * bstat)3297035f972SDarrick J. Wong xfs_bulkstat_to_bstat(
3307035f972SDarrick J. Wong 	struct xfs_mount		*mp,
3317035f972SDarrick J. Wong 	struct xfs_bstat		*bs1,
3327035f972SDarrick J. Wong 	const struct xfs_bulkstat	*bstat)
3337035f972SDarrick J. Wong {
3342e616d9fSDarrick J. Wong 	/* memset is needed here because of padding holes in the structure. */
3357035f972SDarrick J. Wong 	memset(bs1, 0, sizeof(struct xfs_bstat));
3367035f972SDarrick J. Wong 	bs1->bs_ino = bstat->bs_ino;
3377035f972SDarrick J. Wong 	bs1->bs_mode = bstat->bs_mode;
3387035f972SDarrick J. Wong 	bs1->bs_nlink = bstat->bs_nlink;
3397035f972SDarrick J. Wong 	bs1->bs_uid = bstat->bs_uid;
3407035f972SDarrick J. Wong 	bs1->bs_gid = bstat->bs_gid;
3417035f972SDarrick J. Wong 	bs1->bs_rdev = bstat->bs_rdev;
3427035f972SDarrick J. Wong 	bs1->bs_blksize = bstat->bs_blksize;
3437035f972SDarrick J. Wong 	bs1->bs_size = bstat->bs_size;
3447035f972SDarrick J. Wong 	bs1->bs_atime.tv_sec = bstat->bs_atime;
3457035f972SDarrick J. Wong 	bs1->bs_mtime.tv_sec = bstat->bs_mtime;
3467035f972SDarrick J. Wong 	bs1->bs_ctime.tv_sec = bstat->bs_ctime;
3477035f972SDarrick J. Wong 	bs1->bs_atime.tv_nsec = bstat->bs_atime_nsec;
3487035f972SDarrick J. Wong 	bs1->bs_mtime.tv_nsec = bstat->bs_mtime_nsec;
3497035f972SDarrick J. Wong 	bs1->bs_ctime.tv_nsec = bstat->bs_ctime_nsec;
3507035f972SDarrick J. Wong 	bs1->bs_blocks = bstat->bs_blocks;
3517035f972SDarrick J. Wong 	bs1->bs_xflags = bstat->bs_xflags;
3527035f972SDarrick J. Wong 	bs1->bs_extsize = XFS_FSB_TO_B(mp, bstat->bs_extsize_blks);
3537035f972SDarrick J. Wong 	bs1->bs_extents = bstat->bs_extents;
3547035f972SDarrick J. Wong 	bs1->bs_gen = bstat->bs_gen;
3557035f972SDarrick J. Wong 	bs1->bs_projid_lo = bstat->bs_projectid & 0xFFFF;
3567035f972SDarrick J. Wong 	bs1->bs_forkoff = bstat->bs_forkoff;
3577035f972SDarrick J. Wong 	bs1->bs_projid_hi = bstat->bs_projectid >> 16;
3587035f972SDarrick J. Wong 	bs1->bs_sick = bstat->bs_sick;
3597035f972SDarrick J. Wong 	bs1->bs_checked = bstat->bs_checked;
3607035f972SDarrick J. Wong 	bs1->bs_cowextsize = XFS_FSB_TO_B(mp, bstat->bs_cowextsize_blks);
3617035f972SDarrick J. Wong 	bs1->bs_dmevmask = 0;
3627035f972SDarrick J. Wong 	bs1->bs_dmstate = 0;
3637035f972SDarrick J. Wong 	bs1->bs_aextents = bstat->bs_aextents;
3647035f972SDarrick J. Wong }
3657035f972SDarrick J. Wong 
366677717fbSDarrick J. Wong struct xfs_inumbers_chunk {
367677717fbSDarrick J. Wong 	inumbers_fmt_pf		formatter;
368677717fbSDarrick J. Wong 	struct xfs_ibulk	*breq;
369677717fbSDarrick J. Wong };
370677717fbSDarrick J. Wong 
371677717fbSDarrick J. Wong /*
372677717fbSDarrick J. Wong  * INUMBERS
373677717fbSDarrick J. Wong  * ========
374677717fbSDarrick J. Wong  * This is how we export inode btree records to userspace, so that XFS tools
375677717fbSDarrick J. Wong  * can figure out where inodes are allocated.
376677717fbSDarrick J. Wong  */
377677717fbSDarrick J. Wong 
378677717fbSDarrick J. Wong /*
379677717fbSDarrick J. Wong  * Format the inode group structure and report it somewhere.
380677717fbSDarrick J. Wong  *
381677717fbSDarrick J. Wong  * Similar to xfs_bulkstat_one_int, lastino is the inode cursor as we walk
382677717fbSDarrick J. Wong  * through the filesystem so we move it forward unless there was a runtime
383677717fbSDarrick J. Wong  * error.  If the formatter tells us the buffer is now full we also move the
384677717fbSDarrick J. Wong  * cursor forward and abort the walk.
385677717fbSDarrick J. Wong  */
386677717fbSDarrick J. Wong STATIC int
xfs_inumbers_walk(struct xfs_mount * mp,struct xfs_trans * tp,xfs_agnumber_t agno,const struct xfs_inobt_rec_incore * irec,void * data)387677717fbSDarrick J. Wong xfs_inumbers_walk(
388677717fbSDarrick J. Wong 	struct xfs_mount	*mp,
389677717fbSDarrick J. Wong 	struct xfs_trans	*tp,
390677717fbSDarrick J. Wong 	xfs_agnumber_t		agno,
391677717fbSDarrick J. Wong 	const struct xfs_inobt_rec_incore *irec,
392677717fbSDarrick J. Wong 	void			*data)
393faa63e95SMichal Marek {
3945f19c7fcSDarrick J. Wong 	struct xfs_inumbers	inogrp = {
395677717fbSDarrick J. Wong 		.xi_startino	= XFS_AGINO_TO_INO(mp, agno, irec->ir_startino),
396677717fbSDarrick J. Wong 		.xi_alloccount	= irec->ir_count - irec->ir_freecount,
397677717fbSDarrick J. Wong 		.xi_allocmask	= ~irec->ir_free,
3985f19c7fcSDarrick J. Wong 		.xi_version	= XFS_INUMBERS_VERSION_V5,
399677717fbSDarrick J. Wong 	};
400677717fbSDarrick J. Wong 	struct xfs_inumbers_chunk *ic = data;
401677717fbSDarrick J. Wong 	int			error;
402677717fbSDarrick J. Wong 
403677717fbSDarrick J. Wong 	error = ic->formatter(ic->breq, &inogrp);
404e7ee96dfSDarrick J. Wong 	if (error && error != -ECANCELED)
405677717fbSDarrick J. Wong 		return error;
406677717fbSDarrick J. Wong 
4070df5c39bSDarrick J. Wong 	ic->breq->startino = XFS_AGINO_TO_INO(mp, agno, irec->ir_startino) +
4080df5c39bSDarrick J. Wong 			XFS_INODES_PER_CHUNK;
409677717fbSDarrick J. Wong 	return error;
410faa63e95SMichal Marek }
411faa63e95SMichal Marek 
4121da177e4SLinus Torvalds /*
4131da177e4SLinus Torvalds  * Return inode number table for the filesystem.
4141da177e4SLinus Torvalds  */
415677717fbSDarrick J. Wong int
xfs_inumbers(struct xfs_ibulk * breq,inumbers_fmt_pf formatter)4161da177e4SLinus Torvalds xfs_inumbers(
417677717fbSDarrick J. Wong 	struct xfs_ibulk	*breq,
418faa63e95SMichal Marek 	inumbers_fmt_pf		formatter)
4191da177e4SLinus Torvalds {
420677717fbSDarrick J. Wong 	struct xfs_inumbers_chunk ic = {
421677717fbSDarrick J. Wong 		.formatter	= formatter,
422677717fbSDarrick J. Wong 		.breq		= breq,
423677717fbSDarrick J. Wong 	};
424a6343e4dSDarrick J. Wong 	struct xfs_trans	*tp;
425549fa006SJie Liu 	int			error = 0;
4261da177e4SLinus Torvalds 
427677717fbSDarrick J. Wong 	if (xfs_bulkstat_already_done(breq->mp, breq->startino))
428677717fbSDarrick J. Wong 		return 0;
429549fa006SJie Liu 
430a6343e4dSDarrick J. Wong 	/*
431a6343e4dSDarrick J. Wong 	 * Grab an empty transaction so that we can use its recursive buffer
432a6343e4dSDarrick J. Wong 	 * locking abilities to detect cycles in the inobt without deadlocking.
433a6343e4dSDarrick J. Wong 	 */
434a6343e4dSDarrick J. Wong 	error = xfs_trans_alloc_empty(breq->mp, &tp);
435a6343e4dSDarrick J. Wong 	if (error)
436a6343e4dSDarrick J. Wong 		goto out;
437a6343e4dSDarrick J. Wong 
438a6343e4dSDarrick J. Wong 	error = xfs_inobt_walk(breq->mp, tp, breq->startino, breq->flags,
439677717fbSDarrick J. Wong 			xfs_inumbers_walk, breq->icount, &ic);
440a6343e4dSDarrick J. Wong 	xfs_trans_cancel(tp);
441a6343e4dSDarrick J. Wong out:
442549fa006SJie Liu 
443677717fbSDarrick J. Wong 	/*
444677717fbSDarrick J. Wong 	 * We found some inode groups, so clear the error status and return
445677717fbSDarrick J. Wong 	 * them.  The lastino pointer will point directly at the inode that
446677717fbSDarrick J. Wong 	 * triggered any error that occurred, so on the next call the error
447677717fbSDarrick J. Wong 	 * will be triggered again and propagated to userspace as there will be
448677717fbSDarrick J. Wong 	 * no formatted inode groups in the buffer.
449677717fbSDarrick J. Wong 	 */
450677717fbSDarrick J. Wong 	if (breq->ocount > 0)
451677717fbSDarrick J. Wong 		error = 0;
452c7cb51dcSJie Liu 
4531da177e4SLinus Torvalds 	return error;
4541da177e4SLinus Torvalds }
4555f19c7fcSDarrick J. Wong 
4565f19c7fcSDarrick J. Wong /* Convert an inumbers (v5) struct to a inogrp (v1) struct. */
4575f19c7fcSDarrick J. Wong void
xfs_inumbers_to_inogrp(struct xfs_inogrp * ig1,const struct xfs_inumbers * ig)4585f19c7fcSDarrick J. Wong xfs_inumbers_to_inogrp(
4595f19c7fcSDarrick J. Wong 	struct xfs_inogrp		*ig1,
4605f19c7fcSDarrick J. Wong 	const struct xfs_inumbers	*ig)
4615f19c7fcSDarrick J. Wong {
4622e616d9fSDarrick J. Wong 	/* memset is needed here because of padding holes in the structure. */
4632e616d9fSDarrick J. Wong 	memset(ig1, 0, sizeof(struct xfs_inogrp));
4645f19c7fcSDarrick J. Wong 	ig1->xi_startino = ig->xi_startino;
4655f19c7fcSDarrick J. Wong 	ig1->xi_alloccount = ig->xi_alloccount;
4665f19c7fcSDarrick J. Wong 	ig1->xi_allocmask = ig->xi_allocmask;
4675f19c7fcSDarrick J. Wong }
468