Skip to contentSkip to conversation

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

PATCH In review koraynilay 2 participants 33 messages Dec 2 – Dec 4

Subsystems (10)
lore
Share & tools
lore

koraynilay

author
#0 lore raw

v4 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.

The series extends the fileattr API with xflags masks by introducing:
- fa_xflags_{rw,ro}_mask fields to struct file_attr
- fsx_xflags_{rw,ro}_mask fields and has_masks / use_masks / get_only_masks selectors 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

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.

The series 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 FSX_FSXATTR_MASK and defined as part of the new FSX_FILEATTR_MASK group of xflags that will be used moving forward for any new xflag (like the one proposed in [2]).

In addition, FS_XFLAG_COMPR and FS_XFLAG_ENCRYPT are added (as FS_COMPR_FL and FS_ENCRYPT_FL counterparts) for fs/ntfs, which is converted to the new mask API as an example. Old programs will also get these new flags, which should be fine since xflags were made to be expandable.

Documentation/filesystems/fileattr.rst explains how this API can be used by both userspace and filesystems along with 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]. A few of them can be fixed with the robot-proposed simple diff at [10], but I haven't investigated much why it works (as I also don't really understand well how overlayfs works yet).

To help me develop this patch series I used Gemini 4 Hydrogen/Neon in antigravity-cli to audit the code, find edge-cases, propose possible fixes, develop and run the tests above and help with comments/language/style.

Thanks,
koraynilay

v4 changes:
- add patch 2/7 to shorten internal xflag macro names (FSX_*) and document FSX_COMMON / FSX_RDONLY_MASK;
- split ntfs conversion into flags->xflags (5/7) and masks support (6/7);
- add use_masks and get_only_masks selectors to struct file_kattr, and clamp FS_IOC_SETFLAGS in fileattr_set_convert() (as discussed in [9]);
- rename fileattr_set_{rmw_to_mask,mask_to_rmw}() to fileattr_set_{to,from}_masks();
- reject rw_mask == 0 and clearing value attributes with non-zero values with -EINVAL;
- add FSX_FSXATTR_SET_MASK and clamp unmasked new_xflags in fileattr_set_from_masks();
- add fileattr_has_invalid_fsx_masks() and use it in ntfs_fileattr_set();
- update docs per v3 review (focus on internals, reference man-pages, VER0/VER1 phrasing);
- dropped all Assisted-by tags and add LLM-usage explanation above.

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;

[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/vfs-masks/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/
[9]: https://lore.kernel.org/[email protected]/
[10]:

fs/overlayfs/inode.c +3 -0
index 92df4f5c071e..8d4753fd2d92 100644
@@ -747,6 +747,9 @@ int ovl_fileattr_get(struct dentry *dentry, struct file_kattr *fa)
 		err = ovl_real_fileattr_get(&realpath, fa);
 	ovl_fileattr_prot_flags(inode, fa);
 
+	if (!err && fa->has_masks)
+		fa->get_only_masks = true;
+
 	return err;
 }
 

koraynilay (7):
fs: use goto out on error in vfs_fileattr_set()
fs: rename FS_XFLAGS_MASK and shorten other internal macro names
fs: add xflags masks to fileattr API
fs: advertise COMPR/ENCRYPT xflags in file_getattr()
ntfs: switch from flags to xflags in ntfs_fileattr_get/set()
ntfs: support the fileattr API masks in ntfs_fileattr_get/set()
docs: add fileattr API documentation

 Documentation/filesystems/fileattr.rst | 469 +++++++++++++++++++++++++
 Documentation/filesystems/index.rst    |   1 +
 Documentation/filesystems/vfs.rst      |  19 +-
 fs/file_attr.c                         | 404 ++++++++++++++++++---
 fs/ntfs/file.c                         |  98 +++---
 fs/overlayfs/copy_up.c                 |   2 +-
 fs/overlayfs/inode.c                   |   2 +-
 include/linux/fileattr.h               |  95 ++++-
 include/uapi/linux/fcntl.h             |   6 +
 include/uapi/linux/fs.h                |  38 +-
 10 files changed, 1010 insertions(+), 124 deletions(-)
 create mode 100644 Documentation/filesystems/fileattr.rst

base-commit: d8d9179592a52044a52591b31b914cfc0edbf36d

Signature · 1 line
2.56.0
author
#1 lore raw

PATCH v4 1/7 fs: 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 3b249a0a6b36..0f2975c9b7bc 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
#2 lore raw

PATCH v4 2/7 fs: rename FS_XFLAGS_MASK and shorten other internal macro names

In reply to koraynilay

Rename FS_XFLAGS_MASK to FSX_FSXATTR_MASK and add an explanatory comment for it so it's clearer which xflags it includes.

Make the internal xflags collection macros' names shorter so the code that uses them can be cleaner and more readable.

Don't touch the names of the actual xflags macros as those are exposed to userspace so they can't be easily changed.

---
 fs/file_attr.c           | 14 +++++++-------
 fs/overlayfs/copy_up.c   |  2 +-
 fs/overlayfs/inode.c     |  2 +-
 include/linux/fileattr.h | 19 ++++++++++---------
 4 files changed, 19 insertions(+), 18 deletions(-)
fs/file_attr.c +7 -7
index 0f2975c9b7bc..226ee34fde2d 100644
@@ -102,7 +102,7 @@ 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;
+	__u32 mask = FSX_FSXATTR_MASK;
 
 	memset(fattr, 0, sizeof(struct file_attr));
 	fattr->fa_xflags = fa->fsx_xflags & mask;
@@ -122,7 +122,7 @@ static void fileattr_to_file_attr(const struct file_kattr *fa,
 int copy_fsxattr_to_user(const struct file_kattr *fa, struct fsxattr __user *ufa)
 {
 	struct fsxattr xfa;
-	__u32 mask = FS_XFLAGS_MASK;
+	__u32 mask = FSX_FSXATTR_MASK;
 
 	memset(&xfa, 0, sizeof(xfa));
 	xfa.fsx_xflags = fa->fsx_xflags & mask;
@@ -141,12 +141,12 @@ 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 = FSX_FSXATTR_MASK;
 
 	if (fattr->fa_xflags & ~mask)
 		return -EINVAL;
 
-	fileattr_fill_xflags(fa, fattr->fa_xflags & ~FS_XFLAG_RDONLY_MASK);
+	fileattr_fill_xflags(fa, fattr->fa_xflags & ~FSX_RDONLY_MASK);
 	fa->fsx_extsize = fattr->fa_extsize;
 	fa->fsx_projid = fattr->fa_projid;
 	fa->fsx_cowextsize = fattr->fa_cowextsize;
@@ -158,7 +158,7 @@ static int copy_fsxattr_from_user(struct file_kattr *fa,
 				  struct fsxattr __user *ufa)
 {
 	struct fsxattr xfa;
-	__u32 mask = FS_XFLAGS_MASK;
+	__u32 mask = FSX_FSXATTR_MASK;
 
 	if (copy_from_user(&xfa, ufa, sizeof(xfa)))
 		return -EFAULT;
@@ -166,7 +166,7 @@ static int copy_fsxattr_from_user(struct file_kattr *fa,
 	if (xfa.fsx_xflags & ~mask)
 		return -EOPNOTSUPP;
 
-	fileattr_fill_xflags(fa, xfa.fsx_xflags & ~FS_XFLAG_RDONLY_MASK);
+	fileattr_fill_xflags(fa, xfa.fsx_xflags & ~FSX_RDONLY_MASK);
 	fa->fsx_extsize = xfa.fsx_extsize;
 	fa->fsx_nextents = xfa.fsx_nextents;
 	fa->fsx_projid = xfa.fsx_projid;
@@ -290,7 +290,7 @@ int vfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry,
 
 	/* initialize missing bits from old_ma */
 	if (fa->flags_valid) {
-		fa->fsx_xflags |= old_ma.fsx_xflags & ~FS_XFLAG_COMMON;
+		fa->fsx_xflags |= old_ma.fsx_xflags & ~FSX_COMMON;
 		fa->fsx_extsize = old_ma.fsx_extsize;
 		fa->fsx_nextents = old_ma.fsx_nextents;
 		fa->fsx_projid = old_ma.fsx_projid;
fs/overlayfs/copy_up.c +1 -1
index c147ad155b79..20f369863faf 100644
@@ -220,7 +220,7 @@ static int ovl_copy_fileattr(struct inode *inode, const struct path *old,
 	newfa.flags &= ~OVL_COPY_FS_FLAGS_MASK;
 	newfa.flags |= (oldfa.flags & OVL_COPY_FS_FLAGS_MASK);
 
-	BUILD_BUG_ON(OVL_COPY_FSX_FLAGS_MASK & ~FS_XFLAG_COMMON);
+	BUILD_BUG_ON(OVL_COPY_FSX_FLAGS_MASK & ~FSX_COMMON);
 	newfa.fsx_xflags &= ~OVL_COPY_FSX_FLAGS_MASK;
 	newfa.fsx_xflags |= (oldfa.fsx_xflags & OVL_COPY_FSX_FLAGS_MASK);
 
fs/overlayfs/inode.c +1 -1
index f1c6df39d0ae..92df4f5c071e 100644
@@ -709,7 +709,7 @@ int ovl_fileattr_set(const struct mnt_idmap *idmap,
 static void ovl_fileattr_prot_flags(struct inode *inode, struct file_kattr *fa)
 {
 	BUILD_BUG_ON(OVL_PROT_FS_FLAGS_MASK & ~FS_COMMON_FL);
-	BUILD_BUG_ON(OVL_PROT_FSX_FLAGS_MASK & ~FS_XFLAG_COMMON);
+	BUILD_BUG_ON(OVL_PROT_FSX_FLAGS_MASK & ~FSX_COMMON);
 
 	if (inode->i_flags & S_APPEND) {
 		fa->flags |= FS_APPEND_FL;
include/linux/fileattr.h +10 -9
index 09e32b84e02a..c26f70aaa3b8 100644
@@ -9,31 +9,32 @@
 	 FS_NODUMP_FL |	FS_NOATIME_FL | FS_DAX_FL | \
 	 FS_PROJINHERIT_FL | FS_VERITY_FL)
 
-#define FS_XFLAG_COMMON \
+#define FSX_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)
 
 /* Read-only inode flags */
-#define FS_XFLAG_RDONLY_MASK \
+#define FSX_RDONLY_MASK \
 	(FS_XFLAG_PREALLOC | FS_XFLAG_HASATTR | FS_XFLAG_VERITY | \
 	 FS_XFLAG_CASEFOLD | FS_XFLAG_CASENONPRESERVING)
 
 /* Flags to indicate valid value of fsx_ fields */
-#define FS_XFLAG_VALUES_MASK \
+#define FSX_VALUES_MASK \
 	(FS_XFLAG_EXTSIZE | FS_XFLAG_COWEXTSIZE)
 
 /* Flags for directories */
-#define FS_XFLAG_DIRONLY_MASK \
+#define FSX_DIRONLY_MASK \
 	(FS_XFLAG_RTINHERIT | FS_XFLAG_NOSYMLINKS | FS_XFLAG_EXTSZINHERIT)
 
 /* Misc settable flags */
-#define FS_XFLAG_MISC_MASK \
+#define FSX_MISC_MASK \
 	(FS_XFLAG_REALTIME | FS_XFLAG_NODEFRAG | FS_XFLAG_FILESTREAM)
 
-#define FS_XFLAGS_MASK \
-	(FS_XFLAG_COMMON | FS_XFLAG_RDONLY_MASK | FS_XFLAG_VALUES_MASK | \
-	 FS_XFLAG_DIRONLY_MASK | FS_XFLAG_MISC_MASK)
+/* xflags supported by FS_IOC_FS[GS]ETXATTR and file_[gs]etattr() */
+#define FSX_FSXATTR_MASK \
+	(FSX_COMMON | FSX_RDONLY_MASK | FSX_VALUES_MASK | \
+	 FSX_DIRONLY_MASK | FSX_MISC_MASK)
 
 /*
  * Merged interface for miscellaneous file attributes.  'flags' originates from
@@ -69,7 +70,7 @@ void fileattr_fill_flags(struct file_kattr *fa, u32 flags);
 static inline bool fileattr_has_fsx(const struct file_kattr *fa)
 {
 	return fa->fsx_valid &&
-		((fa->fsx_xflags & ~FS_XFLAG_COMMON) || fa->fsx_extsize != 0 ||
+		((fa->fsx_xflags & ~FSX_COMMON) || fa->fsx_extsize != 0 ||
 		 fa->fsx_projid != 0 ||	fa->fsx_cowextsize != 0);
 }
 
Signature · 1 line
2.56.0
maintainer Reviewed-by on v4 2/7
#3 lore raw

Re: [PATCH v4 2/7] fs: rename FS_XFLAGS_MASK and shorten other internal macro names

Quoted 141 lines · koraynilay

Rename FS_XFLAGS_MASK to FSX_FSXATTR_MASK and add an explanatory comment for it so it's clearer which xflags it includes.

Make the internal xflags collection macros' names shorter so the code that uses them can be cleaner and more readable.

Don't touch the names of the actual xflags macros as those are exposed to userspace so they can't be easily changed.

---
 fs/file_attr.c           | 14 +++++++-------
 fs/overlayfs/copy_up.c   |  2 +-
 fs/overlayfs/inode.c     |  2 +-
 include/linux/fileattr.h | 19 ++++++++++---------
 4 files changed, 19 insertions(+), 18 deletions(-)
fs/file_attr.c +7 -7
index 0f2975c9b7bc..226ee34fde2d 100644
@@ -102,7 +102,7 @@ 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;
+       __u32 mask = FSX_FSXATTR_MASK;
 
        memset(fattr, 0, sizeof(struct file_attr));
        fattr->fa_xflags = fa->fsx_xflags & mask;
@@ -122,7 +122,7 @@ static void fileattr_to_file_attr(const struct file_kattr *fa,
 int copy_fsxattr_to_user(const struct file_kattr *fa, struct fsxattr __user *ufa)
 {
        struct fsxattr xfa;
-       __u32 mask = FS_XFLAGS_MASK;
+       __u32 mask = FSX_FSXATTR_MASK;
 
        memset(&xfa, 0, sizeof(xfa));
        xfa.fsx_xflags = fa->fsx_xflags & mask;
@@ -141,12 +141,12 @@ 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 = FSX_FSXATTR_MASK;
 
        if (fattr->fa_xflags & ~mask)
                return -EINVAL;
 
-       fileattr_fill_xflags(fa, fattr->fa_xflags & ~FS_XFLAG_RDONLY_MASK);
+       fileattr_fill_xflags(fa, fattr->fa_xflags & ~FSX_RDONLY_MASK);
        fa->fsx_extsize = fattr->fa_extsize;
        fa->fsx_projid = fattr->fa_projid;
        fa->fsx_cowextsize = fattr->fa_cowextsize;
@@ -158,7 +158,7 @@ static int copy_fsxattr_from_user(struct file_kattr *fa,
                                  struct fsxattr __user *ufa)
 {
        struct fsxattr xfa;
-       __u32 mask = FS_XFLAGS_MASK;
+       __u32 mask = FSX_FSXATTR_MASK;
 
        if (copy_from_user(&xfa, ufa, sizeof(xfa)))
                return -EFAULT;
@@ -166,7 +166,7 @@ static int copy_fsxattr_from_user(struct file_kattr *fa,
        if (xfa.fsx_xflags & ~mask)
                return -EOPNOTSUPP;
 
-       fileattr_fill_xflags(fa, xfa.fsx_xflags & ~FS_XFLAG_RDONLY_MASK);
+       fileattr_fill_xflags(fa, xfa.fsx_xflags & ~FSX_RDONLY_MASK);
        fa->fsx_extsize = xfa.fsx_extsize;
        fa->fsx_nextents = xfa.fsx_nextents;
        fa->fsx_projid = xfa.fsx_projid;
@@ -290,7 +290,7 @@ int vfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry,
 
        /* initialize missing bits from old_ma */
        if (fa->flags_valid) {
-               fa->fsx_xflags |= old_ma.fsx_xflags & ~FS_XFLAG_COMMON;
+               fa->fsx_xflags |= old_ma.fsx_xflags & ~FSX_COMMON;
                fa->fsx_extsize = old_ma.fsx_extsize;
                fa->fsx_nextents = old_ma.fsx_nextents;
                fa->fsx_projid = old_ma.fsx_projid;
fs/overlayfs/copy_up.c +1 -1
index c147ad155b79..20f369863faf 100644
@@ -220,7 +220,7 @@ static int ovl_copy_fileattr(struct inode *inode, const struct path *old,
        newfa.flags &= ~OVL_COPY_FS_FLAGS_MASK;
        newfa.flags |= (oldfa.flags & OVL_COPY_FS_FLAGS_MASK);
 
-       BUILD_BUG_ON(OVL_COPY_FSX_FLAGS_MASK & ~FS_XFLAG_COMMON);
+       BUILD_BUG_ON(OVL_COPY_FSX_FLAGS_MASK & ~FSX_COMMON);
        newfa.fsx_xflags &= ~OVL_COPY_FSX_FLAGS_MASK;
        newfa.fsx_xflags |= (oldfa.fsx_xflags & OVL_COPY_FSX_FLAGS_MASK);
 
fs/overlayfs/inode.c +1 -1
index f1c6df39d0ae..92df4f5c071e 100644
@@ -709,7 +709,7 @@ int ovl_fileattr_set(const struct mnt_idmap *idmap,
 static void ovl_fileattr_prot_flags(struct inode *inode, struct file_kattr *fa)
 {
        BUILD_BUG_ON(OVL_PROT_FS_FLAGS_MASK & ~FS_COMMON_FL);
-       BUILD_BUG_ON(OVL_PROT_FSX_FLAGS_MASK & ~FS_XFLAG_COMMON);
+       BUILD_BUG_ON(OVL_PROT_FSX_FLAGS_MASK & ~FSX_COMMON);
 
        if (inode->i_flags & S_APPEND) {
                fa->flags |= FS_APPEND_FL;
include/linux/fileattr.h +5 -5
index 09e32b84e02a..c26f70aaa3b8 100644
@@ -9,31 +9,32 @@
         FS_NODUMP_FL | FS_NOATIME_FL | FS_DAX_FL | \
         FS_PROJINHERIT_FL | FS_VERITY_FL)
 
-#define FS_XFLAG_COMMON \
+#define FSX_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)
 
 /* Read-only inode flags */
-#define FS_XFLAG_RDONLY_MASK \
+#define FSX_RDONLY_MASK \
        (FS_XFLAG_PREALLOC | FS_XFLAG_HASATTR | FS_XFLAG_VERITY | \
         FS_XFLAG_CASEFOLD | FS_XFLAG_CASENONPRESERVING)
 
 /* Flags to indicate valid value of fsx_ fields */
-#define FS_XFLAG_VALUES_MASK \
+#define FSX_VALUES_MASK \
        (FS_XFLAG_EXTSIZE | FS_XFLAG_COWEXTSIZE)
 
 /* Flags for directories */
-#define FS_XFLAG_DIRONLY_MASK \
+#define FSX_DIRONLY_MASK \
        (FS_XFLAG_RTINHERIT | FS_XFLAG_NOSYMLINKS | FS_XFLAG_EXTSZINHERIT)
 
 /* Misc settable flags */
-#define FS_XFLAG_MISC_MASK \
+#define FSX_MISC_MASK \

include/linux/fileattr.h line 38

Quoted 10 lines
include/linux/fileattr.h +4 -3
        (FS_XFLAG_REALTIME | FS_XFLAG_NODEFRAG | FS_XFLAG_FILESTREAM)
 
-#define FS_XFLAGS_MASK \
-       (FS_XFLAG_COMMON | FS_XFLAG_RDONLY_MASK | FS_XFLAG_VALUES_MASK | \
-        FS_XFLAG_DIRONLY_MASK | FS_XFLAG_MISC_MASK)
+/* xflags supported by FS_IOC_FS[GS]ETXATTR and file_[gs]etattr() */
+#define FSX_FSXATTR_MASK \
+       (FSX_COMMON | FSX_RDONLY_MASK | FSX_VALUES_MASK | \
+        FSX_DIRONLY_MASK | FSX_MISC_MASK)
 

As I wrote in patch 4, I suggest adding the comments for COMMON and RDONLY macros in this patch.

In any case, feel free to add

Thanks,
Amir.

author
#4 lore raw

PATCH v4 3/7 fs: 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 and the value is 0, this informs the kernel to unset/clear that value (if the value field is non-zero it gets rejected with -EINVAL).

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 has_masks, use_masks and get_only_masks selectors to struct file_kattr, 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: convert the input as necessary for non-masks filesystems or pass the input directly to the filesystem if it supports masks; if the rw_mask is 0, reject it with -EINVAL.

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_from_masks() / fileattr_set_to_masks(): 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 FSX_FILEATTR_MASK (superset of FSX_FSXATTR_MASK) to support new xflags and FSX_FSXATTR_SET_MASK to allow masked requests to use FS_XFLAG_PROJID on filesystems without masks.

Finally, move FILE_ATTR_SIZE_* constants from the user-facing include/uapi/linux/fs.h to the internal include/linux/fileattr.h.

---
 fs/file_attr.c             | 365 +++++++++++++++++++++++++++++++++----
 include/linux/fileattr.h   |  29 ++-
 include/uapi/linux/fcntl.h |   6 +
 include/uapi/linux/fs.h    |  36 +++-
 4 files changed, 394 insertions(+), 42 deletions(-)
fs/file_attr.c +329 -36
index 226ee34fde2d..3b3e84226f71 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 &= FSX_FILEATTR_MASK;
+		fa->fsx_xflags_ro_mask &= FSX_FILEATTR_MASK;
+	}
+	fa->fsx_xflags &= FSX_FILEATTR_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 = FSX_FSXATTR_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,62 @@ EXPORT_SYMBOL(copy_fsxattr_to_user);
 static int file_attr_to_fileattr(const struct file_attr *fattr,
 				 struct file_kattr *fa)
 {
-	__u64 mask = FSX_FSXATTR_MASK;
+	bool use_masks = fa->use_masks && fa->has_masks;
+	u64 mask = use_masks ? FSX_FILEATTR_MASK : FSX_FSXATTR_MASK;
 
 	if (fattr->fa_xflags & ~mask)
 		return -EINVAL;
 
-	fileattr_fill_xflags(fa, fattr->fa_xflags & ~FSX_RDONLY_MASK);
-	fa->fsx_extsize = fattr->fa_extsize;
-	fa->fsx_projid = fattr->fa_projid;
-	fa->fsx_cowextsize = fattr->fa_cowextsize;
+	if (use_masks) {
+		u64 rw_user_mask = fattr->fa_xflags_rw_mask;
+		u64 tmp_xflag = 0;
+
+		if (rw_user_mask & ~FSX_FILEATTR_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;
+
+		/*
+		 * For value attributes, clearing the xflag bit in fa_xflags
+		 * while selecting it in rw_mask requires the field value to
+		 * be 0, to prevent accidental resets.
+		 */
+		tmp_xflag = FS_XFLAG_EXTSIZE | FS_XFLAG_EXTSZINHERIT;
+		if (fa->fsx_xflags & tmp_xflag)
+			fa->fsx_extsize = fattr->fa_extsize;
+		else if ((rw_user_mask & tmp_xflag) && fattr->fa_extsize)
+			return -EINVAL;
+		else
+			fa->fsx_extsize = 0;
+
+		tmp_xflag = FS_XFLAG_PROJID;
+		if (fa->fsx_xflags & tmp_xflag)
+			fa->fsx_projid = fattr->fa_projid;
+		else if ((rw_user_mask & tmp_xflag) && fattr->fa_projid)
+			return -EINVAL;
+		else
+			fa->fsx_projid = 0;
+
+		tmp_xflag = FS_XFLAG_COWEXTSIZE;
+		if (fa->fsx_xflags & tmp_xflag)
+			fa->fsx_cowextsize = fattr->fa_cowextsize;
+		else if ((rw_user_mask & tmp_xflag) && fattr->fa_cowextsize)
+			return -EINVAL;
+		else
+			fa->fsx_cowextsize = 0;
+	} else {
+		fileattr_fill_xflags(fa,
+				     fattr->fa_xflags & ~FSX_RDONLY_MASK);
+		fa->fsx_extsize = fattr->fa_extsize;
+		fa->fsx_projid = fattr->fa_projid;
+		fa->fsx_cowextsize = fattr->fa_cowextsize;
+	}
 
 	return 0;
 }
@@ -175,6 +253,178 @@ static int copy_fsxattr_from_user(struct file_kattr *fa,
 	return 0;
 }
 
+/*
+ * Convert FSSETXATTR input format without masks, coming from userspace, to
+ * input format with masks, for filesystems that already support masks.
+ */
+static int fileattr_set_to_masks(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 &= FSX_FSXATTR_MASK & ~FSX_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->use_masks = true;
+
+	fa->fsx_valid = true;
+	fa->flags_valid = false;
+
+	return 0;
+}
+
+/*
+ * Convert input format with masks, coming from userspace, to FSSETXATTR input
+ * format without masks, for filesystems that don't support masks yet.
+ */
+static int fileattr_set_from_masks(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;
+
+	if (rw_user_mask & ~FSX_FSXATTR_SET_MASK)
+		return -EOPNOTSUPP;
+
+	/* Do RMW. */
+	new_xflags |= old_ma->fsx_xflags & ~rw_user_mask;
+
+	/* RMW filesystems only expect writable fsxattr xflags. */
+	new_xflags &= FSX_FSXATTR_SET_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->use_masks = false;
+
+	fa->fsx_valid = true;
+	fa->flags_valid = false;
+
+	return 0;
+}
+
+/*
+ * Handle converting between FSSETXATTR 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)
+{
+	bool fs_has_masks = old_ma->has_masks && !old_ma->get_only_masks;
+
+	if (fa->has_masks) {
+		if (!fs_has_masks)
+			return fileattr_set_from_masks(fa, old_ma);
+
+		/* Both have masks, pass the request through. */
+		return 0;
+	}
+
+	if (fs_has_masks)
+		return fileattr_set_to_masks(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 & ~FSX_COMMON;
+		fa->fsx_xflags &= FSX_FSXATTR_MASK;
+		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 +435,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 : FSX_FILEATTR_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 +464,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 +510,8 @@ 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;
+	if (fa->fsx_projid == 0)
+		fa->fsx_xflags &= ~FS_XFLAG_PROJID;
 
 	return 0;
 }
@@ -283,21 +545,24 @@ 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 semantics */
+	if (!fa->use_masks)
+		fa->has_masks = false;
+
+	if (fa->has_masks && !fa->fsx_xflags_rw_mask)
+		return -EINVAL;
+
 	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 & ~FSX_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 +655,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 +665,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 +693,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 &= FSX_FSXATTR_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 +723,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 +733,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 +744,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 +763,15 @@ SYSCALL_DEFINE5(file_setattr, int, dfd, const char __user *, filename,
 			return error;
 	}
 
+	if (at_flags & AT_FILEATTR_MASKS) {
+		fa.has_masks = true;
+		fa.use_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 +28 -1
index c26f70aaa3b8..4db19556981a 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 | \
@@ -31,11 +36,25 @@
 #define FSX_MISC_MASK \
 	(FS_XFLAG_REALTIME | FS_XFLAG_NODEFRAG | FS_XFLAG_FILESTREAM)
 
-/* xflags supported by FS_IOC_FS[GS]ETXATTR and file_[gs]etattr() */
+/* xflags supported by FS_IOC_FS[GS]ETXATTR and VER0 file_[gs]etattr() */
 #define FSX_FSXATTR_MASK \
 	(FSX_COMMON | FSX_RDONLY_MASK | FSX_VALUES_MASK | \
 	 FSX_DIRONLY_MASK | FSX_MISC_MASK)
 
+/*
+ * 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)
+
+/* All xflags supported by VER1+ file_[gs]etattr() with AT_FILEATTR_MASKS */
+#define FSX_FILEATTR_MASK \
+	(FSX_FSXATTR_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
@@ -46,6 +65,9 @@ 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 writable 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) */
@@ -53,6 +75,11 @@ struct file_kattr {
 	/* selectors: */
 	bool	flags_valid:1;
 	bool	fsx_valid:1;
+	bool	has_masks:1;	/* struct has valid masks (get/set) */
+	bool	use_masks:1;	/* equivalent to AT_FILEATTR_MASKS but for
+				   in-kernel calls (set) */
+	bool	get_only_masks:1;/* 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);
include/uapi/linux/fcntl.h +6 -0
index faa63d0b60d2..7347efb3df3c 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 +31 -5
index a13c7b32a1bd..337bb00b0e64 100644
@@ -225,9 +225,34 @@ 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 writable 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.
@@ -238,11 +263,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 writable 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 +294,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
maintainer
#5 lore raw

Re: [PATCH v4 3/7] fs: add xflags masks to fileattr API

Quoted 75 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 and the value is 0, this informs the kernel to unset/clear that value (if the value field is non-zero it gets rejected with -EINVAL).

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 has_masks, use_masks and get_only_masks selectors to struct file_kattr, 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: convert the input as necessary for non-masks filesystems or pass the input directly to the filesystem if it supports masks; if the rw_mask is 0, reject it with -EINVAL.

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_from_masks() / fileattr_set_to_masks(): 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 FSX_FILEATTR_MASK (superset of FSX_FSXATTR_MASK) to support new xflags and FSX_FSXATTR_SET_MASK to allow masked requests to use FS_XFLAG_PROJID on filesystems without masks.

Quoted 6 lines

Finally, move FILE_ATTR_SIZE_* constants from the user-facing include/uapi/linux/fs.h to the internal include/linux/fileattr.h.

---
 fs/file_attr.c             | 365 +++++++++++++++++++++++++++++++++----

This patch is too much for me I cannot review it as is

Try to use mechanical smaller steps which are easier to review

Some suggestions from the bot:
1. switch fileattr_set_prepare() to xflags + factor out fileattr_set_convert() (no functional change)
2. kernel-internal masks in file_kattr + fileattr_set_to_masks()
3. uapi: AT_FILEATTR_MASKS, file_attr masks + fileattr_set_from_masks()
4. FS_XFLAG_PROJID

Honestly, I don't know if those are good suggestion and I really don't care how it is split, just try to frikin read it yourself - it's too big to swallow - or maybe I am too old for this - a dinosaur still trying to read a patch with human eyes :)

Quoted 78 lines
 include/linux/fileattr.h   |  29 ++-
 include/uapi/linux/fcntl.h |   6 +
 include/uapi/linux/fs.h    |  36 +++-
 4 files changed, 394 insertions(+), 42 deletions(-)
fs/file_attr.c +36 -5
index 226ee34fde2d..3b3e84226f71 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 &= FSX_FILEATTR_MASK;
+               fa->fsx_xflags_ro_mask &= FSX_FILEATTR_MASK;
+       }
+       fa->fsx_xflags &= FSX_FILEATTR_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 = FSX_FSXATTR_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,62 @@ EXPORT_SYMBOL(copy_fsxattr_to_user);
 static int file_attr_to_fileattr(const struct file_attr *fattr,
                                 struct file_kattr *fa)
 {
-       __u64 mask = FSX_FSXATTR_MASK;
+       bool use_masks = fa->use_masks && fa->has_masks;

fs/file_attr.c EXPORT_SYMBOL(copy_fsxattr_to_user); · line 181

Quoted 10 lines
fs/file_attr.c +2 -4
+       u64 mask = use_masks ? FSX_FILEATTR_MASK : FSX_FSXATTR_MASK;
 
        if (fattr->fa_xflags & ~mask)
                return -EINVAL;
 
-       fileattr_fill_xflags(fa, fattr->fa_xflags & ~FSX_RDONLY_MASK);
-       fa->fsx_extsize = fattr->fa_extsize;
-       fa->fsx_projid = fattr->fa_projid;
-       fa->fsx_cowextsize = fattr->fa_cowextsize;
+       if (use_masks) {

It's too frikin long scope to stay in an if statement it needs a helper functions need to be shorter and avoid unneeded nesting.

Sorry I cannot give more constructive feedback.

That's what bots are for :-p

Thanks,
Amir.

Quoted 573 lines
fs/file_attr.c +291 -27
+               u64 rw_user_mask = fattr->fa_xflags_rw_mask;
+               u64 tmp_xflag = 0;
+
+               if (rw_user_mask & ~FSX_FILEATTR_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;
+
+               /*
+                * For value attributes, clearing the xflag bit in fa_xflags
+                * while selecting it in rw_mask requires the field value to
+                * be 0, to prevent accidental resets.
+                */
+               tmp_xflag = FS_XFLAG_EXTSIZE | FS_XFLAG_EXTSZINHERIT;
+               if (fa->fsx_xflags & tmp_xflag)
+                       fa->fsx_extsize = fattr->fa_extsize;
+               else if ((rw_user_mask & tmp_xflag) && fattr->fa_extsize)
+                       return -EINVAL;
+               else
+                       fa->fsx_extsize = 0;
+
+               tmp_xflag = FS_XFLAG_PROJID;
+               if (fa->fsx_xflags & tmp_xflag)
+                       fa->fsx_projid = fattr->fa_projid;
+               else if ((rw_user_mask & tmp_xflag) && fattr->fa_projid)
+                       return -EINVAL;
+               else
+                       fa->fsx_projid = 0;
+
+               tmp_xflag = FS_XFLAG_COWEXTSIZE;
+               if (fa->fsx_xflags & tmp_xflag)
+                       fa->fsx_cowextsize = fattr->fa_cowextsize;
+               else if ((rw_user_mask & tmp_xflag) && fattr->fa_cowextsize)
+                       return -EINVAL;
+               else
+                       fa->fsx_cowextsize = 0;
+       } else {
+               fileattr_fill_xflags(fa,
+                                    fattr->fa_xflags & ~FSX_RDONLY_MASK);
+               fa->fsx_extsize = fattr->fa_extsize;
+               fa->fsx_projid = fattr->fa_projid;
+               fa->fsx_cowextsize = fattr->fa_cowextsize;
+       }
 
        return 0;
 }
@@ -175,6 +253,178 @@ static int copy_fsxattr_from_user(struct file_kattr *fa,
        return 0;
 }
 
+/*
+ * Convert FSSETXATTR input format without masks, coming from userspace, to
+ * input format with masks, for filesystems that already support masks.
+ */
+static int fileattr_set_to_masks(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 &= FSX_FSXATTR_MASK & ~FSX_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->use_masks = true;
+
+       fa->fsx_valid = true;
+       fa->flags_valid = false;
+
+       return 0;
+}
+
+/*
+ * Convert input format with masks, coming from userspace, to FSSETXATTR input
+ * format without masks, for filesystems that don't support masks yet.
+ */
+static int fileattr_set_from_masks(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;
+
+       if (rw_user_mask & ~FSX_FSXATTR_SET_MASK)
+               return -EOPNOTSUPP;
+
+       /* Do RMW. */
+       new_xflags |= old_ma->fsx_xflags & ~rw_user_mask;
+
+       /* RMW filesystems only expect writable fsxattr xflags. */
+       new_xflags &= FSX_FSXATTR_SET_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->use_masks = false;
+
+       fa->fsx_valid = true;
+       fa->flags_valid = false;
+
+       return 0;
+}
+
+/*
+ * Handle converting between FSSETXATTR 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)
+{
+       bool fs_has_masks = old_ma->has_masks && !old_ma->get_only_masks;
+
+       if (fa->has_masks) {
+               if (!fs_has_masks)
+                       return fileattr_set_from_masks(fa, old_ma);
+
+               /* Both have masks, pass the request through. */
+               return 0;
+       }
+
+       if (fs_has_masks)
+               return fileattr_set_to_masks(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 & ~FSX_COMMON;
+               fa->fsx_xflags &= FSX_FSXATTR_MASK;
+               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 +435,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 : FSX_FILEATTR_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 +464,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 +510,8 @@ 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;
+       if (fa->fsx_projid == 0)
+               fa->fsx_xflags &= ~FS_XFLAG_PROJID;
 
        return 0;
 }
@@ -283,21 +545,24 @@ 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 semantics */
+       if (!fa->use_masks)
+               fa->has_masks = false;
+
+       if (fa->has_masks && !fa->fsx_xflags_rw_mask)
+               return -EINVAL;
+
        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 & ~FSX_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 +655,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 +665,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 +693,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 &= FSX_FSXATTR_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 +723,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 +733,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 +744,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 +763,15 @@ SYSCALL_DEFINE5(file_setattr, int, dfd, const char __user *, filename,
                        return error;
        }
 
+       if (at_flags & AT_FILEATTR_MASKS) {
+               fa.has_masks = true;
+               fa.use_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 +28 -1
index c26f70aaa3b8..4db19556981a 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 | \
@@ -31,11 +36,25 @@
 #define FSX_MISC_MASK \
        (FS_XFLAG_REALTIME | FS_XFLAG_NODEFRAG | FS_XFLAG_FILESTREAM)
 
-/* xflags supported by FS_IOC_FS[GS]ETXATTR and file_[gs]etattr() */
+/* xflags supported by FS_IOC_FS[GS]ETXATTR and VER0 file_[gs]etattr() */
 #define FSX_FSXATTR_MASK \
        (FSX_COMMON | FSX_RDONLY_MASK | FSX_VALUES_MASK | \
         FSX_DIRONLY_MASK | FSX_MISC_MASK)
 
+/*
+ * 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)
+
+/* All xflags supported by VER1+ file_[gs]etattr() with AT_FILEATTR_MASKS */
+#define FSX_FILEATTR_MASK \
+       (FSX_FSXATTR_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
@@ -46,6 +65,9 @@ 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 writable 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) */
@@ -53,6 +75,11 @@ struct file_kattr {
        /* selectors: */
        bool    flags_valid:1;
        bool    fsx_valid:1;
+       bool    has_masks:1;    /* struct has valid masks (get/set) */
+       bool    use_masks:1;    /* equivalent to AT_FILEATTR_MASKS but for
+                                  in-kernel calls (set) */
+       bool    get_only_masks:1;/* 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);
include/uapi/linux/fcntl.h +6 -0
index faa63d0b60d2..7347efb3df3c 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 +31 -6
index a13c7b32a1bd..337bb00b0e64 100644
@@ -225,9 +225,34 @@ 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 writable 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.
@@ -238,11 +263,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 writable 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 +294,8 @@ struct file_attr {
  */
 #define FS_XFLAG_CASEFOLD      0x00040000      /* case-insensitive lookups */
 #define FS_XFLAG_CASENONPRESERVING 0x00080000  /* case not preserved */
+#define FS_XFLAG_PROJID                0x00100000      /* project ID */
+
 #define FS_XFLAG_HASATTR       0x80000000      /* no DIFLAG for this   */
 
 /* the read-only stuff doesn't really belong here, but any other place is
--

2.56.0

author
#6 lore raw

Re: [PATCH v4 3/7] fs: add xflags masks to fileattr API

Quoted 94 lines · Amir Goldstein
Quoted 75 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 and the value is 0, this informs the kernel to unset/clear that value (if the value field is non-zero it gets rejected with -EINVAL).

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 has_masks, use_masks and get_only_masks selectors to struct file_kattr, 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: convert the input as necessary for non-masks filesystems or pass the input directly to the filesystem if it supports masks; if the rw_mask is 0, reject it with -EINVAL.

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_from_masks() / fileattr_set_to_masks(): 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 FSX_FILEATTR_MASK (superset of FSX_FSXATTR_MASK) to support new xflags and FSX_FSXATTR_SET_MASK to allow masked requests to use FS_XFLAG_PROJID on filesystems without masks.

Quoted 6 lines

Finally, move FILE_ATTR_SIZE_* constants from the user-facing include/uapi/linux/fs.h to the internal include/linux/fileattr.h.

---
 fs/file_attr.c             | 365 +++++++++++++++++++++++++++++++++----

This patch is too much for me I cannot review it as is

Try to use mechanical smaller steps which are easier to review

Some suggestions from the bot:
1. switch fileattr_set_prepare() to xflags + factor out fileattr_set_convert() (no functional change)
2. kernel-internal masks in file_kattr + fileattr_set_to_masks()
3. uapi: AT_FILEATTR_MASKS, file_attr masks + fileattr_set_from_masks()
4. FS_XFLAG_PROJID

Quoted 4 lines

Honestly, I don't know if those are good suggestion and I really don't care how it is split, just try to frikin read it yourself - it's too big to swallow - or maybe I am too old for this - a dinosaur still trying to read a patch with human eyes :)

Yeah, totally fair, I wouldn't mind reading it whole, but I also have a lot of time to dedicate to this and I especially don't have any other patches to review :D

Quoted 83 lines
Quoted 83 lines
 include/linux/fileattr.h   |  29 ++-
 include/uapi/linux/fcntl.h |   6 +
 include/uapi/linux/fs.h    |  36 +++-
 4 files changed, 394 insertions(+), 42 deletions(-)
fs/file_attr.c +37 -5
index 226ee34fde2d..3b3e84226f71 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 &= FSX_FILEATTR_MASK;
+               fa->fsx_xflags_ro_mask &= FSX_FILEATTR_MASK;
+       }
+       fa->fsx_xflags &= FSX_FILEATTR_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 = FSX_FSXATTR_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,62 @@ EXPORT_SYMBOL(copy_fsxattr_to_user);
 static int file_attr_to_fileattr(const struct file_attr *fattr,
                                 struct file_kattr *fa)
 {
-       __u64 mask = FSX_FSXATTR_MASK;
+       bool use_masks = fa->use_masks && fa->has_masks;
+       u64 mask = use_masks ? FSX_FILEATTR_MASK : FSX_FSXATTR_MASK;
 
        if (fattr->fa_xflags & ~mask)
                return -EINVAL;
 
Quoted 8 lines

fs/file_attr.c EXPORT_SYMBOL(copy_fsxattr_to_user); · line 181

Quoted 5 lines
fs/file_attr.c +1 -4
-       fileattr_fill_xflags(fa, fattr->fa_xflags & ~FSX_RDONLY_MASK);
-       fa->fsx_extsize = fattr->fa_extsize;
-       fa->fsx_projid = fattr->fa_projid;
-       fa->fsx_cowextsize = fattr->fa_cowextsize;
+       if (use_masks) {

It's too frikin long scope to stay in an if statement it needs a helper functions need to be shorter and avoid unneeded nesting.

Fair, I will also try to find a way (if it exists) to abstract the value fields checks so we don't need to repeat the 3 if elses in 3 difference places in the code. I had it in mind while doing v3 already, but didn't get around to it.

Quoted 3 lines

Sorry I cannot give more constructive feedback.

That's what bots are for :-p

The bots will make even longer commit messages though :)

Thanks,
koraynilay

Quoted 577 lines

Thanks,
Amir.

Quoted 573 lines
fs/file_attr.c +291 -27
+               u64 rw_user_mask = fattr->fa_xflags_rw_mask;
+               u64 tmp_xflag = 0;
+
+               if (rw_user_mask & ~FSX_FILEATTR_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;
+
+               /*
+                * For value attributes, clearing the xflag bit in fa_xflags
+                * while selecting it in rw_mask requires the field value to
+                * be 0, to prevent accidental resets.
+                */
+               tmp_xflag = FS_XFLAG_EXTSIZE | FS_XFLAG_EXTSZINHERIT;
+               if (fa->fsx_xflags & tmp_xflag)
+                       fa->fsx_extsize = fattr->fa_extsize;
+               else if ((rw_user_mask & tmp_xflag) && fattr->fa_extsize)
+                       return -EINVAL;
+               else
+                       fa->fsx_extsize = 0;
+
+               tmp_xflag = FS_XFLAG_PROJID;
+               if (fa->fsx_xflags & tmp_xflag)
+                       fa->fsx_projid = fattr->fa_projid;
+               else if ((rw_user_mask & tmp_xflag) && fattr->fa_projid)
+                       return -EINVAL;
+               else
+                       fa->fsx_projid = 0;
+
+               tmp_xflag = FS_XFLAG_COWEXTSIZE;
+               if (fa->fsx_xflags & tmp_xflag)
+                       fa->fsx_cowextsize = fattr->fa_cowextsize;
+               else if ((rw_user_mask & tmp_xflag) && fattr->fa_cowextsize)
+                       return -EINVAL;
+               else
+                       fa->fsx_cowextsize = 0;
+       } else {
+               fileattr_fill_xflags(fa,
+                                    fattr->fa_xflags & ~FSX_RDONLY_MASK);
+               fa->fsx_extsize = fattr->fa_extsize;
+               fa->fsx_projid = fattr->fa_projid;
+               fa->fsx_cowextsize = fattr->fa_cowextsize;
+       }
 
        return 0;
 }
@@ -175,6 +253,178 @@ static int copy_fsxattr_from_user(struct file_kattr *fa,
        return 0;
 }
 
+/*
+ * Convert FSSETXATTR input format without masks, coming from userspace, to
+ * input format with masks, for filesystems that already support masks.
+ */
+static int fileattr_set_to_masks(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 &= FSX_FSXATTR_MASK & ~FSX_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->use_masks = true;
+
+       fa->fsx_valid = true;
+       fa->flags_valid = false;
+
+       return 0;
+}
+
+/*
+ * Convert input format with masks, coming from userspace, to FSSETXATTR input
+ * format without masks, for filesystems that don't support masks yet.
+ */
+static int fileattr_set_from_masks(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;
+
+       if (rw_user_mask & ~FSX_FSXATTR_SET_MASK)
+               return -EOPNOTSUPP;
+
+       /* Do RMW. */
+       new_xflags |= old_ma->fsx_xflags & ~rw_user_mask;
+
+       /* RMW filesystems only expect writable fsxattr xflags. */
+       new_xflags &= FSX_FSXATTR_SET_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->use_masks = false;
+
+       fa->fsx_valid = true;
+       fa->flags_valid = false;
+
+       return 0;
+}
+
+/*
+ * Handle converting between FSSETXATTR 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)
+{
+       bool fs_has_masks = old_ma->has_masks && !old_ma->get_only_masks;
+
+       if (fa->has_masks) {
+               if (!fs_has_masks)
+                       return fileattr_set_from_masks(fa, old_ma);
+
+               /* Both have masks, pass the request through. */
+               return 0;
+       }
+
+       if (fs_has_masks)
+               return fileattr_set_to_masks(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 & ~FSX_COMMON;
+               fa->fsx_xflags &= FSX_FSXATTR_MASK;
+               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 +435,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 : FSX_FILEATTR_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 +464,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 +510,8 @@ 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;
+       if (fa->fsx_projid == 0)
+               fa->fsx_xflags &= ~FS_XFLAG_PROJID;
 
        return 0;
 }
@@ -283,21 +545,24 @@ 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 semantics */
+       if (!fa->use_masks)
+               fa->has_masks = false;
+
+       if (fa->has_masks && !fa->fsx_xflags_rw_mask)
+               return -EINVAL;
+
        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 & ~FSX_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 +655,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 +665,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 +693,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 &= FSX_FSXATTR_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 +723,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 +733,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 +744,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 +763,15 @@ SYSCALL_DEFINE5(file_setattr, int, dfd, const char __user *, filename,
                        return error;
        }
 
+       if (at_flags & AT_FILEATTR_MASKS) {
+               fa.has_masks = true;
+               fa.use_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 +28 -1
index c26f70aaa3b8..4db19556981a 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 | \
@@ -31,11 +36,25 @@
 #define FSX_MISC_MASK \
        (FS_XFLAG_REALTIME | FS_XFLAG_NODEFRAG | FS_XFLAG_FILESTREAM)
 
-/* xflags supported by FS_IOC_FS[GS]ETXATTR and file_[gs]etattr() */
+/* xflags supported by FS_IOC_FS[GS]ETXATTR and VER0 file_[gs]etattr() */
 #define FSX_FSXATTR_MASK \
        (FSX_COMMON | FSX_RDONLY_MASK | FSX_VALUES_MASK | \
         FSX_DIRONLY_MASK | FSX_MISC_MASK)
 
+/*
+ * 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)
+
+/* All xflags supported by VER1+ file_[gs]etattr() with AT_FILEATTR_MASKS */
+#define FSX_FILEATTR_MASK \
+       (FSX_FSXATTR_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
@@ -46,6 +65,9 @@ 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 writable 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) */
@@ -53,6 +75,11 @@ struct file_kattr {
        /* selectors: */
        bool    flags_valid:1;
        bool    fsx_valid:1;
+       bool    has_masks:1;    /* struct has valid masks (get/set) */
+       bool    use_masks:1;    /* equivalent to AT_FILEATTR_MASKS but for
+                                  in-kernel calls (set) */
+       bool    get_only_masks:1;/* 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);
include/uapi/linux/fcntl.h +6 -0
index faa63d0b60d2..7347efb3df3c 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 +31 -6
index a13c7b32a1bd..337bb00b0e64 100644
@@ -225,9 +225,34 @@ 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 writable 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.
@@ -238,11 +263,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 writable 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 +294,8 @@ struct file_attr {
  */
 #define FS_XFLAG_CASEFOLD      0x00040000      /* case-insensitive lookups */
 #define FS_XFLAG_CASENONPRESERVING 0x00080000  /* case not preserved */
+#define FS_XFLAG_PROJID                0x00100000      /* project ID */
+
 #define FS_XFLAG_HASATTR       0x80000000      /* no DIFLAG for this   */
 
 /* the read-only stuff doesn't really belong here, but any other place is
--

2.56.0

author
#7 lore raw

PATCH v4 4/7 fs: advertise COMPR/ENCRYPT xflags in file_getattr()

In reply to koraynilay

Introduce FS_XFLAG_COMPR and FS_XFLAG_ENCRYPT and update fileattr_fill_*() helpers so file_getattr() and FS_IOC_FSGETXATTR advertise them too, in preparation for upgrading fs/ntfs to the masks API ([1] and newer versions).

Since FS_COMPR_FL is read-write only with FS_IOC_SETFLAGS for some filesystems, put it in FSX_RDONLY_MASK but not in FSX_COMMON, following the FS_XFLAG_CASEFOLD example.

Since FS_ENCRYPT_FL is read-only with both FS_IOC_SETFLAGS and FS_IOC_FSSETXATTR/file_setattr(), put it in both FSX_RDONLY_MASK and FSX_COMMON, following the FS_XFLAG_VERITY example.

---
 fs/file_attr.c           |  8 ++++++++
 include/linux/fileattr.h | 18 +++++++++++++-----
 include/uapi/linux/fs.h  |  2 ++
 3 files changed, 23 insertions(+), 5 deletions(-)
fs/file_attr.c +8 -0
index 3b3e84226f71..5e8979e2bb44 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 +13 -5
index 4db19556981a..898511c04f3f 100644
@@ -8,21 +8,29 @@
 #define FILE_ATTR_SIZE_VER1 40
 #define FILE_ATTR_SIZE_LATEST FILE_ATTR_SIZE_VER1
 
-/* Flags shared betwen flags/xflags */
+/*
+ * FS_COMMON_FL/FSX_COMMON are preserved through an RMW cycle via either UAPI
+ * flavor. They must NOT include flags that are read-write via one API flavor
+ * and read-only in another (e.g. CASEFOLD, COMPR).
+ */
 #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 FSX_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 */
+/*
+ * Read-only xflags via FS_IOC_FS[GS]ETXATTR and file_[gs]etattr().
+ * The respective flags may still be read-write via FS_IOC_SETFLAGS.
+ */
 #define FSX_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 FSX_VALUES_MASK \
include/uapi/linux/fs.h +2 -0
index 337bb00b0e64..54d970dd30dc 100644
@@ -295,6 +295,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
maintainer Reviewed-by on v4 4/7
#8 lore raw

Re: [PATCH v4 4/7] fs: advertise COMPR/ENCRYPT xflags in file_getattr()

Quoted 53 lines · koraynilay

Introduce FS_XFLAG_COMPR and FS_XFLAG_ENCRYPT and update fileattr_fill_*() helpers so file_getattr() and FS_IOC_FSGETXATTR advertise them too, in preparation for upgrading fs/ntfs to the masks API ([1] and newer versions).

Since FS_COMPR_FL is read-write only with FS_IOC_SETFLAGS for some filesystems, put it in FSX_RDONLY_MASK but not in FSX_COMMON, following the FS_XFLAG_CASEFOLD example.

Since FS_ENCRYPT_FL is read-only with both FS_IOC_SETFLAGS and FS_IOC_FSSETXATTR/file_setattr(), put it in both FSX_RDONLY_MASK and FSX_COMMON, following the FS_XFLAG_VERITY example.

---
 fs/file_attr.c           |  8 ++++++++
 include/linux/fileattr.h | 18 +++++++++++++-----
 include/uapi/linux/fs.h  |  2 ++
 3 files changed, 23 insertions(+), 5 deletions(-)
fs/file_attr.c +8 -0
index 3b3e84226f71..5e8979e2bb44 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 +0 -0
index 4db19556981a..898511c04f3f 100644

include/linux/fileattr.h line 15

Quoted 9 lines
include/linux/fileattr.h +5 -1
@@ -8,21 +8,29 @@
 #define FILE_ATTR_SIZE_VER1 40
 #define FILE_ATTR_SIZE_LATEST FILE_ATTR_SIZE_VER1
 
-/* Flags shared betwen flags/xflags */
+/*
+ * FS_COMMON_FL/FSX_COMMON are preserved through an RMW cycle via either UAPI
+ * flavor. They must NOT include flags that are read-write via one API flavor
+ * and read-only in another (e.g. CASEFOLD, COMPR).
+ */

This comment ...

Quoted 7 lines
include/linux/fileattr.h +1 -1
 #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 FSX_COMMON \

include/linux/fileattr.h line 29

Quoted 10 lines
include/linux/fileattr.h +5 -2
        (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 */
+/*
+ * Read-only xflags via FS_IOC_FS[GS]ETXATTR and file_[gs]etattr().
+ * The respective flags may still be read-write via FS_IOC_SETFLAGS.
+ */

... and this one are very nice explanatory comments

THIS patch does not change their meaning it only follows it for adding the respective flags.

Would you consider adding those comments in patch 2 which changes the macro names and adds an explanatory comment to FS_XFLAGS_MASK?

In either case, feel free to add

Thanks,
Amir.

author
#9 lore raw

Re: [PATCH v4 4/7] fs: advertise COMPR/ENCRYPT xflags in file_getattr()

Quoted 86 lines · Amir Goldstein
Quoted 53 lines · koraynilay

Introduce FS_XFLAG_COMPR and FS_XFLAG_ENCRYPT and update fileattr_fill_*() helpers so file_getattr() and FS_IOC_FSGETXATTR advertise them too, in preparation for upgrading fs/ntfs to the masks API ([1] and newer versions).

Since FS_COMPR_FL is read-write only with FS_IOC_SETFLAGS for some filesystems, put it in FSX_RDONLY_MASK but not in FSX_COMMON, following the FS_XFLAG_CASEFOLD example.

Since FS_ENCRYPT_FL is read-only with both FS_IOC_SETFLAGS and FS_IOC_FSSETXATTR/file_setattr(), put it in both FSX_RDONLY_MASK and FSX_COMMON, following the FS_XFLAG_VERITY example.

---
 fs/file_attr.c           |  8 ++++++++
 include/linux/fileattr.h | 18 +++++++++++++-----
 include/uapi/linux/fs.h  |  2 ++
 3 files changed, 23 insertions(+), 5 deletions(-)
fs/file_attr.c +8 -0
index 3b3e84226f71..5e8979e2bb44 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 +0 -0
index 4db19556981a..898511c04f3f 100644

include/linux/fileattr.h line 15

Quoted 9 lines
include/linux/fileattr.h +5 -1
@@ -8,21 +8,29 @@
 #define FILE_ATTR_SIZE_VER1 40
 #define FILE_ATTR_SIZE_LATEST FILE_ATTR_SIZE_VER1
 
-/* Flags shared betwen flags/xflags */
+/*
+ * FS_COMMON_FL/FSX_COMMON are preserved through an RMW cycle via either UAPI
+ * flavor. They must NOT include flags that are read-write via one API flavor
+ * and read-only in another (e.g. CASEFOLD, COMPR).
+ */

This comment ...

Quoted 7 lines
include/linux/fileattr.h +1 -1
 #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 FSX_COMMON \

include/linux/fileattr.h line 29

Quoted 10 lines
include/linux/fileattr.h +5 -2
        (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 */
+/*
+ * Read-only xflags via FS_IOC_FS[GS]ETXATTR and file_[gs]etattr().
+ * The respective flags may still be read-write via FS_IOC_SETFLAGS.
+ */

... and this one are very nice explanatory comments

Quoted 2 lines

THIS patch does not change their meaning it only follows it for adding the respective flags.

Yes, that makes sense.

Quoted 3 lines

Would you consider adding those comments in patch 2 which changes the macro names and adds an explanatory comment to FS_XFLAGS_MASK?

Changed:
https://github.com/koraynilay/linux/commit/a39a0f05594c501324a2b5c1bfae4037ec56f479 (2/7)
https://github.com/koraynilay/linux/commit/f11560e0e73663d0b8c01a188ae6242f48a8711b (4/7)

Quoted 3 lines

In either case, feel free to add

Thanks,
koraynilay

Quoted 2 lines

Thanks,
Amir.

author
#10 lore raw

PATCH v4 5/7 ntfs: switch from flags to xflags in ntfs_fileattr_get/set()

In reply to koraynilay

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
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;
 }
 
@@ -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;
 	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;
 
 	/* chattr passes the unchanged read-only flags back to us too. */
 	if (NInoCompressed(ni) || NInoWofCompressed(ni))
-		readonly |= FS_COMPR_FL;
+		readonly |= FS_XFLAG_COMPR;
 	if (NInoEncrypted(ni))
-		readonly |= FS_ENCRYPT_FL;
-	if ((fa->flags ^ readonly) & (FS_COMPR_FL | FS_ENCRYPT_FL))
+		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_CASEFOLD_FL;
-	if (fa->flags & ~allowed)
+		allowed |= FS_XFLAG_CASEFOLD;
+	if (fa->fsx_xflags & ~allowed)
 		return -EOPNOTSUPP;
 
 	forced_immutable = ntfs_is_forced_immutable(vi);
-	if (!(fa->flags & FS_IMMUTABLE_FL) && forced_immutable)
+	if (!(fa->fsx_xflags & FS_XFLAG_IMMUTABLE) && forced_immutable)
 		return -EPERM;
 
-	if (fa->flags & FS_IMMUTABLE_FL) {
+	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;
 	}
-	if (fa->flags & FS_APPEND_FL) {
+	if (fa->fsx_xflags & FS_XFLAG_APPEND) {
 		new_fl |= S_APPEND;
 		linuxflags |= NTFS_LINUXFLAGS_APPEND;
 	}
-	if (fa->flags & FS_NODUMP_FL)
+	if (fa->fsx_xflags & FS_XFLAG_NODUMP)
 		linuxflags |= NTFS_LINUXFLAGS_NODUMP;
-	if (fa->flags & FS_NOATIME_FL) {
+	if (fa->fsx_xflags & FS_XFLAG_NOATIME) {
 		new_fl |= S_NOATIME;
 		linuxflags |= NTFS_LINUXFLAGS_NOATIME;
 	}
Signature · 1 line
2.56.0
maintainer
#11 lore raw

Re: [PATCH v4 5/7] ntfs: switch from flags to xflags in ntfs_fileattr_get/set()

Quoted 62 lines · koraynilay

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 +11 -11
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;
 }
 
@@ -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;
        u32 readonly = 0;
        u32 linuxflags = ni->linuxflags & ~NTFS_LINUXFLAGS_MASK;
        unsigned int new_fl = 0;

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

Quoted 6 lines
fs/ntfs/file.c +1 -1
@@ -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 coding is ugly see my comment in patch 6 about fileattr_has_unsupported_fsx_flags().

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

Quoted 11 lines
fs/ntfs/file.c +3 -3
        /* chattr passes the unchanged read-only flags back to us too. */
        if (NInoCompressed(ni) || NInoWofCompressed(ni))
-               readonly |= FS_COMPR_FL;
+               readonly |= FS_XFLAG_COMPR;
        if (NInoEncrypted(ni))
-               readonly |= FS_ENCRYPT_FL;
-       if ((fa->flags ^ readonly) & (FS_COMPR_FL | FS_ENCRYPT_FL))
+               readonly |= FS_XFLAG_ENCRYPT;
+       if ((fa->fsx_xflags ^ readonly) & (FS_XFLAG_COMPR | FS_XFLAG_ENCRYPT))
                return -EOPNOTSUPP;

There is a mid series regression here, which is fixed by patch 6:

Setting xflags for compressed files fails with EOPNOTSUPP because vfs clear COMPR and ntfs does not allow clearing COMPR on compressed files.

There is already a regression in patch 4 for encrypted files. vfs clears ENCRYPT and ntfs does not allow this.

AKAICS, ntfs is the only filesystem that enforces those restrictions so it is the only filesystem regressed by patch 4.

The fix is to include in patch 4 the fix to ntfs:

         /* chattr passes the unchanged read-only flags back to us too. */
        if (fa->flags_valid &&
            ((fa->flags ^ readonly) & (FS_COMPR_FL | FS_ENCRYPT_FL)))
                return -EOPNOTSUPP;

The if (fa->flags_valid && stays in patch 5 and goes away in patch 6.

Thanks,
Amir.

author
#12 lore raw

Re: [PATCH v4 5/7] ntfs: switch from flags to xflags in ntfs_fileattr_get/set()

Quoted 66 lines · Amir Goldstein
Quoted 65 lines · koraynilay

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 +11 -11
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;
 }
 
@@ -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;
        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;
 
Quoted 6 lines

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

Quoted 3 lines
fs/ntfs/file.c +1 -1
-       if (fileattr_has_fsx(fa))
+       if (fa->fsx_extsize || fa->fsx_cowextsize || fa->fsx_projid)
                return -EOPNOTSUPP;

This open coding is ugly see my comment in patch 6 about fileattr_has_unsupported_fsx_flags().

Right, so this was the reason to introduce the helper and then change it in the patch after, I forgot and the robot didn't notice it :(

Quoted 24 lines

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

Quoted 11 lines
fs/ntfs/file.c +3 -3
        /* chattr passes the unchanged read-only flags back to us too. */
        if (NInoCompressed(ni) || NInoWofCompressed(ni))
-               readonly |= FS_COMPR_FL;
+               readonly |= FS_XFLAG_COMPR;
        if (NInoEncrypted(ni))
-               readonly |= FS_ENCRYPT_FL;
-       if ((fa->flags ^ readonly) & (FS_COMPR_FL | FS_ENCRYPT_FL))
+               readonly |= FS_XFLAG_ENCRYPT;
+       if ((fa->fsx_xflags ^ readonly) & (FS_XFLAG_COMPR | FS_XFLAG_ENCRYPT))
                return -EOPNOTSUPP;

There is a mid series regression here, which is fixed by patch 6:

Setting xflags for compressed files fails with EOPNOTSUPP because vfs clear COMPR and ntfs does not allow clearing COMPR on compressed files.

There is already a regression in patch 4 for encrypted files. vfs clears ENCRYPT and ntfs does not allow this.

AKAICS, ntfs is the only filesystem that enforces those restrictions so it is the only filesystem regressed by patch 4.

Quoted 8 lines

The fix is to include in patch 4 the fix to ntfs:

         /* chattr passes the unchanged read-only flags back to us too. */
        if (fa->flags_valid &&
            ((fa->flags ^ readonly) & (FS_COMPR_FL | FS_ENCRYPT_FL)))
                return -EOPNOTSUPP;

The if (fa->flags_valid && stays in patch 5 and goes away in patch 6.

Ah, thank you, I'll investigate and include it in v5.

Thanks,
koraynilay

Quoted 2 lines

Thanks,
Amir.

author
#13 lore raw

PATCH v4 6/7 ntfs: support the fileattr API masks in ntfs_fileattr_get/set()

In reply to koraynilay

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

To check for unsupported xflags, add fileattr_has_invalid_fsx_masks() helper and use it in place of the previous checks in ntfs_fileattr_set().

---
 fs/ntfs/file.c           | 72 +++++++++++++++++++++++-----------------
 include/linux/fileattr.h | 31 +++++++++++++++++
 2 files changed, 72 insertions(+), 31 deletions(-)
fs/ntfs/file.c +41 -31
index ca088e7d636b..44c025c7280a 100644
@@ -150,6 +150,11 @@ int ntfs_file_release(struct inode *vi, struct file *filp)
 	return 0;
 }
 
+#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)
+
 /*
  * ntfs_fileattr_get - inode_operations::fileattr_get
  * @dentry: dentry to report the flags of
@@ -180,6 +185,9 @@ int ntfs_fileattr_get(struct dentry *dentry, struct file_kattr *fa)
 		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 +215,67 @@ 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))
+	if (fileattr_has_invalid_fsx_masks(fa, NTFS_RW_XFLAGS, NTFS_RO_XFLAGS))
 		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;
include/linux/fileattr.h +31 -0
index 898511c04f3f..2f9b9fed1600 100644
@@ -95,6 +95,37 @@ int copy_fsxattr_to_user(const struct file_kattr *fa, struct fsxattr __user *ufa
 void fileattr_fill_xflags(struct file_kattr *fa, u32 xflags);
 void fileattr_fill_flags(struct file_kattr *fa, u32 flags);
 
+static inline bool fileattr_has_invalid_fsx_masks(const struct file_kattr *fa,
+						  const u32 allowed_rw,
+						  const u32 allowed_ro)
+{
+	const u32 allowed_all = allowed_rw | allowed_ro;
+
+	if (WARN_ON_ONCE(!fa->fsx_valid || !fa->has_masks))
+		return true;
+
+	if (WARN_ON_ONCE((fa->fsx_xflags & allowed_rw) & ~fa->fsx_xflags_rw_mask))
+		return true;
+
+	if (fa->fsx_xflags_rw_mask & ~allowed_rw)
+		return true;
+
+	if (fa->fsx_xflags & ~allowed_all)
+		return true;
+
+	if (!(allowed_all & (FS_XFLAG_EXTSIZE | FS_XFLAG_EXTSZINHERIT)) &&
+	    fa->fsx_extsize)
+		return true;
+
+	if (!(allowed_all & FS_XFLAG_PROJID) && fa->fsx_projid)
+		return true;
+
+	if (!(allowed_all & FS_XFLAG_COWEXTSIZE) && fa->fsx_cowextsize)
+		return true;
+
+	return false;
+}
+
 /**
  * fileattr_has_fsx - check for extended flags/attributes
  * @fa:		fileattr pointer
Signature · 1 line
2.56.0
maintainer
#14 lore raw

Re: [PATCH v4 6/7] ntfs: support the fileattr API masks in ntfs_fileattr_get/set()

Quoted 18 lines · koraynilay

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

To check for unsupported xflags, add fileattr_has_invalid_fsx_masks() helper and use it in place of the previous checks in ntfs_fileattr_set().

---
 fs/ntfs/file.c           | 72 +++++++++++++++++++++++-----------------
 include/linux/fileattr.h | 31 +++++++++++++++++
 2 files changed, 72 insertions(+), 31 deletions(-)
fs/ntfs/file.c +0 -0
index ca088e7d636b..44c025c7280a 100644

fs/ntfs/file.c int ntfs_file_release(struct inode *vi, struct file *filp) · line 156

Quoted 7 lines
fs/ntfs/file.c +4 -0
@@ -150,6 +150,11 @@ int ntfs_file_release(struct inode *vi, struct file *filp)
        return 0;
 }
 
+#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)

I think you may have misunderstood or missed my comments about introducing those in the prep patch, or maybe I missed your answer, so I will repeat it here. Include the above and below in the prep patch...

#define NTFS_XFLAGS (NTFS_RO_XFLAGS | NTFS_RW_XFLAGS)

Quoted 35 lines
fs/ntfs/file.c +9 -6
+
 /*
  * ntfs_fileattr_get - inode_operations::fileattr_get
  * @dentry: dentry to report the flags of
@@ -180,6 +185,9 @@ int ntfs_fileattr_get(struct dentry *dentry, struct file_kattr *fa)
                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 +215,67 @@ 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;
 

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

Quoted 10 lines
fs/ntfs/file.c +1 -9
-       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))
+       if (fileattr_has_invalid_fsx_masks(fa, NTFS_RW_XFLAGS, NTFS_RO_XFLAGS))

If has INVAL flag return EOPNOPSUPP? do you read how weird that sounds? Why not has_unsupported_?

Also if you use the fileattr_has_unsupported_fsx_flags() (see below) in prep patch,
this patch becomes:

- if (fileattr_has_unsupported_fsx_flags(fa, NTFS_XFLAGS)
+ if (fileattr_has_unsupported_fsx_masks(fa, NTFS_RW_XFLAGS,
NTFS_RO_XFLAGS))

Do you see what I mean?

Quoted 73 lines
fs/ntfs/file.c +27 -16
                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;
include/linux/fileattr.h +0 -0
index 898511c04f3f..2f9b9fed1600 100644

include/linux/fileattr.h int copy_fsxattr_to_user(const struct file_kattr *fa, struct fsxattr __user *ufa · line 97

Quoted 3 lines
include/linux/fileattr.h +0 -0
@@ -95,6 +95,37 @@ int copy_fsxattr_to_user(const struct file_kattr *fa, struct fsxattr __user *ufa
 void fileattr_fill_xflags(struct file_kattr *fa, u32 xflags);
 void fileattr_fill_flags(struct file_kattr *fa, u32 flags);
 

This would be the helper introduced in prep patch:
(document that it is used from set methods of fs that support setting fsx flags but do not support masks)

static inline bool fileattr_has_unsupported_fsx_flags(const struct file_kattr *fa,
u32 supported)

{
        if (fa->fsx_xflags & ~supported)
                return true;
        if (fa->fsx_extsize &&
            !(supported & (FS_XFLAG_EXTSIZE | FS_XFLAG_EXTSZINHERIT)))
                return true;
        if (fa->fsx_projid && !(supported & FS_XFLAG_PROJID))
                return true;
        if (fa->fsx_cowextsize && !(supported & FS_XFLAG_COWEXTSIZE))
                return true;
        return false;
}

include/linux/fileattr.h int copy_fsxattr_to_user(const struct file_kattr *fa, struct fsxattr __user *ufa · line 100

Quoted 3 lines
include/linux/fileattr.h +3 -0
+static inline bool fileattr_has_invalid_fsx_masks(const struct file_kattr *fa,
+                                                 const u32 allowed_rw,
+                                                 const u32 allowed_ro)

This should be fileattr_has_unsupported_fsx_masks() and document it would typically be used as a gate in filesystem set methods which support the masks in set.

include/linux/fileattr.h int copy_fsxattr_to_user(const struct file_kattr *fa, struct fsxattr __user *ufa · line 112

Quoted 12 lines
include/linux/fileattr.h +12 -0
+{
+       const u32 allowed_all = allowed_rw | allowed_ro;
+
+       if (WARN_ON_ONCE(!fa->fsx_valid || !fa->has_masks))
+               return true;
+
+       if (WARN_ON_ONCE((fa->fsx_xflags & allowed_rw) & ~fa->fsx_xflags_rw_mask))
+               return true;
+
+       if (fa->fsx_xflags_rw_mask & ~allowed_rw)
+               return true;
+
        return fileattr_has_unsupported_fsx_flags(fa, allowed_rw | allowed_ro);
}

Do you see what I mean?

Thanks,
Amir.

author
#15 lore raw

Re: [PATCH v4 6/7] ntfs: support the fileattr API masks in ntfs_fileattr_get/set()

Quoted 22 lines · Amir Goldstein
Quoted 21 lines · koraynilay

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

To check for unsupported xflags, add fileattr_has_invalid_fsx_masks() helper and use it in place of the previous checks in ntfs_fileattr_set().

---
 fs/ntfs/file.c           | 72 +++++++++++++++++++++++-----------------
 include/linux/fileattr.h | 31 +++++++++++++++++
 2 files changed, 72 insertions(+), 31 deletions(-)
fs/ntfs/file.c +0 -0
index ca088e7d636b..44c025c7280a 100644
@@ -150,6 +150,11 @@ int ntfs_file_release(struct inode *vi, struct file *filp)
        return 0;
 }
 
Quoted 8 lines

fs/ntfs/file.c int ntfs_file_release(struct inode *vi, struct file *filp) · line 156

Quoted 4 lines
fs/ntfs/file.c +4 -0
+#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)

I think you may have misunderstood or missed my comments about introducing those in the prep patch, or maybe I missed your answer, so I will repeat it here. Include the above and below in the prep patch...

Ah, yeah, completely forgot about that.

Quoted 37 lines

#define NTFS_XFLAGS (NTFS_RO_XFLAGS | NTFS_RW_XFLAGS)

Quoted 35 lines
fs/ntfs/file.c +9 -6
+
 /*
  * ntfs_fileattr_get - inode_operations::fileattr_get
  * @dentry: dentry to report the flags of
@@ -180,6 +185,9 @@ int ntfs_fileattr_get(struct dentry *dentry, struct file_kattr *fa)
                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 +215,67 @@ 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;
 
Quoted 12 lines

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

Quoted 10 lines
fs/ntfs/file.c +1 -9
-       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))
+       if (fileattr_has_invalid_fsx_masks(fa, NTFS_RW_XFLAGS, NTFS_RO_XFLAGS))

If has INVAL flag return EOPNOPSUPP? do you read how weird that sounds?

Personally I find it fine lmao, but I can see what you mean.

Quoted 1 line

Why not has_unsupported_?

I did it with has_unsupported_ originally, but fileattr_has_unsupported_fsx_masks() is a bit too long and I'd have had to put the ntfs arguments in a new line and I didn't like that for a new function introduced by us lol.

if (fileattr_has_unsupported_fsx_masks(fa, NTFS_RW_XFLAGS | NTFS_RO_XFLAGS)

is more than 80 chars and

if (fileattr_has_unsupported_fsx_masks(fa,
NTFS_RW_XFLAGS | NTFS_RO_XFLAGS)

is pretty ugly IMO.

(this could also be done, but eh, still not the best:

if (fileattr_has_unsupported_fsx_masks(fa,
NTFS_RW_XFLAGS | NTFS_RO_XFLAGS)

)

Quoted 115 lines

Also if you use the fileattr_has_unsupported_fsx_flags() (see below) in prep patch,
this patch becomes:

(quoted diff) +1 -1
-        if (fileattr_has_unsupported_fsx_flags(fa, NTFS_XFLAGS)
+       if (fileattr_has_unsupported_fsx_masks(fa, NTFS_RW_XFLAGS,

NTFS_RO_XFLAGS))

Do you see what I mean?

Quoted 73 lines
fs/ntfs/file.c +27 -16
                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;
include/linux/fileattr.h +0 -0
index 898511c04f3f..2f9b9fed1600 100644

include/linux/fileattr.h int copy_fsxattr_to_user(const struct file_kattr *fa, struct fsxattr __user *ufa · line 97

Quoted 3 lines
include/linux/fileattr.h +0 -0
@@ -95,6 +95,37 @@ int copy_fsxattr_to_user(const struct file_kattr *fa, struct fsxattr __user *ufa
 void fileattr_fill_xflags(struct file_kattr *fa, u32 xflags);
 void fileattr_fill_flags(struct file_kattr *fa, u32 flags);
 

This would be the helper introduced in prep patch:
(document that it is used from set methods of fs that support setting fsx flags but do not support masks)

static inline bool fileattr_has_unsupported_fsx_flags(const struct file_kattr *fa,
u32 supported)

{
        if (fa->fsx_xflags & ~supported)
                return true;
        if (fa->fsx_extsize &&
            !(supported & (FS_XFLAG_EXTSIZE | FS_XFLAG_EXTSZINHERIT)))
                return true;
        if (fa->fsx_projid && !(supported & FS_XFLAG_PROJID))
                return true;
        if (fa->fsx_cowextsize && !(supported & FS_XFLAG_COWEXTSIZE))
                return true;
        return false;
}

include/linux/fileattr.h int copy_fsxattr_to_user(const struct file_kattr *fa, struct fsxattr __user *ufa · line 100

Quoted 3 lines
include/linux/fileattr.h +3 -0
+static inline bool fileattr_has_invalid_fsx_masks(const struct file_kattr *fa,
+                                                 const u32 allowed_rw,
+                                                 const u32 allowed_ro)

This should be fileattr_has_unsupported_fsx_masks() and document it would typically be used as a gate in filesystem set methods which support the masks in set.

Quoted 14 lines

include/linux/fileattr.h int copy_fsxattr_to_user(const struct file_kattr *fa, struct fsxattr __user *ufa · line 112

Quoted 12 lines
include/linux/fileattr.h +12 -0
+{
+       const u32 allowed_all = allowed_rw | allowed_ro;
+
+       if (WARN_ON_ONCE(!fa->fsx_valid || !fa->has_masks))
+               return true;
+
+       if (WARN_ON_ONCE((fa->fsx_xflags & allowed_rw) & ~fa->fsx_xflags_rw_mask))
+               return true;
+
+       if (fa->fsx_xflags_rw_mask & ~allowed_rw)
+               return true;
+
        return fileattr_has_unsupported_fsx_flags(fa, allowed_rw | allowed_ro);

Oh, right, I didn't think of having both helpers, that is indeed cleaner.

Quoted 3 lines

}

Do you see what I mean?

Yes (comment on Patch 5 for the reason).

Thanks,
koraynilay

Quoted 2 lines

Thanks,
Amir.

maintainer
#16 lore raw

Re: [PATCH v4 6/7] ntfs: support the fileattr API masks in ntfs_fileattr_get/set()

Quoted 85 lines · koraynilay
Quoted 22 lines · Amir Goldstein
Quoted 21 lines · koraynilay

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

To check for unsupported xflags, add fileattr_has_invalid_fsx_masks() helper and use it in place of the previous checks in ntfs_fileattr_set().

---
 fs/ntfs/file.c           | 72 +++++++++++++++++++++++-----------------
 include/linux/fileattr.h | 31 +++++++++++++++++
 2 files changed, 72 insertions(+), 31 deletions(-)
fs/ntfs/file.c +0 -0
index ca088e7d636b..44c025c7280a 100644
@@ -150,6 +150,11 @@ int ntfs_file_release(struct inode *vi, struct file *filp)
        return 0;
 }
 
Quoted 8 lines

fs/ntfs/file.c int ntfs_file_release(struct inode *vi, struct file *filp) · line 156

Quoted 4 lines
fs/ntfs/file.c +4 -0
+#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)

I think you may have misunderstood or missed my comments about introducing those in the prep patch, or maybe I missed your answer, so I will repeat it here. Include the above and below in the prep patch...

Ah, yeah, completely forgot about that.

Quoted 37 lines

#define NTFS_XFLAGS (NTFS_RO_XFLAGS | NTFS_RW_XFLAGS)

Quoted 35 lines
fs/ntfs/file.c +9 -6
+
 /*
  * ntfs_fileattr_get - inode_operations::fileattr_get
  * @dentry: dentry to report the flags of
@@ -180,6 +185,9 @@ int ntfs_fileattr_get(struct dentry *dentry, struct file_kattr *fa)
                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 +215,67 @@ 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;
 
Quoted 12 lines

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

Quoted 10 lines
fs/ntfs/file.c +1 -9
-       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))
+       if (fileattr_has_invalid_fsx_masks(fa, NTFS_RW_XFLAGS, NTFS_RO_XFLAGS))

If has INVAL flag return EOPNOPSUPP? do you read how weird that sounds?

Quoted 6 lines

Personally I find it fine lmao, but I can see what you mean.

Quoted 1 line

Why not has_unsupported_?

I did it with has_unsupported_ originally, but fileattr_has_unsupported_fsx_masks() is a bit too long and I'd have had

Yes it's a mouthful. If you prefer shorter something like:

if (!fileattr_check_fsx_{flags,masks}

Is also fine IMO.

Thanks,
Amir.

author
#17 lore raw

Re: [PATCH v4 6/7] ntfs: support the fileattr API masks in ntfs_fileattr_get/set()

Quoted 90 lines · Amir Goldstein
Quoted 89 lines · koraynilay
Quoted 22 lines · Amir Goldstein
Quoted 21 lines · koraynilay

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

To check for unsupported xflags, add fileattr_has_invalid_fsx_masks() helper and use it in place of the previous checks in ntfs_fileattr_set().

---
 fs/ntfs/file.c           | 72 +++++++++++++++++++++++-----------------
 include/linux/fileattr.h | 31 +++++++++++++++++
 2 files changed, 72 insertions(+), 31 deletions(-)
fs/ntfs/file.c +0 -0
index ca088e7d636b..44c025c7280a 100644
@@ -150,6 +150,11 @@ int ntfs_file_release(struct inode *vi, struct file *filp)
        return 0;
 }
 
Quoted 8 lines

fs/ntfs/file.c int ntfs_file_release(struct inode *vi, struct file *filp) · line 156

Quoted 4 lines
fs/ntfs/file.c +4 -0
+#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)

I think you may have misunderstood or missed my comments about introducing those in the prep patch, or maybe I missed your answer, so I will repeat it here. Include the above and below in the prep patch...

Ah, yeah, completely forgot about that.

Quoted 37 lines

#define NTFS_XFLAGS (NTFS_RO_XFLAGS | NTFS_RW_XFLAGS)

Quoted 35 lines
fs/ntfs/file.c +9 -6
+
 /*
  * ntfs_fileattr_get - inode_operations::fileattr_get
  * @dentry: dentry to report the flags of
@@ -180,6 +185,9 @@ int ntfs_fileattr_get(struct dentry *dentry, struct file_kattr *fa)
                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 +215,67 @@ 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;
 
Quoted 12 lines

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

Quoted 10 lines
fs/ntfs/file.c +1 -9
-       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))
+       if (fileattr_has_invalid_fsx_masks(fa, NTFS_RW_XFLAGS, NTFS_RO_XFLAGS))

If has INVAL flag return EOPNOPSUPP? do you read how weird that sounds?

Personally I find it fine lmao, but I can see what you mean.

Quoted 1 line

Why not has_unsupported_?

Quoted 8 lines
Quoted 2 lines

I did it with has_unsupported_ originally, but fileattr_has_unsupported_fsx_masks() is a bit too long and I'd have had

Yes it's a mouthful. If you prefer shorter something like:

if (!fileattr_check_fsx_{flags,masks}

Is also fine IMO.

Yeah, that is better, I'll see if there is anything better than "check", but I like the format.

Thanks,
koraynilay

Quoted 2 lines

Thanks,
Amir.

author
#18 lore raw

PATCH v4 7/7 docs: add fileattr API documentation

In reply to koraynilay

Add documentation for the fileattr API.

---
 Documentation/filesystems/fileattr.rst | 469 +++++++++++++++++++++++++
 Documentation/filesystems/index.rst    |   1 +
 Documentation/filesystems/vfs.rst      |  19 +-
 3 files changed, 481 insertions(+), 8 deletions(-)
 create mode 100644 Documentation/filesystems/fileattr.rst
Documentation/filesystems/fileattr.rst new file +469 -0
new file mode 100644
index 000000000000..ee47ae2c9447
@@ -0,0 +1,469 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+============
+Fileattr API
+============
+
+Overview
+========
+
+The fileattr API provides a modern way to query and modify file attributes and
+it should be used instead of the older ``FS_IOC_[GS]ETFLAGS``
+(:manpage:`fs_ioc_getflags(2const)` / :manpage:`fs_ioc_setflags(2const)`) and
+``FS_IOC_FS[GS]ETXATTR`` (:manpage:`ioctl_xfs_fsgetxattr(2)` /
+:manpage:`ioctl_xfs_fssetxattr(2)`) ioctls.
+
+The "file attributes" that this document refers to are an overlap of the ones
+reported by :manpage:`statx(2)` in its ``stx_attributes`` field (e.g. immutable
+or append-only). Not to be confused with "Extended attributes", which are
+the ones managed by :manpage:`getxattr(2)` and :manpage:`setxattr(2)`.
+
+.. note::
+    For usage by userspace, see :manpage:`file_getattr(2)`,
+    :manpage:`file_setattr(2)` and :manpage:`file_attr(2type)`.
+
+Userspace API Summary
+=====================
+
+struct file_attr is versioned by size:
+
+- ``VER0``: The base structure, equivalent to struct fsxattr.
+- ``VER1``: Adds the ``fa_xflags_rw_mask`` and ``fa_xflags_ro_mask`` fields,
+  see :ref:`Masks <masks>` for more details.
+
+.. kernel-doc:: include/uapi/linux/fs.h
+   :identifiers: file_attr
+
+.. _fileattr_flags:
+
+Flag Reference
+==============
+
+This is the full list of currently implemented xflags.
+
+.. note::
+   In the Mode column, "RO/RW" means that the flag is read-only via the xflags
+   APIs (``FS_IOC_FSSETXATTR`` and file_setattr()), while some filesystems
+   allow modifying its ``FS_*_FL`` counterpart via ``FS_IOC_SETFLAGS``.
+
+   "RW" means the flag is writable via file_setattr() (and via
+   ``FS_IOC_FSSETXATTR`` / ``FS_IOC_SETFLAGS`` where applicable), and "RO"
+   means it is read-only across all fileattr userspace APIs. Filesystem support
+   may still vary.
+
++--------------------------------+------------+--------+-------------------------------------------------------+
+| 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 (VER1).                         |
+|                                |            |        | 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.             |
++--------------------------------+------------+--------+-------------------------------------------------------+
+
+
+.. _masks:
+
+Masks
+=====
+
+In ``VER1``, the fileattr API introduced the ``fa_xflags_rw_mask`` and
+``fa_xflags_ro_mask`` fields, to enable its usage without needing 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 file_getattr() / file_setattr(), otherwise the normal behaviour is kept.
+
+.. note::
+   For filesystems that don't support masks (and for userspace without
+   ``AT_FILEATTR_MASKS``), conversion between the two formats on set is handled
+   by the VFS as described :ref:`below <conversion>`.
+
+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 (e.g. in backup/sync workloads)::
+
+       file_getattr(file1, &fa, ..., AT_FILEATTR_MASKS);
+       file_setattr(file2, &fa, ..., AT_FILEATTR_MASKS);
+
+   is a change of behaviour compared to the old RMW APIs, where unsupported
+   flags were often silently dropped:
+
+   - on filesystems without mask support, file_getattr() returns
+     ``fa_xflags_rw_mask = 0``, causing file_setattr() to fail with
+     ``-EINVAL``;
+   - on filesystems with mask support, file_getattr() returns all supported
+     writable xflags in ``fa_xflags_rw_mask``, so file_setattr() tries to set
+     all of them on ``file2`` and fails with ``-EOPNOTSUPP`` if ``file2``
+     doesn't support all of them, instead of silently dropping them.
+
+   With masks, the program can inspect ``file2``'s masks first and decide how
+   to handle unsupported flags.
+
+On file_setattr()
+-----------------
+
+- Userspace populates ``fa_xflags_rw_mask`` with the xflags it wishes to
+  change. If ``fa_xflags_rw_mask`` is ``0``, the request fails with
+  ``-EINVAL``.
+- 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 xflag bit in ``fa_xflags_rw_mask`` and clearing the bit in
+    ``fa_xflags`` disables the feature.
+
+- For value attributes (like ``FS_XFLAG_PROJID`` and ``FS_XFLAG_COWEXTSIZE``):
+
+  - Setting the flag bit in both ``fa_xflags_rw_mask`` and ``fa_xflags`` sets
+    the attribute, taking the value from the corresponding field in the struct.
+  - Setting the flag bit in ``fa_xflags_rw_mask``, clearing the bit in
+    ``fa_xflags`` and setting the field value to ``0`` unsets/clears the
+    attribute. If instead the field value is left non-zero, the request fails
+    with ``-EINVAL``.
+
+- Validation:
+
+  - 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 modification of any
+    flag outside ``FSX_FSXATTR_SET_MASK`` fails with ``-EOPNOTSUPP``.
+
+Usage Examples
+--------------
+
+See :manpage:`file_setattr(2)` for examples.
+
+Behaviour without ``AT_FILEATTR_MASKS``
+=======================================
+
+When not using ``AT_FILEATTR_MASKS``, file_getattr() and file_setattr() behave
+in the same way as ``FS_IOC_FSGETXATTR`` and ``FS_IOC_FSSETXATTR``
+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;        /* Struct has valid masks */
+        bool use_masks:1;        /* Set request uses masks (AT_FILEATTR_MASKS) */
+        bool get_only_masks:1;   /* Transitional: fs supports ro_mask on get, RMW on set */
+    };
+
+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``
+    Indicates whether ``fsx_xflags_rw_mask`` and ``fsx_xflags_ro_mask`` contain
+    valid masks.
+
+``use_masks``
+    Indicates whether the set request uses mask semantics (the in-kernel
+    equivalent of ``AT_FILEATTR_MASKS``).
+
+``get_only_masks``
+    Transitional flag indicating that the filesystem supports ``fsx_xflags_ro_mask``
+    on get, but still expects unmasked RMW input in ``->fileattr_set()``.
+
+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);
+
+This way filesystems can handle flags internally either as ``FS_*_FL`` flags or
+``FS_XFLAG_*`` xflags and let VFS translate the common ones (defined in
+``FS_COMMON_FL`` / ``FSX_COMMON``).
+
+New and mask-aware drivers should use fileattr_fill_xflags() and set
+``fa->has_masks = true`` along with their supported masks, while
+``FS_*_FL``-only drivers use fileattr_fill_flags().
+
+Drivers that only support ``FS_*_FL`` flags can check for unsupported
+xflags in ``->fileattr_set()`` using fileattr_has_fsx(), while
+mask-aware drivers can validate against their allowed xflags masks with
+fileattr_has_invalid_fsx_masks()::
+
+    bool fileattr_has_fsx(const struct file_kattr *fa);
+
+    bool fileattr_has_invalid_fsx_masks(const struct file_kattr *fa,
+                                        const u32 allowed_rw,
+                                        const u32 allowed_ro);
+
+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``           |
++------------------------+--------------------------------+
+
+.. _conversion:
+
+VFS Conversion 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_to_masks(): Converts non-masked requests (``FS_IOC_SETFLAGS``,
+  ``FS_IOC_FSSETXATTR``, file_setattr() without ``AT_FILEATTR_MASKS``, or
+  in-kernel callers with ``use_masks = false``) to a masked request when
+  targeting a filesystem that accepts masks on set.
+- fileattr_set_from_masks(): Converts masked requests (file_setattr() with
+  ``AT_FILEATTR_MASKS``) to unmasked format when targeting a
+  filesystem that doesn't accept masks on set.
+- flags_to_xflags() / xflags_to_flags(): Convert between ``FS_XFLAG_*`` xflags
+  and ``FS_*_FL`` flags.
+
+Adding New Flags
+----------------
+
+When adding new xflags or value attributes:
+
+- New flags should target the ``VER1+`` syscall API (``FSX_FILEATTR_MASK``)
+  only, keeping ``FSX_FSXATTR_MASK`` and ``FSX_RDONLY_MASK`` frozen except
+  when exposing xflag counterparts for existing ``FS_*_FL`` flags.
+- Add a flag to ``FSX_COMMON`` / ``FS_COMMON_FL`` only if it has the same
+  read-only / read-write behaviour across both ``FS_IOC_SETFLAGS`` and the
+  xflags APIs, for example ``FS_XFLAG_CASEFOLD`` and ``FS_XFLAG_COMPR``, which
+  are writable via ``FS_IOC_SETFLAGS`` but read-only in xflags must not be
+  added to ``FSX_COMMON``.
+- Filesystems that only want to report new read-only flags on get without
+  converting ``->fileattr_set()`` to masks have to change their
+  ``->fileattr_get()`` so that it:
+
+  - sets ``has_masks = true``,
+  - sets ``get_only_masks = true``,
+  - sets the xflag bit(s) they want to report as read-only in
+    ``fsx_xflags_ro_mask``.
+
+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(), so no external locking is required.
+
+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,
+    ``AT_FILEATTR_MASKS`` passed to file_setattr() with ``fa_xflags_rw_mask = 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``, invalid inode type for the specified xflags,
+    incompatible attribute combinations (e.g. DAX on fs-verity), invalid
+    project ID or changing ``FS_XFLAG_PROJID`` / ``FS_XFLAG_PROJINHERIT``
+    outside the host namespace, specifying xflags added in or after ``VER1``
+    without ``AT_FILEATTR_MASKS``, or clearing a value attribute with a
+    non-zero field value.
+
+``-EOPNOTSUPP``
+    The filesystem does not implement ``fileattr`` operations for this inode,
+    or userspace requested modification of an attribute not writable by the
+    filesystem.
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 9ab182dbbd73..01fddb2dacf3 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
Signature · 1 line
2.56.0
maintainer
#19 lore raw

Re: [PATCH v4 7/7] docs: add fileattr API documentation

Quoted 264 lines · koraynilay

Add documentation for the fileattr API.

---
 Documentation/filesystems/fileattr.rst | 469 +++++++++++++++++++++++++
 Documentation/filesystems/index.rst    |   1 +
 Documentation/filesystems/vfs.rst      |  19 +-
 3 files changed, 481 insertions(+), 8 deletions(-)
 create mode 100644 Documentation/filesystems/fileattr.rst
Documentation/filesystems/fileattr.rst new file +247 -0
new file mode 100644
index 000000000000..ee47ae2c9447
@@ -0,0 +1,469 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+============
+Fileattr API
+============
+
+Overview
+========
+
+The fileattr API provides a modern way to query and modify file attributes and
+it should be used instead of the older ``FS_IOC_[GS]ETFLAGS``
+(:manpage:`fs_ioc_getflags(2const)` / :manpage:`fs_ioc_setflags(2const)`) and
+``FS_IOC_FS[GS]ETXATTR`` (:manpage:`ioctl_xfs_fsgetxattr(2)` /
+:manpage:`ioctl_xfs_fssetxattr(2)`) ioctls.
+
+The "file attributes" that this document refers to are an overlap of the ones
+reported by :manpage:`statx(2)` in its ``stx_attributes`` field (e.g. immutable
+or append-only). Not to be confused with "Extended attributes", which are
+the ones managed by :manpage:`getxattr(2)` and :manpage:`setxattr(2)`.
+
+.. note::
+    For usage by userspace, see :manpage:`file_getattr(2)`,
+    :manpage:`file_setattr(2)` and :manpage:`file_attr(2type)`.
+
+Userspace API Summary
+=====================
+
+struct file_attr is versioned by size:
+
+- ``VER0``: The base structure, equivalent to struct fsxattr.
+- ``VER1``: Adds the ``fa_xflags_rw_mask`` and ``fa_xflags_ro_mask`` fields,
+  see :ref:`Masks <masks>` for more details.
+
+.. kernel-doc:: include/uapi/linux/fs.h
+   :identifiers: file_attr
+
+.. _fileattr_flags:
+
+Flag Reference
+==============
+
+This is the full list of currently implemented xflags.
+
+.. note::
+   In the Mode column, "RO/RW" means that the flag is read-only via the xflags
+   APIs (``FS_IOC_FSSETXATTR`` and file_setattr()), while some filesystems
+   allow modifying its ``FS_*_FL`` counterpart via ``FS_IOC_SETFLAGS``.
+
+   "RW" means the flag is writable via file_setattr() (and via
+   ``FS_IOC_FSSETXATTR`` / ``FS_IOC_SETFLAGS`` where applicable), and "RO"
+   means it is read-only across all fileattr userspace APIs. Filesystem support
+   may still vary.
+
++--------------------------------+------------+--------+-------------------------------------------------------+
+| 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 (VER1).                         |
+|                                |            |        | 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.             |
++--------------------------------+------------+--------+-------------------------------------------------------+
+
+
+.. _masks:
+
+Masks
+=====
+
+In ``VER1``, the fileattr API introduced the ``fa_xflags_rw_mask`` and
+``fa_xflags_ro_mask`` fields, to enable its usage without needing 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 file_getattr() / file_setattr(), otherwise the normal behaviour is kept.
+
+.. note::
+   For filesystems that don't support masks (and for userspace without
+   ``AT_FILEATTR_MASKS``), conversion between the two formats on set is handled
+   by the VFS as described :ref:`below <conversion>`.
+
+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 (e.g. in backup/sync workloads)::
+
+       file_getattr(file1, &fa, ..., AT_FILEATTR_MASKS);
+       file_setattr(file2, &fa, ..., AT_FILEATTR_MASKS);
+
+   is a change of behaviour compared to the old RMW APIs, where unsupported
+   flags were often silently dropped:
+
+   - on filesystems without mask support, file_getattr() returns
+     ``fa_xflags_rw_mask = 0``, causing file_setattr() to fail with
+     ``-EINVAL``;
+   - on filesystems with mask support, file_getattr() returns all supported
+     writable xflags in ``fa_xflags_rw_mask``, so file_setattr() tries to set
+     all of them on ``file2`` and fails with ``-EOPNOTSUPP`` if ``file2``
+     doesn't support all of them, instead of silently dropping them.
+
+   With masks, the program can inspect ``file2``'s masks first and decide how
+   to handle unsupported flags.
+
+On file_setattr()
+-----------------
+
+- Userspace populates ``fa_xflags_rw_mask`` with the xflags it wishes to
+  change. If ``fa_xflags_rw_mask`` is ``0``, the request fails with
+  ``-EINVAL``.
+- 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 xflag bit in ``fa_xflags_rw_mask`` and clearing the bit in
+    ``fa_xflags`` disables the feature.
+
+- For value attributes (like ``FS_XFLAG_PROJID`` and ``FS_XFLAG_COWEXTSIZE``):
+
+  - Setting the flag bit in both ``fa_xflags_rw_mask`` and ``fa_xflags`` sets
+    the attribute, taking the value from the corresponding field in the struct.
+  - Setting the flag bit in ``fa_xflags_rw_mask``, clearing the bit in
+    ``fa_xflags`` and setting the field value to ``0`` unsets/clears the
+    attribute. If instead the field value is left non-zero, the request fails
+    with ``-EINVAL``.
+
+- Validation:
+
+  - 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 modification of any
+    flag outside ``FSX_FSXATTR_SET_MASK`` fails with ``-EOPNOTSUPP``.
+
+Usage Examples
+--------------
+
+See :manpage:`file_setattr(2)` for examples.
+
+Behaviour without ``AT_FILEATTR_MASKS``
+=======================================
+
+When not using ``AT_FILEATTR_MASKS``, file_getattr() and file_setattr() behave
+in the same way as ``FS_IOC_FSGETXATTR`` and ``FS_IOC_FSSETXATTR``
+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;        /* Struct has valid masks */
+        bool use_masks:1;        /* Set request uses masks (AT_FILEATTR_MASKS) */
+        bool get_only_masks:1;   /* Transitional: fs supports ro_mask on get, RMW on set */
+    };
+

Documentation/filesystems/fileattr.rst line 257

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

Not accurate. On get they are hints, and on set both may be true (e.g. ksmbd). Please describe what each means on get and on set.

Documentation/filesystems/fileattr.rst line 265

Quoted 8 lines
Documentation/filesystems/fileattr.rst new file +8 -0
+
+``has_masks``
+    Indicates whether ``fsx_xflags_rw_mask`` and ``fsx_xflags_ro_mask`` contain
+    valid masks.
+
+``use_masks``
+    Indicates whether the set request uses mask semantics (the in-kernel
+    equivalent of ``AT_FILEATTR_MASKS``).

Please describe the contract for a filesystem that accepts masks in set:
what it receives (only bits within fsx_xflags_rw_mask, value fields zero unless selected, flags_valid false) and that it must change only what rw_mask selects.

Documentation/filesystems/fileattr.rst line 269

Quoted 4 lines
Documentation/filesystems/fileattr.rst new file +4 -0
+
+``get_only_masks``
+    Transitional flag indicating that the filesystem supports ``fsx_xflags_ro_mask``
+    on get, but still expects unmasked RMW input in ``->fileattr_set()``.

Please document the rule that a filesystem should accept masks on set only after all of its writable FS_*_FL flags have xflag counterparts, because FS_IOC_SETFLAGS of a flag without an xflag will fail with EOPNOTSUPP. Until then, use get_only_masks.

Quoted 109 lines
Documentation/filesystems/fileattr.rst new file +109 -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);
+
+This way filesystems can handle flags internally either as ``FS_*_FL`` flags or
+``FS_XFLAG_*`` xflags and let VFS translate the common ones (defined in
+``FS_COMMON_FL`` / ``FSX_COMMON``).
+
+New and mask-aware drivers should use fileattr_fill_xflags() and set
+``fa->has_masks = true`` along with their supported masks, while
+``FS_*_FL``-only drivers use fileattr_fill_flags().
+
+Drivers that only support ``FS_*_FL`` flags can check for unsupported
+xflags in ``->fileattr_set()`` using fileattr_has_fsx(), while
+mask-aware drivers can validate against their allowed xflags masks with
+fileattr_has_invalid_fsx_masks()::
+
+    bool fileattr_has_fsx(const struct file_kattr *fa);
+
+    bool fileattr_has_invalid_fsx_masks(const struct file_kattr *fa,
+                                        const u32 allowed_rw,
+                                        const u32 allowed_ro);
+
+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 388

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``           |
++------------------------+--------------------------------+

Still very much not happy about this section.
How about merging only some of it with with XFLAGS table above For example:

Quoted 1 line
Documentation/filesystems/fileattr.rst new file +1 -0
+| Flag Macro                     | Hex Value  | Xflags mode | Flags mode  | Description and Constraints                           |

...

Quoted 11 lines

+| ``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 (VER1).                         |
+|                                |            |        | Updates require ``init_user_ns``.                     |

++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``FS_XFLAG_COMPR`` | 0x00200000 | RO | RW | Compressed file flag. |
++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``FS_XFLAG_ENCRYPT`` | 0x00400000 | RO | RO | Encrypted file flag (fscrypt). Read-only. |
++--------------------------------+------------+--------+-------------------------------------------------------+

Thanks,
Amir.

author
#20 lore raw

Re: [PATCH v4 7/7] docs: add fileattr API documentation

Quoted 270 lines · Amir Goldstein
Quoted 269 lines · koraynilay

Add documentation for the fileattr API.

---
 Documentation/filesystems/fileattr.rst | 469 +++++++++++++++++++++++++
 Documentation/filesystems/index.rst    |   1 +
 Documentation/filesystems/vfs.rst      |  19 +-
 3 files changed, 481 insertions(+), 8 deletions(-)
 create mode 100644 Documentation/filesystems/fileattr.rst
Documentation/filesystems/fileattr.rst new file +252 -0
new file mode 100644
index 000000000000..ee47ae2c9447
@@ -0,0 +1,469 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+============
+Fileattr API
+============
+
+Overview
+========
+
+The fileattr API provides a modern way to query and modify file attributes and
+it should be used instead of the older ``FS_IOC_[GS]ETFLAGS``
+(:manpage:`fs_ioc_getflags(2const)` / :manpage:`fs_ioc_setflags(2const)`) and
+``FS_IOC_FS[GS]ETXATTR`` (:manpage:`ioctl_xfs_fsgetxattr(2)` /
+:manpage:`ioctl_xfs_fssetxattr(2)`) ioctls.
+
+The "file attributes" that this document refers to are an overlap of the ones
+reported by :manpage:`statx(2)` in its ``stx_attributes`` field (e.g. immutable
+or append-only). Not to be confused with "Extended attributes", which are
+the ones managed by :manpage:`getxattr(2)` and :manpage:`setxattr(2)`.
+
+.. note::
+    For usage by userspace, see :manpage:`file_getattr(2)`,
+    :manpage:`file_setattr(2)` and :manpage:`file_attr(2type)`.
+
+Userspace API Summary
+=====================
+
+struct file_attr is versioned by size:
+
+- ``VER0``: The base structure, equivalent to struct fsxattr.
+- ``VER1``: Adds the ``fa_xflags_rw_mask`` and ``fa_xflags_ro_mask`` fields,
+  see :ref:`Masks <masks>` for more details.
+
+.. kernel-doc:: include/uapi/linux/fs.h
+   :identifiers: file_attr
+
+.. _fileattr_flags:
+
+Flag Reference
+==============
+
+This is the full list of currently implemented xflags.
+
+.. note::
+   In the Mode column, "RO/RW" means that the flag is read-only via the xflags
+   APIs (``FS_IOC_FSSETXATTR`` and file_setattr()), while some filesystems
+   allow modifying its ``FS_*_FL`` counterpart via ``FS_IOC_SETFLAGS``.
+
+   "RW" means the flag is writable via file_setattr() (and via
+   ``FS_IOC_FSSETXATTR`` / ``FS_IOC_SETFLAGS`` where applicable), and "RO"
+   means it is read-only across all fileattr userspace APIs. Filesystem support
+   may still vary.
+
++--------------------------------+------------+--------+-------------------------------------------------------+
+| 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 (VER1).                         |
+|                                |            |        | 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.             |
++--------------------------------+------------+--------+-------------------------------------------------------+
+
+
+.. _masks:
+
+Masks
+=====
+
+In ``VER1``, the fileattr API introduced the ``fa_xflags_rw_mask`` and
+``fa_xflags_ro_mask`` fields, to enable its usage without needing 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 file_getattr() / file_setattr(), otherwise the normal behaviour is kept.
+
+.. note::
+   For filesystems that don't support masks (and for userspace without
+   ``AT_FILEATTR_MASKS``), conversion between the two formats on set is handled
+   by the VFS as described :ref:`below <conversion>`.
+
+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 (e.g. in backup/sync workloads)::
+
+       file_getattr(file1, &fa, ..., AT_FILEATTR_MASKS);
+       file_setattr(file2, &fa, ..., AT_FILEATTR_MASKS);
+
+   is a change of behaviour compared to the old RMW APIs, where unsupported
+   flags were often silently dropped:
+
+   - on filesystems without mask support, file_getattr() returns
+     ``fa_xflags_rw_mask = 0``, causing file_setattr() to fail with
+     ``-EINVAL``;
+   - on filesystems with mask support, file_getattr() returns all supported
+     writable xflags in ``fa_xflags_rw_mask``, so file_setattr() tries to set
+     all of them on ``file2`` and fails with ``-EOPNOTSUPP`` if ``file2``
+     doesn't support all of them, instead of silently dropping them.
+
+   With masks, the program can inspect ``file2``'s masks first and decide how
+   to handle unsupported flags.
+
+On file_setattr()
+-----------------
+
+- Userspace populates ``fa_xflags_rw_mask`` with the xflags it wishes to
+  change. If ``fa_xflags_rw_mask`` is ``0``, the request fails with
+  ``-EINVAL``.
+- 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 xflag bit in ``fa_xflags_rw_mask`` and clearing the bit in
+    ``fa_xflags`` disables the feature.
+
+- For value attributes (like ``FS_XFLAG_PROJID`` and ``FS_XFLAG_COWEXTSIZE``):
+
+  - Setting the flag bit in both ``fa_xflags_rw_mask`` and ``fa_xflags`` sets
+    the attribute, taking the value from the corresponding field in the struct.
+  - Setting the flag bit in ``fa_xflags_rw_mask``, clearing the bit in
+    ``fa_xflags`` and setting the field value to ``0`` unsets/clears the
+    attribute. If instead the field value is left non-zero, the request fails
+    with ``-EINVAL``.
+
+- Validation:
+
+  - 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 modification of any
+    flag outside ``FSX_FSXATTR_SET_MASK`` fails with ``-EOPNOTSUPP``.
+
+Usage Examples
+--------------
+
+See :manpage:`file_setattr(2)` for examples.
+
+Behaviour without ``AT_FILEATTR_MASKS``
+=======================================
+
+When not using ``AT_FILEATTR_MASKS``, file_getattr() and file_setattr() behave
+in the same way as ``FS_IOC_FSGETXATTR`` and ``FS_IOC_FSSETXATTR``
+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;        /* Struct has valid masks */
+        bool use_masks:1;        /* Set request uses masks (AT_FILEATTR_MASKS) */
+        bool get_only_masks:1;   /* Transitional: fs supports ro_mask on get, RMW on set */
+    };
+
+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``
Quoted 8 lines

Documentation/filesystems/fileattr.rst line 257

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

Not accurate. On get they are hints, and on set both may be true (e.g. ksmbd). Please describe what each means on get and on set.

Ah, yes, will do.

Quoted 4 lines
Quoted 4 lines
Documentation/filesystems/fileattr.rst new file +4 -0
+
+``has_masks``
+    Indicates whether ``fsx_xflags_rw_mask`` and ``fsx_xflags_ro_mask`` contain
+    valid masks.
Quoted 8 lines

Documentation/filesystems/fileattr.rst line 265

Quoted 4 lines
Documentation/filesystems/fileattr.rst new file +4 -0
+
+``use_masks``
+    Indicates whether the set request uses mask semantics (the in-kernel
+    equivalent of ``AT_FILEATTR_MASKS``).

Please describe the contract for a filesystem that accepts masks in set:
what it receives (only bits within fsx_xflags_rw_mask, value fields zero unless selected, flags_valid false) and that it must change only what rw_mask selects.

Yes, right, will add.

Quoted 9 lines

Documentation/filesystems/fileattr.rst line 269

Quoted 4 lines
Documentation/filesystems/fileattr.rst new file +4 -0
+
+``get_only_masks``
+    Transitional flag indicating that the filesystem supports ``fsx_xflags_ro_mask``
+    on get, but still expects unmasked RMW input in ``->fileattr_set()``.

Please document the rule that a filesystem should accept masks on set only after all of its writable FS_*_FL flags have xflag counterparts, because FS_IOC_SETFLAGS of a flag without an xflag will fail with EOPNOTSUPP. Until then, use get_only_masks.

Right, that's what I was missing about get_only_masks (since I mentioned it only in the "adding new flags" part, I had a hunch I missed something).

Quoted 124 lines
Quoted 109 lines
Documentation/filesystems/fileattr.rst new file +109 -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);
+
+This way filesystems can handle flags internally either as ``FS_*_FL`` flags or
+``FS_XFLAG_*`` xflags and let VFS translate the common ones (defined in
+``FS_COMMON_FL`` / ``FSX_COMMON``).
+
+New and mask-aware drivers should use fileattr_fill_xflags() and set
+``fa->has_masks = true`` along with their supported masks, while
+``FS_*_FL``-only drivers use fileattr_fill_flags().
+
+Drivers that only support ``FS_*_FL`` flags can check for unsupported
+xflags in ``->fileattr_set()`` using fileattr_has_fsx(), while
+mask-aware drivers can validate against their allowed xflags masks with
+fileattr_has_invalid_fsx_masks()::
+
+    bool fileattr_has_fsx(const struct file_kattr *fa);
+
+    bool fileattr_has_invalid_fsx_masks(const struct file_kattr *fa,
+                                        const u32 allowed_rw,
+                                        const u32 allowed_ro);
+
+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 388

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``           |
++------------------------+--------------------------------+

Still very much not happy about this section.
How about merging only some of it with with XFLAGS table above For example:

Quoted 13 lines
Quoted 1 line
Documentation/filesystems/fileattr.rst new file +1 -0
+| Flag Macro                     | Hex Value  | Xflags mode | Flags mode  | Description and Constraints                           |

...

Quoted 11 lines

+| ``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 (VER1).                         |
+|                                |            |        | Updates require ``init_user_ns``.                     |

++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``FS_XFLAG_COMPR`` | 0x00200000 | RO | RW | Compressed file flag. |
++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``FS_XFLAG_ENCRYPT`` | 0x00400000 | RO | RO | Encrypted file flag (fscrypt). Read-only. |
++--------------------------------+------------+--------+-------------------------------------------------------+

Oh, yeah, the 2 columns for RO/RW are nicer and I can add a column so we have "Flag Macro" "XFlag Macro" ((I really don't like "Xflag")) and have only 1 table instead of 2?

Thanks,
koraynilay

Quoted 2 lines

Thanks,
Amir.

maintainer
#21 lore raw

Re: [PATCH v4 7/7] docs: add fileattr API documentation

Quoted 449 lines · koraynilay
Quoted 270 lines · Amir Goldstein
Quoted 269 lines · koraynilay

Add documentation for the fileattr API.

---
 Documentation/filesystems/fileattr.rst | 469 +++++++++++++++++++++++++
 Documentation/filesystems/index.rst    |   1 +
 Documentation/filesystems/vfs.rst      |  19 +-
 3 files changed, 481 insertions(+), 8 deletions(-)
 create mode 100644 Documentation/filesystems/fileattr.rst
Documentation/filesystems/fileattr.rst new file +252 -0
new file mode 100644
index 000000000000..ee47ae2c9447
@@ -0,0 +1,469 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+============
+Fileattr API
+============
+
+Overview
+========
+
+The fileattr API provides a modern way to query and modify file attributes and
+it should be used instead of the older ``FS_IOC_[GS]ETFLAGS``
+(:manpage:`fs_ioc_getflags(2const)` / :manpage:`fs_ioc_setflags(2const)`) and
+``FS_IOC_FS[GS]ETXATTR`` (:manpage:`ioctl_xfs_fsgetxattr(2)` /
+:manpage:`ioctl_xfs_fssetxattr(2)`) ioctls.
+
+The "file attributes" that this document refers to are an overlap of the ones
+reported by :manpage:`statx(2)` in its ``stx_attributes`` field (e.g. immutable
+or append-only). Not to be confused with "Extended attributes", which are
+the ones managed by :manpage:`getxattr(2)` and :manpage:`setxattr(2)`.
+
+.. note::
+    For usage by userspace, see :manpage:`file_getattr(2)`,
+    :manpage:`file_setattr(2)` and :manpage:`file_attr(2type)`.
+
+Userspace API Summary
+=====================
+
+struct file_attr is versioned by size:
+
+- ``VER0``: The base structure, equivalent to struct fsxattr.
+- ``VER1``: Adds the ``fa_xflags_rw_mask`` and ``fa_xflags_ro_mask`` fields,
+  see :ref:`Masks <masks>` for more details.
+
+.. kernel-doc:: include/uapi/linux/fs.h
+   :identifiers: file_attr
+
+.. _fileattr_flags:
+
+Flag Reference
+==============
+
+This is the full list of currently implemented xflags.
+
+.. note::
+   In the Mode column, "RO/RW" means that the flag is read-only via the xflags
+   APIs (``FS_IOC_FSSETXATTR`` and file_setattr()), while some filesystems
+   allow modifying its ``FS_*_FL`` counterpart via ``FS_IOC_SETFLAGS``.
+
+   "RW" means the flag is writable via file_setattr() (and via
+   ``FS_IOC_FSSETXATTR`` / ``FS_IOC_SETFLAGS`` where applicable), and "RO"
+   means it is read-only across all fileattr userspace APIs. Filesystem support
+   may still vary.
+
++--------------------------------+------------+--------+-------------------------------------------------------+
+| 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 (VER1).                         |
+|                                |            |        | 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.             |
++--------------------------------+------------+--------+-------------------------------------------------------+
+
+
+.. _masks:
+
+Masks
+=====
+
+In ``VER1``, the fileattr API introduced the ``fa_xflags_rw_mask`` and
+``fa_xflags_ro_mask`` fields, to enable its usage without needing 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 file_getattr() / file_setattr(), otherwise the normal behaviour is kept.
+
+.. note::
+   For filesystems that don't support masks (and for userspace without
+   ``AT_FILEATTR_MASKS``), conversion between the two formats on set is handled
+   by the VFS as described :ref:`below <conversion>`.
+
+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 (e.g. in backup/sync workloads)::
+
+       file_getattr(file1, &fa, ..., AT_FILEATTR_MASKS);
+       file_setattr(file2, &fa, ..., AT_FILEATTR_MASKS);
+
+   is a change of behaviour compared to the old RMW APIs, where unsupported
+   flags were often silently dropped:
+
+   - on filesystems without mask support, file_getattr() returns
+     ``fa_xflags_rw_mask = 0``, causing file_setattr() to fail with
+     ``-EINVAL``;
+   - on filesystems with mask support, file_getattr() returns all supported
+     writable xflags in ``fa_xflags_rw_mask``, so file_setattr() tries to set
+     all of them on ``file2`` and fails with ``-EOPNOTSUPP`` if ``file2``
+     doesn't support all of them, instead of silently dropping them.
+
+   With masks, the program can inspect ``file2``'s masks first and decide how
+   to handle unsupported flags.
+
+On file_setattr()
+-----------------
+
+- Userspace populates ``fa_xflags_rw_mask`` with the xflags it wishes to
+  change. If ``fa_xflags_rw_mask`` is ``0``, the request fails with
+  ``-EINVAL``.
+- 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 xflag bit in ``fa_xflags_rw_mask`` and clearing the bit in
+    ``fa_xflags`` disables the feature.
+
+- For value attributes (like ``FS_XFLAG_PROJID`` and ``FS_XFLAG_COWEXTSIZE``):
+
+  - Setting the flag bit in both ``fa_xflags_rw_mask`` and ``fa_xflags`` sets
+    the attribute, taking the value from the corresponding field in the struct.
+  - Setting the flag bit in ``fa_xflags_rw_mask``, clearing the bit in
+    ``fa_xflags`` and setting the field value to ``0`` unsets/clears the
+    attribute. If instead the field value is left non-zero, the request fails
+    with ``-EINVAL``.
+
+- Validation:
+
+  - 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 modification of any
+    flag outside ``FSX_FSXATTR_SET_MASK`` fails with ``-EOPNOTSUPP``.
+
+Usage Examples
+--------------
+
+See :manpage:`file_setattr(2)` for examples.
+
+Behaviour without ``AT_FILEATTR_MASKS``
+=======================================
+
+When not using ``AT_FILEATTR_MASKS``, file_getattr() and file_setattr() behave
+in the same way as ``FS_IOC_FSGETXATTR`` and ``FS_IOC_FSSETXATTR``
+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;        /* Struct has valid masks */
+        bool use_masks:1;        /* Set request uses masks (AT_FILEATTR_MASKS) */
+        bool get_only_masks:1;   /* Transitional: fs supports ro_mask on get, RMW on set */
+    };
+
+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``
Quoted 8 lines

Documentation/filesystems/fileattr.rst line 257

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

Not accurate. On get they are hints, and on set both may be true (e.g. ksmbd). Please describe what each means on get and on set.

Ah, yes, will do.

Quoted 4 lines
Quoted 4 lines
Documentation/filesystems/fileattr.rst new file +4 -0
+
+``has_masks``
+    Indicates whether ``fsx_xflags_rw_mask`` and ``fsx_xflags_ro_mask`` contain
+    valid masks.
Quoted 8 lines

Documentation/filesystems/fileattr.rst line 265

Quoted 4 lines
Documentation/filesystems/fileattr.rst new file +4 -0
+
+``use_masks``
+    Indicates whether the set request uses mask semantics (the in-kernel
+    equivalent of ``AT_FILEATTR_MASKS``).

Please describe the contract for a filesystem that accepts masks in set:
what it receives (only bits within fsx_xflags_rw_mask, value fields zero unless selected, flags_valid false) and that it must change only what rw_mask selects.

Yes, right, will add.

Quoted 9 lines

Documentation/filesystems/fileattr.rst line 269

Quoted 4 lines
Documentation/filesystems/fileattr.rst new file +4 -0
+
+``get_only_masks``
+    Transitional flag indicating that the filesystem supports ``fsx_xflags_ro_mask``
+    on get, but still expects unmasked RMW input in ``->fileattr_set()``.

Please document the rule that a filesystem should accept masks on set only after all of its writable FS_*_FL flags have xflag counterparts, because FS_IOC_SETFLAGS of a flag without an xflag will fail with EOPNOTSUPP. Until then, use get_only_masks.

Right, that's what I was missing about get_only_masks (since I mentioned it only in the "adding new flags" part, I had a hunch I missed something).

Quoted 124 lines
Quoted 109 lines
Documentation/filesystems/fileattr.rst new file +109 -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);
+
+This way filesystems can handle flags internally either as ``FS_*_FL`` flags or
+``FS_XFLAG_*`` xflags and let VFS translate the common ones (defined in
+``FS_COMMON_FL`` / ``FSX_COMMON``).
+
+New and mask-aware drivers should use fileattr_fill_xflags() and set
+``fa->has_masks = true`` along with their supported masks, while
+``FS_*_FL``-only drivers use fileattr_fill_flags().
+
+Drivers that only support ``FS_*_FL`` flags can check for unsupported
+xflags in ``->fileattr_set()`` using fileattr_has_fsx(), while
+mask-aware drivers can validate against their allowed xflags masks with
+fileattr_has_invalid_fsx_masks()::
+
+    bool fileattr_has_fsx(const struct file_kattr *fa);
+
+    bool fileattr_has_invalid_fsx_masks(const struct file_kattr *fa,
+                                        const u32 allowed_rw,
+                                        const u32 allowed_ro);
+
+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 388

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``           |
++------------------------+--------------------------------+

Still very much not happy about this section.
How about merging only some of it with with XFLAGS table above For example:

Quoted 13 lines
Quoted 1 line
Documentation/filesystems/fileattr.rst new file +1 -0
+| Flag Macro                     | Hex Value  | Xflags mode | Flags mode  | Description and Constraints                           |

...

Quoted 11 lines

+| ``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 (VER1).                         |
+|                                |            |        | Updates require ``init_user_ns``.                     |

++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``FS_XFLAG_COMPR`` | 0x00200000 | RO | RW | Compressed file flag. |
++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``FS_XFLAG_ENCRYPT`` | 0x00400000 | RO | RO | Encrypted file flag (fscrypt). Read-only. |
++--------------------------------+------------+--------+-------------------------------------------------------+

Quoted 1 line

Oh, yeah, the 2 columns for RO/RW are nicer and I can add a column so we

Yes.

Quoted 2 lines

have "Flag Macro" "XFlag Macro" ((I really don't like "Xflag")) and have only 1 table instead of 2?

One table, yes.
Writing all the flags there like this and their hex values:

FS_XFLAG_ENCRYPT | FS_XFLAG_ENCRYPT

I think it's just silly and has no place in this doc.

If you want you can tell the story about file attributes, some can be access by one API, some by another and some by both the story can tell about the template of the FS_*_FL and FS_XFLAG_* constants etc and the table could be keyed by the attr name (e.g. ENCRYPT) and the table will say for each what it is and which API can access it with which mode, hence:

| Attribute              | xflags | flags  | Description and
Constraints                           |
...
| ``NOCOMP``       |   -   | RW  |  ....
+------------------------+-----+-------+-------------------------------------------------------+
| ``COMPR``          | RO | RW  | Compressed file flag.
                 |
+-------------------------+-----+------+-------------------------------------------------------+
| ``ENCRYPT``         | RO | RO | Encrypted file flag (fscrypt).
Read-only.             |
+-------------------------+-----+-----+-------------------------------------------------------+

And then somehow also represent masked only xflags, maybe:

| ``MAYSHAREEXT``   | MRO | - | Maye sharing data extends with other
files.             |
| ``BTIME``                   | MRW | - | Birth time (modifiable on
some filesystems).             |

Thanks,
Amir.

author
#22 lore raw

Re: [PATCH v4 7/7] docs: add fileattr API documentation

Quoted 454 lines · Amir Goldstein
Quoted 449 lines · koraynilay
Quoted 270 lines · Amir Goldstein
Quoted 269 lines · koraynilay

Add documentation for the fileattr API.

---
 Documentation/filesystems/fileattr.rst | 469 +++++++++++++++++++++++++
 Documentation/filesystems/index.rst    |   1 +
 Documentation/filesystems/vfs.rst      |  19 +-
 3 files changed, 481 insertions(+), 8 deletions(-)
 create mode 100644 Documentation/filesystems/fileattr.rst
Documentation/filesystems/fileattr.rst new file +252 -0
new file mode 100644
index 000000000000..ee47ae2c9447
@@ -0,0 +1,469 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+============
+Fileattr API
+============
+
+Overview
+========
+
+The fileattr API provides a modern way to query and modify file attributes and
+it should be used instead of the older ``FS_IOC_[GS]ETFLAGS``
+(:manpage:`fs_ioc_getflags(2const)` / :manpage:`fs_ioc_setflags(2const)`) and
+``FS_IOC_FS[GS]ETXATTR`` (:manpage:`ioctl_xfs_fsgetxattr(2)` /
+:manpage:`ioctl_xfs_fssetxattr(2)`) ioctls.
+
+The "file attributes" that this document refers to are an overlap of the ones
+reported by :manpage:`statx(2)` in its ``stx_attributes`` field (e.g. immutable
+or append-only). Not to be confused with "Extended attributes", which are
+the ones managed by :manpage:`getxattr(2)` and :manpage:`setxattr(2)`.
+
+.. note::
+    For usage by userspace, see :manpage:`file_getattr(2)`,
+    :manpage:`file_setattr(2)` and :manpage:`file_attr(2type)`.
+
+Userspace API Summary
+=====================
+
+struct file_attr is versioned by size:
+
+- ``VER0``: The base structure, equivalent to struct fsxattr.
+- ``VER1``: Adds the ``fa_xflags_rw_mask`` and ``fa_xflags_ro_mask`` fields,
+  see :ref:`Masks <masks>` for more details.
+
+.. kernel-doc:: include/uapi/linux/fs.h
+   :identifiers: file_attr
+
+.. _fileattr_flags:
+
+Flag Reference
+==============
+
+This is the full list of currently implemented xflags.
+
+.. note::
+   In the Mode column, "RO/RW" means that the flag is read-only via the xflags
+   APIs (``FS_IOC_FSSETXATTR`` and file_setattr()), while some filesystems
+   allow modifying its ``FS_*_FL`` counterpart via ``FS_IOC_SETFLAGS``.
+
+   "RW" means the flag is writable via file_setattr() (and via
+   ``FS_IOC_FSSETXATTR`` / ``FS_IOC_SETFLAGS`` where applicable), and "RO"
+   means it is read-only across all fileattr userspace APIs. Filesystem support
+   may still vary.
+
++--------------------------------+------------+--------+-------------------------------------------------------+
+| 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 (VER1).                         |
+|                                |            |        | 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.             |
++--------------------------------+------------+--------+-------------------------------------------------------+
+
+
+.. _masks:
+
+Masks
+=====
+
+In ``VER1``, the fileattr API introduced the ``fa_xflags_rw_mask`` and
+``fa_xflags_ro_mask`` fields, to enable its usage without needing 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 file_getattr() / file_setattr(), otherwise the normal behaviour is kept.
+
+.. note::
+   For filesystems that don't support masks (and for userspace without
+   ``AT_FILEATTR_MASKS``), conversion between the two formats on set is handled
+   by the VFS as described :ref:`below <conversion>`.
+
+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 (e.g. in backup/sync workloads)::
+
+       file_getattr(file1, &fa, ..., AT_FILEATTR_MASKS);
+       file_setattr(file2, &fa, ..., AT_FILEATTR_MASKS);
+
+   is a change of behaviour compared to the old RMW APIs, where unsupported
+   flags were often silently dropped:
+
+   - on filesystems without mask support, file_getattr() returns
+     ``fa_xflags_rw_mask = 0``, causing file_setattr() to fail with
+     ``-EINVAL``;
+   - on filesystems with mask support, file_getattr() returns all supported
+     writable xflags in ``fa_xflags_rw_mask``, so file_setattr() tries to set
+     all of them on ``file2`` and fails with ``-EOPNOTSUPP`` if ``file2``
+     doesn't support all of them, instead of silently dropping them.
+
+   With masks, the program can inspect ``file2``'s masks first and decide how
+   to handle unsupported flags.
+
+On file_setattr()
+-----------------
+
+- Userspace populates ``fa_xflags_rw_mask`` with the xflags it wishes to
+  change. If ``fa_xflags_rw_mask`` is ``0``, the request fails with
+  ``-EINVAL``.
+- 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 xflag bit in ``fa_xflags_rw_mask`` and clearing the bit in
+    ``fa_xflags`` disables the feature.
+
+- For value attributes (like ``FS_XFLAG_PROJID`` and ``FS_XFLAG_COWEXTSIZE``):
+
+  - Setting the flag bit in both ``fa_xflags_rw_mask`` and ``fa_xflags`` sets
+    the attribute, taking the value from the corresponding field in the struct.
+  - Setting the flag bit in ``fa_xflags_rw_mask``, clearing the bit in
+    ``fa_xflags`` and setting the field value to ``0`` unsets/clears the
+    attribute. If instead the field value is left non-zero, the request fails
+    with ``-EINVAL``.
+
+- Validation:
+
+  - 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 modification of any
+    flag outside ``FSX_FSXATTR_SET_MASK`` fails with ``-EOPNOTSUPP``.
+
+Usage Examples
+--------------
+
+See :manpage:`file_setattr(2)` for examples.
+
+Behaviour without ``AT_FILEATTR_MASKS``
+=======================================
+
+When not using ``AT_FILEATTR_MASKS``, file_getattr() and file_setattr() behave
+in the same way as ``FS_IOC_FSGETXATTR`` and ``FS_IOC_FSSETXATTR``
+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;        /* Struct has valid masks */
+        bool use_masks:1;        /* Set request uses masks (AT_FILEATTR_MASKS) */
+        bool get_only_masks:1;   /* Transitional: fs supports ro_mask on get, RMW on set */
+    };
+
+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``
Quoted 8 lines

Documentation/filesystems/fileattr.rst line 257

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

Not accurate. On get they are hints, and on set both may be true (e.g. ksmbd). Please describe what each means on get and on set.

Ah, yes, will do.

Quoted 4 lines
Quoted 4 lines
Documentation/filesystems/fileattr.rst new file +4 -0
+
+``has_masks``
+    Indicates whether ``fsx_xflags_rw_mask`` and ``fsx_xflags_ro_mask`` contain
+    valid masks.
Quoted 8 lines

Documentation/filesystems/fileattr.rst line 265

Quoted 4 lines
Documentation/filesystems/fileattr.rst new file +4 -0
+
+``use_masks``
+    Indicates whether the set request uses mask semantics (the in-kernel
+    equivalent of ``AT_FILEATTR_MASKS``).

Please describe the contract for a filesystem that accepts masks in set:
what it receives (only bits within fsx_xflags_rw_mask, value fields zero unless selected, flags_valid false) and that it must change only what rw_mask selects.

Yes, right, will add.

Quoted 9 lines

Documentation/filesystems/fileattr.rst line 269

Quoted 4 lines
Documentation/filesystems/fileattr.rst new file +4 -0
+
+``get_only_masks``
+    Transitional flag indicating that the filesystem supports ``fsx_xflags_ro_mask``
+    on get, but still expects unmasked RMW input in ``->fileattr_set()``.

Please document the rule that a filesystem should accept masks on set only after all of its writable FS_*_FL flags have xflag counterparts, because FS_IOC_SETFLAGS of a flag without an xflag will fail with EOPNOTSUPP. Until then, use get_only_masks.

Right, that's what I was missing about get_only_masks (since I mentioned it only in the "adding new flags" part, I had a hunch I missed something).

Quoted 124 lines
Quoted 109 lines
Documentation/filesystems/fileattr.rst new file +109 -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);
+
+This way filesystems can handle flags internally either as ``FS_*_FL`` flags or
+``FS_XFLAG_*`` xflags and let VFS translate the common ones (defined in
+``FS_COMMON_FL`` / ``FSX_COMMON``).
+
+New and mask-aware drivers should use fileattr_fill_xflags() and set
+``fa->has_masks = true`` along with their supported masks, while
+``FS_*_FL``-only drivers use fileattr_fill_flags().
+
+Drivers that only support ``FS_*_FL`` flags can check for unsupported
+xflags in ``->fileattr_set()`` using fileattr_has_fsx(), while
+mask-aware drivers can validate against their allowed xflags masks with
+fileattr_has_invalid_fsx_masks()::
+
+    bool fileattr_has_fsx(const struct file_kattr *fa);
+
+    bool fileattr_has_invalid_fsx_masks(const struct file_kattr *fa,
+                                        const u32 allowed_rw,
+                                        const u32 allowed_ro);
+
+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 388

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``           |
++------------------------+--------------------------------+

Still very much not happy about this section.
How about merging only some of it with with XFLAGS table above For example:

Quoted 13 lines
Quoted 1 line
Documentation/filesystems/fileattr.rst new file +1 -0
+| Flag Macro                     | Hex Value  | Xflags mode | Flags mode  | Description and Constraints                           |

...

Quoted 11 lines

+| ``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 (VER1).                         |
+|                                |            |        | Updates require ``init_user_ns``.                     |

++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``FS_XFLAG_COMPR`` | 0x00200000 | RO | RW | Compressed file flag. |
++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``FS_XFLAG_ENCRYPT`` | 0x00400000 | RO | RO | Encrypted file flag (fscrypt). Read-only. |
++--------------------------------+------------+--------+-------------------------------------------------------+

Quoted 1 line

Oh, yeah, the 2 columns for RO/RW are nicer and I can add a column so we

Yes.

Quoted 7 lines
Quoted 2 lines

have "Flag Macro" "XFlag Macro" ((I really don't like "Xflag")) and have only 1 table instead of 2?

One table, yes.
Writing all the flags there like this and their hex values:

FS_XFLAG_ENCRYPT | FS_XFLAG_ENCRYPT

(Assuming you meant FS_XFLAG_ENCRYPT | FS_ENCRYPT_FL)

Quoted 2 lines

I think it's just silly and has no place in this doc.

Hmm, can you check how it looks? https://koraynilay.neocities.org/vfs-masks/fileattr
(don't mind the unclear writing in the note above it, I haven't read it manually yet)

Quoted 7 lines

If you want you can tell the story about file attributes, some can be access by one API, some by another and some by both the story can tell about the template of the FS_*_FL and FS_XFLAG_* constants etc and the table could be keyed by the attr name (e.g. ENCRYPT) and the table will say for each what it is and which API can access it with which mode, hence:

Quoted 11 lines
| Attribute              | xflags | flags  | Description and
Constraints                           |
...
| ``NOCOMP``       |   -   | RW  |  ....
+------------------------+-----+-------+-------------------------------------------------------+
| ``COMPR``          | RO | RW  | Compressed file flag.
                 |
+-------------------------+-----+------+-------------------------------------------------------+
| ``ENCRYPT``         | RO | RO | Encrypted file flag (fscrypt).
Read-only.             |
+-------------------------+-----+-----+-------------------------------------------------------+

So you mean only saying the attribute name and not the flag/xflags macros?

In that case, I would still keep the flag|xflag table as it is in this patch series (so devs have a visual way to know which flags have a xflag counterpart and which don't)

Quoted 6 lines

And then somehow also represent masked only xflags, maybe:

| ``MAYSHAREEXT``   | MRO | - | Maye sharing data extends with other
files.             |
| ``BTIME``                   | MRW | - | Birth time (modifiable on
some filesystems).             |

Yes, but IMO this could be done in the same table as in the link above, just like all other FS_XFLAG_*.

Thanks,
koraynilay

Quoted 2 lines

Thanks,
Amir.

author
#23 lore raw

Re: [PATCH v4 7/7] docs: add fileattr API documentation

Quoted 5 lines · koraynilay
Quoted 4 lines · Amir Goldstein
Quoted 3 lines · koraynilay
Quoted 1 line · Amir Goldstein

On Wed, Dec 2, 2026 at 10:54 PM koraynilay <[email protected]> wrote:

...

Quoted 39 lines
Quoted 39 lines
Quoted 29 lines
Quoted 11 lines
Quoted 10 lines

+| | ``FS_XFLAG_FILESTREAM`` |
++------------------------+--------------------------------+
+| | ``FS_XFLAG_COWEXTSIZE`` |
++------------------------+--------------------------------+
+| | ``FS_XFLAG_CASENONPRESERVING`` |
++------------------------+--------------------------------+
+| | ``FS_XFLAG_PROJID`` |
++------------------------+--------------------------------+
+| | ``FS_XFLAG_HASATTR`` |
++------------------------+--------------------------------+

Quoted 17 lines

Still very much not happy about this section.
How about merging only some of it with with XFLAGS table above For example:

Quoted 1 line

+| Flag Macro | Hex Value | Xflags mode | Flags mode | Description and Constraints |

...

Quoted 11 lines

+| ``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 (VER1).                         |
+|                                |            |        | Updates require ``init_user_ns``.                     |

++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``FS_XFLAG_COMPR`` | 0x00200000 | RO | RW | Compressed file flag. |
++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``FS_XFLAG_ENCRYPT`` | 0x00400000 | RO | RO | Encrypted file flag (fscrypt). Read-only. |
++--------------------------------+------------+--------+-------------------------------------------------------+

Quoted 1 line

Oh, yeah, the 2 columns for RO/RW are nicer and I can add a column so we

Yes.

Quoted 2 lines

have "Flag Macro" "XFlag Macro" ((I really don't like "Xflag")) and have only 1 table instead of 2?

One table, yes.
Writing all the flags there like this and their hex values:

Quoted 8 lines
Quoted 1 line

FS_XFLAG_ENCRYPT | FS_XFLAG_ENCRYPT

(Assuming you meant FS_XFLAG_ENCRYPT | FS_ENCRYPT_FL)

Quoted 2 lines

I think it's just silly and has no place in this doc.

Hmm, can you check how it looks? https://koraynilay.neocities.org/vfs-masks/fileattr
(don't mind the unclear writing in the note above it, I haven't read it manually yet)

Ok sorry updated the html at the link to not include the hex values, that is indeed pretty useless since it's just a matter of reading fs/uapi/linux/fs.h.

Thanks,
koraynilay

Quoted 41 lines
Quoted 7 lines

If you want you can tell the story about file attributes, some can be access by one API, some by another and some by both the story can tell about the template of the FS_*_FL and FS_XFLAG_* constants etc and the table could be keyed by the attr name (e.g. ENCRYPT) and the table will say for each what it is and which API can access it with which mode, hence:

Quoted 11 lines
| Attribute              | xflags | flags  | Description and
Constraints                           |
...
| ``NOCOMP``       |   -   | RW  |  ....
+------------------------+-----+-------+-------------------------------------------------------+
| ``COMPR``          | RO | RW  | Compressed file flag.
                 |
+-------------------------+-----+------+-------------------------------------------------------+
| ``ENCRYPT``         | RO | RO | Encrypted file flag (fscrypt).
Read-only.             |
+-------------------------+-----+-----+-------------------------------------------------------+

So you mean only saying the attribute name and not the flag/xflags macros?

In that case, I would still keep the flag|xflag table as it is in this patch series (so devs have a visual way to know which flags have a xflag counterpart and which don't)

Quoted 6 lines

And then somehow also represent masked only xflags, maybe:

| ``MAYSHAREEXT``   | MRO | - | Maye sharing data extends with other
files.             |
| ``BTIME``                   | MRW | - | Birth time (modifiable on
some filesystems).             |

Yes, but IMO this could be done in the same table as in the link above, just like all other FS_XFLAG_*.

Thanks,
koraynilay

Quoted 2 lines

Thanks,
Amir.

maintainer
#24 lore raw

Re: [PATCH v4 7/7] docs: add fileattr API documentation

Quoted 47 lines · koraynilay
Quoted 5 lines · koraynilay
Quoted 4 lines · Amir Goldstein
Quoted 3 lines · koraynilay
Quoted 1 line · Amir Goldstein

On Wed, Dec 2, 2026 at 10:54 PM koraynilay <[email protected]> wrote:

...

Quoted 39 lines
Quoted 39 lines
Quoted 29 lines
Quoted 11 lines
Quoted 10 lines

+| | ``FS_XFLAG_FILESTREAM`` |
++------------------------+--------------------------------+
+| | ``FS_XFLAG_COWEXTSIZE`` |
++------------------------+--------------------------------+
+| | ``FS_XFLAG_CASENONPRESERVING`` |
++------------------------+--------------------------------+
+| | ``FS_XFLAG_PROJID`` |
++------------------------+--------------------------------+
+| | ``FS_XFLAG_HASATTR`` |
++------------------------+--------------------------------+

Quoted 17 lines

Still very much not happy about this section.
How about merging only some of it with with XFLAGS table above For example:

Quoted 1 line

+| Flag Macro | Hex Value | Xflags mode | Flags mode | Description and Constraints |

...

Quoted 11 lines

+| ``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 (VER1).                         |
+|                                |            |        | Updates require ``init_user_ns``.                     |

++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``FS_XFLAG_COMPR`` | 0x00200000 | RO | RW | Compressed file flag. |
++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``FS_XFLAG_ENCRYPT`` | 0x00400000 | RO | RO | Encrypted file flag (fscrypt). Read-only. |
++--------------------------------+------------+--------+-------------------------------------------------------+

Quoted 1 line

Oh, yeah, the 2 columns for RO/RW are nicer and I can add a column so we

Yes.

Quoted 2 lines

have "Flag Macro" "XFlag Macro" ((I really don't like "Xflag")) and have only 1 table instead of 2?

One table, yes.
Writing all the flags there like this and their hex values:

Quoted 8 lines
Quoted 8 lines
Quoted 1 line

FS_XFLAG_ENCRYPT | FS_XFLAG_ENCRYPT

(Assuming you meant FS_XFLAG_ENCRYPT | FS_ENCRYPT_FL)

Quoted 2 lines

I think it's just silly and has no place in this doc.

Hmm, can you check how it looks? https://koraynilay.neocities.org/vfs-masks/fileattr
(don't mind the unclear writing in the note above it, I haven't read it manually yet)

In my eyes it is ugly, because it looks like a page from a spec, which is not what
this doc is about, but I can get over it if it means so much to you.

How exactly is this:

 | ``NOCOMP``       |   -   | RW  |  ....
 +------------------------+-----+-------+-------------------------------------------------------+
  | ``COMPR``          | RO | RW  | Compressed file flag.

Less informative than your expanded form?

As a design/overview doc, what is missing to you when you read this?

Thanks,
Amir.

author
#25 lore raw

Re: [PATCH v4 7/7] docs: add fileattr API documentation

Quoted 63 lines · Amir Goldstein
Quoted 47 lines · koraynilay
Quoted 5 lines · koraynilay
Quoted 4 lines · Amir Goldstein
Quoted 3 lines · koraynilay
Quoted 1 line · Amir Goldstein

On Wed, Dec 2, 2026 at 10:54 PM koraynilay <[email protected]> wrote:

...

Quoted 39 lines
Quoted 39 lines
Quoted 29 lines
Quoted 11 lines
Quoted 10 lines

+| | ``FS_XFLAG_FILESTREAM`` |
++------------------------+--------------------------------+
+| | ``FS_XFLAG_COWEXTSIZE`` |
++------------------------+--------------------------------+
+| | ``FS_XFLAG_CASENONPRESERVING`` |
++------------------------+--------------------------------+
+| | ``FS_XFLAG_PROJID`` |
++------------------------+--------------------------------+
+| | ``FS_XFLAG_HASATTR`` |
++------------------------+--------------------------------+

Quoted 17 lines

Still very much not happy about this section.
How about merging only some of it with with XFLAGS table above For example:

Quoted 1 line

+| Flag Macro | Hex Value | Xflags mode | Flags mode | Description and Constraints |

...

Quoted 11 lines

+| ``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 (VER1).                         |
+|                                |            |        | Updates require ``init_user_ns``.                     |

++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``FS_XFLAG_COMPR`` | 0x00200000 | RO | RW | Compressed file flag. |
++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``FS_XFLAG_ENCRYPT`` | 0x00400000 | RO | RO | Encrypted file flag (fscrypt). Read-only. |
++--------------------------------+------------+--------+-------------------------------------------------------+

Quoted 1 line

Oh, yeah, the 2 columns for RO/RW are nicer and I can add a column so we

Yes.

Quoted 2 lines

have "Flag Macro" "XFlag Macro" ((I really don't like "Xflag")) and have only 1 table instead of 2?

One table, yes.
Writing all the flags there like this and their hex values:

Quoted 8 lines
Quoted 8 lines
Quoted 1 line

FS_XFLAG_ENCRYPT | FS_XFLAG_ENCRYPT

(Assuming you meant FS_XFLAG_ENCRYPT | FS_ENCRYPT_FL)

Quoted 2 lines

I think it's just silly and has no place in this doc.

Hmm, can you check how it looks? https://koraynilay.neocities.org/vfs-masks/fileattr
(don't mind the unclear writing in the note above it, I haven't read it manually yet)

In my eyes it is ugly, because it looks like a page from a spec, which is not what
this doc is about, but I can get over it if it means so much to you.

How exactly is this:

Quoted 7 lines
 | ``NOCOMP``       |   -   | RW  |  ....
 +------------------------+-----+-------+-------------------------------------------------------+
  | ``COMPR``          | RO | RW  | Compressed file flag.

Less informative than your expanded form?

As a design/overview doc, what is missing to you when you read this?

A way to see, at a glance, which flags already got converted to xflags and which not.
While I like this because it's informative, thinking about it it would be pretty useful for the

Quoted 4 lines

Please document the rule that a filesystem should accept masks on set only after all of its writable FS_*_FL flags have xflag counterparts, because FS_IOC_SETFLAGS of a flag without an xflag will fail with EOPNOTSUPP. Until then, use get_only_masks.

part from the first email, so developers can see how many of their flags need to be converted and decide if it's worth the effort or not.

This way they don't have to grep around and still not be 100% sure (or they just make their LLM generate a table like this).

Quoted 1 line

As a design/overview doc

Personally I don't mind if it's in this exact doc or not, but if not, where would we put it?

(I am also totally fine with keeping 1 general table like this

Quoted 3 lines
 | ``NOCOMP``       |   -   | RW  |  ....
 +------------------------+-----+-------+-------------------------------------------------------+
  | ``COMPR``          | RO | RW  | Compressed file flag.

and have the table with only the conversions by itself (like in this patch).)

Thanks,
koraynilay

Quoted 2 lines

Thanks,
Amir.

maintainer
#26 lore raw

Re: [PATCH v4 7/7] docs: add fileattr API documentation

Quoted 91 lines · koraynilay
Quoted 63 lines · Amir Goldstein
Quoted 47 lines · koraynilay
Quoted 5 lines · koraynilay
Quoted 4 lines · Amir Goldstein
Quoted 3 lines · koraynilay
Quoted 1 line · Amir Goldstein

On Wed, Dec 2, 2026 at 10:54 PM koraynilay <[email protected]> wrote:

...

Quoted 39 lines
Quoted 39 lines
Quoted 29 lines
Quoted 11 lines
Quoted 10 lines

+| | ``FS_XFLAG_FILESTREAM`` |
++------------------------+--------------------------------+
+| | ``FS_XFLAG_COWEXTSIZE`` |
++------------------------+--------------------------------+
+| | ``FS_XFLAG_CASENONPRESERVING`` |
++------------------------+--------------------------------+
+| | ``FS_XFLAG_PROJID`` |
++------------------------+--------------------------------+
+| | ``FS_XFLAG_HASATTR`` |
++------------------------+--------------------------------+

Quoted 17 lines

Still very much not happy about this section.
How about merging only some of it with with XFLAGS table above For example:

Quoted 1 line

+| Flag Macro | Hex Value | Xflags mode | Flags mode | Description and Constraints |

...

Quoted 11 lines

+| ``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 (VER1).                         |
+|                                |            |        | Updates require ``init_user_ns``.                     |

++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``FS_XFLAG_COMPR`` | 0x00200000 | RO | RW | Compressed file flag. |
++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``FS_XFLAG_ENCRYPT`` | 0x00400000 | RO | RO | Encrypted file flag (fscrypt). Read-only. |
++--------------------------------+------------+--------+-------------------------------------------------------+

Quoted 1 line

Oh, yeah, the 2 columns for RO/RW are nicer and I can add a column so we

Yes.

Quoted 2 lines

have "Flag Macro" "XFlag Macro" ((I really don't like "Xflag")) and have only 1 table instead of 2?

One table, yes.
Writing all the flags there like this and their hex values:

Quoted 8 lines
Quoted 8 lines
Quoted 1 line

FS_XFLAG_ENCRYPT | FS_XFLAG_ENCRYPT

(Assuming you meant FS_XFLAG_ENCRYPT | FS_ENCRYPT_FL)

Quoted 2 lines

I think it's just silly and has no place in this doc.

Hmm, can you check how it looks? https://koraynilay.neocities.org/vfs-masks/fileattr
(don't mind the unclear writing in the note above it, I haven't read it manually yet)

In my eyes it is ugly, because it looks like a page from a spec, which is not what
this doc is about, but I can get over it if it means so much to you.

How exactly is this:

Quoted 7 lines
 | ``NOCOMP``       |   -   | RW  |  ....
 +------------------------+-----+-------+-------------------------------------------------------+
  | ``COMPR``          | RO | RW  | Compressed file flag.

Less informative than your expanded form?

As a design/overview doc, what is missing to you when you read this?

A way to see, at a glance, which flags already got converted to xflags and which not.
While I like this because it's informative, thinking about it it would be pretty useful for the

Quoted 4 lines

Please document the rule that a filesystem should accept masks on set only after all of its writable FS_*_FL flags have xflag counterparts, because FS_IOC_SETFLAGS of a flag without an xflag will fail with EOPNOTSUPP. Until then, use get_only_masks.

part from the first email, so developers can see how many of their flags need to be converted and decide if it's worth the effort or not.

This way they don't have to grep around and still not be 100% sure (or they just make their LLM generate a table like this).

Quoted 1 line

As a design/overview doc

Personally I don't mind if it's in this exact doc or not, but if not, where would we put it?

Quoted 7 lines

(I am also totally fine with keeping 1 general table like this

Quoted 3 lines
 | ``NOCOMP``       |   -   | RW  |  ....
 +------------------------+-----+-------+-------------------------------------------------------+
  | ``COMPR``          | RO | RW  | Compressed file flag.

and have the table with only the conversions by itself (like in this patch).)

We are talking past each other.

I didn't mean to remove any INFORMATION that you included in your docs, I just proposed a compact format for the same exact information that was in two bloated tables inside one compat table, so I ask again: what information is missing in the table format that I proposed?

Quoted 1 line
Quoted 1 line

| ``NOCOMP`` | - | RW | ....

is a non converted old flag entry

Quoted 1 line
Quoted 1 line

| ``COMPR`` | RO | RW | Compressed file flag.

is a readonly converted flag

Quoted 1 line
Quoted 1 line

| ``BTIME`` | MRW | - | Birth time.

a masks only flag

What information does it not describe that you want another table or more columns to describe?

Thanks,
Amir.

author
#27 lore raw

Re: [PATCH v4 7/7] docs: add fileattr API documentation

Quoted 114 lines · Amir Goldstein
Quoted 91 lines · koraynilay
Quoted 63 lines · Amir Goldstein
Quoted 47 lines · koraynilay
Quoted 5 lines · koraynilay
Quoted 4 lines · Amir Goldstein
Quoted 3 lines · koraynilay
Quoted 1 line · Amir Goldstein

On Wed, Dec 2, 2026 at 10:54 PM koraynilay <[email protected]> wrote:

...

Quoted 39 lines
Quoted 39 lines
Quoted 29 lines
Quoted 11 lines
Quoted 10 lines

+| | ``FS_XFLAG_FILESTREAM`` |
++------------------------+--------------------------------+
+| | ``FS_XFLAG_COWEXTSIZE`` |
++------------------------+--------------------------------+
+| | ``FS_XFLAG_CASENONPRESERVING`` |
++------------------------+--------------------------------+
+| | ``FS_XFLAG_PROJID`` |
++------------------------+--------------------------------+
+| | ``FS_XFLAG_HASATTR`` |
++------------------------+--------------------------------+

Quoted 17 lines

Still very much not happy about this section.
How about merging only some of it with with XFLAGS table above For example:

Quoted 1 line

+| Flag Macro | Hex Value | Xflags mode | Flags mode | Description and Constraints |

...

Quoted 11 lines

+| ``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 (VER1).                         |
+|                                |            |        | Updates require ``init_user_ns``.                     |

++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``FS_XFLAG_COMPR`` | 0x00200000 | RO | RW | Compressed file flag. |
++--------------------------------+------------+--------+-------------------------------------------------------+
+| ``FS_XFLAG_ENCRYPT`` | 0x00400000 | RO | RO | Encrypted file flag (fscrypt). Read-only. |
++--------------------------------+------------+--------+-------------------------------------------------------+

Quoted 1 line

Oh, yeah, the 2 columns for RO/RW are nicer and I can add a column so we

Yes.

Quoted 2 lines

have "Flag Macro" "XFlag Macro" ((I really don't like "Xflag")) and have only 1 table instead of 2?

One table, yes.
Writing all the flags there like this and their hex values:

Quoted 8 lines
Quoted 8 lines
Quoted 1 line

FS_XFLAG_ENCRYPT | FS_XFLAG_ENCRYPT

(Assuming you meant FS_XFLAG_ENCRYPT | FS_ENCRYPT_FL)

Quoted 2 lines

I think it's just silly and has no place in this doc.

Hmm, can you check how it looks? https://koraynilay.neocities.org/vfs-masks/fileattr
(don't mind the unclear writing in the note above it, I haven't read it manually yet)

In my eyes it is ugly, because it looks like a page from a spec, which is not what
this doc is about, but I can get over it if it means so much to you.

How exactly is this:

Quoted 7 lines
 | ``NOCOMP``       |   -   | RW  |  ....
 +------------------------+-----+-------+-------------------------------------------------------+
  | ``COMPR``          | RO | RW  | Compressed file flag.

Less informative than your expanded form?

As a design/overview doc, what is missing to you when you read this?

A way to see, at a glance, which flags already got converted to xflags and which not.
While I like this because it's informative, thinking about it it would be pretty useful for the

Quoted 4 lines

Please document the rule that a filesystem should accept masks on set only after all of its writable FS_*_FL flags have xflag counterparts, because FS_IOC_SETFLAGS of a flag without an xflag will fail with EOPNOTSUPP. Until then, use get_only_masks.

part from the first email, so developers can see how many of their flags need to be converted and decide if it's worth the effort or not.

This way they don't have to grep around and still not be 100% sure (or they just make their LLM generate a table like this).

Quoted 1 line

As a design/overview doc

Personally I don't mind if it's in this exact doc or not, but if not, where would we put it?

Quoted 7 lines

(I am also totally fine with keeping 1 general table like this

Quoted 3 lines
 | ``NOCOMP``       |   -   | RW  |  ....
 +------------------------+-----+-------+-------------------------------------------------------+
  | ``COMPR``          | RO | RW  | Compressed file flag.

and have the table with only the conversions by itself (like in this patch).)

We are talking past each other.

I didn't mean to remove any INFORMATION that you included in your docs, I just proposed a compact format for the same exact information that was in two bloated tables inside one compat table, so I ask again: what information is missing in the table format that I proposed?

Quoted 1 line
Quoted 1 line

| ``NOCOMP`` | - | RW | ....

is a non converted old flag entry

Quoted 1 line
Quoted 1 line

| ``COMPR`` | RO | RW | Compressed file flag.

Quoted 8 lines

is a readonly converted flag

Quoted 1 line
Quoted 1 line

| ``BTIME`` | MRW | - | Birth time.

a masks only flag

What information does it not describe that you want another table or more columns to describe?

Ah, I'm sorry, yeah I understand now, that makes sense. Basically it's "concept" "xflag support" "flag support" and if someone wants to see the actual macros they can just grep the concept string and find both, right?

Thanks,
koraynilay

Quoted 2 lines

Thanks,
Amir.

author
#28 lore raw

Re: [PATCH v4 7/7] docs: add fileattr API documentation

Quoted 2 lines · koraynilay
Quoted 1 line · Amir Goldstein

On Fri, Dec 4, 2026 at 12:37 AM koraynilay <[email protected]> wrote:

...

Quoted 21 lines
Quoted 21 lines

We are talking past each other.

I didn't mean to remove any INFORMATION that you included in your docs, I just proposed a compact format for the same exact information that was in two bloated tables inside one compat table, so I ask again: what information is missing in the table format that I proposed?

Quoted 1 line
Quoted 1 line

| ``NOCOMP`` | - | RW | ....

is a non converted old flag entry

Quoted 1 line
Quoted 1 line

| ``COMPR`` | RO | RW | Compressed file flag.

is a readonly converted flag

Quoted 1 line
Quoted 1 line

| ``BTIME`` | MRW | - | Birth time.

a masks only flag

Quoted 7 lines
Quoted 2 lines

What information does it not describe that you want another table or more columns to describe?

Ah, I'm sorry, yeah I understand now, that makes sense. Basically it's "concept" "xflag support" "flag support" and if someone wants to see the actual macros they can just grep the concept string and find both, right?

Updated: https://koraynilay.neocities.org/vfs-masks/fileattr

What do you think?

Thanks,
koraynilay

Quoted 2 lines
Quoted 2 lines

Thanks,
Amir.

maintainer
#29 lore raw

Re: [PATCH v4 7/7] docs: add fileattr API documentation

Quoted 29 lines · koraynilay
Quoted 2 lines · koraynilay
Quoted 1 line · Amir Goldstein

On Fri, Dec 4, 2026 at 12:37 AM koraynilay <[email protected]> wrote:

...

Quoted 24 lines
Quoted 23 lines

We are talking past each other.

I didn't mean to remove any INFORMATION that you included in your docs, I just proposed a compact format for the same exact information that was in two bloated tables inside one compat table, so I ask again: what information is missing in the table format that I proposed?

Quoted 1 line
Quoted 1 line

| ``NOCOMP`` | - | RW | ....

is a non converted old flag entry

Quoted 1 line
Quoted 1 line

| ``COMPR`` | RO | RW | Compressed file flag.

is a readonly converted flag

Quoted 1 line
Quoted 1 line

| ``BTIME`` | MRW | - | Birth time.

a masks only flag

What information does it not describe that you want another table or more columns to describe?

Quoted 9 lines
Quoted 4 lines

Ah, I'm sorry, yeah I understand now, that makes sense. Basically it's "concept" "xflag support" "flag support" and if someone wants to see the actual macros they can just grep the concept string and find both, right?

Updated: https://koraynilay.neocities.org/vfs-masks/fileattr

What do you think?

Awsome!

Things to improve:

1. The order (by bit order) makes no sense.
I suggest to order similar to your seconds table:
Start with BOTH, which are likely the most important, followed by XFLAGS only, followed by FLAGS only. Those are arguably the legacy least important and when a legacy flag is converted to an XFLAGS it goes up the list because it is obviously important.

2. MRW: Read-write only with masks.
I know I suggested the name, but now it looks a bit like RMW... It would read better as RWM (Read-Write only with Masks) and would look better in the legend.
I am indecisive about the desired internal order in the table for XFLAGS-only vs. MASKS-only (RWM). With PROJINHERIT being the last in the BOTH group, this order also happens to place PROJID just below PROJINHERIT which is a nice outcome

3. Some flags are implemented by a single filesystem, e.g. most of the XFLAGS-only flags are XFS-only and HUGE_FILE/EXTENT/EA_INODE are ext4-only. You already note "(XFS)" for REALTIME and FILESTREAM; please do that consistently for all single-fs flags, so readers know which flags are generic and which are fs-specific.

4. FS_RESERVED_FL is reserved for e2fsprogs, it is not a file attribute and has no place in this table.
The "Reserved for ext3/ext4" comments in uapi fs.h are historical bit allocations, not descriptions. INLINE_DATA, HUGE_FILE and JOURNAL_DATA are real attributes, describe what they mean. EOFBLOCKS has no in-tree user, so it can be dropped or marked unused.

Thanks,
Amir.

author
#30 lore raw

Re: [PATCH v4 7/7] docs: add fileattr API documentation

Quoted 35 lines · Amir Goldstein
Quoted 34 lines · koraynilay
Quoted 2 lines · koraynilay
Quoted 1 line · Amir Goldstein

On Fri, Dec 4, 2026 at 12:37 AM koraynilay <[email protected]> wrote:

...

Quoted 28 lines
Quoted 15 lines

We are talking past each other.

I didn't mean to remove any INFORMATION that you included in your docs, I just proposed a compact format for the same exact information that was in two bloated tables inside one compat table, so I ask again: what information is missing in the table format that I proposed?

Quoted 1 line
Quoted 1 line

| ``NOCOMP`` | - | RW | ....

is a non converted old flag entry

Quoted 1 line
Quoted 1 line

| ``COMPR`` | RO | RW | Compressed file flag.

Quoted 8 lines

is a readonly converted flag

Quoted 1 line
Quoted 1 line

| ``BTIME`` | MRW | - | Birth time.

a masks only flag

What information does it not describe that you want another table or more columns to describe?

Ah, I'm sorry, yeah I understand now, that makes sense. Basically it's "concept" "xflag support" "flag support" and if someone wants to see the actual macros they can just grep the concept string and find both, right?

Quoted 8 lines
Quoted 4 lines

Updated: https://koraynilay.neocities.org/vfs-masks/fileattr

What do you think?

Awsome!

Things to improve:

Updated: https://koraynilay.neocities.org/vfs-masks/fileattr

Quoted 8 lines

1. The order (by bit order) makes no sense.
I suggest to order similar to your seconds table:
Start with BOTH, which are likely the most important, followed by XFLAGS only, followed by FLAGS only. Those are arguably the legacy least important and when a legacy flag is converted to an XFLAGS it goes up the list because it is obviously important.

Added the requirement to move the line up in the "Adding new flags" section.

Quoted 8 lines

2. MRW: Read-write only with masks.
I know I suggested the name, but now it looks a bit like RMW... It would read better as RWM (Read-Write only with Masks) and would look better in the legend.
I am indecisive about the desired internal order in the table for XFLAGS-only vs. MASKS-only (RWM). With PROJINHERIT being the last in the BOTH group, this order also happens to place PROJID just below PROJINHERIT which is a nice outcome

I used RW/M since IMO RWM reads even more like RMW than MRW.

Quoted 5 lines

3. Some flags are implemented by a single filesystem, e.g. most of the XFLAGS-only flags are XFS-only and HUGE_FILE/EXTENT/EA_INODE are ext4-only. You already note "(XFS)" for REALTIME and FILESTREAM; please do that consistently for all single-fs flags, so readers know which flags are generic and which are fs-specific.

Added the (filesystem).

Quoted 6 lines

4. FS_RESERVED_FL is reserved for e2fsprogs, it is not a file attribute and has no place in this table.
The "Reserved for ext3/ext4" comments in uapi fs.h are historical bit allocations, not descriptions. INLINE_DATA, HUGE_FILE and JOURNAL_DATA are real attributes, describe what they mean. EOFBLOCKS has no in-tree user, so it can be dropped or marked unused.

Removed these.

Thanks,
koraynilay

Quoted 2 lines

Thanks,
Amir.

maintainer
#31 lore raw
In reply to koraynilay
Quoted 46 lines · koraynilay

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.

The series extends the fileattr API with xflags masks by introducing:
- fa_xflags_{rw,ro}_mask fields to struct file_attr
- fsx_xflags_{rw,ro}_mask fields and has_masks / use_masks / get_only_masks selectors 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

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.

The series 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 FSX_FSXATTR_MASK and defined as part of the new FSX_FILEATTR_MASK group of xflags that will be used moving forward for any new xflag (like the one proposed in [2]).

In addition, FS_XFLAG_COMPR and FS_XFLAG_ENCRYPT are added (as FS_COMPR_FL and FS_ENCRYPT_FL counterparts) for fs/ntfs, which is converted to the new mask API as an example. Old programs will also get these new flags, which should be fine since xflags were made to be expandable.

Documentation/filesystems/fileattr.rst explains how this API can be used by both userspace and filesystems along with 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.

Quoted 7 lines

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]. A few of them can be fixed with the robot-proposed simple diff at [10], but I haven't investigated much why it works (as I also don't really understand well how overlayfs works yet).

You can ask me ;)

These bugs are real. ovl_fileattr_get() reports the real layer's masks as its own, so overlayfs gets masked set requests, but ovl_set_protattr() treats fa->flags as the full set of flags. For example, chattr +d on an append-only file would clear the append-only flag.

This robot fix [10] is correct: overlayfs does not support masks on set. Please post it as an overlayfs patch in this series, before converting ntfs of course.

Thanks,
Amir.

author
#32 lore raw
Quoted 55 lines · Amir Goldstein
Quoted 46 lines · koraynilay

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.

The series extends the fileattr API with xflags masks by introducing:
- fa_xflags_{rw,ro}_mask fields to struct file_attr
- fsx_xflags_{rw,ro}_mask fields and has_masks / use_masks / get_only_masks selectors 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

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.

The series 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 FSX_FSXATTR_MASK and defined as part of the new FSX_FILEATTR_MASK group of xflags that will be used moving forward for any new xflag (like the one proposed in [2]).

In addition, FS_XFLAG_COMPR and FS_XFLAG_ENCRYPT are added (as FS_COMPR_FL and FS_ENCRYPT_FL counterparts) for fs/ntfs, which is converted to the new mask API as an example. Old programs will also get these new flags, which should be fine since xflags were made to be expandable.

Documentation/filesystems/fileattr.rst explains how this API can be used by both userspace and filesystems along with 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.

Quoted 7 lines

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]. A few of them can be fixed with the robot-proposed simple diff at [10], but I haven't investigated much why it works (as I also don't really understand well how overlayfs works yet).

Quoted 1 line

You can ask me ;)

One of the reasons I added the robot report and fix ;)

Quoted 4 lines

These bugs are real. ovl_fileattr_get() reports the real layer's masks as its own, so overlayfs gets masked set requests, but ovl_set_protattr() treats fa->flags as the full set of flags. For example, chattr +d on an append-only file would clear the append-only flag.

Ah ok, I'm gonna have to understand better how overlayfs works though, right now this basically just goes over my head lol.

Quoted 1 line

This robot fix [10] is correct:

Nice.

Quoted 3 lines

overlayfs does not support masks on set.
Please post it as an overlayfs patch in this series, before converting ntfs of course.

Ah, ok yeah that makes more sense (I debated whether to include it as a patch or not, but I would've added it at the end).

Thanks,
koraynilay

Quoted 2 lines

Thanks,
Amir.