]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blobdiff - scrub/phase1.c
libfrog: convert fsgeom.c functions to negative error codes
[thirdparty/xfsprogs-dev.git] / scrub / phase1.c
index 87847259e37428449427f878f28930e84c710b25..6125d3245dda7fbf88e228aa38cdea879f815f9e 100644 (file)
@@ -1,21 +1,7 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * Copyright (C) 2018 Oracle.  All Rights Reserved.
- *
  * Author: Darrick J. Wong <darrick.wong@oracle.com>
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it would be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write the Free Software Foundation,
- * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA.
  */
 #include "xfs.h"
 #include <unistd.h>
 #include <dirent.h>
 #include <stdint.h>
 #include <pthread.h>
-#include "libfrog.h"
-#include "workqueue.h"
+#include "libfrog/util.h"
+#include "libfrog/workqueue.h"
 #include "input.h"
-#include "path.h"
+#include "libfrog/paths.h"
 #include "handle.h"
 #include "bitops.h"
-#include "avl64.h"
+#include "libfrog/avl64.h"
 #include "list.h"
 #include "xfs_scrub.h"
 #include "common.h"
 #include "disk.h"
 #include "scrub.h"
+#include "repair.h"
+#include "libfrog/fsgeom.h"
 
 /* Phase 1: Find filesystem geometry (and clean up after) */
 
@@ -51,17 +39,18 @@ xfs_shutdown_fs(
 
        flag = XFS_FSOP_GOING_FLAGS_LOGFLUSH;
        str_info(ctx, ctx->mntpoint, _("Shutting down filesystem!"));
-       if (ioctl(ctx->mnt_fd, XFS_IOC_GOINGDOWN, &flag))
+       if (ioctl(ctx->mnt.fd, XFS_IOC_GOINGDOWN, &flag))
                str_errno(ctx, ctx->mntpoint);
 }
 
 /* Clean up the XFS-specific state data. */
-bool
-xfs_cleanup_fs(
+int
+scrub_cleanup(
        struct scrub_ctx        *ctx)
 {
        int                     error;
 
+       action_lists_free(&ctx->action_lists);
        if (ctx->fshandle)
                free_handle(ctx->fshandle, ctx->fshandle_len);
        if (ctx->rtdev)
@@ -71,20 +60,20 @@ xfs_cleanup_fs(
        if (ctx->datadev)
                disk_close(ctx->datadev);
        fshandle_destroy();
-       error = close(ctx->mnt_fd);
+       error = -xfd_close(&ctx->mnt);
        if (error)
-               str_errno(ctx, _("closing mountpoint fd"));
+               str_liberror(ctx, error, _("closing mountpoint fd"));
        fs_table_destroy();
 
-       return true;
+       return error;
 }
 
 /*
  * Bind to the mountpoint, read the XFS geometry, bind to the block devices.
- * Anything we've already built will be cleaned up by xfs_cleanup_fs.
+ * Anything we've already built will be cleaned up by scrub_cleanup.
  */
-bool
-xfs_setup_fs(
+int
+phase1_func(
        struct scrub_ctx                *ctx)
 {
        int                             error;
@@ -95,43 +84,34 @@ 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)
-                       str_info(ctx, ctx->mntpoint,
+       error = -xfd_open(&ctx->mnt, ctx->mntpoint,
+                       O_RDONLY | O_NOATIME | O_DIRECTORY);
+       if (error) {
+               if (error == EPERM)
+                       str_error(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);
-               return false;
+                       str_liberror(ctx, error, ctx->mntpoint);
+               return error;
        }
 
-       error = fstat(ctx->mnt_fd, &ctx->mnt_sb);
+       error = fstat(ctx->mnt.fd, &ctx->mnt_sb);
        if (error) {
                str_errno(ctx, ctx->mntpoint);
-               return false;
+               return error;
        }
-       error = fstatvfs(ctx->mnt_fd, &ctx->mnt_sv);
+       error = fstatvfs(ctx->mnt.fd, &ctx->mnt_sv);
        if (error) {
                str_errno(ctx, ctx->mntpoint);
-               return false;
+               return error;
        }
-       error = fstatfs(ctx->mnt_fd, &ctx->mnt_sf);
+       error = fstatfs(ctx->mnt.fd, &ctx->mnt_sf);
        if (error) {
                str_errno(ctx, ctx->mntpoint);
-               return false;
-       }
-
-       ctx->nr_io_threads = nproc;
-       if (verbose) {
-               fprintf(stdout, _("%s: using %d threads to scrub.\n"),
-                               ctx->mntpoint, scrub_nproc(ctx));
-               fflush(stdout);
-       }
-
-       if (!platform_test_xfs_fd(ctx->mnt_fd)) {
-               str_info(ctx, ctx->mntpoint,
-_("Does not appear to be an XFS filesystem!"));
-               return false;
+               return error;
        }
 
        /*
@@ -139,29 +119,24 @@ _("Does not appear to be an XFS filesystem!"));
         * This seems to reduce the incidence of stale file handle
         * errors when we open things by handle.
         */
-       error = syncfs(ctx->mnt_fd);
+       error = syncfs(ctx->mnt.fd);
        if (error) {
                str_errno(ctx, ctx->mntpoint);
-               return false;
+               return error;
        }
 
-       /* Retrieve XFS geometry. */
-       error = ioctl(ctx->mnt_fd, XFS_IOC_FSGEOMETRY, &ctx->geo);
+       error = action_lists_alloc(ctx->mnt.fsgeom.agcount,
+                       &ctx->action_lists);
        if (error) {
-               str_errno(ctx, ctx->mntpoint);
-               return false;
+               str_liberror(ctx, error, _("allocating action lists"));
+               return error;
        }
 
-       ctx->agblklog = log2_roundup(ctx->geo.agblocks);
-       ctx->blocklog = highbit32(ctx->geo.blocksize);
-       ctx->inodelog = highbit32(ctx->geo.inodesize);
-       ctx->inopblog = ctx->blocklog - ctx->inodelog;
-
        error = path_to_fshandle(ctx->mntpoint, &ctx->fshandle,
                        &ctx->fshandle_len);
        if (error) {
                str_errno(ctx, _("getting fshandle"));
-               return false;
+               return error;
        }
 
        /* Do we have kernel-assisted metadata scrubbing? */
@@ -169,49 +144,56 @@ _("Does not appear to be an XFS filesystem!"));
            !xfs_can_scrub_bmap(ctx) || !xfs_can_scrub_dir(ctx) ||
            !xfs_can_scrub_attr(ctx) || !xfs_can_scrub_symlink(ctx) ||
            !xfs_can_scrub_parent(ctx)) {
-               str_info(ctx, ctx->mntpoint,
+               str_error(ctx, ctx->mntpoint,
 _("Kernel metadata scrubbing facility is not available."));
-               return false;
+               return ECANCELED;
        }
 
        /* Do we need kernel-assisted metadata repair? */
        if (ctx->mode != SCRUB_MODE_DRY_RUN && !xfs_can_repair(ctx)) {
-               str_info(ctx, ctx->mntpoint,
+               str_error(ctx, ctx->mntpoint,
 _("Kernel metadata repair facility is not available.  Use -n to scrub."));
-               return false;
+               return ECANCELED;
        }
 
        /* Did we find the log and rt devices, if they're present? */
-       if (ctx->geo.logstart == 0 && ctx->fsinfo.fs_log == NULL) {
-               str_info(ctx, ctx->mntpoint,
+       if (ctx->mnt.fsgeom.logstart == 0 && ctx->fsinfo.fs_log == NULL) {
+               str_error(ctx, ctx->mntpoint,
 _("Unable to find log device path."));
-               return false;
+               return ECANCELED;
        }
-       if (ctx->geo.rtblocks && ctx->fsinfo.fs_rt == NULL) {
-               str_info(ctx, ctx->mntpoint,
+       if (ctx->mnt.fsgeom.rtblocks && ctx->fsinfo.fs_rt == NULL) {
+               str_error(ctx, ctx->mntpoint,
 _("Unable to find realtime device path."));
-               return false;
+               return ECANCELED;
        }
 
        /* Open the raw devices. */
        ctx->datadev = disk_open(ctx->fsinfo.fs_name);
        if (error) {
                str_errno(ctx, ctx->fsinfo.fs_name);
-               return false;
+               return error;
+       }
+
+       ctx->nr_io_threads = disk_heads(ctx->datadev);
+       if (verbose) {
+               fprintf(stdout, _("%s: using %d threads to scrub.\n"),
+                               ctx->mntpoint, scrub_nproc(ctx));
+               fflush(stdout);
        }
 
        if (ctx->fsinfo.fs_log) {
                ctx->logdev = disk_open(ctx->fsinfo.fs_log);
                if (error) {
                        str_errno(ctx, ctx->fsinfo.fs_name);
-                       return false;
+                       return error;
                }
        }
        if (ctx->fsinfo.fs_rt) {
                ctx->rtdev = disk_open(ctx->fsinfo.fs_rt);
                if (error) {
                        str_errno(ctx, ctx->fsinfo.fs_name);
-                       return false;
+                       return error;
                }
        }
 
@@ -222,5 +204,5 @@ _("Unable to find realtime device path."));
         */
        log_info(ctx, _("Invoking online scrub."), ctx);
        ctx->scrub_setup_succeeded = true;
-       return true;
+       return 0;
 }