Skip to contentSkip to conversation

mkfs.f2fs: check zeros in first 16MB for Android

PATCH In review Jaegeuk Kim 2 participants 9 messages Aug 8, 2019 – Aug 16, 2019 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mkfs.f2fs: check zeros in first 16MB for Android

We actually don't need to issue trim on entire disk by checking first blocks having zeros.

---
 mkfs/f2fs_format_utils.c | 47 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 45 insertions(+), 2 deletions(-)
mkfs/f2fs_format_utils.c +45 -2
index 8bf128c..52fedc7 100644
@@ -110,13 +110,56 @@ static int trim_device(int i)
 	return 0;
 }
 
+static int f2fs_zero_blocks(int i)
+{
+#ifdef WITH_ANDROID
+	struct device_info *dev = c.devices + i;
+	int fd = dev->fd;
+	char buf[F2FS_BLKSIZE];
+	char *zero_buf;
+	int j, ret;
+	int ret2 = 0;
+
+	zero_buf = calloc(1, F2FS_BLKSIZE);
+	if (zero_buf == NULL) {
+		MSG(1, "\tError: Malloc Failed for zero buf!!!\n");
+		return -1;
+	}
+
+	/* check first 16MB blocks */
+	for (j = 0; j < 4096; j++) {
+		ret = lseek(fd, j * F2FS_BLKSIZE, SEEK_SET);
+		if (ret < 0) {
+			ret2 = -1;
+			break;
+		}
+		ret = read(fd, buf, F2FS_BLKSIZE);
+		if (ret != F2FS_BLKSIZE) {
+			ret2 = -1;
+			break;
+		}
+		if (memcmp(buf, zero_buf, F2FS_BLKSIZE)) {
+			ret2 = -1;
+			break;
+		}
+	}
+	free(zero_buf);
+	if (!ret2)
+		MSG(0, "Info: Skip discarding blocks (found all zeros\n");
+	return ret2;
+#else
+	return -1;
+#endif
+}
+
 int f2fs_trim_devices(void)
 {
 	int i;
 
-	for (i = 0; i < c.ndevs; i++)
-		if (trim_device(i))
+	for (i = 0; i < c.ndevs; i++) {
+		if (f2fs_zero_blocks(i) && trim_device(i))
 			return -1;
+	}
 	c.trimmed = 1;
 	return 0;
 }
Signature · 8 lines
2.19.0.605.g01d371f741-goog



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

We actually don't need to issue trim on entire disk by checking first blocks having zeros.

---
v2 from v1:
- clean up

 mkfs/f2fs_format_utils.c | 53 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 51 insertions(+), 2 deletions(-)
mkfs/f2fs_format_utils.c +51 -2
index 8bf128c..f2d55ad 100644
@@ -25,6 +25,7 @@
 #include <stdio.h>
 #include <unistd.h>
 #include <stdlib.h>
+#include <stdbool.h>
 #ifndef ANDROID_WINDOWS_HOST
 #include <sys/ioctl.h>
 #endif
@@ -110,13 +111,61 @@ static int trim_device(int i)
 	return 0;
 }
 
+static bool is_wiped_device(int i)
+{
+#ifdef WITH_ANDROID
+	struct device_info *dev = c.devices + i;
+	int fd = dev->fd;
+	char *buf, *zero_buf;
+	bool wiped = true;
+	int nblocks = 4096;	/* 16MB size */
+	int j;
+
+	buf = malloc(F2FS_BLKSIZE);
+	if (buf == NULL) {
+		MSG(1, "\tError: Malloc Failed for buf!!!\n");
+		return false;
+	}
+	zero_buf = calloc(1, F2FS_BLKSIZE);
+	if (zero_buf == NULL) {
+		MSG(1, "\tError: Calloc Failed for zero buf!!!\n");
+		free(buf);
+		return false;
+	}
+
+	if (lseek(fd, 0, SEEK_SET) < 0) {
+		free(zero_buf);
+		free(buf);
+		return false;
+	}
+
+	/* check first n blocks */
+	for (j = 0; j < nblocks; j++) {
+		if (read(fd, buf, F2FS_BLKSIZE) != F2FS_BLKSIZE ||
+				memcmp(buf, zero_buf, F2FS_BLKSIZE)) {
+			wiped = false;
+			break;
+		}
+	}
+	free(zero_buf);
+	free(buf);
+
+	if (wiped)
+		MSG(0, "Info: Found all zeros in first %d blocks\n", nblocks);
+	return wiped;
+#else
+	return false;
+#endif
+}
+
 int f2fs_trim_devices(void)
 {
 	int i;
 
-	for (i = 0; i < c.ndevs; i++)
-		if (trim_device(i))
+	for (i = 0; i < c.ndevs; i++) {
+		if (!is_wiped_device(i) && trim_device(i))
 			return -1;
+	}
 	c.trimmed = 1;
 	return 0;
 }
