linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [f2fs-dev] [PATCH 1/2] f2fs: avoid non-section-aligned size pinned file generation
@ 2025-06-17  3:57 wangzijie
  2025-06-17  3:57 ` [f2fs-dev] [PATCH 2/2] f2fs: cleanup F2FS_I_SB in f2fs_setattr() wangzijie
  2025-06-17  5:22 ` [f2fs-dev] [PATCH 1/2] f2fs: avoid non-section-aligned size pinned file generation Chao Yu
  0 siblings, 2 replies; 6+ messages in thread
From: wangzijie @ 2025-06-17  3:57 UTC (permalink / raw)
  To: jaegeuk, chao
  Cc: linux-f2fs-devel, linux-kernel, bintian.wang, feng.han,
	niuzhiguo84, wangzijie

To prevent non-section-aligned size pinned file generated from truncation,
add check condition in setattr.

Signed-off-by: wangzijie <wangzijie1@honor.com>
---
 fs/f2fs/file.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
index 6bd3de64f..72f7d1b4a 100644
--- a/fs/f2fs/file.c
+++ b/fs/f2fs/file.c
@@ -1026,6 +1026,7 @@ int f2fs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
 {
 	struct inode *inode = d_inode(dentry);
 	struct f2fs_inode_info *fi = F2FS_I(inode);
+	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
 	int err;
 
 	if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
@@ -1047,6 +1048,11 @@ int f2fs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
 			!IS_ALIGNED(attr->ia_size,
 			F2FS_BLK_TO_BYTES(fi->i_cluster_size)))
 			return -EINVAL;
+		if (f2fs_is_pinned_file(inode) &&
+			attr->ia_size < i_size_read(inode) &&
+			!IS_ALIGNED(attr->ia_size,
+			F2FS_BLK_TO_BYTES(CAP_BLKS_PER_SEC(sbi))))
+			return -EINVAL;
 	}
 
 	err = setattr_prepare(idmap, dentry, attr);
-- 
2.25.1


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

* [f2fs-dev] [PATCH 2/2] f2fs: cleanup F2FS_I_SB in f2fs_setattr()
  2025-06-17  3:57 [f2fs-dev] [PATCH 1/2] f2fs: avoid non-section-aligned size pinned file generation wangzijie
@ 2025-06-17  3:57 ` wangzijie
  2025-06-17  5:22 ` [f2fs-dev] [PATCH 1/2] f2fs: avoid non-section-aligned size pinned file generation Chao Yu
  1 sibling, 0 replies; 6+ messages in thread
From: wangzijie @ 2025-06-17  3:57 UTC (permalink / raw)
  To: jaegeuk, chao
  Cc: linux-f2fs-devel, linux-kernel, bintian.wang, feng.han,
	niuzhiguo84, wangzijie

After introduce sbi in f2fs_setattr(), cleanup F2FS_I_SB. No logic change.

Signed-off-by: wangzijie <wangzijie1@honor.com>
---
 fs/f2fs/file.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)

diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
index 72f7d1b4a..4ca1c2dfc 100644
--- a/fs/f2fs/file.c
+++ b/fs/f2fs/file.c
@@ -1029,7 +1029,7 @@ int f2fs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
 	int err;
 
-	if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
+	if (unlikely(f2fs_cp_error(sbi)))
 		return -EIO;
 
 	if (unlikely(IS_IMMUTABLE(inode)))
@@ -1074,12 +1074,11 @@ int f2fs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
 	}
 	if (i_uid_needs_update(idmap, attr, inode) ||
 	    i_gid_needs_update(idmap, attr, inode)) {
-		f2fs_lock_op(F2FS_I_SB(inode));
+		f2fs_lock_op(sbi);
 		err = dquot_transfer(idmap, inode, attr);
 		if (err) {
-			set_sbi_flag(F2FS_I_SB(inode),
-					SBI_QUOTA_NEED_REPAIR);
-			f2fs_unlock_op(F2FS_I_SB(inode));
+			set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
+			f2fs_unlock_op(sbi);
 			return err;
 		}
 		/*
@@ -1089,7 +1088,7 @@ int f2fs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
 		i_uid_update(idmap, attr, inode);
 		i_gid_update(idmap, attr, inode);
 		f2fs_mark_inode_dirty_sync(inode, true);
-		f2fs_unlock_op(F2FS_I_SB(inode));
+		f2fs_unlock_op(sbi);
 	}
 
 	if (attr->ia_valid & ATTR_SIZE) {
@@ -1150,7 +1149,7 @@ int f2fs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
 	f2fs_mark_inode_dirty_sync(inode, true);
 
 	/* inode change will produce dirty node pages flushed by checkpoint */
-	f2fs_balance_fs(F2FS_I_SB(inode), true);
+	f2fs_balance_fs(sbi, true);
 
 	return err;
 }
-- 
2.25.1


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

* Re: [f2fs-dev] [PATCH 1/2] f2fs: avoid non-section-aligned size pinned file generation
  2025-06-17  3:57 [f2fs-dev] [PATCH 1/2] f2fs: avoid non-section-aligned size pinned file generation wangzijie
  2025-06-17  3:57 ` [f2fs-dev] [PATCH 2/2] f2fs: cleanup F2FS_I_SB in f2fs_setattr() wangzijie
@ 2025-06-17  5:22 ` Chao Yu
  2025-06-17  7:36   ` wangzijie
  1 sibling, 1 reply; 6+ messages in thread
From: Chao Yu @ 2025-06-17  5:22 UTC (permalink / raw)
  To: wangzijie, jaegeuk
  Cc: chao, linux-f2fs-devel, linux-kernel, bintian.wang, feng.han,
	niuzhiguo84

On 6/17/25 11:57, wangzijie wrote:
> To prevent non-section-aligned size pinned file generated from truncation,
> add check condition in setattr.
> 
> Signed-off-by: wangzijie <wangzijie1@honor.com>
> ---
>  fs/f2fs/file.c | 6 ++++++
>  1 file changed, 6 insertions(+)
> 
> diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
> index 6bd3de64f..72f7d1b4a 100644
> --- a/fs/f2fs/file.c
> +++ b/fs/f2fs/file.c
> @@ -1026,6 +1026,7 @@ int f2fs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
>  {
>  	struct inode *inode = d_inode(dentry);
>  	struct f2fs_inode_info *fi = F2FS_I(inode);
> +	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
>  	int err;
>  
>  	if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
> @@ -1047,6 +1048,11 @@ int f2fs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
>  			!IS_ALIGNED(attr->ia_size,
>  			F2FS_BLK_TO_BYTES(fi->i_cluster_size)))
>  			return -EINVAL;
> +		if (f2fs_is_pinned_file(inode) &&
> +			attr->ia_size < i_size_read(inode) &&

Do we need to consider attr->ia_size > i_size case?

Thanks,

> +			!IS_ALIGNED(attr->ia_size,
> +			F2FS_BLK_TO_BYTES(CAP_BLKS_PER_SEC(sbi))))
> +			return -EINVAL;
>  	}
>  
>  	err = setattr_prepare(idmap, dentry, attr);


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

* Re: [f2fs-dev] [PATCH 1/2] f2fs: avoid non-section-aligned size pinned file generation
  2025-06-17  5:22 ` [f2fs-dev] [PATCH 1/2] f2fs: avoid non-section-aligned size pinned file generation Chao Yu
@ 2025-06-17  7:36   ` wangzijie
  2025-06-17  9:30     ` Chao Yu
  0 siblings, 1 reply; 6+ messages in thread
From: wangzijie @ 2025-06-17  7:36 UTC (permalink / raw)
  To: linux-f2fs-devel; +Cc: chao, feng.han, jaegeuk, linux-kernel, wangzijie1

> On 6/17/25 11:57, wangzijie wrote:
> > To prevent non-section-aligned size pinned file generated from truncation,
> > add check condition in setattr.
> > 
> > Signed-off-by: wangzijie <wangzijie1@honor.com>
> > ---
> >  fs/f2fs/file.c | 6 ++++++
> >  1 file changed, 6 insertions(+)
> > 
> > diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
> > index 6bd3de64f..72f7d1b4a 100644
> > --- a/fs/f2fs/file.c
> > +++ b/fs/f2fs/file.c
> > @@ -1026,6 +1026,7 @@ int f2fs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
> >  {
> >  	struct inode *inode = d_inode(dentry);
> >  	struct f2fs_inode_info *fi = F2FS_I(inode);
> > +	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
> >  	int err;
> >  
> >  	if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
> > @@ -1047,6 +1048,11 @@ int f2fs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
> >  			!IS_ALIGNED(attr->ia_size,
> >  			F2FS_BLK_TO_BYTES(fi->i_cluster_size)))
> >  			return -EINVAL;
> > +		if (f2fs_is_pinned_file(inode) &&
> > +			attr->ia_size < i_size_read(inode) &&
> 
> Do we need to consider attr->ia_size > i_size case?
> 
> Thanks,

Hi, Chao
After commit 3fdd89b452c2("f2fs: prevent writing without fallocate() for pinned
files"), when we want to write data to pinned file, we need to use pin+fallocate, 
and we did CAP_BLKS_PER_SEC roundup align when fallocate pinned file:

block_t sec_len = roundup(map.m_len, sec_blks);

Even if we truncate the file to a larger size(maybe larger than sec_len, section
align or not), and write data to offset beyond sec_len, the write will fail(commit
3fdd89b452c2 prevent it). The scattered pin block cannot be generated by this way,
so I did not consider attr->ia_size > i_size case.
Do you have some suggestions?

> > +			!IS_ALIGNED(attr->ia_size,
> > +			F2FS_BLK_TO_BYTES(CAP_BLKS_PER_SEC(sbi))))
> > +			return -EINVAL;
> >  	}
> >  
> >  	err = setattr_prepare(idmap, dentry, attr);




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

* Re: [f2fs-dev] [PATCH 1/2] f2fs: avoid non-section-aligned size pinned file generation
  2025-06-17  7:36   ` wangzijie
@ 2025-06-17  9:30     ` Chao Yu
  2025-06-17 10:05       ` wangzijie
  0 siblings, 1 reply; 6+ messages in thread
From: Chao Yu @ 2025-06-17  9:30 UTC (permalink / raw)
  To: wangzijie, linux-f2fs-devel; +Cc: chao, feng.han, jaegeuk, linux-kernel

On 6/17/25 15:36, wangzijie wrote:
>> On 6/17/25 11:57, wangzijie wrote:
>>> To prevent non-section-aligned size pinned file generated from truncation,
>>> add check condition in setattr.
>>>
>>> Signed-off-by: wangzijie <wangzijie1@honor.com>
>>> ---
>>>  fs/f2fs/file.c | 6 ++++++
>>>  1 file changed, 6 insertions(+)
>>>
>>> diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
>>> index 6bd3de64f..72f7d1b4a 100644
>>> --- a/fs/f2fs/file.c
>>> +++ b/fs/f2fs/file.c
>>> @@ -1026,6 +1026,7 @@ int f2fs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
>>>  {
>>>  	struct inode *inode = d_inode(dentry);
>>>  	struct f2fs_inode_info *fi = F2FS_I(inode);
>>> +	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
>>>  	int err;
>>>  
>>>  	if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
>>> @@ -1047,6 +1048,11 @@ int f2fs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
>>>  			!IS_ALIGNED(attr->ia_size,
>>>  			F2FS_BLK_TO_BYTES(fi->i_cluster_size)))
>>>  			return -EINVAL;
>>> +		if (f2fs_is_pinned_file(inode) &&
>>> +			attr->ia_size < i_size_read(inode) &&
>>
>> Do we need to consider attr->ia_size > i_size case?
>>
>> Thanks,
> 
> Hi, Chao
> After commit 3fdd89b452c2("f2fs: prevent writing without fallocate() for pinned
> files"), when we want to write data to pinned file, we need to use pin+fallocate, 
> and we did CAP_BLKS_PER_SEC roundup align when fallocate pinned file:
> 
> block_t sec_len = roundup(map.m_len, sec_blks);
> 
> Even if we truncate the file to a larger size(maybe larger than sec_len, section
> align or not), and write data to offset beyond sec_len, the write will fail(commit
> 3fdd89b452c2 prevent it). The scattered pin block cannot be generated by this way,
> so I did not consider attr->ia_size > i_size case.
> Do you have some suggestions?

Ah, correct, so what about adding comments here to describe why we don't
need consider attr->ia_size > i_size case?

> 
>>> +			!IS_ALIGNED(attr->ia_size,
>>> +			F2FS_BLK_TO_BYTES(CAP_BLKS_PER_SEC(sbi))))
>>> +			return -EINVAL;
>>>  	}
>>>  
>>>  	err = setattr_prepare(idmap, dentry, attr);
> 
> 
> 


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

* Re: [f2fs-dev] [PATCH 1/2] f2fs: avoid non-section-aligned size pinned file generation
  2025-06-17  9:30     ` Chao Yu
@ 2025-06-17 10:05       ` wangzijie
  0 siblings, 0 replies; 6+ messages in thread
From: wangzijie @ 2025-06-17 10:05 UTC (permalink / raw)
  To: linux-f2fs-devel; +Cc: chao, feng.han, jaegeuk, linux-kernel, wangzijie1

>On 6/17/25 15:36, wangzijie wrote:
>>> On 6/17/25 11:57, wangzijie wrote:
>>>> To prevent non-section-aligned size pinned file generated from truncation,
>>>> add check condition in setattr.
>>>>
>>>> Signed-off-by: wangzijie <wangzijie1@honor.com>
>>>> ---
>>>>  fs/f2fs/file.c | 6 ++++++
>>>>  1 file changed, 6 insertions(+)
>>>>
>>>> diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
>>>> index 6bd3de64f..72f7d1b4a 100644
>>>> --- a/fs/f2fs/file.c
>>>> +++ b/fs/f2fs/file.c
>>>> @@ -1026,6 +1026,7 @@ int f2fs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
>>>>  {
>>>>  	struct inode *inode = d_inode(dentry);
>>>>  	struct f2fs_inode_info *fi = F2FS_I(inode);
>>>> +	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
>>>>  	int err;
>>>>  
>>>>  	if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
>>>> @@ -1047,6 +1048,11 @@ int f2fs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
>>>>  			!IS_ALIGNED(attr->ia_size,
>>>>  			F2FS_BLK_TO_BYTES(fi->i_cluster_size)))
>>>>  			return -EINVAL;
>>>> +		if (f2fs_is_pinned_file(inode) &&
>>>> +			attr->ia_size < i_size_read(inode) &&
>>>
>>> Do we need to consider attr->ia_size > i_size case?
>>>
>>> Thanks,
>> 
>> Hi, Chao
>> After commit 3fdd89b452c2("f2fs: prevent writing without fallocate() for pinned
>> files"), when we want to write data to pinned file, we need to use pin+fallocate, 
>> and we did CAP_BLKS_PER_SEC roundup align when fallocate pinned file:
>> 
>> block_t sec_len = roundup(map.m_len, sec_blks);
>> 
>> Even if we truncate the file to a larger size(maybe larger than sec_len, section
>> align or not), and write data to offset beyond sec_len, the write will fail(commit
>> 3fdd89b452c2 prevent it). The scattered pin block cannot be generated by this way,
>> so I did not consider attr->ia_size > i_size case.
>> Do you have some suggestions?
>
>Ah, correct, so what about adding comments here to describe why we don't
>need consider attr->ia_size > i_size case?

OK, I will add comments in next version.

>> 
>>>> +			!IS_ALIGNED(attr->ia_size,
>>>> +			F2FS_BLK_TO_BYTES(CAP_BLKS_PER_SEC(sbi))))
>>>> +			return -EINVAL;
>>>>  	}
>>>>  
>>>>  	err = setattr_prepare(idmap, dentry, attr);
>> 
>> 
>> 




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

end of thread, other threads:[~2025-06-17 10:05 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-17  3:57 [f2fs-dev] [PATCH 1/2] f2fs: avoid non-section-aligned size pinned file generation wangzijie
2025-06-17  3:57 ` [f2fs-dev] [PATCH 2/2] f2fs: cleanup F2FS_I_SB in f2fs_setattr() wangzijie
2025-06-17  5:22 ` [f2fs-dev] [PATCH 1/2] f2fs: avoid non-section-aligned size pinned file generation Chao Yu
2025-06-17  7:36   ` wangzijie
2025-06-17  9:30     ` Chao Yu
2025-06-17 10:05       ` wangzijie

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).