]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blobdiff - io/open.c
xfs_io: factor out new get_last_inode() helper
[thirdparty/xfsprogs-dev.git] / io / open.c
index ac5a5e0f3a4eabc599fe299ecb38cd03394f6a0e..c8c82b773e102523d553d50c7430f7ca08fb7722 100644 (file)
--- a/io/open.c
+++ b/io/open.c
@@ -20,6 +20,7 @@
 #include "input.h"
 #include "init.h"
 #include "io.h"
+#include "libxfs.h"
 
 #ifndef __O_TMPFILE
 #if defined __alpha__
@@ -44,6 +45,7 @@ static cmdinfo_t statfs_cmd;
 static cmdinfo_t chproj_cmd;
 static cmdinfo_t lsproj_cmd;
 static cmdinfo_t extsize_cmd;
+static cmdinfo_t inode_cmd;
 static prid_t prid;
 static long extsize;
 
@@ -115,9 +117,9 @@ stat_f(
        }
        if (file->flags & IO_FOREIGN)
                return 0;
-       if ((xfsctl(file->name, file->fd, XFS_IOC_FSGETXATTR, &fsx)) < 0 ||
+       if ((xfsctl(file->name, file->fd, FS_IOC_FSGETXATTR, &fsx)) < 0 ||
            (xfsctl(file->name, file->fd, XFS_IOC_FSGETXATTRA, &fsxa)) < 0) {
-               perror("XFS_IOC_FSGETXATTR");
+               perror("FS_IOC_FSGETXATTR");
        } else {
                printf(_("fsxattr.xflags = 0x%x "), fsx.fsx_xflags);
                printxattr(fsx.fsx_xflags, verbose, 0, file->name, 1, 1);
@@ -193,15 +195,15 @@ openfile(
        if (!(flags & IO_READONLY) && (flags & IO_REALTIME)) {
                struct fsxattr  attr;
 
-               if (xfsctl(path, fd, XFS_IOC_FSGETXATTR, &attr) < 0) {
-                       perror("XFS_IOC_FSGETXATTR");
+               if (xfsctl(path, fd, FS_IOC_FSGETXATTR, &attr) < 0) {
+                       perror("FS_IOC_FSGETXATTR");
                        close(fd);
                        return -1;
                }
-               if (!(attr.fsx_xflags & XFS_XFLAG_REALTIME)) {
-                       attr.fsx_xflags |= XFS_XFLAG_REALTIME;
-                       if (xfsctl(path, fd, XFS_IOC_FSSETXATTR, &attr) < 0) {
-                               perror("XFS_IOC_FSSETXATTR");
+               if (!(attr.fsx_xflags & FS_XFLAG_REALTIME)) {
+                       attr.fsx_xflags |= FS_XFLAG_REALTIME;
+                       if (xfsctl(path, fd, FS_IOC_FSSETXATTR, &attr) < 0) {
+                               perror("FS_IOC_FSSETXATTR");
                                close(fd);
                                return -1;
                        }
@@ -559,8 +561,8 @@ get_extsize(const char *path, int fd)
 {
        struct fsxattr  fsx;
 
-       if ((xfsctl(path, fd, XFS_IOC_FSGETXATTR, &fsx)) < 0) {
-               printf("%s: XFS_IOC_FSGETXATTR %s: %s\n",
+       if ((xfsctl(path, fd, FS_IOC_FSGETXATTR, &fsx)) < 0) {
+               printf("%s: FS_IOC_FSGETXATTR %s: %s\n",
                        progname, path, strerror(errno));
                return 0;
        }
@@ -578,24 +580,24 @@ set_extsize(const char *path, int fd, long extsz)
                perror("fstat64");
                return 0;
        }
-       if ((xfsctl(path, fd, XFS_IOC_FSGETXATTR, &fsx)) < 0) {
-               printf("%s: XFS_IOC_FSGETXATTR %s: %s\n",
+       if ((xfsctl(path, fd, FS_IOC_FSGETXATTR, &fsx)) < 0) {
+               printf("%s: FS_IOC_FSGETXATTR %s: %s\n",
                        progname, path, strerror(errno));
                return 0;
        }
 
        if (S_ISREG(stat.st_mode)) {
-               fsx.fsx_xflags |= XFS_XFLAG_EXTSIZE;
+               fsx.fsx_xflags |= FS_XFLAG_EXTSIZE;
        } else if (S_ISDIR(stat.st_mode)) {
-               fsx.fsx_xflags |= XFS_XFLAG_EXTSZINHERIT;
+               fsx.fsx_xflags |= FS_XFLAG_EXTSZINHERIT;
        } else {
                printf(_("invalid target file type - file %s\n"), path);
                return 0;
        }
        fsx.fsx_extsize = extsz;
 
-       if ((xfsctl(path, fd, XFS_IOC_FSSETXATTR, &fsx)) < 0) {
-               printf("%s: XFS_IOC_FSSETXATTR %s: %s\n",
+       if ((xfsctl(path, fd, FS_IOC_FSSETXATTR, &fsx)) < 0) {
+               printf("%s: FS_IOC_FSSETXATTR %s: %s\n",
                        progname, path, strerror(errno));
                return 0;
        }
@@ -750,6 +752,160 @@ statfs_f(
        return 0;
 }
 
+static void
+inode_help(void)
+{
+       printf(_(
+"\n"
+"Query physical information about an inode"
+"\n"
+" Default:     -- Return 1 if any inode number greater than 32 bits exists in\n"
+"                 the filesystem, or 0 if none exist\n"
+" num          -- Return inode number [num] if in use, or 0 if not in use\n"
+" -n num       -- Return the next used inode after [num]\n"
+" -v           -- Verbose mode - display returned inode number's size in bits\n"
+"\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;
+       __u64                   lastino = 0;
+       __u64                   userino = 0;
+       char                    *p;
+       int                     c;
+       int                     verbose = 0;
+       int                     ret_next = 0;
+       int                     cmd = 0;
+       struct xfs_fsop_bulkreq bulkreq;
+       struct xfs_bstat        bstat;
+
+       while ((c = getopt(argc, argv, "nv")) != EOF) {
+               switch (c) {
+               case 'v':
+                       verbose = 1;
+                       break;
+               case 'n':
+                       ret_next = 1;
+                       break;
+               default:
+                       return command_usage(&inode_cmd);
+               }
+       }
+
+       /*
+        * Inode number can be passed with or without extra arguments, so we
+        * should handle inode numbers passed by user out of getopt()
+        */
+       if (optind < argc) {
+
+               if (ret_next) {
+                       cmd = XFS_IOC_FSBULKSTAT;
+               } else {
+                       if ((argc > 2) && !verbose)
+                               return command_usage(&inode_cmd);
+                       else
+                               cmd = XFS_IOC_FSBULKSTAT_SINGLE;
+               }
+
+               userino = strtoull(argv[optind], &p, 10);
+               if ((*p != '\0')) {
+                       printf(_("[num] must be a numeric value\n"));
+                       exitcode = 1;
+                       return 0;
+               }
+
+               bulkreq.lastip = &userino;
+               bulkreq.icount = 1;
+               bulkreq.ubuffer = &bstat;
+               bulkreq.ocount = &count;
+
+               if (xfsctl(file->name, file->fd, cmd, &bulkreq)) {
+                       if (errno == EINVAL) {
+                               if (!ret_next)
+                                       printf("0\n");
+                       } else {
+                               perror("xfsctl");
+                       }
+                       exitcode = 1;
+                       return 0;
+               }
+
+               if (ret_next)
+                       userino = bstat.bs_ino;
+
+               if (verbose)
+                       printf("%llu:%d\n",
+                              userino,
+                              userino > XFS_MAXINUMBER_32 ? 64 : 32);
+               else
+                       /* Inode in use */
+                       printf("%llu\n", userino);
+               return 0;
+
+       /* -n option must not be used stand alone */
+       } else if (ret_next) {
+               return command_usage(&inode_cmd);
+       }
+
+       /* We are finding last inode in use */
+       lastino = get_last_inode();
+       if (!lastino) {
+               exitcode = 1;
+               return 0;
+       }
+
+       if (verbose)
+               printf("%llu:%d\n", lastino,
+                       lastino > XFS_MAXINUMBER_32 ? 64 : 32);
+       else
+               printf("%d\n", lastino > XFS_MAXINUMBER_32 ? 1 : 0);
+
+       return 0;
+}
+
 void
 open_init(void)
 {
@@ -790,7 +946,7 @@ open_init(void)
        chproj_cmd.args = _("[-D | -R] projid");
        chproj_cmd.argmin = 1;
        chproj_cmd.argmax = -1;
-       chproj_cmd.flags = CMD_NOMAP_OK;
+       chproj_cmd.flags = CMD_NOMAP_OK | CMD_FOREIGN_OK;
        chproj_cmd.oneline =
                _("change project identifier on the currently open file");
        chproj_cmd.help = chproj_help;
@@ -815,6 +971,16 @@ open_init(void)
                _("get/set preferred extent size (in bytes) for the open file");
        extsize_cmd.help = extsize_help;
 
+       inode_cmd.name = "inode";
+       inode_cmd.cfunc = inode_f;
+       inode_cmd.args = _("[-nv] [num]");
+       inode_cmd.argmin = 0;
+       inode_cmd.argmax = 3;
+       inode_cmd.flags = CMD_NOMAP_OK;
+       inode_cmd.oneline =
+               _("Query inode number usage in the filesystem");
+       inode_cmd.help = inode_help;
+
        add_command(&open_cmd);
        add_command(&stat_cmd);
        add_command(&close_cmd);
@@ -822,4 +988,5 @@ open_init(void)
        add_command(&chproj_cmd);
        add_command(&lsproj_cmd);
        add_command(&extsize_cmd);
+       add_command(&inode_cmd);
 }