xref: /openbmc/linux/fs/xfs/xfs_itable.c (revision f7e67b20ecbbcb9180c888a5c4fde267935e075f)
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"
221da177e4SLinus Torvalds 
237dce11dbSChristoph Hellwig /*
242810bd68SDarrick J. Wong  * Bulk Stat
252810bd68SDarrick J. Wong  * =========
262810bd68SDarrick J. Wong  *
277035f972SDarrick J. Wong  * Use the inode walking functions to fill out struct xfs_bulkstat for every
282810bd68SDarrick J. Wong  * allocated inode, then pass the stat information to some externally provided
292810bd68SDarrick J. Wong  * iteration function.
307dce11dbSChristoph Hellwig  */
312810bd68SDarrick J. Wong 
322810bd68SDarrick J. Wong struct xfs_bstat_chunk {
332810bd68SDarrick J. Wong 	bulkstat_one_fmt_pf	formatter;
342810bd68SDarrick J. Wong 	struct xfs_ibulk	*breq;
357035f972SDarrick J. Wong 	struct xfs_bulkstat	*buf;
362810bd68SDarrick J. Wong };
372810bd68SDarrick J. Wong 
382810bd68SDarrick J. Wong /*
392810bd68SDarrick J. Wong  * Fill out the bulkstat info for a single inode and report it somewhere.
402810bd68SDarrick J. Wong  *
412810bd68SDarrick J. Wong  * bc->breq->lastino is effectively the inode cursor as we walk through the
422810bd68SDarrick J. Wong  * filesystem.  Therefore, we update it any time we need to move the cursor
432810bd68SDarrick J. Wong  * forward, regardless of whether or not we're sending any bstat information
442810bd68SDarrick J. Wong  * back to userspace.  If the inode is internal metadata or, has been freed
452810bd68SDarrick J. Wong  * out from under us, we just simply keep going.
462810bd68SDarrick J. Wong  *
472810bd68SDarrick J. Wong  * However, if any other type of error happens we want to stop right where we
482810bd68SDarrick J. Wong  * are so that userspace will call back with exact number of the bad inode and
492810bd68SDarrick J. Wong  * we can send back an error code.
502810bd68SDarrick J. Wong  *
512810bd68SDarrick J. Wong  * Note that if the formatter tells us there's no space left in the buffer we
522810bd68SDarrick J. Wong  * move the cursor forward and abort the walk.
532810bd68SDarrick J. Wong  */
542810bd68SDarrick J. Wong STATIC int
557dce11dbSChristoph Hellwig xfs_bulkstat_one_int(
562810bd68SDarrick J. Wong 	struct xfs_mount	*mp,
572810bd68SDarrick J. Wong 	struct xfs_trans	*tp,
582810bd68SDarrick J. Wong 	xfs_ino_t		ino,
592810bd68SDarrick J. Wong 	struct xfs_bstat_chunk	*bc)
601da177e4SLinus Torvalds {
617dce11dbSChristoph Hellwig 	struct xfs_icdinode	*dic;		/* dinode core info pointer */
627dce11dbSChristoph Hellwig 	struct xfs_inode	*ip;		/* incore inode pointer */
633987848cSDave Chinner 	struct inode		*inode;
647035f972SDarrick J. Wong 	struct xfs_bulkstat	*buf = bc->buf;
652810bd68SDarrick J. Wong 	int			error = -EINVAL;
667dce11dbSChristoph Hellwig 
672810bd68SDarrick J. Wong 	if (xfs_internal_inum(mp, ino))
682810bd68SDarrick J. Wong 		goto out_advance;
697dce11dbSChristoph Hellwig 
702810bd68SDarrick J. Wong 	error = xfs_iget(mp, tp, ino,
715132ba8fSDave Chinner 			 (XFS_IGET_DONTCACHE | XFS_IGET_UNTRUSTED),
725132ba8fSDave Chinner 			 XFS_ILOCK_SHARED, &ip);
732810bd68SDarrick J. Wong 	if (error == -ENOENT || error == -EINVAL)
742810bd68SDarrick J. Wong 		goto out_advance;
758fe65776SJie Liu 	if (error)
762810bd68SDarrick J. Wong 		goto out;
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds 	ASSERT(ip != NULL);
7992bfc6e7SChristoph Hellwig 	ASSERT(ip->i_imap.im_blkno != 0);
803987848cSDave Chinner 	inode = VFS_I(ip);
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds 	dic = &ip->i_d;
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds 	/* xfs_iget returns the following without needing
851da177e4SLinus Torvalds 	 * further change.
861da177e4SLinus Torvalds 	 */
87de7a866fSChristoph Hellwig 	buf->bs_projectid = ip->i_d.di_projid;
881da177e4SLinus Torvalds 	buf->bs_ino = ino;
89ba8adad5SChristoph Hellwig 	buf->bs_uid = i_uid_read(inode);
90ba8adad5SChristoph Hellwig 	buf->bs_gid = i_gid_read(inode);
911da177e4SLinus Torvalds 	buf->bs_size = dic->di_size;
923987848cSDave Chinner 
9354d7b5c1SDave Chinner 	buf->bs_nlink = inode->i_nlink;
947035f972SDarrick J. Wong 	buf->bs_atime = inode->i_atime.tv_sec;
957035f972SDarrick J. Wong 	buf->bs_atime_nsec = inode->i_atime.tv_nsec;
967035f972SDarrick J. Wong 	buf->bs_mtime = inode->i_mtime.tv_sec;
977035f972SDarrick J. Wong 	buf->bs_mtime_nsec = inode->i_mtime.tv_nsec;
987035f972SDarrick J. Wong 	buf->bs_ctime = inode->i_ctime.tv_sec;
997035f972SDarrick J. Wong 	buf->bs_ctime_nsec = inode->i_ctime.tv_nsec;
1008d2d878dSChristoph Hellwig 	buf->bs_btime = dic->di_crtime.tv_sec;
1018d2d878dSChristoph Hellwig 	buf->bs_btime_nsec = dic->di_crtime.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);
1067035f972SDarrick J. Wong 	buf->bs_extsize_blks = dic->di_extsize;
107daf83964SChristoph Hellwig 	buf->bs_extents = xfs_ifork_nextents(&ip->i_df);
10889d139d5SDarrick J. Wong 	xfs_bulkstat_health(ip, buf);
109daf83964SChristoph Hellwig 	buf->bs_aextents = xfs_ifork_nextents(ip->i_afp);
11007000ee6SDave Chinner 	buf->bs_forkoff = XFS_IFORK_BOFF(ip);
1117035f972SDarrick J. Wong 	buf->bs_version = XFS_BULKSTAT_VERSION_V5;
1121da177e4SLinus Torvalds 
1136471e9c5SChristoph Hellwig 	if (xfs_sb_version_has_v3inode(&mp->m_sb)) {
114f7ca3522SDarrick J. Wong 		if (dic->di_flags2 & XFS_DIFLAG2_COWEXTSIZE)
1157035f972SDarrick J. Wong 			buf->bs_cowextsize_blks = dic->di_cowextsize;
116f7ca3522SDarrick J. Wong 	}
117f7ca3522SDarrick J. Wong 
118*f7e67b20SChristoph Hellwig 	switch (ip->i_df.if_format) {
1191da177e4SLinus Torvalds 	case XFS_DINODE_FMT_DEV:
12066f36464SChristoph Hellwig 		buf->bs_rdev = sysv_encode_dev(inode->i_rdev);
1211da177e4SLinus Torvalds 		buf->bs_blksize = BLKDEV_IOSIZE;
1221da177e4SLinus Torvalds 		buf->bs_blocks = 0;
1231da177e4SLinus Torvalds 		break;
1241da177e4SLinus Torvalds 	case XFS_DINODE_FMT_LOCAL:
1251da177e4SLinus Torvalds 		buf->bs_rdev = 0;
1261da177e4SLinus Torvalds 		buf->bs_blksize = mp->m_sb.sb_blocksize;
1271da177e4SLinus Torvalds 		buf->bs_blocks = 0;
1281da177e4SLinus Torvalds 		break;
1291da177e4SLinus Torvalds 	case XFS_DINODE_FMT_EXTENTS:
1301da177e4SLinus Torvalds 	case XFS_DINODE_FMT_BTREE:
1311da177e4SLinus Torvalds 		buf->bs_rdev = 0;
1321da177e4SLinus Torvalds 		buf->bs_blksize = mp->m_sb.sb_blocksize;
1331da177e4SLinus Torvalds 		buf->bs_blocks = dic->di_nblocks + ip->i_delayed_blks;
1341da177e4SLinus Torvalds 		break;
1351da177e4SLinus Torvalds 	}
136f2d67614SChristoph Hellwig 	xfs_iunlock(ip, XFS_ILOCK_SHARED);
13744a8736bSDarrick J. Wong 	xfs_irele(ip);
1387dce11dbSChristoph Hellwig 
1392810bd68SDarrick J. Wong 	error = bc->formatter(bc->breq, buf);
140e7ee96dfSDarrick J. Wong 	if (error == -ECANCELED)
1412810bd68SDarrick J. Wong 		goto out_advance;
1422810bd68SDarrick J. Wong 	if (error)
1432810bd68SDarrick J. Wong 		goto out;
1447dce11dbSChristoph Hellwig 
1452810bd68SDarrick J. Wong out_advance:
1462810bd68SDarrick J. Wong 	/*
1472810bd68SDarrick J. Wong 	 * Advance the cursor to the inode that comes after the one we just
1482810bd68SDarrick J. Wong 	 * looked at.  We want the caller to move along if the bulkstat
1492810bd68SDarrick J. Wong 	 * information was copied successfully; if we tried to grab the inode
1502810bd68SDarrick J. Wong 	 * but it's no longer allocated; or if it's internal metadata.
1512810bd68SDarrick J. Wong 	 */
1522810bd68SDarrick J. Wong 	bc->breq->startino = ino + 1;
1532810bd68SDarrick J. Wong out:
1541da177e4SLinus Torvalds 	return error;
1551da177e4SLinus Torvalds }
1561da177e4SLinus Torvalds 
1572810bd68SDarrick J. Wong /* Bulkstat a single inode. */
1582ee4fa5cSsandeen@sandeen.net int
1592ee4fa5cSsandeen@sandeen.net xfs_bulkstat_one(
1602810bd68SDarrick J. Wong 	struct xfs_ibulk	*breq,
1612810bd68SDarrick J. Wong 	bulkstat_one_fmt_pf	formatter)
1622ee4fa5cSsandeen@sandeen.net {
1632810bd68SDarrick J. Wong 	struct xfs_bstat_chunk	bc = {
1642810bd68SDarrick J. Wong 		.formatter	= formatter,
1652810bd68SDarrick J. Wong 		.breq		= breq,
1662810bd68SDarrick J. Wong 	};
1672810bd68SDarrick J. Wong 	int			error;
1682810bd68SDarrick J. Wong 
1692810bd68SDarrick J. Wong 	ASSERT(breq->icount == 1);
1702810bd68SDarrick J. Wong 
1717035f972SDarrick J. Wong 	bc.buf = kmem_zalloc(sizeof(struct xfs_bulkstat),
172707e0ddaSTetsuo Handa 			KM_MAYFAIL);
1732810bd68SDarrick J. Wong 	if (!bc.buf)
1742810bd68SDarrick J. Wong 		return -ENOMEM;
1752810bd68SDarrick J. Wong 
1762810bd68SDarrick J. Wong 	error = xfs_bulkstat_one_int(breq->mp, NULL, breq->startino, &bc);
1772810bd68SDarrick J. Wong 
1782810bd68SDarrick J. Wong 	kmem_free(bc.buf);
1792810bd68SDarrick J. Wong 
1802810bd68SDarrick J. Wong 	/*
1812810bd68SDarrick J. Wong 	 * If we reported one inode to userspace then we abort because we hit
1822810bd68SDarrick J. Wong 	 * the end of the buffer.  Don't leak that back to userspace.
1832810bd68SDarrick J. Wong 	 */
184e7ee96dfSDarrick J. Wong 	if (error == -ECANCELED)
1852810bd68SDarrick J. Wong 		error = 0;
1862810bd68SDarrick J. Wong 
1872810bd68SDarrick J. Wong 	return error;
1888b56f083SNathan Scott }
1898b56f083SNathan Scott 
190bf4a5af2SDave Chinner static int
1912810bd68SDarrick J. Wong xfs_bulkstat_iwalk(
1921e773c49SJie Liu 	struct xfs_mount	*mp,
1932810bd68SDarrick J. Wong 	struct xfs_trans	*tp,
1942810bd68SDarrick J. Wong 	xfs_ino_t		ino,
1952810bd68SDarrick J. Wong 	void			*data)
1961e773c49SJie Liu {
1972810bd68SDarrick J. Wong 	int			error;
1981e773c49SJie Liu 
1992810bd68SDarrick J. Wong 	error = xfs_bulkstat_one_int(mp, tp, ino, data);
2002810bd68SDarrick J. Wong 	/* bulkstat just skips over missing inodes */
2012810bd68SDarrick J. Wong 	if (error == -ENOENT || error == -EINVAL)
2022810bd68SDarrick J. Wong 		return 0;
2031e773c49SJie Liu 	return error;
2041e773c49SJie Liu }
2051e773c49SJie Liu 
2061e773c49SJie Liu /*
2072810bd68SDarrick J. Wong  * Check the incoming lastino parameter.
2082810bd68SDarrick J. Wong  *
2092810bd68SDarrick J. Wong  * We allow any inode value that could map to physical space inside the
2102810bd68SDarrick J. Wong  * filesystem because if there are no inodes there, bulkstat moves on to the
2112810bd68SDarrick J. Wong  * next chunk.  In other words, the magic agino value of zero takes us to the
2122810bd68SDarrick J. Wong  * first chunk in the AG, and an agino value past the end of the AG takes us to
2132810bd68SDarrick J. Wong  * the first chunk in the next AG.
2142810bd68SDarrick J. Wong  *
2152810bd68SDarrick J. Wong  * Therefore we can end early if the requested inode is beyond the end of the
2162810bd68SDarrick J. Wong  * filesystem or doesn't map properly.
2171da177e4SLinus Torvalds  */
2182810bd68SDarrick J. Wong static inline bool
2192810bd68SDarrick J. Wong xfs_bulkstat_already_done(
2202810bd68SDarrick J. Wong 	struct xfs_mount	*mp,
2212810bd68SDarrick J. Wong 	xfs_ino_t		startino)
2221da177e4SLinus Torvalds {
2232810bd68SDarrick J. Wong 	xfs_agnumber_t		agno = XFS_INO_TO_AGNO(mp, startino);
2242810bd68SDarrick J. Wong 	xfs_agino_t		agino = XFS_INO_TO_AGINO(mp, startino);
2251da177e4SLinus Torvalds 
2262810bd68SDarrick J. Wong 	return agno >= mp->m_sb.sb_agcount ||
2272810bd68SDarrick J. Wong 	       startino != XFS_AGINO_TO_INO(mp, agno, agino);
2282810bd68SDarrick J. Wong }
2292810bd68SDarrick J. Wong 
2302810bd68SDarrick J. Wong /* Return stat information in bulk (by-inode) for the filesystem. */
2312810bd68SDarrick J. Wong int
2322810bd68SDarrick J. Wong xfs_bulkstat(
2332810bd68SDarrick J. Wong 	struct xfs_ibulk	*breq,
2342810bd68SDarrick J. Wong 	bulkstat_one_fmt_pf	formatter)
2352810bd68SDarrick J. Wong {
2362810bd68SDarrick J. Wong 	struct xfs_bstat_chunk	bc = {
2372810bd68SDarrick J. Wong 		.formatter	= formatter,
2382810bd68SDarrick J. Wong 		.breq		= breq,
2392810bd68SDarrick J. Wong 	};
2402810bd68SDarrick J. Wong 	int			error;
2412810bd68SDarrick J. Wong 
2422810bd68SDarrick J. Wong 	if (xfs_bulkstat_already_done(breq->mp, breq->startino))
2431da177e4SLinus Torvalds 		return 0;
244296dfd7fSJie Liu 
2457035f972SDarrick J. Wong 	bc.buf = kmem_zalloc(sizeof(struct xfs_bulkstat),
246707e0ddaSTetsuo Handa 			KM_MAYFAIL);
2472810bd68SDarrick J. Wong 	if (!bc.buf)
2482451337dSDave Chinner 		return -ENOMEM;
249bb3c7d29SNathan Scott 
25013d59a2aSDarrick J. Wong 	error = xfs_iwalk(breq->mp, NULL, breq->startino, breq->flags,
25113d59a2aSDarrick J. Wong 			xfs_bulkstat_iwalk, breq->icount, &bc);
2526e57c542SDave Chinner 
2532810bd68SDarrick J. Wong 	kmem_free(bc.buf);
254febe3cbeSDave Chinner 
255cd57e594SLachlan McIlroy 	/*
256febe3cbeSDave Chinner 	 * We found some inodes, so clear the error status and return them.
257febe3cbeSDave Chinner 	 * The lastino pointer will point directly at the inode that triggered
258febe3cbeSDave Chinner 	 * any error that occurred, so on the next call the error will be
259febe3cbeSDave Chinner 	 * triggered again and propagated to userspace as there will be no
260febe3cbeSDave Chinner 	 * formatted inodes in the buffer.
261cd57e594SLachlan McIlroy 	 */
2622810bd68SDarrick J. Wong 	if (breq->ocount > 0)
263febe3cbeSDave Chinner 		error = 0;
264febe3cbeSDave Chinner 
265febe3cbeSDave Chinner 	return error;
2661da177e4SLinus Torvalds }
2671da177e4SLinus Torvalds 
2687035f972SDarrick J. Wong /* Convert bulkstat (v5) to bstat (v1). */
2697035f972SDarrick J. Wong void
2707035f972SDarrick J. Wong xfs_bulkstat_to_bstat(
2717035f972SDarrick J. Wong 	struct xfs_mount		*mp,
2727035f972SDarrick J. Wong 	struct xfs_bstat		*bs1,
2737035f972SDarrick J. Wong 	const struct xfs_bulkstat	*bstat)
2747035f972SDarrick J. Wong {
2752e616d9fSDarrick J. Wong 	/* memset is needed here because of padding holes in the structure. */
2767035f972SDarrick J. Wong 	memset(bs1, 0, sizeof(struct xfs_bstat));
2777035f972SDarrick J. Wong 	bs1->bs_ino = bstat->bs_ino;
2787035f972SDarrick J. Wong 	bs1->bs_mode = bstat->bs_mode;
2797035f972SDarrick J. Wong 	bs1->bs_nlink = bstat->bs_nlink;
2807035f972SDarrick J. Wong 	bs1->bs_uid = bstat->bs_uid;
2817035f972SDarrick J. Wong 	bs1->bs_gid = bstat->bs_gid;
2827035f972SDarrick J. Wong 	bs1->bs_rdev = bstat->bs_rdev;
2837035f972SDarrick J. Wong 	bs1->bs_blksize = bstat->bs_blksize;
2847035f972SDarrick J. Wong 	bs1->bs_size = bstat->bs_size;
2857035f972SDarrick J. Wong 	bs1->bs_atime.tv_sec = bstat->bs_atime;
2867035f972SDarrick J. Wong 	bs1->bs_mtime.tv_sec = bstat->bs_mtime;
2877035f972SDarrick J. Wong 	bs1->bs_ctime.tv_sec = bstat->bs_ctime;
2887035f972SDarrick J. Wong 	bs1->bs_atime.tv_nsec = bstat->bs_atime_nsec;
2897035f972SDarrick J. Wong 	bs1->bs_mtime.tv_nsec = bstat->bs_mtime_nsec;
2907035f972SDarrick J. Wong 	bs1->bs_ctime.tv_nsec = bstat->bs_ctime_nsec;
2917035f972SDarrick J. Wong 	bs1->bs_blocks = bstat->bs_blocks;
2927035f972SDarrick J. Wong 	bs1->bs_xflags = bstat->bs_xflags;
2937035f972SDarrick J. Wong 	bs1->bs_extsize = XFS_FSB_TO_B(mp, bstat->bs_extsize_blks);
2947035f972SDarrick J. Wong 	bs1->bs_extents = bstat->bs_extents;
2957035f972SDarrick J. Wong 	bs1->bs_gen = bstat->bs_gen;
2967035f972SDarrick J. Wong 	bs1->bs_projid_lo = bstat->bs_projectid & 0xFFFF;
2977035f972SDarrick J. Wong 	bs1->bs_forkoff = bstat->bs_forkoff;
2987035f972SDarrick J. Wong 	bs1->bs_projid_hi = bstat->bs_projectid >> 16;
2997035f972SDarrick J. Wong 	bs1->bs_sick = bstat->bs_sick;
3007035f972SDarrick J. Wong 	bs1->bs_checked = bstat->bs_checked;
3017035f972SDarrick J. Wong 	bs1->bs_cowextsize = XFS_FSB_TO_B(mp, bstat->bs_cowextsize_blks);
3027035f972SDarrick J. Wong 	bs1->bs_dmevmask = 0;
3037035f972SDarrick J. Wong 	bs1->bs_dmstate = 0;
3047035f972SDarrick J. Wong 	bs1->bs_aextents = bstat->bs_aextents;
3057035f972SDarrick J. Wong }
3067035f972SDarrick J. Wong 
307677717fbSDarrick J. Wong struct xfs_inumbers_chunk {
308677717fbSDarrick J. Wong 	inumbers_fmt_pf		formatter;
309677717fbSDarrick J. Wong 	struct xfs_ibulk	*breq;
310677717fbSDarrick J. Wong };
311677717fbSDarrick J. Wong 
312677717fbSDarrick J. Wong /*
313677717fbSDarrick J. Wong  * INUMBERS
314677717fbSDarrick J. Wong  * ========
315677717fbSDarrick J. Wong  * This is how we export inode btree records to userspace, so that XFS tools
316677717fbSDarrick J. Wong  * can figure out where inodes are allocated.
317677717fbSDarrick J. Wong  */
318677717fbSDarrick J. Wong 
319677717fbSDarrick J. Wong /*
320677717fbSDarrick J. Wong  * Format the inode group structure and report it somewhere.
321677717fbSDarrick J. Wong  *
322677717fbSDarrick J. Wong  * Similar to xfs_bulkstat_one_int, lastino is the inode cursor as we walk
323677717fbSDarrick J. Wong  * through the filesystem so we move it forward unless there was a runtime
324677717fbSDarrick J. Wong  * error.  If the formatter tells us the buffer is now full we also move the
325677717fbSDarrick J. Wong  * cursor forward and abort the walk.
326677717fbSDarrick J. Wong  */
327677717fbSDarrick J. Wong STATIC int
328677717fbSDarrick J. Wong xfs_inumbers_walk(
329677717fbSDarrick J. Wong 	struct xfs_mount	*mp,
330677717fbSDarrick J. Wong 	struct xfs_trans	*tp,
331677717fbSDarrick J. Wong 	xfs_agnumber_t		agno,
332677717fbSDarrick J. Wong 	const struct xfs_inobt_rec_incore *irec,
333677717fbSDarrick J. Wong 	void			*data)
334faa63e95SMichal Marek {
3355f19c7fcSDarrick J. Wong 	struct xfs_inumbers	inogrp = {
336677717fbSDarrick J. Wong 		.xi_startino	= XFS_AGINO_TO_INO(mp, agno, irec->ir_startino),
337677717fbSDarrick J. Wong 		.xi_alloccount	= irec->ir_count - irec->ir_freecount,
338677717fbSDarrick J. Wong 		.xi_allocmask	= ~irec->ir_free,
3395f19c7fcSDarrick J. Wong 		.xi_version	= XFS_INUMBERS_VERSION_V5,
340677717fbSDarrick J. Wong 	};
341677717fbSDarrick J. Wong 	struct xfs_inumbers_chunk *ic = data;
342677717fbSDarrick J. Wong 	int			error;
343677717fbSDarrick J. Wong 
344677717fbSDarrick J. Wong 	error = ic->formatter(ic->breq, &inogrp);
345e7ee96dfSDarrick J. Wong 	if (error && error != -ECANCELED)
346677717fbSDarrick J. Wong 		return error;
347677717fbSDarrick J. Wong 
3480df5c39bSDarrick J. Wong 	ic->breq->startino = XFS_AGINO_TO_INO(mp, agno, irec->ir_startino) +
3490df5c39bSDarrick J. Wong 			XFS_INODES_PER_CHUNK;
350677717fbSDarrick J. Wong 	return error;
351faa63e95SMichal Marek }
352faa63e95SMichal Marek 
3531da177e4SLinus Torvalds /*
3541da177e4SLinus Torvalds  * Return inode number table for the filesystem.
3551da177e4SLinus Torvalds  */
356677717fbSDarrick J. Wong int
3571da177e4SLinus Torvalds xfs_inumbers(
358677717fbSDarrick J. Wong 	struct xfs_ibulk	*breq,
359faa63e95SMichal Marek 	inumbers_fmt_pf		formatter)
3601da177e4SLinus Torvalds {
361677717fbSDarrick J. Wong 	struct xfs_inumbers_chunk ic = {
362677717fbSDarrick J. Wong 		.formatter	= formatter,
363677717fbSDarrick J. Wong 		.breq		= breq,
364677717fbSDarrick J. Wong 	};
365549fa006SJie Liu 	int			error = 0;
3661da177e4SLinus Torvalds 
367677717fbSDarrick J. Wong 	if (xfs_bulkstat_already_done(breq->mp, breq->startino))
368677717fbSDarrick J. Wong 		return 0;
369549fa006SJie Liu 
37013d59a2aSDarrick J. Wong 	error = xfs_inobt_walk(breq->mp, NULL, breq->startino, breq->flags,
371677717fbSDarrick J. Wong 			xfs_inumbers_walk, breq->icount, &ic);
372549fa006SJie Liu 
373677717fbSDarrick J. Wong 	/*
374677717fbSDarrick J. Wong 	 * We found some inode groups, so clear the error status and return
375677717fbSDarrick J. Wong 	 * them.  The lastino pointer will point directly at the inode that
376677717fbSDarrick J. Wong 	 * triggered any error that occurred, so on the next call the error
377677717fbSDarrick J. Wong 	 * will be triggered again and propagated to userspace as there will be
378677717fbSDarrick J. Wong 	 * no formatted inode groups in the buffer.
379677717fbSDarrick J. Wong 	 */
380677717fbSDarrick J. Wong 	if (breq->ocount > 0)
381677717fbSDarrick J. Wong 		error = 0;
382c7cb51dcSJie Liu 
3831da177e4SLinus Torvalds 	return error;
3841da177e4SLinus Torvalds }
3855f19c7fcSDarrick J. Wong 
3865f19c7fcSDarrick J. Wong /* Convert an inumbers (v5) struct to a inogrp (v1) struct. */
3875f19c7fcSDarrick J. Wong void
3885f19c7fcSDarrick J. Wong xfs_inumbers_to_inogrp(
3895f19c7fcSDarrick J. Wong 	struct xfs_inogrp		*ig1,
3905f19c7fcSDarrick J. Wong 	const struct xfs_inumbers	*ig)
3915f19c7fcSDarrick J. Wong {
3922e616d9fSDarrick J. Wong 	/* memset is needed here because of padding holes in the structure. */
3932e616d9fSDarrick J. Wong 	memset(ig1, 0, sizeof(struct xfs_inogrp));
3945f19c7fcSDarrick J. Wong 	ig1->xi_startino = ig->xi_startino;
3955f19c7fcSDarrick J. Wong 	ig1->xi_alloccount = ig->xi_alloccount;
3965f19c7fcSDarrick J. Wong 	ig1->xi_allocmask = ig->xi_allocmask;
3975f19c7fcSDarrick J. Wong }
398