From d0e8bb48b7a607d849d21f9b74d62ba460cef006 Mon Sep 17 00:00:00 2001 From: Eric Sandeen Date: Mon, 3 Oct 2016 10:46:03 +1100 Subject: [PATCH] xfs_io: factor out new get_last_inode() helper The inode command by default finds the last allocated inode in the filesystem via bulkstat, and this specific function is open-coded after other cases are handled, leading to a fairly long inode_f function and confusing code flow. Clean it up by factoring it into a new function, more refactoring will follow. Signed-off-by: Eric Sandeen Reviewed-by: Dave Chinner Signed-off-by: Dave Chinner --- io/open.c | 68 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 44 insertions(+), 24 deletions(-) diff --git a/io/open.c b/io/open.c index 5efa739d8..c8c82b773 100644 --- a/io/open.c +++ b/io/open.c @@ -767,14 +767,51 @@ inode_help(void) "\n")); } +static __u64 +get_last_inode(void) +{ + __u64 lastip = 0; + __u64 lastgrp = 0; + __s32 ocount = 0; + __u64 last_ino; + struct xfs_inogrp igroup[1024]; + struct xfs_fsop_bulkreq bulkreq; + + bulkreq.lastip = &lastip; + bulkreq.ubuffer = &igroup; + bulkreq.icount = sizeof(igroup) / sizeof(struct xfs_inogrp); + bulkreq.ocount = &ocount; + + for (;;) { + if (xfsctl(file->name, file->fd, XFS_IOC_FSINUMBERS, + &bulkreq)) { + perror("XFS_IOC_FSINUMBERS"); + return 0; + } + + /* Did we reach the last inode? */ + if (ocount == 0) + break; + + /* last inode in igroup table */ + lastgrp = ocount; + } + + lastgrp--; + + /* The last inode number in use */ + last_ino = igroup[lastgrp].xi_startino + + libxfs_highbit64(igroup[lastgrp].xi_allocmask); + + return last_ino; +} + static int inode_f( int argc, char **argv) { __s32 count = 0; - __s32 lastgrp = 0; - __u64 last = 0; __u64 lastino = 0; __u64 userino = 0; char *p; @@ -782,7 +819,6 @@ inode_f( int verbose = 0; int ret_next = 0; int cmd = 0; - struct xfs_inogrp igroup[1024]; struct xfs_fsop_bulkreq bulkreq; struct xfs_bstat bstat; @@ -854,29 +890,13 @@ inode_f( return command_usage(&inode_cmd); } - bulkreq.lastip = &last; - bulkreq.icount = 1024; /* User-defined maybe!? */ - bulkreq.ubuffer = &igroup; - bulkreq.ocount = &count; - - for (;;) { - if (xfsctl(file->name, file->fd, XFS_IOC_FSINUMBERS, - &bulkreq)) { - perror("XFS_IOC_FSINUMBERS"); - exitcode = 1; - return 0; - } - - if (count == 0) - break; - - lastgrp = count; + /* We are finding last inode in use */ + lastino = get_last_inode(); + if (!lastino) { + exitcode = 1; + return 0; } - lastgrp--; - lastino = igroup[lastgrp].xi_startino + - libxfs_highbit64(igroup[lastgrp].xi_allocmask); - if (verbose) printf("%llu:%d\n", lastino, lastino > XFS_MAXINUMBER_32 ? 64 : 32); -- 2.47.2