Switch from using if (!err) { ... } to the cleaner if (err) goto out; in the main body of vfs_fileattr_set(). Reviewed-by: Andrey Albershteyn Reviewed-by: Amir Goldstein Signed-off-by: koraynilay --- fs/file_attr.c | 45 +++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/fs/file_attr.c b/fs/file_attr.c index 3b249a0a6b36..0f2975c9b7bc 100644 --- a/fs/file_attr.c +++ b/fs/file_attr.c @@ -285,30 +285,31 @@ int vfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry, inode_lock(inode); err = vfs_fileattr_get(dentry, &old_ma); - if (!err) { - /* initialize missing bits from old_ma */ - if (fa->flags_valid) { - fa->fsx_xflags |= old_ma.fsx_xflags & ~FS_XFLAG_COMMON; - fa->fsx_extsize = old_ma.fsx_extsize; - fa->fsx_nextents = old_ma.fsx_nextents; - fa->fsx_projid = old_ma.fsx_projid; - fa->fsx_cowextsize = old_ma.fsx_cowextsize; - } else { - fa->flags |= old_ma.flags & ~FS_COMMON_FL; - } - - err = fileattr_set_prepare(inode, &old_ma, fa); - if (err) - goto out; - err = security_inode_file_setattr(dentry, fa); - if (err) - goto out; - err = inode->i_op->fileattr_set(idmap, dentry, fa); - if (err) - goto out; - fsnotify_xattr(dentry); + if (err) + goto out; + + /* initialize missing bits from old_ma */ + if (fa->flags_valid) { + fa->fsx_xflags |= old_ma.fsx_xflags & ~FS_XFLAG_COMMON; + fa->fsx_extsize = old_ma.fsx_extsize; + fa->fsx_nextents = old_ma.fsx_nextents; + fa->fsx_projid = old_ma.fsx_projid; + fa->fsx_cowextsize = old_ma.fsx_cowextsize; + } else { + fa->flags |= old_ma.flags & ~FS_COMMON_FL; } + err = fileattr_set_prepare(inode, &old_ma, fa); + if (err) + goto out; + err = security_inode_file_setattr(dentry, fa); + if (err) + goto out; + err = inode->i_op->fileattr_set(idmap, dentry, fa); + if (err) + goto out; + fsnotify_xattr(dentry); + out: inode_unlock(inode); return err; -- 2.56.0 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. Signed-off-by: koraynilay --- 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(-) diff --git a/fs/file_attr.c b/fs/file_attr.c index 0f2975c9b7bc..226ee34fde2d 100644 --- a/fs/file_attr.c +++ b/fs/file_attr.c @@ -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; diff --git a/fs/overlayfs/copy_up.c b/fs/overlayfs/copy_up.c index c147ad155b79..20f369863faf 100644 --- a/fs/overlayfs/copy_up.c +++ b/fs/overlayfs/copy_up.c @@ -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); diff --git a/fs/overlayfs/inode.c b/fs/overlayfs/inode.c index f1c6df39d0ae..92df4f5c071e 100644 --- a/fs/overlayfs/inode.c +++ b/fs/overlayfs/inode.c @@ -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; diff --git a/include/linux/fileattr.h b/include/linux/fileattr.h index 09e32b84e02a..c26f70aaa3b8 100644 --- a/include/linux/fileattr.h +++ b/include/linux/fileattr.h @@ -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); } -- 2.56.0 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. Signed-off-by: koraynilay --- 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(-) diff --git a/fs/file_attr.c b/fs/file_attr.c index 226ee34fde2d..3b3e84226f71 100644 --- a/fs/file_attr.c +++ b/fs/file_attr.c @@ -74,6 +74,22 @@ void fileattr_fill_flags(struct file_kattr *fa, u32 flags) } EXPORT_SYMBOL(fileattr_fill_flags); +static u32 flags_to_xflags(u32 flags) +{ + struct file_kattr tmp = {}; + + fileattr_fill_flags(&tmp, flags); + return tmp.fsx_xflags; +} + +static u32 xflags_to_flags(u32 xflags) +{ + struct file_kattr tmp = {}; + + fileattr_fill_xflags(&tmp, xflags); + return tmp.flags; +} + /** * vfs_fileattr_get - retrieve miscellaneous file attributes * @dentry: the object to retrieve from @@ -95,20 +111,35 @@ int vfs_fileattr_get(struct dentry *dentry, struct file_kattr *fa) if (error) return error; - return inode->i_op->fileattr_get(dentry, fa); + error = inode->i_op->fileattr_get(dentry, fa); + if (error) + return error; + + if (fa->has_masks) { + fa->fsx_xflags_rw_mask &= 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), diff --git a/include/linux/fileattr.h b/include/linux/fileattr.h index c26f70aaa3b8..4db19556981a 100644 --- a/include/linux/fileattr.h +++ b/include/linux/fileattr.h @@ -3,6 +3,11 @@ #ifndef _LINUX_FILEATTR_H #define _LINUX_FILEATTR_H +/* Version UAPI's struct file_attr based on size */ +#define FILE_ATTR_SIZE_VER0 24 +#define FILE_ATTR_SIZE_VER1 40 +#define FILE_ATTR_SIZE_LATEST FILE_ATTR_SIZE_VER1 + /* Flags shared betwen flags/xflags */ #define FS_COMMON_FL \ (FS_SYNC_FL | FS_IMMUTABLE_FL | FS_APPEND_FL | \ @@ -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); diff --git a/include/uapi/linux/fcntl.h b/include/uapi/linux/fcntl.h index faa63d0b60d2..7347efb3df3c 100644 --- a/include/uapi/linux/fcntl.h +++ b/include/uapi/linux/fcntl.h @@ -191,4 +191,10 @@ struct delegation { #define AT_EXECVE_CHECK 0x10000 /* Only perform a check if execution would be allowed. */ +/* Flags for file_getattr(2) / file_setattr(2). */ +#define AT_FILEATTR_MASKS 0x001 /* Advertise knowing and being able to + handle the struct file_attr fields + fa_xflags_rw_mask and + fa_xflags_ro_mask */ + #endif /* _UAPI_LINUX_FCNTL_H */ diff --git a/include/uapi/linux/fs.h b/include/uapi/linux/fs.h index a13c7b32a1bd..337bb00b0e64 100644 --- a/include/uapi/linux/fs.h +++ b/include/uapi/linux/fs.h @@ -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 ` 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 ` for details. + * @fa_xflags_ro_mask: Bit mask of read-only xflags supported by the filesystem + * (out). See :ref:`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 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. Link: https://lore.kernel.org/20261122000856.536902-1-koray.fra@gmail.com/ [1] Signed-off-by: koraynilay --- fs/file_attr.c | 8 ++++++++ include/linux/fileattr.h | 18 +++++++++++++----- include/uapi/linux/fs.h | 2 ++ 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/fs/file_attr.c b/fs/file_attr.c index 3b3e84226f71..5e8979e2bb44 100644 --- a/fs/file_attr.c +++ b/fs/file_attr.c @@ -39,6 +39,10 @@ void fileattr_fill_xflags(struct file_kattr *fa, u32 xflags) fa->flags |= FS_VERITY_FL; if (fa->fsx_xflags & FS_XFLAG_CASEFOLD) fa->flags |= FS_CASEFOLD_FL; + if (fa->fsx_xflags & FS_XFLAG_COMPR) + fa->flags |= FS_COMPR_FL; + if (fa->fsx_xflags & FS_XFLAG_ENCRYPT) + fa->flags |= FS_ENCRYPT_FL; } EXPORT_SYMBOL(fileattr_fill_xflags); @@ -71,6 +75,10 @@ void fileattr_fill_flags(struct file_kattr *fa, u32 flags) fa->fsx_xflags |= FS_XFLAG_VERITY; if (fa->flags & FS_CASEFOLD_FL) fa->fsx_xflags |= FS_XFLAG_CASEFOLD; + if (fa->flags & FS_COMPR_FL) + fa->fsx_xflags |= FS_XFLAG_COMPR; + if (fa->flags & FS_ENCRYPT_FL) + fa->fsx_xflags |= FS_XFLAG_ENCRYPT; } EXPORT_SYMBOL(fileattr_fill_flags); diff --git a/include/linux/fileattr.h b/include/linux/fileattr.h index 4db19556981a..898511c04f3f 100644 --- a/include/linux/fileattr.h +++ b/include/linux/fileattr.h @@ -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 \ diff --git a/include/uapi/linux/fs.h b/include/uapi/linux/fs.h index 337bb00b0e64..54d970dd30dc 100644 --- a/include/uapi/linux/fs.h +++ b/include/uapi/linux/fs.h @@ -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 */ -- 2.56.0 Switch ntfs_fileattr_get() and ntfs_fileattr_set() to use FS_XFLAG_* xflags instead of FS_*_FL flags. Signed-off-by: koraynilay --- fs/ntfs/file.c | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/fs/ntfs/file.c b/fs/ntfs/file.c index 50be49f4c913..ca088e7d636b 100644 --- a/fs/ntfs/file.c +++ b/fs/ntfs/file.c @@ -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; } -- 2.56.0 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(). Signed-off-by: koraynilay --- fs/ntfs/file.c | 72 +++++++++++++++++++++++----------------- include/linux/fileattr.h | 31 +++++++++++++++++ 2 files changed, 72 insertions(+), 31 deletions(-) diff --git a/fs/ntfs/file.c b/fs/ntfs/file.c index ca088e7d636b..44c025c7280a 100644 --- a/fs/ntfs/file.c +++ b/fs/ntfs/file.c @@ -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; diff --git a/include/linux/fileattr.h b/include/linux/fileattr.h index 898511c04f3f..2f9b9fed1600 100644 --- a/include/linux/fileattr.h +++ b/include/linux/fileattr.h @@ -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 -- 2.56.0 Add documentation for the fileattr API. Signed-off-by: koraynilay --- 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 diff --git a/Documentation/filesystems/fileattr.rst b/Documentation/filesystems/fileattr.rst new file mode 100644 index 000000000000..ee47ae2c9447 --- /dev/null +++ b/Documentation/filesystems/fileattr.rst @@ -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 ` 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 `. + +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 ````: + +.. 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. diff --git a/Documentation/filesystems/index.rst b/Documentation/filesystems/index.rst index a556084c8858..71a1ae852e5b 100644 --- a/Documentation/filesystems/index.rst +++ b/Documentation/filesystems/index.rst @@ -28,6 +28,7 @@ algorithms work. dnotify fiemap files + fileattr locks mmap_prepare multigrain-ts diff --git a/Documentation/filesystems/vfs.rst b/Documentation/filesystems/vfs.rst index 9ab182dbbd73..01fddb2dacf3 100644 --- a/Documentation/filesystems/vfs.rst +++ b/Documentation/filesystems/vfs.rst @@ -621,16 +621,19 @@ otherwise noted. the end. ``fileattr_get`` - called on ioctl(FS_IOC_GETFLAGS) and ioctl(FS_IOC_FSGETXATTR) to - retrieve miscellaneous file flags and attributes. Also called - before the relevant SET operation to check what is being changed - (in this case with i_rwsem locked exclusive). If unset, then - fall back to f_op->ioctl(). + called on ioctl(FS_IOC_GETFLAGS), ioctl(FS_IOC_FSGETXATTR) and + file_getattr(2) to retrieve miscellaneous file flags and attributes + (see Documentation/filesystems/fileattr.rst). Also called before the + relevant SET operation to check what is being changed (in this case + with i_rwsem locked exclusive). If unset, then fall back to + f_op->ioctl(). ``fileattr_set`` - called on ioctl(FS_IOC_SETFLAGS) and ioctl(FS_IOC_FSSETXATTR) to - change miscellaneous file flags and attributes. Callers hold - i_rwsem exclusive. If unset, then fall back to f_op->ioctl(). + called on ioctl(FS_IOC_SETFLAGS), ioctl(FS_IOC_FSSETXATTR) and + file_setattr(2) to change miscellaneous file flags and attributes (see + Documentation/filesystems/fileattr.rst). Callers hold i_rwsem + exclusive. If unset, then fall back to f_op->ioctl(). + ``get_offset_ctx`` called to get the offset context for a directory inode. A filesystem must define this operation to use -- 2.56.0