]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/commitdiff
libfrog: create xfd_open function
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)
Create a helper to open a file and initialize the xfd structure.

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>
fsr/xfs_fsr.c
include/fsgeom.h
libfrog/fsgeom.c
quota/quot.c
scrub/phase1.c

index f7e7474d344c7977adb2debaa751461e8bf8620d..64892dd5eca22873fa62ba243b0596cc27eb2adb 100644 (file)
@@ -593,18 +593,10 @@ fsrfs(char *mntdir, xfs_ino_t startino, int targetrange)
                return -1;
        }
 
-       if ((fsxfd.fd = open(mntdir, O_RDONLY)) < 0) {
-               fsrprintf(_("unable to open: %s: %s\n"),
-                         mntdir, strerror( errno ));
-               free(fshandlep);
-               return -1;
-       }
-
-       ret = xfd_prepare_geometry(&fsxfd);
+       ret = xfd_open(&fsxfd, mntdir, O_RDONLY);
        if (ret) {
-               fsrprintf(_("Skipping %s: could not get XFS geometry\n"),
-                         mntdir);
-               xfd_close(&fsxfd);
+               fsrprintf(_("unable to open XFS file: %s: %s\n"),
+                         mntdir, strerror(ret));
                free(fshandlep);
                return -1;
        }
@@ -726,16 +718,10 @@ fsrfile(
         * Need to open something on the same filesystem as the
         * file.  Open the parent.
         */
-       fsxfd.fd = open(getparent(fname), O_RDONLY);
-       if (fsxfd.fd < 0) {
-               fsrprintf(_("unable to open sys handle for %s: %s\n"),
-                       fname, strerror(errno));
-               goto out;
-       }
-
-       error = xfd_prepare_geometry(&fsxfd);
+       error = xfd_open(&fsxfd, getparent(fname), O_RDONLY);
        if (error) {
-               fsrprintf(_("Unable to get geom on fs for: %s\n"), fname);
+               fsrprintf(_("unable to open sys handle for XFS file %s: %s\n"),
+                       fname, strerror(error));
                goto out;
        }
 
index 771732f2f5b887e448cd42262fb0ab0fcd9e5f83..1c397cb67b4a76a48776b3538c8969d932ea2560 100644 (file)
@@ -42,6 +42,7 @@ struct xfs_fd {
 #define XFS_FD_INIT_EMPTY      XFS_FD_INIT(-1)
 
 int xfd_prepare_geometry(struct xfs_fd *xfd);
+int xfd_open(struct xfs_fd *xfd, const char *pathname, int flags);
 int xfd_close(struct xfs_fd *xfd);
 
 /* Convert AG number and AG inode number into fs inode number. */
index 39604556a7dc19e532b0c19ee016d99c83f1e91a..9a428bf6a9f36136d304807a8359adb8073592a3 100644 (file)
@@ -116,6 +116,28 @@ xfd_prepare_geometry(
        return 0;
 }
 
+/* Open a file on an XFS filesystem.  Returns zero or a positive error code. */
+int
+xfd_open(
+       struct xfs_fd           *xfd,
+       const char              *pathname,
+       int                     flags)
+{
+       int                     ret;
+
+       xfd->fd = open(pathname, flags);
+       if (xfd->fd < 0)
+               return errno;
+
+       ret = xfd_prepare_geometry(xfd);
+       if (ret) {
+               xfd_close(xfd);
+               return ret;
+       }
+
+       return 0;
+}
+
 /*
  * Release any resources associated with this xfs_fd structure.  Returns zero
  * or a positive error code.
index 6fb6f8332c735b98ce2a835e05ae8bb1684fa879..b718b09d743c73271bb97cca577a6e9a3e1bd68c 100644 (file)
@@ -132,7 +132,7 @@ quot_bulkstat_mount(
        struct xfs_bstat        *buf;
        uint64_t                last = 0;
        uint32_t                count;
-       int                     i, sts;
+       int                     i, sts, ret;
        du_t                    **dp;
 
        /*
@@ -147,8 +147,9 @@ quot_bulkstat_mount(
                        *dp = NULL;
        ndu[0] = ndu[1] = ndu[2] = 0;
 
-       fsxfd.fd = open(fsdir, O_RDONLY);
-       if (fsxfd.fd < 0) {
+       ret = xfd_open(&fsxfd, fsdir, O_RDONLY);
+       if (ret) {
+               errno = ret;
                perror(fsdir);
                return;
        }
index 6879cc331882357d6d49acdb30d0879107f1403b..81b0990dce2c6b205cb4d7afc890029d883acb95 100644 (file)
@@ -84,13 +84,17 @@ xfs_setup_fs(
         * CAP_SYS_ADMIN, which we probably need to do anything fancy
         * with the (XFS driver) kernel.
         */
-       ctx->mnt.fd = open(ctx->mntpoint, O_RDONLY | O_NOATIME | O_DIRECTORY);
-       if (ctx->mnt.fd < 0) {
-               if (errno == EPERM)
+       error = xfd_open(&ctx->mnt, ctx->mntpoint,
+                       O_RDONLY | O_NOATIME | O_DIRECTORY);
+       if (error) {
+               if (error == EPERM)
                        str_info(ctx, ctx->mntpoint,
 _("Must be root to run scrub."));
+               else if (error == ENOTTY)
+                       str_error(ctx, ctx->mntpoint,
+_("Not an XFS filesystem."));
                else
-                       str_errno(ctx, ctx->mntpoint);
+                       str_liberror(ctx, error, ctx->mntpoint);
                return false;
        }
 
@@ -110,12 +114,6 @@ _("Must be root to run scrub."));
                return false;
        }
 
-       if (!platform_test_xfs_fd(ctx->mnt.fd)) {
-               str_info(ctx, ctx->mntpoint,
-_("Does not appear to be an XFS filesystem!"));
-               return false;
-       }
-
        /*
         * Flush everything out to disk before we start checking.
         * This seems to reduce the incidence of stale file handle
@@ -127,13 +125,6 @@ _("Does not appear to be an XFS filesystem!"));
                return false;
        }
 
-       /* Retrieve XFS geometry. */
-       error = xfd_prepare_geometry(&ctx->mnt);
-       if (error) {
-               str_liberror(ctx, error, _("Retrieving XFS geometry"));
-               return false;
-       }
-
        if (!xfs_action_lists_alloc(ctx->mnt.fsgeom.agcount,
                                &ctx->action_lists)) {
                str_error(ctx, ctx->mntpoint, _("Not enough memory."));