diff options
author | Maxim Patlasov <MPatlasov@parallels.com> | 2013-06-13 04:16:39 -0400 |
---|---|---|
committer | Miklos Szeredi <mszeredi@suse.cz> | 2013-06-17 19:39:03 -0400 |
commit | 14c14414d157ea851119c96c61a17306a2b4a035 (patch) | |
tree | de18c44d2398a16d3248847b424d5cabd619b7bf /fs/fuse/file.c | |
parent | 8177a9d79c0e942dcac3312f15585d0344d505a5 (diff) |
fuse: hold i_mutex in fuse_file_fallocate()
Changing size of a file on server and local update (fuse_write_update_size)
should be always protected by inode->i_mutex. Otherwise a race like this is
possible:
1. Process 'A' calls fallocate(2) to extend file (~FALLOC_FL_KEEP_SIZE).
fuse_file_fallocate() sends FUSE_FALLOCATE request to the server.
2. Process 'B' calls ftruncate(2) shrinking the file. fuse_do_setattr()
sends shrinking FUSE_SETATTR request to the server and updates local i_size
by i_size_write(inode, outarg.attr.size).
3. Process 'A' resumes execution of fuse_file_fallocate() and calls
fuse_write_update_size(inode, offset + length). But 'offset + length' was
obsoleted by ftruncate from previous step.
Changed in v2 (thanks Brian and Anand for suggestions):
- made relation between mutex_lock() and fuse_set_nowrite(inode) more
explicit and clear.
- updated patch description to use ftruncate(2) in example
Signed-off-by: Maxim V. Patlasov <MPatlasov@parallels.com>
Reviewed-by: Brian Foster <bfoster@redhat.com>
Signed-off-by: Miklos Szeredi <mszeredi@suse.cz>
Diffstat (limited to 'fs/fuse/file.c')
-rw-r--r-- | fs/fuse/file.c | 12 |
1 files changed, 8 insertions, 4 deletions
diff --git a/fs/fuse/file.c b/fs/fuse/file.c index e570081f9f76..35f281033142 100644 --- a/fs/fuse/file.c +++ b/fs/fuse/file.c | |||
@@ -2470,13 +2470,16 @@ static long fuse_file_fallocate(struct file *file, int mode, loff_t offset, | |||
2470 | .mode = mode | 2470 | .mode = mode |
2471 | }; | 2471 | }; |
2472 | int err; | 2472 | int err; |
2473 | bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) || | ||
2474 | (mode & FALLOC_FL_PUNCH_HOLE); | ||
2473 | 2475 | ||
2474 | if (fc->no_fallocate) | 2476 | if (fc->no_fallocate) |
2475 | return -EOPNOTSUPP; | 2477 | return -EOPNOTSUPP; |
2476 | 2478 | ||
2477 | if (mode & FALLOC_FL_PUNCH_HOLE) { | 2479 | if (lock_inode) { |
2478 | mutex_lock(&inode->i_mutex); | 2480 | mutex_lock(&inode->i_mutex); |
2479 | fuse_set_nowrite(inode); | 2481 | if (mode & FALLOC_FL_PUNCH_HOLE) |
2482 | fuse_set_nowrite(inode); | ||
2480 | } | 2483 | } |
2481 | 2484 | ||
2482 | req = fuse_get_req_nopages(fc); | 2485 | req = fuse_get_req_nopages(fc); |
@@ -2511,8 +2514,9 @@ static long fuse_file_fallocate(struct file *file, int mode, loff_t offset, | |||
2511 | fuse_invalidate_attr(inode); | 2514 | fuse_invalidate_attr(inode); |
2512 | 2515 | ||
2513 | out: | 2516 | out: |
2514 | if (mode & FALLOC_FL_PUNCH_HOLE) { | 2517 | if (lock_inode) { |
2515 | fuse_release_nowrite(inode); | 2518 | if (mode & FALLOC_FL_PUNCH_HOLE) |
2519 | fuse_release_nowrite(inode); | ||
2516 | mutex_unlock(&inode->i_mutex); | 2520 | mutex_unlock(&inode->i_mutex); |
2517 | } | 2521 | } |
2518 | 2522 | ||