Linux-f2fs-devel Archive on lore.kernel.org
 help / color / Atom feed
* [f2fs-dev] [PATCH] f2fs: remove race condition in releasing cblocks
@ 2020-05-08  9:29 Daeho Jeong
  2020-05-08 10:09 ` Chao Yu
  2020-05-08 13:47 ` Jaegeuk Kim
  0 siblings, 2 replies; 11+ messages in thread
From: Daeho Jeong @ 2020-05-08  9:29 UTC (permalink / raw)
  To: kernel, linux-f2fs-devel, kernel-team, jaegeuk, yuchao0; +Cc: Daeho Jeong

From: Daeho Jeong <daehojeong@google.com>

Now, if writing pages and releasing compress blocks occur
simultaneously, and releasing cblocks is executed more than one time
to a file, then total block count of filesystem and block count of the
file could be incorrect and damaged.

We have to execute releasing compress blocks only one time for a file
without being interfered by writepages path.

Signed-off-by: Daeho Jeong <daehojeong@google.com>
---
 fs/f2fs/file.c | 34 ++++++++++++++++++++++++----------
 1 file changed, 24 insertions(+), 10 deletions(-)

diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
index 4aab4b42d8ba..f7de2a1da528 100644
--- a/fs/f2fs/file.c
+++ b/fs/f2fs/file.c
@@ -3488,6 +3488,7 @@ static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)
 	pgoff_t page_idx = 0, last_idx;
 	unsigned int released_blocks = 0;
 	int ret;
+	int writecount;
 
 	if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
 		return -EOPNOTSUPP;
@@ -3502,20 +3503,33 @@ static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)
 	if (ret)
 		return ret;
 
-	if (!F2FS_I(inode)->i_compr_blocks)
-		goto out;
-
 	f2fs_balance_fs(F2FS_I_SB(inode), true);
 
 	inode_lock(inode);
 
-	if (!IS_IMMUTABLE(inode)) {
-		F2FS_I(inode)->i_flags |= F2FS_IMMUTABLE_FL;
-		f2fs_set_inode_flags(inode);
-		inode->i_ctime = current_time(inode);
-		f2fs_mark_inode_dirty_sync(inode, true);
+	writecount = atomic_read(&inode->i_writecount);
+	if ((filp->f_mode & FMODE_WRITE && writecount != 1) || writecount) {
+		ret = -EBUSY;
+		goto out;
 	}
 
+	if (IS_IMMUTABLE(inode)) {
+		ret = -EINVAL;
+		goto out;
+	}
+
+	ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
+	if (ret)
+		goto out;
+
+	if (!F2FS_I(inode)->i_compr_blocks)
+		goto out;
+
+	F2FS_I(inode)->i_flags |= F2FS_IMMUTABLE_FL;
+	f2fs_set_inode_flags(inode);
+	inode->i_ctime = current_time(inode);
+	f2fs_mark_inode_dirty_sync(inode, true);
+
 	down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
 	down_write(&F2FS_I(inode)->i_mmap_sem);
 
@@ -3554,9 +3568,9 @@ static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)
 
 	up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
 	up_write(&F2FS_I(inode)->i_mmap_sem);
-
-	inode_unlock(inode);
 out:
