]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/commitdiff
mkfs: discard only after all validations
authorJan Tulak <jtulak@redhat.com>
Tue, 9 Oct 2018 16:49:47 +0000 (11:49 -0500)
committerEric Sandeen <sandeen@redhat.com>
Tue, 9 Oct 2018 16:49:47 +0000 (11:49 -0500)
Discard should happen only when everything has been validated, just
before we start writing to the device. If it happens earlier, it is
possible that mkfs will abort, but managed to already wipe data. This
patch moves the discard to the latest possible moment.

Signed-off-by: Jan Tulak <jtulak@redhat.com>
Reviewed-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: Eric Sandeen <sandeen@sandeen.net>
mkfs/xfs_mkfs.c

index 7c05c6f6ea3bc02e1befd41c4d731d0679137980..87ca1d274978244088fca61945454d16e4de9883 100644 (file)
@@ -2389,8 +2389,7 @@ _("log stripe unit (%d bytes) is too large (maximum is 256KiB)\n"
 static void
 open_devices(
        struct mkfs_params      *cfg,
 static void
 open_devices(
        struct mkfs_params      *cfg,
-       struct libxfs_xinit     *xi,
-       bool                    discard)
+       struct libxfs_xinit     *xi)
 {
        uint64_t                sector_mask;
 
 {
        uint64_t                sector_mask;
 
@@ -2419,10 +2418,15 @@ open_devices(
        xi->dsize &= sector_mask;
        xi->rtsize &= sector_mask;
        xi->logBBsize &= (uint64_t)-1 << (max(cfg->lsectorlog, 10) - BBSHIFT);
        xi->dsize &= sector_mask;
        xi->rtsize &= sector_mask;
        xi->logBBsize &= (uint64_t)-1 << (max(cfg->lsectorlog, 10) - BBSHIFT);
+}
 
 
-
-       if (!discard)
-               return;
+static void
+discard_devices(
+       struct libxfs_xinit     *xi)
+{
+       /*
+        * This function has to be called after libxfs has been initialized.
+        */
 
        if (!xi->disfile)
                discard_blocks(xi->ddev, xi->dsize);
 
        if (!xi->disfile)
                discard_blocks(xi->ddev, xi->dsize);
@@ -3902,7 +3906,7 @@ main(
        /*
         * Open and validate the device configurations
         */
        /*
         * Open and validate the device configurations
         */
-       open_devices(&cfg, &xi, (discard && !dry_run));
+       open_devices(&cfg, &xi);
        validate_datadev(&cfg, &cli);
        validate_logdev(&cfg, &cli, &logfile);
        validate_rtdev(&cfg, &cli, &rtfile);
        validate_datadev(&cfg, &cli);
        validate_logdev(&cfg, &cli, &logfile);
        validate_rtdev(&cfg, &cli, &rtfile);
@@ -3953,6 +3957,12 @@ main(
                        exit(0);
        }
 
                        exit(0);
        }
 
+       /*
+        * All values have been validated, discard the old device layout.
+        */
+       if (discard && !dry_run)
+               discard_devices(&xi);
+
        /*
         * we need the libxfs buffer cache from here on in.
         */
        /*
         * we need the libxfs buffer cache from here on in.
         */