Skip to contentSkip to conversation

mkfs: discard only after all validations

PATCH Superseded Jan Tulak 3 participants 10 messages Sep 19, 2018 – Sep 21, 2018

lore
Share & tools
lore

Jan Tulak

author
#0 lore raw

PATCH 1/2 mkfs: discard only after all validations

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.

---
 mkfs/xfs_mkfs.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)
mkfs/xfs_mkfs.c +16 -4
index 2e53c1e8..81d9859a 100644
@@ -2389,8 +2389,7 @@ _("log stripe unit (%d bytes) is too large (maximum is 256KiB)\n"
 static void
 open_devices(
 	struct mkfs_params	*cfg,
-	struct libxfs_xinit	*xi,
-	bool			discard)
+	struct libxfs_xinit	*xi)
 {
 	uint64_t		sector_mask;
 
@@ -2419,8 +2418,16 @@ open_devices(
 	xi->dsize &= sector_mask;
 	xi->rtsize &= sector_mask;
 	xi->logBBsize &= (uint64_t)-1 << (max(cfg->lsectorlog, 10) - BBSHIFT);
+}
 
-
+static void
+discard_data(
+	struct libxfs_xinit	*xi,
+	bool			discard)
+{
+	/*
+	 * This function has to be called after libxfs has been initialized.
+	 */
 	if (!discard)
 		return;
 
@@ -3901,7 +3908,7 @@ main(
 	/*
 	 * 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);
@@ -3952,6 +3959,11 @@ main(
 			exit(0);
 	}
 
+	/*
+	 * All values have been validated, discard the old device layout.
+	 */
+	discard_data(&xi, (discard && !dry_run));
+
 	/*
 	 * we need the libxfs buffer cache from here on in.
 	 */
Signature · 1 line
2.18.0
author
#1 lore raw

PATCH 2/2 mkfs: move 'mounted' check before 'existing fs' check

Check if a device is mounted (and issue an error about that) before asking for -f flag.

Example of the old behaviour I'm changing:

$ mkfs.xfs /dev/sda1
mkfs.xfs: /dev/sda1 appears to contain an existing filesystem (xfs).
mkfs.xfs: Use the -f option to force overwrite.
$ mkfs.xfs -f /dev/sda1
mkfs.xfs: /dev/sda1 contains a mounted filesystem
---
 mkfs/xfs_mkfs.c | 32 ++++++++++++++++++--------------
 1 file changed, 18 insertions(+), 14 deletions(-)
mkfs/xfs_mkfs.c +18 -14
index 81d9859a..97e78647 100644
@@ -1012,7 +1012,6 @@ check_device_type(
 	bool		no_size,
 	bool		no_name,
 	int		*create,
-	bool		force_overwrite,
 	const char	*optname)
 {
 	struct stat statbuf;
@@ -1043,13 +1042,6 @@ check_device_type(
 		return;
 	}
 
-	if (!force_overwrite && check_overwrite(name)) {
-		fprintf(stderr,
-	_("%s: Use the -f option to force overwrite.\n"),
-			progname);
-		exit(1);
-	}
-
 	/*
 	 * We only want to completely truncate and recreate an existing file if
 	 * we were specifically told it was a file. Set the create flag only in
@@ -1079,6 +1071,20 @@ check_device_type(
 	usage();
 }
 
+static void
+validate_overwrite(
+	const char	*name,
+	bool		force_overwrite)
+{
+	if (!force_overwrite && check_overwrite(name)) {
+		fprintf(stderr,
+	_("%s: Use the -f option to force overwrite.\n"),
+			progname);
+		exit(1);
+	}
+
+}
+
 static void
 validate_ag_geometry(
 	int		blocklog,
@@ -1731,18 +1737,15 @@ validate_sectorsize(
 	 * files or block devices and set the control parameters correctly.
 	 */
 	check_device_type(dfile, &cli->xi->disfile, !cli->dsize, !dfile,
-			  dry_run ? NULL : &cli->xi->dcreat,
-			  force_overwrite, "d");
+			  dry_run ? NULL : &cli->xi->dcreat, "d");
 	if (!cli->loginternal)
 		check_device_type(cli->xi->logname, &cli->xi->lisfile,
 				  !cli->logsize, !cli->xi->logname,
-				  dry_run ? NULL : &cli->xi->lcreat,
-				  force_overwrite, "l");
+				  dry_run ? NULL : &cli->xi->lcreat, "l");
 	if (cli->xi->rtname)
 		check_device_type(cli->xi->rtname, &cli->xi->risfile,
 				  !cli->rtsize, !cli->xi->rtname,
-				  dry_run ? NULL : &cli->xi->rcreat,
-				  force_overwrite, "r");
+				  dry_run ? NULL : &cli->xi->rcreat, "r");
 
 	/*
 	 * Explicitly disable direct IO for image files so we don't error out on
@@ -3909,6 +3912,7 @@ main(
 	 * Open and validate the device configurations
 	 */
 	open_devices(&cfg, &xi);
+	validate_overwrite(dfile, force_overwrite);
 	validate_datadev(&cfg, &cli);
 	validate_logdev(&cfg, &cli, &logfile);
 	validate_rtdev(&cfg, &cli, &rtfile);

--
2.18.0

#2 lore raw

Re: [PATCH 2/2] mkfs: move 'mounted' check before 'existing fs' check

Quoted 10 lines · Jan Tulak

Check if a device is mounted (and issue an error about that) before asking for -f flag.

Example of the old behaviour I'm changing:

$ mkfs.xfs /dev/sda1
mkfs.xfs: /dev/sda1 appears to contain an existing filesystem (xfs).
mkfs.xfs: Use the -f option to force overwrite.
$ mkfs.xfs -f /dev/sda1
mkfs.xfs: /dev/sda1 contains a mounted filesystem

I'm confused by the commit message -- what is the new behavior?

Is it this?

$ mkfs.xfs /dev/sda1
mkfs.xfs: /dev/sda1 contains a mounted filesystem
$ mkfs.xfs -f /dev/sda1
mkfs.xfs: /dev/sda1 contains a mounted filesystem

--D

Quoted 84 lines
---
 mkfs/xfs_mkfs.c | 32 ++++++++++++++++++--------------
 1 file changed, 18 insertions(+), 14 deletions(-)
mkfs/xfs_mkfs.c +18 -15
index 81d9859a..97e78647 100644
@@ -1012,7 +1012,6 @@ check_device_type(
 	bool		no_size,
 	bool		no_name,
 	int		*create,
-	bool		force_overwrite,
 	const char	*optname)
 {
 	struct stat statbuf;
@@ -1043,13 +1042,6 @@ check_device_type(
 		return;
 	}
 
-	if (!force_overwrite && check_overwrite(name)) {
-		fprintf(stderr,
-	_("%s: Use the -f option to force overwrite.\n"),
-			progname);
-		exit(1);
-	}
-
 	/*
 	 * We only want to completely truncate and recreate an existing file if
 	 * we were specifically told it was a file. Set the create flag only in
@@ -1079,6 +1071,20 @@ check_device_type(
 	usage();
 }
 
+static void
+validate_overwrite(
+	const char	*name,
+	bool		force_overwrite)
+{
+	if (!force_overwrite && check_overwrite(name)) {
+		fprintf(stderr,
+	_("%s: Use the -f option to force overwrite.\n"),
+			progname);
+		exit(1);
+	}
+
+}
+
 static void
 validate_ag_geometry(
 	int		blocklog,
@@ -1731,18 +1737,15 @@ validate_sectorsize(
 	 * files or block devices and set the control parameters correctly.
 	 */
 	check_device_type(dfile, &cli->xi->disfile, !cli->dsize, !dfile,
-			  dry_run ? NULL : &cli->xi->dcreat,
-			  force_overwrite, "d");
+			  dry_run ? NULL : &cli->xi->dcreat, "d");
 	if (!cli->loginternal)
 		check_device_type(cli->xi->logname, &cli->xi->lisfile,
 				  !cli->logsize, !cli->xi->logname,
-				  dry_run ? NULL : &cli->xi->lcreat,
-				  force_overwrite, "l");
+				  dry_run ? NULL : &cli->xi->lcreat, "l");
 	if (cli->xi->rtname)
 		check_device_type(cli->xi->rtname, &cli->xi->risfile,
 				  !cli->rtsize, !cli->xi->rtname,
-				  dry_run ? NULL : &cli->xi->rcreat,
-				  force_overwrite, "r");
+				  dry_run ? NULL : &cli->xi->rcreat, "r");
 
 	/*
 	 * Explicitly disable direct IO for image files so we don't error out on
@@ -3909,6 +3912,7 @@ main(
 	 * Open and validate the device configurations
 	 */
 	open_devices(&cfg, &xi);
