Skip to contentSkip to conversation

erofs-utils: lib: fix erofs_io_sendfile()

PATCH New Gao Xiang 1 participant 2 messages Oct 10, 2025 Library code

lore
Share & tools
lore

Gao Xiang

author
#0 lore raw

PATCH 1/2 erofs-utils: lib: fix erofs_io_sendfile()

The fallback logic was incomplete and has been fixed.

---
 include/erofs/io.h |  1 +
 lib/io.c           | 29 ++++++++++++++++++++++-------
 2 files changed, 23 insertions(+), 7 deletions(-)
include/erofs/io.h +1 -0
index a9f6d2e..4bc216a 100644
@@ -35,6 +35,7 @@ struct erofs_vfops {
 	int (*fallocate)(struct erofs_vfile *vf, u64 offset, size_t len, bool pad);
 	int (*ftruncate)(struct erofs_vfile *vf, u64 length);
 	ssize_t (*read)(struct erofs_vfile *vf, void *buf, size_t len);
+	ssize_t (*write)(struct erofs_vfile *vf, void *buf, size_t len);
 	off_t (*lseek)(struct erofs_vfile *vf, u64 offset, int whence);
 	int (*fstat)(struct erofs_vfile *vf, struct stat *buf);
 	ssize_t (*sendfile)(struct erofs_vfile *vout, struct erofs_vfile *vin,
lib/io.c +22 -7
index 664dd8d..d4cfbef 100644
@@ -558,6 +558,13 @@ ssize_t erofs_io_read(struct erofs_vfile *vf, void *buf, size_t bytes)
         return i;
 }
 
+ssize_t erofs_io_write(struct erofs_vfile *vf, void *buf, size_t len)
+{
+	if (vf->ops)
+		return vf->ops->write(vf, buf, len);
+	return __erofs_io_write(vf->fd, buf, len);
+}
+
 #ifdef HAVE_SYS_SENDFILE_H
 #include <sys/sendfile.h>
 #endif
@@ -573,7 +580,7 @@ off_t erofs_io_lseek(struct erofs_vfile *vf, u64 offset, int whence)
 ssize_t erofs_io_sendfile(struct erofs_vfile *vout, struct erofs_vfile *vin,
 			  off_t *pos, size_t count)
 {
-	ssize_t written;
+	ssize_t read, written;
 
 	if (vin->ops || vout->ops) {
 		if (vin->ops)
@@ -597,16 +604,24 @@ ssize_t erofs_io_sendfile(struct erofs_vfile *vout, struct erofs_vfile *vin,
 	while (count) {
 		char buf[EROFS_MAX_BLOCK_SIZE];
 
-		written = min_t(u64, count, sizeof(buf));
+		read = min_t(u64, count, sizeof(buf));
 		if (pos)
-			written = erofs_io_pread(vin, buf, written, *pos);
+			read = erofs_io_pread(vin, buf, read, *pos);
 		else
-			written = erofs_io_read(vin, buf, written);
-		if (written <= 0)
+			read = erofs_io_read(vin, buf, read);
+		if (read <= 0) {
+			written = read;
 			break;
-		count -= written;
+		}
+		count -= read;
 		if (pos)
-			*pos += written;
+			*pos += read;
+		do {
+			written = erofs_io_write(vout, buf, read);
+			if (written < 0)
+				break;
+			read -= written;
+		} while (read);
 	}
 	return written < 0 ? written : count;
 }
Signature · 1 line
2.43.5
author
#1 lore raw

PATCH 2/2 erofs-utils: get rid of `{erofsmount_tarindex,ocierofs_io}_sendfile()`

Use the default fallback approach directly.

---
 lib/io.c          |  9 +++++----
 lib/remotes/oci.c | 43 -------------------------------------------
 mount/main.c      | 37 -------------------------------------
 3 files changed, 5 insertions(+), 84 deletions(-)
lib/io.c +5 -4
index d4cfbef..2aa81de 100644
@@ -583,12 +583,13 @@ ssize_t erofs_io_sendfile(struct erofs_vfile *vout, struct erofs_vfile *vin,
 	ssize_t read, written;
 
 	if (vin->ops || vout->ops) {
-		if (vin->ops)
+		if (vin->ops && vin->ops->sendfile)
 			return vin->ops->sendfile(vout, vin, pos, count);
-		return vout->ops->sendfile(vout, vin, pos, count);
+		if (vout->ops && vout->ops->sendfile)
+			return vout->ops->sendfile(vout, vin, pos, count);
 	}
 #if defined(HAVE_SYS_SENDFILE_H) && defined(HAVE_SENDFILE)
-	do {
+	else do {
 		written = sendfile(vout->fd, vin->fd, pos, count);
 		if (written <= 0) {
 			if (written < 0) {
@@ -602,7 +603,7 @@ ssize_t erofs_io_sendfile(struct erofs_vfile *vout, struct erofs_vfile *vin,
 	} while (written);
 #endif
 	while (count) {
-		char buf[EROFS_MAX_BLOCK_SIZE];
+		char buf[max(EROFS_MAX_BLOCK_SIZE, 32768)];
 
 		read = min_t(u64, count, sizeof(buf));
 		if (pos)
lib/remotes/oci.c +0 -43

55 lines not on this page · raw message

mount/main.c +0 -37
index fa4c322..d98e1e9 100644
@@ -409,45 +409,8 @@ static void erofsmount_tarindex_close(struct erofs_vfile *vf)
 	free(tp);
 }
 
-static ssize_t erofsmount_tarindex_sendfile(struct erofs_vfile *vout,
-					    struct erofs_vfile *vin,
-					    off_t *pos, size_t count)
-{
-	static char buf[32768];
-	ssize_t total_written = 0, ret = 0, written;
-	size_t to_read;
-	u64 read_offset;
-
-	while (count > 0) {
-		to_read = min_t(size_t, count, sizeof(buf));
-		read_offset = pos ? *pos : 0;
-
-		ret = erofsmount_tarindex_pread(vin, buf, to_read, read_offset);
-		if (ret <= 0) {
-			if (ret < 0 && total_written == 0)
-				return ret;
-			break;
-		}
-
-		written = __erofs_io_write(vout->fd, buf, ret);
-		if (written < 0) {
-			ret = -errno;
-			break;
-		}
-		if (written != ret)
-			ret = written;
-
-		total_written += ret;
-		count -= ret;
-		if (pos)
-			*pos += ret;
-	}
-	return count;
-}
-
 static struct erofs_vfops tarindex_vfile_ops = {
 	.pread = erofsmount_tarindex_pread,
-	.sendfile = erofsmount_tarindex_sendfile,
 	.close = erofsmount_tarindex_close,
 };
 
Signature · 1 line
2.43.5