Add f*_xflags_{rw,ro}_mask flags to the fileattr API
Subsystems (6)
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.
The first patch is a preparatory patch so that the code in the patch after can be formatted better.
The second patch is the actual implementation, which introduces:
- fa_xflags_{rw,ro}_mask fields to struct file_attr
- fsx_xflags_{rw,ro}_mask fields and has_masks selector to struct file_kattr
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
(ro_mask for userspace was also proposed as a "verification mask"[3] but I left out that semantic to keep this patch series more contained, and can be added later if needed.)
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.
Patch 2/3 also introduces the missing FS_XFLAG_PROJID flag so that programs like chattr can leverage the new mask API. FS_XFLAG_PROJID is kept out of the legacy FS_XFLAG_MASK and so it's defined as part of a new FS_FILE_ATTR_XFLAGS_MASK group of xflags that will be used moving forward for any new xflag (like the one proposed in [2]).
Patch 3/3 converts fs/ntfs to this new API, to show how it can benefit filesystem drivers too.
The series has been tested across NTFS, ext4 (with project quotas), XFS (project quotas and extent size hints), and Btrfs.
Thanks,
koraynilay
[1]: https://lore.kernel.org/CAOQ4uxg8aVcWyNiFDk8djdn-M3xJcu_7-kRxVq9jjFsbcGp1ew@mail.gmail.com/
[2]: https://lore.kernel.org/CAOQ4uxjVQ9cjV=_jBjQtNg9zCuWUEJT9KNYMKV+1jwBkHen5OA@mail.gmail.com/
[3]: https://lore.kernel.org/CAOQ4uxjCN-ekd18R-soD8tkGFnXjK0S1VdSNhq9b3Ev6y8K9jQ@mail.gmail.com/
koraynilay (3):
vfs: use goto out on error in vfs_fileattr_set()
vfs: add xflags mask to fileattr API
ntfs: switch to the new xflags fileattr API
fs/file_attr.c | 179 ++++++++++++++++++++++++++++++++------- fs/ntfs/file.c | 63 +++++++------- include/linux/fileattr.h | 13 ++- include/uapi/linux/fs.h | 9 +- 4 files changed, 197 insertions(+), 67 deletions(-)
base-commit: 3857587d9d6fa44d40d1c73bde6a39f6f7a5216c
prerequisite-patch-id: 7af19117a27af492e3e525774310fb3a76700ffa
Signature · 1 line
2.56.0
PATCH 1/3 vfs: use goto out on error in vfs_fileattr_set()
Switch from using if (!err) { ... } to the cleaner if (err) goto out; in the main body of vfs_fileattr_set().
--- fs/file_attr.c | 45 +++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 22 deletions(-)
fs/file_attr.c +23 -22
@@ -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
PATCH 2/3 vfs: add xflags mask to fileattr API
In reply to koraynilayAdd fa_xflags_rw_mask and fa_xflags_ro_mask to struct file_attr and equivalent masks to struct file_kattr.
Also add has_masks selector to struct file_kattr so we can know when a user of this API (userspace or filesystem) supports them.
This lets filesystems advertise their support for specific xflags, like statx.stx_attributes_mask does, and userspace can specify only which xflags it wants to change without needing a RMW cycle:
- on GET: each filesystem that wants to support this new API fills the masks according to its supported 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;
- on SET: userspace fills the rw_mask with the bits of the xflags it wants to change, then:
* for boolean xflags: it provides the xflag value as a bit in fa_xflags
* for value xflags: it sets the xflag bit to 1 in fa_xflags and then provides the value in the struct field; if the xflag bit is set to 0 instead, this informs the kernel to unset/clear that value.
Since fa_xflags_rw_mask means "I want to change the xflags specified in here", if userspace specifies fa_xflags_rw_mask = 0, only check for permissions and fileattr support without checking any xflags.
Behaviour of older programs that already use file_[gs]etattr() is preserved by checking against FILE_ATTR_SIZE_VER*.
Also add the missing FS_XFLAG_PROJID xflag, to allow userspace to set the projid using file_[gs]etattr() by leveraging the new masks.
Finally, in order to prevent the legacy FS_IOC_FSSETXATTR from accepting FS_XFLAG_PROJID, add a FS_FILE_ATTR_XFLAGS_MASK to support new xflags and use it to validate/clamp both the _xflags fields and the masks.
--- fs/file_attr.c | 128 ++++++++++++++++++++++++++++++++++++--- include/linux/fileattr.h | 7 +++ include/uapi/linux/fs.h | 7 ++- 3 files changed, 131 insertions(+), 11 deletions(-)
fs/file_attr.c +118 -10
@@ -102,13 +102,20 @@ EXPORT_SYMBOL(vfs_fileattr_get);static void fileattr_to_file_attr (const struct file_kattr *fa,struct file_attr *fattr) { -__u32 mask =FS_XFLAGS_MASK ; -memset (fattr,0 ,sizeof (struct file_attr )); - fattr->fa_xflags = fa->fsx_xflags & mask; +/* Pass the masks to userspace if underlying fs uses them. */ +if (fa->has_masks) { + fattr->fa_xflags_rw_mask = fa->fsx_xflags_rw_mask & +FS_FILE_ATTR_XFLAGS_MASK ; + fattr->fa_xflags_ro_mask = fa->fsx_xflags_ro_mask & +FS_FILE_ATTR_XFLAGS_MASK ; + } + fattr->fa_xflags = fa->fsx_xflags &FS_FILE_ATTR_XFLAGS_MASK ; 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,12 +148,30 @@ 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 rw_mask = fa->has_masks ? fattr->fa_xflags_rw_mask :FS_XFLAGS_MASK ; -if (fattr->fa_xflags & ~mask) +/* +* No need to check for has_masks here, as fa_xflags_rw_mask is 0 in +* VER0 mode. +*/ +if (fattr->fa_xflags_rw_mask & ~FS_FILE_ATTR_XFLAGS_MASK )return -EINVAL ; -fileattr_fill_xflags (fa, fattr->fa_xflags & ~FS_XFLAG_RDONLY_MASK ); +if (fattr->fa_xflags & ~rw_mask) +return -EINVAL ; + +/* +* Filter with ~FS_XFLAG_RDONLY_MASK only when in VER0 mode, so +* filtering with the actual filesystem read-only xflags can be done in +* vfs_fileattr_set() once we have the fsx_xflags_ro_mask from the fs. +*/ +if (fa->has_masks) { +fileattr_fill_xflags (fa, fattr->fa_xflags); + fa->fsx_xflags_rw_mask = fattr->fa_xflags_rw_mask; + }else { +fileattr_fill_xflags (fa, + fattr->fa_xflags & ~FS_XFLAG_RDONLY_MASK ); + } fa->fsx_extsize = fattr->fa_extsize; fa->fsx_projid = fattr->fa_projid; fa->fsx_cowextsize = fattr->fa_cowextsize;
@@ -187,6 +212,14 @@ static int fileattr_set_prepare(struct inode *inode, {int err; +/* +* If the fs supports the fsx_xflags_{rw,ro}_mask API, fail when the +* requested rw flags are not in the supported ones for that fs +*/ +if (old_ma->has_masks && + (fa->fsx_xflags_rw_mask & ~old_ma->fsx_xflags_rw_mask)) +return -EOPNOTSUPP ; +/* * The IMMUTABLE and APPEND_ONLY flags can only be changed by * the relevant capability.
@@ -211,6 +244,11 @@ static int fileattr_set_prepare(struct inode *inode, FS_XFLAG_PROJINHERIT)return -EINVAL ; }else { +/* If user wants to unset projid, set it to 0 */ +if ((fa->fsx_xflags_rw_mask &FS_XFLAG_PROJID ) && + ((fa->fsx_xflags &FS_XFLAG_PROJID ) ==0 )) + fa->fsx_projid =0 ; +/* * Caller is allowed to change the project ID. If it is being * changed, make sure that the new value is valid.
@@ -250,6 +288,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 ; }
@@ -275,6 +315,8 @@ int vfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry, {struct inode *inode =d_inode (dentry);struct file_kattr old_ma = {}; +u32 rw_user_mask = fa->fsx_xflags_rw_mask; +u32 rw_fs_mask =0 , ro_fs_mask =0 ;int err;if (!inode->i_op->fileattr_set)
@@ -288,15 +330,53 @@ int vfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry,if (err) goto out; +if (old_ma.has_masks) { + old_ma.fsx_xflags_rw_mask &=FS_FILE_ATTR_XFLAGS_MASK ; + old_ma.fsx_xflags_ro_mask &=FS_FILE_ATTR_XFLAGS_MASK ; + rw_fs_mask = old_ma.fsx_xflags_rw_mask; + ro_fs_mask = old_ma.fsx_xflags_ro_mask; + } + old_ma.fsx_xflags &=FS_FILE_ATTR_XFLAGS_MASK ; +/* initialize missing bits from old_ma */ if (fa->flags_valid) { +/* FS_IOC_SETFLAGS */ + fa->fsx_xflags_rw_mask =0 ; 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 { +/* +* Preserve flags not being set when using FS_IOC_FSSETXATTR or +* file_setattr VER0+. +*/ fa->flags |= old_ma.flags & ~FS_COMMON_FL ; + +/* file_setattr VER1+ */ +if (fa->has_masks) { +/* Preserve old xflags that aren't being set. */ + fa->fsx_xflags |= old_ma.fsx_xflags & ~rw_user_mask; + +/* +* Filter fsx_xflags by removing xflags that are: +* - filesystem-reported read only +* - globally read only +* but keeping xflags that are: +* - filesystem-reported rw, globally ro +*/ + ro_fs_mask |=FS_XFLAG_RDONLY_MASK & ~rw_fs_mask; +fileattr_fill_xflags (fa, fa->fsx_xflags & ~ro_fs_mask); + +if (!(rw_user_mask & + (FS_XFLAG_EXTSIZE |FS_XFLAG_EXTSZINHERIT ))) + fa->fsx_extsize = old_ma.fsx_extsize; +if (!(rw_user_mask &FS_XFLAG_PROJID )) + fa->fsx_projid = old_ma.fsx_projid; +if (!(rw_user_mask &FS_XFLAG_COWEXTSIZE )) + fa->fsx_cowextsize = old_ma.fsx_cowextsize; + } } err =fileattr_set_prepare (inode, &old_ma, fa);
@@ -424,6 +504,14 @@ SYSCALL_DEFINE5(file_getattr, int, dfd, const char __user *, filename,return error;fileattr_to_file_attr (&fa, &fattr); +/* +* Remove xflags added in or after VER1 so userspace programs compiled +* against VER0 don't try to write xflags they don't know about (since +* they do RMW). +*/ +if (usize <FILE_ATTR_SIZE_VER1 ) + fattr.fa_xflags &=FS_XFLAGS_MASK ; + error =copy_struct_to_user (ufattr, usize, &fattr,sizeof (struct file_attr ),NULL );
@@ -460,10 +548,6 @@ 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; -CLASS (filename_maybe_null, name)(filename, at_flags);if (!name && dfd >=0 ) {CLASS (fd, f)(dfd);
@@ -479,6 +563,30 @@ SYSCALL_DEFINE5(file_setattr, int, dfd, const char __user *, filename,return error; } +/* +* Userspace said "I don't want to change any flag", so be it I guess. +* Still check for fileattr support and permissions, also in case this +* is just a probe. +*/ +if (usize >=FILE_ATTR_SIZE_VER1 && fattr.fa_xflags_rw_mask ==0 ) { +struct inode *inode =d_inode (filepath.dentry); + +if (!inode->i_op->fileattr_set) +return -EOPNOTSUPP ; + +if (!inode_owner_or_capable (mnt_idmap (filepath.mnt), inode)) +return -EPERM ; + +return error; + } + +if (usize >=FILE_ATTR_SIZE_VER1 ) + fa.has_masks = true; + + error =file_attr_to_fileattr (&fattr, &fa); +if (error) +return error; + error =mnt_want_write (filepath.mnt);if (!error) { error = vfs_fileattr_set(mnt_idmap(filepath.mnt),
include/linux/fileattr.h +7 -0
@@ -35,6 +35,10 @@ (FS_XFLAG_COMMON |FS_XFLAG_RDONLY_MASK |FS_XFLAG_VALUES_MASK | \FS_XFLAG_DIRONLY_MASK |FS_XFLAG_MISC_MASK ) +/* Flags only for fa_ fields */ +#define FS_FILE_ATTR_XFLAGS_MASK \ + (FS_XFLAGS_MASK | FS_XFLAG_PROJID) +/* * Merged interface for miscellaneous file attributes. 'flags' originates from * ext* and 'fsx_flags' from xfs. There's some overlap between the two, which
@@ -45,6 +49,8 @@ struct file_kattr {u32 flags;/* flags (FS_IOC_GETFLAGS/FS_IOC_SETFLAGS) */ /* struct fsxattr: */ u32 fsx_xflags;/* xflags field value (get/set) */ +u32 fsx_xflags_rw_mask;/* supported writeable xflags (get) / flags to set (set) */ +u32 fsx_xflags_ro_mask;/* supported read-only xflags (get) */ u32 fsx_extsize;/* extsize field value (get/set)*/ u32 fsx_nextents;/* nextents field value (get) */ u32 fsx_projid;/* project identifier (get/set) */
@@ -52,6 +58,7 @@ struct file_kattr {/* selectors: */ bool flags_valid:1; bool fsx_valid:1; + bool has_masks:1; };int copy_fsxattr_to_user (const struct file_kattr *fa,struct fsxattr __user *ufa);
include/uapi/linux/fs.h +6 -1
@@ -238,10 +238,13 @@ 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) */ +__u32 fa_xflags_rw_mask;/* supported writeable xflags (get) / flags to set (set) */ +__u32 fa_xflags_ro_mask;/* supported read-only xflags (get) */ };#define FILE_ATTR_SIZE_VER0 24 -#define FILE_ATTR_SIZE_LATEST FILE_ATTR_SIZE_VER0 +#define FILE_ATTR_SIZE_VER1 32 +#define FILE_ATTR_SIZE_LATEST FILE_ATTR_SIZE_VER1/* * Flags for the fsx_xflags field
@@ -270,6 +273,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
PATCH 3/3 ntfs: switch to the new xflags fileattr API
In reply to koraynilaySwitch ntfs_fileattr_[gs]et() from the legacy FS_*_FL flags to the modern xflags (FS_XFLAG_*) fileattr API with masks.
Introduce FS_XFLAG_COMPR and FS_XFLAG_ENCRYPT and add them to FS_XFLAG_COMMON (and add FS_COMPR_FL and FS_ENCRYPT_FL to FS_COMMON_FL) to be used in this API switch.
--- fs/file_attr.c | 8 +++++ fs/ntfs/file.c | 63 +++++++++++++++++++--------------------- include/linux/fileattr.h | 6 ++-- include/uapi/linux/fs.h | 2 ++ 4 files changed, 44 insertions(+), 35 deletions(-)
fs/file_attr.c +8 -0
@@ -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);
fs/ntfs/file.c +30 -33
112 lines not on this page · raw message
include/linux/fileattr.h +4 -2
@@ -7,12 +7,14 @@#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_COMPR_FL | \ + FS_ENCRYPT_FL)#define FS_XFLAG_COMMON \ (FS_XFLAG_SYNC | FS_XFLAG_IMMUTABLE | FS_XFLAG_APPEND | \ FS_XFLAG_NODUMP | FS_XFLAG_NOATIME | FS_XFLAG_DAX | \ - FS_XFLAG_PROJINHERIT | FS_XFLAG_VERITY) + FS_XFLAG_PROJINHERIT | FS_XFLAG_VERITY | FS_XFLAG_COMPR | \ + FS_XFLAG_ENCRYPT)/* Read-only inode flags */ #define FS_XFLAG_RDONLY_MASK \
include/uapi/linux/fs.h +2 -0
@@ -275,6 +275,8 @@ struct file_attr {#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 */ /* the read-only stuff doesn't really belong here, but any other place is
Signature · 1 line
2.56.0