Signature · 8 lines
2.19.0.605.g01d371f741-goog



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

A message that is not in the archive (sent off-list or not yet received): CAD14+f2V=j8o=0sUGMgmJHmwKgm80WyzJC5yW7qmyffL=CBJhw@mail.gmail.com

#3 lore raw
Quoted 8 lines · Ju Hyung Park

Hi Jaegeuk,

Just out of curiosity, what's the point of this?

I thought flash chips skip erasing blocks if it's already erased to preserve P/E cycles as much as possible.
All Android devices I had(various versions of eMMC and UFS) ran full range block-level discards pretty fast too.

Unfortunately, some of them are giving long delays on a bunch of unmap commands resulting in user janky issue.

Quoted 98 lines

Thanks.

2019년 8월 10일 (토) 오전 12:13, Jaegeuk Kim <[email protected]>님이 작성:

Quoted 93 lines

We actually don't need to issue trim on entire disk by checking first blocks having zeros.

---
v2 from v1:
- clean up

 mkfs/f2fs_format_utils.c | 53 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 51 insertions(+), 2 deletions(-)
mkfs/f2fs_format_utils.c +51 -2
index 8bf128c..f2d55ad 100644
@@ -25,6 +25,7 @@
 #include <stdio.h>
 #include <unistd.h>
 #include <stdlib.h>
+#include <stdbool.h>
 #ifndef ANDROID_WINDOWS_HOST
 #include <sys/ioctl.h>
 #endif
@@ -110,13 +111,61 @@ static int trim_device(int i)
        return 0;
 }
 
+static bool is_wiped_device(int i)
+{
+#ifdef WITH_ANDROID
+       struct device_info *dev = c.devices + i;
+       int fd = dev->fd;
+       char *buf, *zero_buf;
+       bool wiped = true;
+       int nblocks = 4096;     /* 16MB size */
+       int j;
+
+       buf = malloc(F2FS_BLKSIZE);
+       if (buf == NULL) {
+               MSG(1, "\tError: Malloc Failed for buf!!!\n");
+               return false;
+       }
+       zero_buf = calloc(1, F2FS_BLKSIZE);
+       if (zero_buf == NULL) {
+               MSG(1, "\tError: Calloc Failed for zero buf!!!\n");
+               free(buf);
+               return false;
+       }
+
+       if (lseek(fd, 0, SEEK_SET) < 0) {
+               free(zero_buf);
+               free(buf);
+               return false;
+       }
+
+       /* check first n blocks */
+       for (j = 0; j < nblocks; j++) {
+               if (read(fd, buf, F2FS_BLKSIZE) != F2FS_BLKSIZE ||
+                               memcmp(buf, zero_buf, F2FS_BLKSIZE)) {
+                       wiped = false;
+                       break;
+               }
+       }
+       free(zero_buf);
+       free(buf);
+
+       if (wiped)
+               MSG(0, "Info: Found all zeros in first %d blocks\n",
 nblocks);
+       return wiped;
+#else
+       return false;
+#endif
+}
+
 int f2fs_trim_devices(void)
 {
        int i;
 
-       for (i = 0; i < c.ndevs; i++)
-               if (trim_device(i))
+       for (i = 0; i < c.ndevs; i++) {
+               if (!is_wiped_device(i) && trim_device(i))
                        return -1;
+       }
        c.trimmed = 1;
        return 0;

}
--
2.19.0.605.g01d371f741-goog

Quoted 5 lines
Quoted 5 lines

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

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

#4 lore raw
Quoted 5 lines · Jaegeuk Kim
Quoted 4 lines · Ju Hyung Park

Hi Jaegeuk,

Just out of curiosity, what's the point of this?

Quoted 7 lines
Quoted 4 lines

I thought flash chips skip erasing blocks if it's already erased to preserve P/E cycles as much as possible.
All Android devices I had(various versions of eMMC and UFS) ran full range block-level discards pretty fast too.

Unfortunately, some of them are giving long delays on a bunch of unmap commands resulting in user janky issue.

Agreed, the performance completely depends on FTL implementation, some of them perform very bad...

Thanks,

Quoted 99 lines
Quoted 99 lines

Thanks.

2019년 8월 10일 (토) 오전 12:13, Jaegeuk Kim <[email protected]>님이 작성:

Quoted 93 lines

We actually don't need to issue trim on entire disk by checking first blocks having zeros.

---
v2 from v1:
- clean up

 mkfs/f2fs_format_utils.c | 53 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 51 insertions(+), 2 deletions(-)
mkfs/f2fs_format_utils.c +51 -2
index 8bf128c..f2d55ad 100644
@@ -25,6 +25,7 @@
 #include <stdio.h>
 #include <unistd.h>
 #include <stdlib.h>
+#include <stdbool.h>
 #ifndef ANDROID_WINDOWS_HOST
 #include <sys/ioctl.h>
 #endif
@@ -110,13 +111,61 @@ static int trim_device(int i)
        return 0;
 }
 
+static bool is_wiped_device(int i)
+{
+#ifdef WITH_ANDROID
+       struct device_info *dev = c.devices + i;
+       int fd = dev->fd;
+       char *buf, *zero_buf;
+       bool wiped = true;
+       int nblocks = 4096;     /* 16MB size */
+       int j;
+
+       buf = malloc(F2FS_BLKSIZE);
+       if (buf == NULL) {
+               MSG(1, "\tError: Malloc Failed for buf!!!\n");
+               return false;
+       }
+       zero_buf = calloc(1, F2FS_BLKSIZE);
+       if (zero_buf == NULL) {
+               MSG(1, "\tError: Calloc Failed for zero buf!!!\n");
+               free(buf);
+               return false;
+       }
+
+       if (lseek(fd, 0, SEEK_SET) < 0) {
+               free(zero_buf);
+               free(buf);
+               return false;
+       }
+
+       /* check first n blocks */
+       for (j = 0; j < nblocks; j++) {
+               if (read(fd, buf, F2FS_BLKSIZE) != F2FS_BLKSIZE ||
+                               memcmp(buf, zero_buf, F2FS_BLKSIZE)) {
+                       wiped = false;
+                       break;
+               }
+       }
+       free(zero_buf);
+       free(buf);
+
+       if (wiped)
+               MSG(0, "Info: Found all zeros in first %d blocks\n",
 nblocks);
+       return wiped;
+#else
+       return false;
+#endif
+}
+
 int f2fs_trim_devices(void)
 {
        int i;
 
-       for (i = 0; i < c.ndevs; i++)
-               if (trim_device(i))
+       for (i = 0; i < c.ndevs; i++) {
+               if (!is_wiped_device(i) && trim_device(i))
                        return -1;
+       }
        c.trimmed = 1;
        return 0;

}
--
2.19.0.605.g01d371f741-goog

Quoted 12 lines
Quoted 5 lines
Quoted 5 lines

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

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

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

#5 lore raw
In reply to Jaegeuk Kim
Quoted 2 lines · Jaegeuk Kim

We actually don't need to issue trim on entire disk by checking first blocks having zeros.

In heap mode, we locate node log header to tail end of device, should we consider to check block contain according to heap option?

BTW, if we changed cp_ver whenever mkfs, why should we still issue trim to obsolete old data in node remained in image?

Thanks,

Quoted 76 lines

---
v2 from v1:
- clean up

 mkfs/f2fs_format_utils.c | 53 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 51 insertions(+), 2 deletions(-)
