[PATCH 2/3] initramfs: pass a non-f_pos offset to xwrite

From: Christoph Hellwig
Date: Mon Jul 27 2020 - 12:07:59 EST


Pass a offset with the same scope as the file instead of ->f_pos.

Fixes: b63c1cd1c339 ("initramfs: switch initramfs unpacking to struct file based APIs")
Reported-by: Al Viro <viro@xxxxxxxxxxxxxxxxxx>
Signed-off-by: Christoph Hellwig <hch@xxxxxx>
---
init/initramfs.c | 15 ++++++++++-----
1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/init/initramfs.c b/init/initramfs.c
index 50ec7e3c5389aa..584bc8fe88e77c 100644
--- a/init/initramfs.c
+++ b/init/initramfs.c
@@ -13,13 +13,14 @@
#include <linux/memblock.h>
#include <linux/namei.h>

-static ssize_t __init xwrite(struct file *file, const char *p, size_t count)
+static ssize_t __init xwrite(struct file *file, const char *p, size_t count,
+ loff_t *pos)
{
ssize_t out = 0;

/* sys_write only can write MAX_RW_COUNT aka 2G-4K bytes at most */
while (count) {
- ssize_t rv = kernel_write(file, p, count, &file->f_pos);
+ ssize_t rv = kernel_write(file, p, count, pos);

if (rv < 0) {
if (rv == -EINTR || rv == -EAGAIN)
@@ -317,6 +318,7 @@ static int __init maybe_link(void)
}

static __initdata struct file *wfile;
+static __initdata loff_t wfile_pos;

static int __init do_name(void)
{
@@ -336,6 +338,7 @@ static int __init do_name(void)
wfile = filp_open(collected, openflags, mode);
if (IS_ERR(wfile))
return 0;
+ wfile_pos = 0;

vfs_fchown(wfile, uid, gid);
vfs_fchmod(wfile, mode);
@@ -365,7 +368,7 @@ static int __init do_copy(void)
if (byte_count >= body_len) {
struct timespec64 t[2] = { };

- if (xwrite(wfile, victim, body_len) != body_len)
+ if (xwrite(wfile, victim, body_len, &wfile_pos) != body_len)
error("write error");

t[0].tv_sec = mtime;
@@ -377,7 +380,7 @@ static int __init do_copy(void)
state = SkipIt;
return 0;
} else {
- if (xwrite(wfile, victim, byte_count) != byte_count)
+ if (xwrite(wfile, victim, byte_count, &wfile_pos) != byte_count)
error("write error");
body_len -= byte_count;
eat(byte_count);
@@ -580,6 +583,7 @@ static void __init populate_initrd_image(char *err)
{
ssize_t written;
struct file *file;
+ loff_t pos = 0;

unpack_to_rootfs(__initramfs_start, __initramfs_size);

@@ -589,7 +593,8 @@ static void __init populate_initrd_image(char *err)
if (IS_ERR(file))
return;

- written = xwrite(file, (char *)initrd_start, initrd_end - initrd_start);
+ written = xwrite(file, (char *)initrd_start, initrd_end - initrd_start,
+ &pos);
if (written != initrd_end - initrd_start)
pr_err("/initrd.image: incomplete write (%zd != %ld)\n",
written, initrd_end - initrd_start);
--
2.27.0