The number of bytes written should be returned.

Fixes: 29466e7f1cbf ("erofs-utils: lib: fix erofs_io_sendfile()")
Signed-off-by: Gao Xiang <[email protected]>
---
 lib/io.c     | 13 +++++++------
 mount/main.c | 12 ++++++------
 2 files changed, 13 insertions(+), 12 deletions(-)

diff --git a/lib/io.c b/lib/io.c
index 440f69b..90d2e54 100644
--- a/lib/io.c
+++ b/lib/io.c
@@ -581,6 +581,7 @@ ssize_t erofs_io_sendfile(struct erofs_vfile *vout, struct 
erofs_vfile *vin,
                          off_t *pos, size_t count)
 {
        ssize_t read, written;
+       size_t rem = count;
 
        if (vin->ops || vout->ops) {
                if (vin->ops && vin->ops->sendfile)
@@ -600,13 +601,13 @@ ssize_t erofs_io_sendfile(struct erofs_vfile *vout, 
struct erofs_vfile *vin,
                        }
                        break;
                }
-               count -= written;
+               rem -= written;
        } while (written);
 #endif
-       while (count) {
+       while (rem) {
                char buf[max(EROFS_MAX_BLOCK_SIZE, 32768)];
 
-               read = min_t(u64, count, sizeof(buf));
+               read = min_t(u64, rem, sizeof(buf));
                if (pos)
                        read = erofs_io_pread(vin, buf, read, *pos);
                else
@@ -615,17 +616,17 @@ ssize_t erofs_io_sendfile(struct erofs_vfile *vout, 
struct erofs_vfile *vin,
                        written = read;
                        break;
                }
-               count -= read;
+               rem -= read;
                if (pos)
                        *pos += read;
                do {
                        written = erofs_io_write(vout, buf, read);
                        if (written < 0)
-                               break;
+                               return written;
                        read -= written;
                } while (read);
        }
-       return written < 0 ? written : count;
+       return written < 0 ? written : count - rem;
 }
 
 int erofs_io_xcopy(struct erofs_vfile *vout, off_t pos,
diff --git a/mount/main.c b/mount/main.c
index 53bd2b2..e25134c 100644
--- a/mount/main.c
+++ b/mount/main.c
@@ -577,7 +577,7 @@ static void *erofsmount_nbd_loopfn(void *arg)
 
        while (1) {
                struct erofs_nbd_request rq;
-               ssize_t rem;
+               ssize_t written;
                off_t pos;
 
                err = erofs_nbd_get_request(ctx->sk.fd, &rq);
@@ -597,13 +597,13 @@ static void *erofsmount_nbd_loopfn(void *arg)
                erofs_nbd_send_reply_header(ctx->sk.fd, rq.cookie, 0);
                pos = rq.from;
                do {
-                       rem = erofs_io_sendfile(&ctx->sk, &ctx->vd, &pos, 
rq.len);
-                       if (rem == -EINTR) {
-                               err = rem;
+                       written = erofs_io_sendfile(&ctx->sk, &ctx->vd, &pos, 
rq.len);
+                       if (written == -EINTR) {
+                               err = written;
                                goto out;
                        }
-               } while (rem < 0);
-               err = __erofs_0write(ctx->sk.fd, rem);
+               } while (written < 0);
+               err = __erofs_0write(ctx->sk.fd, rq.len - written);
                if (err) {
                        if (err > 0)
                                err = -EIO;
-- 
2.43.5


Reply via email to