Add FS_XFLAG_PROJID to the UAPI header and include it in FSX_FILEATTR_MASK so file_[gs]etattr() with AT_FILEATTR_MASKS can advertise, set and clear the project ID using masks, while keeping it out of FS_IOC_FS[GS]ETXATTR and non-AT_FILEATTR_MASKS callers. Add FSX_FSXATTR_SET_MASK to specify xflags that are settable by file_setattr with masks on non-masks filesystems. This macro is then used to validate input xflags in fileattr_set_from_masks(). Signed-off-by: koraynilay --- fs/file_attr.c | 32 ++++++++++++++++++++++++++------ include/linux/fileattr.h | 12 +++++++++++- include/uapi/linux/fs.h | 6 ++++-- 3 files changed, 41 insertions(+), 9 deletions(-) diff --git a/fs/file_attr.c b/fs/file_attr.c index 8b9785447e72..7bfd7651d1d8 100644 --- a/fs/file_attr.c +++ b/fs/file_attr.c @@ -138,6 +138,8 @@ static void fileattr_to_file_attr(const struct file_kattr *fa, 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; } @@ -208,6 +210,13 @@ static int file_attr_to_fileattr(const struct file_attr *fattr, else fa->fsx_extsize = 0; + if (fa->fsx_xflags & FS_XFLAG_PROJID) + fa->fsx_projid = fattr->fa_projid; + else if ((rw_user_mask & FS_XFLAG_PROJID) && fattr->fa_projid) + return -EINVAL; + else + fa->fsx_projid = 0; + if (fa->fsx_xflags & FS_XFLAG_COWEXTSIZE) fa->fsx_cowextsize = fattr->fa_cowextsize; else if ((rw_user_mask & FS_XFLAG_COWEXTSIZE) && fattr->fa_cowextsize) @@ -290,8 +299,11 @@ static int fileattr_set_to_masks(struct file_kattr *fa, 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 (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) @@ -326,14 +338,14 @@ static int fileattr_set_from_masks(struct file_kattr *fa, u32 new_xflags = fa->fsx_xflags; u32 new_flags = 0; - if (rw_user_mask & ~(FSX_FSXATTR_MASK & ~FSX_RDONLY_MASK)) + 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_MASK & ~FSX_RDONLY_MASK; + new_xflags &= FSX_FSXATTR_SET_MASK; new_flags = xflags_to_flags(new_xflags); new_flags |= old_ma->flags & ~FS_COMMON_FL; @@ -357,7 +369,12 @@ static int fileattr_set_from_masks(struct file_kattr *fa, } } - fa->fsx_projid = old_ma->fsx_projid; + if (!(rw_user_mask & FS_XFLAG_PROJID)) + fa->fsx_projid = old_ma->fsx_projid; + else if (!(fa->fsx_xflags & FS_XFLAG_PROJID)) + fa->fsx_projid = 0; + /* Strip PROJID now that we've used it, old format doesn't have it. */ + new_xflags &= ~FS_XFLAG_PROJID; if (!(rw_user_mask & FS_XFLAG_COWEXTSIZE)) fa->fsx_cowextsize = old_ma->fsx_cowextsize; @@ -443,7 +460,8 @@ 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 (!fa->has_masks && old_ma->fsx_projid != fa->fsx_projid) { + 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; @@ -488,6 +506,8 @@ static int fileattr_set_prepare(struct inode *inode, fa->fsx_xflags &= ~FSX_EXTSIZE_MASK; if (fa->fsx_cowextsize == 0) fa->fsx_xflags &= ~FS_XFLAG_COWEXTSIZE; + if (fa->fsx_projid == 0) + fa->fsx_xflags &= ~FS_XFLAG_PROJID; return 0; } diff --git a/include/linux/fileattr.h b/include/linux/fileattr.h index adb586e21fd5..8dcabbb294fa 100644 --- a/include/linux/fileattr.h +++ b/include/linux/fileattr.h @@ -52,9 +52,19 @@ (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) + (FSX_FSXATTR_MASK | FS_XFLAG_PROJID) /* * Merged interface for miscellaneous file attributes. 'flags' originates from diff --git a/include/uapi/linux/fs.h b/include/uapi/linux/fs.h index d402db1a2254..337bb00b0e64 100644 --- a/include/uapi/linux/fs.h +++ b/include/uapi/linux/fs.h @@ -236,8 +236,8 @@ struct fsxattr { * 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. Used by quota systems quotactl(2) to group - * related files. (in/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) @@ -294,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.1.dirty