f2fs: use generic checking function for FS_IOC_FSSETXATTR
Make the f2fs implementation of FS_IOC_FSSETXATTR use the new VFS helper
function vfs_ioc_fssetxattr_check(), and remove the project quota check
since it's now done by the helper function.
This is based on a patch from Darrick Wong, but reworked to apply after
commit 360985573b55 ("f2fs: separate f2fs i_flags from fs_flags and ext4
i_flags").
Originally-from: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: Eric Biggers <ebiggers@google.com>
Reviewed-by: Chao Yu <yuchao0@huawei.com>
Reviewed-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
This commit is contained in:
committed by
Jaegeuk Kim
parent
f62199a95c
commit
e831418555
@@ -2862,52 +2862,32 @@ static inline u32 f2fs_xflags_to_iflags(u32 xflags)
|
||||
return iflags;
|
||||
}
|
||||
|
||||
static void f2fs_fill_fsxattr(struct inode *inode, struct fsxattr *fa)
|
||||
{
|
||||
struct f2fs_inode_info *fi = F2FS_I(inode);
|
||||
|
||||
simple_fill_fsxattr(fa, f2fs_iflags_to_xflags(fi->i_flags));
|
||||
|
||||
if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)))
|
||||
fa->fsx_projid = from_kprojid(&init_user_ns, fi->i_projid);
|
||||
}
|
||||
|
||||
static int f2fs_ioc_fsgetxattr(struct file *filp, unsigned long arg)
|
||||
{
|
||||
struct inode *inode = file_inode(filp);
|
||||
struct f2fs_inode_info *fi = F2FS_I(inode);
|
||||
struct fsxattr fa;
|
||||
|
||||
memset(&fa, 0, sizeof(struct fsxattr));
|
||||
fa.fsx_xflags = f2fs_iflags_to_xflags(fi->i_flags);
|
||||
|
||||
if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)))
|
||||
fa.fsx_projid = (__u32)from_kprojid(&init_user_ns,
|
||||
fi->i_projid);
|
||||
f2fs_fill_fsxattr(inode, &fa);
|
||||
|
||||
if (copy_to_user((struct fsxattr __user *)arg, &fa, sizeof(fa)))
|
||||
return -EFAULT;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int f2fs_ioctl_check_project(struct inode *inode, struct fsxattr *fa)
|
||||
{
|
||||
/*
|
||||
* Project Quota ID state is only allowed to change from within the init
|
||||
* 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)
|
||||
return 0;
|
||||
|
||||
if (__kprojid_val(F2FS_I(inode)->i_projid) != fa->fsx_projid)
|
||||
return -EINVAL;
|
||||
|
||||
if (F2FS_I(inode)->i_flags & F2FS_PROJINHERIT_FL) {
|
||||
if (!(fa->fsx_xflags & FS_XFLAG_PROJINHERIT))
|
||||
return -EINVAL;
|
||||
} else {
|
||||
if (fa->fsx_xflags & FS_XFLAG_PROJINHERIT)
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int f2fs_ioc_fssetxattr(struct file *filp, unsigned long arg)
|
||||
{
|
||||
struct inode *inode = file_inode(filp);
|
||||
struct fsxattr fa;
|
||||
struct fsxattr fa, old_fa;
|
||||
u32 iflags;
|
||||
int err;
|
||||
|
||||
@@ -2930,9 +2910,12 @@ static int f2fs_ioc_fssetxattr(struct file *filp, unsigned long arg)
|
||||
return err;
|
||||
|
||||
inode_lock(inode);
|
||||
err = f2fs_ioctl_check_project(inode, &fa);
|
||||
|
||||
f2fs_fill_fsxattr(inode, &old_fa);
|
||||
err = vfs_ioc_fssetxattr_check(inode, &old_fa, &fa);
|
||||
if (err)
|
||||
goto out;
|
||||
|
||||
err = f2fs_setflags_common(inode, iflags,
|
||||
f2fs_xflags_to_iflags(F2FS_SUPPORTED_XFLAGS));
|
||||
if (err)
|
||||
|
||||
62
fs/inode.c
62
fs/inode.c
@@ -2180,3 +2180,65 @@ int vfs_ioc_setflags_prepare(struct inode *inode, unsigned int oldflags,
|
||||
return 0;
|
||||
}
|
||||
EXPORT_SYMBOL(vfs_ioc_setflags_prepare);
|
||||
|
||||
/*
|
||||
* Generic function to check FS_IOC_FSSETXATTR values and reject any invalid
|
||||
* configurations.
|
||||
*
|
||||
* Note: the caller should be holding i_mutex, or else be sure that they have
|
||||
* exclusive access to the inode structure.
|
||||
*/
|
||||
int vfs_ioc_fssetxattr_check(struct inode *inode, const struct fsxattr *old_fa,
|
||||
struct fsxattr *fa)
|
||||
{
|
||||
/*
|
||||
* Can't modify an immutable/append-only file unless we have
|
||||
* appropriate permission.
|
||||
*/
|
||||
if ((old_fa->fsx_xflags ^ fa->fsx_xflags) &
|
||||
(FS_XFLAG_IMMUTABLE | FS_XFLAG_APPEND) &&
|
||||
!capable(CAP_LINUX_IMMUTABLE))
|
||||
return -EPERM;
|
||||
|
||||
/*
|
||||
* Project Quota ID state is only allowed to change from within the init
|
||||
* 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_fa->fsx_projid != fa->fsx_projid)
|
||||
return -EINVAL;
|
||||
if ((old_fa->fsx_xflags ^ fa->fsx_xflags) &
|
||||
FS_XFLAG_PROJINHERIT)
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
/* Check extent size hints. */
|
||||
if ((fa->fsx_xflags & FS_XFLAG_EXTSIZE) && !S_ISREG(inode->i_mode))
|
||||
return -EINVAL;
|
||||
|
||||
if ((fa->fsx_xflags & FS_XFLAG_EXTSZINHERIT) &&
|
||||
!S_ISDIR(inode->i_mode))
|
||||
return -EINVAL;
|
||||
|
||||
if ((fa->fsx_xflags & FS_XFLAG_COWEXTSIZE) &&
|
||||
!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
|
||||
return -EINVAL;
|
||||
|
||||
/*
|
||||
* It is only valid to set the DAX flag on regular files and
|
||||
* directories on filesystems.
|
||||
*/
|
||||
if ((fa->fsx_xflags & FS_XFLAG_DAX) &&
|
||||
!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)))
|
||||
return -EINVAL;
|
||||
|
||||
/* Extent size hints of zero turn off the flags. */
|
||||
if (fa->fsx_extsize == 0)
|
||||
fa->fsx_xflags &= ~(FS_XFLAG_EXTSIZE | FS_XFLAG_EXTSZINHERIT);
|
||||
if (fa->fsx_cowextsize == 0)
|
||||
fa->fsx_xflags &= ~FS_XFLAG_COWEXTSIZE;
|
||||
|
||||
return 0;
|
||||
}
|
||||
EXPORT_SYMBOL(vfs_ioc_fssetxattr_check);
|
||||
|
||||
@@ -3456,4 +3456,13 @@ extern int vfs_fadvise(struct file *file, loff_t offset, loff_t len,
|
||||
int vfs_ioc_setflags_prepare(struct inode *inode, unsigned int oldflags,
|
||||
unsigned int flags);
|
||||
|
||||
int vfs_ioc_fssetxattr_check(struct inode *inode, const struct fsxattr *old_fa,
|
||||
struct fsxattr *fa);
|
||||
|
||||
static inline void simple_fill_fsxattr(struct fsxattr *fa, __u32 xflags)
|
||||
{
|
||||
memset(fa, 0, sizeof(*fa));
|
||||
fa->fsx_xflags = xflags;
|
||||
}
|
||||
|
||||
#endif /* _LINUX_FS_H */
|
||||
|
||||
Reference in New Issue
Block a user