Skip to contentSkip to conversation

mkfs.f2fs: wipe other FS magics given -f

PATCH In review Jaegeuk Kim 3 participants 7 messages Sep 10, 2021 – Sep 22, 2021 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mkfs.f2fs: wipe other FS magics given -f

This patch fixes the below stale magic info.

$ mkfs.btrfs -f test.img
btrfs-progs v5.10.1
See http://btrfs.wiki.kernel.org for more information.
Label:              (null)
UUID:               941d2db7-3ece-4090-8b22-c4ea548b5dae
Node size:          16384
Sector size:        4096
Filesystem size:    1.00GiB
Block group profiles:
  Data:             single            8.00MiB
  Metadata:         DUP              51.19MiB
  System:           DUP               8.00MiB
SSD detected:       no
Incompat features:  extref, skinny-metadata
Runtime features:
Checksum:           crc32c
Number of devices:  1
Devices:
   ID        SIZE  PATH
    1     1.00GiB  test.img
$ hexdump -s 0x10000 -n 128 -C test.img
00010000  81 29 94 0a 00 00 00 00  00 00 00 00 00 00 00 00  |.)..............|
00010010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00010020  94 1d 2d b7 3e ce 40 90  8b 22 c4 ea 54 8b 5d ae  |..-.>.@.."..T.].|
00010030  00 00 01 00 00 00 00 00  01 00 00 00 00 00 00 00  |................|
00010040  5f 42 48 52 66 53 5f 4d  05 00 00 00 00 00 00 00  |_BHRfS_M........|
00010050  00 40 d2 01 00 00 00 00  00 40 50 01 00 00 00 00  |.@.......@P.....|
00010060  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00010070  00 00 00 40 00 00 00 00  00 00 02 00 00 00 00 00  |...@............|
00010080
$ mkfs.f2fs -t 0 -f test.img
	F2FS-tools: mkfs.f2fs Ver: 1.14.0 (2020-12-28)

Info: Disable heap-based policy
Info: Debug level = 0
Info: Trim is disabled
Info: Segments per section = 1
Info: Sections per zone = 1
Info: sector size = 512
Info: total sectors = 2097152 (1024 MB)
Info: zone aligned segment0 blkaddr: 512
Info: format version with
"Linux version 5.10.46-4rodete1-amd64 ([email protected]) (gcc-10 (Debian 10.2.1-6+build2) 10.2.1 20210110, GNU ld (GNU Binutils for Debian) 2.35.2) #1 SMP Debian 5.10.46-4rodete1 (2021-08-20)"
Info: Overprovision ratio = 6.360%
Info: Overprovision segments = 68 (GC reserved = 39)
Info: format successful

$hexdump -s 0x10000 -n 128 -C test.img

00010000  c2 8a c8 26 00 00 00 00  00 00 00 00 00 00 00 00  |...&............|
00010010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00010020  92 ab 3f c6 b7 82 49 5e  93 23 e8 c9 e9 45 7d ac  |..?...I^.#...E}.|
00010030  00 00 01 00 00 00 00 00  01 00 00 00 00 00 00 00  |................|
00010040  5f 42 48 52 66 53 5f 4d  05 00 00 00 00 00 00 00  |_BHRfS_M........|
00010050  00 40 d2 01 00 00 00 00  00 40 50 01 00 00 00 00  |.@.......@P.....|
00010060  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00010070  00 00 00 40 00 00 00 00  00 00 02 00 00 00 00 00  |...@............|

00010080

--- After this patch ---

$ mkfs.f2fs -t 0 -f test.img
	F2FS-tools: mkfs.f2fs Ver: 1.14.0 (2020-12-28)

Info: Disable heap-based policy
Info: Debug level = 0
Info: Trim is disabled
test.img appears to contain an existing filesystem (btrfs).
Info: Segments per section = 1
Info: Sections per zone = 1
Info: sector size = 512
Info: total sectors = 2097152 (1024 MB)
Info: zone aligned segment0 blkaddr: 512
Info: format version with
"Linux version 5.10.46-4rodete1-amd64 ([email protected]) (gcc-10 (Debian 10.2.1-6+build2) 10.2.1 20210110, GNU ld (GNU Binutils for Debian) 2.35.2) #1 SMP Debian 5.10.46-4rodete1 (2021-08-20)"
Info: Overprovision ratio = 6.360%
Info: Overprovision segments = 68 (GC reserved = 39)
Info: format successful

$ hexdump -s 0x10000 -n 128 -C test.img
00010000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00010080
---
 fsck/main.c             |  2 +-
 include/f2fs_fs.h       |  1 +
 lib/libf2fs.c           |  6 ++++++
 mkfs/f2fs_format_main.c | 26 ++++++++++++++++++++++----
 4 files changed, 30 insertions(+), 5 deletions(-)
fsck/main.c +1 -1
index eda399cf0679..e4cfdf443867 100644
@@ -1120,7 +1120,7 @@ int main(int argc, char **argv)
 	}
 
 	/* Get device */
-	if (f2fs_get_device_info() < 0) {
+	if (f2fs_get_device_info() < 0 || f2fs_get_f2fs_info() < 0) {
 		ret = -1;
 		if (c.func == FSCK)
 			ret = FSCK_OPERATIONAL_ERROR;
include/f2fs_fs.h +1 -0
index 2a9c1169456c..69260a6fccd9 100644
@@ -1341,6 +1341,7 @@ extern int f2fs_devs_are_umounted(void);
 extern int f2fs_dev_is_writable(void);
 extern int f2fs_dev_is_umounted(char *);
 extern int f2fs_get_device_info(void);
+extern int f2fs_get_f2fs_info(void);
 extern unsigned int calc_extra_isize(void);
 extern int get_device_info(int);
 extern int f2fs_init_sparse_file(void);
lib/libf2fs.c +6 -0
index b27785dec757..420dfdae56da 100644
@@ -1183,6 +1183,12 @@ int f2fs_get_device_info(void)
 	for (i = 0; i < c.ndevs; i++)
 		if (get_device_info(i))
 			return -1;
+	return 0;
+}
+
+int f2fs_get_f2fs_info(void)
+{
+	int i;
 
 	if (c.wanted_total_sectors < c.total_sectors) {
 		MSG(0, "Info: total device sectors = %"PRIu64" (in %u bytes)\n",
mkfs/f2fs_format_main.c +22 -4
index 031244d5d67a..de93f1b5b778 100644
@@ -396,18 +396,36 @@ int main(int argc, char *argv[])
 
 	c.func = MKFS;
 
-	if (!force_overwrite && f2fs_check_overwrite()) {
-		MSG(0, "\tUse the -f option to force overwrite.\n");
+	if (f2fs_get_device_info() < 0)
 		return -1;
+
+	if (f2fs_check_overwrite()) {
+		char *zero_buf = NULL;
+		int i;
+
+		if (!force_overwrite) {
+			MSG(0, "\tUse the -f option to force overwrite.\n");
+			goto err_format;
+		}
+		zero_buf = calloc(F2FS_BLKSIZE, 1);
+		if (!zero_buf) {
+			MSG(0, "\tFaile to allocate zero buffer.\n");
+			goto err_format;
+		}
+		/* wipe out other FS magics mostly first 4MB space */
+		for (i = 0; i < 1024; i++)
+			dev_fill_block(zero_buf, i);
+		free(zero_buf);
+		f2fs_fsync_device();
 	}
 
 	if (f2fs_devs_are_umounted() < 0) {
 		if (errno != EBUSY)
 			MSG(0, "\tError: Not available on mounted device!\n");
-		return -1;
+		goto err_format;
 	}
 
-	if (f2fs_get_device_info() < 0)
+	if (f2fs_get_f2fs_info() < 0)
 		goto err_format;
 
 	/*
Signature · 8 lines
2.33.0.309.g3052b89438-goog



_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
#1 lore raw
Quoted 144 lines · Jaegeuk Kim

This patch fixes the below stale magic info.

$ mkfs.btrfs -f test.img
btrfs-progs v5.10.1
See http://btrfs.wiki.kernel.org for more information.
Label:              (null)
UUID:               941d2db7-3ece-4090-8b22-c4ea548b5dae
Node size:          16384
Sector size:        4096
Filesystem size:    1.00GiB
Block group profiles:
  Data:             single            8.00MiB
  Metadata:         DUP              51.19MiB
  System:           DUP               8.00MiB
SSD detected:       no
Incompat features:  extref, skinny-metadata
Runtime features:
Checksum:           crc32c
Number of devices:  1
Devices:
   ID        SIZE  PATH
    1     1.00GiB  test.img
$ hexdump -s 0x10000 -n 128 -C test.img
00010000  81 29 94 0a 00 00 00 00  00 00 00 00 00 00 00 00  |.)..............|
00010010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00010020  94 1d 2d b7 3e ce 40 90  8b 22 c4 ea 54 8b 5d ae  |..-.>.@.."..T.].|
00010030  00 00 01 00 00 00 00 00  01 00 00 00 00 00 00 00  |................|
00010040  5f 42 48 52 66 53 5f 4d  05 00 00 00 00 00 00 00  |_BHRfS_M........|
00010050  00 40 d2 01 00 00 00 00  00 40 50 01 00 00 00 00  |.@.......@P.....|
00010060  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00010070  00 00 00 40 00 00 00 00  00 00 02 00 00 00 00 00  |...@............|
00010080
$ mkfs.f2fs -t 0 -f test.img
	F2FS-tools: mkfs.f2fs Ver: 1.14.0 (2020-12-28)

Info: Disable heap-based policy
Info: Debug level = 0
Info: Trim is disabled
Info: Segments per section = 1
Info: Sections per zone = 1
Info: sector size = 512
Info: total sectors = 2097152 (1024 MB)
Info: zone aligned segment0 blkaddr: 512
Info: format version with
"Linux version 5.10.46-4rodete1-amd64 ([email protected]) (gcc-10 (Debian 10.2.1-6+build2) 10.2.1 20210110, GNU ld (GNU Binutils for Debian) 2.35.2) #1 SMP Debian 5.10.46-4rodete1 (2021-08-20)"
Info: Overprovision ratio = 6.360%
Info: Overprovision segments = 68 (GC reserved = 39)
Info: format successful

$hexdump -s 0x10000 -n 128 -C test.img

00010000  c2 8a c8 26 00 00 00 00  00 00 00 00 00 00 00 00  |...&............|
00010010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00010020  92 ab 3f c6 b7 82 49 5e  93 23 e8 c9 e9 45 7d ac  |..?...I^.#...E}.|
00010030  00 00 01 00 00 00 00 00  01 00 00 00 00 00 00 00  |................|
00010040  5f 42 48 52 66 53 5f 4d  05 00 00 00 00 00 00 00  |_BHRfS_M........|
00010050  00 40 d2 01 00 00 00 00  00 40 50 01 00 00 00 00  |.@.......@P.....|
00010060  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00010070  00 00 00 40 00 00 00 00  00 00 02 00 00 00 00 00  |...@............|

00010080

--- After this patch ---

$ mkfs.f2fs -t 0 -f test.img
	F2FS-tools: mkfs.f2fs Ver: 1.14.0 (2020-12-28)

Info: Disable heap-based policy
Info: Debug level = 0
Info: Trim is disabled
test.img appears to contain an existing filesystem (btrfs).
Info: Segments per section = 1
Info: Sections per zone = 1
Info: sector size = 512
Info: total sectors = 2097152 (1024 MB)
Info: zone aligned segment0 blkaddr: 512
Info: format version with
"Linux version 5.10.46-4rodete1-amd64 ([email protected]) (gcc-10 (Debian 10.2.1-6+build2) 10.2.1 20210110, GNU ld (GNU Binutils for Debian) 2.35.2) #1 SMP Debian 5.10.46-4rodete1 (2021-08-20)"
Info: Overprovision ratio = 6.360%
Info: Overprovision segments = 68 (GC reserved = 39)
Info: format successful

$ hexdump -s 0x10000 -n 128 -C test.img
00010000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00010080
---
 fsck/main.c             |  2 +-
 include/f2fs_fs.h       |  1 +
 lib/libf2fs.c           |  6 ++++++
 mkfs/f2fs_format_main.c | 26 ++++++++++++++++++++++----
 4 files changed, 30 insertions(+), 5 deletions(-)
fsck/main.c +1 -1
index eda399cf0679..e4cfdf443867 100644
@@ -1120,7 +1120,7 @@ int main(int argc, char **argv)
 	}
 
 	/* Get device */
-	if (f2fs_get_device_info() < 0) {
+	if (f2fs_get_device_info() < 0 || f2fs_get_f2fs_info() < 0) {
 		ret = -1;
 		if (c.func == FSCK)
 			ret = FSCK_OPERATIONAL_ERROR;
include/f2fs_fs.h +1 -0
index 2a9c1169456c..69260a6fccd9 100644
@@ -1341,6 +1341,7 @@ extern int f2fs_devs_are_umounted(void);
 extern int f2fs_dev_is_writable(void);
 extern int f2fs_dev_is_umounted(char *);
 extern int f2fs_get_device_info(void);
+extern int f2fs_get_f2fs_info(void);
 extern unsigned int calc_extra_isize(void);
 extern int get_device_info(int);
 extern int f2fs_init_sparse_file(void);
lib/libf2fs.c +6 -0
index b27785dec757..420dfdae56da 100644
@@ -1183,6 +1183,12 @@ int f2fs_get_device_info(void)
 	for (i = 0; i < c.ndevs; i++)
 		if (get_device_info(i))
 			return -1;
+	return 0;
+}
+
+int f2fs_get_f2fs_info(void)
+{
+	int i;
 
 	if (c.wanted_total_sectors < c.total_sectors) {
 		MSG(0, "Info: total device sectors = %"PRIu64" (in %u bytes)\n",
mkfs/f2fs_format_main.c +0 -0
index 031244d5d67a..de93f1b5b778 100644

mkfs/f2fs_format_main.c int main(int argc, char *argv[]) · line 400

Quoted 7 lines
mkfs/f2fs_format_main.c +1 -2
@@ -396,18 +396,36 @@ int main(int argc, char *argv[])
 
 	c.func = MKFS;
 
-	if (!force_overwrite && f2fs_check_overwrite()) {
-		MSG(0, "\tUse the -f option to force overwrite.\n");
+	if (f2fs_get_device_info() < 0)
 		return -1;

Jaegeuk, thanks for the patch. Today, I had a chance to try out the dev branch with this patch, and observed mkfs.f2fs for block devices failed with the messages as follows:

$ sudo mkfs.f2fs -f /dev/sdc
        F2FS-tools: mkfs.f2fs Ver: 1.14.0 (2021-09-10)

Info: Disable heap-based policy
Info: Debug level = 0
Info: Trim is enabled
Info: [/dev/sdc] Disk Model: SanDisk SDSSDH32
/dev/sdc appears to contain an existing filesystem (f2fs).
Error: In use by the system!

I think f2fs_get_device_info() call above opens the block device with O_EXCL. And the following f2fs_devs_are_umounted() opens the device again with O_EXCL. The second open results in E_BUSY and the error message. I tried to move the f2fs_devs_are_umounted() call before the f2fs_get_device_info() call, then the error disappeared. So, I suggest to move the f2fs_devs_are_umounted() call.

mkfs/f2fs_format_main.c int main(int argc, char *argv[]) · line 412

Quoted 12 lines
mkfs/f2fs_format_main.c +12 -0
+
+	if (f2fs_check_overwrite()) {
+		char *zero_buf = NULL;
+		int i;
+
+		if (!force_overwrite) {
+			MSG(0, "\tUse the -f option to force overwrite.\n");
+			goto err_format;
+		}
+		zero_buf = calloc(F2FS_BLKSIZE, 1);
+		if (!zero_buf) {
+			MSG(0, "\tFaile to allocate zero buffer.\n");

I guess you meant "Failed" in place of "Faile".

Quoted 30 lines
mkfs/f2fs_format_main.c +9 -2
+			goto err_format;
+		}
+		/* wipe out other FS magics mostly first 4MB space */
+		for (i = 0; i < 1024; i++)
+			dev_fill_block(zero_buf, i);
+		free(zero_buf);
+		f2fs_fsync_device();
 	}
 
 	if (f2fs_devs_are_umounted() < 0) {
 		if (errno != EBUSY)
 			MSG(0, "\tError: Not available on mounted device!\n");
-		return -1;
+		goto err_format;
 	}
 
-	if (f2fs_get_device_info() < 0)
+	if (f2fs_get_f2fs_info() < 0)
 		goto err_format;
 
 	/*
Signature · 8 lines
2.33.0.309.g3052b89438-goog



_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
Signature · 7 lines
Best Regards,
Shin'ichiro Kawasaki

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
#2 lore raw
Quoted 168 lines · Shinichiro Kawasaki
Quoted 144 lines · Jaegeuk Kim

This patch fixes the below stale magic info.

$ mkfs.btrfs -f test.img
btrfs-progs v5.10.1
See http://btrfs.wiki.kernel.org for more information.
Label:              (null)
UUID:               941d2db7-3ece-4090-8b22-c4ea548b5dae
Node size:          16384
Sector size:        4096
Filesystem size:    1.00GiB
Block group profiles:
  Data:             single            8.00MiB
  Metadata:         DUP              51.19MiB
  System:           DUP               8.00MiB
SSD detected:       no
Incompat features:  extref, skinny-metadata
Runtime features:
Checksum:           crc32c
Number of devices:  1
Devices:
   ID        SIZE  PATH
    1     1.00GiB  test.img
$ hexdump -s 0x10000 -n 128 -C test.img
00010000  81 29 94 0a 00 00 00 00  00 00 00 00 00 00 00 00  |.)..............|
00010010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00010020  94 1d 2d b7 3e ce 40 90  8b 22 c4 ea 54 8b 5d ae  |..-.>.@.."..T.].|
00010030  00 00 01 00 00 00 00 00  01 00 00 00 00 00 00 00  |................|
00010040  5f 42 48 52 66 53 5f 4d  05 00 00 00 00 00 00 00  |_BHRfS_M........|
00010050  00 40 d2 01 00 00 00 00  00 40 50 01 00 00 00 00  |.@.......@P.....|
00010060  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00010070  00 00 00 40 00 00 00 00  00 00 02 00 00 00 00 00  |...@............|
00010080
$ mkfs.f2fs -t 0 -f test.img
	F2FS-tools: mkfs.f2fs Ver: 1.14.0 (2020-12-28)

Info: Disable heap-based policy
Info: Debug level = 0
Info: Trim is disabled
Info: Segments per section = 1
Info: Sections per zone = 1
Info: sector size = 512
Info: total sectors = 2097152 (1024 MB)
Info: zone aligned segment0 blkaddr: 512
Info: format version with
"Linux version 5.10.46-4rodete1-amd64 ([email protected]) (gcc-10 (Debian 10.2.1-6+build2) 10.2.1 20210110, GNU ld (GNU Binutils for Debian) 2.35.2) #1 SMP Debian 5.10.46-4rodete1 (2021-08-20)"
Info: Overprovision ratio = 6.360%
Info: Overprovision segments = 68 (GC reserved = 39)
Info: format successful

$hexdump -s 0x10000 -n 128 -C test.img

00010000  c2 8a c8 26 00 00 00 00  00 00 00 00 00 00 00 00  |...&............|
00010010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00010020  92 ab 3f c6 b7 82 49 5e  93 23 e8 c9 e9 45 7d ac  |..?...I^.#...E}.|
00010030  00 00 01 00 00 00 00 00  01 00 00 00 00 00 00 00  |................|
00010040  5f 42 48 52 66 53 5f 4d  05 00 00 00 00 00 00 00  |_BHRfS_M........|
00010050  00 40 d2 01 00 00 00 00  00 40 50 01 00 00 00 00  |.@.......@P.....|
00010060  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00010070  00 00 00 40 00 00 00 00  00 00 02 00 00 00 00 00  |...@............|

00010080

--- After this patch ---

$ mkfs.f2fs -t 0 -f test.img
	F2FS-tools: mkfs.f2fs Ver: 1.14.0 (2020-12-28)

Info: Disable heap-based policy
Info: Debug level = 0
Info: Trim is disabled
test.img appears to contain an existing filesystem (btrfs).
Info: Segments per section = 1
Info: Sections per zone = 1
Info: sector size = 512
Info: total sectors = 2097152 (1024 MB)
Info: zone aligned segment0 blkaddr: 512
Info: format version with
"Linux version 5.10.46-4rodete1-amd64 ([email protected]) (gcc-10 (Debian 10.2.1-6+build2) 10.2.1 20210110, GNU ld (GNU Binutils for Debian) 2.35.2) #1 SMP Debian 5.10.46-4rodete1 (2021-08-20)"
Info: Overprovision ratio = 6.360%
Info: Overprovision segments = 68 (GC reserved = 39)
Info: format successful

$ hexdump -s 0x10000 -n 128 -C test.img
00010000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00010080
---
 fsck/main.c             |  2 +-
 include/f2fs_fs.h       |  1 +
 lib/libf2fs.c           |  6 ++++++
 mkfs/f2fs_format_main.c | 26 ++++++++++++++++++++++----
 4 files changed, 30 insertions(+), 5 deletions(-)
fsck/main.c +1 -1
index eda399cf0679..e4cfdf443867 100644
@@ -1120,7 +1120,7 @@ int main(int argc, char **argv)
 	}
 
 	/* Get device */
-	if (f2fs_get_device_info() < 0) {
+	if (f2fs_get_device_info() < 0 || f2fs_get_f2fs_info() < 0) {
 		ret = -1;
 		if (c.func == FSCK)
 			ret = FSCK_OPERATIONAL_ERROR;
include/f2fs_fs.h +1 -0
index 2a9c1169456c..69260a6fccd9 100644
@@ -1341,6 +1341,7 @@ extern int f2fs_devs_are_umounted(void);
 extern int f2fs_dev_is_writable(void);
 extern int f2fs_dev_is_umounted(char *);
 extern int f2fs_get_device_info(void);
+extern int f2fs_get_f2fs_info(void);
 extern unsigned int calc_extra_isize(void);
 extern int get_device_info(int);
 extern int f2fs_init_sparse_file(void);
lib/libf2fs.c +6 -0
index b27785dec757..420dfdae56da 100644
@@ -1183,6 +1183,12 @@ int f2fs_get_device_info(void)
 	for (i = 0; i < c.ndevs; i++)
 		if (get_device_info(i))
 			return -1;
+	return 0;
+}
+
+int f2fs_get_f2fs_info(void)
+{
+	int i;
 
 	if (c.wanted_total_sectors < c.total_sectors) {
 		MSG(0, "Info: total device sectors = %"PRIu64" (in %u bytes)\n",
mkfs/f2fs_format_main.c +0 -0
index 031244d5d67a..de93f1b5b778 100644

mkfs/f2fs_format_main.c int main(int argc, char *argv[]) · line 400

Quoted 7 lines
mkfs/f2fs_format_main.c +1 -2
@@ -396,18 +396,36 @@ int main(int argc, char *argv[])
 
 	c.func = MKFS;
 
-	if (!force_overwrite && f2fs_check_overwrite()) {
-		MSG(0, "\tUse the -f option to force overwrite.\n");
+	if (f2fs_get_device_info() < 0)
 		return -1;

Jaegeuk, thanks for the patch. Today, I had a chance to try out the dev branch with this patch, and observed mkfs.f2fs for block devices failed with the messages as follows:

$ sudo mkfs.f2fs -f /dev/sdc
        F2FS-tools: mkfs.f2fs Ver: 1.14.0 (2021-09-10)

Info: Disable heap-based policy
Info: Debug level = 0
Info: Trim is enabled
Info: [/dev/sdc] Disk Model: SanDisk SDSSDH32
/dev/sdc appears to contain an existing filesystem (f2fs).
Error: In use by the system!

Quoted 5 lines

I think f2fs_get_device_info() call above opens the block device with O_EXCL. And the following f2fs_devs_are_umounted() opens the device again with O_EXCL. The second open results in E_BUSY and the error message. I tried to move the f2fs_devs_are_umounted() call before the f2fs_get_device_info() call, then the error disappeared. So, I suggest to move the f2fs_devs_are_umounted() call.

Thank you. Could you please take a look at v2?

Quoted 42 lines

mkfs/f2fs_format_main.c int main(int argc, char *argv[]) · line 412

Quoted 13 lines
mkfs/f2fs_format_main.c +12 -0
+
+	if (f2fs_check_overwrite()) {
+		char *zero_buf = NULL;
+		int i;
+
+		if (!force_overwrite) {
+			MSG(0, "\tUse the -f option to force overwrite.\n");
+			goto err_format;
+		}
+		zero_buf = calloc(F2FS_BLKSIZE, 1);
+		if (!zero_buf) {
+			MSG(0, "\tFaile to allocate zero buffer.\n");

I guess you meant "Failed" in place of "Faile".

Quoted 26 lines
mkfs/f2fs_format_main.c +9 -2
+			goto err_format;
+		}
+		/* wipe out other FS magics mostly first 4MB space */
+		for (i = 0; i < 1024; i++)
+			dev_fill_block(zero_buf, i);
+		free(zero_buf);
+		f2fs_fsync_device();
 	}
 
 	if (f2fs_devs_are_umounted() < 0) {
 		if (errno != EBUSY)
 			MSG(0, "\tError: Not available on mounted device!\n");
-		return -1;
+		goto err_format;
 	}
 
-	if (f2fs_get_device_info() < 0)
+	if (f2fs_get_f2fs_info() < 0)
 		goto err_format;
 
 	/*
Signature · 1 line
2.33.0.309.g3052b89438-goog
Quoted 8 lines
Quoted 4 lines

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

Signature · 2 lines
Best Regards,
Shin'ichiro Kawasaki

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#3 lore raw
Quoted 169 lines · Jaegeuk Kim
Quoted 168 lines · Shinichiro Kawasaki
Quoted 144 lines · Jaegeuk Kim

This patch fixes the below stale magic info.

$ mkfs.btrfs -f test.img
btrfs-progs v5.10.1
See http://btrfs.wiki.kernel.org for more information.
Label:              (null)
UUID:               941d2db7-3ece-4090-8b22-c4ea548b5dae
Node size:          16384
Sector size:        4096
Filesystem size:    1.00GiB
Block group profiles:
  Data:             single            8.00MiB
  Metadata:         DUP              51.19MiB
  System:           DUP               8.00MiB
SSD detected:       no
Incompat features:  extref, skinny-metadata
Runtime features:
Checksum:           crc32c
Number of devices:  1
Devices:
   ID        SIZE  PATH
    1     1.00GiB  test.img
$ hexdump -s 0x10000 -n 128 -C test.img
00010000  81 29 94 0a 00 00 00 00  00 00 00 00 00 00 00 00  |.)..............|
00010010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00010020  94 1d 2d b7 3e ce 40 90  8b 22 c4 ea 54 8b 5d ae  |..-.>.@.."..T.].|
00010030  00 00 01 00 00 00 00 00  01 00 00 00 00 00 00 00  |................|
00010040  5f 42 48 52 66 53 5f 4d  05 00 00 00 00 00 00 00  |_BHRfS_M........|
00010050  00 40 d2 01 00 00 00 00  00 40 50 01 00 00 00 00  |.@.......@P.....|
00010060  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00010070  00 00 00 40 00 00 00 00  00 00 02 00 00 00 00 00  |...@............|
00010080
$ mkfs.f2fs -t 0 -f test.img
	F2FS-tools: mkfs.f2fs Ver: 1.14.0 (2020-12-28)

Info: Disable heap-based policy
Info: Debug level = 0
Info: Trim is disabled
Info: Segments per section = 1
Info: Sections per zone = 1
Info: sector size = 512
Info: total sectors = 2097152 (1024 MB)
Info: zone aligned segment0 blkaddr: 512
Info: format version with
"Linux version 5.10.46-4rodete1-amd64 ([email protected]) (gcc-10 (Debian 10.2.1-6+build2) 10.2.1 20210110, GNU ld (GNU Binutils for Debian) 2.35.2) #1 SMP Debian 5.10.46-4rodete1 (2021-08-20)"
Info: Overprovision ratio = 6.360%
Info: Overprovision segments = 68 (GC reserved = 39)
Info: format successful

$hexdump -s 0x10000 -n 128 -C test.img

00010000  c2 8a c8 26 00 00 00 00  00 00 00 00 00 00 00 00  |...&............|
00010010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00010020  92 ab 3f c6 b7 82 49 5e  93 23 e8 c9 e9 45 7d ac  |..?...I^.#...E}.|
00010030  00 00 01 00 00 00 00 00  01 00 00 00 00 00 00 00  |................|
00010040  5f 42 48 52 66 53 5f 4d  05 00 00 00 00 00 00 00  |_BHRfS_M........|
00010050  00 40 d2 01 00 00 00 00  00 40 50 01 00 00 00 00  |.@.......@P.....|
00010060  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00010070  00 00 00 40 00 00 00 00  00 00 02 00 00 00 00 00  |...@............|

00010080

--- After this patch ---

$ mkfs.f2fs -t 0 -f test.img
	F2FS-tools: mkfs.f2fs Ver: 1.14.0 (2020-12-28)

Info: Disable heap-based policy
Info: Debug level = 0
Info: Trim is disabled
test.img appears to contain an existing filesystem (btrfs).
Info: Segments per section = 1
Info: Sections per zone = 1
Info: sector size = 512
Info: total sectors = 2097152 (1024 MB)
Info: zone aligned segment0 blkaddr: 512
Info: format version with
"Linux version 5.10.46-4rodete1-amd64 ([email protected]) (gcc-10 (Debian 10.2.1-6+build2) 10.2.1 20210110, GNU ld (GNU Binutils for Debian) 2.35.2) #1 SMP Debian 5.10.46-4rodete1 (2021-08-20)"
Info: Overprovision ratio = 6.360%
Info: Overprovision segments = 68 (GC reserved = 39)
Info: format successful

$ hexdump -s 0x10000 -n 128 -C test.img
00010000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00010080
---
 fsck/main.c             |  2 +-
 include/f2fs_fs.h       |  1 +
 lib/libf2fs.c           |  6 ++++++
 mkfs/f2fs_format_main.c | 26 ++++++++++++++++++++++----
 4 files changed, 30 insertions(+), 5 deletions(-)
fsck/main.c +1 -1
index eda399cf0679..e4cfdf443867 100644
@@ -1120,7 +1120,7 @@ int main(int argc, char **argv)
 	}
 
 	/* Get device */
-	if (f2fs_get_device_info() < 0) {
+	if (f2fs_get_device_info() < 0 || f2fs_get_f2fs_info() < 0) {
 		ret = -1;
 		if (c.func == FSCK)
 			ret = FSCK_OPERATIONAL_ERROR;
include/f2fs_fs.h +1 -0
index 2a9c1169456c..69260a6fccd9 100644
@@ -1341,6 +1341,7 @@ extern int f2fs_devs_are_umounted(void);
 extern int f2fs_dev_is_writable(void);
 extern int f2fs_dev_is_umounted(char *);
 extern int f2fs_get_device_info(void);
+extern int f2fs_get_f2fs_info(void);
 extern unsigned int calc_extra_isize(void);
 extern int get_device_info(int);
 extern int f2fs_init_sparse_file(void);
lib/libf2fs.c +6 -0
index b27785dec757..420dfdae56da 100644
@@ -1183,6 +1183,12 @@ int f2fs_get_device_info(void)
 	for (i = 0; i < c.ndevs; i++)
 		if (get_device_info(i))
 			return -1;
+	return 0;
+}
+
+int f2fs_get_f2fs_info(void)
+{
+	int i;
 
 	if (c.wanted_total_sectors < c.total_sectors) {
 		MSG(0, "Info: total device sectors = %"PRIu64" (in %u bytes)\n",
mkfs/f2fs_format_main.c +0 -0
index 031244d5d67a..de93f1b5b778 100644

mkfs/f2fs_format_main.c int main(int argc, char *argv[]) · line 400

Quoted 7 lines
mkfs/f2fs_format_main.c +1 -2
@@ -396,18 +396,36 @@ int main(int argc, char *argv[])
 
 	c.func = MKFS;
 
-	if (!force_overwrite && f2fs_check_overwrite()) {
-		MSG(0, "\tUse the -f option to force overwrite.\n");
+	if (f2fs_get_device_info() < 0)
 		return -1;

Jaegeuk, thanks for the patch. Today, I had a chance to try out the dev branch with this patch, and observed mkfs.f2fs for block devices failed with the messages as follows:

$ sudo mkfs.f2fs -f /dev/sdc
        F2FS-tools: mkfs.f2fs Ver: 1.14.0 (2021-09-10)

Info: Disable heap-based policy
Info: Debug level = 0
Info: Trim is enabled
Info: [/dev/sdc] Disk Model: SanDisk SDSSDH32
/dev/sdc appears to contain an existing filesystem (f2fs).
Error: In use by the system!

Quoted 7 lines
Quoted 5 lines

I think f2fs_get_device_info() call above opens the block device with O_EXCL. And the following f2fs_devs_are_umounted() opens the device again with O_EXCL. The second open results in E_BUSY and the error message. I tried to move the f2fs_devs_are_umounted() call before the f2fs_get_device_info() call, then the error disappeared. So, I suggest to move the f2fs_devs_are_umounted() call.

Thank you. Could you please take a look at v2?

With the v2 patch, the error about O_EXCL open went away. Thanks!

Signature · 7 lines
Best Regards,
Shin'ichiro Kawasaki

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
#4 lore raw
In reply to Jaegeuk Kim
Quoted 158 lines · Jaegeuk Kim

This patch fixes the below stale magic info.

$ mkfs.btrfs -f test.img
btrfs-progs v5.10.1
See http://btrfs.wiki.kernel.org for more information.
Label:              (null)
UUID:               941d2db7-3ece-4090-8b22-c4ea548b5dae
Node size:          16384
Sector size:        4096
Filesystem size:    1.00GiB
Block group profiles:
   Data:             single            8.00MiB
   Metadata:         DUP              51.19MiB
   System:           DUP               8.00MiB
SSD detected:       no
Incompat features:  extref, skinny-metadata
Runtime features:
Checksum:           crc32c
Number of devices:  1

Devices:
ID SIZE PATH
1 1.00GiB test.img

$ hexdump -s 0x10000 -n 128 -C test.img
00010000  81 29 94 0a 00 00 00 00  00 00 00 00 00 00 00 00  |.)..............|
00010010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00010020  94 1d 2d b7 3e ce 40 90  8b 22 c4 ea 54 8b 5d ae  |..-.>.@.."..T.].|
00010030  00 00 01 00 00 00 00 00  01 00 00 00 00 00 00 00  |................|
00010040  5f 42 48 52 66 53 5f 4d  05 00 00 00 00 00 00 00  |_BHRfS_M........|
00010050  00 40 d2 01 00 00 00 00  00 40 50 01 00 00 00 00  |.@.......@P.....|
00010060  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00010070  00 00 00 40 00 00 00 00  00 00 02 00 00 00 00 00  |...@............|
00010080
$ mkfs.f2fs -t 0 -f test.img
	F2FS-tools: mkfs.f2fs Ver: 1.14.0 (2020-12-28)

Info: Disable heap-based policy
Info: Debug level = 0
Info: Trim is disabled
Info: Segments per section = 1
Info: Sections per zone = 1
Info: sector size = 512
Info: total sectors = 2097152 (1024 MB)
Info: zone aligned segment0 blkaddr: 512
Info: format version with
"Linux version 5.10.46-4rodete1-amd64 ([email protected]) (gcc-10 (Debian 10.2.1-6+build2) 10.2.1 20210110, GNU ld (GNU Binutils for Debian) 2.35.2) #1 SMP Debian 5.10.46-4rodete1 (2021-08-20)"
Info: Overprovision ratio = 6.360%
Info: Overprovision segments = 68 (GC reserved = 39)
Info: format successful

$hexdump -s 0x10000 -n 128 -C test.img

00010000  c2 8a c8 26 00 00 00 00  00 00 00 00 00 00 00 00  |...&............|
00010010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00010020  92 ab 3f c6 b7 82 49 5e  93 23 e8 c9 e9 45 7d ac  |..?...I^.#...E}.|
00010030  00 00 01 00 00 00 00 00  01 00 00 00 00 00 00 00  |................|
00010040  5f 42 48 52 66 53 5f 4d  05 00 00 00 00 00 00 00  |_BHRfS_M........|
00010050  00 40 d2 01 00 00 00 00  00 40 50 01 00 00 00 00  |.@.......@P.....|
00010060  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00010070  00 00 00 40 00 00 00 00  00 00 02 00 00 00 00 00  |...@............|

00010080

--- After this patch ---

$ mkfs.f2fs -t 0 -f test.img
	F2FS-tools: mkfs.f2fs Ver: 1.14.0 (2020-12-28)

Info: Disable heap-based policy
Info: Debug level = 0
Info: Trim is disabled
test.img appears to contain an existing filesystem (btrfs).
Info: Segments per section = 1
Info: Sections per zone = 1
Info: sector size = 512
Info: total sectors = 2097152 (1024 MB)
Info: zone aligned segment0 blkaddr: 512
Info: format version with
"Linux version 5.10.46-4rodete1-amd64 ([email protected]) (gcc-10 (Debian 10.2.1-6+build2) 10.2.1 20210110, GNU ld (GNU Binutils for Debian) 2.35.2) #1 SMP Debian 5.10.46-4rodete1 (2021-08-20)"
Info: Overprovision ratio = 6.360%
Info: Overprovision segments = 68 (GC reserved = 39)
Info: format successful

$ hexdump -s 0x10000 -n 128 -C test.img
00010000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00010080
---
  fsck/main.c             |  2 +-
  include/f2fs_fs.h       |  1 +
  lib/libf2fs.c           |  6 ++++++
  mkfs/f2fs_format_main.c | 26 ++++++++++++++++++++++----
  4 files changed, 30 insertions(+), 5 deletions(-)
fsck/main.c +1 -1
index eda399cf0679..e4cfdf443867 100644
@@ -1120,7 +1120,7 @@ int main(int argc, char **argv)
  	}
 
  	/* Get device */
-	if (f2fs_get_device_info() < 0) {
+	if (f2fs_get_device_info() < 0 || f2fs_get_f2fs_info() < 0) {
  		ret = -1;
  		if (c.func == FSCK)
  			ret = FSCK_OPERATIONAL_ERROR;
include/f2fs_fs.h +1 -0
index 2a9c1169456c..69260a6fccd9 100644
@@ -1341,6 +1341,7 @@ extern int f2fs_devs_are_umounted(void);
  extern int f2fs_dev_is_writable(void);
  extern int f2fs_dev_is_umounted(char *);
  extern int f2fs_get_device_info(void);
+extern int f2fs_get_f2fs_info(void);
  extern unsigned int calc_extra_isize(void);
  extern int get_device_info(int);
  extern int f2fs_init_sparse_file(void);
lib/libf2fs.c +6 -0
index b27785dec757..420dfdae56da 100644
@@ -1183,6 +1183,12 @@ int f2fs_get_device_info(void)
  	for (i = 0; i < c.ndevs; i++)
  		if (get_device_info(i))
  			return -1;
+	return 0;
+}
+
+int f2fs_get_f2fs_info(void)
+{
+	int i;
 
  	if (c.wanted_total_sectors < c.total_sectors) {
  		MSG(0, "Info: total device sectors = %"PRIu64" (in %u bytes)\n",
mkfs/f2fs_format_main.c +8 -2
index 031244d5d67a..de93f1b5b778 100644
@@ -396,18 +396,36 @@ int main(int argc, char *argv[])
 
  	c.func = MKFS;
 
-	if (!force_overwrite && f2fs_check_overwrite()) {
-		MSG(0, "\tUse the -f option to force overwrite.\n");
+	if (f2fs_get_device_info() < 0)
  		return -1;
+
+	if (f2fs_check_overwrite()) {
+		char *zero_buf = NULL;
+		int i;
+
+		if (!force_overwrite) {
+			MSG(0, "\tUse the -f option to force overwrite.\n");

mkfs/f2fs_format_main.c int main(int argc, char *argv[]) · line 417

Quoted 10 lines
mkfs/f2fs_format_main.c +10 -0
+			goto err_format;
+		}
+		zero_buf = calloc(F2FS_BLKSIZE, 1);
+		if (!zero_buf) {
+			MSG(0, "\tFaile to allocate zero buffer.\n");
+			goto err_format;
+		}
+		/* wipe out other FS magics mostly first 4MB space */
+		for (i = 0; i < 1024; i++)
+			dev_fill_block(zero_buf, i);
if (dev_fill_block()) {
	free(zero_buf);
	MSG(0, "\tFaile to zeroout blocks.\n");
	goto err_format;
}

mkfs/f2fs_format_main.c int main(int argc, char *argv[]) · line 419

Quoted 2 lines
mkfs/f2fs_format_main.c +2 -0
+		free(zero_buf);
+		f2fs_fsync_device();
if (f2fs_fsync_device()) {
	MSG(0, "\tFaile to fsync device.\n");
	goto err_format;
}

Thanks,

Quoted 5 lines
mkfs/f2fs_format_main.c +0 -0
  	}
 
  	if (f2fs_devs_are_umounted() < 0) {
  		if (errno != EBUSY)

mkfs/f2fs_format_main.c int main(int argc, char *argv[]) · line 431

Quoted 10 lines
mkfs/f2fs_format_main.c +2 -2
  			MSG(0, "\tError: Not available on mounted device!\n");
-		return -1;
+		goto err_format;
  	}
 
-	if (f2fs_get_device_info() < 0)
+	if (f2fs_get_f2fs_info() < 0)
  		goto err_format;
 
  	/*

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#5 lore raw
In reply to Jaegeuk Kim

This patch fixes the below stale magic info.

$ mkfs.btrfs -f test.img
btrfs-progs v5.10.1
See http://btrfs.wiki.kernel.org for more information.
Label:              (null)
UUID:               941d2db7-3ece-4090-8b22-c4ea548b5dae
Node size:          16384
Sector size:        4096
Filesystem size:    1.00GiB
Block group profiles:
  Data:             single            8.00MiB
  Metadata:         DUP              51.19MiB
  System:           DUP               8.00MiB
SSD detected:       no
Incompat features:  extref, skinny-metadata
Runtime features:
Checksum:           crc32c
Number of devices:  1
Devices:
   ID        SIZE  PATH
    1     1.00GiB  test.img
$ hexdump -s 0x10000 -n 128 -C test.img
00010000  81 29 94 0a 00 00 00 00  00 00 00 00 00 00 00 00  |.)..............|
00010010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00010020  94 1d 2d b7 3e ce 40 90  8b 22 c4 ea 54 8b 5d ae  |..-.>.@.."..T.].|
00010030  00 00 01 00 00 00 00 00  01 00 00 00 00 00 00 00  |................|
00010040  5f 42 48 52 66 53 5f 4d  05 00 00 00 00 00 00 00  |_BHRfS_M........|
00010050  00 40 d2 01 00 00 00 00  00 40 50 01 00 00 00 00  |.@.......@P.....|
00010060  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00010070  00 00 00 40 00 00 00 00  00 00 02 00 00 00 00 00  |...@............|
00010080
$ mkfs.f2fs -t 0 -f test.img
	F2FS-tools: mkfs.f2fs Ver: 1.14.0 (2020-12-28)

Info: Disable heap-based policy
Info: Debug level = 0
Info: Trim is disabled
Info: Segments per section = 1
Info: Sections per zone = 1
Info: sector size = 512
Info: total sectors = 2097152 (1024 MB)
Info: zone aligned segment0 blkaddr: 512
Info: format version with
"Linux version 5.10.46-4rodete1-amd64 ([email protected]) (gcc-10 (Debian 10.2.1-6+build2) 10.2.1 20210110, GNU ld (GNU Binutils for Debian) 2.35.2) #1 SMP Debian 5.10.46-4rodete1 (2021-08-20)"
Info: Overprovision ratio = 6.360%
Info: Overprovision segments = 68 (GC reserved = 39)
Info: format successful

$hexdump -s 0x10000 -n 128 -C test.img

00010000  c2 8a c8 26 00 00 00 00  00 00 00 00 00 00 00 00  |...&............|
00010010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00010020  92 ab 3f c6 b7 82 49 5e  93 23 e8 c9 e9 45 7d ac  |..?...I^.#...E}.|
00010030  00 00 01 00 00 00 00 00  01 00 00 00 00 00 00 00  |................|
00010040  5f 42 48 52 66 53 5f 4d  05 00 00 00 00 00 00 00  |_BHRfS_M........|
00010050  00 40 d2 01 00 00 00 00  00 40 50 01 00 00 00 00  |.@.......@P.....|
00010060  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00010070  00 00 00 40 00 00 00 00  00 00 02 00 00 00 00 00  |...@............|

00010080

--- After this patch ---

$ mkfs.f2fs -t 0 -f test.img
	F2FS-tools: mkfs.f2fs Ver: 1.14.0 (2020-12-28)

Info: Disable heap-based policy
Info: Debug level = 0
Info: Trim is disabled
test.img appears to contain an existing filesystem (btrfs).
Info: Segments per section = 1
Info: Sections per zone = 1
Info: sector size = 512
Info: total sectors = 2097152 (1024 MB)
Info: zone aligned segment0 blkaddr: 512
Info: format version with
"Linux version 5.10.46-4rodete1-amd64 ([email protected]) (gcc-10 (Debian 10.2.1-6+build2) 10.2.1 20210110, GNU ld (GNU Binutils for Debian) 2.35.2) #1 SMP Debian 5.10.46-4rodete1 (2021-08-20)"
Info: Overprovision ratio = 6.360%
Info: Overprovision segments = 68 (GC reserved = 39)
Info: format successful

$ hexdump -s 0x10000 -n 128 -C test.img
00010000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00010080

---

Change log from v1:
- add error handlers
- call f2fs_devs_are_umounted() followed by f2fs_get_device_info() to fix open(O_EXCL)

 fsck/main.c             |  2 +-
 include/f2fs_fs.h       |  1 +
 lib/libf2fs.c           |  6 ++++++
 mkfs/f2fs_format_main.c | 36 ++++++++++++++++++++++++++++++------
 4 files changed, 38 insertions(+), 7 deletions(-)
fsck/main.c +1 -1
index eda399cf0679..e4cfdf443867 100644
@@ -1120,7 +1120,7 @@ int main(int argc, char **argv)
 	}
 
 	/* Get device */
-	if (f2fs_get_device_info() < 0) {
+	if (f2fs_get_device_info() < 0 || f2fs_get_f2fs_info() < 0) {
 		ret = -1;
 		if (c.func == FSCK)
 			ret = FSCK_OPERATIONAL_ERROR;
include/f2fs_fs.h +1 -0
index 2a9c1169456c..69260a6fccd9 100644
@@ -1341,6 +1341,7 @@ extern int f2fs_devs_are_umounted(void);
 extern int f2fs_dev_is_writable(void);
 extern int f2fs_dev_is_umounted(char *);
 extern int f2fs_get_device_info(void);
+extern int f2fs_get_f2fs_info(void);
 extern unsigned int calc_extra_isize(void);
 extern int get_device_info(int);
 extern int f2fs_init_sparse_file(void);
lib/libf2fs.c +6 -0
index b27785dec757..420dfdae56da 100644
@@ -1183,6 +1183,12 @@ int f2fs_get_device_info(void)
 	for (i = 0; i < c.ndevs; i++)
 		if (get_device_info(i))
 			return -1;
+	return 0;
+}
+
+int f2fs_get_f2fs_info(void)
+{
+	int i;
 
 	if (c.wanted_total_sectors < c.total_sectors) {
 		MSG(0, "Info: total device sectors = %"PRIu64" (in %u bytes)\n",
mkfs/f2fs_format_main.c +30 -6
index 031244d5d67a..b89efe691a49 100644
@@ -396,18 +396,42 @@ int main(int argc, char *argv[])
 
 	c.func = MKFS;
 
-	if (!force_overwrite && f2fs_check_overwrite()) {
-		MSG(0, "\tUse the -f option to force overwrite.\n");
-		return -1;
-	}
-
 	if (f2fs_devs_are_umounted() < 0) {
 		if (errno != EBUSY)
 			MSG(0, "\tError: Not available on mounted device!\n");
-		return -1;
+		goto err_format;
 	}
 
 	if (f2fs_get_device_info() < 0)
+		return -1;
+
+	if (f2fs_check_overwrite()) {
+		char *zero_buf = NULL;
+		int i;
+
+		if (!force_overwrite) {
+			MSG(0, "\tUse the -f option to force overwrite.\n");
+			goto err_format;
+		}
+		zero_buf = calloc(F2FS_BLKSIZE, 1);
+		if (!zero_buf) {
+			MSG(0, "\tError: Fail to allocate zero buffer.\n");
+			goto err_format;
+		}
+		/* wipe out other FS magics mostly first 4MB space */
+		for (i = 0; i < 1024; i++)
+			if (dev_fill_block(zero_buf, i))
+				break;
+		free(zero_buf);
+		if (i != 1024) {
+			MSG(0, "\tError: Fail to fill zeros till %d.\n", i);
+			goto err_format;
+		}
+		if (f2fs_fsync_device())
+			goto err_format;
+	}
+
+	if (f2fs_get_f2fs_info() < 0)
 		goto err_format;
 
 	/*
Signature · 8 lines
2.33.0.464.g1972c5931b-goog



_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
Reviewed-by
#6 lore raw
Quoted 84 lines · Jaegeuk Kim

This patch fixes the below stale magic info.

$ mkfs.btrfs -f test.img
btrfs-progs v5.10.1
See http://btrfs.wiki.kernel.org for more information.
Label:              (null)
UUID:               941d2db7-3ece-4090-8b22-c4ea548b5dae
Node size:          16384
Sector size:        4096
Filesystem size:    1.00GiB
Block group profiles:
   Data:             single            8.00MiB
   Metadata:         DUP              51.19MiB
   System:           DUP               8.00MiB
SSD detected:       no
Incompat features:  extref, skinny-metadata
Runtime features:
Checksum:           crc32c
Number of devices:  1

Devices:
ID SIZE PATH
1 1.00GiB test.img

$ hexdump -s 0x10000 -n 128 -C test.img
00010000  81 29 94 0a 00 00 00 00  00 00 00 00 00 00 00 00  |.)..............|
00010010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00010020  94 1d 2d b7 3e ce 40 90  8b 22 c4 ea 54 8b 5d ae  |..-.>.@.."..T.].|
00010030  00 00 01 00 00 00 00 00  01 00 00 00 00 00 00 00  |................|
00010040  5f 42 48 52 66 53 5f 4d  05 00 00 00 00 00 00 00  |_BHRfS_M........|
00010050  00 40 d2 01 00 00 00 00  00 40 50 01 00 00 00 00  |.@.......@P.....|
00010060  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00010070  00 00 00 40 00 00 00 00  00 00 02 00 00 00 00 00  |...@............|
00010080
$ mkfs.f2fs -t 0 -f test.img
	F2FS-tools: mkfs.f2fs Ver: 1.14.0 (2020-12-28)

Info: Disable heap-based policy
Info: Debug level = 0
Info: Trim is disabled
Info: Segments per section = 1
Info: Sections per zone = 1
Info: sector size = 512
Info: total sectors = 2097152 (1024 MB)
Info: zone aligned segment0 blkaddr: 512
Info: format version with
"Linux version 5.10.46-4rodete1-amd64 ([email protected]) (gcc-10 (Debian 10.2.1-6+build2) 10.2.1 20210110, GNU ld (GNU Binutils for Debian) 2.35.2) #1 SMP Debian 5.10.46-4rodete1 (2021-08-20)"
Info: Overprovision ratio = 6.360%
Info: Overprovision segments = 68 (GC reserved = 39)
Info: format successful

$hexdump -s 0x10000 -n 128 -C test.img

00010000  c2 8a c8 26 00 00 00 00  00 00 00 00 00 00 00 00  |...&............|
00010010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00010020  92 ab 3f c6 b7 82 49 5e  93 23 e8 c9 e9 45 7d ac  |..?...I^.#...E}.|
00010030  00 00 01 00 00 00 00 00  01 00 00 00 00 00 00 00  |................|
00010040  5f 42 48 52 66 53 5f 4d  05 00 00 00 00 00 00 00  |_BHRfS_M........|
00010050  00 40 d2 01 00 00 00 00  00 40 50 01 00 00 00 00  |.@.......@P.....|
00010060  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00010070  00 00 00 40 00 00 00 00  00 00 02 00 00 00 00 00  |...@............|

00010080

--- After this patch ---

$ mkfs.f2fs -t 0 -f test.img
	F2FS-tools: mkfs.f2fs Ver: 1.14.0 (2020-12-28)

Info: Disable heap-based policy
Info: Debug level = 0
Info: Trim is disabled
test.img appears to contain an existing filesystem (btrfs).
Info: Segments per section = 1
Info: Sections per zone = 1
Info: sector size = 512
Info: total sectors = 2097152 (1024 MB)
Info: zone aligned segment0 blkaddr: 512
Info: format version with
"Linux version 5.10.46-4rodete1-amd64 ([email protected]) (gcc-10 (Debian 10.2.1-6+build2) 10.2.1 20210110, GNU ld (GNU Binutils for Debian) 2.35.2) #1 SMP Debian 5.10.46-4rodete1 (2021-08-20)"
Info: Overprovision ratio = 6.360%
Info: Overprovision segments = 68 (GC reserved = 39)
Info: format successful

Quoted 6 lines
$ hexdump -s 0x10000 -n 128 -C test.img
00010000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00010080

Thanks,

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel