linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] fuse: hold i_mutex in fuse_file_fallocate() - v2
@ 2013-06-13  8:16 Maxim Patlasov
  2013-06-13 11:46 ` Brian Foster
  0 siblings, 1 reply; 3+ messages in thread
From: Maxim Patlasov @ 2013-06-13  8:16 UTC (permalink / raw)
  To: miklos; +Cc: fuse-devel, bfoster, avati, linux-kernel, devel

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>
---
 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 e570081..35f2810 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,
 		.mode = mode
 	};
 	int err;
+	bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
+			   (mode & FALLOC_FL_PUNCH_HOLE);
 
 	if (fc->no_fallocate)
 		return -EOPNOTSUPP;
 
-	if (mode & FALLOC_FL_PUNCH_HOLE) {
+	if (lock_inode) {
 		mutex_lock(&inode->i_mutex);
-		fuse_set_nowrite(inode);
+		if (mode & FALLOC_FL_PUNCH_HOLE)
+			fuse_set_nowrite(inode);
 	}
 
 	req = fuse_get_req_nopages(fc);
@@ -2511,8 +2514,9 @@ static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
 	fuse_invalidate_attr(inode);
 
 out:
-	if (mode & FALLOC_FL_PUNCH_HOLE) {
-		fuse_release_nowrite(inode);
+	if (lock_inode) {
+		if (mode & FALLOC_FL_PUNCH_HOLE)
+			fuse_release_nowrite(inode);
 		mutex_unlock(&inode->i_mutex);
 	}
 


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] fuse: hold i_mutex in fuse_file_fallocate() - v2
  2013-06-13  8:16 [PATCH] fuse: hold i_mutex in fuse_file_fallocate() - v2 Maxim Patlasov
@ 2013-06-13 11:46 ` Brian Foster
  2013-06-17 23:49   ` Miklos Szeredi
  0 siblings, 1 reply; 3+ messages in thread
From: Brian Foster @ 2013-06-13 11:46 UTC (permalink / raw)
  To: Maxim Patlasov; +Cc: miklos, fuse-devel, avati, linux-kernel, devel

On 06/13/2013 04:16 AM, Maxim Patlasov wrote:
> 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>
> ---

Makes more sense and I like the cleanup... thanks.

Reviewed-by: Brian Foster <bfoster@redhat.com>

>  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 e570081..35f2810 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,
>  		.mode = mode
>  	};
>  	int err;
> +	bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
> +			   (mode & FALLOC_FL_PUNCH_HOLE);
>  
>  	if (fc->no_fallocate)
>  		return -EOPNOTSUPP;
>  
> -	if (mode & FALLOC_FL_PUNCH_HOLE) {
> +	if (lock_inode) {
>  		mutex_lock(&inode->i_mutex);
> -		fuse_set_nowrite(inode);
> +		if (mode & FALLOC_FL_PUNCH_HOLE)
> +			fuse_set_nowrite(inode);
>  	}
>  
>  	req = fuse_get_req_nopages(fc);
> @@ -2511,8 +2514,9 @@ static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
>  	fuse_invalidate_attr(inode);
>  
>  out:
> -	if (mode & FALLOC_FL_PUNCH_HOLE) {
> -		fuse_release_nowrite(inode);
> +	if (lock_inode) {
> +		if (mode & FALLOC_FL_PUNCH_HOLE)
> +			fuse_release_nowrite(inode);
>  		mutex_unlock(&inode->i_mutex);
>  	}
>  
> 


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] fuse: hold i_mutex in fuse_file_fallocate() - v2
  2013-06-13 11:46 ` Brian Foster
@ 2013-06-17 23:49   ` Miklos Szeredi
  0 siblings, 0 replies; 3+ messages in thread
From: Miklos Szeredi @ 2013-06-17 23:49 UTC (permalink / raw)
  To: Brian Foster
  Cc: Maxim Patlasov, fuse-devel, Anand Avati, Kernel Mailing List,
	devel

On Thu, Jun 13, 2013 at 1:46 PM, Brian Foster <bfoster@redhat.com> wrote:
> On 06/13/2013 04:16 AM, Maxim Patlasov wrote:
>> 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>

Thanks, applied.

Miklos

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2013-06-17 23:49 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-06-13  8:16 [PATCH] fuse: hold i_mutex in fuse_file_fallocate() - v2 Maxim Patlasov
2013-06-13 11:46 ` Brian Foster
2013-06-17 23:49   ` Miklos Szeredi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).