Skip to content

Commit 10c35ab

Browse files
Zizhi Wobrauner
authored andcommitted
cachefiles: Fix incorrect length return value in cachefiles_ondemand_fd_write_iter()
cachefiles_ondemand_fd_write_iter() function first aligns "pos" and "len" to block boundaries. When calling __cachefiles_write(), the aligned "pos" is passed in, but "len" is the original unaligned value(iter->count). Additionally, the returned length of the write operation is the modified "len" aligned by block size, which is unreasonable. The alignment of "pos" and "len" is intended only to check whether the cache has enough space. But the modified len should not be used as the return value of cachefiles_ondemand_fd_write_iter() because the length we passed to __cachefiles_write() is the previous "len". Doing so would result in a mismatch in the data written on-demand. For example, if the length of the user state passed in is not aligned to the block size (the preread scene/DIO writes only need 512 alignment/Fault injection), the length of the write will differ from the actual length of the return. To solve this issue, since the __cachefiles_prepare_write() modifies the size of "len", we pass "aligned_len" to __cachefiles_prepare_write() to calculate the free blocks and use the original "len" as the return value of cachefiles_ondemand_fd_write_iter(). Fixes: c838305 ("cachefiles: notify the user daemon when looking up cookie") Signed-off-by: Zizhi Wo <[email protected]> Link: https://lore.kernel.org/r/[email protected] Reviewed-by: David Howells <[email protected]> Signed-off-by: Christian Brauner <[email protected]>
1 parent 9b8e809 commit 10c35ab

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

fs/cachefiles/ondemand.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ static ssize_t cachefiles_ondemand_fd_write_iter(struct kiocb *kiocb,
6161
struct cachefiles_object *object = kiocb->ki_filp->private_data;
6262
struct cachefiles_cache *cache = object->volume->cache;
6363
struct file *file = object->file;
64-
size_t len = iter->count;
64+
size_t len = iter->count, aligned_len = len;
6565
loff_t pos = kiocb->ki_pos;
6666
const struct cred *saved_cred;
6767
int ret;
@@ -70,7 +70,7 @@ static ssize_t cachefiles_ondemand_fd_write_iter(struct kiocb *kiocb,
7070
return -ENOBUFS;
7171

7272
cachefiles_begin_secure(cache, &saved_cred);
73-
ret = __cachefiles_prepare_write(object, file, &pos, &len, len, true);
73+
ret = __cachefiles_prepare_write(object, file, &pos, &aligned_len, len, true);
7474
cachefiles_end_secure(cache, saved_cred);
7575
if (ret < 0)
7676
return ret;

0 commit comments

Comments
 (0)