Skip to contentSkip to conversation

Add f*_xflags_{rw,ro}_mask flags to the fileattr API

PATCH In review koraynilay 4 participants 47 messages Nov 29 – Dec 1

Subsystems (9)
lore
Share & tools
lore

koraynilay

author
#0 lore raw

v3 cover Add f*_xflags_{rw,ro}_mask flags to the fileattr API

This is a follow up patch series stemmed from [1] and [2], splitting the _mask change for the fileattr API into its own separate patch series.

Patch 1 is a preparatory cleanup so that the code in the later patches can be formatted better.

Patch 2 is the actual implementation, which introduces:
- fa_xflags_{rw,ro}_mask fields to struct file_attr
- fsx_xflags_{rw,ro}_mask fields and has_masks selector to struct file_kattr
- AT_FILEATTR_MASKS at_flag for file_[gs]etattr()

These masks have the following semantics:
- rw_mask:
* for filesystems: advertise which xflags they support changing
* for userspace: specify which xflags it wants to modify
- ro_mask:
* for filesystems: advertise which xflags they support only reading
* for userspace: ignored

(More details available in Patch 5)

This allows userspace to not have to do a RMW cycle when using the file_[gs]etattr() syscalls, avoiding possible race conditions and simplifying their usage.

This patch also introduces the missing FS_XFLAG_PROJID flag so that programs like chattr can leverage the new mask API. FS_XFLAG_PROJID is kept out of the legacy FS_XFLAGS_MASK and so it's defined as part of a new FS_FILE_ATTR_XFLAGS_MASK group of xflags that will be used moving forward for any new xflag (like the one proposed in [2]).

Patch 3 adds FS_XFLAG_COMPR and FS_XFLAG_ENCRYPT (as FS_COMPR_FL and FS_ENCRYPT_FL counterparts), which are needed for Patch 4. This means that old programs will also get these new flags, but that should be fine since xflags were made to be expandable.

Patch 4 converts fs/ntfs to this new API, to show how it can benefit filesystem drivers too.

Patch 5 adds the Documentation/filesystems/fileattr.rst page that explains well how this API can be used by both userspace and by filesystems and the various semantics.
(It doesn't go into *too much* detail though, since that would mean writing the code itself).
The rendered page can be found at [4].

The series has been tested with custom xfstests [3] (generic/816-835, xfs/611, xfs/673) across NTFS, ext4 (project quotas), XFS (project quotas and extent size hints), Btrfs, and F2FS.
Note: these tests were written by gemini and not manually checked, before sending them upstream (if needed) I'll have to review/rewrite them manually.

IMPORTANT NOTE: there are quite a few bugs with overlayfs (generic/832-835 above) when using a masks filesystem, I haven't investigated them deeply yet since I would first need to understand a lot more how overlayfs works, but the robot's report is in [5].

v3 changes:
- refactor the big if/else in vfs_fileattr_set() to a fileattr_set_convert() helper and simplify (hopefully?) its code;
- drop fscrypt flags -> xflags patch and just use xflags_to_flags() in vfs;
- add FS_XFLAG_COMPR/ENCRYPT in a separate patch instead of in the ntfs one ([6]);
- move FILE_ATTR_SIZE_VER* variables from the uapi header to the internal header ([7]);
- rename AT_FILE_ATTR_MASKS to AT_FILEATTR_MASKS ([8]);
- change "legacy" references to "rmw" references in both comments and helper names ([8]);
- add docs.

v2 changes:
- make fa_xflags_{rw,ro}_mask in struct file_attr __u64;
- add AT_FILE_ATTR_MASKS to opt in to mask semantics instead of needing unreliable heuristics on usize and mask values;
- reject intermediate sizes between VER0 and VER1;
- reject non-zero masks on SET without AT_FILE_ATTR_MASKS;
- add patch 2/4 for fscrypt_prepare_setflags;
- add xflags_to_flags() / flags_to_xflags() and fileattr_set_{legacy_to_mask,mask_to_legacy}() to translate between legacy RMW callers/filesystems and _mask callers/filesystems;
- clamp fa->fsx_xflags to rw_user_mask in file_attr_to_fileattr();
- update fileattr_set_prepare() to validate capabilities and projid changes against rw_user_mask / xflags;
- move the rw_mask == 0 early-return no-op from file_setattr() into vfs_fileattr_set();
- put FS_XFLAG_COMPR in FS_XFLAG_RDONLY_MASK only (keep FS_XFLAG_ENCRYPT in both FS_XFLAG_COMMON and FS_XFLAG_RDONLY_MASK);
- update ntfs_fileattr_set() to apply only bits selected in fsx_xflags_rw_mask;

Thanks,
koraynilay

[1]: https://lore.kernel.org/CAOQ4uxg8aVcWyNiFDk8djdn-M3xJcu_7-kRxVq9jjFsbcGp1ew@mail.gmail.com/
[2]: https://lore.kernel.org/CAOQ4uxjVQ9cjV=_jBjQtNg9zCuWUEJT9KNYMKV+1jwBkHen5OA@mail.gmail.com/
[3]: https://github.com/koraynilay/xfstests/tree/patches-in-queue
[4]: https://koraynilay.neocities.org/linuxdocs/fileattr
[5]: https://koraynilay.neocities.org/vfs-masks/overlayfs-fileattr-bugs
[6]: https://lore.kernel.org/CAOQ4uxgJjy3TaORBkMOdef-JvzX2uA5K2sbMDEcLqmpkedsESw@mail.gmail.com/
[7]: https://lore.kernel.org/CAOQ4uxi6oyFj_A9nPbgGeSufGo11L2N=QxLi2DW=ZKy25YQ5Tw@mail.gmail.com/
[8]: https://lore.kernel.org/CAOQ4uxhA+93bo5WseLxe7cAvJHJ8eQodoQHLwh=iToyEVkeXEA@mail.gmail.com/

koraynilay (5):
vfs: use goto out on error in vfs_fileattr_set()
vfs: add xflags masks to fileattr API
vfs: add COMPR/ENCRYPT xflags from FS_COMPR_FL/FS_ENCRYPT_FL
ntfs: switch to the new xflags fileattr API
docs: add fileattr API documentation

 Documentation/filesystems/fileattr.rst | 530 +++++++++++++++++++++++++
 Documentation/filesystems/index.rst    |   1 +
 Documentation/filesystems/vfs.rst      |  19 +-
 fs/file_attr.c                         | 379 +++++++++++++++---
 fs/ntfs/file.c                         | 109 ++---
 include/linux/fileattr.h               |  19 +-
 include/uapi/linux/fcntl.h             |   6 +
 include/uapi/linux/fs.h                |  37 +-
 8 files changed, 985 insertions(+), 115 deletions(-)
 create mode 100644 Documentation/filesystems/fileattr.rst

base-commit: 3857587d9d6fa44d40d1c73bde6a39f6f7a5216c

Signature · 1 line
2.56.0
author
#1 lore raw

PATCH v3 1/5 vfs: use goto out on error in vfs_fileattr_set()

Switch from using if (!err) { ... } to the cleaner if (err) goto out; in the main body of vfs_fileattr_set().

---
 fs/file_attr.c | 45 +++++++++++++++++++++++----------------------
 1 file changed, 23 insertions(+), 22 deletions(-)
fs/file_attr.c +23 -22
index 833e03810b24..40d6518f1d8e 100644
@@ -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;
Signature · 1 line
2.56.0
author
#4 lore raw

PATCH v3 2/5 vfs: add xflags masks to fileattr API

In reply to 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
---
 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(-)
fs/file_attr.c +310 -36
index 40d6518f1d8e..5d3834f4405b 100644
@@ -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),
include/linux/fileattr.h +12 -0
index 09e32b84e02a..fb158269b4ea 100644
@@ -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);
include/uapi/linux/fcntl.h +6 -0
index e43e3de3e9ee..9af205794404 100644
@@ -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 */
include/uapi/linux/fs.h +4 -3
index a13c7b32a1bd..9834f6eee000 100644
@@ -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
Signature · 1 line
2.56.0
author
#28 lore raw

PATCH v3 3/5 vfs: add COMPR/ENCRYPT xflags from FS_COMPR_FL/FS_ENCRYPT_FL

In reply to 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.

Assisted-by Gemini:3.8-flash-high antigravity-cli-1.5.11
---
 fs/file_attr.c           | 8 ++++++++
 include/linux/fileattr.h | 7 ++++---
 include/uapi/linux/fs.h  | 2 ++
 3 files changed, 14 insertions(+), 3 deletions(-)
fs/file_attr.c +8 -0
index 5d3834f4405b..314d779e41d6 100644
@@ -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);
 
include/linux/fileattr.h +4 -3
index fb158269b4ea..9a8de4190133 100644
@@ -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 \
include/uapi/linux/fs.h +2 -0
index 9834f6eee000..c1c487d07d29 100644
@@ -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	*/
 
Signature · 1 line
2.56.0
author
#33 lore raw

PATCH v3 4/5 ntfs: switch to the new xflags fileattr API

In reply to 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
---
 fs/ntfs/file.c | 109 ++++++++++++++++++++++++++-----------------------
 1 file changed, 58 insertions(+), 51 deletions(-)
fs/ntfs/file.c +58 -51
index 50be49f4c913..34acf9333c41 100644
@@ -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;
Signature · 1 line
2.56.0
author
#39 lore raw

PATCH v3 5/5 docs: add fileattr API documentation

In reply to koraynilay

Add documentation for the fileattr API.

---
 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
Documentation/filesystems/fileattr.rst new file +530 -0
new file mode 100644
index 000000000000..9e3515dc4517
@@ -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.
Documentation/filesystems/index.rst +1 -0
index a556084c8858..71a1ae852e5b 100644
@@ -28,6 +28,7 @@ algorithms work.
    dnotify
    fiemap
    files
+   fileattr
    locks
    mmap_prepare
    multigrain-ts
Documentation/filesystems/vfs.rst +11 -8
index 00fbcd549f15..e222d5025fe4 100644
@@ -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
include/uapi/linux/fs.h +26 -2
index c1c487d07d29..5a58e1029bf8 100644
@@ -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.
Signature · 1 line
2.56.0
#34 lore raw

Re: [PATCH v3 4/5] ntfs: switch to the new xflags fileattr API

In reply to koraynilay
Quoted 5 lines · 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

You know you are not required to add this tag anymore. Some vfs maintainers would prefer that rather than specifying the model and version of LLM to describe the how/what you used LLM in cover letter or notes, which you already partly did.

I know how much extensive personal effort you put into this patch set and for me this work is a great example of human+LLM synergy, but new reviewers don't know that if the scope of "Assisted-by" is not spelled out.

So my recommendation is to drop it and add a small section in the cover letter about the essential role of LLM in auditing all the filesystems and all the possible UAPI compatibility issues with legacy UAPI and internal vfs api.

Otherwise, this is a very clean patch and very nice reference example of how to convert a filesystem to the new masks api.

Some minor comments below

Quoted 73 lines
---
 fs/ntfs/file.c | 109 ++++++++++++++++++++++++++-----------------------
 1 file changed, 58 insertions(+), 51 deletions(-)
fs/ntfs/file.c +19 -14
index 50be49f4c913..34acf9333c41 100644
@@ -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;
 

fs/ntfs/file.c int ntfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry, · line 223

Quoted 5 lines
fs/ntfs/file.c +0 -2
@@ -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;

Something about this feels wrong, meaning that the filesystem should protect itself
from getting called with the wrong fileattr and not fully rely on vfs to do that.

This over trust is probably part of the problems you see with overlayfs and also creates problems with ksmbd over ntfs.

We discussed at some point a replacement for this helper - I am not sure what exactly it should be called or exactly doing and maybe it even needs to WARN_ON_ONCE() if an internal caller passes unexpected fa format, because this function now expects a certain fa format.

Quoted 5 lines
fs/ntfs/file.c +0 -5
-
-       /* chattr passes the unchanged read-only flags back to us too. */
-       if (NInoCompressed(ni) || NInoWofCompressed(ni))
-               readonly |= FS_COMPR_FL;
-       if (NInoEncrypted(ni))

fs/ntfs/file.c int ntfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry, · line 238

Quoted 10 lines
fs/ntfs/file.c +0 -10
-               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;

It is very surprising to see this hunk removed when reviewing the series backwards ;)

A commit should explain itself without relying on the reviewer keeping context from previous patches, so the commit message needs to explain why those protections were removed.

Quoted 50 lines
fs/ntfs/file.c +30 -18
-
-       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;

fs/ntfs/file.c int ntfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry, · line 267

Quoted 10 lines
fs/ntfs/file.c +8 -1
-               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;
+               }
        }

It's a bummer that email review has so much diffstat for what is essentially indentation. Sometimes we need to accept that but I have a proposal If you like it, maybe the diffstat will become even smaller.

      if (rw_user_mask & FS_XFLAG_IMMUTABLE) {
              mask_fl |= S_IMMUTABLE;
              linuxflags_mask |= NTFS_LINUXFLAGS_IMMUTABLE;
      }
      if (fa->fsx_xflags & FS_XFLAG_IMMUTABLE) {

[ no change here and no else ]

...

        err = ntfs_ea_set_linuxflags(vi, linuxflags & ~linuxflags_mask);

consistent with inode_set_flags(vi, new_fl, mask_fl);

Whether fa->fsx_xflags check should be logically in the nested scope is a matter of taste. I personally like better the smaller patch which is easier to review, but it is not a must.

Using linuxflags_mask instead of the else is definitely better IMO, because it makes S_ flags and LINUXFLAGS handling consistent.

WDYT?

Thanks,
Amir.

Reviewed-by on v3 1/5
#2 lore raw

Re: [PATCH v3 1/5] vfs: use goto out on error in vfs_fileattr_set()

In reply to koraynilay
Quoted 6 lines · koraynilay

Switch from using if (!err) { ... } to the cleaner if (err) goto out; in the main body of vfs_fileattr_set().

A note about RVB/S-o-b order.
This is not a rule and not universally followed practice, but for me S-o-b is signing the entire contact of the commit body and commit message above the S-o-b which includes the RVB collected by you.

If this was a standalone patch and Christian would be picking it up to vfs tree based on our RVB, the history records will show
S-o-b: koraynilay
RVB: aalbersh
RVB: amir73il
S-o-b: brauner

But if you posted v4 first and Christian would pick the series later history records would show

RVB: aalbersh
RVB: amir73il
S-o-b: koraynilay
S-o-b: brauner

I think this is a more accurate representation of the change record.

This is definitely not critical at all, but since you are developing your developer SKILLs, I thought this was worth mentioning.

And also if it was not clear:
- You never need to post another revision of a patch set if all that changed in RVBs. This is something that the maintainers pick up when applying the patch set
- Please take care to carry RVBs yourself in following revisions, as a sign for reviewers that nothing has changed since their last review
- Please do NOT remove RVB for minor changes!
This rule requires judgement and common sense and the definition of "minor changes" may very well depend on the reviewer, so if unsure you can ask, or better yet remove RVB and write in notes after --- what changed since vX, so the re-review becomes less of a burden

Thanks,
Amir.

Quoted 65 lines
---
 fs/file_attr.c | 45 +++++++++++++++++++++++----------------------
 1 file changed, 23 insertions(+), 22 deletions(-)
fs/file_attr.c +23 -23
index 833e03810b24..40d6518f1d8e 100644
@@ -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

#29 lore raw

Re: [PATCH v3 3/5] vfs: add COMPR/ENCRYPT xflags from FS_COMPR_FL/FS_ENCRYPT_FL

In reply to koraynilay
Quoted 1 line · koraynilay

"vfs: add COMPR/ENCRYPT xflags from FS_COMPR_FL/FS_ENCRYPT_FL"

This is not the best description of this change.

A better description would be

"vfs: advertise COMPR/ENCRYPT xflags in file_getattr()"

Which is exactly the behavior change that I asked you to spell out. You spelled it out in the cover letter, but not here.

The commit message should also mention FS_IOC_FSGETXATTR but for brevity the subject above is enough.

Quoted 7 lines

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.

This sentence is very hard to understand, because it is missing some important information.
The CASEFOLD flag (and now COMPR) is only read-write via the legacy SETFLAGS ioctl and read-only via the xflag apis.
Also in your documentation patch, you list them as RO/RW which does not fully convey this meaning.
Don't forget why you started this patch set - BTIME is also going to be RO on some fs and RW on others, but in a very different category than those two.

Quoted 4 lines

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.

... in all the uapi flavors.

Quoted 3 lines

Also update vfs fileattr_fill_* helpers to convert these two flags as well.

That's backwards. It's not the "Also" it's the main dish, as my comment about the patch subject implies.
There is no "Also" in this patch - it does one thing.

Thanks,
Amir.

Quoted 50 lines
Assisted-by Gemini:3.8-flash-high antigravity-cli-1.5.11
---
 fs/file_attr.c           | 8 ++++++++
 include/linux/fileattr.h | 7 ++++---
 include/uapi/linux/fs.h  | 2 ++
 3 files changed, 14 insertions(+), 3 deletions(-)
fs/file_attr.c +8 -0
index 5d3834f4405b..314d779e41d6 100644
@@ -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);
 
include/linux/fileattr.h +1 -1
index fb158269b4ea..9a8de4190133 100644
@@ -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 | \

include/linux/fileattr.h line 27

Quoted 10 lines
include/linux/fileattr.h +3 -2
-        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)
 

The addition of FS_XFLAG_CASEFOLD in FS_XFLAG_RDONLY_MASK and not in FS_XFLAG_COMMON left the code in a very fragile undocumented state which took me personally a lot of time to figure out.

Let's please document the meaning of these macros better. I propose something like:

/*
* FS_COMMON_FL/FS_XFLAG_COMMON are preserved through RMW
* cycle via either uapi flavor.
* They may NOT include flags which are read-write via one api flavor and
* read-only in another (e.g. CASEFOLD, COMPR).
*/
#define FS_XFLAG_COMMON \
...

/*
* Read-only xflags via FS_IOC_FS[GS]ETXATTR and file_[gs]etattr().
* The respective flags may be read-write via the FS_IOC_SETFLAGS ioctl.
*/
#define FS_XFLAG_RDONLY_MASK \

Thanks,
Amir.

#40 lore raw

Re: [PATCH v3 5/5] docs: add fileattr API documentation

In reply to koraynilay
Quoted 21 lines · koraynilay

Add documentation for the fileattr API.

---
 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
Documentation/filesystems/fileattr.rst new file +3 -0
new file mode 100644
index 000000000000..9e3515dc4517
@@ -0,0 +1,530 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+============

Documentation/filesystems/fileattr.rst line 13

Quoted 10 lines
Documentation/filesystems/fileattr.rst new file +10 -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).
+

This is nice!
Much easier to review the UAPI this way.

I would add something about the terminology "file attributes" which was a source of confusion and bikeshedding and the relation to other UAPIs:

The "file attributes" that this document refers to are structured file attributes
much like the inode information advertised by stat(2) and statx(2). statx(2) even advertises some of the file attributes described in this document:
File attributes
The stx_attributes field contains a set of ORed flags that
indicate additional attributes of the file.

The file attributes described in this document could be described as an extension to the standard inode attributes of a POSIX filesystem inode. However, "extended attributes" terminology is already used for describing the unstructured string key/blob value inode attributes of the [gs]etxattr() api,
which are something completely different.

Quoted 53 lines
Documentation/filesystems/fileattr.rst new file +53 -0
+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
+------------------------------

Documentation/filesystems/fileattr.rst line 76

Quoted 10 lines
Documentation/filesystems/fileattr.rst new file +10 -0
+
+.. 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``

The "in 7.x" language is inspired by man-pages, but it has no place here because this document lives in the same repo as the code.

I think you should mention the versions V0 V1 somewhere in the doc and then you can refer to them instead of saying 7.x or when describing a flag which applies only to V1 (PROJID).

Documentation/filesystems/fileattr.rst line 86

Quoted 10 lines
Documentation/filesystems/fileattr.rst new file +10 -0
+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.

I understand it, but don't think it is clear to readers what this note means.

Also please avoid text that will go stale as "Currently most filesystems" Use language that is not dependent on how many fs are converted.

Nore to bot: this doc should not go stale/incorrect while filesystems are converted and support for more flags added in the future.

Quoted 18 lines
Documentation/filesystems/fileattr.rst new file +18 -0
+
+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);
+

Documentation/filesystems/fileattr.rst line 114

Quoted 10 lines
Documentation/filesystems/fileattr.rst new file +10 -0
+   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.

This sounds like a caution for regressing sync/backup workloads with the new UAPI, but isn't it the same with the old UAPI when file2 is on an fs that does not accept a flag that file1 advertizes?

I anything, the new UAPI improves the situation by making it transparent and testable for the sync/backup program and allows the program to make intelligent decisions based on the advertized information in the masks.

So the caution should be framed as change of behavior for backup/sync - instead of silently dropping unsupported flags, the new apis may reject them and provide the reason to the program so that it can handle the situation correctly.

Quoted 7 lines
Documentation/filesystems/fileattr.rst new file +7 -0
+
+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.

Documentation/filesystems/fileattr.rst line 131

Quoted 10 lines
Documentation/filesystems/fileattr.rst new file +10 -0
+- 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``):

Phrase as generic rule with examples so doc won't go stale when adding new value fields.

Documentation/filesystems/fileattr.rst line 143

Quoted 12 lines
Documentation/filesystems/fileattr.rst new file +12 -0
+
+  - 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

Sorry this is too ugly to live.
Need to do something about this.
Nobody can understand and follow this PROJID exemption it needs to be better abstracted.

Quoted 39 lines
Documentation/filesystems/fileattr.rst new file +39 -0
+    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``)::

Documentation/filesystems/fileattr.rst line 192

Quoted 10 lines
Documentation/filesystems/fileattr.rst new file +10 -0
+
+    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".

I still very much dislike this.
Yes, it is elegant and consistent, but humans sometimes have expectations from apis which are different than documentation and this becomes a source of bugs.

I ask again, what do we lose by requiring fa.fa_projid = 0 along with clearing FS_XFLAG_PROJID?

We can always relax it later if the need arises and in the meanwhile we make sure that all programs do not reset projid by mistake. This is a bug so easy to write by mistake or by using a stale copy of the constant FS_XFLAGS_MASK in a program, which copied it from the kernel headers.

People do not read the fine print of documentation and man pages when writing software!

Quoted 43 lines
Documentation/filesystems/fileattr.rst new file +43 -0
+
+.. _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).  |
++--------------------------------+------------+--------+-------------------------------------------------------+

Documentation/filesystems/fileattr.rst line 245

Quoted 10 lines
Documentation/filesystems/fileattr.rst new file +10 -0
+| ``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.                             |

RO/RW not very clear. Maybe a footnote RO/RW[*] for RW only via SETFLAGS

Documentation/filesystems/fileattr.rst line 249

Quoted 4 lines
Documentation/filesystems/fileattr.rst new file +4 -0
++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``FS_XFLAG_CASENONPRESERVING`` | 0x00080000 | RO     | Case not preserved in directory lookups.              |
++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``FS_XFLAG_PROJID``            | 0x00100000 | RW     | Project ID value flag (Linux >= 7.X).                 |

No 7.x please, V1 or something

Documentation/filesystems/fileattr.rst line 252

Quoted 3 lines
Documentation/filesystems/fileattr.rst new file +3 -0
+|                                |            |        | Updates require ``init_user_ns``.                     |
++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``FS_XFLAG_COMPR``             | 0x00200000 | RO/RW  | Compressed file flag.                                 |

Same RO/RW[*]

Quoted 58 lines
Documentation/filesystems/fileattr.rst new file +58 -0
++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``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:
+

Documentation/filesystems/fileattr.rst line 320

Quoted 10 lines
Documentation/filesystems/fileattr.rst new file +10 -0
+``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.

Probably better as Kerneldoc in the patch itself.

Quoted 100 lines
Documentation/filesystems/fileattr.rst new file +100 -0
+
+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``          |
++------------------------+--------------------------------+

Documentation/filesystems/fileattr.rst line 430

Quoted 10 lines
Documentation/filesystems/fileattr.rst new file +10 -0
+|                        | ``FS_XFLAG_FILESTREAM``        |
++------------------------+--------------------------------+
+|                        | ``FS_XFLAG_COWEXTSIZE``        |
++------------------------+--------------------------------+
+|                        | ``FS_XFLAG_CASENONPRESERVING`` |
++------------------------+--------------------------------+
+|                        | ``FS_XFLAG_PROJID``            |
++------------------------+--------------------------------+
+|                        | ``FS_XFLAG_HASATTR``           |
++------------------------+--------------------------------+

Come on! This is writing the code in the doc.

This document should tell the high level story of why those converters exist and when and why they should be used by vfs and/or filesystems.

The documentation of each helper interface belongs in Kerneldoc not here.

Quoted 22 lines
Documentation/filesystems/fileattr.rst new file +22 -0
+
+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

Documentation/filesystems/fileattr.rst line 462

Quoted 10 lines
Documentation/filesystems/fileattr.rst new file +10 -0
+  ``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.
+

This is a hard read, try to simplify if you can, less interface, more high level maybe. not sure.

Quoted 10 lines
Documentation/filesystems/fileattr.rst new file +9 -0
+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(),
Quoted 2 lines

a vfs_fileattr_set_locked() will be
+ added for cases where inode lock is already held.

This is an odd thing to write in the context of this doc.

Quoted 10 lines
Documentation/filesystems/fileattr.rst new file +10 -0
+
+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.

Documentation/filesystems/fileattr.rst

Quoted 10 lines
Documentation/filesystems/fileattr.rst new file +10 -0
+
+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``),

We must get rid of this exemption somehow.
It's unreadable.
Possibly with V1/V0 phrasing.

Quoted 2 lines

invalid inode type for ``FS_XFLAG_EXTSIZE`` /
+ ``FS_XFLAG_EXTSZINHERIT`` / ``FS_XFLAG_COWEXTSIZE`` / ``FS_XFLAG_DAX``,

Note to bot: keep it general with examples. no need to bloat the doc with a list of all the relevant flags in current code.

Quoted 27 lines
Documentation/filesystems/fileattr.rst new file +27 -0
+    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.

Documentation/filesystems/fileattr.rst line 530

Quoted 10 lines
Documentation/filesystems/fileattr.rst new file +10 -0
+
+``-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.

This is not supposed to be a man page and this duplicates information not specific
to the new UAPI and not new.

Only the new EINVAL/EOPNOTSUPP related to this patch set are relevant in this doc.

Quoted 71 lines
Documentation/filesystems/index.rst +1 -0
index a556084c8858..71a1ae852e5b 100644
@@ -28,6 +28,7 @@ algorithms work.
    dnotify
    fiemap
    files
+   fileattr
    locks
    mmap_prepare
    multigrain-ts
Documentation/filesystems/vfs.rst +11 -8
index 00fbcd549f15..e222d5025fe4 100644
@@ -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
include/uapi/linux/fs.h +17 -2
index c1c487d07d29..5a58e1029bf8 100644
@@ -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

