diff mbox series

[f2fs-dev] libf2fs_io: change lseek & read/write to pread/pwrite

Message ID 20241205080109.43552-1-panglu2022@gmail.com (mailing list archive)
State New
Headers show
Series [f2fs-dev] libf2fs_io: change lseek & read/write to pread/pwrite | expand

Commit Message

Lu Pang Dec. 5, 2024, 8:01 a.m. UTC
Most of read() and write() are performed with lseek(),
combining two operations into one can slightly improve the performance.

Signed-off-by: Lu Pang <panglu2022@gmail.com>
---
 lib/libf2fs_io.c | 22 +++++-----------------
 1 file changed, 5 insertions(+), 17 deletions(-)

Comments

Sheng Yong Dec. 5, 2024, noon UTC | #1
On 2024/12/5 16:01, Lu Pang wrote:
> Most of read() and write() are performed with lseek(),
> combining two operations into one can slightly improve the performance.
Hi, Lu Pang,

It seems that both pread and pwrite are not available on windows. However,
(I think) windows targets are needed for Windows Android SDK.

thanks,
shengyong
> 
> Signed-off-by: Lu Pang <panglu2022@gmail.com>
> ---
>   lib/libf2fs_io.c | 22 +++++-----------------
>   1 file changed, 5 insertions(+), 17 deletions(-)
> 
> diff --git a/lib/libf2fs_io.c b/lib/libf2fs_io.c
> index 520ae03..6388ace 100644
> --- a/lib/libf2fs_io.c
> +++ b/lib/libf2fs_io.c
> @@ -279,11 +279,7 @@ static int dcache_io_read(long entry, __u64 offset, off_t blk)
>   	if (fd < 0)
>   		return fd;
>   
> -	if (lseek(fd, offset, SEEK_SET) < 0) {
> -		MSG(0, "\n lseek fail.\n");
> -		return -1;
> -	}
> -	if (read(fd, dcache_buf + entry * F2FS_BLKSIZE, F2FS_BLKSIZE) < 0) {
> +	if (pread(fd, dcache_buf + entry * F2FS_BLKSIZE, F2FS_BLKSIZE, (off_t)offset) < 0) {
>   		MSG(0, "\n read() fail.\n");
>   		return -1;
>   	}
> @@ -393,9 +389,7 @@ int dev_read_version(void *buf, __u64 offset, size_t len)
>   {
>   	if (c.sparse_mode)
>   		return 0;
> -	if (lseek(c.kd, (off_t)offset, SEEK_SET) < 0)
> -		return -1;
> -	if (read(c.kd, buf, len) < 0)
> +	if (pread(c.kd, buf, len, (off_t)offset) < 0)
>   		return -1;
>   	return 0;
>   }
> @@ -535,9 +529,7 @@ int dev_read(void *buf, __u64 offset, size_t len)
>   	fd = __get_device_fd(&offset);
>   	if (fd < 0)
>   		return fd;
> -	if (lseek(fd, (off_t)offset, SEEK_SET) < 0)
> -		return -1;
> -	if (read(fd, buf, len) < 0)
> +	if (pread(fd, buf, len, (off_t)offset) < 0)
>   		return -1;
>   	return 0;
>   }
> @@ -615,8 +607,6 @@ static int __dev_write(void *buf, __u64 offset, size_t len, enum rw_hint whint)
>   	if (fd < 0)
>   		return fd;
>   
> -	if (lseek(fd, (off_t)offset, SEEK_SET) < 0)
> -		return -1;
>   
>   #if ! defined(__MINGW32__)
>   	if (c.need_whint && (c.whint != whint)) {
> @@ -629,7 +619,7 @@ static int __dev_write(void *buf, __u64 offset, size_t len, enum rw_hint whint)
>   	}
>   #endif
>   
> -	if (write(fd, buf, len) < 0)
> +	if (pwrite(fd, buf, len, (off_t)offset) < 0)
>   		return -1;
>   
>   	c.need_fsync = true;
> @@ -663,9 +653,7 @@ int dev_write_block(void *buf, __u64 blk_addr, enum rw_hint whint)
>   
>   int dev_write_dump(void *buf, __u64 offset, size_t len)
>   {
> -	if (lseek(c.dump_fd, (off_t)offset, SEEK_SET) < 0)
> -		return -1;
> -	if (write(c.dump_fd, buf, len) < 0)
> +	if (pwrite(c.dump_fd, buf, len, (off_t)offset) < 0)
>   		return -1;
>   	return 0;
>   }
Lu Pang Dec. 6, 2024, 2:15 a.m. UTC | #2
On Thu, Dec 5, 2024 at 8:00 PM Sheng Yong <shengyong@oppo.com> wrote:
> It seems that both pread and pwrite are not available on windows. However,
> (I think) windows targets are needed for Windows Android SDK.
Hi, Sheng Yong,

You are right. I forgot to consider Windows.
Is there any equivalent form of pread and pwrite on Windows?

Thanks,
Lu Pang
Sheng Yong Dec. 9, 2024, 2:13 a.m. UTC | #3
On 2024/12/6 10:15, Lu Pang wrote:
> On Thu, Dec 5, 2024 at 8:00 PM Sheng Yong <shengyong@oppo.com> wrote:
>> It seems that both pread and pwrite are not available on windows. However,
>> (I think) windows targets are needed for Windows Android SDK.
> Hi, Sheng Yong,
> 
> You are right. I forgot to consider Windows.
> Is there any equivalent form of pread and pwrite on Windows?

Sorry, I don't have any suggestions on that :-(

thanks,
shengyong
> 
> Thanks,
> Lu Pang
diff mbox series

Patch

diff --git a/lib/libf2fs_io.c b/lib/libf2fs_io.c
index 520ae03..6388ace 100644
--- a/lib/libf2fs_io.c
+++ b/lib/libf2fs_io.c
@@ -279,11 +279,7 @@  static int dcache_io_read(long entry, __u64 offset, off_t blk)
 	if (fd < 0)
 		return fd;
 
-	if (lseek(fd, offset, SEEK_SET) < 0) {
-		MSG(0, "\n lseek fail.\n");
-		return -1;
-	}
-	if (read(fd, dcache_buf + entry * F2FS_BLKSIZE, F2FS_BLKSIZE) < 0) {
+	if (pread(fd, dcache_buf + entry * F2FS_BLKSIZE, F2FS_BLKSIZE, (off_t)offset) < 0) {
 		MSG(0, "\n read() fail.\n");
 		return -1;
 	}
@@ -393,9 +389,7 @@  int dev_read_version(void *buf, __u64 offset, size_t len)
 {
 	if (c.sparse_mode)
 		return 0;
-	if (lseek(c.kd, (off_t)offset, SEEK_SET) < 0)
-		return -1;
-	if (read(c.kd, buf, len) < 0)
+	if (pread(c.kd, buf, len, (off_t)offset) < 0)
 		return -1;
 	return 0;
 }
@@ -535,9 +529,7 @@  int dev_read(void *buf, __u64 offset, size_t len)
 	fd = __get_device_fd(&offset);
 	if (fd < 0)
 		return fd;
-	if (lseek(fd, (off_t)offset, SEEK_SET) < 0)
-		return -1;
-	if (read(fd, buf, len) < 0)
+	if (pread(fd, buf, len, (off_t)offset) < 0)
 		return -1;
 	return 0;
 }
@@ -615,8 +607,6 @@  static int __dev_write(void *buf, __u64 offset, size_t len, enum rw_hint whint)
 	if (fd < 0)
 		return fd;
 
-	if (lseek(fd, (off_t)offset, SEEK_SET) < 0)
-		return -1;
 
 #if ! defined(__MINGW32__)
 	if (c.need_whint && (c.whint != whint)) {
@@ -629,7 +619,7 @@  static int __dev_write(void *buf, __u64 offset, size_t len, enum rw_hint whint)
 	}
 #endif
 
-	if (write(fd, buf, len) < 0)
+	if (pwrite(fd, buf, len, (off_t)offset) < 0)
 		return -1;
 
 	c.need_fsync = true;
@@ -663,9 +653,7 @@  int dev_write_block(void *buf, __u64 blk_addr, enum rw_hint whint)
 
 int dev_write_dump(void *buf, __u64 offset, size_t len)
 {
-	if (lseek(c.dump_fd, (off_t)offset, SEEK_SET) < 0)
-		return -1;
-	if (write(c.dump_fd, buf, len) < 0)
+	if (pwrite(c.dump_fd, buf, len, (off_t)offset) < 0)
 		return -1;
 	return 0;
 }