mkfs/f2fs_format_utils.c +48 -0
index 8bf128c..f2d55ad 100644
@@ -25,6 +25,7 @@
 #include <stdio.h>
 #include <unistd.h>
 #include <stdlib.h>
+#include <stdbool.h>
 #ifndef ANDROID_WINDOWS_HOST
 #include <sys/ioctl.h>
 #endif
@@ -110,13 +111,61 @@ static int trim_device(int i)
 	return 0;
 }
 
+static bool is_wiped_device(int i)
+{
+#ifdef WITH_ANDROID
+	struct device_info *dev = c.devices + i;
+	int fd = dev->fd;
+	char *buf, *zero_buf;
+	bool wiped = true;
+	int nblocks = 4096;	/* 16MB size */
+	int j;
+
+	buf = malloc(F2FS_BLKSIZE);
+	if (buf == NULL) {
+		MSG(1, "\tError: Malloc Failed for buf!!!\n");
+		return false;
+	}
+	zero_buf = calloc(1, F2FS_BLKSIZE);
+	if (zero_buf == NULL) {
+		MSG(1, "\tError: Calloc Failed for zero buf!!!\n");
+		free(buf);
+		return false;
+	}
+
+	if (lseek(fd, 0, SEEK_SET) < 0) {
+		free(zero_buf);
+		free(buf);
+		return false;
+	}
+
+	/* check first n blocks */
+	for (j = 0; j < nblocks; j++) {
+		if (read(fd, buf, F2FS_BLKSIZE) != F2FS_BLKSIZE ||
+				memcmp(buf, zero_buf, F2FS_BLKSIZE)) {
+			wiped = false;
+			break;
+		}
+	}
+	free(zero_buf);
+	free(buf);
+
+	if (wiped)
+		MSG(0, "Info: Found all zeros in first %d blocks\n", nblocks);
+	return wiped;
+#else
+	return false;
+#endif
+}
+
 int f2fs_trim_devices(void)
 {
 	int i;

mkfs/f2fs_format_utils.c static int trim_device(int i) · line 171

Quoted 10 lines
mkfs/f2fs_format_utils.c +3 -2
 
-	for (i = 0; i < c.ndevs; i++)
-		if (trim_device(i))
+	for (i = 0; i < c.ndevs; i++) {
+		if (!is_wiped_device(i) && trim_device(i))
 			return -1;
+	}
 	c.trimmed = 1;
 	return 0;
 }

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

#6 lore raw
Quoted 6 lines · Chao Yu
Quoted 2 lines · Jaegeuk Kim

We actually don't need to issue trim on entire disk by checking first blocks having zeros.

In heap mode, we locate node log header to tail end of device, should we consider to check block contain according to heap option?

I wanted to check F2FS metadata mainly.

Quoted 3 lines

BTW, if we changed cp_ver whenever mkfs, why should we still issue trim to obsolete old data in node remained in image?

For simplicity. :P

Quoted 79 lines

Thanks,

Quoted 76 lines

---
v2 from v1:
- clean up

 mkfs/f2fs_format_utils.c | 53 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 51 insertions(+), 2 deletions(-)
mkfs/f2fs_format_utils.c +48 -0
index 8bf128c..f2d55ad 100644
@@ -25,6 +25,7 @@
 #include <stdio.h>
 #include <unistd.h>
 #include <stdlib.h>
+#include <stdbool.h>
 #ifndef ANDROID_WINDOWS_HOST
 #include <sys/ioctl.h>
 #endif
@@ -110,13 +111,61 @@ static int trim_device(int i)
 	return 0;
 }
 