+	validate_overwrite(dfile, force_overwrite);
 	validate_datadev(&cfg, &cli);
 	validate_logdev(&cfg, &cli, &logfile);
 	validate_rtdev(&cfg, &cli, &rtfile);
--

2.18.0

author
#3 lore raw

Re: [PATCH 2/2] mkfs: move 'mounted' check before 'existing fs' check

Quoted 15 lines · Darrick J. Wong
Quoted 10 lines · Jan Tulak

Check if a device is mounted (and issue an error about that) before asking for -f flag.

Example of the old behaviour I'm changing:

$ mkfs.xfs /dev/sda1
mkfs.xfs: /dev/sda1 appears to contain an existing filesystem (xfs).
mkfs.xfs: Use the -f option to force overwrite.
$ mkfs.xfs -f /dev/sda1
mkfs.xfs: /dev/sda1 contains a mounted filesystem

I'm confused by the commit message -- what is the new behavior?

Quoted 7 lines

Is it this?

$ mkfs.xfs /dev/sda1
mkfs.xfs: /dev/sda1 contains a mounted filesystem
$ mkfs.xfs -f /dev/sda1
mkfs.xfs: /dev/sda1 contains a mounted filesystem

Yes. I should put it right into the commit message.

Jan

Signature · 2 lines
Jan Tulak
[email protected] / [email protected]
Reviewed-by
#4 lore raw

