[PATCH v5 04/12] fs: switch fileattr_set_prepare() to xflags and refactor projid check
From: koraynilay
Date: Sat Dec 05 2026 - 09:25:28 EST
In preparation for the addition of masks to the fileattr API, switch
fileattr_set_prepare() checks from using FS_*_FL flags to FS_XFLAG_*
xflags and refactor project ID value check to be cleaner.
Signed-off-by: koraynilay <koray.fra@xxxxxxxxx>
---
fs/file_attr.c | 30 ++++++++++++++++++++----------
1 file changed, 20 insertions(+), 10 deletions(-)
diff --git a/fs/file_attr.c b/fs/file_attr.c
index 77e2f1bc22ad..c1a0272f1a83 100644
--- a/fs/file_attr.c
+++ b/fs/file_attr.c
@@ -74,6 +74,14 @@ void fileattr_fill_flags(struct file_kattr *fa, u32 flags)
}
EXPORT_SYMBOL(fileattr_fill_flags);
+static u32 xflags_to_flags(u32 xflags)
+{
+ struct file_kattr tmp = {};
+
+ fileattr_fill_xflags(&tmp, xflags);
+ return tmp.flags;
+}
+
/**
* vfs_fileattr_get - retrieve miscellaneous file attributes
* @dentry: the object to retrieve from
@@ -201,17 +209,20 @@ static int fileattr_set_prepare(struct inode *inode,
const struct file_kattr *old_ma,
struct file_kattr *fa)
{
+ u32 changed = (fa->fsx_xflags ^ old_ma->fsx_xflags) & FSX_FSXATTR_MASK;
int err;
/*
* The IMMUTABLE and APPEND_ONLY flags can only be changed by
* the relevant capability.
*/
- if ((fa->flags ^ old_ma->flags) & (FS_APPEND_FL | FS_IMMUTABLE_FL) &&
+ if (changed & (FS_XFLAG_APPEND | FS_XFLAG_IMMUTABLE) &&
!capable(CAP_LINUX_IMMUTABLE))
return -EPERM;
- err = fscrypt_prepare_setflags(inode, old_ma->flags, fa->flags);
+ err = fscrypt_prepare_setflags(inode,
+ xflags_to_flags(old_ma->fsx_xflags),
+ xflags_to_flags(fa->fsx_xflags));
if (err)
return err;
@@ -220,21 +231,20 @@ static int fileattr_set_prepare(struct inode *inode,
* namespace. Enforce that restriction only if we are trying to change
* the quota ID state. Everything else is allowed in user namespaces.
*/
- if (current_user_ns() != &init_user_ns) {
- if (old_ma->fsx_projid != fa->fsx_projid)
+ if (old_ma->fsx_projid != fa->fsx_projid) {
+ /* Caller is not allowed to change the project ID. */
+ if (current_user_ns() != &init_user_ns)
return -EINVAL;
- if ((old_ma->fsx_xflags ^ fa->fsx_xflags) &
- FS_XFLAG_PROJINHERIT)
- return -EINVAL;
- } else {
/*
* Caller is allowed to change the project ID. If it is being
* changed, make sure that the new value is valid.
*/
- if (old_ma->fsx_projid != fa->fsx_projid &&
- !projid_valid(make_kprojid(&init_user_ns, fa->fsx_projid)))
+ if (!projid_valid(make_kprojid(&init_user_ns, fa->fsx_projid)))
return -EINVAL;
}
+ if ((changed & FS_XFLAG_PROJINHERIT) &&
+ current_user_ns() != &init_user_ns)
+ return -EINVAL;
/* Check extent size hints. */
if ((fa->fsx_xflags & FS_XFLAG_EXTSIZE) && !S_ISREG(inode->i_mode))
--
2.56.1.dirty