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 fsx_xflags; * for value xflags: it sets the xflag bit to 1 in fsx_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 should get rejected with -EINVAL). To achieve this, add fsx_xflags_{rw,ro}_mask fields and {has,use}_masks selectors to struct file_kattr, so we can know when a user of this API (userspace or filesystem) supports them. To eventually make both non-masks and masks programs interact correctly with both non-masks and masks filesystems, add: - fileattr_set_{from,to}_masks(): convert between non-masks read-modify-write API and masks API; - fileattr_set_convert(): handles calling the above helpers as needed, then calls fileattr_set_preserve() if neither userspace nor filesystem has masks. Note that this is only the initial internal VFS implementation: it doesn't include any changes to UAPI yet, nor does it include FS_XFLAG_PROJID, so with only this patch it's not possible to set the project ID using masks. Signed-off-by: koraynilay --- fs/file_attr.c | 197 +++++++++++++++++++++++++++++++++++++-- include/linux/fileattr.h | 6 ++ 2 files changed, 197 insertions(+), 6 deletions(-) diff --git a/fs/file_attr.c b/fs/file_attr.c index c1a0272f1a83..2cc06b5117f8 100644 --- a/fs/file_attr.c +++ b/fs/file_attr.c @@ -74,6 +74,14 @@ 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 = {}; @@ -103,7 +111,17 @@ 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_FSXATTR_MASK; + fa->fsx_xflags_ro_mask &= FSX_FSXATTR_MASK; + } + fa->fsx_xflags &= FSX_FSXATTR_MASK; + + return 0; } EXPORT_SYMBOL(vfs_fileattr_get); @@ -189,16 +207,165 @@ static void fileattr_set_preserve(struct file_kattr *fa, 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 */ + /* FS_IOC_FSSETXATTR or file_setattr without masks */ fa->flags |= old_ma->flags & ~FS_COMMON_FL; } } +/* + * 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 & FSX_EXTSIZE_MASK; + + if (fa->fsx_projid != old_ma->fsx_projid) + return -EOPNOTSUPP; + + 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; + + if (rw_user_mask & ~(FSX_FSXATTR_MASK & ~FSX_RDONLY_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_MASK & ~FSX_RDONLY_MASK; + + new_flags = xflags_to_flags(new_xflags); + new_flags |= old_ma->flags & ~FS_COMMON_FL; + + /* User didn't turn on any flag for extsize. */ + if (!(fa->fsx_xflags & FSX_EXTSIZE_MASK)) { + if ((rw_user_mask & FSX_EXTSIZE_MASK) && + !(new_xflags & FSX_EXTSIZE_MASK)) { + /* + * 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; + } + } + + fa->fsx_projid = old_ma->fsx_projid; + + if (!(rw_user_mask & FS_XFLAG_COWEXTSIZE)) + fa->fsx_cowextsize = old_ma->fsx_cowextsize; + else if (!(new_xflags & FS_XFLAG_COWEXTSIZE)) + 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; + + 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. */ + fileattr_set_preserve(fa, old_ma); + return 0; +} + /* * Generic function to check FS_IOC_FSSETXATTR/FS_IOC_SETFLAGS values and reject * any invalid configurations. @@ -209,9 +376,16 @@ static int fileattr_set_prepare(struct inode *inode, const struct file_kattr *old_ma, struct file_kattr *fa) { - u32 changed = (fa->fsx_xflags ^ old_ma->fsx_xflags) & FSX_FSXATTR_MASK; + u32 rw_user_mask = fa->has_masks ? + fa->fsx_xflags_rw_mask : FSX_FSXATTR_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. @@ -231,7 +405,7 @@ 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 (old_ma->fsx_projid != fa->fsx_projid) { + if (!fa->has_masks && 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; @@ -309,13 +483,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 */ - fileattr_set_preserve(fa, &old_ma); + err = fileattr_set_convert(fa, &old_ma); + if (err) + goto out; err = fileattr_set_prepare(inode, &old_ma, fa); if (err) diff --git a/include/linux/fileattr.h b/include/linux/fileattr.h index d9e89e171f3e..ef835436550d 100644 --- a/include/linux/fileattr.h +++ b/include/linux/fileattr.h @@ -61,6 +61,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) */ @@ -68,6 +71,9 @@ 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; /* in-kernel callers need to set this to use + masks (set) */ }; int copy_fsxattr_to_user(const struct file_kattr *fa, struct fsxattr __user *ufa); -- 2.56.1.dirty