include/uapi/linux/fs.h struct fsxattr { · line 254

Quoted 10 lines
include/uapi/linux/fs.h +9 -0
+ *                     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.
+ */
+/*

Keep this with the code patch.

Thanks,
Amir.

#5 lore raw

Re: [PATCH v3 2/5] vfs: add xflags masks to fileattr API

In reply to koraynilay

Review

Quoted 8 lines · koraynilay
(quoted diff) +8 -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;

I would like to ask for a change here before v4: let FS_IOC_SETFLAGS keep working for flags that the rw mask cannot express, instead of failing with EOPNOTSUPP. A sketch and the reason follow; would you take something like it?

I ran into this assumption while putting the may-share flag from the FIEMAP thread [1] on top of v3, in a series I have not posted yet. The flag is read-only, so it is reported through fa_xflags_ro_mask, which the VFS fills only when the filesystem sets has_masks. My series therefore sets has_masks for ocfs2 as well as xfs. On ocfs2 that broke FS_IOC_SETFLAGS for six flags it accepts today. Five have no FS_XFLAG_*
counterpart: SECRM, UNRM and NOTAIL on files, DIRSYNC and TOPDIR on directories. The sixth is COMPR, whose xflag from 3/5 is read-only, so it can never be in fsx_xflags_rw_mask. Each set failed with EOPNOTSUPP (QEMU, v7.4-rc1 plus v3 1-3 and my series); without my series all six work.

ocfs2 is only the first case. Of the filesystems in v3 only ntfs sets has_masks, but ext4 (DIRSYNC, TOPDIR, JOURNAL_DATA, EXTENTS) and btrfs (DIRSYNC, NOCOW) have the same kind of flags, so neither could set it without losing chattr for them. COMPR on btrfs is the case Amir asked to footnote in 5/5 as "RW only via SETFLAGS": with masks, chattr +c would fail the same way, and the footnote would no longer be true. With the fallback below it stays true. FS_IOC_FSSETXATTR is not affected, since it has never been able to set these flags.

The sketch, against v3 2/5: when an FS_IOC_SETFLAGS call changes a flag that the rw mask cannot express, give the filesystem the full read-modify-write request it got before masks. The check quoted above can then never fire, so the sketch removes it:

fs/file_attr.c +0 -0
@@ static int fileattr_set_rmw_to_mask(struct file_kattr *fa,
 	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);
+		changed = flags_to_xflags(fa->flags ^ old_ma->flags);
 	} else { /* if (fa->fsx_valid) */
@@ static int fileattr_set_convert(struct file_kattr *fa,
-	if (old_ma->has_masks)
+	/*
+	 * A masked request names only flags with an FS_XFLAG_* counterpart.
+	 * FS_IOC_SETFLAGS changing any other flag (e.g. FS_DIRSYNC_FL) gets
+	 * the full request below, which the filesystem handled before masks.
+	 */
+	if (old_ma->has_masks &&
+	    !(fa->flags_valid && ((fa->flags ^ old_ma->flags) &
+				  ~xflags_to_flags(old_ma->fsx_xflags_rw_mask))))
 		return fileattr_set_rmw_to_mask(fa, old_ma);

With it, all six ocfs2 flags set and clear again, and nothing else in my xfs, ocfs2 and overlayfs checks changes. The cost is in the contract: a filesystem that sets has_masks must still accept a full, unmasked request from FS_IOC_SETFLAGS. Every ->fileattr_set() does today, because has_masks on set is new in this series, but the documentation in 5/5 would need to say so. The other way I can see is to keep the masked request and pass the unmapped FS_*_FL changes beside it, which needs a second mask. That seems like more API than the case needs, so I would go with the fallback.

[1] https://lore.kernel.org/linux-xfs/[email protected]/

Lauri

author
#3 lore raw

Re: [PATCH v3 1/5] vfs: use goto out on error in vfs_fileattr_set()

In reply to Amir Goldstein
Quoted 9 lines · Amir Goldstein
Quoted 6 lines · koraynilay

Switch from using if (!err) { ... } to the cleaner if (err) goto out; in the main body of vfs_fileattr_set().

Thanks.

Quoted 15 lines

A note about RVB/S-o-b order.
This is not a rule and not universally followed practice, but for me S-o-b is signing the entire contact of the commit body and commit message above the S-o-b which includes the RVB collected by you.

If this was a standalone patch and Christian would be picking it up to vfs tree based on our RVB, the history records will show
S-o-b: koraynilay
RVB: aalbersh
RVB: amir73il
S-o-b: brauner

But if you posted v4 first and Christian would pick the series later history records would show

Quoted 6 lines

RVB: aalbersh
RVB: amir73il
S-o-b: koraynilay
S-o-b: brauner

I think this is a more accurate representation of the change record.

That makes sense, I was indeed unsure on how to order them.

Quoted 9 lines

This is definitely not critical at all, but since you are developing your developer SKILLs, I thought this was worth mentioning.

And also if it was not clear:
- You never need to post another revision of a patch set if all that changed in RVBs. This is something that the maintainers pick up when applying the patch set
- Please take care to carry RVBs yourself in following revisions, as a sign for reviewers that nothing has changed since their last review

(Added yours now on my tree so I don't forget ahah)

Quoted 5 lines

- Please do NOT remove RVB for minor changes!
This rule requires judgement and common sense and the definition of "minor changes" may very well depend on the reviewer, so if unsure you can ask, or better yet remove RVB and write in notes after --- what changed since vX, so the re-review becomes less of a burden

Thanks.

koraynilay

Quoted 68 lines

Thanks,
Amir.

Quoted 65 lines
---
 fs/file_attr.c | 45 +++++++++++++++++++++++----------------------
 1 file changed, 23 insertions(+), 22 deletions(-)
fs/file_attr.c +23 -23
index 833e03810b24..40d6518f1d8e 100644
@@ -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

#6 lore raw

Re: [PATCH v3 2/5] vfs: add xflags masks to fileattr API

In reply to Lauri Gates
Quoted 69 lines · Lauri Gates

Review

Quoted 8 lines · koraynilay
(quoted diff) +8 -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;

I would like to ask for a change here before v4: let FS_IOC_SETFLAGS keep working for flags that the rw mask cannot express, instead of failing with EOPNOTSUPP. A sketch and the reason follow; would you take something like it?

I ran into this assumption while putting the may-share flag from the FIEMAP thread [1] on top of v3, in a series I have not posted yet. The flag is read-only, so it is reported through fa_xflags_ro_mask, which the VFS fills only when the filesystem sets has_masks. My series therefore sets has_masks for ocfs2 as well as xfs. On ocfs2 that broke FS_IOC_SETFLAGS for six flags it accepts today. Five have no FS_XFLAG_*
counterpart: SECRM, UNRM and NOTAIL on files, DIRSYNC and TOPDIR on directories. The sixth is COMPR, whose xflag from 3/5 is read-only, so it can never be in fsx_xflags_rw_mask. Each set failed with EOPNOTSUPP (QEMU, v7.4-rc1 plus v3 1-3 and my series); without my series all six work.

ocfs2 is only the first case. Of the filesystems in v3 only ntfs sets has_masks, but ext4 (DIRSYNC, TOPDIR, JOURNAL_DATA, EXTENTS) and btrfs (DIRSYNC, NOCOW) have the same kind of flags, so neither could set it without losing chattr for them. COMPR on btrfs is the case Amir asked to footnote in 5/5 as "RW only via SETFLAGS": with masks, chattr +c would fail the same way, and the footnote would no longer be true. With the fallback below it stays true. FS_IOC_FSSETXATTR is not affected, since it has never been able to set these flags.

The sketch, against v3 2/5: when an FS_IOC_SETFLAGS call changes a flag that the rw mask cannot express, give the filesystem the full read-modify-write request it got before masks. The check quoted above can then never fire, so the sketch removes it:

fs/file_attr.c +0 -0

@@ static int fileattr_set_rmw_to_mask(struct file_kattr *fa,

        if (fa->flags_valid) {
fs/file_attr.c +1 -9
-               /*
-                * 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);
+               changed = flags_to_xflags(fa->flags ^ old_ma->flags);
        } else { /* if (fa->fsx_valid) */

@@ static int fileattr_set_convert(struct file_kattr *fa,

fs/file_attr.c +8 -1
-       if (old_ma->has_masks)
+       /*
+        * A masked request names only flags with an FS_XFLAG_* counterpart.
+        * FS_IOC_SETFLAGS changing any other flag (e.g. FS_DIRSYNC_FL) gets
+        * the full request below, which the filesystem handled before masks.
+        */
+       if (old_ma->has_masks &&
+           !(fa->flags_valid && ((fa->flags ^ old_ma->flags) &
+                                 ~xflags_to_flags(old_ma->fsx_xflags_rw_mask))))
                return fileattr_set_rmw_to_mask(fa, old_ma);
Quoted 9 lines

With it, all six ocfs2 flags set and clear again, and nothing else in my xfs, ocfs2 and overlayfs checks changes. The cost is in the contract: a filesystem that sets has_masks must still accept a full, unmasked request from FS_IOC_SETFLAGS. Every ->fileattr_set() does today, because has_masks on set is new in this series, but the documentation in 5/5 would need to say so. The other way I can see is to keep the masked request and pass the unmapped FS_*_FL changes beside it, which needs a second mask. That seems like more API than the case needs, so I would go with the fallback.

I didn't look at the sketch, but I agree with the request, it makes a lot of sense and for the context of FS_XFLAG_BTIME, it will also allow simpler conversion for all the filesystems that only want to advertise read-only btime.

w.r.t describing capabilities, we need at least three different properties instead of the single has_masks:
- filesystem can report masks in get
- filesystem can accept masks in set
- caller has specified masks (AT_FILEATTR_MASKS)

The single has_masks in v3 is probably the root cause of most of the reported v3 overlayfs bugs.

With those properties, I think it is simpler than your sketch:
ocfs2 can advertise read-only flags in get without accepting masks in set, so its legacy requests are passed as is.

The long term goal is for all fs methods to be converted to deal only with the canonical masked format, so they don't need to carry legacy code and all conversions will be done in the vfs. A fs should accept masks in set only when all its writable flags can be expressed with xflags, which may require adding new xflags first (e.g. DIRSYNC).

Thanks,
Amir.

author
#41 lore raw

Re: [PATCH v3 5/5] docs: add fileattr API documentation

In reply to Amir Goldstein
Quoted 39 lines · Amir Goldstein
Quoted 21 lines · koraynilay

Add documentation for the fileattr API.

---
 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
Documentation/filesystems/fileattr.rst new file +3 -0
new file mode 100644
index 000000000000..9e3515dc4517
@@ -0,0 +1,530 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+============

Documentation/filesystems/fileattr.rst line 13

Quoted 10 lines
Documentation/filesystems/fileattr.rst new file +10 -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).
+

This is nice!
Much easier to review the UAPI this way.

I would add something about the terminology "file attributes" which was a source of confusion and bikeshedding and the relation to other UAPIs:

Quoted 7 lines

The "file attributes" that this document refers to are structured file attributes
much like the inode information advertised by stat(2) and statx(2). statx(2) even advertises some of the file attributes described in this document:
File attributes
The stx_attributes field contains a set of ORed flags that
indicate additional attributes of the file.

Yes, I used "inode flags" because "file attributes" was used for a bunch of other things too (even tho the API is "fileattr").

Quoted 6 lines

The file attributes described in this document could be described as an extension to the standard inode attributes of a POSIX filesystem inode. However, "extended attributes" terminology is already used for describing the unstructured string key/blob value inode attributes of the [gs]etxattr() api,
which are something completely different.

I hadn't checked statx(2) but since it does report the same ones, I'll switch the terminology to just "file attributes" in the doc, maybe adding a clarification at the start (like the

Quoted 1 line
Quoted 1 line

(like immutable and append-only).

I already have) like the one you wrote here.

Quoted 67 lines
Quoted 53 lines
Documentation/filesystems/fileattr.rst new file +53 -0
+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
+------------------------------

Documentation/filesystems/fileattr.rst line 76

Quoted 10 lines
Documentation/filesystems/fileattr.rst new file +10 -0
+
+.. 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``

The "in 7.x" language is inspired by man-pages, but it has no place here because this document lives in the same repo as the code.

Quoted 3 lines

I think you should mention the versions V0 V1 somewhere in the doc and then you can refer to them instead of saying 7.x or when describing a flag which applies only to V1 (PROJID).

Ah ok, will do.

Quoted 4 lines
Quoted 4 lines
Documentation/filesystems/fileattr.rst new file +4 -0
+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.
+
Quoted 8 lines

Documentation/filesystems/fileattr.rst line 86

Quoted 6 lines
Documentation/filesystems/fileattr.rst new file +6 -0
+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.

I understand it, but don't think it is clear to readers what this note means.

Yeah I wanted to explain a bit about the conversions but I thought that since this is the userspace section it was too detailed. I'll change it to mention the format conversion without many details and make a reference to the detailed in-kernel explanation below.

Quoted 2 lines

Also please avoid text that will go stale as "Currently most filesystems" Use language that is not dependent on how many fs are converted.

Right, makes sense.

Quoted 25 lines

Nore to bot: this doc should not go stale/incorrect while filesystems are converted and support for more flags added in the future.

Quoted 22 lines
Documentation/filesystems/fileattr.rst new file +22 -0
+
+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
Quoted 8 lines

Documentation/filesystems/fileattr.rst line 114

Quoted 6 lines
Documentation/filesystems/fileattr.rst new file +6 -0
+     ``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.

This sounds like a caution for regressing sync/backup workloads

Mainly yes, I added it also because you had mentioned get -> set should work directly (so users may expect that), but since it can't I put it as caution.

Quoted 2 lines

with the new UAPI, but isn't it the same with the old UAPI when file2 is on an fs that does not accept a flag that file1 advertizes?

Yes.

Quoted 9 lines

I anything, the new UAPI improves the situation by making it transparent and testable for the sync/backup program and allows the program to make intelligent decisions based on the advertized information in the masks.

So the caution should be framed as change of behavior for backup/sync - instead of silently dropping unsupported flags, the new apis may reject them and provide the reason to the program so that it can handle the situation correctly.

Hmm yes, the main reason for this caution was non masks filesystems returning 0 as rw_mask -> no-op, which could be unexpected by programs. I'll update it so it's clearer and mentions sync/backups, since yeah, I can't really think of many use-cases where this get -> set (using masks) would make sense apart from that.

Quoted 12 lines
Quoted 12 lines
Documentation/filesystems/fileattr.rst new file +12 -0
+
+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.
Quoted 8 lines

Documentation/filesystems/fileattr.rst line 131

Quoted 5 lines
Documentation/filesystems/fileattr.rst new file +5 -0
+  - 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``):

Phrase as generic rule with examples so doc won't go stale when adding new value fields.

Right.

Quoted 6 lines
Quoted 6 lines
Documentation/filesystems/fileattr.rst new file +6 -0
+
+  - 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).
+
Quoted 8 lines

Documentation/filesystems/fileattr.rst line 143

Quoted 6 lines

+- Validation and capability rules:

Documentation/filesystems/fileattr.rst new file +5 -0
+
+  - 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

Sorry this is too ugly to live.

Lmao.
Yeah and FS_XFLAGS_MASK etc aren't even exposed to userspace, forgot about that.

Quoted 3 lines

Need to do something about this.
Nobody can understand and follow this PROJID exemption it needs to be better abstracted.

Hmm, maybe in the userspace part we should treat FS_XFLAG_PROJID as part of FS_XFLAGS_MASK, since that's basically how we treat it as, and I'll rephrase that so it's actually understandable by people.

Quoted 56 lines
Quoted 40 lines
Documentation/filesystems/fileattr.rst new file +39 -0
+    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``)::

Documentation/filesystems/fileattr.rst line 192

Quoted 10 lines
Documentation/filesystems/fileattr.rst new file +10 -0
+
+    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".

I still very much dislike this.
Yes, it is elegant and consistent, but humans sometimes have expectations from apis which are different than documentation and this becomes a source of bugs.

Quoted 2 lines

I ask again, what do we lose by requiring fa.fa_projid = 0 along with clearing FS_XFLAG_PROJID?

The possibility for filesystems to use a value that is not 0 to mean "unset" for a specific field, but since

Quoted 1 line

We can always relax it later if the need arises

that's fair.

Quoted 5 lines

and in the meanwhile
we make sure that all programs do not reset projid by mistake. This is a bug so easy to write by mistake or by using a stale copy of the constant FS_XFLAGS_MASK in a program, which copied it from the kernel headers.

You mean someone forgetting to set FS_XFLAG_PROJID in fa_xflags and setting it only in rw_mask (along with fa_projid > 0), since PROJID in fa_xflags was never required before (unlike extsize/cowextsize)?

So should we require fa_value == 0 when fa_xflags doesn't have VALUE also for extsize/cowextsize too?

Quoted 2 lines

People do not read the fine print of documentation and man pages when writing software!

Yeah fair.

Quoted 47 lines
Quoted 47 lines
Documentation/filesystems/fileattr.rst new file +47 -0
+
+.. _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``.                                  |
++--------------------------------+------------+--------+-------------------------------------------------------+
Quoted 8 lines
Quoted 6 lines
+| ``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. |

RO/RW not very clear. Maybe a footnote RO/RW[*] for RW only via SETFLAGS

Huh why only via SETFLAGS? It should be settable also by file_setattr once filesystems that support it as rw switch to using xflags (without even the need for supporting masks) no?

Quoted 75 lines
Quoted 4 lines

++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``FS_XFLAG_CASENONPRESERVING`` | 0x00080000 | RO | Case not preserved in directory lookups. |
++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``FS_XFLAG_PROJID`` | 0x00100000 | RW | Project ID value flag (Linux >= 7.X). |

No 7.x please, V1 or something

Quoted 3 lines

+| | | | Updates require ``init_user_ns``. |
++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``FS_XFLAG_COMPR`` | 0x00200000 | RO/RW | Compressed file flag. |

Same RO/RW[*]

Quoted 62 lines

++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``FS_XFLAG_ENCRYPT`` | 0x00400000 | RO | Encrypted file flag (fscrypt). Read-only. |
++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``FS_XFLAG_HASATTR`` | 0x80000000 | RO | Inode has extended attributes. Read-only. |
++--------------------------------+------------+--------+-------------------------------------------------------+

Documentation/filesystems/fileattr.rst new file +57 -0
+
+
+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``
Quoted 8 lines

Documentation/filesystems/fileattr.rst line 320

Quoted 6 lines
Documentation/filesystems/fileattr.rst new file +6 -0
+    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.

Probably better as Kerneldoc in the patch itself.

Ah yeah, I forgot to add an explanation comment there.

Quoted 104 lines
Quoted 104 lines
Documentation/filesystems/fileattr.rst new file +104 -0
+
+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``        |
++------------------------+--------------------------------+
Quoted 8 lines
Quoted 6 lines

+| | ``FS_XFLAG_CASENONPRESERVING`` |
++------------------------+--------------------------------+
+| | ``FS_XFLAG_PROJID`` |
++------------------------+--------------------------------+
+| | ``FS_XFLAG_HASATTR`` |
++------------------------+--------------------------------+

Come on! This is writing the code in the doc.

I like having the visual table here though :(
since in the headers they're just separate lists (and actually, I thought the overlap between FS_*_FL flags and FS_XFLAG_* xflags was a lot more before having this table).

Quoted 6 lines

This document should tell the high level story of why those converters exist and when and why they should be used by vfs and/or filesystems.

The documentation of each helper interface belongs in Kerneldoc not here.

Right, for now I only added a simple comments but I should add the full kernel-doc for them.

Kinda related, but should we EXPORT_SYMBOL the rmw_to_mask and back helpers?

Quoted 28 lines
Quoted 28 lines
Documentation/filesystems/fileattr.rst new file +27 -0
+
+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,
Quoted 8 lines

Documentation/filesystems/fileattr.rst line 462

Quoted 5 lines
Documentation/filesystems/fileattr.rst new file +5 -0
+  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.
+

This is a hard read, try to simplify if you can, less interface, more high level maybe. not sure.

Hmm, for me it's high level enough, but since I already know what these functions do I'll ask friends/other people too and take it from there.

Quoted 10 lines
Quoted 9 lines
Documentation/filesystems/fileattr.rst new file +9 -0
+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(),
Quoted 4 lines
Quoted 2 lines

a vfs_fileattr_set_locked() will be
+ added for cases where inode lock is already held.

This is an odd thing to write in the context of this doc.

Fair, I added it since I had to make the _locked() variant for NFS/CIFS (iirc), so I thought it could be useful to point out in the doc that it hasn't been added yet but it will (whenever we'll need to use it for changing btime probably).

Quoted 16 lines
Quoted 16 lines
Documentation/filesystems/fileattr.rst new file +16 -0
+
+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,
Quoted 8 lines

Documentation/filesystems/fileattr.rst

Quoted 4 lines
Documentation/filesystems/fileattr.rst new file +4 -0
+    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``),

We must get rid of this exemption somehow.
It's unreadable.
Possibly with V1/V0 phrasing.

Yeah.

Quoted 5 lines
Quoted 2 lines

invalid inode type for ``FS_XFLAG_EXTSIZE`` /
+ ``FS_XFLAG_EXTSZINHERIT`` / ``FS_XFLAG_COWEXTSIZE`` / ``FS_XFLAG_DAX``,

Note to bot: keep it general with examples. no need to bloat the doc with a list of all the relevant flags in current code.

Right, same as the above for the generic phrasing.

Quoted 42 lines
Quoted 27 lines
Documentation/filesystems/fileattr.rst new file +27 -0
+    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.

Documentation/filesystems/fileattr.rst line 530

Quoted 10 lines
Documentation/filesystems/fileattr.rst new file +10 -0
+
+``-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.

This is not supposed to be a man page and this duplicates information not specific
to the new UAPI and not new.

Quoted 2 lines

Only the new EINVAL/EOPNOTSUPP related to this patch set are relevant in this doc.

Ah ok, will remove them.

Quoted 75 lines
Quoted 75 lines
Documentation/filesystems/index.rst +1 -0
index a556084c8858..71a1ae852e5b 100644
@@ -28,6 +28,7 @@ algorithms work.
    dnotify
    fiemap
    files
+   fileattr
    locks
    mmap_prepare
    multigrain-ts
Documentation/filesystems/vfs.rst +11 -8
index 00fbcd549f15..e222d5025fe4 100644
@@ -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
include/uapi/linux/fs.h +21 -2
index c1c487d07d29..5a58e1029bf8 100644
@@ -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
Quoted 8 lines

include/uapi/linux/fs.h struct fsxattr { · line 254

Quoted 6 lines
include/uapi/linux/fs.h +5 -0
+ *                     (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.
+ */
+/*

Keep this with the code patch.

Ah ok.

Thanks,
koraynilay

Quoted 2 lines

Thanks,
Amir.

#10 lore raw

Re: [PATCH v3 2/5] vfs: add xflags masks to fileattr API

In reply to koraynilay
Quoted 207 lines · 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
---
 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(-)
fs/file_attr.c +72 -9
index 40d6518f1d8e..5d3834f4405b 100644
@@ -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;
 }

fs/file_attr.c static int copy_fsxattr_from_user(struct file_kattr *fa, · line 244

Quoted 7 lines
fs/file_attr.c +4 -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.
+ */

Sorry I am not fond of this terminology as well It's not like the new API cannot be used RMW style. I suggest to stick with plain boring converter names like fileattr_set_v0_to_v1() similar to FS_XFLAGS_VER[01]_MASK comment below.

Quoted 135 lines
fs/file_attr.c +135 -0
+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.

fs/file_attr.c static int copy_fsxattr_from_user(struct file_kattr *fa, · line 389

Quoted 10 lines
fs/file_attr.c +10 -0
+ */
+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;

This is where the single has_masks bites twice.

fa->has_masks is supposed to mean "caller specified AT_FILEATTR_MASKS", but for in-kernel get+modify+set callers (ovl_copy_fileattr(), ksmbd_vfs_set_compression()) it was set by vfs_fileattr_get() of a fs that reports masks, so their legacy requests are passed through as masked requests instead of going through rmw_to_mask().

For ksmbd over ntfs, FS_COMPR_FL is not in the rw mask, so it is silently ignored instead of failing with -EOPNOTSUPP as ksmbd expects.

old_ma->has_masks is used as "fs accepts masks in set", but it really means "fs reports masks in get", which is the problem Lauri reported for ocfs2.

As I wrote in reply to Lauri, these should be three different properties:
- caller has specified masks (AT_FILEATTR_MASKS => has_masks)
- fs reported masks in get (masks_valid?)
- fs can accept masks in set (accepts_masks?)

That last hint is the one that would help Lauri's case and read-only btime and it remains vfs internal, not exposed to userspace for now.

Note that, as you have seen for yourself in the FUSE bug that you fixed, overloading the _valid hints for both directions was a source of bugs. If the _valid hints were split into hints from the caller and hints from the filesystem, things might have been less confusing (but you don't need to change that now).

Quoted 101 lines
fs/file_attr.c +50 -10
+       }
+
+       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;
 }

fs/file_attr.c int vfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry, · line 540

Quoted 6 lines
fs/file_attr.c +3 -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;
+

I understand the logic behind zero rw_mask means do nothing, but it looks to me like setting a trap for programs that read from old fs (zero rw_mask) and set the same fa blob to new fs.

It is a variant of the caution clause in your patch 5 doc.

IMO, AT_FILEATTR_MASKS should treat zero rw_masks as EINVAL.

Alternatively, I hate to change everything again, but maybe we made a bad choice with AT_FILEATTR_MASKS, because using FS_XFLAG_HASMASKS may have been better for all the backup/sync cases?
I leave it up to you if you want to explore this alley.

fs/file_attr.c int vfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry, · line 291

Quoted 6 lines
fs/file_attr.c +0 -1
        inode_lock(inode);
        err = vfs_fileattr_get(dentry, &old_ma);
        if (err)
                goto out;
 
-       /* initialize missing bits from old_ma */

Is this comment not still relevant in this context after get of old_ma?

Quoted 38 lines
fs/file_attr.c +9 -11
-       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);

fs/file_attr.c SYSCALL_DEFINE5(file_getattr, int, dfd, const char __user *, filename, · line 683

Quoted 8 lines
fs/file_attr.c +5 -0
@@ -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.
+        */

See comment below on FS_XFLAGS_VER0_MASK

Quoted 75 lines
fs/file_attr.c +22 -5
+       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),
include/linux/fileattr.h +5 -0
index 09e32b84e02a..fb158269b4ea 100644
@@ -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 | \

include/linux/fileattr.h line 45

Quoted 6 lines
include/linux/fileattr.h +3 -0
@@ -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)

I think these two similarly named constants will be confusing for coders and reviewers and the special-casing of PROJID is a pain for documentation.

How about FS_XFLAGS_VER[01]_MASK?
Do not define FS_XFLAGS_MASK so each macro
user will need to be converted to use the correct macro.

Using alias FS_XFLAGS_LATEST_MASK could read nicely in some common helpers, e.g.:

        fa->fsx_xflags &= FS_XFLAGS_LATEST_MASK;
        return 0;

explicit VER1 could read nicer in other helpers, e.g.

        u64 mask = fa->has_masks ? FS_XFLAGS_VER1_MASK : FS_XFLAGS_VER0_MASK;

Thanks,
Amir.

author
#35 lore raw

Re: [PATCH v3 4/5] ntfs: switch to the new xflags fileattr API

In reply to Amir Goldstein
Quoted 12 lines · Amir Goldstein
Quoted 5 lines · 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

You know you are not required to add this tag anymore. Some vfs maintainers would prefer that rather than specifying the model and version of LLM to describe the how/what you used LLM in cover letter or notes, which you already partly did.

Quoted 7 lines

I know how much extensive personal effort you put into this patch set and for me this work is a great example of human+LLM synergy, but new reviewers don't know that if the scope of "Assisted-by" is not spelled out.

So my recommendation is to drop it and add a small section in the cover letter about the essential role of LLM in auditing all the filesystems and all the possible UAPI compatibility issues with legacy UAPI and internal vfs api.

Ok yeah that makes sense, I'll do like I actually wanted to do for the docs patch (but didn't do), remove the Assisted-by and explain briefly how I used it in either the cover letter or in the --- comments.

Quoted 2 lines

Otherwise, this is a very clean patch and very nice reference example of how to convert a filesystem to the new masks api.

Thanks.

Quoted 86 lines

Some minor comments below

Quoted 73 lines
---
 fs/ntfs/file.c | 109 ++++++++++++++++++++++++++-----------------------
 1 file changed, 58 insertions(+), 51 deletions(-)
fs/ntfs/file.c +19 -14
index 50be49f4c913..34acf9333c41 100644
@@ -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;
 

fs/ntfs/file.c int ntfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry, · line 223

Quoted 5 lines
fs/ntfs/file.c +0 -2
@@ -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;

Something about this feels wrong, meaning that the filesystem should protect itself
from getting called with the wrong fileattr and not fully rely on vfs to do that.

Quoted 7 lines

This over trust is probably part of the problems you see with overlayfs and also creates problems with ksmbd over ntfs.

We discussed at some point a replacement for this helper - I am not sure what exactly it should be called or exactly doing and maybe it even needs to WARN_ON_ONCE() if an internal caller passes unexpected fa format, because this function now expects a certain fa format.

Hmm, I'll think about the best way to go about that, if returning with -EINVAL or WARN_ON_ONCE() or both.

Actually, inode_set_flags already does something like this:

  void inode_set_flags(struct inode *inode, unsigned int flags,
  		     unsigned int mask)
  {
  	WARN_ON_ONCE(flags & ~mask);
  	set_mask_bits(&inode->i_flags, mask, flags);
  }
Quoted 19 lines
Quoted 5 lines
fs/ntfs/file.c +0 -5
-
-       /* chattr passes the unchanged read-only flags back to us too. */
-       if (NInoCompressed(ni) || NInoWofCompressed(ni))
-               readonly |= FS_COMPR_FL;
-       if (NInoEncrypted(ni))

fs/ntfs/file.c int ntfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry, · line 238

Quoted 10 lines
fs/ntfs/file.c +0 -10
-               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;

It is very surprising to see this hunk removed when reviewing the series backwards ;)

Quoted 3 lines

A commit should explain itself without relying on the reviewer keeping context from previous patches, so the commit message needs to explain why those protections were removed.

Ah, yeah, I'll add it to the commit message.

Quoted 56 lines
Quoted 56 lines
fs/ntfs/file.c +34 -19
-
-       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;
Quoted 8 lines

fs/ntfs/file.c int ntfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry, · line 267

Quoted 5 lines
fs/ntfs/file.c +4 -0
+                       linuxflags |= NTFS_LINUXFLAGS_NOATIME;
+               } else {
+                       linuxflags &= ~NTFS_LINUXFLAGS_NOATIME;
+               }
        }

It's a bummer that email review has so much diffstat for what is essentially

Yeah and that's why I love the --word-diff and --ignore-all-spaces options.

Quoted 16 lines

indentation. Sometimes we need to accept that but I have a proposal If you like it, maybe the diffstat will become even smaller.

      if (rw_user_mask & FS_XFLAG_IMMUTABLE) {
              mask_fl |= S_IMMUTABLE;
              linuxflags_mask |= NTFS_LINUXFLAGS_IMMUTABLE;
      }
      if (fa->fsx_xflags & FS_XFLAG_IMMUTABLE) {

[ no change here and no else ]

...

        err = ntfs_ea_set_linuxflags(vi, linuxflags & ~linuxflags_mask);

consistent with inode_set_flags(vi, new_fl, mask_fl);

Quoted 8 lines

Whether fa->fsx_xflags check should be logically in the nested scope is a matter of taste. I personally like better the smaller patch which is easier to review, but it is not a must.

Using linuxflags_mask instead of the else is definitely better IMO, because it makes S_ flags and LINUXFLAGS handling consistent.

WDYT?

I tried it and with a preparatory patch that only switches ntfs to use xflags it looks very clean [1].

While I logically prefer to have them in if blocks, the new approach also makes sense because it's now 1:1 with new_fl (since set_mask_bits called by inode_set_flags does the exact same ((old & ~mask) | new).

Thanks,
koraynilay

Quoted 2 lines

Thanks,
Amir.

[1]:

commit ccda307355008ba86354128d934f7ab97be62a96

Author: koraynilay <[email protected]>
Date:   Sun Nov 22 00:17:29 2026 +0100

ntfs: support the fileattr API masks in ntfs_fileattr_get/set()

Add support for the new fileattr API with masks in ntfs_fileattr_get() and ntfs_fileattr_set().

This also allows us to remove checks for unsupported xflags, since VFS will take care of that by using the rw and ro masks returned in ntfs_fileattr_get().

---
 fs/ntfs/file.c | 73 +++++++++++++++++++++++++++++++---------------------------
 1 file changed, 39 insertions(+), 34 deletions(-)
fs/ntfs/file.c +39 -34
index ca088e7d636b..0a45359f3a2a 100644
@@ -155,6 +155,10 @@ 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);
@@ -179,7 +183,9 @@ int ntfs_fileattr_get(struct dentry *dentry, struct file_kattr *fa)
 	if (!NVolCaseSensitive(ni->vol))
 		xflags |= FS_XFLAG_CASEFOLD;
 
