* [PATCH v3 1/5] vfs: use goto out on error in vfs_fileattr_set()
2026-11-29 2:42 [PATCH v3 0/5] Add f*_xflags_{rw,ro}_mask flags to the fileattr API koraynilay
@ 2026-11-29 2:42 ` koraynilay
2026-11-29 2:42 ` [PATCH v3 2/5] vfs: add xflags masks to fileattr API koraynilay
` (4 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: koraynilay @ 2026-11-29 2:42 UTC (permalink / raw)
To: Namjae Jeon, Hyunchul Lee, Jonathan Corbet, Eric Biggers,
Theodore Y . Ts'o, Jaegeuk Kim, Alexander Viro,
Christian Brauner
Cc: Shuah Khan, Randy Dunlap, Chuck Lever, Jeff Layton, Jan Kara,
Alexander Aring, Darrick J . Wong, Andrey Albershteyn,
Amir Goldstein, Miklos Szeredi, Pali Rohár, Lauri Gates,
Christoph Hellwig, Dave Chinner, linux-btrfs, linux-xfs,
linux-kernel, linux-fsdevel, ntfs, linux-nfs, linux-api,
linux-doc, linux-unionfs, koraynilay
Switch from using if (!err) { ... } to the cleaner if (err) goto out;
in the main body of vfs_fileattr_set().
Signed-off-by: koraynilay <koray.fra@gmail.com>
Reviewed-by: Andrey Albershteyn <aalbersh@kernel.org>
---
fs/file_attr.c | 45 +++++++++++++++++++++++----------------------
1 file changed, 23 insertions(+), 22 deletions(-)
diff --git a/fs/file_attr.c b/fs/file_attr.c
index 833e03810b24..40d6518f1d8e 100644
--- a/fs/file_attr.c
+++ b/fs/file_attr.c
@@ -285,30 +285,31 @@ int vfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry,
inode_lock(inode);
err = vfs_fileattr_get(dentry, &old_ma);
- if (!err) {
- /* initialize missing bits from old_ma */
- if (fa->flags_valid) {
- fa->fsx_xflags |= old_ma.fsx_xflags & ~FS_XFLAG_COMMON;
- fa->fsx_extsize = old_ma.fsx_extsize;
- fa->fsx_nextents = old_ma.fsx_nextents;
- fa->fsx_projid = old_ma.fsx_projid;
- fa->fsx_cowextsize = old_ma.fsx_cowextsize;
- } else {
- fa->flags |= old_ma.flags & ~FS_COMMON_FL;
- }
-
- err = fileattr_set_prepare(inode, &old_ma, fa);
- if (err)
- goto out;
- err = security_inode_file_setattr(dentry, fa);
- if (err)
- goto out;
- err = inode->i_op->fileattr_set(idmap, dentry, fa);
- if (err)
- goto out;
- fsnotify_xattr(dentry);
+ if (err)
+ goto out;
+
+ /* initialize missing bits from old_ma */
+ if (fa->flags_valid) {
+ fa->fsx_xflags |= old_ma.fsx_xflags & ~FS_XFLAG_COMMON;
+ fa->fsx_extsize = old_ma.fsx_extsize;
+ fa->fsx_nextents = old_ma.fsx_nextents;
+ fa->fsx_projid = old_ma.fsx_projid;
+ fa->fsx_cowextsize = old_ma.fsx_cowextsize;
+ } else {
+ fa->flags |= old_ma.flags & ~FS_COMMON_FL;
}
+ err = fileattr_set_prepare(inode, &old_ma, fa);
+ if (err)
+ goto out;
+ err = security_inode_file_setattr(dentry, fa);
+ if (err)
+ goto out;
+ err = inode->i_op->fileattr_set(idmap, dentry, fa);
+ if (err)
+ goto out;
+ fsnotify_xattr(dentry);
+
out:
inode_unlock(inode);
return err;
--
2.56.0
^ permalink raw reply [flat|nested] 7+ messages in thread* [PATCH v3 2/5] vfs: add xflags masks to fileattr API
2026-11-29 2:42 [PATCH v3 0/5] Add f*_xflags_{rw,ro}_mask flags to the fileattr API koraynilay
2026-11-29 2:42 ` [PATCH v3 1/5] vfs: use goto out on error in vfs_fileattr_set() koraynilay
@ 2026-11-29 2:42 ` koraynilay
2026-11-29 2:42 ` [PATCH v3 3/5] vfs: add COMPR/ENCRYPT xflags from FS_COMPR_FL/FS_ENCRYPT_FL koraynilay
` (3 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: koraynilay @ 2026-11-29 2:42 UTC (permalink / raw)
To: Namjae Jeon, Hyunchul Lee, Jonathan Corbet, Eric Biggers,
Theodore Y . Ts'o, Jaegeuk Kim, Alexander Viro,
Christian Brauner
Cc: Shuah Khan, Randy Dunlap, Chuck Lever, Jeff Layton, Jan Kara,
Alexander Aring, Darrick J . Wong, Andrey Albershteyn,
Amir Goldstein, Miklos Szeredi, Pali Rohár, Lauri Gates,
Christoph Hellwig, Dave Chinner, linux-btrfs, linux-xfs,
linux-kernel, linux-fsdevel, ntfs, linux-nfs, linux-api,
linux-doc, linux-unionfs, koraynilay
Add read-write and read-only masks to the fileattr API to let
filesystems advertise their support for specific xflags, like
statx.stx_attributes_mask, and to let userspace specify only which
xflags it wants to change without needing a RMW cycle.
The masks have the following semantics:
- read-write mask:
* for filesystems: advertise which xflags they support changing
* for userspace: specify which xflags it wants to modify
- read-only mask:
* for filesystems: advertise which xflags they support only reading
* for userspace: ignored
And they should be filled according to the following:
- GET:
each filesystem that wants to implement this new API fills the masks
according to its supported read-write and read-only xflags,
then:
* for boolean xflags: it fills fsx_xflags with the current on-disk value;
* for value xflags: it sets the xflag bit to 1 in fsx_xflags and the
xflag value in the struct field;
- SET:
userspace fills the rw_mask with the bits of the xflags it wants to
change, then:
* for boolean xflags: it provides the xflag value as a bit in fa_xflags;
* for value xflags: it sets the xflag bit to 1 in fa_xflags and then
provides the value in the struct field; if the xflag bit is set to 0
instead, this informs the kernel to unset/clear that value.
To achieve this, add fa_xflags_rw_mask and fa_xflags_ro_mask to struct
file_attr, both __u64 to match fa_xflags' size, and bump
FILE_ATTR_SIZE_LATEST to FILE_ATTR_SIZE_VER1.
Add AT_FILEATTR_MASKS at_flags option that userspace needs to pass
(with usize >= FILE_ATTR_SIZE_VER1) in order to be able to use masks,
otherwise normal RMW semantics are used.
Add equivalent masks and a has_masks selector to struct file_kattr, set
according to AT_FILEATTR_MASKS, so we can know when a user of this API
(userspace or filesystem) supports them.
If AT_FILEATTR_MASKS is not specified:
- on GET: set fa_xflags_rw_mask and fa_xflags_ro_mask to 0 and strip new
xflags added in or after VER1, so older programs doing RMW don't pass
them back on SET;
- on SET: reject the operation with -EINVAL if fa_xflags_rw_mask or
fa_xflags_ro_mask is non-zero; if they're zero, convert the input as
necessary for masks filesystem or do normal RMW for non-masks
filesystems.
If AT_FILEATTR_MASKS is specified:
- on GET: if the filesystem supports masks, they will be filled,
otherwise they will be zero, either way return the full struct;
- on SET: with fa_xflags_rw_mask = 0, return early as a no-op after
permission and support checks; if it's non-zero, convert the input as
necessary for non-masks filesystems or pass the input directly to the
filesystem if it supports masks.
To make both non-masks and masks programs interact correctly with both
non-masks and masks filesystems, add:
- xflags_to_flags() / flags_to_xflags(): use fileattr_fill_*() to
convert FS_XFLAG_* xflags to their FS_*_FL flag counterpart and
vice-versa, returning only the converted bitmask;
- fileattr_set_mask_to_rmw() / fileattr_set_rmw_to_mask(): convert
between non-masks read-modify-write API and masks API.
Furthermore, add the missing FS_XFLAG_PROJID xflag, to allow userspace
to set the Project ID using file_[gs]etattr() by leveraging the new
masks. In order to prevent FS_IOC_FSSETXATTR from accepting
FS_XFLAG_PROJID, add FS_FILE_ATTR_XFLAGS_MASK (superset of
FS_XFLAGS_MASK) to support new xflags and use it to validate/clamp both
the {fa,fsx}_xflags fields and the masks.
Finally, move FILE_ATTR_SIZE_* constants from the user-facing
include/uapi/linux/fs.h to the internal include/linux/fileattr.h.
Assisted-by: Gemini:3.8-flash Gemini:4-hydrogen antigravity-cli-1.5
Signed-off-by: koraynilay <koray.fra@gmail.com>
---
fs/file_attr.c | 346 +++++++++++++++++++++++++++++++++----
include/linux/fileattr.h | 12 ++
include/uapi/linux/fcntl.h | 6 +
include/uapi/linux/fs.h | 7 +-
4 files changed, 332 insertions(+), 39 deletions(-)
diff --git a/fs/file_attr.c b/fs/file_attr.c
index 40d6518f1d8e..5d3834f4405b 100644
--- a/fs/file_attr.c
+++ b/fs/file_attr.c
@@ -74,6 +74,22 @@ void fileattr_fill_flags(struct file_kattr *fa, u32 flags)
}
EXPORT_SYMBOL(fileattr_fill_flags);
+static u32 flags_to_xflags(u32 flags)
+{
+ struct file_kattr tmp = {};
+
+ fileattr_fill_flags(&tmp, flags);
+ return tmp.fsx_xflags;
+}
+
+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
@@ -95,20 +111,35 @@ int vfs_fileattr_get(struct dentry *dentry, struct file_kattr *fa)
if (error)
return error;
- return inode->i_op->fileattr_get(dentry, fa);
+ error = inode->i_op->fileattr_get(dentry, fa);
+ if (error)
+ return error;
+
+ if (fa->has_masks) {
+ fa->fsx_xflags_rw_mask &= FS_FILE_ATTR_XFLAGS_MASK;
+ fa->fsx_xflags_ro_mask &= FS_FILE_ATTR_XFLAGS_MASK;
+ }
+ fa->fsx_xflags &= FS_FILE_ATTR_XFLAGS_MASK;
+
+ return 0;
}
EXPORT_SYMBOL(vfs_fileattr_get);
static void fileattr_to_file_attr(const struct file_kattr *fa,
struct file_attr *fattr)
{
- __u32 mask = FS_XFLAGS_MASK;
-
memset(fattr, 0, sizeof(struct file_attr));
- fattr->fa_xflags = fa->fsx_xflags & mask;
+ /* Pass the masks to userspace if underlying fs uses them. */
+ if (fa->has_masks) {
+ fattr->fa_xflags_rw_mask = fa->fsx_xflags_rw_mask;
+ fattr->fa_xflags_ro_mask = fa->fsx_xflags_ro_mask;
+ }
+ fattr->fa_xflags = fa->fsx_xflags;
fattr->fa_extsize = fa->fsx_extsize;
fattr->fa_nextents = fa->fsx_nextents;
fattr->fa_projid = fa->fsx_projid;
+ if (fa->fsx_projid)
+ fattr->fa_xflags |= FS_XFLAG_PROJID;
fattr->fa_cowextsize = fa->fsx_cowextsize;
}
@@ -141,15 +172,47 @@ EXPORT_SYMBOL(copy_fsxattr_to_user);
static int file_attr_to_fileattr(const struct file_attr *fattr,
struct file_kattr *fa)
{
- __u64 mask = FS_XFLAGS_MASK;
+ u64 mask = fa->has_masks ? FS_FILE_ATTR_XFLAGS_MASK : FS_XFLAGS_MASK;
if (fattr->fa_xflags & ~mask)
return -EINVAL;
- fileattr_fill_xflags(fa, fattr->fa_xflags & ~FS_XFLAG_RDONLY_MASK);
- fa->fsx_extsize = fattr->fa_extsize;
- fa->fsx_projid = fattr->fa_projid;
- fa->fsx_cowextsize = fattr->fa_cowextsize;
+ if (fa->has_masks) {
+ u64 rw_user_mask = fattr->fa_xflags_rw_mask;
+
+ if (rw_user_mask & ~FS_FILE_ATTR_XFLAGS_MASK)
+ return -EINVAL;
+
+ /*
+ * Pass xflags clamped to the rw_mask, so the rest of the code
+ * can assume that if a flag is not in fsx_xflags, it isn't
+ * being changed.
+ */
+ fileattr_fill_xflags(fa, fattr->fa_xflags & rw_user_mask);
+ fa->fsx_xflags_rw_mask = rw_user_mask;
+
+ /* As per the comment above, no need to check rw_mask. */
+ if (fa->fsx_xflags & (FS_XFLAG_EXTSIZE | FS_XFLAG_EXTSZINHERIT))
+ fa->fsx_extsize = fattr->fa_extsize;
+ else
+ fa->fsx_extsize = 0;
+
+ if (fa->fsx_xflags & FS_XFLAG_PROJID)
+ fa->fsx_projid = fattr->fa_projid;
+ else
+ fa->fsx_projid = 0;
+
+ if (fa->fsx_xflags & FS_XFLAG_COWEXTSIZE)
+ fa->fsx_cowextsize = fattr->fa_cowextsize;
+ else
+ fa->fsx_cowextsize = 0;
+ } else {
+ fileattr_fill_xflags(fa,
+ fattr->fa_xflags & ~FS_XFLAG_RDONLY_MASK);
+ fa->fsx_extsize = fattr->fa_extsize;
+ fa->fsx_projid = fattr->fa_projid;
+ fa->fsx_cowextsize = fattr->fa_cowextsize;
+ }
return 0;
}
@@ -175,6 +238,178 @@ static int copy_fsxattr_from_user(struct file_kattr *fa,
return 0;
}
+/*
+ * Convert RMW input format without masks, coming from userspace, to input
+ * format with masks, for filesystems that already support masks.
+ */
+static int fileattr_set_rmw_to_mask(struct file_kattr *fa,
+ const struct file_kattr *old_ma)
+{
+ u32 changed = 0;
+ u32 new_rw_mask = 0;
+ u32 new_xflags = 0;
+
+ if (fa->flags_valid) {
+ /*
+ * Assume that if the filesystem supports masks it supports its
+ * writable FS_*_FL flags via FS_XFLAG_* and fsx_xflags_rw_mask.
+ */
+ changed = fa->flags ^ old_ma->flags;
+ if (changed & ~xflags_to_flags(old_ma->fsx_xflags_rw_mask))
+ return -EOPNOTSUPP;
+
+ changed = flags_to_xflags(changed);
+ } else { /* if (fa->fsx_valid) */
+ changed = fa->fsx_xflags ^ old_ma->fsx_xflags;
+ changed &= FS_XFLAGS_MASK & ~FS_XFLAG_RDONLY_MASK;
+ }
+
+ new_xflags = fa->fsx_xflags;
+
+ new_rw_mask = changed;
+
+ if (fa->fsx_valid) {
+ if (fa->fsx_extsize != old_ma->fsx_extsize)
+ new_rw_mask |= new_xflags &
+ (FS_XFLAG_EXTSIZE | FS_XFLAG_EXTSZINHERIT);
+
+ if (fa->fsx_projid != old_ma->fsx_projid) {
+ new_rw_mask |= FS_XFLAG_PROJID;
+ if (fa->fsx_projid > 0)
+ new_xflags |= FS_XFLAG_PROJID;
+ }
+
+ if ((new_xflags & FS_XFLAG_COWEXTSIZE) &&
+ fa->fsx_cowextsize != old_ma->fsx_cowextsize)
+ new_rw_mask |= FS_XFLAG_COWEXTSIZE;
+ }
+
+ /* Make sure we're only setting the wanted bits. */
+ new_xflags &= new_rw_mask;
+
+ /* Mask aware filesystems shouldn't use fa->flags, but just to be sure. */
+ fa->flags = xflags_to_flags(new_xflags);
+
+ fa->fsx_xflags = new_xflags;
+ fa->fsx_xflags_rw_mask = new_rw_mask;
+ fa->has_masks = true;
+
+ fa->fsx_valid = true;
+ fa->flags_valid = false;
+
+ return 0;
+}
+
+/*
+ * Convert input format with masks, coming from userspace, to RMW input format
+ * without masks, for filesystems that don't support masks yet.
+ */
+static int fileattr_set_mask_to_rmw(struct file_kattr *fa,
+ const struct file_kattr *old_ma)
+{
+ u32 rw_user_mask = fa->fsx_xflags_rw_mask;
+ u32 new_xflags = fa->fsx_xflags;
+ u32 new_flags = 0;
+ u32 tmp_xflag = 0;
+
+ /*
+ * Project ID was already supported before masks, but FS_XFLAG_PROJID
+ * didn't exist, so allow it explicitly.
+ */
+ if (rw_user_mask &
+ ~((FS_XFLAGS_MASK & ~FS_XFLAG_RDONLY_MASK) | FS_XFLAG_PROJID))
+ return -EOPNOTSUPP;
+
+ /* Do RMW. */
+ new_xflags |= old_ma->fsx_xflags & ~rw_user_mask;
+
+ /* RMW filesystems expect to not have any globally read-only flag. */
+ new_xflags &= ~FS_XFLAG_RDONLY_MASK;
+
+ new_flags = xflags_to_flags(new_xflags);
+ new_flags |= old_ma->flags & ~FS_COMMON_FL;
+
+ tmp_xflag = FS_XFLAG_EXTSIZE | FS_XFLAG_EXTSZINHERIT;
+ /* User didn't turn on any flag for extsize. */
+ if (!(fa->fsx_xflags & tmp_xflag)) {
+ if ((rw_user_mask & tmp_xflag) && !(new_xflags & tmp_xflag)) {
+ /*
+ * If the user wants to clear one of them and neither
+ * is set after the RMW above, clear fsx_extsize.
+ */
+ fa->fsx_extsize = 0;
+ } else {
+ /*
+ * If the user doesn't want to clear either of them OR
+ * the other flag is still set after RMW, preserve the
+ * old value.
+ */
+ fa->fsx_extsize = old_ma->fsx_extsize;
+ }
+ }
+
+ tmp_xflag = FS_XFLAG_PROJID;
+ if (!(rw_user_mask & tmp_xflag))
+ fa->fsx_projid = old_ma->fsx_projid;
+ else if (!(fa->fsx_xflags & tmp_xflag))
+ fa->fsx_projid = 0;
+ /* Strip PROJID now that we've used it, old format doesn't have it. */
+ new_xflags &= ~FS_XFLAG_PROJID;
+
+ tmp_xflag = FS_XFLAG_COWEXTSIZE;
+ if (!(rw_user_mask & tmp_xflag))
+ fa->fsx_cowextsize = old_ma->fsx_cowextsize;
+ else if (!(new_xflags & tmp_xflag))
+ fa->fsx_cowextsize = 0;
+
+ fa->flags = new_flags;
+ fa->fsx_xflags = new_xflags;
+ fa->fsx_xflags_rw_mask = 0;
+ fa->has_masks = false;
+
+ fa->fsx_valid = true;
+ fa->flags_valid = false;
+
+ return 0;
+}
+
+/*
+ * Handle converting between RMW and masks formats based on userspace and
+ * filesystem support.
+ * Preserve unchanged bits from old_ma.
+ */
+static int fileattr_set_convert(struct file_kattr *fa,
+ const struct file_kattr *old_ma)
+{
+ if (fa->has_masks) {
+ if (!old_ma->has_masks)
+ return fileattr_set_mask_to_rmw(fa, old_ma);
+
+ /* Both have masks, pass the request through. */
+ return 0;
+ }
+
+ if (old_ma->has_masks)
+ return fileattr_set_rmw_to_mask(fa, old_ma);
+
+ fa->fsx_xflags_rw_mask = 0;
+
+ /* Initialize missing bits from old_ma. */
+ if (fa->flags_valid) {
+ /* FS_IOC_SETFLAGS */
+ fa->fsx_xflags |= old_ma->fsx_xflags & ~FS_XFLAG_COMMON;
+ fa->fsx_extsize = old_ma->fsx_extsize;
+ fa->fsx_nextents = old_ma->fsx_nextents;
+ fa->fsx_projid = old_ma->fsx_projid;
+ fa->fsx_cowextsize = old_ma->fsx_cowextsize;
+ } else {
+ /* FS_IOC_FSSETXATTR or file_setattr without AT_FILEATTR_MASKS */
+ fa->flags |= old_ma->flags & ~FS_COMMON_FL;
+ }
+
+ return 0;
+}
+
/*
* Generic function to check FS_IOC_FSSETXATTR/FS_IOC_SETFLAGS values and reject
* any invalid configurations.
@@ -185,17 +420,27 @@ static int fileattr_set_prepare(struct inode *inode,
const struct file_kattr *old_ma,
struct file_kattr *fa)
{
+ u32 rw_user_mask = fa->has_masks ?
+ fa->fsx_xflags_rw_mask : FS_FILE_ATTR_XFLAGS_MASK;
+ u32 changed = 0;
int err;
+ if (fa->has_masks && (rw_user_mask & ~old_ma->fsx_xflags_rw_mask))
+ return -EOPNOTSUPP;
+
+ changed = (fa->fsx_xflags ^ old_ma->fsx_xflags) & rw_user_mask;
+
/*
* 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;
@@ -204,21 +449,21 @@ 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)
- return -EINVAL;
- if ((old_ma->fsx_xflags ^ fa->fsx_xflags) &
- FS_XFLAG_PROJINHERIT)
+ if ((rw_user_mask & FS_XFLAG_PROJID) &&
+ 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;
- } 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))
@@ -250,6 +495,13 @@ static int fileattr_set_prepare(struct inode *inode,
fa->fsx_xflags &= ~(FS_XFLAG_EXTSIZE | FS_XFLAG_EXTSZINHERIT);
if (fa->fsx_cowextsize == 0)
fa->fsx_xflags &= ~FS_XFLAG_COWEXTSIZE;
+ /*
+ * No need for projid here, because
+ * (xflags & PROJID) && (rw_user_mask & PROJID) && fsx_projid == 0
+ * should give the same result as
+ * !(xflags & PROJID) && (rw_user_mask & PROJID) && fsx_projid >= 0.
+ * but it depends on how each filesystem wants to implement it.
+ */
return 0;
}
@@ -283,21 +535,17 @@ int vfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry,
if (!inode_owner_or_capable(idmap, inode))
return -EPERM;
+ if (fa->has_masks && !fa->fsx_xflags_rw_mask)
+ return 0;
+
inode_lock(inode);
err = vfs_fileattr_get(dentry, &old_ma);
if (err)
goto out;
- /* initialize missing bits from old_ma */
- if (fa->flags_valid) {
- fa->fsx_xflags |= old_ma.fsx_xflags & ~FS_XFLAG_COMMON;
- fa->fsx_extsize = old_ma.fsx_extsize;
- fa->fsx_nextents = old_ma.fsx_nextents;
- fa->fsx_projid = old_ma.fsx_projid;
- fa->fsx_cowextsize = old_ma.fsx_cowextsize;
- } else {
- fa->flags |= old_ma.flags & ~FS_COMMON_FL;
- }
+ err = fileattr_set_convert(fa, &old_ma);
+ if (err)
+ goto out;
err = fileattr_set_prepare(inode, &old_ma, fa);
if (err)
@@ -390,7 +638,8 @@ SYSCALL_DEFINE5(file_getattr, int, dfd, const char __user *, filename,
BUILD_BUG_ON(sizeof(struct file_attr) < FILE_ATTR_SIZE_VER0);
BUILD_BUG_ON(sizeof(struct file_attr) != FILE_ATTR_SIZE_LATEST);
- if ((at_flags & ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH)) != 0)
+ if ((at_flags &
+ ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH | AT_FILEATTR_MASKS)) != 0)
return -EINVAL;
if (!(at_flags & AT_SYMLINK_NOFOLLOW))
@@ -399,7 +648,10 @@ SYSCALL_DEFINE5(file_getattr, int, dfd, const char __user *, filename,
if (usize > PAGE_SIZE)
return -E2BIG;
- if (usize < FILE_ATTR_SIZE_VER0)
+ if (usize != FILE_ATTR_SIZE_VER0 && usize < FILE_ATTR_SIZE_VER1)
+ return -EINVAL;
+
+ if ((at_flags & AT_FILEATTR_MASKS) && usize < FILE_ATTR_SIZE_VER1)
return -EINVAL;
CLASS(filename_maybe_null, name)(filename, at_flags);
@@ -424,6 +676,17 @@ SYSCALL_DEFINE5(file_getattr, int, dfd, const char __user *, filename,
return error;
fileattr_to_file_attr(&fa, &fattr);
+ /*
+ * Caller doesn't know about masks, so it also doesn't know about
+ * xflags added in or after VER1.
+ * Zero out the masks and strip VER1+ xflags.
+ */
+ if (!(at_flags & AT_FILEATTR_MASKS)) {
+ fattr.fa_xflags &= FS_XFLAGS_MASK;
+ fattr.fa_xflags_rw_mask = 0;
+ fattr.fa_xflags_ro_mask = 0;
+ }
+
error = copy_struct_to_user(ufattr, usize, &fattr,
sizeof(struct file_attr), NULL);
@@ -443,7 +706,8 @@ SYSCALL_DEFINE5(file_setattr, int, dfd, const char __user *, filename,
BUILD_BUG_ON(sizeof(struct file_attr) < FILE_ATTR_SIZE_VER0);
BUILD_BUG_ON(sizeof(struct file_attr) != FILE_ATTR_SIZE_LATEST);
- if ((at_flags & ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH)) != 0)
+ if ((at_flags &
+ ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH | AT_FILEATTR_MASKS)) != 0)
return -EINVAL;
if (!(at_flags & AT_SYMLINK_NOFOLLOW))
@@ -452,7 +716,10 @@ SYSCALL_DEFINE5(file_setattr, int, dfd, const char __user *, filename,
if (usize > PAGE_SIZE)
return -E2BIG;
- if (usize < FILE_ATTR_SIZE_VER0)
+ if (usize != FILE_ATTR_SIZE_VER0 && usize < FILE_ATTR_SIZE_VER1)
+ return -EINVAL;
+
+ if ((at_flags & AT_FILEATTR_MASKS) && usize < FILE_ATTR_SIZE_VER1)
return -EINVAL;
error = copy_struct_from_user(&fattr, sizeof(struct file_attr), ufattr,
@@ -460,9 +727,9 @@ SYSCALL_DEFINE5(file_setattr, int, dfd, const char __user *, filename,
if (error)
return error;
- error = file_attr_to_fileattr(&fattr, &fa);
- if (error)
- return error;
+ if (!(at_flags & AT_FILEATTR_MASKS) &&
+ (fattr.fa_xflags_rw_mask || fattr.fa_xflags_ro_mask))
+ return -EINVAL;
CLASS(filename_maybe_null, name)(filename, at_flags);
if (!name && dfd >= 0) {
@@ -479,6 +746,13 @@ SYSCALL_DEFINE5(file_setattr, int, dfd, const char __user *, filename,
return error;
}
+ if (at_flags & AT_FILEATTR_MASKS)
+ fa.has_masks = true;
+
+ error = file_attr_to_fileattr(&fattr, &fa);
+ if (error)
+ return error;
+
error = mnt_want_write(filepath.mnt);
if (!error) {
error = vfs_fileattr_set(mnt_idmap(filepath.mnt),
diff --git a/include/linux/fileattr.h b/include/linux/fileattr.h
index 09e32b84e02a..fb158269b4ea 100644
--- a/include/linux/fileattr.h
+++ b/include/linux/fileattr.h
@@ -3,6 +3,11 @@
#ifndef _LINUX_FILEATTR_H
#define _LINUX_FILEATTR_H
+/* Version UAPI's struct file_attr based on size */
+#define FILE_ATTR_SIZE_VER0 24
+#define FILE_ATTR_SIZE_VER1 40
+#define FILE_ATTR_SIZE_LATEST FILE_ATTR_SIZE_VER1
+
/* Flags shared betwen flags/xflags */
#define FS_COMMON_FL \
(FS_SYNC_FL | FS_IMMUTABLE_FL | FS_APPEND_FL | \
@@ -35,6 +40,10 @@
(FS_XFLAG_COMMON | FS_XFLAG_RDONLY_MASK | FS_XFLAG_VALUES_MASK | \
FS_XFLAG_DIRONLY_MASK | FS_XFLAG_MISC_MASK)
+/* Flags only for fa_ fields */
+#define FS_FILE_ATTR_XFLAGS_MASK \
+ (FS_XFLAGS_MASK | FS_XFLAG_PROJID)
+
/*
* Merged interface for miscellaneous file attributes. 'flags' originates from
* ext* and 'fsx_flags' from xfs. There's some overlap between the two, which
@@ -45,6 +54,8 @@ struct file_kattr {
u32 flags; /* flags (FS_IOC_GETFLAGS/FS_IOC_SETFLAGS) */
/* struct fsxattr: */
u32 fsx_xflags; /* xflags field value (get/set) */
+ u32 fsx_xflags_rw_mask; /* supported writeable xflags (get) / flags to set (set) */
+ u32 fsx_xflags_ro_mask; /* supported read-only xflags (get) */
u32 fsx_extsize; /* extsize field value (get/set)*/
u32 fsx_nextents; /* nextents field value (get) */
u32 fsx_projid; /* project identifier (get/set) */
@@ -52,6 +63,7 @@ struct file_kattr {
/* selectors: */
bool flags_valid:1;
bool fsx_valid:1;
+ bool has_masks:1;
};
int copy_fsxattr_to_user(const struct file_kattr *fa, struct fsxattr __user *ufa);
diff --git a/include/uapi/linux/fcntl.h b/include/uapi/linux/fcntl.h
index e43e3de3e9ee..9af205794404 100644
--- a/include/uapi/linux/fcntl.h
+++ b/include/uapi/linux/fcntl.h
@@ -191,4 +191,10 @@ struct delegation {
#define AT_EXECVE_CHECK 0x10000 /* Only perform a check if execution
would be allowed. */
+/* Flags for file_getattr(2) / file_setattr(2). */
+#define AT_FILEATTR_MASKS 0x001 /* Advertise knowing and being able to
+ handle the struct file_attr fields
+ fa_xflags_rw_mask and
+ fa_xflags_ro_mask */
+
#endif /* _UAPI_LINUX_FCNTL_H */
diff --git a/include/uapi/linux/fs.h b/include/uapi/linux/fs.h
index a13c7b32a1bd..9834f6eee000 100644
--- a/include/uapi/linux/fs.h
+++ b/include/uapi/linux/fs.h
@@ -238,11 +238,10 @@ struct file_attr {
__u32 fa_nextents; /* nextents field value (get) */
__u32 fa_projid; /* project identifier (get/set) */
__u32 fa_cowextsize; /* CoW extsize field value (get/set) */
+ __u64 fa_xflags_rw_mask;/* supported writeable xflags (get) / flags to set (set) */
+ __u64 fa_xflags_ro_mask;/* supported read-only xflags (get) */
};
-#define FILE_ATTR_SIZE_VER0 24
-#define FILE_ATTR_SIZE_LATEST FILE_ATTR_SIZE_VER0
-
/*
* Flags for the fsx_xflags field
*/
@@ -270,6 +269,8 @@ struct file_attr {
*/
#define FS_XFLAG_CASEFOLD 0x00040000 /* case-insensitive lookups */
#define FS_XFLAG_CASENONPRESERVING 0x00080000 /* case not preserved */
+#define FS_XFLAG_PROJID 0x00100000 /* project ID */
+
#define FS_XFLAG_HASATTR 0x80000000 /* no DIFLAG for this */
/* the read-only stuff doesn't really belong here, but any other place is
--
2.56.0
^ permalink raw reply [flat|nested] 7+ messages in thread* [PATCH v3 3/5] vfs: add COMPR/ENCRYPT xflags from FS_COMPR_FL/FS_ENCRYPT_FL
2026-11-29 2:42 [PATCH v3 0/5] Add f*_xflags_{rw,ro}_mask flags to the fileattr API koraynilay
2026-11-29 2:42 ` [PATCH v3 1/5] vfs: use goto out on error in vfs_fileattr_set() koraynilay
2026-11-29 2:42 ` [PATCH v3 2/5] vfs: add xflags masks to fileattr API koraynilay
@ 2026-11-29 2:42 ` koraynilay
2026-11-29 2:42 ` [PATCH v3 4/5] ntfs: switch to the new xflags fileattr API koraynilay
` (2 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: koraynilay @ 2026-11-29 2:42 UTC (permalink / raw)
To: Namjae Jeon, Hyunchul Lee, Jonathan Corbet, Eric Biggers,
Theodore Y . Ts'o, Jaegeuk Kim, Alexander Viro,
Christian Brauner
Cc: Shuah Khan, Randy Dunlap, Chuck Lever, Jeff Layton, Jan Kara,
Alexander Aring, Darrick J . Wong, Andrey Albershteyn,
Amir Goldstein, Miklos Szeredi, Pali Rohár, Lauri Gates,
Christoph Hellwig, Dave Chinner, linux-btrfs, linux-xfs,
linux-kernel, linux-fsdevel, ntfs, linux-nfs, linux-api,
linux-doc, linux-unionfs, koraynilay
Introduce FS_XFLAG_COMPR and FS_XFLAG_ENCRYPT to be used in the vfs
_masks API upgrade for fs/ntfs ([1] and newer versions).
Following the FS_XFLAG_CASEFOLD example, put FS_XFLAG_COMPR in
FS_XFLAG_RDONLY_MASK but not in FS_XFLAG_COMMON, since it's read-write
for some filesystems and read-only for others, to avoid breaking
existing filesystems.
Following the FS_XFLAG_VERITY example, put FS_XFLAG_ENCRYPT in
FS_XFLAG_RDONLY_MASK and in FS_XFLAG_COMMON, since it's read-only for
all filesystems.
Also update vfs fileattr_fill_* helpers to convert these two flags as
well.
Link: https://lore.kernel.org/20261122000856.536902-1-koray.fra@gmail.com/ [1]
Assisted-by: Gemini:3.8-flash-high antigravity-cli-1.5.11
Signed-off-by: koraynilay <koray.fra@gmail.com>
---
fs/file_attr.c | 8 ++++++++
include/linux/fileattr.h | 7 ++++---
include/uapi/linux/fs.h | 2 ++
3 files changed, 14 insertions(+), 3 deletions(-)
diff --git a/fs/file_attr.c b/fs/file_attr.c
index 5d3834f4405b..314d779e41d6 100644
--- a/fs/file_attr.c
+++ b/fs/file_attr.c
@@ -39,6 +39,10 @@ void fileattr_fill_xflags(struct file_kattr *fa, u32 xflags)
fa->flags |= FS_VERITY_FL;
if (fa->fsx_xflags & FS_XFLAG_CASEFOLD)
fa->flags |= FS_CASEFOLD_FL;
+ if (fa->fsx_xflags & FS_XFLAG_COMPR)
+ fa->flags |= FS_COMPR_FL;
+ if (fa->fsx_xflags & FS_XFLAG_ENCRYPT)
+ fa->flags |= FS_ENCRYPT_FL;
}
EXPORT_SYMBOL(fileattr_fill_xflags);
@@ -71,6 +75,10 @@ void fileattr_fill_flags(struct file_kattr *fa, u32 flags)
fa->fsx_xflags |= FS_XFLAG_VERITY;
if (fa->flags & FS_CASEFOLD_FL)
fa->fsx_xflags |= FS_XFLAG_CASEFOLD;
+ if (fa->flags & FS_COMPR_FL)
+ fa->fsx_xflags |= FS_XFLAG_COMPR;
+ if (fa->flags & FS_ENCRYPT_FL)
+ fa->fsx_xflags |= FS_XFLAG_ENCRYPT;
}
EXPORT_SYMBOL(fileattr_fill_flags);
diff --git a/include/linux/fileattr.h b/include/linux/fileattr.h
index fb158269b4ea..9a8de4190133 100644
--- a/include/linux/fileattr.h
+++ b/include/linux/fileattr.h
@@ -12,17 +12,18 @@
#define FS_COMMON_FL \
(FS_SYNC_FL | FS_IMMUTABLE_FL | FS_APPEND_FL | \
FS_NODUMP_FL | FS_NOATIME_FL | FS_DAX_FL | \
- FS_PROJINHERIT_FL | FS_VERITY_FL)
+ FS_PROJINHERIT_FL | FS_VERITY_FL | FS_ENCRYPT_FL)
#define FS_XFLAG_COMMON \
(FS_XFLAG_SYNC | FS_XFLAG_IMMUTABLE | FS_XFLAG_APPEND | \
FS_XFLAG_NODUMP | FS_XFLAG_NOATIME | FS_XFLAG_DAX | \
- FS_XFLAG_PROJINHERIT | FS_XFLAG_VERITY)
+ FS_XFLAG_PROJINHERIT | FS_XFLAG_VERITY | FS_XFLAG_ENCRYPT)
/* Read-only inode flags */
#define FS_XFLAG_RDONLY_MASK \
(FS_XFLAG_PREALLOC | FS_XFLAG_HASATTR | FS_XFLAG_VERITY | \
- FS_XFLAG_CASEFOLD | FS_XFLAG_CASENONPRESERVING)
+ FS_XFLAG_CASEFOLD | FS_XFLAG_CASENONPRESERVING | FS_XFLAG_ENCRYPT | \
+ FS_XFLAG_COMPR)
/* Flags to indicate valid value of fsx_ fields */
#define FS_XFLAG_VALUES_MASK \
diff --git a/include/uapi/linux/fs.h b/include/uapi/linux/fs.h
index 9834f6eee000..c1c487d07d29 100644
--- a/include/uapi/linux/fs.h
+++ b/include/uapi/linux/fs.h
@@ -270,6 +270,8 @@ struct file_attr {
#define FS_XFLAG_CASEFOLD 0x00040000 /* case-insensitive lookups */
#define FS_XFLAG_CASENONPRESERVING 0x00080000 /* case not preserved */
#define FS_XFLAG_PROJID 0x00100000 /* project ID */
+#define FS_XFLAG_COMPR 0x00200000 /* Compress file */
+#define FS_XFLAG_ENCRYPT 0x00400000 /* Encrypted file */
#define FS_XFLAG_HASATTR 0x80000000 /* no DIFLAG for this */
--
2.56.0
^ permalink raw reply [flat|nested] 7+ messages in thread* [PATCH v3 4/5] ntfs: switch to the new xflags fileattr API
2026-11-29 2:42 [PATCH v3 0/5] Add f*_xflags_{rw,ro}_mask flags to the fileattr API koraynilay
` (2 preceding siblings ...)
2026-11-29 2:42 ` [PATCH v3 3/5] vfs: add COMPR/ENCRYPT xflags from FS_COMPR_FL/FS_ENCRYPT_FL koraynilay
@ 2026-11-29 2:42 ` koraynilay
2026-11-29 2:42 ` [PATCH v3 5/5] docs: add fileattr API documentation koraynilay
2026-11-29 2:47 ` [PATCH v3 0/5] Add f*_xflags_{rw,ro}_mask flags to the fileattr API koraynilay
5 siblings, 0 replies; 7+ messages in thread
From: koraynilay @ 2026-11-29 2:42 UTC (permalink / raw)
To: Namjae Jeon, Hyunchul Lee, Jonathan Corbet, Eric Biggers,
Theodore Y . Ts'o, Jaegeuk Kim, Alexander Viro,
Christian Brauner
Cc: Shuah Khan, Randy Dunlap, Chuck Lever, Jeff Layton, Jan Kara,
Alexander Aring, Darrick J . Wong, Andrey Albershteyn,
Amir Goldstein, Miklos Szeredi, Pali Rohár, Lauri Gates,
Christoph Hellwig, Dave Chinner, linux-btrfs, linux-xfs,
linux-kernel, linux-fsdevel, ntfs, linux-nfs, linux-api,
linux-doc, linux-unionfs, koraynilay
Switch ntfs_fileattr_[gs]et() from the legacy FS_*_FL flags to the
modern xflags (FS_XFLAG_*) fileattr API with masks.
Assisted-by: Gemini:3.8-flash-high antigravity-cli-1.5.11
Signed-off-by: koraynilay <koray.fra@gmail.com>
---
fs/ntfs/file.c | 109 ++++++++++++++++++++++++++-----------------------
1 file changed, 58 insertions(+), 51 deletions(-)
diff --git a/fs/ntfs/file.c b/fs/ntfs/file.c
index 50be49f4c913..34acf9333c41 100644
--- a/fs/ntfs/file.c
+++ b/fs/ntfs/file.c
@@ -155,31 +155,38 @@ int ntfs_file_release(struct inode *vi, struct file *filp)
* @dentry: dentry to report the flags of
* @fa: filled in with the flags of @dentry
*/
+#define NTFS_RO_XFLAGS (FS_XFLAG_COMPR | FS_XFLAG_ENCRYPT | \
+ FS_XFLAG_CASEFOLD)
+#define NTFS_RW_XFLAGS (FS_XFLAG_IMMUTABLE | FS_XFLAG_APPEND | \
+ FS_XFLAG_NOATIME | FS_XFLAG_NODUMP)
int ntfs_fileattr_get(struct dentry *dentry, struct file_kattr *fa)
{
struct inode *vi = d_inode(dentry);
struct ntfs_inode *ni = NTFS_I(vi);
- u32 flags = 0;
+ u32 xflags = 0;
if (NVolShutdown(ni->vol))
return -EIO;
if (NInoCompressed(ni) || NInoWofCompressed(ni))
- flags |= FS_COMPR_FL;
+ xflags |= FS_XFLAG_COMPR;
if (NInoEncrypted(ni))
- flags |= FS_ENCRYPT_FL;
+ xflags |= FS_XFLAG_ENCRYPT;
if (vi->i_flags & S_IMMUTABLE)
- flags |= FS_IMMUTABLE_FL;
+ xflags |= FS_XFLAG_IMMUTABLE;
if (vi->i_flags & S_APPEND)
- flags |= FS_APPEND_FL;
+ xflags |= FS_XFLAG_APPEND;
if (vi->i_flags & S_NOATIME)
- flags |= FS_NOATIME_FL;
+ xflags |= FS_XFLAG_NOATIME;
if (ni->linuxflags & NTFS_LINUXFLAGS_NODUMP)
- flags |= FS_NODUMP_FL;
+ xflags |= FS_XFLAG_NODUMP;
if (!NVolCaseSensitive(ni->vol))
- flags |= FS_CASEFOLD_FL;
+ xflags |= FS_XFLAG_CASEFOLD;
- fileattr_fill_flags(fa, flags);
+ fileattr_fill_xflags(fa, xflags);
+ fa->fsx_xflags_ro_mask = NTFS_RO_XFLAGS;
+ fa->fsx_xflags_rw_mask = NTFS_RW_XFLAGS;
+ fa->has_masks = true;
return 0;
}
@@ -207,11 +214,9 @@ int ntfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry,
{
struct inode *vi = d_inode(dentry);
struct ntfs_inode *ni = NTFS_I(vi);
- u32 allowed = FS_IMMUTABLE_FL | FS_APPEND_FL | FS_NODUMP_FL |
- FS_NOATIME_FL;
- u32 readonly = 0;
- u32 linuxflags = ni->linuxflags & ~NTFS_LINUXFLAGS_MASK;
- unsigned int new_fl = 0;
+ u32 linuxflags = ni->linuxflags;
+ u32 rw_user_mask = fa->fsx_xflags_rw_mask;
+ unsigned int new_fl = 0, mask_fl = 0;
bool forced_immutable;
int err;
@@ -219,44 +224,46 @@ int ntfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry,
if (err)
return err;
- if (fileattr_has_fsx(fa))
- return -EOPNOTSUPP;
-
- /* chattr passes the unchanged read-only flags back to us too. */
- if (NInoCompressed(ni) || NInoWofCompressed(ni))
- readonly |= FS_COMPR_FL;
- if (NInoEncrypted(ni))
- readonly |= FS_ENCRYPT_FL;
- if ((fa->flags ^ readonly) & (FS_COMPR_FL | FS_ENCRYPT_FL))
- return -EOPNOTSUPP;
- allowed |= readonly;
-
- /* Case folding is a read-only, mount-wide property. */
- if (!NVolCaseSensitive(ni->vol))
- allowed |= FS_CASEFOLD_FL;
- if (fa->flags & ~allowed)
- return -EOPNOTSUPP;
-
- forced_immutable = ntfs_is_forced_immutable(vi);
- if (!(fa->flags & FS_IMMUTABLE_FL) && forced_immutable)
- return -EPERM;
-
- if (fa->flags & FS_IMMUTABLE_FL) {
- new_fl |= S_IMMUTABLE;
- /* Do not persist an immutable bit derived from the mount. */
- if (!forced_immutable ||
- (ni->linuxflags & NTFS_LINUXFLAGS_IMMUTABLE))
- linuxflags |= NTFS_LINUXFLAGS_IMMUTABLE;
+ if (rw_user_mask & FS_XFLAG_IMMUTABLE) {
+ forced_immutable = ntfs_is_forced_immutable(vi);
+ if (!(fa->fsx_xflags & FS_XFLAG_IMMUTABLE) && forced_immutable)
+ return -EPERM;
+
+ mask_fl |= S_IMMUTABLE;
+ if (fa->fsx_xflags & FS_XFLAG_IMMUTABLE) {
+ new_fl |= S_IMMUTABLE;
+ /* Do not persist an immutable bit derived from the mount. */
+ if (!forced_immutable ||
+ (ni->linuxflags & NTFS_LINUXFLAGS_IMMUTABLE))
+ linuxflags |= NTFS_LINUXFLAGS_IMMUTABLE;
+ } else {
+ if (ni->linuxflags & NTFS_LINUXFLAGS_IMMUTABLE)
+ linuxflags &= ~NTFS_LINUXFLAGS_IMMUTABLE;
+ }
}
- if (fa->flags & FS_APPEND_FL) {
- new_fl |= S_APPEND;
- linuxflags |= NTFS_LINUXFLAGS_APPEND;
+ if (rw_user_mask & FS_XFLAG_APPEND) {
+ mask_fl |= S_APPEND;
+ if (fa->fsx_xflags & FS_XFLAG_APPEND) {
+ new_fl |= S_APPEND;
+ linuxflags |= NTFS_LINUXFLAGS_APPEND;
+ } else {
+ linuxflags &= ~NTFS_LINUXFLAGS_APPEND;
+ }
+ }
+ if (rw_user_mask & FS_XFLAG_NODUMP) {
+ if (fa->fsx_xflags & FS_XFLAG_NODUMP)
+ linuxflags |= NTFS_LINUXFLAGS_NODUMP;
+ else
+ linuxflags &= ~NTFS_LINUXFLAGS_NODUMP;
}
- if (fa->flags & FS_NODUMP_FL)
- linuxflags |= NTFS_LINUXFLAGS_NODUMP;
- if (fa->flags & FS_NOATIME_FL) {
- new_fl |= S_NOATIME;
- linuxflags |= NTFS_LINUXFLAGS_NOATIME;
+ if (rw_user_mask & FS_XFLAG_NOATIME) {
+ mask_fl |= S_NOATIME;
+ if (fa->fsx_xflags & FS_XFLAG_NOATIME) {
+ new_fl |= S_NOATIME;
+ linuxflags |= NTFS_LINUXFLAGS_NOATIME;
+ } else {
+ linuxflags &= ~NTFS_LINUXFLAGS_NOATIME;
+ }
}
mutex_lock(&ni->mrec_lock);
@@ -265,7 +272,7 @@ int ntfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry,
if (err)
return err;
- inode_set_flags(vi, new_fl, S_IMMUTABLE | S_APPEND | S_NOATIME);
+ inode_set_flags(vi, new_fl, mask_fl);
inode_set_ctime_current(vi);
mark_inode_dirty(vi);
return 0;
--
2.56.0
^ permalink raw reply [flat|nested] 7+ messages in thread* [PATCH v3 5/5] docs: add fileattr API documentation
2026-11-29 2:42 [PATCH v3 0/5] Add f*_xflags_{rw,ro}_mask flags to the fileattr API koraynilay
` (3 preceding siblings ...)
2026-11-29 2:42 ` [PATCH v3 4/5] ntfs: switch to the new xflags fileattr API koraynilay
@ 2026-11-29 2:42 ` koraynilay
2026-11-29 2:47 ` [PATCH v3 0/5] Add f*_xflags_{rw,ro}_mask flags to the fileattr API koraynilay
5 siblings, 0 replies; 7+ messages in thread
From: koraynilay @ 2026-11-29 2:42 UTC (permalink / raw)
To: Namjae Jeon, Hyunchul Lee, Jonathan Corbet, Eric Biggers,
Theodore Y . Ts'o, Jaegeuk Kim, Alexander Viro,
Christian Brauner
Cc: Shuah Khan, Randy Dunlap, Chuck Lever, Jeff Layton, Jan Kara,
Alexander Aring, Darrick J . Wong, Andrey Albershteyn,
Amir Goldstein, Miklos Szeredi, Pali Rohár, Lauri Gates,
Christoph Hellwig, Dave Chinner, linux-btrfs, linux-xfs,
linux-kernel, linux-fsdevel, ntfs, linux-nfs, linux-api,
linux-doc, linux-unionfs, koraynilay
Add documentation for the fileattr API.
Signed-off-by: koraynilay <koray.fra@gmail.com>
---
Documentation/filesystems/fileattr.rst | 530 +++++++++++++++++++++++++
Documentation/filesystems/index.rst | 1 +
Documentation/filesystems/vfs.rst | 19 +-
include/uapi/linux/fs.h | 28 +-
4 files changed, 568 insertions(+), 10 deletions(-)
create mode 100644 Documentation/filesystems/fileattr.rst
diff --git a/Documentation/filesystems/fileattr.rst b/Documentation/filesystems/fileattr.rst
new file mode 100644
index 000000000000..9e3515dc4517
--- /dev/null
+++ b/Documentation/filesystems/fileattr.rst
@@ -0,0 +1,530 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+============
+Fileattr API
+============
+
+Overview
+========
+
+The fileattr API provides a modern way (instead of the older
+``FS_IOC_[GS]ETFLAGS`` and ``FS_IOC_FS[GS]ETXATTR`` ioctls) to query and modify inode
+flags (like immutable and append-only).
+
+Userspace API
+=============
+
+System Call Interface
+---------------------
+
+::
+
+ #include <linux/fcntl.h>
+ #include <linux/fs.h>
+ #include <sys/syscall.h>
+ #include <unistd.h>
+
+ long syscall(SYS_file_getattr, int dfd, const char *filename,
+ struct file_attr *attr, size_t usize,
+ unsigned int at_flags);
+
+ long syscall(SYS_file_setattr, int dfd, const char *filename,
+ struct file_attr *attr, size_t usize,
+ unsigned int at_flags);
+
+Since C library wrappers don't exist yet, file_getattr() and
+file_setattr() need to be called using :manpage:`syscall(2)`.
+
+Parameters
+----------
+
+``dfd``
+ Directory file descriptor for relative path lookups, ``AT_FDCWD`` for the
+ current working directory, or the target file descriptor when used with
+ ``AT_EMPTY_PATH``.
+
+``filename``
+ Target pathname (relative to ``dfd`` when not absolute).
+
+``attr``
+ Pointer to a struct file_attr buffer.
+
+``usize``
+ Size of the buffer pointed to by ``attr``, in bytes.
+
+``at_flags``
+ Bitwise OR of control flags:
+
+ - ``AT_FILEATTR_MASKS``: Enable mask semantics (as detailed in :ref:`Masks <masks>`).
+ - ``AT_SYMLINK_NOFOLLOW``: Do not follow symbolic links, operate on the
+ link itself.
+ - ``AT_EMPTY_PATH``: If ``filename`` is an empty string (``""`` or
+ ``NULL``), operate directly on ``dfd``.
+
+
+The struct file_attr Structure
+------------------------------
+
+.. kernel-doc:: include/uapi/linux/fs.h
+ :identifiers: file_attr
+
+.. _masks:
+
+Masks
+=====
+
+In 7.X, the fileattr API introduced ``_masks`` fields (``fa_xflags_rw_mask``
+and ``fa_xflags_ro_mask``), to enable usage of this API without having to do a
+Read-Modify-Write cycle like ``FS_IOC_SETFLAGS`` and ``FS_IOC_FSSETXATTR``
+require.
+
+To use this feature ``AT_FILEATTR_MASKS`` needs to be set in the
+``at_flags`` argument of the syscall, otherwise the normal behaviour is kept.
+
+.. note::
+ Currently most filesystems don't support masks, but this only has a visible
+ effect with file_getattr(), since file_setattr() is handled internally.
+
+On file_getattr()
+-----------------
+
+- If the filesystem supports mask reporting, file_getattr() populates
+ ``fa_xflags_rw_mask`` with all xflags it supports modifying on this inode,
+ and ``fa_xflags_ro_mask`` with all read-only xflags it supports reporting.
+- If the filesystem does not support masks, file_getattr() sets to ``0``
+ both ``fa_xflags_rw_mask`` and ``fa_xflags_ro_mask``.
+
+.. caution::
+
+ Passing the result of file_getattr() directly into file_setattr()
+ like this::
+
+ file_getattr(file1, &fa, ..., AT_FILEATTR_MASKS);
+ file_setattr(file2, &fa, ..., AT_FILEATTR_MASKS);
+
+ does not always work as-is because of the different semantics of
+ ``fa_xflags_rw_mask`` on get vs set:
+
+ - on filesystems without mask support, file_getattr() returns
+ ``fa_xflags_rw_mask = 0``, turning file_setattr() into a no-op (because of
+ the meaning of ``fa_xflags_rw_mask``);
+ - on filesystems with mask support, file_getattr() returns all supported
+ writable xflags in ``fa_xflags_rw_mask``, so file_setattr() sets all of
+ them on ``file2``, which could fail if ``file2`` is on a different
+ filesystem that supports a different subset of xflags.
+
+On file_setattr()
+-----------------
+
+- Userspace populates ``fa_xflags_rw_mask`` with the xflags it wishes to
+ change. If ``fa_xflags_rw_mask`` is ``0``, VFS treats it as a no-op, checking
+ only inode permissions and if the filesystem supports the fileattr API.
+- Any valid flag outside ``fa_xflags_rw_mask`` is ignored and left unmodified.
+- For boolean attributes:
+
+ - Setting the xflag bit in both ``fa_xflags_rw_mask`` and ``fa_xflags``
+ enables the feature.
+ - Setting the bit in ``fa_xflags_rw_mask`` and clearing the bit in
+ ``fa_xflags`` disables the feature.
+
+- For value attributes (``FS_XFLAG_PROJID``, ``FS_XFLAG_EXTSIZE``,
+ ``FS_XFLAG_EXTSZINHERIT``, ``FS_XFLAG_COWEXTSIZE``):
+
+ - Setting the flag bit in both ``fa_xflags_rw_mask`` and ``fa_xflags`` sets
+ the attribute from the corresponding struct field.
+ - Setting the flag bit in ``fa_xflags_rw_mask`` and clearing the bit in
+ ``fa_xflags`` unsets/clears the attribute (resetting the value to 0).
+
+- Validation and capability rules:
+
+ - If the filesystem supports masks, requesting modification of any flag not advertised in
+ the filesystem's writable mask fails with ``-EOPNOTSUPP``.
+ - If the filesystem does not support masks, requesting any flag outside
+ ``((FS_XFLAGS_MASK & ~FS_XFLAG_RDONLY_MASK) | FS_XFLAG_PROJID)`` fails
+ with ``-EOPNOTSUPP``.
+
+Usage Examples
+--------------
+
+Setting normal boolean flags::
+
+ struct file_attr fa = {
+ .fa_xflags = FS_XFLAG_NOATIME | FS_XFLAG_NODUMP,
+ .fa_xflags_rw_mask = FS_XFLAG_NOATIME | FS_XFLAG_NODUMP,
+ };
+ syscall(SYS_file_setattr, dfd, path, &fa, sizeof(fa), AT_FILEATTR_MASKS);
+
+Clearing both flags in one call::
+
+ struct file_attr fa = {
+ .fa_xflags = 0,
+ .fa_xflags_rw_mask = FS_XFLAG_NOATIME | FS_XFLAG_NODUMP,
+ };
+ syscall(SYS_file_setattr, dfd, path, &fa, sizeof(fa), AT_FILEATTR_MASKS);
+
+Setting one flag while clearing another::
+
+ struct file_attr fa = {
+ .fa_xflags = FS_XFLAG_NOATIME,
+ .fa_xflags_rw_mask = FS_XFLAG_NOATIME | FS_XFLAG_NODUMP,
+ };
+ syscall(SYS_file_setattr, dfd, path, &fa, sizeof(fa), AT_FILEATTR_MASKS);
+
+Setting a value flag (``FS_XFLAG_PROJID``)::
+
+ struct file_attr fa = {
+ .fa_xflags = FS_XFLAG_PROJID,
+ .fa_xflags_rw_mask = FS_XFLAG_PROJID,
+ .fa_projid = 67,
+ };
+ syscall(SYS_file_setattr, dfd, path, &fa, sizeof(fa), AT_FILEATTR_MASKS);
+
+Clearing/unsetting a value flag (``FS_XFLAG_PROJID``)::
+
+ struct file_attr fa = {
+ .fa_xflags = 0,
+ .fa_xflags_rw_mask = FS_XFLAG_PROJID,
+ };
+ syscall(SYS_file_setattr, dfd, path, &fa, sizeof(fa), AT_FILEATTR_MASKS);
+
+.. note::
+ Here, there is no need to also set ``fa.fa_projid = 0`` since the above
+ code already means "unset".
+
+.. _fileattr_flags:
+
+Flag Reference
+==============
+
++--------------------------------+------------+--------+-------------------------------------------------------+
+| Flag Macro | Hex Value | Mode | Description and Constraints |
++================================+============+========+=======================================================+
+| ``FS_XFLAG_REALTIME`` | 0x00000001 | RW | Realtime volume storage (XFS). |
++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``FS_XFLAG_PREALLOC`` | 0x00000002 | RO | File has preallocated extents. |
++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``FS_XFLAG_IMMUTABLE`` | 0x00000008 | RW | Inode cannot be modified. Requires |
+| | | | ``CAP_LINUX_IMMUTABLE``. |
++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``FS_XFLAG_APPEND`` | 0x00000010 | RW | All writes append only. Requires |
+| | | | ``CAP_LINUX_IMMUTABLE``. |
++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``FS_XFLAG_SYNC`` | 0x00000020 | RW | File writes are synchronous. |
++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``FS_XFLAG_NOATIME`` | 0x00000040 | RW | Do not update inode access time (atime). |
++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``FS_XFLAG_NODUMP`` | 0x00000080 | RW | Exclude file from dump/backup utilities. |
++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``FS_XFLAG_RTINHERIT`` | 0x00000100 | RW | Inherit realtime bit on newly created files |
+| | | | (directories only). |
++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``FS_XFLAG_PROJINHERIT`` | 0x00000200 | RW | Inherit project ID on newly created files |
+| | | | (directories only). |
+| | | | Updates require ``init_user_ns``. |
++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``FS_XFLAG_NOSYMLINKS`` | 0x00000400 | RW | Disallow creation of symlinks (directories only). |
++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``FS_XFLAG_EXTSIZE`` | 0x00000800 | RW | Extent size allocator hint (regular files only). |
++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``FS_XFLAG_EXTSZINHERIT`` | 0x00001000 | RW | Inherit extent size allocator hint |
+| | | | (directories only). |
++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``FS_XFLAG_NODEFRAG`` | 0x00002000 | RW | Disable filesystem defragmentation for file. |
++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``FS_XFLAG_FILESTREAM`` | 0x00004000 | RW | Enable filestream allocator (directories only; XFS). |
++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``FS_XFLAG_DAX`` | 0x00008000 | RW | Direct Access (DAX) I/O (regular files and |
+| | | | directories only); mutually exclusive with |
+| | | | ``FS_XFLAG_VERITY``. |
++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``FS_XFLAG_COWEXTSIZE`` | 0x00010000 | RW | Copy-on-Write extent size allocator hint |
+| | | | (regular files and directories only). |
++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``FS_XFLAG_VERITY`` | 0x00020000 | RO | fs-verity enabled, read-only (set via verity ioctl). |
++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``FS_XFLAG_CASEFOLD`` | 0x00040000 | RO/RW | Case-insensitive lookups. |
++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``FS_XFLAG_CASENONPRESERVING`` | 0x00080000 | RO | Case not preserved in directory lookups. |
++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``FS_XFLAG_PROJID`` | 0x00100000 | RW | Project ID value flag (Linux >= 7.X). |
+| | | | Updates require ``init_user_ns``. |
++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``FS_XFLAG_COMPR`` | 0x00200000 | RO/RW | Compressed file flag. |
++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``FS_XFLAG_ENCRYPT`` | 0x00400000 | RO | Encrypted file flag (fscrypt). Read-only. |
++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``FS_XFLAG_HASATTR`` | 0x80000000 | RO | Inode has extended attributes. Read-only. |
++--------------------------------+------------+--------+-------------------------------------------------------+
+
+
+Behaviour without ``AT_FILEATTR_MASKS``
+=======================================
+
+When not using ``AT_FILEATTR_MASKS``, file_getattr() and
+file_setattr() behave in the same way as :manpage:`ioctl_xfs_fsgetxattr(2)`
+and :manpage:`ioctl_xfs_fssetxattr(2)` respectively.
+
+In-Kernel Filesystem Interface
+==============================
+
+Filesystem Operations
+---------------------
+
+Filesystems wanting to implement this API, need to set the ``fileattr_get`` and
+``fileattr_set`` functions in ``struct inode_operations``:
+
+.. code-block:: c
+
+ struct inode_operations {
+ ...
+ int (*fileattr_get)(struct dentry *dentry, struct file_kattr *fa);
+ int (*fileattr_set)(const struct mnt_idmap *idmap, struct dentry *dentry,
+ struct file_kattr *fa);
+ ...
+ };
+
+Internal Structure: struct file_kattr
+-------------------------------------
+
+Defined in ``<linux/fileattr.h>``:
+
+.. code-block:: c
+
+ struct file_kattr {
+ u32 flags; /* FS_*_FL legacy flags */
+ u32 fsx_xflags; /* FS_XFLAG_* modern flags */
+ u32 fsx_xflags_rw_mask; /* Supported writable flags (get) / flags to set (set) */
+ u32 fsx_xflags_ro_mask; /* Supported read-only flags (get) */
+ u32 fsx_extsize; /* Extent size hint */
+ u32 fsx_nextents; /* Extent count */
+ u32 fsx_projid; /* Project ID */
+ u32 fsx_cowextsize; /* CoW extent size hint */
+ bool flags_valid:1; /* Legacy flags valid */
+ bool fsx_valid:1; /* Modern xflags valid */
+ bool has_masks:1; /* Mask API supported */
+ };
+
+Fields of struct file_kattr have the same semantics as their user-facing
+counterparts in struct file_attr above.
+In addition, struct file_kattr has:
+
+``flags``
+ Bit mask of the ``FS_*_FL`` flags, used by ``FS_IOC_GETFLAGS`` and ``FS_IOC_SETFLAGS``.
+
+``flags_valid`` / ``fsx_valid``
+ Selector indicating whether the field to be used is ``flags`` or
+ ``fsx_xflags``. They should be treated as mutually exclusive.
+
+``has_masks``
+ Selector indicating whether the filesystem (on get) or request (on set)
+ uses the mask API.
+
+VFS Helpers
+-----------
+
+When implementing ``->fileattr_get()``, filesystems can populate
+struct file_kattr using either fileattr_fill_xflags() or
+fileattr_fill_flags()::
+
+ void fileattr_fill_xflags(struct file_kattr *fa, u32 xflags);
+
+ void fileattr_fill_flags(struct file_kattr *fa, u32 flags);
+
+fileattr_fill_xflags() sets ``fa->fsx_valid`` to ``true``,
+copies ``xflags`` into ``fa->fsx_xflags`` and translates the passed
+``FS_XFLAG_*`` xflags to their corresponding ``FS_*_FL`` flags into
+``fa->flags``.
+
+fileattr_fill_flags() sets ``fa->flags_valid`` to ``true``, copies ``flags``
+into ``fa->flags`` and translates the passed ``FS_*_FL`` flags to their
+corresponding ``FS_XFLAG_*`` xflags into ``fa->fsx_xflags``.
+
+The table below lists all ``FS_*_FL`` and ``FS_XFLAG_*`` flags and their
+corresponding translations:
+
++------------------------+--------------------------------+
+| ``FS_*_FL`` | ``FS_XFLAG_*`` |
++========================+================================+
+| ``FS_IMMUTABLE_FL`` | ``FS_XFLAG_IMMUTABLE`` |
++------------------------+--------------------------------+
+| ``FS_APPEND_FL`` | ``FS_XFLAG_APPEND`` |
++------------------------+--------------------------------+
+| ``FS_SYNC_FL`` | ``FS_XFLAG_SYNC`` |
++------------------------+--------------------------------+
+| ``FS_NOATIME_FL`` | ``FS_XFLAG_NOATIME`` |
++------------------------+--------------------------------+
+| ``FS_NODUMP_FL`` | ``FS_XFLAG_NODUMP`` |
++------------------------+--------------------------------+
+| ``FS_DAX_FL`` | ``FS_XFLAG_DAX`` |
++------------------------+--------------------------------+
+| ``FS_PROJINHERIT_FL`` | ``FS_XFLAG_PROJINHERIT`` |
++------------------------+--------------------------------+
+| ``FS_VERITY_FL`` | ``FS_XFLAG_VERITY`` |
++------------------------+--------------------------------+
+| ``FS_CASEFOLD_FL`` | ``FS_XFLAG_CASEFOLD`` |
++------------------------+--------------------------------+
+| ``FS_COMPR_FL`` | ``FS_XFLAG_COMPR`` |
++------------------------+--------------------------------+
+| ``FS_ENCRYPT_FL`` | ``FS_XFLAG_ENCRYPT`` |
++------------------------+--------------------------------+
+| ``FS_SECRM_FL`` | |
++------------------------+--------------------------------+
+| ``FS_UNRM_FL`` | |
++------------------------+--------------------------------+
+| ``FS_DIRTY_FL`` | |
++------------------------+--------------------------------+
+| ``FS_COMPRBLK_FL`` | |
++------------------------+--------------------------------+
+| ``FS_NOCOMP_FL`` | |
++------------------------+--------------------------------+
+| ``FS_BTREE_FL`` / | |
+| ``FS_INDEX_FL`` | |
++------------------------+--------------------------------+
+| ``FS_IMAGIC_FL`` | |
++------------------------+--------------------------------+
+| ``FS_JOURNAL_DATA_FL`` | |
++------------------------+--------------------------------+
+| ``FS_NOTAIL_FL`` | |
++------------------------+--------------------------------+
+| ``FS_DIRSYNC_FL`` | |
++------------------------+--------------------------------+
+| ``FS_TOPDIR_FL`` | |
++------------------------+--------------------------------+
+| ``FS_HUGE_FILE_FL`` | |
++------------------------+--------------------------------+
+| ``FS_EXTENT_FL`` | |
++------------------------+--------------------------------+
+| ``FS_EA_INODE_FL`` | |
++------------------------+--------------------------------+
+| ``FS_EOFBLOCKS_FL`` | |
++------------------------+--------------------------------+
+| ``FS_NOCOW_FL`` | |
++------------------------+--------------------------------+
+| ``FS_INLINE_DATA_FL`` | |
++------------------------+--------------------------------+
+| ``FS_RESERVED_FL`` | |
++------------------------+--------------------------------+
+| | ``FS_XFLAG_REALTIME`` |
++------------------------+--------------------------------+
+| | ``FS_XFLAG_PREALLOC`` |
++------------------------+--------------------------------+
+| | ``FS_XFLAG_RTINHERIT`` |
++------------------------+--------------------------------+
+| | ``FS_XFLAG_NOSYMLINKS`` |
++------------------------+--------------------------------+
+| | ``FS_XFLAG_EXTSIZE`` |
++------------------------+--------------------------------+
+| | ``FS_XFLAG_EXTSZINHERIT`` |
++------------------------+--------------------------------+
+| | ``FS_XFLAG_NODEFRAG`` |
++------------------------+--------------------------------+
+| | ``FS_XFLAG_FILESTREAM`` |
++------------------------+--------------------------------+
+| | ``FS_XFLAG_COWEXTSIZE`` |
++------------------------+--------------------------------+
+| | ``FS_XFLAG_CASENONPRESERVING`` |
++------------------------+--------------------------------+
+| | ``FS_XFLAG_PROJID`` |
++------------------------+--------------------------------+
+| | ``FS_XFLAG_HASATTR`` |
++------------------------+--------------------------------+
+
+Drivers that only support ``FS_*_FL`` flags can check for unsupported
+extended attributes in ``->fileattr_set()`` using fileattr_has_fsx()::
+
+ bool fileattr_has_fsx(const struct file_kattr *fa);
+
+fileattr_has_fsx() returns ``true`` if ``fa->fsx_valid`` is set with any
+xflags outside ``FS_XFLAG_COMMON`` or with non-zero ``fsx_extsize``,
+``fsx_projid``, or ``fsx_cowextsize``.
+
+VFS Translation Helpers
+-----------------------
+
+With fileattr_set_convert(), the VFS bridges differing
+caller and driver capabilities automatically using the inode's current
+attributes retrieved via vfs_fileattr_get():
+
+- fileattr_set_rmw_to_mask(): Converts Read-Modify-Write requests
+ (``FS_IOC_SETFLAGS``, ``FS_IOC_FSSETXATTR``, or file_setattr() without
+ ``AT_FILEATTR_MASKS``) to a request with masks when targeting a filesystem
+ that supports them. It does RMW itself, computing which flags and value
+ attributes changed, then populates ``fa->fsx_xflags`` and
+ ``fa->fsx_xflags_rw_mask`` accordingly.
+- fileattr_set_mask_to_rmw(): Converts masked requests (file_setattr() with
+ ``AT_FILEATTR_MASKS``) to Read-Modify-Write format when targeting a
+ filesystem that doesn't support masks. It rejects changes to read-only or
+ unsupported flags and then merges the new values with the current ones,
+ preserving unchanged flags and clearing the masks.
+- flags_to_xflags() / xflags_to_flags(): Convert between ``FS_XFLAG_*`` xflags
+ and ``FS_*_FL`` flags using fileattr_fill_xflags() and fileattr_fill_flags()
+ and returning only the new bitmask.
+
+Mask-Aware Driver Example
+-------------------------
+
+An example of a filesystem that supports masks can be found in ``fs/ntfs/file.c``.
+
+Locking
+-------
+
+- vfs_fileattr_set() takes inode_lock(), a vfs_fileattr_set_locked() will be
+ added for cases where inode lock is already held.
+
+Permissions
+-----------
+
+- Inode ownership or ``CAP_FOWNER`` in the owning user namespace is required.
+- Modifying ``FS_XFLAG_IMMUTABLE`` or ``FS_XFLAG_APPEND`` requires
+ ``CAP_LINUX_IMMUTABLE`` in the initial user namespace.
+- Modifying ``FS_XFLAG_PROJID`` (when ``fsx_projid`` changes) or
+ ``FS_XFLAG_PROJINHERIT`` requires the caller to reside in the initial user
+ namespace.
+
+Error Codes
+===========
+
+``-EINVAL``
+ Invalid structure size, ``AT_FILEATTR_MASKS`` passed with non-mask struct,
+ non-zero masks passed to file_setattr() without ``AT_FILEATTR_MASKS``,
+ unknown flags in ``at_flags``, unknown bits in ``fa_xflags`` or
+ ``fa_xflags_rw_mask`` (including ``FS_XFLAG_PROJID`` without
+ ``AT_FILEATTR_MASKS``), invalid inode type for ``FS_XFLAG_EXTSIZE`` /
+ ``FS_XFLAG_EXTSZINHERIT`` / ``FS_XFLAG_COWEXTSIZE`` / ``FS_XFLAG_DAX``,
+ incompatible attribute combinations (e.g. DAX on
+ fs-verity), invalid project ID or
+ changing ``FS_XFLAG_PROJID`` / ``FS_XFLAG_PROJINHERIT`` outside
+ the host namespace.
+
+``-EOPNOTSUPP``
+ The filesystem does not implement ``fileattr`` operations for this inode,
+ or userspace requested modification of an attribute not writable by the
+ filesystem.
+
+``-EPERM``
+ Insufficient permissions (missing ``CAP_FOWNER``, missing
+ ``CAP_LINUX_IMMUTABLE``, filesystem-enforced immutability constraints).
+
+``-EROFS``
+ Target inode resides on a read-only mount or filesystem.
+
+``-ENOKEY``
+ Required encryption key is unavailable (e.g. enabling ``FS_XFLAG_CASEFOLD``
+ on an encrypted directory without its key).
+
+``-E2BIG``
+ ``usize`` exceeds ``PAGE_SIZE``, or trailing bytes in ``attr`` are non-zero
+ when ``usize > FILE_ATTR_SIZE_LATEST`` on file_setattr().
+
+``-EBADF``
+ ``dfd`` is not a valid file descriptor.
+
+``-EFAULT``
+ Invalid user memory pointer (including ``filename == NULL`` without
+ ``AT_EMPTY_PATH``).
+
+``-ENOENT``
+ Target file does not exist, or empty path without ``AT_EMPTY_PATH``.
+
+``-EACCES`` / ``-ENOTDIR`` / ``-ELOOP`` / ``-ENAMETOOLONG``
+ Pathname resolution errors.
diff --git a/Documentation/filesystems/index.rst b/Documentation/filesystems/index.rst
index a556084c8858..71a1ae852e5b 100644
--- a/Documentation/filesystems/index.rst
+++ b/Documentation/filesystems/index.rst
@@ -28,6 +28,7 @@ algorithms work.
dnotify
fiemap
files
+ fileattr
locks
mmap_prepare
multigrain-ts
diff --git a/Documentation/filesystems/vfs.rst b/Documentation/filesystems/vfs.rst
index 00fbcd549f15..e222d5025fe4 100644
--- a/Documentation/filesystems/vfs.rst
+++ b/Documentation/filesystems/vfs.rst
@@ -621,16 +621,19 @@ otherwise noted.
the end.
``fileattr_get``
- called on ioctl(FS_IOC_GETFLAGS) and ioctl(FS_IOC_FSGETXATTR) to
- retrieve miscellaneous file flags and attributes. Also called
- before the relevant SET operation to check what is being changed
- (in this case with i_rwsem locked exclusive). If unset, then
- fall back to f_op->ioctl().
+ called on ioctl(FS_IOC_GETFLAGS), ioctl(FS_IOC_FSGETXATTR) and
+ file_getattr(2) to retrieve miscellaneous file flags and attributes
+ (see Documentation/filesystems/fileattr.rst). Also called before the
+ relevant SET operation to check what is being changed (in this case
+ with i_rwsem locked exclusive). If unset, then fall back to
+ f_op->ioctl().
``fileattr_set``
- called on ioctl(FS_IOC_SETFLAGS) and ioctl(FS_IOC_FSSETXATTR) to
- change miscellaneous file flags and attributes. Callers hold
- i_rwsem exclusive. If unset, then fall back to f_op->ioctl().
+ called on ioctl(FS_IOC_SETFLAGS), ioctl(FS_IOC_FSSETXATTR) and
+ file_setattr(2) to change miscellaneous file flags and attributes (see
+ Documentation/filesystems/fileattr.rst). Callers hold i_rwsem
+ exclusive. If unset, then fall back to f_op->ioctl().
+
``get_offset_ctx``
called to get the offset context for a directory inode. A
filesystem must define this operation to use
diff --git a/include/uapi/linux/fs.h b/include/uapi/linux/fs.h
index c1c487d07d29..5a58e1029bf8 100644
--- a/include/uapi/linux/fs.h
+++ b/include/uapi/linux/fs.h
@@ -225,9 +225,33 @@ struct fsxattr {
unsigned char fsx_pad[8];
};
-/*
- * Variable size structure for file_[sg]et_attr().
+/**
+ * struct file_attr - Variable size structure for file_getattr() / file_setattr().
+ * @fa_xflags: Bit mask of zero or more FS_XFLAG_* file attribute flags to be
+ * set/unset (in) or currently enabled (out) on the inode. See
+ * :ref:`Flag Reference <fileattr_flags>` for a list of flags.
+ * @fa_extsize: Extent size allocator hint in bytes, value of the
+ * FS_XFLAG_EXTSIZE/FS_XFLAG_EXTSZINHERIT xflag. This value
+ * suggests a preferred extent size for new allocations. (in/out)
+ * @fa_nextents: Number of data extents in the file, read-only. Filled in by
+ * file_getattr(2) and ignored by file_setattr(2). (out)
+ * @fa_projid: Project identifier, value of FS_XFLAG_PROJID. Used by quota
+ * systems quotactl(2) to group related files. (in/out)
+ * @fa_cowextsize: Copy-on-Write (CoW) extent size hint in bytes, value of
+ * FS_XFLAG_COWEXTSIZE. This value suggests a preferred extent
+ * size for CoW operations. (in/out)
+ * @fa_xflags_rw_mask: Bit mask of writeable xflags supported by the filesystem
+ * (out). Bit mask of xflags that need to be set to their
+ * corresponding on/off state in @fa_xflags and, if present,
+ * to their @fa_* value (in) ("flags that userspace wants
+ * to change"). See :ref:`Masks <masks>` for details.
+ * @fa_xflags_ro_mask: Bit mask of read-only xflags supported by the filesystem
+ * (out). See :ref:`Masks <masks>` for details.
*
+ * Note: @fa_xflags_rw_mask and @fa_xflags_ro_mask need the
+ * ``AT_FILEATTR_MASKS`` at_flag to be used.
+ */
+/*
* Note. This is alternative to the structure 'struct file_kattr'/'struct fsxattr'.
* As this structure is passed to/from userspace with its size, this can
* be versioned based on the size.
--
2.56.0
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: [PATCH v3 0/5] Add f*_xflags_{rw,ro}_mask flags to the fileattr API
2026-11-29 2:42 [PATCH v3 0/5] Add f*_xflags_{rw,ro}_mask flags to the fileattr API koraynilay
` (4 preceding siblings ...)
2026-11-29 2:42 ` [PATCH v3 5/5] docs: add fileattr API documentation koraynilay
@ 2026-11-29 2:47 ` koraynilay
5 siblings, 0 replies; 7+ messages in thread
From: koraynilay @ 2026-11-29 2:47 UTC (permalink / raw)
To: koraynilay, Namjae Jeon, Hyunchul Lee, Jonathan Corbet,
Eric Biggers, Theodore Y . Ts'o, Jaegeuk Kim, Alexander Viro,
Christian Brauner
Cc: Shuah Khan, Randy Dunlap, Chuck Lever, Jeff Layton, Jan Kara,
Alexander Aring, Darrick J . Wong, Andrey Albershteyn,
Amir Goldstein, Miklos Szeredi, Pali Rohár, Lauri Gates,
Christoph Hellwig, Dave Chinner, linux-btrfs, linux-xfs,
linux-kernel, linux-fsdevel, ntfs, linux-nfs, linux-api,
linux-doc, linux-unionfs
[-- Attachment #1: Type: text/plain, Size: 284 bytes --]
...
> [4]: https://koraynilay.neocities.org/linuxdocs/fileattr
And of course I had to forget something.
https://koraynilay.neocities.org/vfs-masks/fileattr
^^^^^^^^^ correct link for [4].
> [5]: https://koraynilay.neocities.org/vfs-masks/overlayfs-fileattr-bugs
...
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 228 bytes --]
^ permalink raw reply [flat|nested] 7+ messages in thread