From: Jaegeuk Kim <jaeg...@kernel.org>

[ Upstream commit e23ab8028de0d92df5921a570f5212c0370db3b5 ]

Let's return errors caught by the generic checks. This fixes generic/494 where
it expects to see EBUSY by setattr_prepare instead of EINVAL by f2fs for active
swapfile.

Reviewed-by: Chao Yu <c...@kernel.org>
Signed-off-by: Jaegeuk Kim <jaeg...@kernel.org>
Signed-off-by: Sasha Levin <sas...@kernel.org>
---

LLM Generated explanations, may be completely bogus:

**Backport Status: YES**

This commit should be backported to stable kernel trees for the
following reasons:

## Bug Fix Analysis

1. **Fixes a real bug affecting users**: The commit fixes incorrect
   error code handling for swapfiles. When attempting to modify an
   active swapfile, f2fs was returning `-EINVAL` instead of the correct
   `-EBUSY` error code. This breaks userspace expectations and causes
   test failures in `generic/494`.

2. **Small and contained fix**: The change is minimal - it simply
   reorders the error checking sequence in `f2fs_setattr()` to call
   generic checks (`setattr_prepare`, `fscrypt_prepare_setattr`,
   `fsverity_prepare_setattr`) before f2fs-specific checks. The code
   movement involves only 12 lines being relocated within the same
   function.

## Technical Details

The commit moves three generic preparation calls from lines 1055-1065
(after f2fs-specific checks) to lines 1055-1065 (before f2fs-specific
checks). This ensures that:

- `setattr_prepare()` gets called first, which contains the
  `IS_SWAPFILE()` check that returns `-ETXTBSY` (which gets translated
  to `-EBUSY`)
- The generic VFS layer error codes are returned consistently with other
  filesystems
- F2fs-specific validation (like compression, pinned file checks) only
  happens after generic validation passes

## Risk Assessment

1. **Minimal regression risk**: The change only reorders existing checks
   without adding new logic or modifying the checks themselves. All the
   same validation still occurs, just in a different order.

2. **Follows stable tree rules**: This is a clear bugfix that:
   - Fixes incorrect error reporting to userspace
   - Makes f2fs behavior consistent with VFS expectations
   - Fixes a specific test case (`generic/494`) that validates correct
     swapfile handling
   - Has no feature additions or architectural changes

3. **Limited scope**: The change is confined to a single function in the
   f2fs subsystem and doesn't affect any other kernel components.

4. **Already reviewed**: The commit has been reviewed by a subsystem
   maintainer (Chao Yu) and merged by the f2fs maintainer (Jaegeuk Kim).

The incorrect error code could potentially confuse userspace
applications that rely on specific error codes to determine why an
operation failed. Returning `-EINVAL` (invalid argument) instead of
`-EBUSY` (resource busy) for an active swapfile is semantically
incorrect and breaks POSIX compliance expectations.

 fs/f2fs/file.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)

diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
index 696131e655ed..bb3fd6a8416f 100644
--- a/fs/f2fs/file.c
+++ b/fs/f2fs/file.c
@@ -1047,6 +1047,18 @@ int f2fs_setattr(struct mnt_idmap *idmap, struct dentry 
*dentry,
        if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
                return -EIO;
 
+       err = setattr_prepare(idmap, dentry, attr);
+       if (err)
+               return err;
+
+       err = fscrypt_prepare_setattr(dentry, attr);
+       if (err)
+               return err;
+
+       err = fsverity_prepare_setattr(dentry, attr);
+       if (err)
+               return err;
+
        if (unlikely(IS_IMMUTABLE(inode)))
                return -EPERM;
 
@@ -1065,18 +1077,6 @@ int f2fs_setattr(struct mnt_idmap *idmap, struct dentry 
*dentry,
                        return -EINVAL;
        }
 
-       err = setattr_prepare(idmap, dentry, attr);
-       if (err)
-               return err;
-
-       err = fscrypt_prepare_setattr(dentry, attr);
-       if (err)
-               return err;
-
-       err = fsverity_prepare_setattr(dentry, attr);
-       if (err)
-               return err;
-
        if (is_quota_modification(idmap, inode, attr)) {
                err = f2fs_dquot_initialize(inode);
                if (err)
-- 
2.39.5



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

Reply via email to