Skip to contentSkip to conversation

erofs-utils: fix the erofs_io_pread and erofs_io_pwrite

PATCH Superseded Hongzhen Luo 2 participants 2 messages Jun 18, 2024 Library code

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 erofs-utils: fix the erofs_io_pread and erofs_io_pwrite

When vf->ops is not null, vf->ops->pread returns the number of bytes successfully read, which is inconsistent with the semantics of erofs_io_pread. Similar situation occurs in erofs_io_pwrite. This fixes this issue.

This also fixes erofs_dev_close.

---
v1: https://lore.kernel.org/all/[email protected]/

---
 include/erofs/internal.h |  5 ++++-
 lib/io.c                 | 31 +++++++++++++++++++++++--------
 2 files changed, 27 insertions(+), 9 deletions(-)
include/erofs/internal.h +4 -1
index f8a01ce..ed1fc92 100644
@@ -460,7 +460,10 @@ ssize_t erofs_dev_read(struct erofs_sb_info *sbi, int device_id,
 static inline int erofs_dev_write(struct erofs_sb_info *sbi, const void *buf,
 				  u64 offset, size_t len)
 {
-	return erofs_io_pwrite(&sbi->bdev, buf, offset, len);
+	ssize_t ret;
+
+	ret = erofs_io_pwrite(&sbi->bdev, buf, offset, len);
+	return (size_t)ret == len ? 0 : (int)ret;
 }
 
 static inline int erofs_dev_fillzero(struct erofs_sb_info *sbi, u64 offset,
lib/io.c +23 -8
index c523f00..0149d3b 100644
@@ -30,6 +30,7 @@ ssize_t erofs_io_pwrite(struct erofs_vfile *vf, const void *buf,
 			u64 pos, size_t len)
 {
 	ssize_t ret;
+	size_t saved_len = len;
 
 	if (unlikely(cfg.c_dry_run))
 		return 0;
@@ -53,7 +54,7 @@ ssize_t erofs_io_pwrite(struct erofs_vfile *vf, const void *buf,
 		pos += ret;
 	} while (len);
 
-	return 0;
+	return saved_len;
 }
 
 int erofs_io_fsync(struct erofs_vfile *vf)
@@ -79,6 +80,7 @@ ssize_t erofs_io_fallocate(struct erofs_vfile *vf, u64 offset,
 {
 	static const char zero[EROFS_MAX_BLOCK_SIZE] = {0};
 	ssize_t ret;
+	ssize_t wret;
 
 	if (unlikely(cfg.c_dry_run))
 		return 0;
@@ -92,13 +94,16 @@ ssize_t erofs_io_fallocate(struct erofs_vfile *vf, u64 offset,
 		return 0;
 #endif
 	while (len > EROFS_MAX_BLOCK_SIZE) {
-		ret = erofs_io_pwrite(vf, zero, offset, EROFS_MAX_BLOCK_SIZE);
+		wret = erofs_io_pwrite(vf, zero, offset, EROFS_MAX_BLOCK_SIZE);
+		ret = wret == EROFS_MAX_BLOCK_SIZE ? 0 : wret;
 		if (ret)
 			return ret;
 		len -= EROFS_MAX_BLOCK_SIZE;
 		offset += EROFS_MAX_BLOCK_SIZE;
 	}
-	return erofs_io_pwrite(vf, zero, offset, len);
+	wret = erofs_io_pwrite(vf, zero, offset, len);
+	ret = wret == len ? 0 : wret;
+	return ret;
 }
 
 int erofs_io_ftruncate(struct erofs_vfile *vf, u64 length)
@@ -126,6 +131,7 @@ int erofs_io_ftruncate(struct erofs_vfile *vf, u64 length)
 ssize_t erofs_io_pread(struct erofs_vfile *vf, void *buf, u64 pos, size_t len)
 {
 	ssize_t ret;
+	size_t saved_len = len;
 
 	if (unlikely(cfg.c_dry_run))
 		return 0;
@@ -156,7 +162,8 @@ ssize_t erofs_io_pread(struct erofs_vfile *vf, void *buf, u64 pos, size_t len)
 		len -= ret;
 		buf += ret;
 	} while (len);
-	return 0;
+
+	return saved_len;
 }
 
 static int erofs_get_bdev_size(int fd, u64 *bytes)
@@ -287,7 +294,8 @@ out:
 
 void erofs_dev_close(struct erofs_sb_info *sbi)
 {
-	close(sbi->bdev.fd);
+	if (!sbi->bdev.ops)
+		close(sbi->bdev.fd);
 	free(sbi->devname);
 	sbi->devname = NULL;
 	sbi->bdev.fd = -1;
@@ -320,11 +328,18 @@ int erofs_blob_open_ro(struct erofs_sb_info *sbi, const char *dev)
 ssize_t erofs_dev_read(struct erofs_sb_info *sbi, int device_id,
 		       void *buf, u64 offset, size_t len)
 {
-	if (device_id)
-		return erofs_io_pread(&((struct erofs_vfile) {
+	ssize_t ret;
+
+	if (device_id) {
+		ret = erofs_io_pread(&((struct erofs_vfile) {
 				.fd = sbi->blobfd[device_id - 1],
 			}), buf, offset, len);
-	return erofs_io_pread(&sbi->bdev, buf, offset, len);
+		return ret == len ? 0 : ret;
+
+	}
+
+	ret =  erofs_io_pread(&sbi->bdev, buf, offset, len);
+	return ret == len ? 0 : ret;
 }
 
 static ssize_t __erofs_copy_file_range(int fd_in, u64 *off_in,
Signature · 1 line
2.39.3
#1 lore raw
Quoted 53 lines · Hongzhen Luo

When vf->ops is not null, vf->ops->pread returns the number of bytes successfully read, which is inconsistent with the semantics of erofs_io_pread. Similar situation occurs in erofs_io_pwrite. This fixes this issue.

This also fixes erofs_dev_close.

---
v1: https://lore.kernel.org/all/[email protected]/

---
  include/erofs/internal.h |  5 ++++-
  lib/io.c                 | 31 +++++++++++++++++++++++--------
  2 files changed, 27 insertions(+), 9 deletions(-)
include/erofs/internal.h +4 -1
index f8a01ce..ed1fc92 100644
@@ -460,7 +460,10 @@ ssize_t erofs_dev_read(struct erofs_sb_info *sbi, int device_id,
  static inline int erofs_dev_write(struct erofs_sb_info *sbi, const void *buf,
  				  u64 offset, size_t len)
  {
-	return erofs_io_pwrite(&sbi->bdev, buf, offset, len);
+	ssize_t ret;
+
+	ret = erofs_io_pwrite(&sbi->bdev, buf, offset, len);
+	return (size_t)ret == len ? 0 : (int)ret;
  }
 
  static inline int erofs_dev_fillzero(struct erofs_sb_info *sbi, u64 offset,
lib/io.c +2 -1
index c523f00..0149d3b 100644
@@ -30,6 +30,7 @@ ssize_t erofs_io_pwrite(struct erofs_vfile *vf, const void *buf,
  			u64 pos, size_t len)
  {
  	ssize_t ret;
+	size_t saved_len = len;
 
  	if (unlikely(cfg.c_dry_run))
  		return 0;
@@ -53,7 +54,7 @@ ssize_t erofs_io_pwrite(struct erofs_vfile *vf, const void *buf,
  		pos += ret;
  	} while (len);
 
-	return 0;
+	return saved_len;
  }
 
  int erofs_io_fsync(struct erofs_vfile *vf)

lib/io.c ssize_t erofs_io_fallocate(struct erofs_vfile *vf, u64 offset, · line 83

Quoted 4 lines
lib/io.c +1 -0
@@ -79,6 +80,7 @@ ssize_t erofs_io_fallocate(struct erofs_vfile *vf, u64 offset,
  {
  	static const char zero[EROFS_MAX_BLOCK_SIZE] = {0};
  	ssize_t ret;
+	ssize_t wret;

why not directly using ret here?

Quoted 27 lines
lib/io.c +5 -2
  	if (unlikely(cfg.c_dry_run))
  		return 0;
@@ -92,13 +94,16 @@ ssize_t erofs_io_fallocate(struct erofs_vfile *vf, u64 offset,
  		return 0;
  #endif
  	while (len > EROFS_MAX_BLOCK_SIZE) {
-		ret = erofs_io_pwrite(vf, zero, offset, EROFS_MAX_BLOCK_SIZE);
+		wret = erofs_io_pwrite(vf, zero, offset, EROFS_MAX_BLOCK_SIZE);
+		ret = wret == EROFS_MAX_BLOCK_SIZE ? 0 : wret;
  		if (ret)
  			return ret;
  		len -= EROFS_MAX_BLOCK_SIZE;
  		offset += EROFS_MAX_BLOCK_SIZE;
  	}
-	return erofs_io_pwrite(vf, zero, offset, len);
+	wret = erofs_io_pwrite(vf, zero, offset, len);
+	ret = wret == len ? 0 : wret;
 
 
 
 
+	return ret;
  }
 
  int erofs_io_ftruncate(struct erofs_vfile *vf, u64 length)

lib/io.c int erofs_io_ftruncate(struct erofs_vfile *vf, u64 length) · line 134

Quoted 4 lines
lib/io.c +1 -0
@@ -126,6 +131,7 @@ int erofs_io_ftruncate(struct erofs_vfile *vf, u64 length)
  ssize_t erofs_io_pread(struct erofs_vfile *vf, void *buf, u64 pos, size_t len)
  {
  	ssize_t ret;
+	size_t saved_len = len;

I really think you need to record the data read and

You don't address:

                        if (!ret) {
                                erofs_info("reach EOF of device");
                                memset(buf, 0, len);
                                return 0;
                        }
Quoted 27 lines
lib/io.c +4 -2
  	if (unlikely(cfg.c_dry_run))
  		return 0;
@@ -156,7 +162,8 @@ ssize_t erofs_io_pread(struct erofs_vfile *vf, void *buf, u64 pos, size_t len)
  		len -= ret;
  		buf += ret;
  	} while (len);
-	return 0;
+
+	return saved_len;
  }
 
  static int erofs_get_bdev_size(int fd, u64 *bytes)
@@ -287,7 +294,8 @@ out:
 
  void erofs_dev_close(struct erofs_sb_info *sbi)
  {
-	close(sbi->bdev.fd);
+	if (!sbi->bdev.ops)
+		close(sbi->bdev.fd);
  	free(sbi->devname);
  	sbi->devname = NULL;
  	sbi->bdev.fd = -1;
@@ -320,11 +328,18 @@ int erofs_blob_open_ro(struct erofs_sb_info *sbi, const char *dev)
  ssize_t erofs_dev_read(struct erofs_sb_info *sbi, int device_id,
  		       void *buf, u64 offset, size_t len)
  {

lib/io.c int erofs_blob_open_ro(struct erofs_sb_info *sbi, const char *dev) · line 337

Quoted 10 lines
lib/io.c +5 -3
-	if (device_id)
-		return erofs_io_pread(&((struct erofs_vfile) {
+	ssize_t ret;
+
+	if (device_id) {
+		ret = erofs_io_pread(&((struct erofs_vfile) {
  				.fd = sbi->blobfd[device_id - 1],
  			}), buf, offset, len);
-	return erofs_io_pread(&sbi->bdev, buf, offset, len);
+		return ret == len ? 0 : ret;

What's the meaning if ret > 0?

lib/io.c int erofs_blob_open_ro(struct erofs_sb_info *sbi, const char *dev) · line 342

Quoted 5 lines
lib/io.c +5 -0
+
+	}
+
+	ret =  erofs_io_pread(&sbi->bdev, buf, offset, len);
+	return ret == len ? 0 : ret;

Same here.

Thanks,
Gao Xiang