+static bool is_wiped_device(int i)
+{
+#ifdef WITH_ANDROID
+	struct device_info *dev = c.devices + i;
+	int fd = dev->fd;
+	char *buf, *zero_buf;
+	bool wiped = true;
+	int nblocks = 4096;	/* 16MB size */
+	int j;
+
+	buf = malloc(F2FS_BLKSIZE);
+	if (buf == NULL) {
+		MSG(1, "\tError: Malloc Failed for buf!!!\n");
+		return false;
+	}
+	zero_buf = calloc(1, F2FS_BLKSIZE);
+	if (zero_buf == NULL) {
+		MSG(1, "\tError: Calloc Failed for zero buf!!!\n");
+		free(buf);
+		return false;
+	}
+
+	if (lseek(fd, 0, SEEK_SET) < 0) {
+		free(zero_buf);
+		free(buf);
+		return false;
+	}
+
+	/* check first n blocks */
+	for (j = 0; j < nblocks; j++) {
+		if (read(fd, buf, F2FS_BLKSIZE) != F2FS_BLKSIZE ||
+				memcmp(buf, zero_buf, F2FS_BLKSIZE)) {
+			wiped = false;
+			break;
+		}
+	}
+	free(zero_buf);
+	free(buf);
+
+	if (wiped)
+		MSG(0, "Info: Found all zeros in first %d blocks\n", nblocks);
+	return wiped;
+#else
+	return false;
+#endif
+}
+
 int f2fs_trim_devices(void)
 {
 	int i;
 
Quoted 10 lines

mkfs/f2fs_format_utils.c static int trim_device(int i) · line 171

Quoted 10 lines
mkfs/f2fs_format_utils.c +3 -2
-	for (i = 0; i < c.ndevs; i++)
-		if (trim_device(i))
+	for (i = 0; i < c.ndevs; i++) {
+		if (!is_wiped_device(i) && trim_device(i))
 			return -1;
+	}
 	c.trimmed = 1;
 	return 0;
 }

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

#7 lore raw
Quoted 9 lines · Jaegeuk Kim
Quoted 6 lines · Chao Yu
Quoted 2 lines · Jaegeuk Kim

We actually don't need to issue trim on entire disk by checking first blocks having zeros.

In heap mode, we locate node log header to tail end of device, should we consider to check block contain according to heap option?

I wanted to check F2FS metadata mainly.

Oh, I thought you mean main area. :P

Quoted 6 lines
Quoted 4 lines

BTW, if we changed cp_ver whenever mkfs, why should we still issue trim to obsolete old data in node remained in image?

For simplicity. :P

I didn't get why we can assume all metadata are zeroed if first 16MB are all zero...

BTW, if first 16MB are non-zero, why not just trim F2FS metadata rather than whole area?

Thanks,

Quoted 80 lines
Quoted 80 lines

Thanks,

Quoted 76 lines

---
v2 from v1:
- clean up

 mkfs/f2fs_format_utils.c | 53 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 51 insertions(+), 2 deletions(-)
mkfs/f2fs_format_utils.c +48 -0
index 8bf128c..f2d55ad 100644
@@ -25,6 +25,7 @@
 #include <stdio.h>
 #include <unistd.h>
 #include <stdlib.h>
+#include <stdbool.h>
 #ifndef ANDROID_WINDOWS_HOST
 #include <sys/ioctl.h>
 #endif
@@ -110,13 +111,61 @@ static int trim_device(int i)
 	return 0;
 }
 
+static bool is_wiped_device(int i)
+{
+#ifdef WITH_ANDROID
+	struct device_info *dev = c.devices + i;
+	int fd = dev->fd;
+	char *buf, *zero_buf;
+	bool wiped = true;
+	int nblocks = 4096;	/* 16MB size */
+	int j;
+
+	buf = malloc(F2FS_BLKSIZE);
+	if (buf == NULL) {
+		MSG(1, "\tError: Malloc Failed for buf!!!\n");
+		return false;
+	}
+	zero_buf = calloc(1, F2FS_BLKSIZE);
+	if (zero_buf == NULL) {
+		MSG(1, "\tError: Calloc Failed for zero buf!!!\n");
+		free(buf);
+		return false;
+	}
+
+	if (lseek(fd, 0, SEEK_SET) < 0) {
+		free(zero_buf);
+		free(buf);
+		return false;
+	}
+
+	/* check first n blocks */
+	for (j = 0; j < nblocks; j++) {
+		if (read(fd, buf, F2FS_BLKSIZE) != F2FS_BLKSIZE ||
+				memcmp(buf, zero_buf, F2FS_BLKSIZE)) {
+			wiped = false;
+			break;
+		}
+	}
+	free(zero_buf);
+	free(buf);
+
+	if (wiped)
+		MSG(0, "Info: Found all zeros in first %d blocks\n", nblocks);
+	return wiped;
+#else
+	return false;
+#endif
+}
+
 int f2fs_trim_devices(void)
 {
 	int i;
 
Quoted 12 lines
Quoted 10 lines

mkfs/f2fs_format_utils.c static int trim_device(int i) · line 171

Quoted 10 lines
mkfs/f2fs_format_utils.c +3 -2
-	for (i = 0; i < c.ndevs; i++)
-		if (trim_device(i))
+	for (i = 0; i < c.ndevs; i++) {
+		if (!is_wiped_device(i) && trim_device(i))
 			return -1;
+	}
 	c.trimmed = 1;
 	return 0;
 }

.

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

#8 lore raw
Quoted 18 lines · Chao Yu
Quoted 9 lines · Jaegeuk Kim
Quoted 6 lines · Chao Yu
Quoted 2 lines · Jaegeuk Kim

We actually don't need to issue trim on entire disk by checking first blocks having zeros.

In heap mode, we locate node log header to tail end of device, should we consider to check block contain according to heap option?

I wanted to check F2FS metadata mainly.

Oh, I thought you mean main area. :P

Quoted 5 lines
Quoted 4 lines

BTW, if we changed cp_ver whenever mkfs, why should we still issue trim to obsolete old data in node remained in image?

Quoted 6 lines
Quoted 1 line

For simplicity. :P

I didn't get why we can assume all metadata are zeroed if first 16MB are all zero...

BTW, if first 16MB are non-zero, why not just trim F2FS metadata rather than whole area?

Trim the entire space so that we can skip discard in runtime by the flag, right?

Quoted 95 lines

Thanks,

Quoted 92 lines
Quoted 80 lines

Thanks,

Quoted 76 lines

---
v2 from v1:
- clean up

 mkfs/f2fs_format_utils.c | 53 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 51 insertions(+), 2 deletions(-)
mkfs/f2fs_format_utils.c +48 -0
index 8bf128c..f2d55ad 100644
@@ -25,6 +25,7 @@
 #include <stdio.h>
 #include <unistd.h>
 #include <stdlib.h>
+#include <stdbool.h>
 #ifndef ANDROID_WINDOWS_HOST
 #include <sys/ioctl.h>
 #endif
@@ -110,13 +111,61 @@ static int trim_device(int i)
 	return 0;
 }
 
+static bool is_wiped_device(int i)
+{
+#ifdef WITH_ANDROID
+	struct device_info *dev = c.devices + i;
+	int fd = dev->fd;
+	char *buf, *zero_buf;
+	bool wiped = true;
+	int nblocks = 4096;	/* 16MB size */
+	int j;
+
+	buf = malloc(F2FS_BLKSIZE);
+	if (buf == NULL) {
+		MSG(1, "\tError: Malloc Failed for buf!!!\n");
+		return false;
+	}
+	zero_buf = calloc(1, F2FS_BLKSIZE);
+	if (zero_buf == NULL) {
+		MSG(1, "\tError: Calloc Failed for zero buf!!!\n");
+		free(buf);
+		return false;
+	}
+
+	if (lseek(fd, 0, SEEK_SET) < 0) {
+		free(zero_buf);
+		free(buf);
+		return false;
+	}
+
+	/* check first n blocks */
+	for (j = 0; j < nblocks; j++) {
+		if (read(fd, buf, F2FS_BLKSIZE) != F2FS_BLKSIZE ||
+				memcmp(buf, zero_buf, F2FS_BLKSIZE)) {
+			wiped = false;
+			break;
+		}
+	}
+	free(zero_buf);
+	free(buf);
+
+	if (wiped)
+		MSG(0, "Info: Found all zeros in first %d blocks\n", nblocks);
+	return wiped;
+#else
+	return false;
+#endif
+}
+
 int f2fs_trim_devices(void)
 {
 	int i;
 
Quoted 10 lines

mkfs/f2fs_format_utils.c static int trim_device(int i) · line 171

Quoted 10 lines
mkfs/f2fs_format_utils.c +3 -2
-	for (i = 0; i < c.ndevs; i++)
-		if (trim_device(i))
+	for (i = 0; i < c.ndevs; i++) {
+		if (!is_wiped_device(i) && trim_device(i))
 			return -1;
+	}
 	c.trimmed = 1;
 	return 0;
 }

.

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

#9 lore raw
Quoted 19 lines · Jaegeuk Kim
Quoted 18 lines · Chao Yu
Quoted 9 lines · Jaegeuk Kim
Quoted 6 lines · Chao Yu
Quoted 2 lines · Jaegeuk Kim

We actually don't need to issue trim on entire disk by checking first blocks having zeros.

In heap mode, we locate node log header to tail end of device, should we consider to check block contain according to heap option?

I wanted to check F2FS metadata mainly.

Oh, I thought you mean main area. :P

Quoted 5 lines
Quoted 4 lines

BTW, if we changed cp_ver whenever mkfs, why should we still issue trim to obsolete old data in node remained in image?

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

For simplicity. :P

I didn't get why we can assume all metadata are zeroed if first 16MB are all zero...

BTW, if first 16MB are non-zero, why not just trim F2FS metadata rather than whole area?

Trim the entire space so that we can skip discard in runtime by the flag, right?

You're right, thanks helping recall. ;)

Thanks,

Quoted 96 lines
Quoted 96 lines

Thanks,

Quoted 92 lines
Quoted 80 lines

Thanks,

Quoted 76 lines

---
v2 from v1:
- clean up

 mkfs/f2fs_format_utils.c | 53 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 51 insertions(+), 2 deletions(-)
mkfs/f2fs_format_utils.c +48 -0
index 8bf128c..f2d55ad 100644
@@ -25,6 +25,7 @@
 #include <stdio.h>
 #include <unistd.h>
 #include <stdlib.h>
+#include <stdbool.h>
 #ifndef ANDROID_WINDOWS_HOST
 #include <sys/ioctl.h>
 #endif
@@ -110,13 +111,61 @@ static int trim_device(int i)
 	return 0;
 }
 
+static bool is_wiped_device(int i)
+{
+#ifdef WITH_ANDROID
+	struct device_info *dev = c.devices + i;
+	int fd = dev->fd;
+	char *buf, *zero_buf;
+	bool wiped = true;
+	int nblocks = 4096;	/* 16MB size */
+	int j;
+
+	buf = malloc(F2FS_BLKSIZE);
+	if (buf == NULL) {
+		MSG(1, "\tError: Malloc Failed for buf!!!\n");
+		return false;
+	}
+	zero_buf = calloc(1, F2FS_BLKSIZE);
+	if (zero_buf == NULL) {
+		MSG(1, "\tError: Calloc Failed for zero buf!!!\n");
+		free(buf);
+		return false;
+	}
+
+	if (lseek(fd, 0, SEEK_SET) < 0) {
+		free(zero_buf);
+		free(buf);
+		return false;
+	}
+
+	/* check first n blocks */
+	for (j = 0; j < nblocks; j++) {
+		if (read(fd, buf, F2FS_BLKSIZE) != F2FS_BLKSIZE ||
+				memcmp(buf, zero_buf, F2FS_BLKSIZE)) {
+			wiped = false;
+			break;
+		}
+	}
+	free(zero_buf);
+	free(buf);
+
+	if (wiped)
+		MSG(0, "Info: Found all zeros in first %d blocks\n", nblocks);
+	return wiped;
+#else
+	return false;
+#endif
+}
+
 int f2fs_trim_devices(void)
 {
 	int i;
 
Quoted 10 lines

mkfs/f2fs_format_utils.c static int trim_device(int i) · line 171

Quoted 10 lines
mkfs/f2fs_format_utils.c +3 -2
-	for (i = 0; i < c.ndevs; i++)
-		if (trim_device(i))
+	for (i = 0; i < c.ndevs; i++) {
+		if (!is_wiped_device(i) && trim_device(i))
 			return -1;
+	}
 	c.trimmed = 1;
 	return 0;
 }

.

Quoted 2 lines

.

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