Re: [PATCH] f2fs: fix to remove F2FS_COMPR_FL and tag F2FS_NOCOMP_FL at the same time

From: Chao Liu
Date: Thu Jun 23 2022 - 00:59:57 EST


On Wed, Jun 22, 2022 at 09:42:13PM +0800, Chao Yu wrote:
> On 2022/6/21 14:48, Chao Liu wrote:
> > From: Chao Liu <liuchao@xxxxxxxxxxx>
> >
> > If the inode has the compress flag, it will fail to use
> > 'chattr -c +m' to remove its compress flag and tag no compress flag.
> > However, the same command will be successful when executed again,
> > as shown below:
> >
> > $ touch foo.txt
> > $ chattr +c foo.txt
> > $ chattr -c +m foo.txt
> > chattr: Invalid argument while setting flags on foo.txt
> > $ chattr -c +m foo.txt
> > $ f2fs_io getflags foo.txt
> > get a flag on foo.txt ret=0, flags=nocompression,inline_data
> >
> > Fix this by removing some checks in f2fs_setflags_common()
> > that do not affect the original logic. I go through all the
> > possible scenarios, and the results are as follows. Bold is
> > the only thing that has changed.
> >
> > +---------------+-----------+-----------+----------+
> > | | file flags |
> > + command +-----------+-----------+----------+
> > | | no flag | compr | nocompr |
> > +---------------+-----------+-----------+----------+
> > | chattr +c | compr | compr | -EINVAL |
> > | chattr -c | no flag | no flag | nocompr |
> > | chattr +m | nocompr | -EINVAL | nocompr |
> > | chattr -m | no flag | compr | no flag |
> > | chattr +c +m | -EINVAL | -EINVAL | -EINVAL |
> > | chattr +c -m | compr | compr | compr |
> > | chattr -c +m | nocompr | *nocompr* | nocompr |
> > | chattr -c -m | no flag | no flag | no flag |
> > +---------------+-----------+-----------+----------+
> >
> > Fixes: 4c8ff7095bef ("f2fs: support data compression")
> > Signed-off-by: Chao Liu <liuchao@xxxxxxxxxxx>
> > ---
> >
> > This patch depends on the the patch
> > "f2fs: allow compression of files without blocks" sent earlier this day.
> >
> > fs/f2fs/file.c | 9 +--------
> > 1 file changed, 1 insertion(+), 8 deletions(-)
> >
> > diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
> > index daaa0dfd2d2e..0c3ae5993b7a 100644
> > --- a/fs/f2fs/file.c
> > +++ b/fs/f2fs/file.c
> > @@ -1873,10 +1873,7 @@ static int f2fs_setflags_common(struct inode *inode, u32 iflags, u32 mask)
> > if (masked_flags & F2FS_COMPR_FL) {
> > if (!f2fs_disable_compressed_file(inode))
> > return -EINVAL;
> > - }
> > - if (iflags & F2FS_NOCOMP_FL)
> > - return -EINVAL;
> > - if (iflags & F2FS_COMPR_FL) {
> > + } else {
> > if (!f2fs_may_compress(inode))
> > return -EINVAL;
> > if (S_ISREG(inode->i_mode) && F2FS_HAS_BLOCKS(inode))
> > @@ -1885,10 +1882,6 @@ static int f2fs_setflags_common(struct inode *inode, u32 iflags, u32 mask)
> > set_compress_context(inode);
> > }
> > }
> > - if ((iflags ^ masked_flags) & F2FS_NOCOMP_FL) {
> > - if (masked_flags & F2FS_COMPR_FL)
> > - return -EINVAL;
> > - }
>
> Without above check condition, can we return -EINVAL for the case:
>
> chattr +c on file w/ nocompr flag
>
> | | no flag | compr | nocompr |
> +---------------+-----------+-----------+----------+
> | chattr +c | compr | compr | *-EINVAL* |

Yes, we can.

chattr(1) grabs flags via GETFLAGS, modifies the result,
and passes that to SETFLAGS. If we execute 'chattr +c'
on the file with nocompr flag, the iflags will
contain both compr and nocompr flags, then be refused by:

if ((iflags & F2FS_COMPR_FL) && (iflags & F2FS_NOCOMP_FL))
return -EINVAL;

In addition, if iflags has only compr flag, while masked_flags
has only nocompr flag for some reason
(either because of concurrency of chattr(1) or by a user),
I think we need remove nocompr flag and tag compr flag on the file,
similar to the following.

| | no flag | compr | nocompr |
+---------------+-----------+-----------+----------+
| chattr +c -m | compr | compr | *compr* |

Thanks,