]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/commitdiff
libfrog: refactor open-coded INUMBERS calls
authorDarrick J. Wong <darrick.wong@oracle.com>
Mon, 9 Sep 2019 19:37:05 +0000 (15:37 -0400)
committerEric Sandeen <sandeen@redhat.com>
Mon, 9 Sep 2019 19:37:05 +0000 (15:37 -0400)
Refactor all the INUMBERS ioctl callsites into helper functions.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Reviewed-by: Dave Chinner <dchinner@redhat.com>
Signed-off-by: Eric Sandeen <sandeen@sandeen.net>
io/imap.c
io/open.c
libfrog/bulkstat.c
libfrog/bulkstat.h
scrub/fscounters.c
scrub/inodes.c

index 9667289a6fe1f5bc664842f296788e75844fa817..86d8bda38186d05cc40085716ea53ca9018da0b3 100644 (file)
--- a/io/imap.c
+++ b/io/imap.c
@@ -8,18 +8,21 @@
 #include "input.h"
 #include "init.h"
 #include "io.h"
+#include "fsgeom.h"
+#include "libfrog/bulkstat.h"
 
 static cmdinfo_t imap_cmd;
 
 static int
 imap_f(int argc, char **argv)
 {
-       int             count;
-       int             nent;
-       int             i;
-       __u64           last = 0;
-       struct xfs_inogrp *t;
-       struct xfs_fsop_bulkreq bulkreq;
+       struct xfs_fd           xfd = XFS_FD_INIT(file->fd);
+       struct xfs_inogrp       *t;
+       uint64_t                last = 0;
+       uint32_t                count;
+       uint32_t                nent;
+       int                     i;
+       int                     error;
 
        if (argc != 2)
                nent = 1;
@@ -30,14 +33,8 @@ imap_f(int argc, char **argv)
        if (!t)
                return 0;
 
-       bulkreq.lastip  = &last;
-       bulkreq.icount  = nent;
-       bulkreq.ubuffer = (void *)t;
-       bulkreq.ocount  = &count;
-
-       while (xfsctl(file->name, file->fd, XFS_IOC_FSINUMBERS, &bulkreq) == 0) {
-               if (count == 0)
-                       goto out_free;
+       while ((error = xfrog_inumbers(&xfd, &last, nent, t, &count)) == 0 &&
+              count > 0) {
                for (i = 0; i < count; i++) {
                        printf(_("ino %10llu count %2d mask %016llx\n"),
                                (unsigned long long)t[i].xi_startino,
@@ -45,9 +42,12 @@ imap_f(int argc, char **argv)
                                (unsigned long long)t[i].xi_allocmask);
                }
        }
-       perror("xfsctl(XFS_IOC_FSINUMBERS)");
-       exitcode = 1;
-out_free:
+
+       if (error) {
+               errno = error;
+               perror("xfsctl(XFS_IOC_FSINUMBERS)");
+               exitcode = 1;
+       }
        free(t);
        return 0;
 }
index eab85fd7d3a5aa22322a13de5a4917b3c7b6d3e1..169f375c519c18c6e1583d16a14c3669a01de9af 100644 (file)
--- a/io/open.c
+++ b/io/open.c
@@ -676,24 +676,24 @@ inode_help(void)
 "\n"));
 }
 
+#define IGROUP_NR      (1024)
 static __u64
 get_last_inode(void)
 {
-       __u64                   lastip = 0;
-       __u64                   lastgrp = 0;
-       __s32                   ocount = 0;
+       struct xfs_fd           xfd = XFS_FD_INIT(file->fd);
+       uint64_t                lastip = 0;
+       uint32_t                lastgrp = 0;
+       uint32_t                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;
+       struct xfs_inogrp       igroup[IGROUP_NR];
 
        for (;;) {
-               if (xfsctl(file->name, file->fd, XFS_IOC_FSINUMBERS,
-                               &bulkreq)) {
+               int             ret;
+
+               ret = xfrog_inumbers(&xfd, &lastip, IGROUP_NR, igroup,
+                               &ocount);
+               if (ret) {
+                       errno = ret;
                        perror("XFS_IOC_FSINUMBERS");
                        return 0;
                }
index a61e2fd3b21c711f12e1ac3c033be177b68a6b23..fa10f2989d9a8fe1c94e6bca3f8dac48039aaf7c 100644 (file)
@@ -51,3 +51,29 @@ xfrog_bulkstat(
                return errno;
        return 0;
 }
+
+/*
+ * Query inode allocation bitmask information.  Returns zero or a positive
+ * error code.
+ */
+int
+xfrog_inumbers(
+       struct xfs_fd           *xfd,
+       uint64_t                *lastino,
+       uint32_t                icount,
+       struct xfs_inogrp       *ubuffer,
+       uint32_t                *ocount)
+{
+       struct xfs_fsop_bulkreq bulkreq = {
+               .lastip         = (__u64 *)lastino,
+               .icount         = icount,
+               .ubuffer        = ubuffer,
+               .ocount         = (__s32 *)ocount,
+       };
+       int                     ret;
+
+       ret = ioctl(xfd->fd, XFS_IOC_FSINUMBERS, &bulkreq);
+       if (ret)
+               return errno;
+       return 0;
+}
index 175209133a3bf82e5b23f8995a386d6236d70c52..83ac0e37699c60f0b1e327b4526a0add99defe51 100644 (file)
@@ -13,4 +13,8 @@ int xfrog_bulkstat_single(struct xfs_fd *xfd, uint64_t ino,
 int xfrog_bulkstat(struct xfs_fd *xfd, uint64_t *lastino, uint32_t icount,
                struct xfs_bstat *ubuffer, uint32_t *ocount);
 
+struct xfs_inogrp;
+int xfrog_inumbers(struct xfs_fd *xfd, uint64_t *lastino, uint32_t icount,
+               struct xfs_inogrp *ubuffer, uint32_t *ocount);
+
 #endif /* __LIBFROG_BULKSTAT_H__ */
index ea6af156f72f2a9eabcfa25ba43bb66e6f79ae17..f8cc1e947a9599ac203692cd5550174149c5c94d 100644 (file)
@@ -15,6 +15,7 @@
 #include "xfs_scrub.h"
 #include "common.h"
 #include "fscounters.h"
+#include "libfrog/bulkstat.h"
 
 /*
  * Filesystem counter collection routines.  We can count the number of
@@ -41,30 +42,25 @@ xfs_count_inodes_range(
        uint64_t                last_ino,
        uint64_t                *count)
 {
-       struct xfs_fsop_bulkreq igrpreq = {NULL};
        struct xfs_inogrp       inogrp;
-       __u64                   igrp_ino;
+       uint64_t                igrp_ino;
        uint64_t                nr = 0;
-       __s32                   igrplen = 0;
+       uint32_t                igrplen = 0;
        int                     error;
 
        ASSERT(!(first_ino & (XFS_INODES_PER_CHUNK - 1)));
        ASSERT((last_ino & (XFS_INODES_PER_CHUNK - 1)));
 
-       igrpreq.lastip  = &igrp_ino;
-       igrpreq.icount  = 1;
-       igrpreq.ubuffer = &inogrp;
-       igrpreq.ocount  = &igrplen;
-
        igrp_ino = first_ino;
-       error = ioctl(ctx->mnt.fd, XFS_IOC_FSINUMBERS, &igrpreq);
-       while (!error && igrplen && inogrp.xi_startino < last_ino) {
+       while (!(error = xfrog_inumbers(&ctx->mnt, &igrp_ino, 1, &inogrp,
+                       &igrplen))) {
+               if (igrplen == 0 || inogrp.xi_startino >= last_ino)
+                       break;
                nr += inogrp.xi_alloccount;
-               error = ioctl(ctx->mnt.fd, XFS_IOC_FSINUMBERS, &igrpreq);
        }
 
        if (error) {
-               str_errno(ctx, descr);
+               str_liberror(ctx, error, descr);
                return false;
        }
 
index c2cbe2602019d63bcc00f428a6676240f1e767dc..bf98f6eefaaf9576b90a4bc665c054b3f1e56a1f 100644 (file)
@@ -91,16 +91,15 @@ xfs_iterate_inodes_range(
        xfs_inode_iter_fn       fn,
        void                    *arg)
 {
-       struct xfs_fsop_bulkreq igrpreq = {NULL};
        struct xfs_handle       handle;
        struct xfs_inogrp       inogrp;
        struct xfs_bstat        bstat[XFS_INODES_PER_CHUNK];
        char                    idescr[DESCR_BUFSZ];
        struct xfs_bstat        *bs;
-       __u64                   igrp_ino;
+       uint64_t                igrp_ino;
        uint64_t                ino;
        uint32_t                bulklen = 0;
-       __s32                   igrplen = 0;
+       uint32_t                igrplen = 0;
        bool                    moveon = true;
        int                     i;
        int                     error;
@@ -109,11 +108,6 @@ xfs_iterate_inodes_range(
 
        memset(bstat, 0, XFS_INODES_PER_CHUNK * sizeof(struct xfs_bstat));
 
-       igrpreq.lastip  = &igrp_ino;
-       igrpreq.icount  = 1;
-       igrpreq.ubuffer = &inogrp;
-       igrpreq.ocount  = &igrplen;
-
        memcpy(&handle.ha_fsid, fshandle, sizeof(handle.ha_fsid));
        handle.ha_fid.fid_len = sizeof(xfs_fid_t) -
                        sizeof(handle.ha_fid.fid_len);
@@ -121,7 +115,7 @@ xfs_iterate_inodes_range(
 
        /* Find the inode chunk & alloc mask */
        igrp_ino = first_ino;
-       error = ioctl(ctx->mnt.fd, XFS_IOC_FSINUMBERS, &igrpreq);
+       error = xfrog_inumbers(&ctx->mnt, &igrp_ino, 1, &inogrp, &igrplen);
        while (!error && igrplen) {
                /* Load the inodes. */
                ino = inogrp.xi_startino - 1;
@@ -181,12 +175,13 @@ _("Changed too many times during scan; giving up."));
 
                stale_count = 0;
 igrp_retry:
-               error = ioctl(ctx->mnt.fd, XFS_IOC_FSINUMBERS, &igrpreq);
+               error = xfrog_inumbers(&ctx->mnt, &igrp_ino, 1, &inogrp,
+                               &igrplen);
        }
 
 err:
        if (error) {
-               str_errno(ctx, descr);
+               str_liberror(ctx, error, descr);
                moveon = false;
        }
 out: