Commit 420405ec authored by Bart Van Assche's avatar Bart Van Assche Committed by Christoph Hellwig

configfs: fix the read and write iterators

Commit 7fe1e79b ("configfs: implement the .read_iter and .write_iter
methods") changed the simple_read_from_buffer() calls into copy_to_iter()
calls and the simple_write_to_buffer() calls into copy_from_iter() calls.
The simple*buffer() methods update the file offset (*ppos) but the read
and write iterators not yet. Make the read and write iterators update the
file offset (iocb->ki_pos).

This patch has been tested as follows:

 # modprobe target_core_user
 # dd if=/sys/kernel/config/target/dbroot bs=1
/var/target
12+0 records in
12+0 records out
12 bytes copied, 9.5539e-05 s, 126 kB/s

 # cd /sys/kernel/config/acpi/table
 # mkdir test
 # cd test
 # dmesg -c >/dev/null; printf 'SSDT\x8\0\0\0abcdefghijklmnopqrstuvwxyz' | dd of=aml bs=1; dmesg -c
34+0 records in
34+0 records out
34 bytes copied, 0.010627 s, 3.2 kB/s
[  261.056551] ACPI configfs: invalid table length
Reported-by: default avatarYanko Kaneti <yaneti@declera.com>
Cc: Yanko Kaneti <yaneti@declera.com>
Fixes: 7fe1e79b ("configfs: implement the .read_iter and .write_iter methods")
Signed-off-by: default avatarBart Van Assche <bvanassche@acm.org>
Signed-off-by: default avatarChristoph Hellwig <hch@lst.de>
parent 7fef2edf
...@@ -91,7 +91,10 @@ static ssize_t configfs_read_iter(struct kiocb *iocb, struct iov_iter *to) ...@@ -91,7 +91,10 @@ static ssize_t configfs_read_iter(struct kiocb *iocb, struct iov_iter *to)
} }
pr_debug("%s: count = %zd, pos = %lld, buf = %s\n", pr_debug("%s: count = %zd, pos = %lld, buf = %s\n",
__func__, iov_iter_count(to), iocb->ki_pos, buffer->page); __func__, iov_iter_count(to), iocb->ki_pos, buffer->page);
retval = copy_to_iter(buffer->page, buffer->count, to); if (iocb->ki_pos >= buffer->count)
goto out;
retval = copy_to_iter(buffer->page + iocb->ki_pos,
buffer->count - iocb->ki_pos, to);
iocb->ki_pos += retval; iocb->ki_pos += retval;
if (retval == 0) if (retval == 0)
retval = -EFAULT; retval = -EFAULT;
...@@ -162,7 +165,10 @@ static ssize_t configfs_bin_read_iter(struct kiocb *iocb, struct iov_iter *to) ...@@ -162,7 +165,10 @@ static ssize_t configfs_bin_read_iter(struct kiocb *iocb, struct iov_iter *to)
buffer->needs_read_fill = 0; buffer->needs_read_fill = 0;
} }
retval = copy_to_iter(buffer->bin_buffer, buffer->bin_buffer_size, to); if (iocb->ki_pos >= buffer->bin_buffer_size)
goto out;
retval = copy_to_iter(buffer->bin_buffer + iocb->ki_pos,
buffer->bin_buffer_size - iocb->ki_pos, to);
iocb->ki_pos += retval; iocb->ki_pos += retval;
if (retval == 0) if (retval == 0)
retval = -EFAULT; retval = -EFAULT;
...@@ -171,21 +177,28 @@ static ssize_t configfs_bin_read_iter(struct kiocb *iocb, struct iov_iter *to) ...@@ -171,21 +177,28 @@ static ssize_t configfs_bin_read_iter(struct kiocb *iocb, struct iov_iter *to)
return retval; return retval;
} }
static int fill_write_buffer(struct configfs_buffer *buffer, /* Fill [buffer, buffer + pos) with data coming from @from. */
static int fill_write_buffer(struct configfs_buffer *buffer, loff_t pos,
struct iov_iter *from) struct iov_iter *from)
{ {
loff_t to_copy;
int copied; int copied;
u8 *to;
if (!buffer->page) if (!buffer->page)
buffer->page = (char *)__get_free_pages(GFP_KERNEL, 0); buffer->page = (char *)__get_free_pages(GFP_KERNEL, 0);
if (!buffer->page) if (!buffer->page)
return -ENOMEM; return -ENOMEM;
copied = copy_from_iter(buffer->page, SIMPLE_ATTR_SIZE - 1, from); to_copy = SIMPLE_ATTR_SIZE - 1 - pos;
if (to_copy <= 0)
return 0;
to = buffer->page + pos;
copied = copy_from_iter(to, to_copy, from);
buffer->needs_read_fill = 1; buffer->needs_read_fill = 1;
/* if buf is assumed to contain a string, terminate it by \0, /* if buf is assumed to contain a string, terminate it by \0,
* so e.g. sscanf() can scan the string easily */ * so e.g. sscanf() can scan the string easily */
buffer->page[copied] = 0; to[copied] = 0;
return copied ? : -EFAULT; return copied ? : -EFAULT;
} }
...@@ -217,7 +230,7 @@ static ssize_t configfs_write_iter(struct kiocb *iocb, struct iov_iter *from) ...@@ -217,7 +230,7 @@ static ssize_t configfs_write_iter(struct kiocb *iocb, struct iov_iter *from)
ssize_t len; ssize_t len;
mutex_lock(&buffer->mutex); mutex_lock(&buffer->mutex);
len = fill_write_buffer(buffer, from); len = fill_write_buffer(buffer, iocb->ki_pos, from);
if (len > 0) if (len > 0)
len = flush_write_buffer(file, buffer, len); len = flush_write_buffer(file, buffer, len);
if (len > 0) if (len > 0)
...@@ -272,7 +285,9 @@ static ssize_t configfs_bin_write_iter(struct kiocb *iocb, ...@@ -272,7 +285,9 @@ static ssize_t configfs_bin_write_iter(struct kiocb *iocb,
buffer->bin_buffer_size = end_offset; buffer->bin_buffer_size = end_offset;
} }
len = copy_from_iter(buffer->bin_buffer, buffer->bin_buffer_size, from); len = copy_from_iter(buffer->bin_buffer + iocb->ki_pos,
buffer->bin_buffer_size - iocb->ki_pos, from);
iocb->ki_pos += len;
out: out:
mutex_unlock(&buffer->mutex); mutex_unlock(&buffer->mutex);
return len ? : -EFAULT; return len ? : -EFAULT;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment