Commit 571525bb authored by Miklos Szeredi's avatar Miklos Szeredi Committed by Adrian Bunk

fuse: fix hang on SMP

Fuse didn't always call i_size_write() with i_mutex held which caused
rare hangs on SMP/32bit.  This bug has been present since fuse-2.2,
well before being merged into mainline.

The simplest solution is to protect i_size_write() with the
per-connection spinlock.  Using i_mutex for this purpose would require
some restructuring of the code and I'm not even sure it's always safe
to acquire i_mutex in all places i_size needs to be set.

Since most of vmtruncate is already duplicated for other reasons,
duplicate the remaining part as well, making all i_size_write() calls
internal to fuse.

Using i_size_write() was unnecessary in fuse_init_inode(), since this
function is only called on a newly created locked inode.

Reported by a few people over the years, but special thanks to Dana
Henriksen who was persistent enough in helping me debug it.

Adrian Bunk:
Backported to 2.6.16.
Signed-off-by: default avatarMiklos Szeredi <miklos@szeredi.hu>
Signed-off-by: default avatarAdrian Bunk <bunk@stusta.de>
parent e79366b5
...@@ -897,14 +897,29 @@ static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg) ...@@ -897,14 +897,29 @@ static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg)
} }
} }
static void fuse_vmtruncate(struct inode *inode, loff_t offset)
{
int need_trunc;
spin_lock(&fuse_lock);
need_trunc = inode->i_size > offset;
i_size_write(inode, offset);
spin_unlock(&fuse_lock);
if (need_trunc) {
struct address_space *mapping = inode->i_mapping;
unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
truncate_inode_pages(mapping, offset);
}
}
/* /*
* Set attributes, and at the same time refresh them. * Set attributes, and at the same time refresh them.
* *
* Truncation is slightly complicated, because the 'truncate' request * Truncation is slightly complicated, because the 'truncate' request
* may fail, in which case we don't want to touch the mapping. * may fail, in which case we don't want to touch the mapping.
* vmtruncate() doesn't allow for this case. So do the rlimit * vmtruncate() doesn't allow for this case, so do the rlimit checking
* checking by hand and call vmtruncate() only after the file has * and the actual truncation by hand.
* actually been truncated.
*/ */
static int fuse_setattr(struct dentry *entry, struct iattr *attr) static int fuse_setattr(struct dentry *entry, struct iattr *attr)
{ {
...@@ -956,12 +971,8 @@ static int fuse_setattr(struct dentry *entry, struct iattr *attr) ...@@ -956,12 +971,8 @@ static int fuse_setattr(struct dentry *entry, struct iattr *attr)
make_bad_inode(inode); make_bad_inode(inode);
err = -EIO; err = -EIO;
} else { } else {
if (is_truncate) { if (is_truncate)
loff_t origsize = i_size_read(inode); fuse_vmtruncate(inode, outarg.attr.size);
i_size_write(inode, outarg.attr.size);
if (origsize > outarg.attr.size)
vmtruncate(inode, outarg.attr.size);
}
fuse_change_attributes(inode, &outarg.attr); fuse_change_attributes(inode, &outarg.attr);
fi->i_time = time_to_jiffies(outarg.attr_valid, fi->i_time = time_to_jiffies(outarg.attr_valid,
outarg.attr_valid_nsec); outarg.attr_valid_nsec);
......
...@@ -469,8 +469,10 @@ static int fuse_commit_write(struct file *file, struct page *page, ...@@ -469,8 +469,10 @@ static int fuse_commit_write(struct file *file, struct page *page,
err = -EIO; err = -EIO;
if (!err) { if (!err) {
pos += count; pos += count;
if (pos > i_size_read(inode)) spin_lock(&fuse_lock);
if (pos > inode->i_size)
i_size_write(inode, pos); i_size_write(inode, pos);
spin_unlock(&fuse_lock);
if (offset == 0 && to == PAGE_CACHE_SIZE) { if (offset == 0 && to == PAGE_CACHE_SIZE) {
clear_page_dirty(page); clear_page_dirty(page);
...@@ -570,8 +572,12 @@ static ssize_t fuse_direct_io(struct file *file, const char __user *buf, ...@@ -570,8 +572,12 @@ static ssize_t fuse_direct_io(struct file *file, const char __user *buf,
} }
fuse_put_request(fc, req); fuse_put_request(fc, req);
if (res > 0) { if (res > 0) {
if (write && pos > i_size_read(inode)) if (write) {
spin_lock(&fuse_lock);
if (pos > inode->i_size)
i_size_write(inode, pos); i_size_write(inode, pos);
spin_unlock(&fuse_lock);
}
*ppos = pos; *ppos = pos;
} }
fuse_invalidate_attr(inode); fuse_invalidate_attr(inode);
......
...@@ -115,7 +115,9 @@ void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr) ...@@ -115,7 +115,9 @@ void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr)
inode->i_nlink = attr->nlink; inode->i_nlink = attr->nlink;
inode->i_uid = attr->uid; inode->i_uid = attr->uid;
inode->i_gid = attr->gid; inode->i_gid = attr->gid;
spin_lock(&fuse_lock);
i_size_write(inode, attr->size); i_size_write(inode, attr->size);
spin_unlock(&fuse_lock);
inode->i_blksize = PAGE_CACHE_SIZE; inode->i_blksize = PAGE_CACHE_SIZE;
inode->i_blocks = attr->blocks; inode->i_blocks = attr->blocks;
inode->i_atime.tv_sec = attr->atime; inode->i_atime.tv_sec = attr->atime;
...@@ -129,7 +131,7 @@ void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr) ...@@ -129,7 +131,7 @@ void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr)
static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr) static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr)
{ {
inode->i_mode = attr->mode & S_IFMT; inode->i_mode = attr->mode & S_IFMT;
i_size_write(inode, attr->size); inode->i_size = attr->size;
if (S_ISREG(inode->i_mode)) { if (S_ISREG(inode->i_mode)) {
fuse_init_common(inode); fuse_init_common(inode);
fuse_init_file_inode(inode); fuse_init_file_inode(inode);
......
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