Re: [PATCH 2/2] mkfs: move 'mounted' check before 'existing fs' check

Quoted 18 lines · Jan Tulak
Quoted 17 lines · Darrick J. Wong
Quoted 10 lines · Jan Tulak

Check if a device is mounted (and issue an error about that) before asking for -f flag.

Example of the old behaviour I'm changing:

$ mkfs.xfs /dev/sda1
mkfs.xfs: /dev/sda1 appears to contain an existing filesystem (xfs).
mkfs.xfs: Use the -f option to force overwrite.
$ mkfs.xfs -f /dev/sda1
mkfs.xfs: /dev/sda1 contains a mounted filesystem

I'm confused by the commit message -- what is the new behavior?

Is it this?

Quoted 7 lines
Quoted 5 lines
$ mkfs.xfs /dev/sda1
mkfs.xfs: /dev/sda1 contains a mounted filesystem
$ mkfs.xfs -f /dev/sda1
mkfs.xfs: /dev/sda1 contains a mounted filesystem

Yes. I should put it right into the commit message.

With that added,

--D

Quoted 6 lines

Jan

Signature · 2 lines
Jan Tulak
[email protected] / [email protected]
#5 lore raw
In reply to Jan Tulak
Quoted 25 lines · Jan Tulak

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.

---
 mkfs/xfs_mkfs.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)
mkfs/xfs_mkfs.c +1 -2
index 2e53c1e8..81d9859a 100644
@@ -2389,8 +2389,7 @@ _("log stripe unit (%d bytes) is too large (maximum is 256KiB)\n"
 static void
 open_devices(
 	struct mkfs_params	*cfg,
-	struct libxfs_xinit	*xi,
-	bool			discard)
+	struct libxfs_xinit	*xi)
 {
 	uint64_t		sector_mask;
 

mkfs/xfs_mkfs.c open_devices( · line 2424

Quoted 8 lines
mkfs/xfs_mkfs.c +3 -1
@@ -2419,8 +2418,16 @@ open_devices(
 	xi->dsize &= sector_mask;
 	xi->rtsize &= sector_mask;
 	xi->logBBsize &= (uint64_t)-1 << (max(cfg->lsectorlog, 10) - BBSHIFT);
+}
 
-
+static void
+discard_data(

Perhaps discard_devices(), since this function can DISCARD more than just the data device.

mkfs/xfs_mkfs.c open_devices( · line 2433

Quoted 9 lines
mkfs/xfs_mkfs.c +6 -0
+	struct libxfs_xinit	*xi,
+	bool			discard)
+{
+	/*
+	 * This function has to be called after libxfs has been initialized.
+	 */
 	if (!discard)
 		return;
 

While we're on the topic, I notice that we skip discard for any device that's actually a file. Seeing as fallocate(PUNCH_HOLE) works on files (and block devices), is there a reason why we avoid punching out fs image files?

Quoted 10 lines
mkfs/xfs_mkfs.c +1 -1
@@ -3901,7 +3908,7 @@ main(
 	/*
 	 * 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);

mkfs/xfs_mkfs.c main( · line 3965

Quoted 7 lines
mkfs/xfs_mkfs.c +4 -0
@@ -3952,6 +3959,11 @@ main(
 			exit(0);
 	}
 
+	/*
+	 * All values have been validated, discard the old device layout.
+	 */
+	discard_data(&xi, (discard && !dry_run));

if (discard && !dry_run)

	discard_devices(&xi);

?

--D

Quoted 7 lines
mkfs/xfs_mkfs.c +1 -0
+
 	/*
 	 * we need the libxfs buffer cache from here on in.
 	 */
Signature · 1 line
2.18.0
author
#6 lore raw

On Wed, Sep 19, 2018 at 4:44 PM Darrick J. Wong <[email protected]> wrote:

snip

Quoted 6 lines
Quoted 6 lines
mkfs/xfs_mkfs.c +1 -0
@@ -2419,8 +2418,16 @@ open_devices(
      xi->dsize &= sector_mask;
      xi->rtsize &= sector_mask;
      xi->logBBsize &= (uint64_t)-1 << (max(cfg->lsectorlog, 10) - BBSHIFT);
+}
 
Quoted 6 lines

mkfs/xfs_mkfs.c open_devices( · line 2424

Quoted 3 lines
mkfs/xfs_mkfs.c +2 -1
-
+static void
+discard_data(

Perhaps discard_devices(), since this function can DISCARD more than just the data device.

Agreed.

Quoted 11 lines

mkfs/xfs_mkfs.c open_devices( · line 2433

Quoted 10 lines
mkfs/xfs_mkfs.c +6 -0
+     struct libxfs_xinit     *xi,
+     bool                    discard)
+{
+     /*
+      * This function has to be called after libxfs has been initialized.
+      */
      if (!discard)
              return;
 
Quoted 7 lines

While we're on the topic, I notice that we skip discard for any device that's actually a file. Seeing as fallocate(PUNCH_HOLE) works on files (and block devices), is there a reason why we avoid punching out fs image files?

From what I remember, it went along this: If the user is trying to

mkfs an existing file, we don't want to destroy anything they did with the file before (sparse file, allocations...).

Quoted 19 lines
Quoted 10 lines
mkfs/xfs_mkfs.c +1 -1
@@ -3901,7 +3908,7 @@ main(
      /*
       * 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);

mkfs/xfs_mkfs.c main( · line 3965

Quoted 7 lines
mkfs/xfs_mkfs.c +4 -0
@@ -3952,6 +3959,11 @@ main(
                      exit(0);
      }
 
+     /*
+      * All values have been validated, discard the old device layout.
+      */
+     discard_data(&xi, (discard && !dry_run));

if (discard && !dry_run)

Quoted 3 lines
        discard_devices(&xi);

?

Yes, this makes more sense.

Jan

--
Jan Tulak
[email protected] / [email protected]

#7 lore raw
In reply to Darrick J. Wong
Quoted 38 lines · Darrick J. Wong
Quoted 25 lines · Jan Tulak

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.

---
 mkfs/xfs_mkfs.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)
mkfs/xfs_mkfs.c +1 -2
index 2e53c1e8..81d9859a 100644
@@ -2389,8 +2389,7 @@ _("log stripe unit (%d bytes) is too large (maximum is 256KiB)\n"
 static void
 open_devices(
 	struct mkfs_params	*cfg,
-	struct libxfs_xinit	*xi,
-	bool			discard)
+	struct libxfs_xinit	*xi)
 {
 	uint64_t		sector_mask;
 

mkfs/xfs_mkfs.c open_devices( · line 2424

Quoted 8 lines
mkfs/xfs_mkfs.c +3 -1
@@ -2419,8 +2418,16 @@ open_devices(
 	xi->dsize &= sector_mask;
 	xi->rtsize &= sector_mask;
 	xi->logBBsize &= (uint64_t)-1 << (max(cfg->lsectorlog, 10) - BBSHIFT);
+}
 
-
+static void
+discard_data(

Perhaps discard_devices(), since this function can DISCARD more than just the data device.

Quoted 14 lines

mkfs/xfs_mkfs.c open_devices( · line 2433

Quoted 9 lines
mkfs/xfs_mkfs.c +6 -0
+	struct libxfs_xinit	*xi,
+	bool			discard)
+{
+	/*
+	 * This function has to be called after libxfs has been initialized.
+	 */
 	if (!discard)
 		return;
 

While we're on the topic, I notice that we skip discard for any device that's actually a file. Seeing as fallocate(PUNCH_HOLE) works on files (and block devices), is there a reason why we avoid punching out fs image files?

Yeah - preallocated image files shouldn't be punched, because it defeats the whole purpose of setting up preallocated image files.

Cheers,

Dave.

Signature · 2 lines
Dave Chinner
[email protected]
author
#8 lore raw

PATCH v2 1/2 mkfs: discard only after all validations

In reply to Jan Tulak

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.

---
CHANGES:
v2
* discard_data -> discard_devices
* move the discard condition outside of the function

---
 mkfs/xfs_mkfs.c | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)
mkfs/xfs_mkfs.c +16 -6
index 2e53c1e8..c97c69e8 100644
@@ -2389,8 +2389,7 @@ _("log stripe unit (%d bytes) is too large (maximum is 256KiB)\n"
 static void
 open_devices(
 	struct mkfs_params	*cfg,
-	struct libxfs_xinit	*xi,
-	bool			discard)
+	struct libxfs_xinit	*xi)
 {
 	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);
+}
 
-
-	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);
@@ -3901,7 +3905,7 @@ main(
 	/*
 	 * 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);
@@ -3952,6 +3956,12 @@ main(
 			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.
 	 */
Signature · 1 line
2.18.0
Reviewed-by on v2 1/2
#9 lore raw
Quoted 6 lines · Jan Tulak

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.

Looks ok to me,

--D

Quoted 68 lines

---
CHANGES:
v2
* discard_data -> discard_devices
* move the discard condition outside of the function

---
 mkfs/xfs_mkfs.c | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)
mkfs/xfs_mkfs.c +16 -6
index 2e53c1e8..c97c69e8 100644
@@ -2389,8 +2389,7 @@ _("log stripe unit (%d bytes) is too large (maximum is 256KiB)\n"
 static void
 open_devices(
 	struct mkfs_params	*cfg,
-	struct libxfs_xinit	*xi,
-	bool			discard)
+	struct libxfs_xinit	*xi)
 {
 	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);
+}
 
-
-	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);
@@ -3901,7 +3905,7 @@ main(
 	/*
 	 * 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);
@@ -3952,6 +3956,12 @@ main(
 			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.
 	 */
Signature · 1 line
2.18.0