+	inode_unlock(inode);
+
 	mnt_drop_write_file(filp);
 
 	if (ret >= 0) {
-- 
2.26.2.645.ge9eca65c58-goog



_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

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

* Re: [f2fs-dev] [PATCH] f2fs: remove race condition in releasing cblocks
  2020-05-08  9:29 [f2fs-dev] [PATCH] f2fs: remove race condition in releasing cblocks Daeho Jeong
@ 2020-05-08 10:09 ` Chao Yu
  2020-05-08 13:47 ` Jaegeuk Kim
  1 sibling, 0 replies; 11+ messages in thread
From: Chao Yu @ 2020-05-08 10:09 UTC (permalink / raw)
  To: Daeho Jeong, kernel, linux-f2fs-devel, kernel-team, jaegeuk; +Cc: Daeho Jeong

On 2020/5/8 17:29, Daeho Jeong wrote:
> From: Daeho Jeong <daehojeong@google.com>
> 
> Now, if writing pages and releasing compress blocks occur
> simultaneously, and releasing cblocks is executed more than one time
> to a file, then total block count of filesystem and block count of the
> file could be incorrect and damaged.
> 
> We have to execute releasing compress blocks only one time for a file
> without being interfered by writepages path.
> 
> Signed-off-by: Daeho Jeong <daehojeong@google.com>

Reviewed-by: Chao Yu <yuchao0@huawei.com>

Thanks,


_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

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

* Re: [f2fs-dev] [PATCH] f2fs: remove race condition in releasing cblocks
  2020-05-08  9:29 [f2fs-dev] [PATCH] f2fs: remove race condition in releasing cblocks Daeho Jeong
  2020-05-08 10:09 ` Chao Yu
@ 2020-05-08 13:47 ` Jaegeuk Kim
  1 sibling, 0 replies; 11+ messages in thread
From: Jaegeuk Kim @ 2020-05-08 13:47 UTC (permalink / raw)
  To: Daeho Jeong; +Cc: kernel-team, kernel, Daeho Jeong, linux-f2fs-devel

Hi Daeho,

Please let me integrate this patch into the original patch since it is still in
the dev branch.

Thanks,

On 05/08, Daeho Jeong wrote:
> From: Daeho Jeong <daehojeong@google.com>
> 
> Now, if writing pages and releasing compress blocks occur
> simultaneously, and releasing cblocks is executed more than one time
> to a file, then total block count of filesystem and block count of the
> file could be incorrect and damaged.
> 
> We have to execute releasing compress blocks only one time for a file
> without being interfered by writepages path.
> 
> Signed-off-by: Daeho Jeong <daehojeong@google.com>
> ---
>  fs/f2fs/file.c | 34 ++++++++++++++++++++++++----------
>  1 file changed, 24 insertions(+), 10 deletions(-)
> 
> diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
> index 4aab4b42d8ba..f7de2a1da528 100644
> --- a/fs/f2fs/file.c
> +++ b/fs/f2fs/file.c
> @@ -3488,6 +3488,7 @@ static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)
>  	pgoff_t page_idx = 0, last_idx;
>  	unsigned int released_blocks = 0;
>  	int ret;
> +	int writecount;
>  
>  	if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
>  		return -EOPNOTSUPP;
> @@ -3502,20 +3503,33 @@ static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)
>  	if (ret)
>  		return ret;
>  
> -	if (!F2FS_I(inode)->i_compr_blocks)
> -		goto out;
> -
>  	f2fs_balance_fs(F2FS_I_SB(inode), true);
>  
>  	inode_lock(inode);
>  
> -	if (!IS_IMMUTABLE(inode)) {
> -		F2FS_I(inode)->i_flags |= F2FS_IMMUTABLE_FL;
> -		f2fs_set_inode_flags(inode);
> -		inode->i_ctime = current_time(inode);
> -		f2fs_mark_inode_dirty_sync(inode, true);
> +	writecount = atomic_read(&inode->i_writecount);
> +	if ((filp->f_mode & FMODE_WRITE && writecount != 1) || writecount) {
> +		ret = -EBUSY;
> +		goto out;
>  	}
>  
> +	if (IS_IMMUTABLE(inode)) {
> +		ret = -EINVAL;
> +		goto out;
> +	}
> +
> +	ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
> +	if (ret)
> +		goto out;
> +
> +	if (!F2FS_I(inode)->i_compr_blocks)
> +		goto out;
> +
> +	F2FS_I(inode)->i_flags |= F2FS_IMMUTABLE_FL;
> +	f2fs_set_inode_flags(inode);
> +	inode->i_ctime = current_time(inode);
> +	f2fs_mark_inode_dirty_sync(inode, true);
> +
>  	down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
>  	down_write(&F2FS_I(inode)->i_mmap_sem);
>  
> @@ -3554,9 +3568,9 @@ static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)
>  
>  	up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
>  	up_write(&F2FS_I(inode)->i_mmap_sem);
> -
> -	inode_unlock(inode);
>  out:
> +	inode_unlock(inode);
> +
>  	mnt_drop_write_file(filp);
>  
>  	if (ret >= 0) {
> -- 
> 2.26.2.645.ge9eca65c58-goog


_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

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

* Re: [f2fs-dev] [PATCH] f2fs: remove race condition in releasing cblocks
  2020-05-08 13:48 ` Jaegeuk Kim
@ 2020-05-08 13:58   ` Jaegeuk Kim
  0 siblings, 0 replies; 11+ messages in thread
From: Jaegeuk Kim @ 2020-05-08 13:58 UTC (permalink / raw)
  To: Daeho Jeong; +Cc: kernel, kernel-team, Daeho Jeong, linux-f2fs-devel

On 05/08, Jaegeuk Kim wrote:
> Is this v2?

nvm. it seems the same version.

> 
> On 05/08, Daeho Jeong wrote:
> > From: Daeho Jeong <daehojeong@google.com>
> > 
> > Now, if writing pages and releasing compress blocks occur
> > simultaneously, and releasing cblocks is executed more than one time
> > to a file, then total block count of filesystem and block count of the
> > file could be incorrect and damaged.
> > 
> > We have to execute releasing compress blocks only one time for a file
> > without being interfered by writepages path.
> > 
> > Signed-off-by: Daeho Jeong <daehojeong@google.com>
> > Reviewed-by: Chao Yu <yuchao0@huawei.com>
> > ---
> >  fs/f2fs/file.c | 34 ++++++++++++++++++++++++----------
> >  1 file changed, 24 insertions(+), 10 deletions(-)
> > 
> > diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
> > index 4aab4b42d8ba..f7de2a1da528 100644
> > --- a/fs/f2fs/file.c
> > +++ b/fs/f2fs/file.c
> > @@ -3488,6 +3488,7 @@ static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)
> >  	pgoff_t page_idx = 0, last_idx;
> >  	unsigned int released_blocks = 0;
> >  	int ret;
> > +	int writecount;
> >  
> >  	if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
> >  		return -EOPNOTSUPP;
> > @@ -3502,20 +3503,33 @@ static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)
> >  	if (ret)
> >  		return ret;
> >  
> > -	if (!F2FS_I(inode)->i_compr_blocks)
> > -		goto out;
> > -
> >  	f2fs_balance_fs(F2FS_I_SB(inode), true);
> >  
> >  	inode_lock(inode);
> >  
> > -	if (!IS_IMMUTABLE(inode)) {
> > -		F2FS_I(inode)->i_flags |= F2FS_IMMUTABLE_FL;
> > -		f2fs_set_inode_flags(inode);
> > -		inode->i_ctime = current_time(inode);
> > -		f2fs_mark_inode_dirty_sync(inode, true);
> > +	writecount = atomic_read(&inode->i_writecount);
> > +	if ((filp->f_mode & FMODE_WRITE && writecount != 1) || writecount) {
> > +		ret = -EBUSY;
> > +		goto out;
> >  	}
> >  
> > +	if (IS_IMMUTABLE(inode)) {
> > +		ret = -EINVAL;
> > +		goto out;
> > +	}
> > +
> > +	ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
> > +	if (ret)
> > +		goto out;
> > +
> > +	if (!F2FS_I(inode)->i_compr_blocks)
> > +		goto out;
> > +
> > +	F2FS_I(inode)->i_flags |= F2FS_IMMUTABLE_FL;
> > +	f2fs_set_inode_flags(inode);
> > +	inode->i_ctime = current_time(inode);
> > +	f2fs_mark_inode_dirty_sync(inode, true);
> > +
> >  	down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
> >  	down_write(&F2FS_I(inode)->i_mmap_sem);
> >  
> > @@ -3554,9 +3568,9 @@ static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)
> >  
> >  	up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
> >  	up_write(&F2FS_I(inode)->i_mmap_sem);
> > -
> > -	inode_unlock(inode);
> >  out:
> > +	inode_unlock(inode);
> > +
> >  	mnt_drop_write_file(filp);
> >  
> >  	if (ret >= 0) {
> > -- 
> > 2.26.2.645.ge9eca65c58-goog
> 
> 
> _______________________________________________
> Linux-f2fs-devel mailing list
> Linux-f2fs-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

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

* Re: [f2fs-dev] [PATCH] f2fs: remove race condition in releasing cblocks
  2020-05-08 11:56 Daeho Jeong
@ 2020-05-08 13:48 ` Jaegeuk Kim
  2020-05-08 13:58   ` Jaegeuk Kim
  0 siblings, 1 reply; 11+ messages in thread
From: Jaegeuk Kim @ 2020-05-08 13:48 UTC (permalink / raw)
  To: Daeho Jeong; +Cc: kernel-team, kernel, Daeho Jeong, linux-f2fs-devel

Is this v2?

On 05/08, Daeho Jeong wrote:
> From: Daeho Jeong <daehojeong@google.com>
> 
> Now, if writing pages and releasing compress blocks occur
> simultaneously, and releasing cblocks is executed more than one time
> to a file, then total block count of filesystem and block count of the
> file could be incorrect and damaged.
> 
> We have to execute releasing compress blocks only one time for a file
> without being interfered by writepages path.
> 
> Signed-off-by: Daeho Jeong <daehojeong@google.com>
> Reviewed-by: Chao Yu <yuchao0@huawei.com>
> ---
>  fs/f2fs/file.c | 34 ++++++++++++++++++++++++----------
>  1 file changed, 24 insertions(+), 10 deletions(-)
> 
> diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
> index 4aab4b42d8ba..f7de2a1da528 100644
> --- a/fs/f2fs/file.c
> +++ b/fs/f2fs/file.c
> @@ -3488,6 +3488,7 @@ static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)
>  	pgoff_t page_idx = 0, last_idx;
>  	unsigned int released_blocks = 0;
>  	int ret;
> +	int writecount;
>  
>  	if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
>  		return -EOPNOTSUPP;
> @@ -3502,20 +3503,33 @@ static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)
>  	if (ret)
>  		return ret;
>  
> -	if (!F2FS_I(inode)->i_compr_blocks)
> -		goto out;
> -
>  	f2fs_balance_fs(F2FS_I_SB(inode), true);
>  
>  	inode_lock(inode);
>  
> -	if (!IS_IMMUTABLE(inode)) {
> -		F2FS_I(inode)->i_flags |= F2FS_IMMUTABLE_FL;
> -		f2fs_set_inode_flags(inode);
> -		inode->i_ctime = current_time(inode);
> -		f2fs_mark_inode_dirty_sync(inode, true);
> +	writecount = atomic_read(&inode->i_writecount);
> +	if ((filp->f_mode & FMODE_WRITE && writecount != 1) || writecount) {
> +		ret = -EBUSY;
> +		goto out;
>  	}
>  
> +	if (IS_IMMUTABLE(inode)) {
> +		ret = -EINVAL;
> +		goto out;
> +	}
> +
> +	ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
> +	if (ret)
> +		goto out;
> +
> +	if (!F2FS_I(inode)->i_compr_blocks)
> +		goto out;
> +
> +	F2FS_I(inode)->i_flags |= F2FS_IMMUTABLE_FL;
> +	f2fs_set_inode_flags(inode);
> +	inode->i_ctime = current_time(inode);
> +	f2fs_mark_inode_dirty_sync(inode, true);
> +
>  	down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
>  	down_write(&F2FS_I(inode)->i_mmap_sem);
>  
> @@ -3554,9 +3568,9 @@ static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)
>  
>  	up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
>  	up_write(&F2FS_I(inode)->i_mmap_sem);
> -
> -	inode_unlock(inode);
>  out:
> +	inode_unlock(inode);
> +
>  	mnt_drop_write_file(filp);
>  
>  	if (ret >= 0) {
> -- 
> 2.26.2.645.ge9eca65c58-goog


_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

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

* [f2fs-dev] [PATCH] f2fs: remove race condition in releasing cblocks
@ 2020-05-08 11:56 Daeho Jeong
  2020-05-08 13:48 ` Jaegeuk Kim
  0 siblings, 1 reply; 11+ messages in thread
From: Daeho Jeong @ 2020-05-08 11:56 UTC (permalink / raw)
  To: kernel, linux-f2fs-devel, kernel-team, jaegeuk, yuchao0; +Cc: Daeho Jeong

From: Daeho Jeong <daehojeong@google.com>

Now, if writing pages and releasing compress blocks occur
simultaneously, and releasing cblocks is executed more than one time
to a file, then total block count of filesystem and block count of the
file could be incorrect and damaged.

We have to execute releasing compress blocks only one time for a file
without being interfered by writepages path.

Signed-off-by: Daeho Jeong <daehojeong@google.com>
Reviewed-by: Chao Yu <yuchao0@huawei.com>
---
 fs/f2fs/file.c | 34 ++++++++++++++++++++++++----------
 1 file changed, 24 insertions(+), 10 deletions(-)

diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
index 4aab4b42d8ba..f7de2a1da528 100644
--- a/fs/f2fs/file.c
+++ b/fs/f2fs/file.c
@@ -3488,6 +3488,7 @@ static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)
 	pgoff_t page_idx = 0, last_idx;
 	unsigned int released_blocks = 0;
 	int ret;
+	int writecount;
 
 	if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
 		return -EOPNOTSUPP;
@@ -3502,20 +3503,33 @@ static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)
 	if (ret)
 		return ret;
 
-	if (!F2FS_I(inode)->i_compr_blocks)
-		goto out;
-
 	f2fs_balance_fs(F2FS_I_SB(inode), true);
 
 	inode_lock(inode);
 
-	if (!IS_IMMUTABLE(inode)) {
-		F2FS_I(inode)->i_flags |= F2FS_IMMUTABLE_FL;
-		f2fs_set_inode_flags(inode);
-		inode->i_ctime = current_time(inode);
-		f2fs_mark_inode_dirty_sync(inode, true);
+	writecount = atomic_read(&inode->i_writecount);
+	if ((filp->f_mode & FMODE_WRITE && writecount != 1) || writecount) {
+		ret = -EBUSY;
+		goto out;
 	}
 
+	if (IS_IMMUTABLE(inode)) {
+		ret = -EINVAL;
+		goto out;
+	}
+
+	ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
+	if (ret)
+		goto out;
+
+	if (!F2FS_I(inode)->i_compr_blocks)
+		goto out;
+
+	F2FS_I(inode)->i_flags |= F2FS_IMMUTABLE_FL;
+	f2fs_set_inode_flags(inode);
+	inode->i_ctime = current_time(inode);
+	f2fs_mark_inode_dirty_sync(inode, true);
+
 	down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
 	down_write(&F2FS_I(inode)->i_mmap_sem);
 
@@ -3554,9 +3568,9 @@ static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)
 
 	up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
 	up_write(&F2FS_I(inode)->i_mmap_sem);
-
-	inode_unlock(inode);
 out:
+	inode_unlock(inode);
+
 	mnt_drop_write_file(filp);
 
 	if (ret >= 0) {
-- 
2.26.2.645.ge9eca65c58-goog



_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

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

* Re: [f2fs-dev] [PATCH] f2fs: remove race condition in releasing cblocks
  2020-05-08  7:09     ` Chao Yu
@ 2020-05-08  7:10       ` Daeho Jeong
  0 siblings, 0 replies; 11+ messages in thread
From: Daeho Jeong @ 2020-05-08  7:10 UTC (permalink / raw)
  To: Chao Yu; +Cc: Daeho Jeong, kernel-team, linux-kernel, linux-f2fs-devel

Oops,

I will re-check it.

Thanks,

2020년 5월 8일 (금) 오후 4:09, Chao Yu <yuchao0@huawei.com>님이 작성:
>
> On 2020/5/8 14:58, Daeho Jeong wrote:
> > I moved checking i_compr_blocks phrase after calling inode_lock()
> > already, because we should check this after writing pages.
> >
> > So, if it fails to check i_compr_blocks, we need to call inode_unlock().
> >
> > Am I missing something?
>
> After applying this patch, I get this:
>
>         ret = mnt_want_write_file(filp);
>         if (ret)
>                 return ret;
>
>         if (!F2FS_I(inode)->i_compr_blocks)
>                 goto out;
>
>         f2fs_balance_fs(F2FS_I_SB(inode), true);
>
>         inode_lock(inode);
>
> >
> > 2020년 5월 8일 (금) 오후 3:50, Chao Yu <yuchao0@huawei.com>님이 작성:
> >>
> >> On 2020/5/8 12:25, Daeho Jeong wrote:
> >>> From: Daeho Jeong <daehojeong@google.com>
> >>>
> >>> Now, if writing pages and releasing compress blocks occur
> >>> simultaneously, and releasing cblocks is executed more than one time
> >>> to a file, then total block count of filesystem and block count of the
> >>> file could be incorrect and damaged.
> >>>
> >>> We have to execute releasing compress blocks only one time for a file
> >>> without being interfered by writepages path.
> >>>
> >>> Signed-off-by: Daeho Jeong <daehojeong@google.com>
> >>> ---
> >>>  fs/f2fs/file.c | 31 ++++++++++++++++++++++++-------
> >>>  1 file changed, 24 insertions(+), 7 deletions(-)
> >>>
> >>> diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
> >>> index 4aab4b42d8ba..a92bc51b9b28 100644
> >>> --- a/fs/f2fs/file.c
> >>> +++ b/fs/f2fs/file.c
> >>> @@ -3488,6 +3488,7 @@ static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)
> >>>       pgoff_t page_idx = 0, last_idx;
> >>>       unsigned int released_blocks = 0;
> >>>       int ret;
> >>> +     int writecount;
> >>>
> >>>       if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
> >>>               return -EOPNOTSUPP;
> >>
> >> Before inode_lock(), there is one case we may jump to out label, in
> >> this case, we may unlock inode incorrectly.
> >>
> >>         if (!F2FS_I(inode)->i_compr_blocks)
> >>                 goto out;
> >>
> >>> -
> >>> -     inode_unlock(inode);
> >>>  out:
> >>> +     inode_unlock(inode);
> >>> +
> >>>       mnt_drop_write_file(filp);
> >>
> >> Thanks,
> > .
> >


_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

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

* Re: [f2fs-dev] [PATCH] f2fs: remove race condition in releasing cblocks
  2020-05-08  6:58   ` Daeho Jeong
@ 2020-05-08  7:09     ` Chao Yu
  2020-05-08  7:10       ` Daeho Jeong
  0 siblings, 1 reply; 11+ messages in thread
From: Chao Yu @ 2020-05-08  7:09 UTC (permalink / raw)
  To: Daeho Jeong; +Cc: Daeho Jeong, kernel-team, linux-kernel, linux-f2fs-devel

On 2020/5/8 14:58, Daeho Jeong wrote:
> I moved checking i_compr_blocks phrase after calling inode_lock()
> already, because we should check this after writing pages.
> 
> So, if it fails to check i_compr_blocks, we need to call inode_unlock().
> 
> Am I missing something?

After applying this patch, I get this:

	ret = mnt_want_write_file(filp);
	if (ret)
		return ret;

	if (!F2FS_I(inode)->i_compr_blocks)
		goto out;

	f2fs_balance_fs(F2FS_I_SB(inode), true);

	inode_lock(inode);

> 
> 2020년 5월 8일 (금) 오후 3:50, Chao Yu <yuchao0@huawei.com>님이 작성:
>>
>> On 2020/5/8 12:25, Daeho Jeong wrote:
>>> From: Daeho Jeong <daehojeong@google.com>
>>>
>>> Now, if writing pages and releasing compress blocks occur
>>> simultaneously, and releasing cblocks is executed more than one time
>>> to a file, then total block count of filesystem and block count of the
>>> file could be incorrect and damaged.
>>>
>>> We have to execute releasing compress blocks only one time for a file
>>> without being interfered by writepages path.
>>>
>>> Signed-off-by: Daeho Jeong <daehojeong@google.com>
>>> ---
>>>  fs/f2fs/file.c | 31 ++++++++++++++++++++++++-------
>>>  1 file changed, 24 insertions(+), 7 deletions(-)
>>>
>>> diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
>>> index 4aab4b42d8ba..a92bc51b9b28 100644
>>> --- a/fs/f2fs/file.c
>>> +++ b/fs/f2fs/file.c
>>> @@ -3488,6 +3488,7 @@ static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)
>>>       pgoff_t page_idx = 0, last_idx;
>>>       unsigned int released_blocks = 0;
>>>       int ret;
>>> +     int writecount;
>>>
>>>       if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
>>>               return -EOPNOTSUPP;
>>
>> Before inode_lock(), there is one case we may jump to out label, in
>> this case, we may unlock inode incorrectly.
>>
>>         if (!F2FS_I(inode)->i_compr_blocks)
>>                 goto out;
>>
>>> -
>>> -     inode_unlock(inode);
>>>  out:
>>> +     inode_unlock(inode);
>>> +
>>>       mnt_drop_write_file(filp);
>>
>> Thanks,
> .
> 


_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

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

* Re: [f2fs-dev] [PATCH] f2fs: remove race condition in releasing cblocks
  2020-05-08  6:50 ` Chao Yu
@ 2020-05-08  6:58   ` Daeho Jeong
  2020-05-08  7:09     ` Chao Yu
  0 siblings, 1 reply; 11+ messages in thread
From: Daeho Jeong @ 2020-05-08  6:58 UTC (permalink / raw)
  To: Chao Yu; +Cc: Daeho Jeong, kernel-team, linux-kernel, linux-f2fs-devel

I moved checking i_compr_blocks phrase after calling inode_lock()
already, because we should check this after writing pages.

So, if it fails to check i_compr_blocks, we need to call inode_unlock().

Am I missing something?

2020년 5월 8일 (금) 오후 3:50, Chao Yu <yuchao0@huawei.com>님이 작성:
>
> On 2020/5/8 12:25, Daeho Jeong wrote:
> > From: Daeho Jeong <daehojeong@google.com>
> >
> > Now, if writing pages and releasing compress blocks occur
> > simultaneously, and releasing cblocks is executed more than one time
> > to a file, then total block count of filesystem and block count of the
> > file could be incorrect and damaged.
> >
> > We have to execute releasing compress blocks only one time for a file
> > without being interfered by writepages path.
> >
> > Signed-off-by: Daeho Jeong <daehojeong@google.com>
> > ---
> >  fs/f2fs/file.c | 31 ++++++++++++++++++++++++-------
> >  1 file changed, 24 insertions(+), 7 deletions(-)
> >
> > diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
> > index 4aab4b42d8ba..a92bc51b9b28 100644
> > --- a/fs/f2fs/file.c
> > +++ b/fs/f2fs/file.c
> > @@ -3488,6 +3488,7 @@ static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)
> >       pgoff_t page_idx = 0, last_idx;
> >       unsigned int released_blocks = 0;
> >       int ret;
> > +     int writecount;
> >
> >       if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
> >               return -EOPNOTSUPP;
>
> Before inode_lock(), there is one case we may jump to out label, in
> this case, we may unlock inode incorrectly.
>
>         if (!F2FS_I(inode)->i_compr_blocks)
>                 goto out;
>
> > -
> > -     inode_unlock(inode);
> >  out:
> > +     inode_unlock(inode);
> > +
> >       mnt_drop_write_file(filp);
>
> Thanks,


_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

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

* Re: [f2fs-dev] [PATCH] f2fs: remove race condition in releasing cblocks
  2020-05-08  4:25 Daeho Jeong
@ 2020-05-08  6:50 ` Chao Yu
  2020-05-08  6:58   ` Daeho Jeong
  0 siblings, 1 reply; 11+ messages in thread
From: Chao Yu @ 2020-05-08  6:50 UTC (permalink / raw)
  To: Daeho Jeong, linux-kernel, linux-f2fs-devel, kernel-team; +Cc: Daeho Jeong

On 2020/5/8 12:25, Daeho Jeong wrote:
> From: Daeho Jeong <daehojeong@google.com>
> 
> Now, if writing pages and releasing compress blocks occur
> simultaneously, and releasing cblocks is executed more than one time
> to a file, then total block count of filesystem and block count of the
> file could be incorrect and damaged.
> 
> We have to execute releasing compress blocks only one time for a file
> without being interfered by writepages path.
> 
> Signed-off-by: Daeho Jeong <daehojeong@google.com>
> ---
>  fs/f2fs/file.c | 31 ++++++++++++++++++++++++-------
>  1 file changed, 24 insertions(+), 7 deletions(-)
> 
> diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
> index 4aab4b42d8ba..a92bc51b9b28 100644
> --- a/fs/f2fs/file.c
> +++ b/fs/f2fs/file.c
> @@ -3488,6 +3488,7 @@ static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)
>  	pgoff_t page_idx = 0, last_idx;
>  	unsigned int released_blocks = 0;
>  	int ret;
> +	int writecount;
>  
>  	if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
>  		return -EOPNOTSUPP;

Before inode_lock(), there is one case we may jump to out label, in
this case, we may unlock inode incorrectly.

	if (!F2FS_I(inode)->i_compr_blocks)
		goto out;

> -
> -	inode_unlock(inode);
>  out:
> +	inode_unlock(inode);
> +
>  	mnt_drop_write_file(filp);

Thanks,


_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

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

* [f2fs-dev] [PATCH] f2fs: remove race condition in releasing cblocks
@ 2020-05-08  4:25 Daeho Jeong
  2020-05-08  6:50 ` Chao Yu
  0 siblings, 1 reply; 11+ messages in thread
From: Daeho Jeong @ 2020-05-08  4:25 UTC (permalink / raw)
  To: linux-kernel, linux-f2fs-devel, kernel-team; +Cc: Daeho Jeong

From: Daeho Jeong <daehojeong@google.com>

Now, if writing pages and releasing compress blocks occur
simultaneously, and releasing cblocks is executed more than one time
to a file, then total block count of filesystem and block count of the
file could be incorrect and damaged.

We have to execute releasing compress blocks only one time for a file
without being interfered by writepages path.

Signed-off-by: Daeho Jeong <daehojeong@google.com>
---
 fs/f2fs/file.c | 31 ++++++++++++++++++++++++-------
 1 file changed, 24 insertions(+), 7 deletions(-)

diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
index 4aab4b42d8ba..a92bc51b9b28 100644
--- a/fs/f2fs/file.c
+++ b/fs/f2fs/file.c
@@ -3488,6 +3488,7 @@ static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)
 	pgoff_t page_idx = 0, last_idx;
 	unsigned int released_blocks = 0;
 	int ret;
+	int writecount;
 
 	if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
 		return -EOPNOTSUPP;
@@ -3509,13 +3510,29 @@ static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)
 
 	inode_lock(inode);
 
-	if (!IS_IMMUTABLE(inode)) {
-		F2FS_I(inode)->i_flags |= F2FS_IMMUTABLE_FL;
-		f2fs_set_inode_flags(inode);
-		inode->i_ctime = current_time(inode);
-		f2fs_mark_inode_dirty_sync(inode, true);
+	writecount = atomic_read(&inode->i_writecount);
+	if ((filp->f_mode & FMODE_WRITE && writecount != 1) || writecount) {
+		ret = -EBUSY;
+		goto out;
 	}
 
+	if (IS_IMMUTABLE(inode)) {
+		ret = -EINVAL;
+		goto out;
+	}
+
+	ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
+	if (ret)
+		goto out;
+
+	if (!F2FS_I(inode)->i_compr_blocks)
+		goto out;
+
+	F2FS_I(inode)->i_flags |= F2FS_IMMUTABLE_FL;
+	f2fs_set_inode_flags(inode);
+	inode->i_ctime = current_time(inode);
+	f2fs_mark_inode_dirty_sync(inode, true);
+
 	down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
 	down_write(&F2FS_I(inode)->i_mmap_sem);
 
@@ -3554,9 +3571,9 @@ static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)
 
 	up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
 	up_write(&F2FS_I(inode)->i_mmap_sem);
-
-	inode_unlock(inode);
 out:
+	inode_unlock(inode);
+
 	mnt_drop_write_file(filp);
 
 	if (ret >= 0) {
-- 
2.26.2.526.g744177e7f7-goog



_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

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

end of thread, back to index

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-08  9:29 [f2fs-dev] [PATCH] f2fs: remove race condition in releasing cblocks Daeho Jeong
2020-05-08 10:09 ` Chao Yu
2020-05-08 13:47 ` Jaegeuk Kim
  -- strict thread matches above, loose matches on Subject: below --
2020-05-08 11:56 Daeho Jeong
2020-05-08 13:48 ` Jaegeuk Kim
2020-05-08 13:58   ` Jaegeuk Kim
2020-05-08  4:25 Daeho Jeong
2020-05-08  6:50 ` Chao Yu
2020-05-08  6:58   ` Daeho Jeong
2020-05-08  7:09     ` Chao Yu
2020-05-08  7:10       ` Daeho Jeong

Linux-f2fs-devel Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-f2fs-devel/0 linux-f2fs-devel/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-f2fs-devel linux-f2fs-devel/ https://lore.kernel.org/linux-f2fs-devel \
		linux-f2fs-devel@lists.sourceforge.net
	public-inbox-index linux-f2fs-devel

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/net.sourceforge.lists.linux-f2fs-devel


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git