Skip to content

Commit 7097c96

Browse files
committed
cachefiles: Fix __cachefiles_prepare_write()
Fix __cachefiles_prepare_write() to correctly determine whether the requested write will fit correctly with the DIO alignment. Reported-by: Gao Xiang <hsiangkao@linux.alibaba.com> Signed-off-by: David Howells <dhowells@redhat.com> Tested-by: Yiqun Leng <yqleng@linux.alibaba.com> Tested-by: Jia Zhu <zhujia.zj@bytedance.com> cc: Jeff Layton <jlayton@kernel.org> cc: linux-cachefs@redhat.com cc: linux-erofs@lists.ozlabs.org cc: linux-fsdevel@vger.kernel.org cc: linux-mm@kvack.org
1 parent 80105ed commit 7097c96

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

fs/cachefiles/io.c

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -522,16 +522,22 @@ int __cachefiles_prepare_write(struct cachefiles_object *object,
522522
bool no_space_allocated_yet)
523523
{
524524
struct cachefiles_cache *cache = object->volume->cache;
525-
loff_t start = *_start, pos;
526-
size_t len = *_len, down;
525+
unsigned long long start = *_start, pos;
526+
size_t len = *_len;
527527
int ret;
528528

529529
/* Round to DIO size */
530-
down = start - round_down(start, PAGE_SIZE);
531-
*_start = start - down;
532-
*_len = round_up(down + len, PAGE_SIZE);
533-
if (down < start || *_len > upper_len)
530+
start = round_down(*_start, PAGE_SIZE);
531+
if (start != *_start) {
532+
kleave(" = -ENOBUFS [down]");
533+
return -ENOBUFS;
534+
}
535+
if (*_len > upper_len) {
536+
kleave(" = -ENOBUFS [up]");
534537
return -ENOBUFS;
538+
}
539+
540+
*_len = round_up(len, PAGE_SIZE);
535541

536542
/* We need to work out whether there's sufficient disk space to perform
537543
* the write - but we can skip that check if we have space already
@@ -542,15 +548,15 @@ int __cachefiles_prepare_write(struct cachefiles_object *object,
542548

543549
pos = cachefiles_inject_read_error();
544550
if (pos == 0)
545-
pos = vfs_llseek(file, *_start, SEEK_DATA);
551+
pos = vfs_llseek(file, start, SEEK_DATA);
546552
if (pos < 0 && pos >= (loff_t)-MAX_ERRNO) {
547553
if (pos == -ENXIO)
548554
goto check_space; /* Unallocated tail */
549555
trace_cachefiles_io_error(object, file_inode(file), pos,
550556
cachefiles_trace_seek_error);
551557
return pos;
552558
}
553-
if ((u64)pos >= (u64)*_start + *_len)
559+
if (pos >= start + *_len)
554560
goto check_space; /* Unallocated region */
555561

556562
/* We have a block that's at least partially filled - if we're low on
@@ -563,21 +569,21 @@ int __cachefiles_prepare_write(struct cachefiles_object *object,
563569

564570
pos = cachefiles_inject_read_error();
565571
if (pos == 0)
566-
pos = vfs_llseek(file, *_start, SEEK_HOLE);
572+
pos = vfs_llseek(file, start, SEEK_HOLE);
567573
if (pos < 0 && pos >= (loff_t)-MAX_ERRNO) {
568574
trace_cachefiles_io_error(object, file_inode(file), pos,
569575
cachefiles_trace_seek_error);
570576
return pos;
571577
}
572-
if ((u64)pos >= (u64)*_start + *_len)
578+
if (pos >= start + *_len)
573579
return 0; /* Fully allocated */
574580

575581
/* Partially allocated, but insufficient space: cull. */
576582
fscache_count_no_write_space();
577583
ret = cachefiles_inject_remove_error();
578584
if (ret == 0)
579585
ret = vfs_fallocate(file, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
580-
*_start, *_len);
586+
start, *_len);
581587
if (ret < 0) {
582588
trace_cachefiles_io_error(object, file_inode(file), ret,
583589
cachefiles_trace_fallocate_error);

0 commit comments

Comments
 (0)