Skip to content

Commit 8373200

Browse files
xemulMiklos Szeredi
authored andcommitted
fuse: Trust kernel i_size only
Make fuse think that when writeback is on the inode's i_size is always up-to-date and not update it with the value received from the userspace. This is done because the page cache code may update i_size without letting the FS know. This assumption implies fixing the previously introduced short-read helper -- when a short read occurs the 'hole' is filled with zeroes. fuse_file_fallocate() is also fixed because now we should keep i_size up to date, so it must be updated if FUSE_FALLOCATE request succeeded. Signed-off-by: Maxim V. Patlasov <MPatlasov@parallels.com> Signed-off-by: Miklos Szeredi <mszeredi@suse.cz>
1 parent d5cd66c commit 8373200

3 files changed

Lines changed: 39 additions & 6 deletions

File tree

‎fs/fuse/dir.c‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,11 @@ static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
839839
struct kstat *stat)
840840
{
841841
unsigned int blkbits;
842+
struct fuse_conn *fc = get_fuse_conn(inode);
843+
844+
/* see the comment in fuse_change_attributes() */
845+
if (fc->writeback_cache && S_ISREG(inode->i_mode))
846+
attr->size = i_size_read(inode);
842847

843848
stat->dev = inode->i_sb->s_dev;
844849
stat->ino = attr->ino;
@@ -1580,6 +1585,7 @@ int fuse_do_setattr(struct inode *inode, struct iattr *attr,
15801585
struct fuse_setattr_in inarg;
15811586
struct fuse_attr_out outarg;
15821587
bool is_truncate = false;
1588+
bool is_wb = fc->writeback_cache;
15831589
loff_t oldsize;
15841590
int err;
15851591

@@ -1651,7 +1657,9 @@ int fuse_do_setattr(struct inode *inode, struct iattr *attr,
16511657
fuse_change_attributes_common(inode, &outarg.attr,
16521658
attr_timeout(&outarg));
16531659
oldsize = inode->i_size;
1654-
i_size_write(inode, outarg.attr.size);
1660+
/* see the comment in fuse_change_attributes() */
1661+
if (!is_wb || is_truncate || !S_ISREG(inode->i_mode))
1662+
i_size_write(inode, outarg.attr.size);
16551663

16561664
if (is_truncate) {
16571665
/* NOTE: this may release/reacquire fc->lock */
@@ -1663,7 +1671,8 @@ int fuse_do_setattr(struct inode *inode, struct iattr *attr,
16631671
* Only call invalidate_inode_pages2() after removing
16641672
* FUSE_NOWRITE, otherwise fuse_launder_page() would deadlock.
16651673
*/
1666-
if (S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) {
1674+
if ((is_truncate || !is_wb) &&
1675+
S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) {
16671676
truncate_pagecache(inode, outarg.attr.size);
16681677
invalidate_inode_pages2(inode->i_mapping);
16691678
}

‎fs/fuse/file.c‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -675,9 +675,26 @@ static void fuse_short_read(struct fuse_req *req, struct inode *inode,
675675
u64 attr_ver)
676676
{
677677
size_t num_read = req->out.args[0].size;
678+
struct fuse_conn *fc = get_fuse_conn(inode);
679+
680+
if (fc->writeback_cache) {
681+
/*
682+
* A hole in a file. Some data after the hole are in page cache,
683+
* but have not reached the client fs yet. So, the hole is not
684+
* present there.
685+
*/
686+
int i;
687+
int start_idx = num_read >> PAGE_CACHE_SHIFT;
688+
size_t off = num_read & (PAGE_CACHE_SIZE - 1);
678689

679-
loff_t pos = page_offset(req->pages[0]) + num_read;
680-
fuse_read_update_size(inode, pos, attr_ver);
690+
for (i = start_idx; i < req->num_pages; i++) {
691+
zero_user_segment(req->pages[i], off, PAGE_CACHE_SIZE);
692+
off = 0;
693+
}
694+
} else {
695+
loff_t pos = page_offset(req->pages[0]) + num_read;
696+
fuse_read_update_size(inode, pos, attr_ver);
697+
}
681698
}
682699

683700
static int fuse_readpage(struct file *file, struct page *page)

‎fs/fuse/inode.c‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr,
197197
{
198198
struct fuse_conn *fc = get_fuse_conn(inode);
199199
struct fuse_inode *fi = get_fuse_inode(inode);
200+
bool is_wb = fc->writeback_cache;
200201
loff_t oldsize;
201202
struct timespec old_mtime;
202203

@@ -211,10 +212,16 @@ void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr,
211212
fuse_change_attributes_common(inode, attr, attr_valid);
212213

213214
oldsize = inode->i_size;
214-
i_size_write(inode, attr->size);
215+
/*
216+
* In case of writeback_cache enabled, the cached writes beyond EOF
217+
* extend local i_size without keeping userspace server in sync. So,
218+
* attr->size coming from server can be stale. We cannot trust it.
219+
*/
220+
if (!is_wb || !S_ISREG(inode->i_mode))
221+
i_size_write(inode, attr->size);
215222
spin_unlock(&fc->lock);
216223

217-
if (S_ISREG(inode->i_mode)) {
224+
if (!is_wb && S_ISREG(inode->i_mode)) {
218225
bool inval = false;
219226

220227
if (oldsize != attr->size) {

0 commit comments

Comments
 (0)