-	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,65 +213,64 @@ 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_XFLAG_IMMUTABLE | FS_XFLAG_APPEND | FS_XFLAG_NODUMP |
-		      FS_XFLAG_NOATIME;
-	u32 readonly = 0;
-	u32 linuxflags = ni->linuxflags & ~NTFS_LINUXFLAGS_MASK;
-	unsigned int new_fl = 0;
-	bool forced_immutable;
+	u32 linuxflags = ni->linuxflags;
+	u32 rw_user_mask = fa->fsx_xflags_rw_mask;
+	unsigned int new_fl = 0, mask_fl = 0;
+	unsigned int new_linuxflags = 0, mask_linuxflags = 0;
+	bool forced_immutable = false;
 	int err;
 
 	err = ntfs_emergency_state(ni->vol);
 	if (err)
 		return err;
 
-	if (fa->fsx_extsize || fa->fsx_cowextsize || fa->fsx_projid)
-		return -EOPNOTSUPP;
-
-	/* chattr passes the unchanged read-only flags back to us too. */
-	if (NInoCompressed(ni) || NInoWofCompressed(ni))
-		readonly |= FS_XFLAG_COMPR;
-	if (NInoEncrypted(ni))
-		readonly |= FS_XFLAG_ENCRYPT;
-	if ((fa->fsx_xflags ^ readonly) & (FS_XFLAG_COMPR | FS_XFLAG_ENCRYPT))
-		return -EOPNOTSUPP;
-	allowed |= readonly;
-
-	/* Case folding is a read-only, mount-wide property. */
-	if (!NVolCaseSensitive(ni->vol))
-		allowed |= FS_XFLAG_CASEFOLD;
-	if (fa->fsx_xflags & ~allowed)
-		return -EOPNOTSUPP;
-
-	forced_immutable = ntfs_is_forced_immutable(vi);
-	if (!(fa->fsx_xflags & FS_XFLAG_IMMUTABLE) && forced_immutable)
-		return -EPERM;
+	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;
+		mask_linuxflags |= NTFS_LINUXFLAGS_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;
+			new_linuxflags |= NTFS_LINUXFLAGS_IMMUTABLE;
+	}
+
+	if (rw_user_mask & FS_XFLAG_APPEND) {
+		mask_fl |= S_APPEND;
+		mask_linuxflags |= NTFS_LINUXFLAGS_APPEND;
 	}
 	if (fa->fsx_xflags & FS_XFLAG_APPEND) {
 		new_fl |= S_APPEND;
-		linuxflags |= NTFS_LINUXFLAGS_APPEND;
+		new_linuxflags |= NTFS_LINUXFLAGS_APPEND;
 	}
+
+	if (rw_user_mask & FS_XFLAG_NODUMP)
+		mask_linuxflags |= NTFS_LINUXFLAGS_NODUMP;
 	if (fa->fsx_xflags & FS_XFLAG_NODUMP)
-		linuxflags |= NTFS_LINUXFLAGS_NODUMP;
+		new_linuxflags |= NTFS_LINUXFLAGS_NODUMP;
+
+	if (rw_user_mask & FS_XFLAG_NOATIME) {
+		mask_fl |= S_NOATIME;
+		mask_linuxflags |= NTFS_LINUXFLAGS_NOATIME;
+	}
 	if (fa->fsx_xflags & FS_XFLAG_NOATIME) {
 		new_fl |= S_NOATIME;
-		linuxflags |= NTFS_LINUXFLAGS_NOATIME;
+		new_linuxflags |= NTFS_LINUXFLAGS_NOATIME;
 	}
 
 	mutex_lock(&ni->mrec_lock);
-	err = ntfs_ea_set_linuxflags(vi, linuxflags);
+	new_linuxflags |= linuxflags & ~mask_linuxflags;
+	err = ntfs_ea_set_linuxflags(vi, new_linuxflags);
 	mutex_unlock(&ni->mrec_lock);
 	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;

commit 347c2201ae065d608e9aee9bac2e93f9b9418156

Author: koraynilay <[email protected]>
Date:   Sun Nov 29 18:10:31 2026 +0100

ntfs: switch from flags to xflags in ntfs_fileattr_get/set()

Switch ntfs_fileattr_get() and ntfs_fileattr_set() to use FS_XFLAG_* xflags instead of FS_*_FL flags.

---
 fs/ntfs/file.c | 44 ++++++++++++++++++++++----------------------
 1 file changed, 22 insertions(+), 22 deletions(-)
fs/ntfs/file.c +22 -22

99 lines not on this page · raw message

author
#30 lore raw

Re: [PATCH v3 3/5] vfs: add COMPR/ENCRYPT xflags from FS_COMPR_FL/FS_ENCRYPT_FL

In reply to Amir Goldstein
Quoted 5 lines · Amir Goldstein
Quoted 1 line · koraynilay

"vfs: add COMPR/ENCRYPT xflags from FS_COMPR_FL/FS_ENCRYPT_FL"

Quoted 8 lines

This is not the best description of this change.

A better description would be

"vfs: advertise COMPR/ENCRYPT xflags in file_getattr()"

Which is exactly the behavior change that I asked you to spell out. You spelled it out in the cover letter, but not here.

Hmm ok yeah now I understand what you mean.
I should make the commit more about "let userspace know we have these too" instead of just a "add missing flags".

Quoted 2 lines

The commit message should also mention FS_IOC_FSGETXATTR but for brevity the subject above is enough.

what about this?

"vfs: fileattr: advertise COMPR/ENCRYPT xflags on get"

and the masks patch could become:

"vfs: fileattr: add xflags masks to internal and user API"

Quoted 1 line

The commit message should also mention FS_IOC_FSGETXATTR

(And I'll add this to the description)

Quoted 18 lines
Quoted 7 lines

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.

This sentence is very hard to understand, because it is missing some important information.
The CASEFOLD flag (and now COMPR) is only read-write via the legacy SETFLAGS ioctl and read-only via the xflag apis.
Also in your documentation patch, you list them as RO/RW which does not fully convey this meaning.
Don't forget why you started this patch set - BTIME is also going to be RO on some fs and RW on others, but in a very different category than those two.

Quoted 1 line
Quoted 5 lines
Quoted 3 lines

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.

... in all the uapi flavors.

Hmm, yes, but I should mention both "uapi flavors" and "filesystems", since casefold can be toggled only with SETFLAGS, but not on all filesystems, while saying that ENCRYPT is read-only for all uapi flavours doesn't mean that no filesystems supports setting it (well, in this case that's true, but in general there could be a filesystem that decides to expose a FS-specific ioctl to enable/disable FS_XFLAG_ENCRYPT and that reports FS_XFLAG_ENCRYPT in fileattr_get).

Anyway, I'll change the message to be more easily readable and mention these cases.

Quoted 7 lines
Quoted 3 lines

Also update vfs fileattr_fill_* helpers to convert these two flags as well.

That's backwards. It's not the "Also" it's the main dish, as my comment about the patch subject implies.
There is no "Also" in this patch - it does one thing.

Fair, will change that.

Quoted 77 lines
Quoted 50 lines
Assisted-by Gemini:3.8-flash-high antigravity-cli-1.5.11
---
 fs/file_attr.c           | 8 ++++++++
 include/linux/fileattr.h | 7 ++++---
 include/uapi/linux/fs.h  | 2 ++
 3 files changed, 14 insertions(+), 3 deletions(-)
fs/file_attr.c +8 -0
index 5d3834f4405b..314d779e41d6 100644
@@ -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);
 
include/linux/fileattr.h +1 -1
index fb158269b4ea..9a8de4190133 100644
@@ -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 | \

include/linux/fileattr.h line 27

Quoted 10 lines
include/linux/fileattr.h +3 -2
-        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)
 

The addition of FS_XFLAG_CASEFOLD in FS_XFLAG_RDONLY_MASK and not in FS_XFLAG_COMMON left the code in a very fragile undocumented state which took me personally a lot of time to figure out.

Let's please document the meaning of these macros better. I propose something like:

/*
* FS_COMMON_FL/FS_XFLAG_COMMON are preserved through RMW
* cycle via either uapi flavor.
* They may NOT include flags which are read-write via one api flavor and
* read-only in another (e.g. CASEFOLD, COMPR).
*/
#define FS_XFLAG_COMMON \
...

Quoted 5 lines

/*
* Read-only xflags via FS_IOC_FS[GS]ETXATTR and file_[gs]etattr().
* The respective flags may be read-write via the FS_IOC_SETFLAGS ioctl.
*/
#define FS_XFLAG_RDONLY_MASK \

Yes I agree, I'll add these to v4.

Thanks,
koraynilay

Quoted 2 lines

Thanks,
Amir.

#36 lore raw

Re: [PATCH v3 4/5] ntfs: switch to the new xflags fileattr API

In reply to koraynilay
Quoted 120 lines · koraynilay
Quoted 12 lines · Amir Goldstein
Quoted 5 lines · 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

You know you are not required to add this tag anymore. Some vfs maintainers would prefer that rather than specifying the model and version of LLM to describe the how/what you used LLM in cover letter or notes, which you already partly did.

Quoted 7 lines

I know how much extensive personal effort you put into this patch set and for me this work is a great example of human+LLM synergy, but new reviewers don't know that if the scope of "Assisted-by" is not spelled out.

So my recommendation is to drop it and add a small section in the cover letter about the essential role of LLM in auditing all the filesystems and all the possible UAPI compatibility issues with legacy UAPI and internal vfs api.

Ok yeah that makes sense, I'll do like I actually wanted to do for the docs patch (but didn't do), remove the Assisted-by and explain briefly how I used it in either the cover letter or in the --- comments.

Quoted 2 lines

Otherwise, this is a very clean patch and very nice reference example of how to convert a filesystem to the new masks api.

Thanks.

Quoted 89 lines

Some minor comments below

Quoted 73 lines
---
 fs/ntfs/file.c | 109 ++++++++++++++++++++++++++-----------------------
 1 file changed, 58 insertions(+), 51 deletions(-)
fs/ntfs/file.c +19 -14
index 50be49f4c913..34acf9333c41 100644
@@ -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;
 

fs/ntfs/file.c int ntfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry, · line 223

Quoted 5 lines
fs/ntfs/file.c +0 -2
@@ -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;

Something about this feels wrong, meaning that the filesystem should protect itself
from getting called with the wrong fileattr and not fully rely on vfs to do that.

This over trust is probably part of the problems you see with overlayfs and also creates problems with ksmbd over ntfs.

Quoted 8 lines
Quoted 4 lines

We discussed at some point a replacement for this helper - I am not sure what exactly it should be called or exactly doing and maybe it even needs to WARN_ON_ONCE() if an internal caller passes unexpected fa format, because this function now expects a certain fa format.

Hmm, I'll think about the best way to go about that, if returning with -EINVAL or WARN_ON_ONCE() or both.

I will answer this below..

Quoted 119 lines

Actually, inode_set_flags already does something like this:

  void inode_set_flags(struct inode *inode, unsigned int flags,
                     unsigned int mask)
  {
        WARN_ON_ONCE(flags & ~mask);
        set_mask_bits(&inode->i_flags, mask, flags);
  }
Quoted 19 lines
Quoted 5 lines
fs/ntfs/file.c +0 -5
-
-       /* chattr passes the unchanged read-only flags back to us too. */
-       if (NInoCompressed(ni) || NInoWofCompressed(ni))
-               readonly |= FS_COMPR_FL;
-       if (NInoEncrypted(ni))

fs/ntfs/file.c int ntfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry, · line 238

Quoted 10 lines
fs/ntfs/file.c +0 -10
-               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;

It is very surprising to see this hunk removed when reviewing the series backwards ;)

Quoted 3 lines

A commit should explain itself without relying on the reviewer keeping context from previous patches, so the commit message needs to explain why those protections were removed.

Ah, yeah, I'll add it to the commit message.

Quoted 56 lines
Quoted 56 lines
fs/ntfs/file.c +34 -19
-
-       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;
Quoted 8 lines

fs/ntfs/file.c int ntfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry, · line 267

Quoted 5 lines
fs/ntfs/file.c +4 -0
+                       linuxflags |= NTFS_LINUXFLAGS_NOATIME;
+               } else {
+                       linuxflags &= ~NTFS_LINUXFLAGS_NOATIME;
+               }
        }

It's a bummer that email review has so much diffstat for what is essentially

Yeah and that's why I love the --word-diff and --ignore-all-spaces options.

Quoted 20 lines

indentation. Sometimes we need to accept that but I have a proposal If you like it, maybe the diffstat will become even smaller.

      if (rw_user_mask & FS_XFLAG_IMMUTABLE) {
              mask_fl |= S_IMMUTABLE;
              linuxflags_mask |= NTFS_LINUXFLAGS_IMMUTABLE;
      }
      if (fa->fsx_xflags & FS_XFLAG_IMMUTABLE) {

[ no change here and no else ]

...

        err = ntfs_ea_set_linuxflags(vi, linuxflags & ~linuxflags_mask);

consistent with inode_set_flags(vi, new_fl, mask_fl);

Whether fa->fsx_xflags check should be logically in the nested scope is a matter of taste. I personally like better the smaller patch which is easier to review, but it is not a must.

Quoted 7 lines
Quoted 4 lines

Using linuxflags_mask instead of the else is definitely better IMO, because it makes S_ flags and LINUXFLAGS handling consistent.

WDYT?

I tried it and with a preparatory patch that only switches ntfs to use xflags it looks very clean [1].

The separation looks very nice indeed!
but I would do this a bit differently. see below.

Quoted 17 lines

While I logically prefer to have them in if blocks, the new approach also makes sense because it's now 1:1 with new_fl (since set_mask_bits called by inode_set_flags does the exact same ((old & ~mask) | new).

Thanks,
koraynilay

Quoted 2 lines

Thanks,
Amir.

[1]:

commit ccda307355008ba86354128d934f7ab97be62a96

Author: koraynilay <[email protected]>
Date:   Sun Nov 22 00:17:29 2026 +0100
Quoted 8 lines

ntfs: support the fileattr API masks in ntfs_fileattr_get/set()

Add support for the new fileattr API with masks in ntfs_fileattr_get() and ntfs_fileattr_set().

This also allows us to remove checks for unsupported xflags, since VFS will take care of that by using the rw and ro masks returned in ntfs_fileattr_get().

Not enough that this is "taken care of by VFS" filesystem needs to call a helper that asserts that the interface contract is respected.

Quoted 60 lines
---
 fs/ntfs/file.c | 73 +++++++++++++++++++++++++++++++---------------------------
 1 file changed, 39 insertions(+), 34 deletions(-)
fs/ntfs/file.c +12 -14
index ca088e7d636b..0a45359f3a2a 100644
@@ -155,6 +155,10 @@ 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);
@@ -179,7 +183,9 @@ int ntfs_fileattr_get(struct dentry *dentry, struct file_kattr *fa)
        if (!NVolCaseSensitive(ni->vol))
                xflags |= FS_XFLAG_CASEFOLD;
 
-       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,65 +213,64 @@ 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_XFLAG_IMMUTABLE | FS_XFLAG_APPEND | FS_XFLAG_NODUMP |
-                     FS_XFLAG_NOATIME;
-       u32 readonly = 0;
-       u32 linuxflags = ni->linuxflags & ~NTFS_LINUXFLAGS_MASK;
-       unsigned int new_fl = 0;
-       bool forced_immutable;
+       u32 linuxflags = ni->linuxflags;
+       u32 rw_user_mask = fa->fsx_xflags_rw_mask;
+       unsigned int new_fl = 0, mask_fl = 0;
+       unsigned int new_linuxflags = 0, mask_linuxflags = 0;
+       bool forced_immutable = false;
        int err;
 
        err = ntfs_emergency_state(ni->vol);
        if (err)
                return err;
 
-       if (fa->fsx_extsize || fa->fsx_cowextsize || fa->fsx_projid)
-               return -EOPNOTSUPP;
-
-       /* chattr passes the unchanged read-only flags back to us too. */
-       if (NInoCompressed(ni) || NInoWofCompressed(ni))
-               readonly |= FS_XFLAG_COMPR;
-       if (NInoEncrypted(ni))

fs/ntfs/file.c int ntfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry, · line 238

Quoted 10 lines
fs/ntfs/file.c +0 -10
-               readonly |= FS_XFLAG_ENCRYPT;
-       if ((fa->fsx_xflags ^ readonly) & (FS_XFLAG_COMPR | FS_XFLAG_ENCRYPT))
-               return -EOPNOTSUPP;
-       allowed |= readonly;
-
-       /* Case folding is a read-only, mount-wide property. */
-       if (!NVolCaseSensitive(ni->vol))
-               allowed |= FS_XFLAG_CASEFOLD;
-       if (fa->fsx_xflags & ~allowed)
-               return -EOPNOTSUPP;

if (fileattr_has_unsupported_xflags(fa, NTFS_RO_XFLAGS,
NTFS_RW_XFLAGS))

                   return -EOPNOTSUPP;

Reviewer can see that all the open coded cruft above is replaced with a generic helper that does exactly the same.

I don't care if it's _supported_ or _unsupported_ or some other name. In any case, SET should not assume that the caller called GET and did all the checks. It's just too much to assume.

I am not sure that vfs is always going to do GET before SET. It simply does not make sense to do that IMO for
a caller that only asked to set one flag/value.

I am not asking you to make the GET before SET conditional now, but asking to prepare for this eventuality.

All you need to do is that a converted filesystem set method has some entry check for valid/supported fa.
This is both more readable and prepares for SET without GET.

Quoted 117 lines
fs/ntfs/file.c +27 -10
-
-       forced_immutable = ntfs_is_forced_immutable(vi);
-       if (!(fa->fsx_xflags & FS_XFLAG_IMMUTABLE) && forced_immutable)
-               return -EPERM;
+       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;
+               mask_linuxflags |= NTFS_LINUXFLAGS_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;
+                       new_linuxflags |= NTFS_LINUXFLAGS_IMMUTABLE;
+       }
+
+       if (rw_user_mask & FS_XFLAG_APPEND) {
+               mask_fl |= S_APPEND;
+               mask_linuxflags |= NTFS_LINUXFLAGS_APPEND;
        }
        if (fa->fsx_xflags & FS_XFLAG_APPEND) {
                new_fl |= S_APPEND;
-               linuxflags |= NTFS_LINUXFLAGS_APPEND;
+               new_linuxflags |= NTFS_LINUXFLAGS_APPEND;
        }
+
+       if (rw_user_mask & FS_XFLAG_NODUMP)
+               mask_linuxflags |= NTFS_LINUXFLAGS_NODUMP;
        if (fa->fsx_xflags & FS_XFLAG_NODUMP)
-               linuxflags |= NTFS_LINUXFLAGS_NODUMP;
+               new_linuxflags |= NTFS_LINUXFLAGS_NODUMP;
+
+       if (rw_user_mask & FS_XFLAG_NOATIME) {
+               mask_fl |= S_NOATIME;
+               mask_linuxflags |= NTFS_LINUXFLAGS_NOATIME;
+       }
        if (fa->fsx_xflags & FS_XFLAG_NOATIME) {
                new_fl |= S_NOATIME;
-               linuxflags |= NTFS_LINUXFLAGS_NOATIME;
+               new_linuxflags |= NTFS_LINUXFLAGS_NOATIME;
        }
 
        mutex_lock(&ni->mrec_lock);
-       err = ntfs_ea_set_linuxflags(vi, linuxflags);
+       new_linuxflags |= linuxflags & ~mask_linuxflags;
+       err = ntfs_ea_set_linuxflags(vi, new_linuxflags);
        mutex_unlock(&ni->mrec_lock);
        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;

commit 347c2201ae065d608e9aee9bac2e93f9b9418156

Author: koraynilay <[email protected]>
Date:   Sun Nov 29 18:10:31 2026 +0100

ntfs: switch from flags to xflags in ntfs_fileattr_get/set()

Switch ntfs_fileattr_get() and ntfs_fileattr_set() to use FS_XFLAG_* xflags instead of FS_*_FL flags.

---
 fs/ntfs/file.c | 44 ++++++++++++++++++++++----------------------
 1 file changed, 22 insertions(+), 22 deletions(-)
fs/ntfs/file.c +9 -9
index 50be49f4c913..ca088e7d636b 100644
@@ -159,27 +159,27 @@ 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);
        return 0;
 }
 

fs/ntfs/file.c int ntfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry, · line 211

Quoted 7 lines
fs/ntfs/file.c +2 -2
@@ -207,8 +207,8 @@ 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 allowed = FS_XFLAG_IMMUTABLE | FS_XFLAG_APPEND | FS_XFLAG_NODUMP |
+                     FS_XFLAG_NOATIME;

opportunity: since already changing this line, define NTFS_RW_XFLAGS in this patch and set

            u32 allowed = NTFS_RW_XFLAGS;

fs/ntfs/file.c int ntfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry, · line 223

Quoted 10 lines
fs/ntfs/file.c +1 -1
        u32 readonly = 0;
        u32 linuxflags = ni->linuxflags & ~NTFS_LINUXFLAGS_MASK;
        unsigned int new_fl = 0;
@@ -219,42 +219,42 @@ int ntfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry,
        if (err)
                return err;
 
-       if (fileattr_has_fsx(fa))
+       if (fa->fsx_extsize || fa->fsx_cowextsize || fa->fsx_projid)
                return -EOPNOTSUPP;

This open coded check looks a bit random.
Perhaps we could start with a simple variant of fileattr_has_fsx() which takes a single mask, as we discussed before:

if (fileattr_has_unsupported_xflags(fa, NTFS_RO_XFLAGS |
NTFS_RW_XFLAGS))

                   return -EOPNOTSUPP;

And later, in the next patch, it is changed to a helper that takes two masks and verifies more based on the masks.

I don't have the exact recipe but that's the general idea. Does it make sense?

Anyway, not insisting on anything w.r.t the two steps, just that the end results checks the input fileattr are supported by the fs.

Thanks,
Amir.

#31 lore raw

Re: [PATCH v3 3/5] vfs: add COMPR/ENCRYPT xflags from FS_COMPR_FL/FS_ENCRYPT_FL

In reply to koraynilay
Quoted 23 lines · koraynilay
Quoted 5 lines · Amir Goldstein
Quoted 1 line · koraynilay

"vfs: add COMPR/ENCRYPT xflags from FS_COMPR_FL/FS_ENCRYPT_FL"

Quoted 8 lines

This is not the best description of this change.

A better description would be

"vfs: advertise COMPR/ENCRYPT xflags in file_getattr()"

Which is exactly the behavior change that I asked you to spell out. You spelled it out in the cover letter, but not here.

Hmm ok yeah now I understand what you mean.
I should make the commit more about "let userspace know we have these too" instead of just a "add missing flags".

Quoted 2 lines

The commit message should also mention FS_IOC_FSGETXATTR but for brevity the subject above is enough.

Quoted 8 lines

what about this?

"vfs: fileattr: advertise COMPR/ENCRYPT xflags on get"

and the masks patch could become:

"vfs: fileattr: add xflags masks to internal and user API"

I see no reason to go there

"fs: advertise COMPR/ENCRYPT xflags in file_getattr()"

is better because it names the actual syscall name.

fs: is more standard practice than vfs: in this code base.

fs: add xflags masks to fileattr API

is just fine as a subject IMO.

Quoted 35 lines
Quoted 1 line

The commit message should also mention FS_IOC_FSGETXATTR

(And I'll add this to the description)

Quoted 18 lines
Quoted 7 lines

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.

This sentence is very hard to understand, because it is missing some important information.
The CASEFOLD flag (and now COMPR) is only read-write via the legacy SETFLAGS ioctl and read-only via the xflag apis.
Also in your documentation patch, you list them as RO/RW which does not fully convey this meaning.
Don't forget why you started this patch set - BTIME is also going to be RO on some fs and RW on others, but in a very different category than those two.

Quoted 1 line
Quoted 5 lines
Quoted 3 lines

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.

... in all the uapi flavors.

Hmm, yes, but I should mention both "uapi flavors" and "filesystems", since casefold can be toggled only with SETFLAGS, but not on all filesystems, while saying that ENCRYPT is read-only for all uapi flavours doesn't mean that no filesystems supports setting it (well, in this case that's true, but in general there could be a filesystem that decides to expose a FS-specific ioctl to enable/disable FS_XFLAG_ENCRYPT and that reports FS_XFLAG_ENCRYPT in fileattr_get).

Quoted 3 lines

Anyway, I'll change the message to be more easily readable and mention these cases.

Just try to keep it minimal but also accurate.

The fact that ENCRYPT is settable via another API is very much non interesting for this context, so just need to find a phrasing that is not incorrect but also conveys the rules about when it is ok to put a flag in the common mask.

Thanks,
Amir.

author
#11 lore raw

Re: [PATCH v3 2/5] vfs: add xflags masks to fileattr API

In reply to Amir Goldstein
Quoted 211 lines · Amir Goldstein
Quoted 210 lines · 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
---
 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(-)
fs/file_attr.c +72 -9
index 40d6518f1d8e..5d3834f4405b 100644
@@ -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;
 }
 
Quoted 10 lines

fs/file_attr.c static int copy_fsxattr_from_user(struct file_kattr *fa, · line 244

Quoted 4 lines
fs/file_attr.c +4 -0
+/*
+ * Convert RMW input format without masks, coming from userspace, to input
+ * format with masks, for filesystems that already support masks.
+ */

Sorry I am not fond of this terminology as well It's not like the new API cannot be used RMW style. I suggest to stick with plain boring converter names like fileattr_set_v0_to_v1() similar to FS_XFLAGS_VER[01]_MASK comment below.

I really don't like v0_to_v1 or similar, because it's not informative on what it's doing IMO.

Quoted 1 line

It's not like the new API cannot be used RMW style.

If the new API is used RMW style, it will still have masks tho, so if userspace does a read-modify-write cycle using masks, the semantics of the action would change from "I read the old file, I'll change only what I want, then I'll persist the changes" to "I read the old file; now I want to write all the flags and each needs to have this value".

In my mind at least.

Quoted 167 lines
Quoted 135 lines
fs/file_attr.c +135 -0
+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.

fs/file_attr.c static int copy_fsxattr_from_user(struct file_kattr *fa, · line 389

Quoted 10 lines
fs/file_attr.c +10 -0
+ */
+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;

This is where the single has_masks bites twice.

fa->has_masks is supposed to mean "caller specified AT_FILEATTR_MASKS", but for in-kernel get+modify+set callers (ovl_copy_fileattr(), ksmbd_vfs_set_compression()) it was set by vfs_fileattr_get() of a fs that reports masks, so their legacy requests are passed through as masked requests instead of going through rmw_to_mask().

For ksmbd over ntfs, FS_COMPR_FL is not in the rw mask, so it is silently ignored instead of failing with -EOPNOTSUPP as ksmbd expects.

old_ma->has_masks is used as "fs accepts masks in set", but it really means "fs reports masks in get", which is the problem Lauri reported for ocfs2.

As I wrote in reply to Lauri, these should be three different properties:
- caller has specified masks (AT_FILEATTR_MASKS => has_masks)
- fs reported masks in get (masks_valid?)
- fs can accept masks in set (accepts_masks?)

Quoted 8 lines

That last hint is the one that would help Lauri's case and read-only btime and it remains vfs internal, not exposed to userspace for now.

Note that, as you have seen for yourself in the FUSE bug that you fixed, overloading the _valid hints for both directions was a source of bugs. If the _valid hints were split into hints from the caller and hints from the filesystem, things might have been less confusing (but you don't need to change that now).

I thought about this for the past few hours.

For the 2nd field, I chose use_masks:

bool has_masks:1; /* struct has valid masks (get, only out) */
bool use_masks:1; /* equivalent to AT_FILEATTR_MASKS but for
in-kernel calls (set, only in) */

(current WIP diff [1])

While I don't like much having a 3rd flag for SETFLAGS (hence my comment above the EOPNOTSUPP return), I gave a quick look at ocfs2's code, and I can see how it could be not so trivial to switch to xflags (especially the testing), so I can convince myself it's fine if it's a temporary field before this is finished:

Quoted 5 lines

The long term goal is for all fs methods to be converted to deal only with the canonical masked format, so they don't need to carry legacy code and all conversions will be done in the vfs. A fs should accept masks in set only when all its writable flags can be expressed with xflags, which may require adding new xflags first (e.g. DIRSYNC).

(Which means it will become a permanent field, but anyway)

Quoted 112 lines
Quoted 101 lines
fs/file_attr.c +50 -10
+       }
+
+       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;
 }

fs/file_attr.c int vfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry, · line 540

Quoted 6 lines
fs/file_attr.c +3 -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;
+

I understand the logic behind zero rw_mask means do nothing, but it looks to me like setting a trap for programs that read from old fs (zero rw_mask) and set the same fa blob to new fs.

Quoted 3 lines

It is a variant of the caution clause in your patch 5 doc.

IMO, AT_FILEATTR_MASKS should treat zero rw_masks as EINVAL.

Yeah, that is probably better in the end.

Quoted 5 lines

Alternatively, I hate to change everything again, but maybe we made a bad choice with AT_FILEATTR_MASKS, because using FS_XFLAG_HASMASKS may have been better for all the backup/sync cases?
I leave it up to you if you want to explore this alley.

Eh, unless I missed something, when I was thinking about using FS_XFLAG_HASMASKS, either we use it like AT_FILEATTR_MASKS rn and strip it before calling vfs_fileattr_g/set, so it wouldn't make sense even adding it vs keeping AT_FILEATTR_MASKS, or we use it instead of has_masks, but since it would be in fa/fsx_xflags, it would then be a nightmare to do operations/checks on the actual flags, since the FS_XFLAG_HASMASKS bit would alway be one (when using masks).

So I think it's fine to just return -EINVAL on 0 rw_mask, the only use case I can think of where using 0 as rw_mask would make sense is probing the fs for support/permissions.

Quoted 9 lines

fs/file_attr.c int vfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry, · line 291

Quoted 7 lines
fs/file_attr.c +0 -1
        inode_lock(inode);
        err = vfs_fileattr_get(dentry, &old_ma);
        if (err)
                goto out;
 
-       /* initialize missing bits from old_ma */

Is this comment not still relevant in this context after get of old_ma?

Yes, but I think it's better to keep it on above the same if (which is why I copied it to set_convert along with the if).

Quoted 142 lines
Quoted 38 lines
fs/file_attr.c +9 -11
-       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);

fs/file_attr.c SYSCALL_DEFINE5(file_getattr, int, dfd, const char __user *, filename, · line 683

Quoted 8 lines
fs/file_attr.c +5 -0
@@ -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.
+        */

See comment below on FS_XFLAGS_VER0_MASK

Quoted 75 lines
fs/file_attr.c +22 -5
+       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),
include/linux/fileattr.h +5 -0
index 09e32b84e02a..fb158269b4ea 100644
@@ -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 | \

include/linux/fileattr.h line 45

Quoted 6 lines
include/linux/fileattr.h +3 -0
@@ -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)

I think these two similarly named constants will be confusing for coders and reviewers and the special-casing of PROJID is a pain for documentation.

How about FS_XFLAGS_VER[01]_MASK?
Do not define FS_XFLAGS_MASK so each macro
user will need to be converted to use the correct macro.

Using alias FS_XFLAGS_LATEST_MASK could read nicely in some common helpers, e.g.:

Quoted 6 lines
        fa->fsx_xflags &= FS_XFLAGS_LATEST_MASK;
        return 0;

explicit VER1 could read nicer in other helpers, e.g.

        u64 mask = fa->has_masks ? FS_XFLAGS_VER1_MASK : FS_XFLAGS_VER0_MASK;

Hmm, I don't think I like this approach too much, since right now my thinking for FS_XFLAGS_MASK is

"mask of only the already existing xflags + any xflag being added from the FS_*_FL counterpart"

while FS_FILE_ATTR_XFLAGS_MASK is

"FS_XFLAGS_MASK + all the future xflags that will be added (like MAYSHARE and BTIME)"

For PROJID, personally it could also just be put in FS_XFLAGS_MASK, my concern is that it may break programs, even tho fsx_xflags is already expected to grow (as per your 276e136bff7e ("fs: prepare for extending file_get/setattr()")).

Thanks,
koraynilay

Quoted 2 lines

Thanks,
Amir.

[1]:

fs/file_attr.c +17 -2
index 314d779e41d6..4eda4c165ac6 100644
@@ -185,7 +185,7 @@ static int file_attr_to_fileattr(const struct file_attr *fattr,
 	if (fattr->fa_xflags & ~mask)
 		return -EINVAL;
 
-	if (fa->has_masks) {
+	if (fa->use_masks && fa->has_masks) {
 		u64 rw_user_mask = fattr->fa_xflags_rw_mask;
 
 		if (rw_user_mask & ~FS_FILE_ATTR_XFLAGS_MASK)
@@ -301,6 +301,7 @@ static int fileattr_set_rmw_to_mask(struct file_kattr *fa,
 	fa->fsx_xflags = new_xflags;
 	fa->fsx_xflags_rw_mask = new_rw_mask;
 	fa->has_masks = true;
+	fa->use_masks = true;
 
 	fa->fsx_valid = true;
 	fa->flags_valid = false;
@@ -374,6 +375,7 @@ static int fileattr_set_mask_to_rmw(struct file_kattr *fa,
 	fa->fsx_xflags = new_xflags;
 	fa->fsx_xflags_rw_mask = 0;
 	fa->has_masks = false;
+	fa->use_masks = false;
 
 	fa->fsx_valid = true;
 	fa->flags_valid = false;
@@ -543,8 +545,19 @@ int vfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry,
 	if (!inode_owner_or_capable(idmap, inode))
 		return -EPERM;
 
+	if (fa->use_masks && !fa->has_masks)
+		return -EINVAL;
+
+	/* This comes from an older call or caller wanted VER0 RMW semantics */
+	if (!fa->use_masks)
+		fa->has_masks = false;
+
+	// should probably also verify run rmw_
+	//fa->has_masks = fa->use_masks;
+
 	if (fa->has_masks && !fa->fsx_xflags_rw_mask)
 		return 0;
+	//	return -EINVAL;
 
 	inode_lock(inode);
 	err = vfs_fileattr_get(dentry, &old_ma);
@@ -754,8 +767,10 @@ SYSCALL_DEFINE5(file_setattr, int, dfd, const char __user *, filename,
 			return error;
 	}
 
-	if (at_flags & AT_FILEATTR_MASKS)
+	if (at_flags & AT_FILEATTR_MASKS) {
 		fa.has_masks = true;
+		fa.use_masks = true;
+	}
 
 	error = file_attr_to_fileattr(&fattr, &fa);
 	if (error)
fs/ntfs/file.c +1 -0
index f307d97b929e..6d15dfd07780 100644
@@ -187,6 +187,7 @@ int ntfs_fileattr_get(struct dentry *dentry, struct file_kattr *fa)
 	fa->fsx_xflags_ro_mask = NTFS_RO_XFLAGS;
 	fa->fsx_xflags_rw_mask = NTFS_RW_XFLAGS;
 	fa->has_masks = true;
+	fa->use_masks = false;
 	return 0;
 }
 
include/linux/fileattr.h +2 -1
index 9a8de4190133..a5cbec020f16 100644
@@ -64,7 +64,8 @@ struct file_kattr {
 	/* selectors: */
 	bool	flags_valid:1;
 	bool	fsx_valid:1;
-	bool	has_masks:1;
+	bool	has_masks:1;	/* struct has valid masks (get, only out) */
+	bool	use_masks:1;	/* equivalent to AT_FILEATTR_MASKS but for in-kernel calls (set, only in) */
 };
 
 int copy_fsxattr_to_user(const struct file_kattr *fa, struct fsxattr __user *ufa);
author
#32 lore raw

Re: [PATCH v3 3/5] vfs: add COMPR/ENCRYPT xflags from FS_COMPR_FL/FS_ENCRYPT_FL

In reply to Amir Goldstein
Quoted 30 lines · Amir Goldstein
Quoted 29 lines · koraynilay
Quoted 5 lines · Amir Goldstein
Quoted 1 line · koraynilay

"vfs: add COMPR/ENCRYPT xflags from FS_COMPR_FL/FS_ENCRYPT_FL"

Quoted 8 lines

This is not the best description of this change.

A better description would be

"vfs: advertise COMPR/ENCRYPT xflags in file_getattr()"

Which is exactly the behavior change that I asked you to spell out. You spelled it out in the cover letter, but not here.

Hmm ok yeah now I understand what you mean.
I should make the commit more about "let userspace know we have these too" instead of just a "add missing flags".

Quoted 2 lines

The commit message should also mention FS_IOC_FSGETXATTR but for brevity the subject above is enough.

what about this?

"vfs: fileattr: advertise COMPR/ENCRYPT xflags on get"

and the masks patch could become:

Quoted 8 lines
Quoted 2 lines

"vfs: fileattr: add xflags masks to internal and user API"

I see no reason to go there

"fs: advertise COMPR/ENCRYPT xflags in file_getattr()"

is better because it names the actual syscall name.

Hmm ok.

Quoted 1 line

fs: is more standard practice than vfs: in this code base.

Ah, I did see a bunch of commits had only fs: but didn't think it was most of them, will change it.

Quoted 43 lines

fs: add xflags masks to fileattr API

is just fine as a subject IMO.

Quoted 35 lines
Quoted 1 line

The commit message should also mention FS_IOC_FSGETXATTR

(And I'll add this to the description)

Quoted 18 lines
Quoted 7 lines

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.

This sentence is very hard to understand, because it is missing some important information.
The CASEFOLD flag (and now COMPR) is only read-write via the legacy SETFLAGS ioctl and read-only via the xflag apis.
Also in your documentation patch, you list them as RO/RW which does not fully convey this meaning.
Don't forget why you started this patch set - BTIME is also going to be RO on some fs and RW on others, but in a very different category than those two.

Quoted 1 line
Quoted 5 lines
Quoted 3 lines

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.

... in all the uapi flavors.

Hmm, yes, but I should mention both "uapi flavors" and "filesystems", since casefold can be toggled only with SETFLAGS, but not on all filesystems, while saying that ENCRYPT is read-only for all uapi flavours doesn't mean that no filesystems supports setting it (well, in this case that's true, but in general there could be a filesystem that decides to expose a FS-specific ioctl to enable/disable FS_XFLAG_ENCRYPT and that reports FS_XFLAG_ENCRYPT in fileattr_get).

Quoted 3 lines

Anyway, I'll change the message to be more easily readable and mention these cases.

Quoted 6 lines

Just try to keep it minimal but also accurate.

The fact that ENCRYPT is settable via another API is very much non interesting for this context, so just need to find a phrasing that is not incorrect but also conveys the rules about when it is ok to put a flag in the common mask.

Yes, what I'll say there will probably also depend on the new comments for the constants in fileattr.h.

Thanks,
koraynilay

Quoted 2 lines

Thanks,
Amir.

author
#37 lore raw

Re: [PATCH v3 4/5] ntfs: switch to the new xflags fileattr API

In reply to Amir Goldstein
Quoted 5 lines · Amir Goldstein
Quoted 4 lines · koraynilay
Quoted 2 lines · Amir Goldstein
Quoted 1 line · koraynilay

...

Quoted 32 lines
Quoted 12 lines
Quoted 12 lines

Review

Quoted 3 lines
(quoted diff) +0 -2
-       if (fileattr_has_fsx(fa))
-               return -EOPNOTSUPP;

Something about this feels wrong, meaning that the filesystem should protect itself
from getting called with the wrong fileattr and not fully rely on vfs to do that.

This over trust is probably part of the problems you see with overlayfs and also creates problems with ksmbd over ntfs.

Quoted 8 lines
Quoted 4 lines

We discussed at some point a replacement for this helper - I am not sure what exactly it should be called or exactly doing and maybe it even needs to WARN_ON_ONCE() if an internal caller passes unexpected fa format, because this function now expects a certain fa format.

Hmm, I'll think about the best way to go about that, if returning with -EINVAL or WARN_ON_ONCE() or both.

I will answer this below..

Quoted 9 lines

Actually, inode_set_flags already does something like this:

  void inode_set_flags(struct inode *inode, unsigned int flags,
                     unsigned int mask)
  {
        WARN_ON_ONCE(flags & ~mask);
        set_mask_bits(&inode->i_flags, mask, flags);
  }

...

Quoted 42 lines
Quoted 42 lines
Quoted 17 lines
Quoted 5 lines
        }
(quoted diff) +0 -4
-       if (fa->flags & FS_NODUMP_FL)
-               linuxflags |= NTFS_LINUXFLAGS_NODUMP;
-       if (fa->flags & FS_NOATIME_FL) {
-               new_fl |= S_NOATIME;

Review

Quoted 10 lines
(quoted diff) +8 -1
-               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;
+               }
        }
Quoted 1 line

It's a bummer that email review has so much diffstat for what is essentially

Yeah and that's why I love the --word-diff and --ignore-all-spaces options.

Quoted 23 lines

indentation. Sometimes we need to accept that but I have a proposal If you like it, maybe the diffstat will become even smaller.

      if (rw_user_mask & FS_XFLAG_IMMUTABLE) {
              mask_fl |= S_IMMUTABLE;
              linuxflags_mask |= NTFS_LINUXFLAGS_IMMUTABLE;
      }
      if (fa->fsx_xflags & FS_XFLAG_IMMUTABLE) {

[ no change here and no else ]

...

        err = ntfs_ea_set_linuxflags(vi, linuxflags & ~linuxflags_mask);

consistent with inode_set_flags(vi, new_fl, mask_fl);

Whether fa->fsx_xflags check should be logically in the nested scope is a matter of taste. I personally like better the smaller patch which is easier to review, but it is not a must.

Using linuxflags_mask instead of the else is definitely better IMO, because it makes S_ flags and LINUXFLAGS handling consistent.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

WDYT?

I tried it and with a preparatory patch that only switches ntfs to use xflags it looks very clean [1].

The separation looks very nice indeed!

Nice.

Quoted 117 lines

but I would do this a bit differently. see below.

Quoted 17 lines

While I logically prefer to have them in if blocks, the new approach also makes sense because it's now 1:1 with new_fl (since set_mask_bits called by inode_set_flags does the exact same ((old & ~mask) | new).

Thanks,
koraynilay

Quoted 2 lines

Thanks,
Amir.

[1]:

commit ccda307355008ba86354128d934f7ab97be62a96

Author: koraynilay <[email protected]>
Date:   Sun Nov 22 00:17:29 2026 +0100
Quoted 8 lines

ntfs: support the fileattr API masks in ntfs_fileattr_get/set()

Add support for the new fileattr API with masks in ntfs_fileattr_get() and ntfs_fileattr_set().

This also allows us to remove checks for unsupported xflags, since VFS will take care of that by using the rw and ro masks returned in ntfs_fileattr_get().

Not enough that this is "taken care of by VFS" filesystem needs to call a helper that asserts that the interface contract is respected.

Quoted 60 lines
---
 fs/ntfs/file.c | 73 +++++++++++++++++++++++++++++++---------------------------
 1 file changed, 39 insertions(+), 34 deletions(-)
fs/ntfs/file.c +12 -14
index ca088e7d636b..0a45359f3a2a 100644
@@ -155,6 +155,10 @@ 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);
@@ -179,7 +183,9 @@ int ntfs_fileattr_get(struct dentry *dentry, struct file_kattr *fa)
        if (!NVolCaseSensitive(ni->vol))
                xflags |= FS_XFLAG_CASEFOLD;
 
-       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,65 +213,64 @@ 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_XFLAG_IMMUTABLE | FS_XFLAG_APPEND | FS_XFLAG_NODUMP |
-                     FS_XFLAG_NOATIME;
-       u32 readonly = 0;
-       u32 linuxflags = ni->linuxflags & ~NTFS_LINUXFLAGS_MASK;
-       unsigned int new_fl = 0;
-       bool forced_immutable;
+       u32 linuxflags = ni->linuxflags;
+       u32 rw_user_mask = fa->fsx_xflags_rw_mask;
+       unsigned int new_fl = 0, mask_fl = 0;
+       unsigned int new_linuxflags = 0, mask_linuxflags = 0;
+       bool forced_immutable = false;
        int err;
 
        err = ntfs_emergency_state(ni->vol);
        if (err)
                return err;
 
-       if (fa->fsx_extsize || fa->fsx_cowextsize || fa->fsx_projid)
-               return -EOPNOTSUPP;
-
-       /* chattr passes the unchanged read-only flags back to us too. */
-       if (NInoCompressed(ni) || NInoWofCompressed(ni))
-               readonly |= FS_XFLAG_COMPR;
-       if (NInoEncrypted(ni))

fs/ntfs/file.c int ntfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry, · line 238

Quoted 10 lines
fs/ntfs/file.c +0 -10
-               readonly |= FS_XFLAG_ENCRYPT;
-       if ((fa->fsx_xflags ^ readonly) & (FS_XFLAG_COMPR | FS_XFLAG_ENCRYPT))
-               return -EOPNOTSUPP;
-       allowed |= readonly;
-
-       /* Case folding is a read-only, mount-wide property. */
-       if (!NVolCaseSensitive(ni->vol))
-               allowed |= FS_XFLAG_CASEFOLD;
-       if (fa->fsx_xflags & ~allowed)
-               return -EOPNOTSUPP;

if (fileattr_has_unsupported_xflags(fa, NTFS_RO_XFLAGS,
NTFS_RW_XFLAGS))

                   return -EOPNOTSUPP;

Reviewer can see that all the open coded cruft above is replaced with a generic helper that does exactly the same.

I don't care if it's _supported_ or _unsupported_ or some other name. In any case, SET should not assume that the caller called GET and did all the checks. It's just too much to assume.

I am not sure that vfs is always going to do GET before SET. It simply does not make sense to do that IMO for
a caller that only asked to set one flag/value.

Quoted 6 lines

I am not asking you to make the GET before SET conditional now, but asking to prepare for this eventuality.

All you need to do is that a converted filesystem set method has some entry check for valid/supported fa.
This is both more readable and prepares for SET without GET.

(Answer at the end)

Quoted 1 line
Quoted 1 line

[...]

Quoted 67 lines
Quoted 57 lines

commit 347c2201ae065d608e9aee9bac2e93f9b9418156

Author: koraynilay <[email protected]>
Date:   Sun Nov 29 18:10:31 2026 +0100

ntfs: switch from flags to xflags in ntfs_fileattr_get/set()

Switch ntfs_fileattr_get() and ntfs_fileattr_set() to use FS_XFLAG_* xflags instead of FS_*_FL flags.

---
 fs/ntfs/file.c | 44 ++++++++++++++++++++++----------------------
 1 file changed, 22 insertions(+), 22 deletions(-)
fs/ntfs/file.c +9 -9
index 50be49f4c913..ca088e7d636b 100644
@@ -159,27 +159,27 @@ 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);
        return 0;
 }
 

fs/ntfs/file.c int ntfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry, · line 211

Quoted 7 lines
fs/ntfs/file.c +2 -2
@@ -207,8 +207,8 @@ 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 allowed = FS_XFLAG_IMMUTABLE | FS_XFLAG_APPEND | FS_XFLAG_NODUMP |
+                     FS_XFLAG_NOATIME;

opportunity: since already changing this line, define NTFS_RW_XFLAGS in this patch and set

Quoted 1 line
            u32 allowed = NTFS_RW_XFLAGS;

Hmm, won't this be too much since there is concept of RW/RO masks here?

Quoted 19 lines

fs/ntfs/file.c int ntfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry, · line 223

Quoted 10 lines
fs/ntfs/file.c +1 -1
        u32 readonly = 0;
        u32 linuxflags = ni->linuxflags & ~NTFS_LINUXFLAGS_MASK;
        unsigned int new_fl = 0;
@@ -219,42 +219,42 @@ int ntfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry,
        if (err)
                return err;
 
-       if (fileattr_has_fsx(fa))
+       if (fa->fsx_extsize || fa->fsx_cowextsize || fa->fsx_projid)
                return -EOPNOTSUPP;

This open coded check looks a bit random.
Perhaps we could start with a simple variant of fileattr_has_fsx() which takes a single mask, as we discussed before:

if (fileattr_has_unsupported_xflags(fa, NTFS_RO_XFLAGS |
NTFS_RW_XFLAGS))

                   return -EOPNOTSUPP;
Quoted 8 lines

And later, in the next patch, it is changed to a helper that takes two masks and verifies more based on the masks.

I don't have the exact recipe but that's the general idea. Does it make sense?

Anyway, not insisting on anything w.r.t the two steps, just that the end results checks the input fileattr are supported by the fs.

Ok yeah having an helper would be cleaner and better, I'll add back a fileattr_has_unsupported_xflags() helper.
Would it be fine to have it in fileattr.h like fileattr_has_fsx() is (and like fileattr_has_fsx_mask() was in my ptime series) or should I put it in file_attr.c?

Thanks,
koraynilay

Quoted 2 lines

Thanks,
Amir.

#42 lore raw

Re: [PATCH v3 5/5] docs: add fileattr API documentation

In reply to koraynilay
Quoted 59 lines · koraynilay
Quoted 39 lines · Amir Goldstein
Quoted 21 lines · koraynilay

Add documentation for the fileattr API.

---
 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
Documentation/filesystems/fileattr.rst new file +3 -0
new file mode 100644
index 000000000000..9e3515dc4517
@@ -0,0 +1,530 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+============

Documentation/filesystems/fileattr.rst line 13

Quoted 10 lines
Documentation/filesystems/fileattr.rst new file +10 -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).
+

This is nice!
Much easier to review the UAPI this way.

I would add something about the terminology "file attributes" which was a source of confusion and bikeshedding and the relation to other UAPIs:

Quoted 7 lines

The "file attributes" that this document refers to are structured file attributes
much like the inode information advertised by stat(2) and statx(2). statx(2) even advertises some of the file attributes described in this document:
File attributes
The stx_attributes field contains a set of ORed flags that
indicate additional attributes of the file.

Yes, I used "inode flags" because "file attributes" was used for a bunch of other things too (even tho the API is "fileattr").

Quoted 6 lines

The file attributes described in this document could be described as an extension to the standard inode attributes of a POSIX filesystem inode. However, "extended attributes" terminology is already used for describing the unstructured string key/blob value inode attributes of the [gs]etxattr() api,
which are something completely different.

Quoted 6 lines

I hadn't checked statx(2) but since it does report the same ones, I'll switch the terminology to just "file attributes" in the doc, maybe adding a clarification at the start (like the

Quoted 1 line
Quoted 1 line

(like immutable and append-only).

I already have) like the one you wrote here.

Yes I didnt even notice anything that needs to be changed in the doc I only aimed for a terminology clause to help readers who are confused by the similar names.

Quoted 259 lines
Quoted 67 lines
Quoted 53 lines
Documentation/filesystems/fileattr.rst new file +53 -0
+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
+------------------------------

Documentation/filesystems/fileattr.rst line 76

Quoted 10 lines
Documentation/filesystems/fileattr.rst new file +10 -0
+
+.. 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``

The "in 7.x" language is inspired by man-pages, but it has no place here because this document lives in the same repo as the code.

Quoted 3 lines

I think you should mention the versions V0 V1 somewhere in the doc and then you can refer to them instead of saying 7.x or when describing a flag which applies only to V1 (PROJID).

Ah ok, will do.

Quoted 4 lines
Quoted 4 lines
Documentation/filesystems/fileattr.rst new file +4 -0
+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.
+
Quoted 8 lines

Documentation/filesystems/fileattr.rst line 86

Quoted 6 lines
Documentation/filesystems/fileattr.rst new file +6 -0
+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.

I understand it, but don't think it is clear to readers what this note means.

Yeah I wanted to explain a bit about the conversions but I thought that since this is the userspace section it was too detailed. I'll change it to mention the format conversion without many details and make a reference to the detailed in-kernel explanation below.

Quoted 2 lines

Also please avoid text that will go stale as "Currently most filesystems" Use language that is not dependent on how many fs are converted.

Right, makes sense.

Quoted 25 lines

Nore to bot: this doc should not go stale/incorrect while filesystems are converted and support for more flags added in the future.

Quoted 22 lines
Documentation/filesystems/fileattr.rst new file +22 -0
+
+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
Quoted 8 lines

Documentation/filesystems/fileattr.rst line 114

Quoted 6 lines
Documentation/filesystems/fileattr.rst new file +6 -0
+     ``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.

This sounds like a caution for regressing sync/backup workloads

Mainly yes, I added it also because you had mentioned get -> set should work directly (so users may expect that), but since it can't I put it as caution.

Quoted 2 lines

with the new UAPI, but isn't it the same with the old UAPI when file2 is on an fs that does not accept a flag that file1 advertizes?

Yes.

Quoted 9 lines

I anything, the new UAPI improves the situation by making it transparent and testable for the sync/backup program and allows the program to make intelligent decisions based on the advertized information in the masks.

So the caution should be framed as change of behavior for backup/sync - instead of silently dropping unsupported flags, the new apis may reject them and provide the reason to the program so that it can handle the situation correctly.

Hmm yes, the main reason for this caution was non masks filesystems returning 0 as rw_mask -> no-op, which could be unexpected by programs. I'll update it so it's clearer and mentions sync/backups, since yeah, I can't really think of many use-cases where this get -> set (using masks) would make sense apart from that.

Quoted 12 lines
Quoted 12 lines
Documentation/filesystems/fileattr.rst new file +12 -0
+
+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.
Quoted 8 lines

Documentation/filesystems/fileattr.rst line 131

Quoted 5 lines
Documentation/filesystems/fileattr.rst new file +5 -0
+  - 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``):

Phrase as generic rule with examples so doc won't go stale when adding new value fields.

Right.

Quoted 6 lines
Quoted 6 lines
Documentation/filesystems/fileattr.rst new file +6 -0
+
+  - 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).
+
Quoted 8 lines

Documentation/filesystems/fileattr.rst line 143

Quoted 6 lines

+- Validation and capability rules:

Documentation/filesystems/fileattr.rst new file +5 -0
+
+  - 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

Sorry this is too ugly to live.

Lmao.
Yeah and FS_XFLAGS_MASK etc aren't even exposed to userspace, forgot about that.

Quoted 3 lines

Need to do something about this.
Nobody can understand and follow this PROJID exemption it needs to be better abstracted.

Hmm, maybe in the userspace part we should treat FS_XFLAG_PROJID as part of FS_XFLAGS_MASK, since that's basically how we treat it as, and I'll rephrase that so it's actually understandable by people.

Quoted 56 lines
Quoted 40 lines
Documentation/filesystems/fileattr.rst new file +39 -0
+    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``)::

Documentation/filesystems/fileattr.rst line 192

Quoted 10 lines
Documentation/filesystems/fileattr.rst new file +10 -0
+
+    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".

I still very much dislike this.
Yes, it is elegant and consistent, but humans sometimes have expectations from apis which are different than documentation and this becomes a source of bugs.

Quoted 7 lines
Quoted 2 lines

I ask again, what do we lose by requiring fa.fa_projid = 0 along with clearing FS_XFLAG_PROJID?

The possibility for filesystems to use a value that is not 0 to mean "unset" for a specific field, but since

Quoted 1 line

We can always relax it later if the need arises

that's fair.

Exactly. We do not gain ANYTHING from doing this now. By treating this as EINVAL now, we reserve the right to relax it in the future. The other way (deciding to disallow it later) is much less nice for users and UAPI breakage.

Quoted 10 lines
Quoted 5 lines

and in the meanwhile
we make sure that all programs do not reset projid by mistake. This is a bug so easy to write by mistake or by using a stale copy of the constant FS_XFLAGS_MASK in a program, which copied it from the kernel headers.

You mean someone forgetting to set FS_XFLAG_PROJID in fa_xflags and setting it only in rw_mask (along with fa_projid > 0), since PROJID in fa_xflags was never required before (unlike extsize/cowextsize)?

Yes, something like that or as part of some RMW cycle during backup/sync.

Quoted 3 lines

So should we require fa_value == 0 when fa_xflags doesn't have VALUE also for extsize/cowextsize too?

Yes, in my opinion we should.
For the same reason - there is no use case that makes sense for any program to do that. And by denying it now, we could still relax it later, but not the other way around.

Quoted 61 lines
Quoted 2 lines

People do not read the fine print of documentation and man pages when writing software!

Yeah fair.

Quoted 47 lines
Quoted 47 lines
Documentation/filesystems/fileattr.rst new file +47 -0
+
+.. _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``.                                  |
++--------------------------------+------------+--------+-------------------------------------------------------+
Quoted 8 lines
Quoted 6 lines
+| ``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. |

RO/RW not very clear. Maybe a footnote RO/RW[*] for RW only via SETFLAGS

Quoted 4 lines

Huh why only via SETFLAGS? It should be settable also by file_setattr once filesystems that support it as rw switch to using xflags (without even the need for supporting masks) no?

Ah ok so something like this?
[*] depends on the filesystem

The comment was just that it's not clear what RO/RW mode means

Quoted 199 lines
Quoted 75 lines
Quoted 4 lines

++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``FS_XFLAG_CASENONPRESERVING`` | 0x00080000 | RO | Case not preserved in directory lookups. |
++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``FS_XFLAG_PROJID`` | 0x00100000 | RW | Project ID value flag (Linux >= 7.X). |

No 7.x please, V1 or something

Quoted 3 lines

+| | | | Updates require ``init_user_ns``. |
++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``FS_XFLAG_COMPR`` | 0x00200000 | RO/RW | Compressed file flag. |

Same RO/RW[*]

Quoted 62 lines

++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``FS_XFLAG_ENCRYPT`` | 0x00400000 | RO | Encrypted file flag (fscrypt). Read-only. |
++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``FS_XFLAG_HASATTR`` | 0x80000000 | RO | Inode has extended attributes. Read-only. |
++--------------------------------+------------+--------+-------------------------------------------------------+

Documentation/filesystems/fileattr.rst new file +57 -0
+
+
+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``
Quoted 8 lines

Documentation/filesystems/fileattr.rst line 320

Quoted 6 lines
Documentation/filesystems/fileattr.rst new file +6 -0
+    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.

Probably better as Kerneldoc in the patch itself.

Ah yeah, I forgot to add an explanation comment there.

Quoted 104 lines
Quoted 104 lines
Documentation/filesystems/fileattr.rst new file +104 -0
+
+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``        |
++------------------------+--------------------------------+
Quoted 8 lines
Quoted 6 lines

+| | ``FS_XFLAG_CASENONPRESERVING`` |
++------------------------+--------------------------------+
+| | ``FS_XFLAG_PROJID`` |
++------------------------+--------------------------------+
+| | ``FS_XFLAG_HASATTR`` |
++------------------------+--------------------------------+

Come on! This is writing the code in the doc.

Quoted 5 lines

I like having the visual table here though :(
since in the headers they're just separate lists (and actually, I thought the overlap between FS_*_FL flags and FS_XFLAG_* xflags was a lot more before having this table).

ok.

Quoted 9 lines
Quoted 6 lines

This document should tell the high level story of why those converters exist and when and why they should be used by vfs and/or filesystems.

The documentation of each helper interface belongs in Kerneldoc not here.

Right, for now I only added a simple comments but I should add the full kernel-doc for them.

Well you do not have to add a full kernel doc for the static helpers. I just meant if you are going to describe some helper full kerneldoc style here it would be better doing that in the code, but I guess this is not the case.

Quoted 4 lines

Kinda related, but should we EXPORT_SYMBOL the rmw_to_mask and back helpers?

Why? Do any kernel modules need them?

Quoted 37 lines
Quoted 28 lines
Quoted 28 lines
Documentation/filesystems/fileattr.rst new file +27 -0
+
+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,
Quoted 8 lines

Documentation/filesystems/fileattr.rst line 462

Quoted 5 lines
Documentation/filesystems/fileattr.rst new file +5 -0
+  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.
+

This is a hard read, try to simplify if you can, less interface, more high level maybe. not sure.

Quoted 3 lines

Hmm, for me it's high level enough, but since I already know what these functions do I'll ask friends/other people too and take it from there.

Let's see how that looks after rename to fileattr_set_v0_to_v1() and with the has_masks/masks_valid/accepts_mask properties. If you accept all of the above suggestions, then we will see how it affects documentation - hopefully for the better.

Thanks,
Amir.

#38 lore raw

Re: [PATCH v3 4/5] ntfs: switch to the new xflags fileattr API

In reply to koraynilay
Quoted 314 lines · koraynilay
Quoted 5 lines · Amir Goldstein
Quoted 4 lines · koraynilay
Quoted 2 lines · Amir Goldstein
Quoted 1 line · koraynilay

...

Quoted 32 lines
Quoted 12 lines
Quoted 12 lines

Review

Quoted 3 lines
(quoted diff) +0 -2
-       if (fileattr_has_fsx(fa))
-               return -EOPNOTSUPP;

Something about this feels wrong, meaning that the filesystem should protect itself
from getting called with the wrong fileattr and not fully rely on vfs to do that.

This over trust is probably part of the problems you see with overlayfs and also creates problems with ksmbd over ntfs.

Quoted 8 lines
Quoted 4 lines

We discussed at some point a replacement for this helper - I am not sure what exactly it should be called or exactly doing and maybe it even needs to WARN_ON_ONCE() if an internal caller passes unexpected fa format, because this function now expects a certain fa format.

Hmm, I'll think about the best way to go about that, if returning with -EINVAL or WARN_ON_ONCE() or both.

I will answer this below..

Quoted 9 lines

Actually, inode_set_flags already does something like this:

  void inode_set_flags(struct inode *inode, unsigned int flags,
                     unsigned int mask)
  {
        WARN_ON_ONCE(flags & ~mask);
        set_mask_bits(&inode->i_flags, mask, flags);
  }

...

Quoted 42 lines
Quoted 42 lines
Quoted 17 lines
Quoted 5 lines
        }
(quoted diff) +0 -4
-       if (fa->flags & FS_NODUMP_FL)
-               linuxflags |= NTFS_LINUXFLAGS_NODUMP;
-       if (fa->flags & FS_NOATIME_FL) {
-               new_fl |= S_NOATIME;

Review

Quoted 10 lines
(quoted diff) +8 -1
-               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;
+               }
        }
Quoted 1 line

It's a bummer that email review has so much diffstat for what is essentially

Yeah and that's why I love the --word-diff and --ignore-all-spaces options.

Quoted 23 lines

indentation. Sometimes we need to accept that but I have a proposal If you like it, maybe the diffstat will become even smaller.

      if (rw_user_mask & FS_XFLAG_IMMUTABLE) {
              mask_fl |= S_IMMUTABLE;
              linuxflags_mask |= NTFS_LINUXFLAGS_IMMUTABLE;
      }
      if (fa->fsx_xflags & FS_XFLAG_IMMUTABLE) {

[ no change here and no else ]

...

        err = ntfs_ea_set_linuxflags(vi, linuxflags & ~linuxflags_mask);

consistent with inode_set_flags(vi, new_fl, mask_fl);

Whether fa->fsx_xflags check should be logically in the nested scope is a matter of taste. I personally like better the smaller patch which is easier to review, but it is not a must.

Using linuxflags_mask instead of the else is definitely better IMO, because it makes S_ flags and LINUXFLAGS handling consistent.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

WDYT?

I tried it and with a preparatory patch that only switches ntfs to use xflags it looks very clean [1].

The separation looks very nice indeed!

Nice.

Quoted 117 lines

but I would do this a bit differently. see below.

Quoted 17 lines

While I logically prefer to have them in if blocks, the new approach also makes sense because it's now 1:1 with new_fl (since set_mask_bits called by inode_set_flags does the exact same ((old & ~mask) | new).

Thanks,
koraynilay

Quoted 2 lines

Thanks,
Amir.

[1]:

commit ccda307355008ba86354128d934f7ab97be62a96

Author: koraynilay <[email protected]>
Date:   Sun Nov 22 00:17:29 2026 +0100
Quoted 8 lines

ntfs: support the fileattr API masks in ntfs_fileattr_get/set()

Add support for the new fileattr API with masks in ntfs_fileattr_get() and ntfs_fileattr_set().

This also allows us to remove checks for unsupported xflags, since VFS will take care of that by using the rw and ro masks returned in ntfs_fileattr_get().

Not enough that this is "taken care of by VFS" filesystem needs to call a helper that asserts that the interface contract is respected.

Quoted 60 lines
---
 fs/ntfs/file.c | 73 +++++++++++++++++++++++++++++++---------------------------
 1 file changed, 39 insertions(+), 34 deletions(-)
fs/ntfs/file.c +12 -14
index ca088e7d636b..0a45359f3a2a 100644
@@ -155,6 +155,10 @@ 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);
@@ -179,7 +183,9 @@ int ntfs_fileattr_get(struct dentry *dentry, struct file_kattr *fa)
        if (!NVolCaseSensitive(ni->vol))
                xflags |= FS_XFLAG_CASEFOLD;
 
-       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,65 +213,64 @@ 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_XFLAG_IMMUTABLE | FS_XFLAG_APPEND | FS_XFLAG_NODUMP |
-                     FS_XFLAG_NOATIME;
-       u32 readonly = 0;
-       u32 linuxflags = ni->linuxflags & ~NTFS_LINUXFLAGS_MASK;
-       unsigned int new_fl = 0;
-       bool forced_immutable;
+       u32 linuxflags = ni->linuxflags;
+       u32 rw_user_mask = fa->fsx_xflags_rw_mask;
+       unsigned int new_fl = 0, mask_fl = 0;
+       unsigned int new_linuxflags = 0, mask_linuxflags = 0;
+       bool forced_immutable = false;
        int err;
 
        err = ntfs_emergency_state(ni->vol);
        if (err)
                return err;
 
-       if (fa->fsx_extsize || fa->fsx_cowextsize || fa->fsx_projid)
-               return -EOPNOTSUPP;
-
-       /* chattr passes the unchanged read-only flags back to us too. */
-       if (NInoCompressed(ni) || NInoWofCompressed(ni))
-               readonly |= FS_XFLAG_COMPR;
-       if (NInoEncrypted(ni))

fs/ntfs/file.c int ntfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry, · line 238

Quoted 10 lines
fs/ntfs/file.c +0 -10
-               readonly |= FS_XFLAG_ENCRYPT;
-       if ((fa->fsx_xflags ^ readonly) & (FS_XFLAG_COMPR | FS_XFLAG_ENCRYPT))
-               return -EOPNOTSUPP;
-       allowed |= readonly;
-
-       /* Case folding is a read-only, mount-wide property. */
-       if (!NVolCaseSensitive(ni->vol))
-               allowed |= FS_XFLAG_CASEFOLD;
-       if (fa->fsx_xflags & ~allowed)
-               return -EOPNOTSUPP;

if (fileattr_has_unsupported_xflags(fa, NTFS_RO_XFLAGS,
NTFS_RW_XFLAGS))

                   return -EOPNOTSUPP;

Reviewer can see that all the open coded cruft above is replaced with a generic helper that does exactly the same.

I don't care if it's _supported_ or _unsupported_ or some other name. In any case, SET should not assume that the caller called GET and did all the checks. It's just too much to assume.

I am not sure that vfs is always going to do GET before SET. It simply does not make sense to do that IMO for
a caller that only asked to set one flag/value.

Quoted 6 lines

I am not asking you to make the GET before SET conditional now, but asking to prepare for this eventuality.

All you need to do is that a converted filesystem set method has some entry check for valid/supported fa.
This is both more readable and prepares for SET without GET.

(Answer at the end)

Quoted 1 line
Quoted 1 line

[...]

Quoted 67 lines
Quoted 57 lines

commit 347c2201ae065d608e9aee9bac2e93f9b9418156

Author: koraynilay <[email protected]>
Date:   Sun Nov 29 18:10:31 2026 +0100

ntfs: switch from flags to xflags in ntfs_fileattr_get/set()

Switch ntfs_fileattr_get() and ntfs_fileattr_set() to use FS_XFLAG_* xflags instead of FS_*_FL flags.

---
 fs/ntfs/file.c | 44 ++++++++++++++++++++++----------------------
 1 file changed, 22 insertions(+), 22 deletions(-)
fs/ntfs/file.c +9 -9
index 50be49f4c913..ca088e7d636b 100644
@@ -159,27 +159,27 @@ 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);
        return 0;
 }
 

fs/ntfs/file.c int ntfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry, · line 211

Quoted 7 lines
fs/ntfs/file.c +2 -2
@@ -207,8 +207,8 @@ 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 allowed = FS_XFLAG_IMMUTABLE | FS_XFLAG_APPEND | FS_XFLAG_NODUMP |
+                     FS_XFLAG_NOATIME;

opportunity: since already changing this line, define NTFS_RW_XFLAGS in this patch and set

Quoted 1 line
            u32 allowed = NTFS_RW_XFLAGS;

Hmm, won't this be too much since there is concept of RW/RO masks here?

Quoted 25 lines

fs/ntfs/file.c int ntfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry, · line 223

Quoted 10 lines
fs/ntfs/file.c +1 -1
        u32 readonly = 0;
        u32 linuxflags = ni->linuxflags & ~NTFS_LINUXFLAGS_MASK;
        unsigned int new_fl = 0;
@@ -219,42 +219,42 @@ int ntfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry,
        if (err)
                return err;
 
-       if (fileattr_has_fsx(fa))
+       if (fa->fsx_extsize || fa->fsx_cowextsize || fa->fsx_projid)
                return -EOPNOTSUPP;

This open coded check looks a bit random.
Perhaps we could start with a simple variant of fileattr_has_fsx() which takes a single mask, as we discussed before:

if (fileattr_has_unsupported_xflags(fa, NTFS_RO_XFLAGS |
NTFS_RW_XFLAGS))

                   return -EOPNOTSUPP;

And later, in the next patch, it is changed to a helper that takes two masks and verifies more based on the masks.

I don't have the exact recipe but that's the general idea. Does it make sense?

Quoted 8 lines
Quoted 2 lines

Anyway, not insisting on anything w.r.t the two steps, just that the end results checks the input fileattr are supported by the fs.

Ok yeah having an helper would be cleaner and better, I'll add back a fileattr_has_unsupported_xflags() helper.
Would it be fine to have it in fileattr.h like fileattr_has_fsx() is (and like fileattr_has_fsx_mask() was in my ptime series) or should I put it in file_attr.c?

For the first step, like your old fileattr_has_fsx_mask() sure makes sense.

For the second step, I am not sure what the helper that verifies that the ro flags are not changed looks like.

Maybe it is just as simple. I dont have the full picture yet.

Thanks,
Amir.

author
#12 lore raw

Re: [PATCH v3 2/5] vfs: add xflags masks to fileattr API

In reply to koraynilay

include/linux/fileattr.h struct file_kattr { · line 67

Quoted 11 lines
include/linux/fileattr.h +1 -1
index 9a8de4190133..a5cbec020f16 100644
@@ -64,7 +64,8 @@ struct file_kattr {
 	/* selectors: */
 	bool	flags_valid:1;
 	bool	fsx_valid:1;
-	bool	has_masks:1;
+	bool	has_masks:1;	/* struct has valid masks (get, only out) */

My bad, this comment should be only /* struct has valid masks */, since it should be true when the struct has masks, not only on get.

Quoted 4 lines
include/linux/fileattr.h +1 -0
+	bool	use_masks:1;	/* equivalent to AT_FILEATTR_MASKS but for in-kernel calls (set, only in) */
 };
 
 int copy_fsxattr_to_user(const struct file_kattr *fa, struct fsxattr __user *ufa);
#13 lore raw

Re: [PATCH v3 2/5] vfs: add xflags masks to fileattr API

In reply to koraynilay
Quoted 83 lines · koraynilay
Quoted 81 lines · Amir Goldstein
Quoted 80 lines · 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

---

...

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

+/*
+ * Convert RMW input format without masks, coming from userspace, to input
+ * format with masks, for filesystems that already support masks.
+ */

Quoted 9 lines
Quoted 5 lines

Sorry I am not fond of this terminology as well It's not like the new API cannot be used RMW style. I suggest to stick with plain boring converter names like fileattr_set_v0_to_v1() similar to FS_XFLAGS_VER[01]_MASK comment below.

I really don't like v0_to_v1 or similar, because it's not informative on what it's doing IMO.

Fair enough.

Quoted 10 lines
Quoted 1 line

It's not like the new API cannot be used RMW style.

If the new API is used RMW style, it will still have masks tho, so if userspace does a read-modify-write cycle using masks, the semantics of the action would change from "I read the old file, I'll change only what I want, then I'll persist the changes" to "I read the old file; now I want to write all the flags and each needs to have this value".

In my mind at least.

I could not understand why _set_rmw_to_mask() is a good representation of what it is doing.

Please see this code for example and say if it reads better:

        if (fa->use_masks && !old_ma->accepts_masks)
                return fileattr_set_apply_masks(fa, old_ma);

        if (!fa->use_masks && old_ma->accepts_masks)
                return fileattr_set_derive_masks(fa, old_ma);

...

Quoted 44 lines
Quoted 35 lines

Review

Quoted 13 lines

+ * Handle converting between RMW and masks formats based on userspace and
+ * filesystem support.
+ * Preserve unchanged bits from old_ma.
+ */

(quoted diff) +9 -0
+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;

This is where the single has_masks bites twice.

fa->has_masks is supposed to mean "caller specified AT_FILEATTR_MASKS", but for in-kernel get+modify+set callers (ovl_copy_fileattr(), ksmbd_vfs_set_compression()) it was set by vfs_fileattr_get() of a fs that reports masks, so their legacy requests are passed through as masked requests instead of going through rmw_to_mask().

For ksmbd over ntfs, FS_COMPR_FL is not in the rw mask, so it is silently ignored instead of failing with -EOPNOTSUPP as ksmbd expects.

old_ma->has_masks is used as "fs accepts masks in set", but it really means "fs reports masks in get", which is the problem Lauri reported for ocfs2.

As I wrote in reply to Lauri, these should be three different properties:
- caller has specified masks (AT_FILEATTR_MASKS => has_masks)
- fs reported masks in get (masks_valid?)
- fs can accept masks in set (accepts_masks?)

Quoted 8 lines

That last hint is the one that would help Lauri's case and read-only btime and it remains vfs internal, not exposed to userspace for now.

Note that, as you have seen for yourself in the FUSE bug that you fixed, overloading the _valid hints for both directions was a source of bugs. If the _valid hints were split into hints from the caller and hints from the filesystem, things might have been less confusing (but you don't need to change that now).

Quoted 7 lines

I thought about this for the past few hours.

For the 2nd field, I chose use_masks:

  bool  has_masks:1;    /* struct has valid masks (get, only out) */
  bool  use_masks:1;    /* equivalent to AT_FILEATTR_MASKS but for

in-kernel calls (set, only in) */

ok.

Quoted 8 lines

(current WIP diff [1])

While I don't like much having a 3rd flag for SETFLAGS (hence my comment above the EOPNOTSUPP return), I gave a quick look at ocfs2's code, and I can see how it could be not so trivial to switch to xflags (especially the testing), so I can convince myself it's fine if it's a temporary field before this is finished:

Quoted 8 lines
Quoted 5 lines

The long term goal is for all fs methods to be converted to deal only with the canonical masked format, so they don't need to carry legacy code and all conversions will be done in the vfs. A fs should accept masks in set only when all its writable flags can be expressed with xflags, which may require adding new xflags first (e.g. DIRSYNC).

(Which means it will become a permanent field, but anyway)

Converting all fs is going to be a long mission. There is no guarantee if and when this will happen. I think that Lauri's request for ocfs2 is very reasonable and we need to find an acceptable way to address it.

Quoted 132 lines
Quoted 112 lines
Quoted 101 lines
fs/file_attr.c +50 -10
+       }
+
+       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;
 }

fs/file_attr.c int vfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry, · line 540

Quoted 6 lines
fs/file_attr.c +3 -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;
+

I understand the logic behind zero rw_mask means do nothing, but it looks to me like setting a trap for programs that read from old fs (zero rw_mask) and set the same fa blob to new fs.

Quoted 3 lines

It is a variant of the caution clause in your patch 5 doc.

IMO, AT_FILEATTR_MASKS should treat zero rw_masks as EINVAL.

Yeah, that is probably better in the end.

Quoted 5 lines

Alternatively, I hate to change everything again, but maybe we made a bad choice with AT_FILEATTR_MASKS, because using FS_XFLAG_HASMASKS may have been better for all the backup/sync cases?
I leave it up to you if you want to explore this alley.

Eh, unless I missed something, when I was thinking about using FS_XFLAG_HASMASKS, either we use it like AT_FILEATTR_MASKS rn and strip it before calling vfs_fileattr_g/set, so it wouldn't make sense even adding it vs keeping AT_FILEATTR_MASKS, or we use it instead of has_masks, but since it would be in fa/fsx_xflags, it would then be a nightmare to do operations/checks on the actual flags, since the FS_XFLAG_HASMASKS bit would alway be one (when using masks).

Quoted 4 lines

So I think it's fine to just return -EINVAL on 0 rw_mask, the only use case I can think of where using 0 as rw_mask would make sense is probing the fs for support/permissions.

good.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
(quoted diff) +0 -0
        inode_lock(inode);
        err = vfs_fileattr_get(dentry, &old_ma);
        if (err)
                goto out;
 
Quoted 7 lines
Quoted 3 lines

Review int vfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry, · line 291

Quoted 1 line
(quoted diff) +0 -1
-       /* initialize missing bits from old_ma */

Is this comment not still relevant in this context after get of old_ma?

Yes, but I think it's better to keep it on above the same if (which is why I copied it to set_convert along with the if).

ok.

...

Quoted 25 lines
Quoted 15 lines

Review int vfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry, · line 548

Quoted 3 lines
(quoted diff) +3 -0
+/* Flags only for fa_ fields */
+#define FS_FILE_ATTR_XFLAGS_MASK \
+       (FS_XFLAGS_MASK | FS_XFLAG_PROJID)

I think these two similarly named constants will be confusing for coders and reviewers and the special-casing of PROJID is a pain for documentation.

How about FS_XFLAGS_VER[01]_MASK?
Do not define FS_XFLAGS_MASK so each macro
user will need to be converted to use the correct macro.

Using alias FS_XFLAGS_LATEST_MASK could read nicely in some common helpers, e.g.:

Quoted 6 lines
        fa->fsx_xflags &= FS_XFLAGS_LATEST_MASK;
        return 0;

explicit VER1 could read nicer in other helpers, e.g.

        u64 mask = fa->has_masks ? FS_XFLAGS_VER1_MASK : FS_XFLAGS_VER0_MASK;

Hmm, I don't think I like this approach too much, since right now my thinking for FS_XFLAGS_MASK is

Quoted 7 lines

"mask of only the already existing xflags + any xflag being added from the FS_*_FL counterpart"

while FS_FILE_ATTR_XFLAGS_MASK is

"FS_XFLAGS_MASK + all the future xflags that will be added (like MAYSHARE and BTIME)"

Ok, but "only the already existing xflags" is a cyclic definition or it depends on a specific point in time or uapi version/flavor. How can this documentation avoid from going stale?
I am open to seeing what you come up with.

I suggest FS_XFLAGS_FSXATTR_MASK for the flags supported via the old ioctl api and v0 and FS_XFLAGS_FILE_ATTR_MASK for the flags supported (now and future) via the latest syscall api.

Quoted 6 lines

For PROJID, personally it could also just be put in FS_XFLAGS_MASK, my concern is that it may break programs, even tho fsx_xflags is already expected to grow (as per your 276e136bff7e ("fs: prepare for extending file_get/setattr()")).

I am too concerned that this would break programs.

Thanks,
Amir.

author
#14 lore raw

Re: [PATCH v3 2/5] vfs: add xflags masks to fileattr API

Quoted 116 lines · Amir Goldstein
Quoted 83 lines · koraynilay
Quoted 81 lines · Amir Goldstein
Quoted 80 lines · 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

---

...

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

+/*
+ * Convert RMW input format without masks, coming from userspace, to input
+ * format with masks, for filesystems that already support masks.
+ */

Quoted 9 lines
Quoted 5 lines

Sorry I am not fond of this terminology as well It's not like the new API cannot be used RMW style. I suggest to stick with plain boring converter names like fileattr_set_v0_to_v1() similar to FS_XFLAGS_VER[01]_MASK comment below.

I really don't like v0_to_v1 or similar, because it's not informative on what it's doing IMO.

Fair enough.

Quoted 10 lines
Quoted 1 line

It's not like the new API cannot be used RMW style.

If the new API is used RMW style, it will still have masks tho, so if userspace does a read-modify-write cycle using masks, the semantics of the action would change from "I read the old file, I'll change only what I want, then I'll persist the changes" to "I read the old file; now I want to write all the flags and each needs to have this value".

In my mind at least.

I could not understand why _set_rmw_to_mask() is a good representation of what it is doing.

Quoted 8 lines

Please see this code for example and say if it reads better:

        if (fa->use_masks && !old_ma->accepts_masks)
                return fileattr_set_apply_masks(fa, old_ma);

        if (!fa->use_masks && old_ma->accepts_masks)
                return fileattr_set_derive_masks(fa, old_ma);

You mean mask_to_rmw = apply_masks and rmw_to_mask = derive_masks right?

Considering it wasn't as immediate to me which corresponded to which, I'm not sure ahah.
Once I understood which is which though (since the ifs are also inverted I think) I can see why you thought of apply (derive is more explicit).

btw my current code uses get_only_mask, so it's more explicit that filesystem that fully switch to masks keep that as false:

@@ -389,15 +391,17 @@ static int fileattr_set_mask_to_rmw(struct file_kattr *fa,
static int fileattr_set_convert(struct file_kattr *fa,
const struct file_kattr *old_ma)
{
+ bool fs_has_masks = old_ma->has_masks && !old_ma->get_only_masks;
+
if (fa->has_masks) {
- if (!old_ma->has_masks)
+ if (!fs_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)
+ if (fs_has_masks)
return fileattr_set_rmw_to_mask(fa, old_ma);

fa->fsx_xflags_rw_mask = 0;

include/linux/fileattr.h +7 -1
index 9a8de4190133..1c24de0ded1c 100644
@@ -64,7 +64,13 @@ struct file_kattr {
 	/* selectors: */
 	bool	flags_valid:1;
 	bool	fsx_valid:1;
-	bool	has_masks:1;
+	bool	has_masks:1;	/* struct has valid masks */
+	bool	use_masks:1;	/* equivalent to AT_FILEATTR_MASKS but for
+				   in-kernel calls (set, only in) */
+	bool	get_only_masks:1;/* Temporary. Filesystem doesn't actually
+				    support masks yet, but wants to support
+				    read-only / get-only xflags like
+				    MAYSHAREEXTENT or BTIME */
 };
 
 
Quoted 69 lines

...

Quoted 44 lines
Quoted 35 lines

Review

Quoted 13 lines

+ * Handle converting between RMW and masks formats based on userspace and
+ * filesystem support.
+ * Preserve unchanged bits from old_ma.
+ */

(quoted diff) +9 -0
+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;

This is where the single has_masks bites twice.

fa->has_masks is supposed to mean "caller specified AT_FILEATTR_MASKS", but for in-kernel get+modify+set callers (ovl_copy_fileattr(), ksmbd_vfs_set_compression()) it was set by vfs_fileattr_get() of a fs that reports masks, so their legacy requests are passed through as masked requests instead of going through rmw_to_mask().

For ksmbd over ntfs, FS_COMPR_FL is not in the rw mask, so it is silently ignored instead of failing with -EOPNOTSUPP as ksmbd expects.

old_ma->has_masks is used as "fs accepts masks in set", but it really means "fs reports masks in get", which is the problem Lauri reported for ocfs2.

As I wrote in reply to Lauri, these should be three different properties:
- caller has specified masks (AT_FILEATTR_MASKS => has_masks)
- fs reported masks in get (masks_valid?)
- fs can accept masks in set (accepts_masks?)

Quoted 8 lines

That last hint is the one that would help Lauri's case and read-only btime and it remains vfs internal, not exposed to userspace for now.

Note that, as you have seen for yourself in the FUSE bug that you fixed, overloading the _valid hints for both directions was a source of bugs. If the _valid hints were split into hints from the caller and hints from the filesystem, things might have been less confusing (but you don't need to change that now).

Quoted 7 lines

I thought about this for the past few hours.

For the 2nd field, I chose use_masks:

  bool  has_masks:1;    /* struct has valid masks (get, only out) */
  bool  use_masks:1;    /* equivalent to AT_FILEATTR_MASKS but for

in-kernel calls (set, only in) */

ok.

Quoted 13 lines

(current WIP diff [1])

While I don't like much having a 3rd flag for SETFLAGS (hence my comment above the EOPNOTSUPP return), I gave a quick look at ocfs2's code, and I can see how it could be not so trivial to switch to xflags (especially the testing), so I can convince myself it's fine if it's a temporary field before this is finished:

Quoted 5 lines

The long term goal is for all fs methods to be converted to deal only with the canonical masked format, so they don't need to carry legacy code and all conversions will be done in the vfs. A fs should accept masks in set only when all its writable flags can be expressed with xflags, which may require adding new xflags first (e.g. DIRSYNC).

Quoted 8 lines
Quoted 3 lines

(Which means it will become a permanent field, but anyway)

Converting all fs is going to be a long mission. There is no guarantee if and when this will happen. I think that Lauri's request for ocfs2 is very reasonable and we need to find an acceptable way to address it.

Yeah, I can see that now.

Quoted 187 lines
Quoted 133 lines
Quoted 113 lines
Quoted 102 lines
(quoted diff) +50 -10
+       }
+
+       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;
 }

Review int vfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry, · line 540

Quoted 6 lines
(quoted diff) +3 -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;
+

I understand the logic behind zero rw_mask means do nothing, but it looks to me like setting a trap for programs that read from old fs (zero rw_mask) and set the same fa blob to new fs.

Quoted 3 lines

It is a variant of the caution clause in your patch 5 doc.

IMO, AT_FILEATTR_MASKS should treat zero rw_masks as EINVAL.

Yeah, that is probably better in the end.

Quoted 5 lines

Alternatively, I hate to change everything again, but maybe we made a bad choice with AT_FILEATTR_MASKS, because using FS_XFLAG_HASMASKS may have been better for all the backup/sync cases?
I leave it up to you if you want to explore this alley.

Eh, unless I missed something, when I was thinking about using FS_XFLAG_HASMASKS, either we use it like AT_FILEATTR_MASKS rn and strip it before calling vfs_fileattr_g/set, so it wouldn't make sense even adding it vs keeping AT_FILEATTR_MASKS, or we use it instead of has_masks, but since it would be in fa/fsx_xflags, it would then be a nightmare to do operations/checks on the actual flags, since the FS_XFLAG_HASMASKS bit would alway be one (when using masks).

Quoted 4 lines

So I think it's fine to just return -EINVAL on 0 rw_mask, the only use case I can think of where using 0 as rw_mask would make sense is probing the fs for support/permissions.

good.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
(quoted diff) +0 -0
        inode_lock(inode);
        err = vfs_fileattr_get(dentry, &old_ma);
        if (err)
                goto out;
 
Quoted 7 lines
Quoted 3 lines

Review int vfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry, · line 291

Quoted 1 line
(quoted diff) +0 -1
-       /* initialize missing bits from old_ma */

Is this comment not still relevant in this context after get of old_ma?

Yes, but I think it's better to keep it on above the same if (which is why I copied it to set_convert along with the if).

ok.

...

Quoted 30 lines
Quoted 15 lines

Review int vfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry, · line 548

Quoted 3 lines
(quoted diff) +3 -0
+/* Flags only for fa_ fields */
+#define FS_FILE_ATTR_XFLAGS_MASK \
+       (FS_XFLAGS_MASK | FS_XFLAG_PROJID)

I think these two similarly named constants will be confusing for coders and reviewers and the special-casing of PROJID is a pain for documentation.

How about FS_XFLAGS_VER[01]_MASK?
Do not define FS_XFLAGS_MASK so each macro
user will need to be converted to use the correct macro.

Using alias FS_XFLAGS_LATEST_MASK could read nicely in some common helpers, e.g.:

Quoted 6 lines
        fa->fsx_xflags &= FS_XFLAGS_LATEST_MASK;
        return 0;

explicit VER1 could read nicer in other helpers, e.g.

        u64 mask = fa->has_masks ? FS_XFLAGS_VER1_MASK : FS_XFLAGS_VER0_MASK;

Hmm, I don't think I like this approach too much, since right now my thinking for FS_XFLAGS_MASK is

"mask of only the already existing xflags + any xflag being added from the FS_*_FL counterpart"

while FS_FILE_ATTR_XFLAGS_MASK is

Quoted 7 lines
Quoted 2 lines

"FS_XFLAGS_MASK + all the future xflags that will be added (like MAYSHARE and BTIME)"

Ok, but "only the already existing xflags" is a cyclic definition or it depends on a specific point in time or uapi version/flavor. How can this documentation avoid from going stale?
I am open to seeing what you come up with.

Oh that is just my interpretation, with "currently" I meant right now, november 2026, not also in the future.

Quoted 4 lines

I suggest FS_XFLAGS_FSXATTR_MASK for the flags supported via the old ioctl api and v0 and FS_XFLAGS_FILE_ATTR_MASK for the flags supported (now and future) via the latest syscall api.

Oh yes that's a good idea, tho it would also need to be documented that it can be expanded with FS_*_FL counterparts.

Quoted 8 lines
Quoted 6 lines

For PROJID, personally it could also just be put in FS_XFLAGS_MASK, my concern is that it may break programs, even tho fsx_xflags is already expected to grow (as per your 276e136bff7e ("fs: prepare for extending file_get/setattr()")).

I am too concerned that this would break programs.

Hmm, I'll ask the robot a bit about solutions to this, but... what about adding an exception for PROJID that it must never be set in fsx_xflags and only used in rw_mask (so for chattr -p)?

Thanks,
koraynilay

Quoted 2 lines

Thanks,
Amir.

author
#15 lore raw

Re: [PATCH v3 2/5] vfs: add xflags masks to fileattr API

Quoted 126 lines · koraynilay
Quoted 116 lines · Amir Goldstein
Quoted 83 lines · koraynilay
Quoted 81 lines · Amir Goldstein
Quoted 80 lines · 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

---

...

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

+/*
+ * Convert RMW input format without masks, coming from userspace, to input
+ * format with masks, for filesystems that already support masks.
+ */

Quoted 9 lines
Quoted 5 lines

Sorry I am not fond of this terminology as well It's not like the new API cannot be used RMW style. I suggest to stick with plain boring converter names like fileattr_set_v0_to_v1() similar to FS_XFLAGS_VER[01]_MASK comment below.

I really don't like v0_to_v1 or similar, because it's not informative on what it's doing IMO.

Fair enough.

Quoted 10 lines
Quoted 1 line

It's not like the new API cannot be used RMW style.

If the new API is used RMW style, it will still have masks tho, so if userspace does a read-modify-write cycle using masks, the semantics of the action would change from "I read the old file, I'll change only what I want, then I'll persist the changes" to "I read the old file; now I want to write all the flags and each needs to have this value".

In my mind at least.

I could not understand why _set_rmw_to_mask() is a good representation of what it is doing.

Quoted 8 lines

Please see this code for example and say if it reads better:

        if (fa->use_masks && !old_ma->accepts_masks)
                return fileattr_set_apply_masks(fa, old_ma);

        if (!fa->use_masks && old_ma->accepts_masks)
                return fileattr_set_derive_masks(fa, old_ma);
Quoted 6 lines

You mean mask_to_rmw = apply_masks and rmw_to_mask = derive_masks right?

Considering it wasn't as immediate to me which corresponded to which, I'm not sure ahah.
Once I understood which is which though (since the ifs are also inverted I think) I can see why you thought of apply (derive is more explicit).

And I forgot to give another possible example:

fileattr_set_to_masks();
fileattr_set_from_masks();

which is not the best but it's clear enough I think.

Quoted 346 lines

btw my current code uses get_only_mask, so it's more explicit that filesystem that fully switch to masks keep that as false:

(quoted diff) +4 -2
@@ -389,15 +391,17 @@ static int fileattr_set_mask_to_rmw(struct file_kattr *fa,
 static int fileattr_set_convert(struct file_kattr *fa,
 				const struct file_kattr *old_ma)
 {
+	bool fs_has_masks = old_ma->has_masks && !old_ma->get_only_masks;
+
 	if (fa->has_masks) {
-		if (!old_ma->has_masks)
+		if (!fs_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)
+	if (fs_has_masks)
 		return fileattr_set_rmw_to_mask(fa, old_ma);
 
 	fa->fsx_xflags_rw_mask = 0;
include/linux/fileattr.h +7 -1
index 9a8de4190133..1c24de0ded1c 100644
@@ -64,7 +64,13 @@ struct file_kattr {
 	/* selectors: */
 	bool	flags_valid:1;
 	bool	fsx_valid:1;
-	bool	has_masks:1;
+	bool	has_masks:1;	/* struct has valid masks */
+	bool	use_masks:1;	/* equivalent to AT_FILEATTR_MASKS but for
+				   in-kernel calls (set, only in) */
+	bool	get_only_masks:1;/* Temporary. Filesystem doesn't actually
+				    support masks yet, but wants to support
+				    read-only / get-only xflags like
+				    MAYSHAREEXTENT or BTIME */
 };
 
 
Quoted 69 lines

...

Quoted 44 lines
Quoted 35 lines

Review

Quoted 13 lines

+ * Handle converting between RMW and masks formats based on userspace and
+ * filesystem support.
+ * Preserve unchanged bits from old_ma.
+ */

(quoted diff) +9 -0
+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;

This is where the single has_masks bites twice.

fa->has_masks is supposed to mean "caller specified AT_FILEATTR_MASKS", but for in-kernel get+modify+set callers (ovl_copy_fileattr(), ksmbd_vfs_set_compression()) it was set by vfs_fileattr_get() of a fs that reports masks, so their legacy requests are passed through as masked requests instead of going through rmw_to_mask().

For ksmbd over ntfs, FS_COMPR_FL is not in the rw mask, so it is silently ignored instead of failing with -EOPNOTSUPP as ksmbd expects.

old_ma->has_masks is used as "fs accepts masks in set", but it really means "fs reports masks in get", which is the problem Lauri reported for ocfs2.

As I wrote in reply to Lauri, these should be three different properties:
- caller has specified masks (AT_FILEATTR_MASKS => has_masks)
- fs reported masks in get (masks_valid?)
- fs can accept masks in set (accepts_masks?)

Quoted 8 lines

That last hint is the one that would help Lauri's case and read-only btime and it remains vfs internal, not exposed to userspace for now.

Note that, as you have seen for yourself in the FUSE bug that you fixed, overloading the _valid hints for both directions was a source of bugs. If the _valid hints were split into hints from the caller and hints from the filesystem, things might have been less confusing (but you don't need to change that now).

Quoted 7 lines

I thought about this for the past few hours.

For the 2nd field, I chose use_masks:

  bool  has_masks:1;    /* struct has valid masks (get, only out) */
  bool  use_masks:1;    /* equivalent to AT_FILEATTR_MASKS but for

in-kernel calls (set, only in) */

ok.

Quoted 13 lines

(current WIP diff [1])

While I don't like much having a 3rd flag for SETFLAGS (hence my comment above the EOPNOTSUPP return), I gave a quick look at ocfs2's code, and I can see how it could be not so trivial to switch to xflags (especially the testing), so I can convince myself it's fine if it's a temporary field before this is finished:

Quoted 5 lines

The long term goal is for all fs methods to be converted to deal only with the canonical masked format, so they don't need to carry legacy code and all conversions will be done in the vfs. A fs should accept masks in set only when all its writable flags can be expressed with xflags, which may require adding new xflags first (e.g. DIRSYNC).

Quoted 8 lines
Quoted 3 lines

(Which means it will become a permanent field, but anyway)

Converting all fs is going to be a long mission. There is no guarantee if and when this will happen. I think that Lauri's request for ocfs2 is very reasonable and we need to find an acceptable way to address it.

Yeah, I can see that now.

Quoted 187 lines
Quoted 133 lines
Quoted 113 lines
Quoted 102 lines
(quoted diff) +50 -10
+       }
+
+       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;
 }

Review int vfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry, · line 540

Quoted 6 lines
(quoted diff) +3 -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;
+

I understand the logic behind zero rw_mask means do nothing, but it looks to me like setting a trap for programs that read from old fs (zero rw_mask) and set the same fa blob to new fs.

Quoted 3 lines

It is a variant of the caution clause in your patch 5 doc.

IMO, AT_FILEATTR_MASKS should treat zero rw_masks as EINVAL.

Yeah, that is probably better in the end.

Quoted 5 lines

Alternatively, I hate to change everything again, but maybe we made a bad choice with AT_FILEATTR_MASKS, because using FS_XFLAG_HASMASKS may have been better for all the backup/sync cases?
I leave it up to you if you want to explore this alley.

Eh, unless I missed something, when I was thinking about using FS_XFLAG_HASMASKS, either we use it like AT_FILEATTR_MASKS rn and strip it before calling vfs_fileattr_g/set, so it wouldn't make sense even adding it vs keeping AT_FILEATTR_MASKS, or we use it instead of has_masks, but since it would be in fa/fsx_xflags, it would then be a nightmare to do operations/checks on the actual flags, since the FS_XFLAG_HASMASKS bit would alway be one (when using masks).

Quoted 4 lines

So I think it's fine to just return -EINVAL on 0 rw_mask, the only use case I can think of where using 0 as rw_mask would make sense is probing the fs for support/permissions.

good.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
(quoted diff) +0 -0
        inode_lock(inode);
        err = vfs_fileattr_get(dentry, &old_ma);
        if (err)
                goto out;
 
Quoted 7 lines
Quoted 3 lines

Review int vfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry, · line 291

Quoted 1 line
(quoted diff) +0 -1
-       /* initialize missing bits from old_ma */

Is this comment not still relevant in this context after get of old_ma?

Yes, but I think it's better to keep it on above the same if (which is why I copied it to set_convert along with the if).

ok.

...

Quoted 30 lines
Quoted 15 lines

Review int vfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry, · line 548

Quoted 3 lines
(quoted diff) +3 -0
+/* Flags only for fa_ fields */
+#define FS_FILE_ATTR_XFLAGS_MASK \
+       (FS_XFLAGS_MASK | FS_XFLAG_PROJID)

I think these two similarly named constants will be confusing for coders and reviewers and the special-casing of PROJID is a pain for documentation.

How about FS_XFLAGS_VER[01]_MASK?
Do not define FS_XFLAGS_MASK so each macro
user will need to be converted to use the correct macro.

Using alias FS_XFLAGS_LATEST_MASK could read nicely in some common helpers, e.g.:

Quoted 6 lines
        fa->fsx_xflags &= FS_XFLAGS_LATEST_MASK;
        return 0;

explicit VER1 could read nicer in other helpers, e.g.

        u64 mask = fa->has_masks ? FS_XFLAGS_VER1_MASK : FS_XFLAGS_VER0_MASK;

Hmm, I don't think I like this approach too much, since right now my thinking for FS_XFLAGS_MASK is

"mask of only the already existing xflags + any xflag being added from the FS_*_FL counterpart"

while FS_FILE_ATTR_XFLAGS_MASK is

Quoted 7 lines
Quoted 2 lines

"FS_XFLAGS_MASK + all the future xflags that will be added (like MAYSHARE and BTIME)"

Ok, but "only the already existing xflags" is a cyclic definition or it depends on a specific point in time or uapi version/flavor. How can this documentation avoid from going stale?
I am open to seeing what you come up with.

Oh that is just my interpretation, with "currently" I meant right now, november 2026, not also in the future.

Quoted 4 lines

I suggest FS_XFLAGS_FSXATTR_MASK for the flags supported via the old ioctl api and v0 and FS_XFLAGS_FILE_ATTR_MASK for the flags supported (now and future) via the latest syscall api.

Oh yes that's a good idea, tho it would also need to be documented that it can be expanded with FS_*_FL counterparts.

Quoted 8 lines
Quoted 6 lines

For PROJID, personally it could also just be put in FS_XFLAGS_MASK, my concern is that it may break programs, even tho fsx_xflags is already expected to grow (as per your 276e136bff7e ("fs: prepare for extending file_get/setattr()")).

I am too concerned that this would break programs.

Hmm, I'll ask the robot a bit about solutions to this, but... what about adding an exception for PROJID that it must never be set in fsx_xflags and only used in rw_mask (so for chattr -p)?

Thanks,
koraynilay

Quoted 2 lines

Thanks,
Amir.

#43 lore raw

Re: [PATCH v3 5/5] docs: add fileattr API documentation

In reply to koraynilay
Quoted 19 lines · koraynilay

Add documentation for the fileattr API.

---
 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
Documentation/filesystems/fileattr.rst new file +2 -0
new file mode 100644
index 000000000000..9e3515dc4517
@@ -0,0 +1,530 @@
+.. SPDX-License-Identifier: GPL-2.0
+

Documentation/filesystems/fileattr.rst line 12

Quoted 10 lines
Documentation/filesystems/fileattr.rst new file +10 -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).

Note, there're man-pages for file_getattr()/file_setattr() merged for a next release [1] and [2]. The struct file_attr man page is already merged in 6.20 [3].

I think it worth referencing these and maybe focus this doc more on internals?

1: https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/commit/?id=f73700438c6cdbe80a28619dbc3f41708e339247
2: https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/commit/?id=e5a0a8c70331002298f070920e18ee0ffbcdad32
3: https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/commit/?id=8afa46e2ada9331c44d91026f1214b8ad4c1cfaf

Signature · 1 line
- Andrey
author
#44 lore raw

Re: [PATCH v3 5/5] docs: add fileattr API documentation

Quoted 26 lines · Andrey Albershteyn
Quoted 25 lines · koraynilay

Add documentation for the fileattr API.

---
 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
Documentation/filesystems/fileattr.rst new file +8 -0
new file mode 100644
index 000000000000..9e3515dc4517
@@ -0,0 +1,530 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+============
+Fileattr API
+============
+
+Overview
+========
Quoted 8 lines

Documentation/filesystems/fileattr.rst line 12

Quoted 4 lines
Documentation/filesystems/fileattr.rst new file +4 -0
+
+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).

Note, there're man-pages for file_getattr()/file_setattr() merged for a next release [1] and [2]. The struct file_attr man page is already merged in 6.20 [3].

Ah yes thanks, I was indeed modifying those files to add the masks. (But I'll probably finish those after having the actual code and doc finished)

Quoted 2 lines

I think it worth referencing these and maybe focus this doc more on internals?

Yes, I'll update the doc to reference these.
Should I keep the doc with only stuff that isn't/won't be in the manpages or have *some* things duplicated?

Thanks,
koraynilay

Quoted 3 lines

1: https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/commit/?id=f73700438c6cdbe80a28619dbc3f41708e339247
2: https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/commit/?id=e5a0a8c70331002298f070920e18ee0ffbcdad32
3: https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/commit/?id=8afa46e2ada9331c44d91026f1214b8ad4c1cfaf

#45 lore raw

Re: [PATCH v3 5/5] docs: add fileattr API documentation

Quoted 41 lines · koraynilay
Quoted 26 lines · Andrey Albershteyn
Quoted 25 lines · koraynilay

Add documentation for the fileattr API.

---
 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
Documentation/filesystems/fileattr.rst new file +8 -0
new file mode 100644
index 000000000000..9e3515dc4517
@@ -0,0 +1,530 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+============
+Fileattr API
+============
+
+Overview
+========
Quoted 8 lines

Documentation/filesystems/fileattr.rst line 12

Quoted 4 lines
Documentation/filesystems/fileattr.rst new file +4 -0
+
+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).

Note, there're man-pages for file_getattr()/file_setattr() merged for a next release [1] and [2]. The struct file_attr man page is already merged in 6.20 [3].

Ah yes thanks, I was indeed modifying those files to add the masks. (But I'll probably finish those after having the actual code and doc finished)

Quoted 7 lines
Quoted 2 lines

I think it worth referencing these and maybe focus this doc more on internals?

Yes, I'll update the doc to reference these.
Should I keep the doc with only stuff that isn't/won't be in the manpages or have *some* things duplicated?

You don't need to worry about duplication, only need to consider who this doc you are writing is for and then use common sense to know if this information is relevant (hint: all possible error codes are not).

It is NOT for userspace programmers, it is for the kernel and filesystem developers. For fs developers wanting to convert an fs and for developers wanting to add some new flag/value etc.

Thanks,
Amir.

#16 lore raw

Re: [PATCH v3 2/5] vfs: add xflags masks to fileattr API

In reply to koraynilay
Quoted 121 lines · koraynilay
Quoted 119 lines · koraynilay
Quoted 118 lines · Amir Goldstein
Quoted 83 lines · koraynilay
Quoted 81 lines · Amir Goldstein
Quoted 80 lines · 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

---

...

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

+/*
+ * Convert RMW input format without masks, coming from userspace, to input
+ * format with masks, for filesystems that already support masks.
+ */

Quoted 9 lines
Quoted 5 lines

Sorry I am not fond of this terminology as well It's not like the new API cannot be used RMW style. I suggest to stick with plain boring converter names like fileattr_set_v0_to_v1() similar to FS_XFLAGS_VER[01]_MASK comment below.

I really don't like v0_to_v1 or similar, because it's not informative on what it's doing IMO.

Fair enough.

Quoted 10 lines
Quoted 1 line

It's not like the new API cannot be used RMW style.

If the new API is used RMW style, it will still have masks tho, so if userspace does a read-modify-write cycle using masks, the semantics of the action would change from "I read the old file, I'll change only what I want, then I'll persist the changes" to "I read the old file; now I want to write all the flags and each needs to have this value".

In my mind at least.

I could not understand why _set_rmw_to_mask() is a good representation of what it is doing.

Please see this code for example and say if it reads better:

Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
        if (fa->use_masks && !old_ma->accepts_masks)
                return fileattr_set_apply_masks(fa, old_ma);

        if (!fa->use_masks && old_ma->accepts_masks)
                return fileattr_set_derive_masks(fa, old_ma);

You mean mask_to_rmw = apply_masks and rmw_to_mask = derive_masks right?

Right. you can only apply masks if you have them but you can derive the masks from other bits if you don't.

Quoted 6 lines
Quoted 5 lines

Considering it wasn't as immediate to me which corresponded to which, I'm not sure ahah.
Once I understood which is which though (since the ifs are also inverted I think) I can see why you thought of apply (derive is more explicit).

Quoted 6 lines

And I forgot to give another possible example:

fileattr_set_to_masks();
fileattr_set_from_masks();

which is not the best but it's clear enough I think.

Fine by me. _masks was always the clear part.
It's the _rmw part that I had a problem with.
Let's see:

     if (!fs_has_masks)
                return fileattr_set_from_masks(fa, old_ma);

     if (fs_has_masks)
               return fileattr_set_to_masks(fa, old_ma);

Right? not the best, but fine by me.

Quoted 23 lines
Quoted 23 lines

btw my current code uses get_only_mask, so it's more explicit that filesystem that fully switch to masks keep that as false:

(quoted diff) +4 -2
@@ -389,15 +391,17 @@ static int fileattr_set_mask_to_rmw(struct file_kattr *fa,
 static int fileattr_set_convert(struct file_kattr *fa,
                              const struct file_kattr *old_ma)
 {
+     bool fs_has_masks = old_ma->has_masks && !old_ma->get_only_masks;
+
      if (fa->has_masks) {
-             if (!old_ma->has_masks)
+             if (!fs_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)
+     if (fs_has_masks)
              return fileattr_set_rmw_to_mask(fa, old_ma);
 
Quoted 14 lines
Quoted 6 lines
(quoted diff) +0 -0
      fa->fsx_xflags_rw_mask = 0;
include/linux/fileattr.h +0 -0
index 9a8de4190133..1c24de0ded1c 100644

include/linux/fileattr.h struct file_kattr { · line 70

Quoted 8 lines
include/linux/fileattr.h +4 -1
@@ -64,7 +64,13 @@ struct file_kattr {
      /* selectors: */
      bool    flags_valid:1;
      bool    fsx_valid:1;
-     bool    has_masks:1;
+     bool    has_masks:1;    /* struct has valid masks */
+     bool    use_masks:1;    /* equivalent to AT_FILEATTR_MASKS but for
+                                in-kernel calls (set, only in) */
+     bool    get_only_masks:1;/* Temporary. Filesystem doesn't actually

If we want to be more honest I would say Transitional: ...

Quoted 3 lines

include/linux/fileattr.h struct file_kattr { · line 73

Quoted 3 lines
include/linux/fileattr.h +3 -0
+                                 support masks yet, but wants to support
+                                 read-only / get-only xflags like
+                                 MAYSHAREEXTENT or BTIME */

First of all, documenting future flags here is a bit questionable but I think you can skip the example.

Also, it is not really about read-only xflags, those exist already it's about the xflags_ro_mask, so maybe
"Transitional: filesystem doesn't support xflags_rw_mask for set but does support xflags_ro_mask for get"?

Quoted 42 lines
Quoted 42 lines
Quoted 30 lines
Quoted 30 lines
Quoted 15 lines

Review

Quoted 3 lines
(quoted diff) +3 -0
+/* Flags only for fa_ fields */
+#define FS_FILE_ATTR_XFLAGS_MASK \
+       (FS_XFLAGS_MASK | FS_XFLAG_PROJID)

I think these two similarly named constants will be confusing for coders and reviewers and the special-casing of PROJID is a pain for documentation.

How about FS_XFLAGS_VER[01]_MASK?
Do not define FS_XFLAGS_MASK so each macro
user will need to be converted to use the correct macro.

Using alias FS_XFLAGS_LATEST_MASK could read nicely in some common helpers, e.g.:

Quoted 6 lines
        fa->fsx_xflags &= FS_XFLAGS_LATEST_MASK;
        return 0;

explicit VER1 could read nicer in other helpers, e.g.

        u64 mask = fa->has_masks ? FS_XFLAGS_VER1_MASK : FS_XFLAGS_VER0_MASK;

Hmm, I don't think I like this approach too much, since right now my thinking for FS_XFLAGS_MASK is

"mask of only the already existing xflags + any xflag being added from the FS_*_FL counterpart"

while FS_FILE_ATTR_XFLAGS_MASK is

Quoted 7 lines
Quoted 2 lines

"FS_XFLAGS_MASK + all the future xflags that will be added (like MAYSHARE and BTIME)"

Ok, but "only the already existing xflags" is a cyclic definition or it depends on a specific point in time or uapi version/flavor. How can this documentation avoid from going stale?
I am open to seeing what you come up with.

Oh that is just my interpretation, with "currently" I meant right now, november 2026, not also in the future.

Quoted 1 line
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

I suggest FS_XFLAGS_FSXATTR_MASK for the flags supported via the old ioctl api and v0 and FS_XFLAGS_FILE_ATTR_MASK for the flags supported (now and future) via the latest syscall api.

Oh yes that's a good idea, tho it would also need to be documented that it can be expanded with FS_*_FL counterparts.

Sure. If we manage to document the macros in a clear manner then all is good.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

For PROJID, personally it could also just be put in FS_XFLAGS_MASK, my concern is that it may break programs, even tho fsx_xflags is already expected to grow (as per your 276e136bff7e ("fs: prepare for extending file_get/setattr()")).

Quoted 6 lines
Quoted 6 lines
Quoted 1 line

I am too concerned that this would break programs.

Hmm, I'll ask the robot a bit about solutions to this, but... what about adding an exception for PROJID that it must never be set in fsx_xflags and only used in rw_mask (so for chattr -p)?

I don't think this is the answer. Let's see what the bot has to offer.

Isn't PROJID in FS_XFLAGS_FILE_ATTR_MASK, but not in FS_XFLAGS_FSXATTR_MASK?
Is there more to it than that?

Thanks,
Amir.

author
#17 lore raw

Re: [PATCH v3 2/5] vfs: add xflags masks to fileattr API

Quoted 125 lines · Amir Goldstein
Quoted 124 lines · koraynilay
Quoted 122 lines · koraynilay
Quoted 121 lines · Amir Goldstein
Quoted 83 lines · koraynilay
Quoted 81 lines · Amir Goldstein
Quoted 80 lines · 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

---

...

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

+/*
+ * Convert RMW input format without masks, coming from userspace, to input
+ * format with masks, for filesystems that already support masks.
+ */

Quoted 9 lines
Quoted 5 lines

Sorry I am not fond of this terminology as well It's not like the new API cannot be used RMW style. I suggest to stick with plain boring converter names like fileattr_set_v0_to_v1() similar to FS_XFLAGS_VER[01]_MASK comment below.

I really don't like v0_to_v1 or similar, because it's not informative on what it's doing IMO.

Fair enough.

Quoted 10 lines
Quoted 1 line

It's not like the new API cannot be used RMW style.

If the new API is used RMW style, it will still have masks tho, so if userspace does a read-modify-write cycle using masks, the semantics of the action would change from "I read the old file, I'll change only what I want, then I'll persist the changes" to "I read the old file; now I want to write all the flags and each needs to have this value".

In my mind at least.

I could not understand why _set_rmw_to_mask() is a good representation of what it is doing.

Please see this code for example and say if it reads better:

        if (fa->use_masks && !old_ma->accepts_masks)
                return fileattr_set_apply_masks(fa, old_ma);
Quoted 8 lines
Quoted 5 lines
Quoted 5 lines
Quoted 3 lines
        if (!fa->use_masks && old_ma->accepts_masks)
                return fileattr_set_derive_masks(fa, old_ma);

You mean mask_to_rmw = apply_masks and rmw_to_mask = derive_masks right?

Right. you can only apply masks if you have them but you can derive the masks from other bits if you don't.

Hmm, fair.

Quoted 17 lines
Quoted 6 lines
Quoted 5 lines

Considering it wasn't as immediate to me which corresponded to which, I'm not sure ahah.
Once I understood which is which though (since the ifs are also inverted I think) I can see why you thought of apply (derive is more explicit).

Quoted 6 lines

And I forgot to give another possible example:

fileattr_set_to_masks();
fileattr_set_from_masks();

which is not the best but it's clear enough I think.

Fine by me. _masks was always the clear part.
It's the _rmw part that I had a problem with.
Let's see:

Quoted 7 lines
     if (!fs_has_masks)
                return fileattr_set_from_masks(fa, old_ma);

     if (fs_has_masks)
               return fileattr_set_to_masks(fa, old_ma);

Right? not the best, but fine by me.

Yes, not as explicit as rmw_to_mask imo, but understandable enough, I'll try it out and if I find anything better I'll update you.

Quoted 23 lines
Quoted 23 lines
Quoted 23 lines

btw my current code uses get_only_mask, so it's more explicit that filesystem that fully switch to masks keep that as false:

(quoted diff) +4 -2
@@ -389,15 +391,17 @@ static int fileattr_set_mask_to_rmw(struct file_kattr *fa,
 static int fileattr_set_convert(struct file_kattr *fa,
                              const struct file_kattr *old_ma)
 {
+     bool fs_has_masks = old_ma->has_masks && !old_ma->get_only_masks;
+
      if (fa->has_masks) {
-             if (!old_ma->has_masks)
+             if (!fs_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)
+     if (fs_has_masks)
              return fileattr_set_rmw_to_mask(fa, old_ma);
 
Quoted 16 lines
Quoted 6 lines
Quoted 6 lines
(quoted diff) +0 -0
      fa->fsx_xflags_rw_mask = 0;
include/linux/fileattr.h +0 -0
index 9a8de4190133..1c24de0ded1c 100644
@@ -64,7 +64,13 @@ struct file_kattr {
Quoted 8 lines

include/linux/fileattr.h struct file_kattr { · line 70

Quoted 8 lines
include/linux/fileattr.h +4 -1
      /* selectors: */
      bool    flags_valid:1;
      bool    fsx_valid:1;
-     bool    has_masks:1;
+     bool    has_masks:1;    /* struct has valid masks */
+     bool    use_masks:1;    /* equivalent to AT_FILEATTR_MASKS but for
+                                in-kernel calls (set, only in) */
+     bool    get_only_masks:1;/* Temporary. Filesystem doesn't actually

If we want to be more honest I would say Transitional: ...

Oh, yeah.

Quoted 11 lines
Quoted 3 lines

include/linux/fileattr.h struct file_kattr { · line 73

Quoted 3 lines
include/linux/fileattr.h +3 -0
+                                 support masks yet, but wants to support
+                                 read-only / get-only xflags like
+                                 MAYSHAREEXTENT or BTIME */

First of all, documenting future flags here is a bit questionable but I think you can skip the example.

Also, it is not really about read-only xflags, those exist already it's about the xflags_ro_mask, so maybe
"Transitional: filesystem doesn't support xflags_rw_mask for set but does support xflags_ro_mask for get"?

Yes, this was just a temp comment for me, I'll change it before submitting v4.

Quoted 47 lines
Quoted 47 lines
Quoted 47 lines
Quoted 31 lines
Quoted 31 lines
Quoted 16 lines

Review

Quoted 4 lines
(quoted diff) +3 -0
+/* Flags only for fa_ fields */
+#define FS_FILE_ATTR_XFLAGS_MASK \
+       (FS_XFLAGS_MASK | FS_XFLAG_PROJID)

I think these two similarly named constants will be confusing for coders and reviewers and the special-casing of PROJID is a pain for documentation.

How about FS_XFLAGS_VER[01]_MASK?
Do not define FS_XFLAGS_MASK so each macro
user will need to be converted to use the correct macro.

Using alias FS_XFLAGS_LATEST_MASK could read nicely in some common helpers, e.g.:

Quoted 6 lines
        fa->fsx_xflags &= FS_XFLAGS_LATEST_MASK;
        return 0;

explicit VER1 could read nicer in other helpers, e.g.

        u64 mask = fa->has_masks ? FS_XFLAGS_VER1_MASK : FS_XFLAGS_VER0_MASK;

Hmm, I don't think I like this approach too much, since right now my thinking for FS_XFLAGS_MASK is

"mask of only the already existing xflags + any xflag being added from the FS_*_FL counterpart"

while FS_FILE_ATTR_XFLAGS_MASK is

Quoted 7 lines
Quoted 2 lines

"FS_XFLAGS_MASK + all the future xflags that will be added (like MAYSHARE and BTIME)"

Ok, but "only the already existing xflags" is a cyclic definition or it depends on a specific point in time or uapi version/flavor. How can this documentation avoid from going stale?
I am open to seeing what you come up with.

Oh that is just my interpretation, with "currently" I meant right now, november 2026, not also in the future.

Quoted 4 lines

I suggest FS_XFLAGS_FSXATTR_MASK for the flags supported via the old ioctl api and v0 and FS_XFLAGS_FILE_ATTR_MASK for the flags supported (now and future) via the latest syscall api.

Quoted 6 lines
Quoted 3 lines
Quoted 3 lines

Oh yes that's a good idea, tho it would also need to be documented that it can be expanded with FS_*_FL counterparts.

Sure. If we manage to document the macros in a clear manner then all is good.

Yes.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

For PROJID, personally it could also just be put in FS_XFLAGS_MASK, my concern is that it may break programs, even tho fsx_xflags is already expected to grow (as per your 276e136bff7e ("fs: prepare for extending file_get/setattr()")).

Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
Quoted 1 line

I am too concerned that this would break programs.

Hmm, I'll ask the robot a bit about solutions to this, but... what about adding an exception for PROJID that it must never be set in fsx_xflags and only used in rw_mask (so for chattr -p)?

I don't think this is the answer. Let's see what the bot has to offer.

So the bot said that keeping PROJID outside of FS_FILE_ATTR_XFLAGS_MASK is the best solution, and suggested adding

#define FS_XFLAGS_FSXATTR_RW_MASK \
((FS_XFLAGS_FSXATTR_MASK & ~FS_XFLAG_RDONLY_MASK) | FS_XFLAG_PROJID)

to be used in the code/docs.
I'm not sure this is the best, but it would simplify reading the code.

Also, could we change FS_XFLAGS_FSXATTR_MASK to FS_XFLAGS_FSXATTR? (and so also FS_XFLAGS_FSXATTR_RW)
And I'd also change FS_FILE_ATTR_XFLAGS_MASK to maybe only FS_FILE_ATTR_MASK, so it's not too long.

And at this point, we should consider all _MASKs to be frozen and only expand FS_FILE_ATTR_MASK, with the exception of converterd FS_*_FL flags.

so:

FS_XFLAGS_FSXATTR would have the ones that are in right now + all new FS_*_FL converted ones (same for RDONLY and COMMON, etc)

FS_FILE_ATTR_MASK would have all FS_XFLAGS_FSXATTR + all new ones (MAYSHARE, BTIME, ...).

Quoted 3 lines

Isn't PROJID in FS_XFLAGS_FILE_ATTR_MASK, but not in FS_XFLAGS_FSXATTR_MASK?
Is there more to it than that?

Currently it's set like this, and there shouldn't be anything more to it.

Thanks,
koraynilay

Quoted 2 lines

Thanks,
Amir.

author
#7 lore raw

Re: [PATCH v3 2/5] vfs: add xflags masks to fileattr API

In reply to Lauri Gates
Quoted 68 lines · Lauri Gates

Review

Quoted 8 lines · koraynilay
(quoted diff) +8 -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;

I would like to ask for a change here before v4: let FS_IOC_SETFLAGS keep working for flags that the rw mask cannot express, instead of failing with EOPNOTSUPP. A sketch and the reason follow; would you take something like it?

I ran into this assumption while putting the may-share flag from the FIEMAP thread [1] on top of v3, in a series I have not posted yet. The flag is read-only, so it is reported through fa_xflags_ro_mask, which the VFS fills only when the filesystem sets has_masks. My series therefore sets has_masks for ocfs2 as well as xfs. On ocfs2 that broke FS_IOC_SETFLAGS for six flags it accepts today. Five have no FS_XFLAG_*
counterpart: SECRM, UNRM and NOTAIL on files, DIRSYNC and TOPDIR on directories. The sixth is COMPR, whose xflag from 3/5 is read-only, so it can never be in fsx_xflags_rw_mask. Each set failed with EOPNOTSUPP (QEMU, v7.4-rc1 plus v3 1-3 and my series); without my series all six work.

ocfs2 is only the first case. Of the filesystems in v3 only ntfs sets has_masks, but ext4 (DIRSYNC, TOPDIR, JOURNAL_DATA, EXTENTS) and btrfs (DIRSYNC, NOCOW) have the same kind of flags, so neither could set it without losing chattr for them. COMPR on btrfs is the case Amir asked to footnote in 5/5 as "RW only via SETFLAGS": with masks, chattr +c would fail the same way, and the footnote would no longer be true. With the fallback below it stays true. FS_IOC_FSSETXATTR is not affected, since it has never been able to set these flags.

The sketch, against v3 2/5: when an FS_IOC_SETFLAGS call changes a flag that the rw mask cannot express, give the filesystem the full read-modify-write request it got before masks. The check quoted above can then never fire, so the sketch removes it:

fs/file_attr.c +0 -0

@@ static int fileattr_set_rmw_to_mask(struct file_kattr *fa,

fs/file_attr.c +1 -9
 	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);
+		changed = flags_to_xflags(fa->flags ^ old_ma->flags);
 	} else { /* if (fa->fsx_valid) */

@@ static int fileattr_set_convert(struct file_kattr *fa,

fs/file_attr.c +8 -1
-	if (old_ma->has_masks)
+	/*
+	 * A masked request names only flags with an FS_XFLAG_* counterpart.
+	 * FS_IOC_SETFLAGS changing any other flag (e.g. FS_DIRSYNC_FL) gets
+	 * the full request below, which the filesystem handled before masks.
+	 */
+	if (old_ma->has_masks &&
+	    !(fa->flags_valid && ((fa->flags ^ old_ma->flags) &
+				  ~xflags_to_flags(old_ma->fsx_xflags_rw_mask))))
 		return fileattr_set_rmw_to_mask(fa, old_ma);
Quoted 9 lines

With it, all six ocfs2 flags set and clear again, and nothing else in my xfs, ocfs2 and overlayfs checks changes. The cost is in the contract: a filesystem that sets has_masks must still accept a full, unmasked request from FS_IOC_SETFLAGS. Every ->fileattr_set() does today, because has_masks on set is new in this series, but the documentation in 5/5 would need to say so. The other way I can see is to keep the masked request and pass the unmapped FS_*_FL changes beside it, which needs a second mask. That seems like more API than the case needs, so I would go with the fallback.

Hi, can you try with this diff and setting both get_only_masks and has_masks to true in ocfs2's code?

(to set only get_only_masks and leave has_masks as false, we'd need to also add || fa->get_only_masks in the clamping of vfs_fileattr_get and mask population in fileattr_to_file_attr, which I think wouldn't be as clean)

Thanks,
koraynilay

(WIP, it will be cleaned up for v4)

fs/file_attr.c +20 -4
index 2b3fe36bbab9..a6f6d59eb649 100644
@@ -185,7 +185,7 @@ static int file_attr_to_fileattr(const struct file_attr *fattr,
 	if (fattr->fa_xflags & ~mask)
 		return -EINVAL;
 
-	if (fa->has_masks) {
+	if (fa->use_masks && fa->has_masks) {
 		u64 rw_user_mask = fattr->fa_xflags_rw_mask;
 
 		if (rw_user_mask & ~FS_FILE_ATTR_XFLAGS_MASK)
@@ -301,6 +301,7 @@ static int fileattr_set_rmw_to_mask(struct file_kattr *fa,
 	fa->fsx_xflags = new_xflags;
 	fa->fsx_xflags_rw_mask = new_rw_mask;
 	fa->has_masks = true;
+	fa->use_masks = true;
 
 	fa->fsx_valid = true;
 	fa->flags_valid = false;
@@ -374,6 +375,7 @@ static int fileattr_set_mask_to_rmw(struct file_kattr *fa,
 	fa->fsx_xflags = new_xflags;
 	fa->fsx_xflags_rw_mask = 0;
 	fa->has_masks = false;
+	fa->use_masks = false;
 
 	fa->fsx_valid = true;
 	fa->flags_valid = false;
@@ -389,15 +391,17 @@ static int fileattr_set_mask_to_rmw(struct file_kattr *fa,
 static int fileattr_set_convert(struct file_kattr *fa,
 				const struct file_kattr *old_ma)
 {
+	bool fs_has_masks = old_ma->has_masks && !old_ma->get_only_masks;
+
 	if (fa->has_masks) {
-		if (!old_ma->has_masks)
+		if (!fs_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)
+	if (fs_has_masks)
 		return fileattr_set_rmw_to_mask(fa, old_ma);
 
 	fa->fsx_xflags_rw_mask = 0;
@@ -543,6 +547,16 @@ int vfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry,
 	if (!inode_owner_or_capable(idmap, inode))
 		return -EPERM;
 
+	if (fa->use_masks && !fa->has_masks)
+		return -EINVAL;
+
+	/* This comes from an older call or caller wanted VER0 RMW semantics */
+	if (!fa->use_masks)
+		fa->has_masks = false;
+
+	// also try this, it should be the same as above but less readable
+	//fa->has_masks = fa->use_masks;
+
 	if (fa->has_masks && !fa->fsx_xflags_rw_mask)
 		return -EINVAL;
 
@@ -754,8 +768,10 @@ SYSCALL_DEFINE5(file_setattr, int, dfd, const char __user *, filename,
 			return error;
 	}
 
-	if (at_flags & AT_FILEATTR_MASKS)
+	if (at_flags & AT_FILEATTR_MASKS) {
 		fa.has_masks = true;
+		fa.use_masks = true;
+	}
 
 	error = file_attr_to_fileattr(&fattr, &fa);
 	if (error)
include/linux/fileattr.h +9 -1
index ad77febe8c76..59353a5240f6 100644
@@ -64,7 +64,15 @@ struct file_kattr {
 	/* selectors: */
 	bool	flags_valid:1;
 	bool	fsx_valid:1;
-	bool	has_masks:1;
+	bool	has_masks:1;	/* struct has valid masks */
+	bool	use_masks:1;	/* equivalent to AT_FILEATTR_MASKS but for
+				   in-kernel calls (set, only in) */
+	bool	get_only_masks:1;/* Transitional. Filesystem doesn't actually
+				    support masks yet, but wants to support
+				    read-only / get-only xflags like
+				    MAYSHAREEXTENT or BTIME */
+				/* Transitional: filesystem doesn't support rw_mask for set,
+				   but supports ro_mask on get */
 };
 
 int copy_fsxattr_to_user(const struct file_kattr *fa, struct fsxattr __user *ufa);
#8 lore raw

Re: [PATCH v3 2/5] vfs: add xflags masks to fileattr API

Quoted 2 lines · koraynilay

Hi, can you try with this diff and setting both get_only_masks and has_masks to true in ocfs2's code?

Tested. With your diff, FS_IOC_SETFLAGS sets and clears all six ocfs2 flags again, so the may-share series can drop its VFS patch.

One case broke: on a reflinked ocfs2 file, file_setattr() with AT_FILEATTR_MASKS now fails with EOPNOTSUPP, setting or clearing NOATIME. fileattr_set_mask_to_rmw() fills the unrequested xflags from old_ma->fsx_xflags, which then include the read-only may-share bit. That bit is not in FS_XFLAG_RDONLY_MASK, so it reaches ocfs2_fileattr_set(), where fileattr_has_fsx() rejects the request.

Could you fold something like this into v4?

fs/file_attr.c +0 -0
@@ static int fileattr_set_mask_to_rmw(struct file_kattr *fa,
 	/* RMW filesystems expect to not have any globally read-only flag. */
 	new_xflags &= ~FS_XFLAG_RDONLY_MASK;
+	/* Nor xflags FS_IOC_FSSETXATTR lacks, e.g. MAYSHARE. */
+	new_xflags &= FS_XFLAGS_MASK | FS_XFLAG_PROJID;
 
 	new_flags = xflags_to_flags(new_xflags);

PROJID stays because the code below reads it before stripping it.

With that line added, all my checks pass on xfs, ocfs2, overlayfs metacopy files and ext4. Tree: v7.4-rc1, v3 1-3, your diff (zero rw_mask returning -EINVAL as in your tree), the may-share series without its VFS patch; x86_64 under QEMU.

Thanks,
Lauri

#18 lore raw

Re: [PATCH v3 2/5] vfs: add xflags masks to fileattr API

In reply to koraynilay
Quoted 1 line · koraynilay

...

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Quoted 6 lines

For PROJID, personally it could also just be put in FS_XFLAGS_MASK, my concern is that it may break programs, even tho fsx_xflags is already expected to grow (as per your 276e136bff7e ("fs: prepare for extending file_get/setattr()")).

I am too concerned that this would break programs.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 4 lines

Hmm, I'll ask the robot a bit about solutions to this, but... what about adding an exception for PROJID that it must never be set in fsx_xflags and only used in rw_mask (so for chattr -p)?

I don't think this is the answer. Let's see what the bot has to offer.

So the bot said that keeping PROJID outside of FS_FILE_ATTR_XFLAGS_MASK

Don't you mean the other way around?

PROJID is in FS_FILE_ATTR_XFLAGS_MASK and not in
FS_XFLAGS_FSXATTR_MASK?

Quoted 8 lines

is the best solution, and suggested adding

#define FS_XFLAGS_FSXATTR_RW_MASK \
((FS_XFLAGS_FSXATTR_MASK & ~FS_XFLAG_RDONLY_MASK) | FS_XFLAG_PROJID)

to be used in the code/docs.
I'm not sure this is the best, but it would simplify reading the code.

This is exactly what I was looking for.
PROJID is only singled out in one place (here) when defining the macro and a comment above the macro explains why.
From this point on the macro describes a trait, which has a very clear meaning, without mentioning any specific flag.

Good job, bot ;)

Quoted 2 lines

Also, could we change FS_XFLAGS_FSXATTR_MASK to FS_XFLAGS_FSXATTR? (and so also FS_XFLAGS_FSXATTR_RW)

This reduces readability considerably IMO.
There needs to be a clear separation between the
flag macros and the flag collection macros (traits if you will).

Quoted 2 lines

And I'd also change FS_FILE_ATTR_XFLAGS_MASK to maybe only FS_FILE_ATTR_MASK, so it's not too long.

Losing the FS_XFLAGS_ prefix is not good IMO.
I did want to offer a change to FS_XFLAGS_FILEATTR_MASK to be consistent with the rest of the collection/trait macros.

Quoted 4 lines

And at this point, we should consider all _MASKs to be frozen and only expand FS_FILE_ATTR_MASK, with the exception of converterd FS_*_FL flags.

This is something that we can definitely document as a guideline - use the latest v1 syscall uapi for new flags, with the exception of xflags for existing FS_*_FL flags.

However, a filesystem can only report a new flag after it has opted into masks, so if a new flag requires converting many filesystems, it may not be realistic to maintain the freeze rule.

Hopefully, the work on the may-share flag came just at the right time to allow for the ro_mask exception, so observing the guideline for new read-only flags seems doable.

Quoted 9 lines

so:

FS_XFLAGS_FSXATTR would have the ones that are in right now + all new FS_*_FL converted ones (same for RDONLY and COMMON, etc)

FS_FILE_ATTR_MASK would have all FS_XFLAGS_FSXATTR + all new ones (MAYSHARE, BTIME, ...).

Yes, it sounds like we are converging :)

Quoted 7 lines
Quoted 3 lines

Isn't PROJID in FS_XFLAGS_FILE_ATTR_MASK, but not in FS_XFLAGS_FSXATTR_MASK?
Is there more to it than that?

Currently it's set like this, and there shouldn't be anything more to it.

Nice. Looking forward to v4.
I hope I have some time to rest before, but probably not :)

Thanks,
Amir.

author
#19 lore raw

Re: [PATCH v3 2/5] vfs: add xflags masks to fileattr API

Quoted 17 lines · Amir Goldstein
Quoted 1 line · koraynilay

...

Quoted 14 lines
Quoted 14 lines
Quoted 13 lines
Quoted 13 lines
Quoted 8 lines
Quoted 6 lines

For PROJID, personally it could also just be put in FS_XFLAGS_MASK, my concern is that it may break programs, even tho fsx_xflags is already expected to grow (as per your 276e136bff7e ("fs: prepare for extending file_get/setattr()")).

I am too concerned that this would break programs.

Hmm, I'll ask the robot a bit about solutions to this, but... what about adding an exception for PROJID that it must never be set in fsx_xflags and only used in rw_mask (so for chattr -p)?

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

I don't think this is the answer. Let's see what the bot has to offer.

So the bot said that keeping PROJID outside of FS_FILE_ATTR_XFLAGS_MASK

Don't you mean the other way around?

PROJID is in FS_FILE_ATTR_XFLAGS_MASK and not in
FS_XFLAGS_FSXATTR_MASK?

Ah yes, typo.

Quoted 9 lines
Quoted 8 lines

is the best solution, and suggested adding

#define FS_XFLAGS_FSXATTR_RW_MASK \
((FS_XFLAGS_FSXATTR_MASK & ~FS_XFLAG_RDONLY_MASK) | FS_XFLAG_PROJID)

to be used in the code/docs.
I'm not sure this is the best, but it would simplify reading the code.

Quoted 7 lines

This is exactly what I was looking for.
PROJID is only singled out in one place (here) when defining the macro and a comment above the macro explains why.
From this point on the macro describes a trait, which has a very clear meaning, without mentioning any specific flag.

Good job, bot ;)

Yes, though, when trying to change the current code to use it, I'm not liking the name too much, since it seems like it's a subset of FS_XFLAGS_FSXATTR_MASK, while it's actually not, since FS_XFLAGS_FSXATTR_MASK does not include PROJID.

Quoted 6 lines
Quoted 2 lines

Also, could we change FS_XFLAGS_FSXATTR_MASK to FS_XFLAGS_FSXATTR? (and so also FS_XFLAGS_FSXATTR_RW)

This reduces readability considerably IMO.
There needs to be a clear separation between the
flag macros and the flag collection macros (traits if you will).

Hmm, fair, I proposed this change because the names are getting a bit long and formatting the code is harder, also they have FLAG*S* that can indicate it's a collection, but that's easily missable (and indeed, I can never remember if it's FS_XFLAG_MASK or FS_XFLAGS_MASK, one more reason removing the _MASK may be beneficial, at least to me lol).

Quoted 6 lines
Quoted 2 lines

And I'd also change FS_FILE_ATTR_XFLAGS_MASK to maybe only FS_FILE_ATTR_MASK, so it's not too long.

Losing the FS_XFLAGS_ prefix is not good IMO.
I did want to offer a change to FS_XFLAGS_FILEATTR_MASK to be consistent with the rest of the collection/trait macros.

Hmm, right, FS_XFLAGS_FILEATTR_MASK does indeed look better, though it is getting a bit hard to distinguish between them when they have such a long prefix.

FS_*_FL are easier to read.
Actually, could we maybe do something like FSX_ as prefix, if the FS_ prefix is not that important?

FSX_IMMUTABLE
FSX_PROJID

FSX_RDONLY_MASK
FSX_FSXATTR_MASK
FSX_FILEATTR_MASK

or if FS_ is important:

FS_FSX_IMMUTABLE
FS_FSX_PROJID

FS_FSX_RDONLY_MASK
FS_FSX_FSXATTR_MASK
FS_FSX_FILEATTR_MASK

or maybe?

FS_XFL_IMMUTABLE
FS_XFL_PROJID

FS_XFL_RDONLY_MASK
FS_XFL_FSXATTR_MASK
FS_XFL_FILEATTR_MASK

or something else that is shorter, idk.

Quoted 8 lines
Quoted 4 lines

And at this point, we should consider all _MASKs to be frozen and only expand FS_FILE_ATTR_MASK, with the exception of converterd FS_*_FL flags.

This is something that we can definitely document as a guideline - use the latest v1 syscall uapi for new flags, with the exception of xflags for existing FS_*_FL flags.

Yes.

Quoted 2 lines

However, a filesystem can only report a new flag after it has opted into masks,

Report as RW or as RO?

Since for RO reporting it doesn't need to, it just needs to do that for get (and actually, only for the wanted xflag, it probably doesn't even need to report all its xflags in rw/ro masks apart from e.g. MAYSHARE... or maybe not even in the masks, since on the set path vfs treats it as a "legacy RMW"/VER0 filesystem).

Quoted 6 lines

so if a new flag requires converting many filesystems, it may not be realistic to maintain the freeze rule.

Hopefully, the work on the may-share flag came just at the right time to allow for the ro_mask exception, so observing the guideline for new read-only flags seems doable.

Yes, MAYSHARE for testing read-only, BTIME for testing read-write (and read-only).

Quoted 4 lines
Quoted 4 lines

so:

Quoted 8 lines
Quoted 6 lines

FS_XFLAGS_FSXATTR would have the ones that are in right now + all new FS_*_FL converted ones (same for RDONLY and COMMON, etc)

FS_FILE_ATTR_MASK would have all FS_XFLAGS_FSXATTR + all new ones (MAYSHARE, BTIME, ...).

Yes, it sounds like we are converging :)

Yes, though that has been my thinking since the first time I wrote "FS_FILE_ATTR_MASK" for the ptime patch :D

Quoted 10 lines
Quoted 7 lines
Quoted 3 lines

Isn't PROJID in FS_XFLAGS_FILE_ATTR_MASK, but not in FS_XFLAGS_FSXATTR_MASK?
Is there more to it than that?

Currently it's set like this, and there shouldn't be anything more to it.

Nice. Looking forward to v4.
I hope I have some time to rest before, but probably not :)

I should take >= 1 breaks too instead of working on this when I'm not doing other stuff, but I already know I won't :)

Thanks,
koraynilay

Quoted 2 lines

Thanks,
Amir.

author
#20 lore raw

Re: [PATCH v3 2/5] vfs: add xflags masks to fileattr API

Quoted 44 lines · koraynilay
Quoted 17 lines · Amir Goldstein
Quoted 1 line · koraynilay

...

Quoted 14 lines
Quoted 14 lines
Quoted 13 lines
Quoted 13 lines
Quoted 8 lines
Quoted 6 lines

For PROJID, personally it could also just be put in FS_XFLAGS_MASK, my concern is that it may break programs, even tho fsx_xflags is already expected to grow (as per your 276e136bff7e ("fs: prepare for extending file_get/setattr()")).

I am too concerned that this would break programs.

Hmm, I'll ask the robot a bit about solutions to this, but... what about adding an exception for PROJID that it must never be set in fsx_xflags and only used in rw_mask (so for chattr -p)?

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

I don't think this is the answer. Let's see what the bot has to offer.

So the bot said that keeping PROJID outside of FS_FILE_ATTR_XFLAGS_MASK

Don't you mean the other way around?

PROJID is in FS_FILE_ATTR_XFLAGS_MASK and not in
FS_XFLAGS_FSXATTR_MASK?

Ah yes, typo.

Quoted 15 lines
Quoted 8 lines

is the best solution, and suggested adding

#define FS_XFLAGS_FSXATTR_RW_MASK \
((FS_XFLAGS_FSXATTR_MASK & ~FS_XFLAG_RDONLY_MASK) | FS_XFLAG_PROJID)

to be used in the code/docs.
I'm not sure this is the best, but it would simplify reading the code.

This is exactly what I was looking for.
PROJID is only singled out in one place (here) when defining the macro and a comment above the macro explains why.
From this point on the macro describes a trait, which has a very clear meaning, without mentioning any specific flag.

Quoted 6 lines
Quoted 1 line

Good job, bot ;)

Yes, though, when trying to change the current code to use it, I'm not liking the name too much, since it seems like it's a subset of FS_XFLAGS_FSXATTR_MASK, while it's actually not, since FS_XFLAGS_FSXATTR_MASK does not include PROJID.

Another option for FS_XFLAGS_FSXATTR_RW_MASK is FS_XFLAGS_NOMASK_RW, which I like a bit more but I'm not sure.

(Forgot in the previous reply)

Quoted 122 lines
Quoted 6 lines
Quoted 2 lines

Also, could we change FS_XFLAGS_FSXATTR_MASK to FS_XFLAGS_FSXATTR? (and so also FS_XFLAGS_FSXATTR_RW)

This reduces readability considerably IMO.
There needs to be a clear separation between the
flag macros and the flag collection macros (traits if you will).

Hmm, fair, I proposed this change because the names are getting a bit long and formatting the code is harder, also they have FLAG*S* that can indicate it's a collection, but that's easily missable (and indeed, I can never remember if it's FS_XFLAG_MASK or FS_XFLAGS_MASK, one more reason removing the _MASK may be beneficial, at least to me lol).

Quoted 6 lines
Quoted 2 lines

And I'd also change FS_FILE_ATTR_XFLAGS_MASK to maybe only FS_FILE_ATTR_MASK, so it's not too long.

Losing the FS_XFLAGS_ prefix is not good IMO.
I did want to offer a change to FS_XFLAGS_FILEATTR_MASK to be consistent with the rest of the collection/trait macros.

Hmm, right, FS_XFLAGS_FILEATTR_MASK does indeed look better, though it is getting a bit hard to distinguish between them when they have such a long prefix.

FS_*_FL are easier to read.
Actually, could we maybe do something like FSX_ as prefix, if the FS_ prefix is not that important?

FSX_IMMUTABLE
FSX_PROJID

FSX_RDONLY_MASK
FSX_FSXATTR_MASK
FSX_FILEATTR_MASK

or if FS_ is important:

FS_FSX_IMMUTABLE
FS_FSX_PROJID

FS_FSX_RDONLY_MASK
FS_FSX_FSXATTR_MASK
FS_FSX_FILEATTR_MASK

or maybe?

FS_XFL_IMMUTABLE
FS_XFL_PROJID

FS_XFL_RDONLY_MASK
FS_XFL_FSXATTR_MASK
FS_XFL_FILEATTR_MASK

or something else that is shorter, idk.

Quoted 8 lines
Quoted 4 lines

And at this point, we should consider all _MASKs to be frozen and only expand FS_FILE_ATTR_MASK, with the exception of converterd FS_*_FL flags.

This is something that we can definitely document as a guideline - use the latest v1 syscall uapi for new flags, with the exception of xflags for existing FS_*_FL flags.

Yes.

Quoted 2 lines

However, a filesystem can only report a new flag after it has opted into masks,

Report as RW or as RO?

Since for RO reporting it doesn't need to, it just needs to do that for get (and actually, only for the wanted xflag, it probably doesn't even need to report all its xflags in rw/ro masks apart from e.g. MAYSHARE... or maybe not even in the masks, since on the set path vfs treats it as a "legacy RMW"/VER0 filesystem).

Quoted 6 lines

so if a new flag requires converting many filesystems, it may not be realistic to maintain the freeze rule.

Hopefully, the work on the may-share flag came just at the right time to allow for the ro_mask exception, so observing the guideline for new read-only flags seems doable.

Yes, MAYSHARE for testing read-only, BTIME for testing read-write (and read-only).

Quoted 4 lines
Quoted 4 lines

so:

Quoted 8 lines
Quoted 6 lines

FS_XFLAGS_FSXATTR would have the ones that are in right now + all new FS_*_FL converted ones (same for RDONLY and COMMON, etc)

FS_FILE_ATTR_MASK would have all FS_XFLAGS_FSXATTR + all new ones (MAYSHARE, BTIME, ...).

Yes, it sounds like we are converging :)

Yes, though that has been my thinking since the first time I wrote "FS_FILE_ATTR_MASK" for the ptime patch :D

Quoted 10 lines
Quoted 7 lines
Quoted 3 lines

Isn't PROJID in FS_XFLAGS_FILE_ATTR_MASK, but not in FS_XFLAGS_FSXATTR_MASK?
Is there more to it than that?

Currently it's set like this, and there shouldn't be anything more to it.

Nice. Looking forward to v4.
I hope I have some time to rest before, but probably not :)

I should take >= 1 breaks too instead of working on this when I'm not doing other stuff, but I already know I won't :)

Thanks,
koraynilay

Quoted 2 lines

Thanks,
Amir.

author
#9 lore raw

Re: [PATCH v3 2/5] vfs: add xflags masks to fileattr API

In reply to Lauri Gates
Quoted 26 lines · Lauri Gates
Quoted 2 lines · koraynilay

Hi, can you try with this diff and setting both get_only_masks and has_masks to true in ocfs2's code?

Tested. With your diff, FS_IOC_SETFLAGS sets and clears all six ocfs2 flags again, so the may-share series can drop its VFS patch.

One case broke: on a reflinked ocfs2 file, file_setattr() with AT_FILEATTR_MASKS now fails with EOPNOTSUPP, setting or clearing NOATIME. fileattr_set_mask_to_rmw() fills the unrequested xflags from old_ma->fsx_xflags, which then include the read-only may-share bit. That bit is not in FS_XFLAG_RDONLY_MASK, so it reaches ocfs2_fileattr_set(), where fileattr_has_fsx() rejects the request.

Could you fold something like this into v4?

fs/file_attr.c +0 -0

@@ static int fileattr_set_mask_to_rmw(struct file_kattr *fa,

fs/file_attr.c +2 -0
 	/* RMW filesystems expect to not have any globally read-only flag. */
 	new_xflags &= ~FS_XFLAG_RDONLY_MASK;
+	/* Nor xflags FS_IOC_FSSETXATTR lacks, e.g. MAYSHARE. */
+	new_xflags &= FS_XFLAGS_MASK | FS_XFLAG_PROJID;
 
 	new_flags = xflags_to_flags(new_xflags);
Quoted 1 line

PROJID stays because the code below reads it before stripping it.

Right, that can even be folded in the previous line when using the new FS_XFLAGS_FSXATTR_RW_MASK (or FS_XFLAGS_NOMASK_RW) by just clamping new_flags to that macro.

We could actually also remove PROJID there, since the if below checks it against fa->fsx_xflags and not new_flags, but semantically it's not the best.

Quoted 4 lines

With that line added, all my checks pass on xfs, ocfs2, overlayfs metacopy files and ext4. Tree: v7.4-rc1, v3 1-3, your diff (zero rw_mask returning -EINVAL as in your tree), the may-share series without its VFS patch; x86_64 under QEMU.

Thanks,
koraynilay

Quoted 3 lines

Thanks,
Lauri

#21 lore raw

Re: [PATCH v3 2/5] vfs: add xflags masks to fileattr API

In reply to koraynilay
Quoted 1 line · koraynilay

...

Quoted 6 lines
Quoted 6 lines

This is exactly what I was looking for.
PROJID is only singled out in one place (here) when defining the macro and a comment above the macro explains why.
From this point on the macro describes a trait, which has a very clear meaning, without mentioning any specific flag.

Quoted 7 lines
Quoted 1 line

Good job, bot ;)

Yes, though, when trying to change the current code to use it, I'm not liking the name too much, since it seems like it's a subset of FS_XFLAGS_FSXATTR_MASK, while it's actually not, since FS_XFLAGS_FSXATTR_MASK does not include PROJID.

IDK, you can try _[GS]ET_MASK
it's a more accurate description.

Quoted 20 lines
Quoted 6 lines
Quoted 2 lines

Also, could we change FS_XFLAGS_FSXATTR_MASK to FS_XFLAGS_FSXATTR? (and so also FS_XFLAGS_FSXATTR_RW)

This reduces readability considerably IMO.
There needs to be a clear separation between the
flag macros and the flag collection macros (traits if you will).

Hmm, fair, I proposed this change because the names are getting a bit long and formatting the code is harder, also they have FLAG*S* that can indicate it's a collection, but that's easily missable (and indeed, I can never remember if it's FS_XFLAG_MASK or FS_XFLAGS_MASK, one more reason removing the _MASK may be beneficial, at least to me lol).

Quoted 6 lines
Quoted 2 lines

And I'd also change FS_FILE_ATTR_XFLAGS_MASK to maybe only FS_FILE_ATTR_MASK, so it's not too long.

Losing the FS_XFLAGS_ prefix is not good IMO.
I did want to offer a change to FS_XFLAGS_FILEATTR_MASK to be consistent with the rest of the collection/trait macros.

Quoted 7 lines

Hmm, right, FS_XFLAGS_FILEATTR_MASK does indeed look better, though it is getting a bit hard to distinguish between them when they have such a long prefix.

FS_*_FL are easier to read.
Actually, could we maybe do something like FSX_ as prefix, if the FS_ prefix is not that important?

It's not that important, but..

Quoted 2 lines

FSX_IMMUTABLE

This is in the uapi and changing them in kernel will be a lot of churn

Quoted 5 lines

FSX_PROJID

FSX_RDONLY_MASK
FSX_FSXATTR_MASK
FSX_FILEATTR_MASK

Those are fine by me

Quoted 5 lines

or if FS_ is important:

FS_FSX_IMMUTABLE
FS_FSX_PROJID

Those not for same reason

Quoted 4 lines

FS_FSX_RDONLY_MASK
FS_FSX_FSXATTR_MASK
FS_FSX_FILEATTR_MASK

Those fine

Quoted 9 lines

or maybe?

FS_XFL_IMMUTABLE
FS_XFL_PROJID

FS_XFL_RDONLY_MASK
FS_XFL_FSXATTR_MASK
FS_XFL_FILEATTR_MASK

Those are worse than all of the above IMO

Quoted 17 lines

or something else that is shorter, idk.

Quoted 8 lines
Quoted 4 lines

And at this point, we should consider all _MASKs to be frozen and only expand FS_FILE_ATTR_MASK, with the exception of converterd FS_*_FL flags.

This is something that we can definitely document as a guideline - use the latest v1 syscall uapi for new flags, with the exception of xflags for existing FS_*_FL flags.

Yes.

Quoted 2 lines

However, a filesystem can only report a new flag after it has opted into masks,

Quoted 8 lines

Report as RW or as RO?

Since for RO reporting it doesn't need to, it just needs to do that for get (and actually, only for the wanted xflag, it probably doesn't even need to report all its xflags in rw/ro masks apart from e.g. MAYSHARE... or maybe not even in the masks, since on the set path vfs treats it as a "legacy RMW"/VER0 filesystem).

Whatever guideline you write - if it makes sense its good,

Quoted 17 lines
Quoted 6 lines

so if a new flag requires converting many filesystems, it may not be realistic to maintain the freeze rule.

Hopefully, the work on the may-share flag came just at the right time to allow for the ro_mask exception, so observing the guideline for new read-only flags seems doable.

Yes, MAYSHARE for testing read-only, BTIME for testing read-write (and read-only).

Quoted 7 lines
Quoted 7 lines

so:

FS_XFLAGS_FSXATTR would have the ones that are in right now + all new FS_*_FL converted ones (same for RDONLY and COMMON, etc)

Quoted 9 lines
Quoted 5 lines
Quoted 3 lines

FS_FILE_ATTR_MASK would have all FS_XFLAGS_FSXATTR + all new ones (MAYSHARE, BTIME, ...).

Yes, it sounds like we are converging :)

Yes, though that has been my thinking since the first time I wrote "FS_FILE_ATTR_MASK" for the ptime patch :D

Yeh this is a tough one
and this is before the patch set got review from a wider audience... I praise your perseverance.

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Quoted 3 lines

Isn't PROJID in FS_XFLAGS_FILE_ATTR_MASK, but not in FS_XFLAGS_FSXATTR_MASK?
Is there more to it than that?

Currently it's set like this, and there shouldn't be anything more to it.

Quoted 6 lines
Quoted 2 lines

Nice. Looking forward to v4.
I hope I have some time to rest before, but probably not :)

I should take >= 1 breaks too instead of working on this when I'm not doing other stuff, but I already know I won't :)

Maybe if there is AI outage you can rest :D

Thanks,
Amir.

author
#22 lore raw

Re: [PATCH v3 2/5] vfs: add xflags masks to fileattr API

Quoted 12 lines · Amir Goldstein
Quoted 1 line · koraynilay

...

Quoted 8 lines
Quoted 7 lines

This is exactly what I was looking for.
PROJID is only singled out in one place (here) when defining the macro and a comment above the macro explains why.
From this point on the macro describes a trait, which has a very clear meaning, without mentioning any specific flag.

Good job, bot ;)

Quoted 8 lines
Quoted 5 lines

Yes, though, when trying to change the current code to use it, I'm not liking the name too much, since it seems like it's a subset of FS_XFLAGS_FSXATTR_MASK, while it's actually not, since FS_XFLAGS_FSXATTR_MASK does not include PROJID.

IDK, you can try _[GS]ET_MASK
it's a more accurate description.

Right, I'll with that as well.

Quoted 28 lines
Quoted 20 lines
Quoted 6 lines
Quoted 2 lines

Also, could we change FS_XFLAGS_FSXATTR_MASK to FS_XFLAGS_FSXATTR? (and so also FS_XFLAGS_FSXATTR_RW)

This reduces readability considerably IMO.
There needs to be a clear separation between the
flag macros and the flag collection macros (traits if you will).

Hmm, fair, I proposed this change because the names are getting a bit long and formatting the code is harder, also they have FLAG*S* that can indicate it's a collection, but that's easily missable (and indeed, I can never remember if it's FS_XFLAG_MASK or FS_XFLAGS_MASK, one more reason removing the _MASK may be beneficial, at least to me lol).

Quoted 6 lines
Quoted 2 lines

And I'd also change FS_FILE_ATTR_XFLAGS_MASK to maybe only FS_FILE_ATTR_MASK, so it's not too long.

Losing the FS_XFLAGS_ prefix is not good IMO.
I did want to offer a change to FS_XFLAGS_FILEATTR_MASK to be consistent with the rest of the collection/trait macros.

Quoted 7 lines

Hmm, right, FS_XFLAGS_FILEATTR_MASK does indeed look better, though it is getting a bit hard to distinguish between them when they have such a long prefix.

FS_*_FL are easier to read.
Actually, could we maybe do something like FSX_ as prefix, if the FS_ prefix is not that important?

Quoted 6 lines

It's not that important, but..

Quoted 2 lines

FSX_IMMUTABLE

This is in the uapi and changing them in kernel will be a lot of churn

Yeah, I remembered that after sending the email lol.

Quoted 7 lines
Quoted 5 lines

FSX_PROJID

FSX_RDONLY_MASK
FSX_FSXATTR_MASK
FSX_FILEATTR_MASK

Those are fine by me

Nice, then I'll switch to them if the patch wouldn't be too large.

Quoted 46 lines
Quoted 5 lines

or if FS_ is important:

FS_FSX_IMMUTABLE
FS_FSX_PROJID

Those not for same reason

Quoted 4 lines

FS_FSX_RDONLY_MASK
FS_FSX_FSXATTR_MASK
FS_FSX_FILEATTR_MASK

Those fine

Quoted 9 lines

or maybe?

FS_XFL_IMMUTABLE
FS_XFL_PROJID

FS_XFL_RDONLY_MASK
FS_XFL_FSXATTR_MASK
FS_XFL_FILEATTR_MASK

Those are worse than all of the above IMO

Quoted 19 lines

or something else that is shorter, idk.

Quoted 8 lines
Quoted 4 lines

And at this point, we should consider all _MASKs to be frozen and only expand FS_FILE_ATTR_MASK, with the exception of converterd FS_*_FL flags.

This is something that we can definitely document as a guideline - use the latest v1 syscall uapi for new flags, with the exception of xflags for existing FS_*_FL flags.

Yes.

Quoted 2 lines

However, a filesystem can only report a new flag after it has opted into masks,

Report as RW or as RO?

Quoted 8 lines
Quoted 6 lines

Since for RO reporting it doesn't need to, it just needs to do that for get (and actually, only for the wanted xflag, it probably doesn't even need to report all its xflags in rw/ro masks apart from e.g. MAYSHARE... or maybe not even in the masks, since on the set path vfs treats it as a "legacy RMW"/VER0 filesystem).

Whatever guideline you write - if it makes sense its good,

Fair.

Quoted 23 lines
Quoted 23 lines
Quoted 6 lines

so if a new flag requires converting many filesystems, it may not be realistic to maintain the freeze rule.

Hopefully, the work on the may-share flag came just at the right time to allow for the ro_mask exception, so observing the guideline for new read-only flags seems doable.

Yes, MAYSHARE for testing read-only, BTIME for testing read-write (and read-only).

Quoted 12 lines
Quoted 10 lines

so:

FS_XFLAGS_FSXATTR would have the ones that are in right now + all new FS_*_FL converted ones (same for RDONLY and COMMON, etc)

FS_FILE_ATTR_MASK would have all FS_XFLAGS_FSXATTR + all new ones (MAYSHARE, BTIME, ...).

Yes, it sounds like we are converging :)

Quoted 7 lines
Quoted 3 lines

Yes, though that has been my thinking since the first time I wrote "FS_FILE_ATTR_MASK" for the ptime patch :D

Yeh this is a tough one
and this is before the patch set got review from a wider audience... I praise your perseverance.

I really just want ptime or settable btime, lmao.

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Quoted 4 lines

Isn't PROJID in FS_XFLAGS_FILE_ATTR_MASK, but not in FS_XFLAGS_FSXATTR_MASK?
Is there more to it than that?

Currently it's set like this, and there shouldn't be anything more to it.

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Nice. Looking forward to v4.
I hope I have some time to rest before, but probably not :)

I should take >= 1 breaks too instead of working on this when I'm not doing other stuff, but I already know I won't :)

Maybe if there is AI outage you can rest :D

I actually finished my normal tokens and using some API credits I got for free right now, so maybe I'll take it as an excuse :)

Thanks,
koraynilay

Quoted 2 lines

Thanks,
Amir.

author
#23 lore raw

Re: [PATCH v3 2/5] vfs: add xflags masks to fileattr API

In reply to Amir Goldstein
Quoted 12 lines · Amir Goldstein
Quoted 1 line · koraynilay

...

Quoted 8 lines
Quoted 7 lines

This is exactly what I was looking for.
PROJID is only singled out in one place (here) when defining the macro and a comment above the macro explains why.
From this point on the macro describes a trait, which has a very clear meaning, without mentioning any specific flag.

Good job, bot ;)

Quoted 8 lines
Quoted 5 lines

Yes, though, when trying to change the current code to use it, I'm not liking the name too much, since it seems like it's a subset of FS_XFLAGS_FSXATTR_MASK, while it's actually not, since FS_XFLAGS_FSXATTR_MASK does not include PROJID.

IDK, you can try _[GS]ET_MASK
it's a more accurate description.

(definition reminder:)

/*
* Writable xflags used to check and convert from masked request to unmasked
* request.
* Includes FS_XFLAG_PROJID because masked requests use it but unmasked
* filesystems that support fsx_projid don't know about it (because
* FS_IOC_FSSETXATTR has no FS_XFLAG_PROJID bit).
*/
#define FSX_FSXATTR_SET_MASK \
((FSX_FSXATTR_MASK & ~FSX_RDONLY_MASK) | FS_XFLAG_PROJID)

Which of these do you think is better?

FSX_FSXATTR_SET_MASK
FSX_SET_MASK
FSX_UNMASK_CONV_MASK
FSX_UNMASKED_SET_MASK
FSX_CONVERT_SET_MASK
FSX_COMPAT_SET_MASK

Thanks,
koraynilay

Quoted 149 lines
Quoted 20 lines
Quoted 6 lines
Quoted 2 lines

Also, could we change FS_XFLAGS_FSXATTR_MASK to FS_XFLAGS_FSXATTR? (and so also FS_XFLAGS_FSXATTR_RW)

This reduces readability considerably IMO.
There needs to be a clear separation between the
flag macros and the flag collection macros (traits if you will).

Hmm, fair, I proposed this change because the names are getting a bit long and formatting the code is harder, also they have FLAG*S* that can indicate it's a collection, but that's easily missable (and indeed, I can never remember if it's FS_XFLAG_MASK or FS_XFLAGS_MASK, one more reason removing the _MASK may be beneficial, at least to me lol).

Quoted 6 lines
Quoted 2 lines

And I'd also change FS_FILE_ATTR_XFLAGS_MASK to maybe only FS_FILE_ATTR_MASK, so it's not too long.

Losing the FS_XFLAGS_ prefix is not good IMO.
I did want to offer a change to FS_XFLAGS_FILEATTR_MASK to be consistent with the rest of the collection/trait macros.

Quoted 7 lines

Hmm, right, FS_XFLAGS_FILEATTR_MASK does indeed look better, though it is getting a bit hard to distinguish between them when they have such a long prefix.

FS_*_FL are easier to read.
Actually, could we maybe do something like FSX_ as prefix, if the FS_ prefix is not that important?

It's not that important, but..

Quoted 2 lines

FSX_IMMUTABLE

This is in the uapi and changing them in kernel will be a lot of churn

Quoted 5 lines

FSX_PROJID

FSX_RDONLY_MASK
FSX_FSXATTR_MASK
FSX_FILEATTR_MASK

Those are fine by me

Quoted 5 lines

or if FS_ is important:

FS_FSX_IMMUTABLE
FS_FSX_PROJID

Those not for same reason

Quoted 4 lines

FS_FSX_RDONLY_MASK
FS_FSX_FSXATTR_MASK
FS_FSX_FILEATTR_MASK

Those fine

Quoted 9 lines

or maybe?

FS_XFL_IMMUTABLE
FS_XFL_PROJID

FS_XFL_RDONLY_MASK
FS_XFL_FSXATTR_MASK
FS_XFL_FILEATTR_MASK

Those are worse than all of the above IMO

Quoted 17 lines

or something else that is shorter, idk.

Quoted 8 lines
Quoted 4 lines

And at this point, we should consider all _MASKs to be frozen and only expand FS_FILE_ATTR_MASK, with the exception of converterd FS_*_FL flags.

This is something that we can definitely document as a guideline - use the latest v1 syscall uapi for new flags, with the exception of xflags for existing FS_*_FL flags.

Yes.

Quoted 2 lines

However, a filesystem can only report a new flag after it has opted into masks,

Quoted 8 lines

Report as RW or as RO?

Since for RO reporting it doesn't need to, it just needs to do that for get (and actually, only for the wanted xflag, it probably doesn't even need to report all its xflags in rw/ro masks apart from e.g. MAYSHARE... or maybe not even in the masks, since on the set path vfs treats it as a "legacy RMW"/VER0 filesystem).

Whatever guideline you write - if it makes sense its good,

Quoted 17 lines
Quoted 6 lines

so if a new flag requires converting many filesystems, it may not be realistic to maintain the freeze rule.

Hopefully, the work on the may-share flag came just at the right time to allow for the ro_mask exception, so observing the guideline for new read-only flags seems doable.

Yes, MAYSHARE for testing read-only, BTIME for testing read-write (and read-only).

Quoted 7 lines
Quoted 7 lines

so:

FS_XFLAGS_FSXATTR would have the ones that are in right now + all new FS_*_FL converted ones (same for RDONLY and COMMON, etc)

Quoted 9 lines
Quoted 5 lines
Quoted 3 lines

FS_FILE_ATTR_MASK would have all FS_XFLAGS_FSXATTR + all new ones (MAYSHARE, BTIME, ...).

Yes, it sounds like we are converging :)

Yes, though that has been my thinking since the first time I wrote "FS_FILE_ATTR_MASK" for the ptime patch :D

Yeh this is a tough one
and this is before the patch set got review from a wider audience... I praise your perseverance.

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Quoted 3 lines

Isn't PROJID in FS_XFLAGS_FILE_ATTR_MASK, but not in FS_XFLAGS_FSXATTR_MASK?
Is there more to it than that?

Currently it's set like this, and there shouldn't be anything more to it.

Quoted 6 lines
Quoted 2 lines

Nice. Looking forward to v4.
I hope I have some time to rest before, but probably not :)

I should take >= 1 breaks too instead of working on this when I'm not doing other stuff, but I already know I won't :)

Maybe if there is AI outage you can rest :D

Thanks,
Amir.

#24 lore raw

Re: [PATCH v3 2/5] vfs: add xflags masks to fileattr API

Quoted 25 lines · koraynilay
Quoted 12 lines · Amir Goldstein
Quoted 1 line · koraynilay

...

Quoted 8 lines
Quoted 7 lines

This is exactly what I was looking for.
PROJID is only singled out in one place (here) when defining the macro and a comment above the macro explains why.
From this point on the macro describes a trait, which has a very clear meaning, without mentioning any specific flag.

Good job, bot ;)

Quoted 8 lines
Quoted 5 lines

Yes, though, when trying to change the current code to use it, I'm not liking the name too much, since it seems like it's a subset of FS_XFLAGS_FSXATTR_MASK, while it's actually not, since FS_XFLAGS_FSXATTR_MASK does not include PROJID.

IDK, you can try _[GS]ET_MASK
it's a more accurate description.

(definition reminder:)

Quoted 7 lines

/*
* Writable xflags used to check and convert from masked request to unmasked
* request.
* Includes FS_XFLAG_PROJID because masked requests use it but unmasked
* filesystems that support fsx_projid don't know about it (because
* FS_IOC_FSSETXATTR has no FS_XFLAG_PROJID bit).
*/

If they don't know about it why don't we strip it? This comment does not explain this at all.

Let me try:
/*
* xflags that can be changed on a filesystem that does not support masks.
* The VFS converts a masked request to an FS_IOC_FSSETXATTR style request
* for such a filesystem, so these are the writable xflags of that interface.
* FS_IOC_FSSETXATTR has no FS_XFLAG_PROJID, but it can change the project
* ID via fsx_projid. A masked request selects a project ID change with
* FS_XFLAG_PROJID, so it is allowed here and converted to fsx_projid. */

Quoted 4 lines

#define FSX_FSXATTR_SET_MASK \
((FSX_FSXATTR_MASK & ~FSX_RDONLY_MASK) | FS_XFLAG_PROJID)

Quoted 8 lines

Which of these do you think is better?

FSX_FSXATTR_SET_MASK
FSX_SET_MASK
FSX_UNMASK_CONV_MASK
FSX_UNMASKED_SET_MASK
FSX_CONVERT_SET_MASK
FSX_COMPAT_SET_MASK

FSX_FSXATTR_SET_MASK is ok.
None of the other names make much sense to me.

You could consider FSX_FSXATTR_MODIFIABLE
named after FS_FL_USER_MODIFIABLE.

I don't think it is better than _SET_MASK, but I could live with either.

Thanks,
Amir.

#26 lore raw

Re: [PATCH v3 2/5] vfs: add xflags masks to fileattr API

In reply to Amir Goldstein
Quoted 3 lines · Amir Goldstein

However, a filesystem can only report a new flag after it has opted into masks, so if a new flag requires converting many filesystems, it may not be realistic to maintain the freeze rule.

For writable flags the freeze rule may hold if a filesystem can set has_masks without first giving every FS_*_FL flag an xflag. The FS_IOC_SETFLAGS fallback from my first mail does that. With it, an FS_IOC_SETFLAGS call that changes a flag with no xflag (DIRSYNC, NOTAIL, ...) reaches the filesystem as the full read-modify-write request it got before masks. Every other set call becomes a masked request, as in v3. With the fallback, ocfs2 sets has_masks with only its five xflags, and all six of its other FS_IOC_SETFLAGS flags still set and clear.

A new writable flag then needs only the masked ->fileattr_set() of each filesystem that adds it. That filesystem sees which flags the caller named, so it can tell a change from a bit the VFS carried over, which a get_only_masks filesystem, fed read-modify-write requests, cannot.

My last mail said get_only_masks lets the may-share series drop the fallback. For read-only flags it does; for writable ones the fallback still seems needed. Could v4 take the fallback alongside get_only_masks? On top of your diff its condition becomes
"fs_has_masks && !(...)" in fileattr_set_convert().

With both applied and ocfs2 setting has_masks, not get_only_masks, all my checks pass, including the six ocfs2 FS_IOC_SETFLAGS flags; without the fallback those six fail with EOPNOTSUPP. Tree: v7.4-rc1, v3 1-3, your diff (zero rw_mask returning -EINVAL as in your tree), the mask_to_rmw line from my last mail, the fallback and the rest of the may-share series; x86_64 under QEMU.

Thanks,
Lauri

author
#25 lore raw

Re: [PATCH v3 2/5] vfs: add xflags masks to fileattr API

In reply to Amir Goldstein
Quoted 37 lines · Amir Goldstein
Quoted 25 lines · koraynilay
Quoted 12 lines · Amir Goldstein
Quoted 1 line · koraynilay

...

Quoted 8 lines
Quoted 7 lines

This is exactly what I was looking for.
PROJID is only singled out in one place (here) when defining the macro and a comment above the macro explains why.
From this point on the macro describes a trait, which has a very clear meaning, without mentioning any specific flag.

Good job, bot ;)

Quoted 8 lines
Quoted 5 lines

Yes, though, when trying to change the current code to use it, I'm not liking the name too much, since it seems like it's a subset of FS_XFLAGS_FSXATTR_MASK, while it's actually not, since FS_XFLAGS_FSXATTR_MASK does not include PROJID.

IDK, you can try _[GS]ET_MASK
it's a more accurate description.

(definition reminder:)

Quoted 7 lines

/*
* Writable xflags used to check and convert from masked request to unmasked
* request.
* Includes FS_XFLAG_PROJID because masked requests use it but unmasked
* filesystems that support fsx_projid don't know about it (because
* FS_IOC_FSSETXATTR has no FS_XFLAG_PROJID bit).
*/

If they don't know about it why don't we strip it? This comment does not explain this at all.

Quoted 9 lines

Let me try:
/*
* xflags that can be changed on a filesystem that does not support masks.
* The VFS converts a masked request to an FS_IOC_FSSETXATTR style request
* for such a filesystem, so these are the writable xflags of that interface.
* FS_IOC_FSSETXATTR has no FS_XFLAG_PROJID, but it can change the project
* ID via fsx_projid. A masked request selects a project ID change with
* FS_XFLAG_PROJID, so it is allowed here and converted to fsx_projid. */

Rewrote (the robot + edited by me) yours a little bit so it flows better:

/*
* Writable xflags for filesystems that don't support masks.
* The VFS converts masked requests to FS_IOC_FSSETXATTR format, which lacks
* FS_XFLAG_PROJID and uses only file_kattr.fsx_projid instead.
* Include FS_XFLAG_PROJID here so masked callers can use it to select project
* ID changes on unmasked filesystems.
*/
#define FSX_FSXATTR_SET_MASK \
((FSX_FSXATTR_MASK & ~FSX_RDONLY_MASK) | FS_XFLAG_PROJID)

Quoted 11 lines
Quoted 10 lines

Which of these do you think is better?

FSX_FSXATTR_SET_MASK
FSX_SET_MASK
FSX_UNMASK_CONV_MASK
FSX_UNMASKED_SET_MASK
FSX_CONVERT_SET_MASK
FSX_COMPAT_SET_MASK

Quoted 2 lines

FSX_FSXATTR_SET_MASK is ok.
None of the other names make much sense to me.

Hmm, ok.

Quoted 2 lines

You could consider FSX_FSXATTR_MODIFIABLE
named after FS_FL_USER_MODIFIABLE.

Yeah that's basically the same as _SET_, so I'll keep _SET_. What I don't like too much is the "FSXATTR", though since the project id is part of fsxattr, in that case it kinda makes sense.

Thanks,
koraynilay

Quoted 4 lines

I don't think it is better than _SET_MASK, but I could live with either.

Thanks,
Amir.

#27 lore raw

Re: [PATCH v3 2/5] vfs: add xflags masks to fileattr API

In reply to Lauri Gates
Quoted 27 lines · Lauri Gates
Quoted 3 lines · Amir Goldstein

However, a filesystem can only report a new flag after it has opted into masks, so if a new flag requires converting many filesystems, it may not be realistic to maintain the freeze rule.

For writable flags the freeze rule may hold if a filesystem can set has_masks without first giving every FS_*_FL flag an xflag. The FS_IOC_SETFLAGS fallback from my first mail does that. With it, an FS_IOC_SETFLAGS call that changes a flag with no xflag (DIRSYNC, NOTAIL, ...) reaches the filesystem as the full read-modify-write request it got before masks. Every other set call becomes a masked request, as in v3. With the fallback, ocfs2 sets has_masks with only its five xflags, and all six of its other FS_IOC_SETFLAGS flags still set and clear.

A new writable flag then needs only the masked ->fileattr_set() of each filesystem that adds it. That filesystem sees which flags the caller named, so it can tell a change from a bit the VFS carried over, which a get_only_masks filesystem, fed read-modify-write requests, cannot.

My last mail said get_only_masks lets the may-share series drop the fallback. For read-only flags it does; for writable ones the fallback still seems needed. Could v4 take the fallback alongside get_only_masks? On top of your diff its condition becomes
"fs_has_masks && !(...)" in fileattr_set_convert().

Quoted 7 lines

With both applied and ocfs2 setting has_masks, not get_only_masks, all my checks pass, including the six ocfs2 FS_IOC_SETFLAGS flags; without the fallback those six fail with EOPNOTSUPP. Tree: v7.4-rc1, v3 1-3, your diff (zero rw_mask returning -EINVAL as in your tree), the mask_to_rmw line from my last mail, the fallback and the rest of the may-share series; x86_64 under QEMU.

Not happy to do that now.
This complicates the API contract and works against the end goal of converting modern filesystems to use only one API flavor. If we do that I think filesystems will need to declare which flavors they support (has_masks, has_flags).

We can reconsider that later if the need arises to add a rw xflag for a filesystem (BTIME?) which has many _FL flags and we decide it is not worth it to convert them all to xflags.

For now, if the needs of MAYSHARE flag are met I say leave it be.

Thanks,
Amir.