public inbox for linux-xfs@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] xfs: Check for deallayed allocations before setting extsize
@ 2024-10-10  6:36 Ojaswin Mujoo
  2024-10-10  6:50 ` Christoph Hellwig
  2024-10-10 18:34 ` Darrick J. Wong
  0 siblings, 2 replies; 5+ messages in thread
From: Ojaswin Mujoo @ 2024-10-10  6:36 UTC (permalink / raw)
  To: linux-xfs
  Cc: Ritesh Harjani, linux-kernel, Darrick J . Wong, dchinner,
	Chandan Babu R, Christoph Hellwig

Extsize is allowed to be set on files with no data in it. For this,
we were checking if the files have extents but missed to check if
delayed extents were present. This patch adds that check.

While we are at it, also refactor this check into a helper since
its used in some other places as well like xfs_inactive() or
xfs_ioctl_setattr_xflags()

**Without the patch (SUCCEEDS)**

$ xfs_io -c 'open -f testfile' -c 'pwrite 0 1024' -c 'extsize 65536'

wrote 1024/1024 bytes at offset 0
1 KiB, 1 ops; 0.0002 sec (4.628 MiB/sec and 4739.3365 ops/sec)

**With the patch (FAILS as expected)**

$ xfs_io -c 'open -f testfile' -c 'pwrite 0 1024' -c 'extsize 65536'

wrote 1024/1024 bytes at offset 0
1 KiB, 1 ops; 0.0002 sec (4.628 MiB/sec and 4739.3365 ops/sec)
xfs_io: FS_IOC_FSSETXATTR testfile: Invalid argument

Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Ojaswin Mujoo <ojaswin@linux.ibm.com>
---

* Changes since v1 *

 - RVB by Christoph
 - Added a helper to check if inode has data instead of
   open coding.
	
v1:
https://lore.kernel.org/linux-xfs/Zv_cTc6cgxszKGy3@infradead.org/T/#mf949dafb2b2f63bea1f7c0ce5265a2527aaf22a9

 fs/xfs/xfs_inode.c | 2 +-
 fs/xfs/xfs_inode.h | 5 +++++
 fs/xfs/xfs_ioctl.c | 5 +++--
 3 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/fs/xfs/xfs_inode.c b/fs/xfs/xfs_inode.c
index bcc277fc0a83..3d083a8fd8ed 100644
--- a/fs/xfs/xfs_inode.c
+++ b/fs/xfs/xfs_inode.c
@@ -1409,7 +1409,7 @@ xfs_inactive(
 
 	if (S_ISREG(VFS_I(ip)->i_mode) &&
 	    (ip->i_disk_size != 0 || XFS_ISIZE(ip) != 0 ||
-	     ip->i_df.if_nextents > 0 || ip->i_delayed_blks > 0))
+	     xfs_inode_has_data(ip)))
 		truncate = 1;
 
 	if (xfs_iflags_test(ip, XFS_IQUOTAUNCHECKED)) {
diff --git a/fs/xfs/xfs_inode.h b/fs/xfs/xfs_inode.h
index 97ed912306fd..ae1ccf2a3c8b 100644
--- a/fs/xfs/xfs_inode.h
+++ b/fs/xfs/xfs_inode.h
@@ -292,6 +292,11 @@ static inline bool xfs_is_cow_inode(struct xfs_inode *ip)
 	return xfs_is_reflink_inode(ip) || xfs_is_always_cow_inode(ip);
 }
 
+static inline bool xfs_inode_has_data(struct xfs_inode *ip)
+{
+	return (ip->i_df.if_nextents > 0 || ip->i_delayed_blks > 0);
+}
+
 /*
  * Check if an inode has any data in the COW fork.  This might be often false
  * even for inodes with the reflink flag when there is no pending COW operation.
diff --git a/fs/xfs/xfs_ioctl.c b/fs/xfs/xfs_ioctl.c
index a20d426ef021..88b9c8cf0272 100644
--- a/fs/xfs/xfs_ioctl.c
+++ b/fs/xfs/xfs_ioctl.c
@@ -481,7 +481,7 @@ xfs_ioctl_setattr_xflags(
 
 	if (rtflag != XFS_IS_REALTIME_INODE(ip)) {
 		/* Can't change realtime flag if any extents are allocated. */
-		if (ip->i_df.if_nextents || ip->i_delayed_blks)
+		if (xfs_inode_has_data(ip))
 			return -EINVAL;
 
 		/*
@@ -602,7 +602,8 @@ xfs_ioctl_setattr_check_extsize(
 	if (!fa->fsx_valid)
 		return 0;
 
-	if (S_ISREG(VFS_I(ip)->i_mode) && ip->i_df.if_nextents &&
+	if (S_ISREG(VFS_I(ip)->i_mode) &&
+	    xfs_inode_has_data(ip) &&
 	    XFS_FSB_TO_B(mp, ip->i_extsize) != fa->fsx_extsize)
 		return -EINVAL;
 
-- 
2.43.5


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

* Re: [PATCH v2] xfs: Check for deallayed allocations before setting extsize
  2024-10-10  6:36 [PATCH v2] xfs: Check for deallayed allocations before setting extsize Ojaswin Mujoo
@ 2024-10-10  6:50 ` Christoph Hellwig
  2024-10-10  8:56   ` Ojaswin Mujoo
  2024-10-10 18:34 ` Darrick J. Wong
  1 sibling, 1 reply; 5+ messages in thread
From: Christoph Hellwig @ 2024-10-10  6:50 UTC (permalink / raw)
  To: Ojaswin Mujoo
  Cc: linux-xfs, Ritesh Harjani, linux-kernel, Darrick J . Wong,
	dchinner, Chandan Babu R, Christoph Hellwig

s/deallayed/delayed/

>  
> +static inline bool xfs_inode_has_data(struct xfs_inode *ip)
> +{
> +	return (ip->i_df.if_nextents > 0 || ip->i_delayed_blks > 0);

Nit: no need for the braces.

> +	if (S_ISREG(VFS_I(ip)->i_mode) &&
> +	    xfs_inode_has_data(ip) &&

This can now be condensed to:

	if (S_ISREG(VFS_I(ip)->i_mode) && xfs_inode_has_data(ip) &&

Otherwise this still looks good to me.


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

* Re: [PATCH v2] xfs: Check for deallayed allocations before setting extsize
  2024-10-10  6:50 ` Christoph Hellwig
@ 2024-10-10  8:56   ` Ojaswin Mujoo
  0 siblings, 0 replies; 5+ messages in thread
From: Ojaswin Mujoo @ 2024-10-10  8:56 UTC (permalink / raw)
  To: Christoph Hellwig
  Cc: linux-xfs, Ritesh Harjani, linux-kernel, Darrick J . Wong,
	dchinner, Chandan Babu R

On Thu, Oct 10, 2024 at 08:50:21AM +0200, Christoph Hellwig wrote:
> s/deallayed/delayed/

Hi Christoph, sorry I missed this, will fix it.
> 
> >  
> > +static inline bool xfs_inode_has_data(struct xfs_inode *ip)
> > +{
> > +	return (ip->i_df.if_nextents > 0 || ip->i_delayed_blks > 0);
> 
> Nit: no need for the braces.
> 
> > +	if (S_ISREG(VFS_I(ip)->i_mode) &&
> > +	    xfs_inode_has_data(ip) &&
> 
> This can now be condensed to:
> 
> 	if (S_ISREG(VFS_I(ip)->i_mode) && xfs_inode_has_data(ip) &&
> 
> Otherwise this still looks good to me.
> 

Got it, I'll make the changes and send v3 by end of day.

Regards,
ojaswin

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

* Re: [PATCH v2] xfs: Check for deallayed allocations before setting extsize
  2024-10-10  6:36 [PATCH v2] xfs: Check for deallayed allocations before setting extsize Ojaswin Mujoo
  2024-10-10  6:50 ` Christoph Hellwig
@ 2024-10-10 18:34 ` Darrick J. Wong
  2024-10-11  8:51   ` Ojaswin Mujoo
  1 sibling, 1 reply; 5+ messages in thread
From: Darrick J. Wong @ 2024-10-10 18:34 UTC (permalink / raw)
  To: Ojaswin Mujoo
  Cc: linux-xfs, Ritesh Harjani, linux-kernel, dchinner, Chandan Babu R,
	Christoph Hellwig

On Thu, Oct 10, 2024 at 12:06:17PM +0530, Ojaswin Mujoo wrote:
> Extsize is allowed to be set on files with no data in it. For this,
> we were checking if the files have extents but missed to check if
> delayed extents were present. This patch adds that check.
> 
> While we are at it, also refactor this check into a helper since
> its used in some other places as well like xfs_inactive() or
> xfs_ioctl_setattr_xflags()
> 
> **Without the patch (SUCCEEDS)**
> 
> $ xfs_io -c 'open -f testfile' -c 'pwrite 0 1024' -c 'extsize 65536'
> 
> wrote 1024/1024 bytes at offset 0
> 1 KiB, 1 ops; 0.0002 sec (4.628 MiB/sec and 4739.3365 ops/sec)
> 
> **With the patch (FAILS as expected)**
> 
> $ xfs_io -c 'open -f testfile' -c 'pwrite 0 1024' -c 'extsize 65536'
> 
> wrote 1024/1024 bytes at offset 0
> 1 KiB, 1 ops; 0.0002 sec (4.628 MiB/sec and 4739.3365 ops/sec)
> xfs_io: FS_IOC_FSSETXATTR testfile: Invalid argument
> 
> Reviewed-by: Christoph Hellwig <hch@lst.de>
> Signed-off-by: Ojaswin Mujoo <ojaswin@linux.ibm.com>
> ---
> 
> * Changes since v1 *
> 
>  - RVB by Christoph
>  - Added a helper to check if inode has data instead of
>    open coding.
> 	
> v1:
> https://lore.kernel.org/linux-xfs/Zv_cTc6cgxszKGy3@infradead.org/T/#mf949dafb2b2f63bea1f7c0ce5265a2527aaf22a9
> 
>  fs/xfs/xfs_inode.c | 2 +-
>  fs/xfs/xfs_inode.h | 5 +++++
>  fs/xfs/xfs_ioctl.c | 5 +++--
>  3 files changed, 9 insertions(+), 3 deletions(-)
> 
> diff --git a/fs/xfs/xfs_inode.c b/fs/xfs/xfs_inode.c
> index bcc277fc0a83..3d083a8fd8ed 100644
> --- a/fs/xfs/xfs_inode.c
> +++ b/fs/xfs/xfs_inode.c
> @@ -1409,7 +1409,7 @@ xfs_inactive(
>  
>  	if (S_ISREG(VFS_I(ip)->i_mode) &&
>  	    (ip->i_disk_size != 0 || XFS_ISIZE(ip) != 0 ||
> -	     ip->i_df.if_nextents > 0 || ip->i_delayed_blks > 0))
> +	     xfs_inode_has_data(ip)))
>  		truncate = 1;
>  
>  	if (xfs_iflags_test(ip, XFS_IQUOTAUNCHECKED)) {
> diff --git a/fs/xfs/xfs_inode.h b/fs/xfs/xfs_inode.h
> index 97ed912306fd..ae1ccf2a3c8b 100644
> --- a/fs/xfs/xfs_inode.h
> +++ b/fs/xfs/xfs_inode.h
> @@ -292,6 +292,11 @@ static inline bool xfs_is_cow_inode(struct xfs_inode *ip)
>  	return xfs_is_reflink_inode(ip) || xfs_is_always_cow_inode(ip);
>  }
>  
> +static inline bool xfs_inode_has_data(struct xfs_inode *ip)

Can you please change this to "const struct xfs_inode *ip"?
This predicate function doesn't change @ip.

I might've called it xfs_inode_has_filedata fwiw, but the current name
is fine with me.

--D

> +{
> +	return (ip->i_df.if_nextents > 0 || ip->i_delayed_blks > 0);
> +}
> +
>  /*
>   * Check if an inode has any data in the COW fork.  This might be often false
>   * even for inodes with the reflink flag when there is no pending COW operation.
> diff --git a/fs/xfs/xfs_ioctl.c b/fs/xfs/xfs_ioctl.c
> index a20d426ef021..88b9c8cf0272 100644
> --- a/fs/xfs/xfs_ioctl.c
> +++ b/fs/xfs/xfs_ioctl.c
> @@ -481,7 +481,7 @@ xfs_ioctl_setattr_xflags(
>  
>  	if (rtflag != XFS_IS_REALTIME_INODE(ip)) {
>  		/* Can't change realtime flag if any extents are allocated. */
> -		if (ip->i_df.if_nextents || ip->i_delayed_blks)
> +		if (xfs_inode_has_data(ip))
>  			return -EINVAL;
>  
>  		/*
> @@ -602,7 +602,8 @@ xfs_ioctl_setattr_check_extsize(
>  	if (!fa->fsx_valid)
>  		return 0;
>  
> -	if (S_ISREG(VFS_I(ip)->i_mode) && ip->i_df.if_nextents &&
> +	if (S_ISREG(VFS_I(ip)->i_mode) &&
> +	    xfs_inode_has_data(ip) &&
>  	    XFS_FSB_TO_B(mp, ip->i_extsize) != fa->fsx_extsize)
>  		return -EINVAL;
>  
> -- 
> 2.43.5
> 

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

* Re: [PATCH v2] xfs: Check for deallayed allocations before setting extsize
  2024-10-10 18:34 ` Darrick J. Wong
@ 2024-10-11  8:51   ` Ojaswin Mujoo
  0 siblings, 0 replies; 5+ messages in thread
From: Ojaswin Mujoo @ 2024-10-11  8:51 UTC (permalink / raw)
  To: Darrick J. Wong
  Cc: linux-xfs, Ritesh Harjani, linux-kernel, dchinner, Chandan Babu R,
	Christoph Hellwig

On Thu, Oct 10, 2024 at 11:34:47AM -0700, Darrick J. Wong wrote:
> On Thu, Oct 10, 2024 at 12:06:17PM +0530, Ojaswin Mujoo wrote:
> > Extsize is allowed to be set on files with no data in it. For this,
> > we were checking if the files have extents but missed to check if
> > delayed extents were present. This patch adds that check.
> > 
> > While we are at it, also refactor this check into a helper since
> > its used in some other places as well like xfs_inactive() or
> > xfs_ioctl_setattr_xflags()
> > 
> > **Without the patch (SUCCEEDS)**
> > 
> > $ xfs_io -c 'open -f testfile' -c 'pwrite 0 1024' -c 'extsize 65536'
> > 
> > wrote 1024/1024 bytes at offset 0
> > 1 KiB, 1 ops; 0.0002 sec (4.628 MiB/sec and 4739.3365 ops/sec)
> > 
> > **With the patch (FAILS as expected)**
> > 
> > $ xfs_io -c 'open -f testfile' -c 'pwrite 0 1024' -c 'extsize 65536'
> > 
> > wrote 1024/1024 bytes at offset 0
> > 1 KiB, 1 ops; 0.0002 sec (4.628 MiB/sec and 4739.3365 ops/sec)
> > xfs_io: FS_IOC_FSSETXATTR testfile: Invalid argument
> > 
> > Reviewed-by: Christoph Hellwig <hch@lst.de>
> > Signed-off-by: Ojaswin Mujoo <ojaswin@linux.ibm.com>
> > ---
> > 
> > * Changes since v1 *
> > 
> >  - RVB by Christoph
> >  - Added a helper to check if inode has data instead of
> >    open coding.
> > 	
> > v1:
> > https://lore.kernel.org/linux-xfs/Zv_cTc6cgxszKGy3@infradead.org/T/#mf949dafb2b2f63bea1f7c0ce5265a2527aaf22a9
> > 
> >  fs/xfs/xfs_inode.c | 2 +-
> >  fs/xfs/xfs_inode.h | 5 +++++
> >  fs/xfs/xfs_ioctl.c | 5 +++--
> >  3 files changed, 9 insertions(+), 3 deletions(-)
> > 
> > diff --git a/fs/xfs/xfs_inode.c b/fs/xfs/xfs_inode.c
> > index bcc277fc0a83..3d083a8fd8ed 100644
> > --- a/fs/xfs/xfs_inode.c
> > +++ b/fs/xfs/xfs_inode.c
> > @@ -1409,7 +1409,7 @@ xfs_inactive(
> >  
> >  	if (S_ISREG(VFS_I(ip)->i_mode) &&
> >  	    (ip->i_disk_size != 0 || XFS_ISIZE(ip) != 0 ||
> > -	     ip->i_df.if_nextents > 0 || ip->i_delayed_blks > 0))
> > +	     xfs_inode_has_data(ip)))
> >  		truncate = 1;
> >  
> >  	if (xfs_iflags_test(ip, XFS_IQUOTAUNCHECKED)) {
> > diff --git a/fs/xfs/xfs_inode.h b/fs/xfs/xfs_inode.h
> > index 97ed912306fd..ae1ccf2a3c8b 100644
> > --- a/fs/xfs/xfs_inode.h
> > +++ b/fs/xfs/xfs_inode.h
> > @@ -292,6 +292,11 @@ static inline bool xfs_is_cow_inode(struct xfs_inode *ip)
> >  	return xfs_is_reflink_inode(ip) || xfs_is_always_cow_inode(ip);
> >  }
> >  
> > +static inline bool xfs_inode_has_data(struct xfs_inode *ip)
> 
> Can you please change this to "const struct xfs_inode *ip"?
> This predicate function doesn't change @ip.

> 
> I might've called it xfs_inode_has_filedata fwiw, but the current name
> is fine with me.

Hey Darrick, sure I'll make it const and change data -> filedata.

Thanks for the review,
ojaswin

> 
> --D
> 
> > +{
> > +	return (ip->i_df.if_nextents > 0 || ip->i_delayed_blks > 0);
> > +}
> > +
> >  /*
> >   * Check if an inode has any data in the COW fork.  This might be often false
> >   * even for inodes with the reflink flag when there is no pending COW operation.
> > diff --git a/fs/xfs/xfs_ioctl.c b/fs/xfs/xfs_ioctl.c
> > index a20d426ef021..88b9c8cf0272 100644
> > --- a/fs/xfs/xfs_ioctl.c
> > +++ b/fs/xfs/xfs_ioctl.c
> > @@ -481,7 +481,7 @@ xfs_ioctl_setattr_xflags(
> >  
> >  	if (rtflag != XFS_IS_REALTIME_INODE(ip)) {
> >  		/* Can't change realtime flag if any extents are allocated. */
> > -		if (ip->i_df.if_nextents || ip->i_delayed_blks)
> > +		if (xfs_inode_has_data(ip))
> >  			return -EINVAL;
> >  
> >  		/*
> > @@ -602,7 +602,8 @@ xfs_ioctl_setattr_check_extsize(
> >  	if (!fa->fsx_valid)
> >  		return 0;
> >  
> > -	if (S_ISREG(VFS_I(ip)->i_mode) && ip->i_df.if_nextents &&
> > +	if (S_ISREG(VFS_I(ip)->i_mode) &&
> > +	    xfs_inode_has_data(ip) &&
> >  	    XFS_FSB_TO_B(mp, ip->i_extsize) != fa->fsx_extsize)
> >  		return -EINVAL;
> >  
> > -- 
> > 2.43.5
> > 

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

end of thread, other threads:[~2024-10-11  8:52 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-10  6:36 [PATCH v2] xfs: Check for deallayed allocations before setting extsize Ojaswin Mujoo
2024-10-10  6:50 ` Christoph Hellwig
2024-10-10  8:56   ` Ojaswin Mujoo
2024-10-10 18:34 ` Darrick J. Wong
2024-10-11  8:51   ` Ojaswin Mujoo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox