Skip to contentSkip to conversation

f2fs-tools: use pread and pwrite when they are available.

PATCH Superseded LongPing Wei via Linux-f2fs-devel 1 participant 3 messages Dec 10, 2024 Library code

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 f2fs-tools: use pread and pwrite when they are available.

This patch want to reduce the number of system calls to improve performance. pread and pwrite will only be used when the target platform support them.

---
 configure.ac     |  4 +++-
 lib/libf2fs_io.c | 29 +++++++++++++++++++++++++++++
 2 files changed, 32 insertions(+), 1 deletion(-)
configure.ac +3 -1
index 2053a65..439b97f 100644
@@ -182,6 +182,7 @@ AC_TYPE_SIZE_T
 AC_FUNC_GETMNTENT
 AC_CHECK_FUNCS_ONCE([
 	add_key
+	clock_gettime
 	fallocate
 	fsetxattr
 	fstat
@@ -193,7 +194,8 @@ AC_CHECK_FUNCS_ONCE([
 	keyctl
 	memset
 	setmntent
-	clock_gettime
+	pread
+	pwrite
 ])
 
 AS_IF([test "$ac_cv_header_byteswap_h" = "yes"],
lib/libf2fs_io.c +29 -0
index 520ae03..a8430c9 100644
@@ -279,6 +279,12 @@ static int dcache_io_read(long entry, __u64 offset, off_t blk)
 	if (fd < 0)
 		return fd;
 
+#ifdef HAVE_PREAD
+	if (pread(fd, dcache_buf + entry * F2FS_BLKSIZE, F2FS_BLKSIZE, offset) < 0) {
+		MSG(0, "\n pread() fail.\n");
+		return -1;
+	}
+#else
 	if (lseek(fd, offset, SEEK_SET) < 0) {
 		MSG(0, "\n lseek fail.\n");
 		return -1;
@@ -287,6 +293,7 @@ static int dcache_io_read(long entry, __u64 offset, off_t blk)
 		MSG(0, "\n read() fail.\n");
 		return -1;
 	}
+#endif
 	dcache_lastused[entry] = ++dcache_usetick;
 	dcache_valid[entry] = true;
 	dcache_blk[entry] = blk;
@@ -393,10 +400,15 @@ int dev_read_version(void *buf, __u64 offset, size_t len)
 {
 	if (c.sparse_mode)
 		return 0;
+#ifdef HAVE_RPEAD
+	if (pread(c.kd, buf, len, (off_t)offset) < 0)
+		return -1;
+#else
 	if (lseek(c.kd, (off_t)offset, SEEK_SET) < 0)
 		return -1;
 	if (read(c.kd, buf, len) < 0)
 		return -1;
+#endif
 	return 0;
 }
 
@@ -535,10 +547,15 @@ int dev_read(void *buf, __u64 offset, size_t len)
 	fd = __get_device_fd(&offset);
 	if (fd < 0)
 		return fd;
+#ifdef HAVE_PREAD
+	if (pread(fd, buf, len, (off_t)offset) < 0)
+		return -1;
+#else
 	if (lseek(fd, (off_t)offset, SEEK_SET) < 0)
 		return -1;
 	if (read(fd, buf, len) < 0)
 		return -1;
+#endif
 	return 0;
 }
 
@@ -615,8 +632,10 @@ static int __dev_write(void *buf, __u64 offset, size_t len, enum rw_hint whint)
 	if (fd < 0)
 		return fd;
 
+#ifndef HAVE_PWRITE
 	if (lseek(fd, (off_t)offset, SEEK_SET) < 0)
 		return -1;
+#endif
 
 #if ! defined(__MINGW32__)
 	if (c.need_whint && (c.whint != whint)) {
@@ -629,8 +648,13 @@ static int __dev_write(void *buf, __u64 offset, size_t len, enum rw_hint whint)
 	}
 #endif
 
+#ifdef HAVE_PWRITE
+	if (pwrite(fd, buf, len, (off_t)offset) < 0)
+		return -1;
+#else
 	if (write(fd, buf, len) < 0)
 		return -1;
+#endif
 
 	c.need_fsync = true;
 
@@ -663,10 +687,15 @@ int dev_write_block(void *buf, __u64 blk_addr, enum rw_hint whint)
 
 int dev_write_dump(void *buf, __u64 offset, size_t len)
 {
+#ifdef HAVE_PWRITE
+	if (pwrite(c.dump_fd, buf, len, (off_t)offset) < 0)
+		return -1;
+#else
 	if (lseek(c.dump_fd, (off_t)offset, SEEK_SET) < 0)
 		return -1;
 	if (write(c.dump_fd, buf, len) < 0)
 		return -1;
+#endif
 	return 0;
 }
 
Signature · 8 lines
2.34.1



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

PATCH 2/2 f2fs-tools: add HAVE_PREAD and HAVE_PWRITE

This patch enable pread and pwrite on Android platform.

---
 include/android_config.h | 2 ++
 1 file changed, 2 insertions(+)
include/android_config.h +2 -0
index f5cd4de..b11e2e4 100644
@@ -36,6 +36,8 @@
 #define HAVE_FSTAT64 1
 #define HAVE_GETMNTENT 1
 #define HAVE_MEMSET 1
+#define HAVE_PREAD 1
+#define HAVE_PWRITE 1
 #define HAVE_SELINUX_ANDROID_H 1
 #define HAVE_SETMNTENT 1
 #define HAVE_SPARSE_SPARSE_H 1
Signature · 8 lines
2.34.1



_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
#2 lore raw
In reply to LongPing Wei via Linux-f2fs-devel
Quoted 89 lines · LongPing Wei

This patch want to reduce the number of system calls to improve performance. pread and pwrite will only be used when the target platform support them.

---
  configure.ac     |  4 +++-
  lib/libf2fs_io.c | 29 +++++++++++++++++++++++++++++
  2 files changed, 32 insertions(+), 1 deletion(-)
configure.ac +3 -1
index 2053a65..439b97f 100644
@@ -182,6 +182,7 @@ AC_TYPE_SIZE_T
  AC_FUNC_GETMNTENT
  AC_CHECK_FUNCS_ONCE([
  	add_key
+	clock_gettime
  	fallocate
  	fsetxattr
  	fstat
@@ -193,7 +194,8 @@ AC_CHECK_FUNCS_ONCE([
  	keyctl
  	memset
  	setmntent
-	clock_gettime
+	pread
+	pwrite
  ])
 
  AS_IF([test "$ac_cv_header_byteswap_h" = "yes"],
lib/libf2fs_io.c +17 -0
index 520ae03..a8430c9 100644
@@ -279,6 +279,12 @@ static int dcache_io_read(long entry, __u64 offset, off_t blk)
  	if (fd < 0)
  		return fd;
 
+#ifdef HAVE_PREAD
+	if (pread(fd, dcache_buf + entry * F2FS_BLKSIZE, F2FS_BLKSIZE, offset) < 0) {
+		MSG(0, "\n pread() fail.\n");
+		return -1;
+	}
+#else
  	if (lseek(fd, offset, SEEK_SET) < 0) {
  		MSG(0, "\n lseek fail.\n");
  		return -1;
@@ -287,6 +293,7 @@ static int dcache_io_read(long entry, __u64 offset, off_t blk)
  		MSG(0, "\n read() fail.\n");
  		return -1;
  	}
+#endif
  	dcache_lastused[entry] = ++dcache_usetick;
  	dcache_valid[entry] = true;
  	dcache_blk[entry] = blk;
@@ -393,10 +400,15 @@ int dev_read_version(void *buf, __u64 offset, size_t len)
  {
  	if (c.sparse_mode)
  		return 0;
+#ifdef HAVE_RPEAD
+	if (pread(c.kd, buf, len, (off_t)offset) < 0)
+		return -1;
+#else
  	if (lseek(c.kd, (off_t)offset, SEEK_SET) < 0)
  		return -1;
  	if (read(c.kd, buf, len) < 0)
  		return -1;
+#endif
  	return 0;
  }
 
@@ -535,10 +547,15 @@ int dev_read(void *buf, __u64 offset, size_t len)
  	fd = __get_device_fd(&offset);
  	if (fd < 0)
  		return fd;
+#ifdef HAVE_PREAD
+	if (pread(fd, buf, len, (off_t)offset) < 0)
+		return -1;
+#else
  	if (lseek(fd, (off_t)offset, SEEK_SET) < 0)
  		return -1;
  	if (read(fd, buf, len) < 0)
  		return -1;
+#endif
  	return 0;
  }
 

lib/libf2fs_io.c static int __dev_write(void *buf, __u64 offset, size_t len, enum rw_hint whint) · line 638

Quoted 7 lines
lib/libf2fs_io.c +2 -0
@@ -615,8 +632,10 @@ static int __dev_write(void *buf, __u64 offset, size_t len, enum rw_hint whint)
  	if (fd < 0)
  		return fd;
 
+#ifndef HAVE_PWRITE
  	if (lseek(fd, (off_t)offset, SEEK_SET) < 0)
  		return -1;
+#endif

Hi, LongPing,

Can we move lseek and the following write together after writehint operation? I think there is no need to place writehint between lseek and write, because writehint affects the inode and does not affects the pos in struct file.

However, I am wondering if writehint here works as expected when fd is not opened with O_SYNC or O_DSYN, especially if multiple writes occur in a short period of time. Please correct me if I miss something.

thanks,
shengyong

Quoted 23 lines
lib/libf2fs_io.c +7 -0
  #if ! defined(__MINGW32__)
  	if (c.need_whint && (c.whint != whint)) {
@@ -629,8 +648,13 @@ static int __dev_write(void *buf, __u64 offset, size_t len, enum rw_hint whint)
  	}
  #endif
 
+#ifdef HAVE_PWRITE
+	if (pwrite(fd, buf, len, (off_t)offset) < 0)
+		return -1;
+#else
  	if (write(fd, buf, len) < 0)
  		return -1;
+#endif
 
  	c.need_fsync = true;
 
@@ -663,10 +687,15 @@ int dev_write_block(void *buf, __u64 blk_addr, enum rw_hint whint)
 
  int dev_write_dump(void *buf, __u64 offset, size_t len)
  {
+#ifdef HAVE_PWRITE
+	if (pwrite(c.dump_fd, buf, len, (off_t)offset) < 0)

lib/libf2fs_io.c int dev_write_block(void *buf, __u64 blk_addr, enum rw_hint whint) · line 701

Quoted 10 lines
lib/libf2fs_io.c +3 -0
+		return -1;
+#else
  	if (lseek(c.dump_fd, (off_t)offset, SEEK_SET) < 0)
  		return -1;
  	if (write(c.dump_fd, buf, len) < 0)
  		return -1;
+#endif
  	return 0;
  }
 

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