Merge tag 'fsverity-for-linus' of git://git.kernel.org/pub/scm/fs/fscrypt/fscrypt
[linux-2.6-block.git] / fs / f2fs / file.c
index 838bfeecbd863449656eece4429480d6606614b7..39fffc19e00c910a2b6218d14fd213aa75c0db47 100644 (file)
@@ -1661,19 +1661,12 @@ static int f2fs_file_flush(struct file *file, fl_owner_t id)
 static int f2fs_setflags_common(struct inode *inode, u32 iflags, u32 mask)
 {
        struct f2fs_inode_info *fi = F2FS_I(inode);
-       u32 oldflags;
 
        /* Is it quota file? Do not allow user to mess with it */
        if (IS_NOQUOTA(inode))
                return -EPERM;
 
-       oldflags = fi->i_flags;
-
-       if ((iflags ^ oldflags) & (F2FS_APPEND_FL | F2FS_IMMUTABLE_FL))
-               if (!capable(CAP_LINUX_IMMUTABLE))
-                       return -EPERM;
-
-       fi->i_flags = iflags | (oldflags & ~mask);
+       fi->i_flags = iflags | (fi->i_flags & ~mask);
 
        if (fi->i_flags & F2FS_PROJINHERIT_FL)
                set_inode_flag(inode, FI_PROJ_INHERIT);
@@ -1781,7 +1774,8 @@ static int f2fs_ioc_getflags(struct file *filp, unsigned long arg)
 static int f2fs_ioc_setflags(struct file *filp, unsigned long arg)
 {
        struct inode *inode = file_inode(filp);
-       u32 fsflags;
+       struct f2fs_inode_info *fi = F2FS_I(inode);
+       u32 fsflags, old_fsflags;
        u32 iflags;
        int ret;
 
@@ -1805,8 +1799,14 @@ static int f2fs_ioc_setflags(struct file *filp, unsigned long arg)
 
        inode_lock(inode);
 
+       old_fsflags = f2fs_iflags_to_fsflags(fi->i_flags);
+       ret = vfs_ioc_setflags_prepare(inode, old_fsflags, fsflags);
+       if (ret)
+               goto out;
+
        ret = f2fs_setflags_common(inode, iflags,
                        f2fs_fsflags_to_iflags(F2FS_SETTABLE_FS_FL));
+out:
        inode_unlock(inode);
        mnt_drop_write_file(filp);
        return ret;
@@ -2195,6 +2195,49 @@ out_err:
        return err;
 }
 
+static int f2fs_ioc_get_encryption_policy_ex(struct file *filp,
+                                            unsigned long arg)
+{
+       if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
+               return -EOPNOTSUPP;
+
+       return fscrypt_ioctl_get_policy_ex(filp, (void __user *)arg);
+}
+
+static int f2fs_ioc_add_encryption_key(struct file *filp, unsigned long arg)
+{
+       if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
+               return -EOPNOTSUPP;
+
+       return fscrypt_ioctl_add_key(filp, (void __user *)arg);
+}
+
+static int f2fs_ioc_remove_encryption_key(struct file *filp, unsigned long arg)
+{
+       if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
+               return -EOPNOTSUPP;
+
+       return fscrypt_ioctl_remove_key(filp, (void __user *)arg);
+}
+
+static int f2fs_ioc_remove_encryption_key_all_users(struct file *filp,
+                                                   unsigned long arg)
+{
+       if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
+               return -EOPNOTSUPP;
+
+       return fscrypt_ioctl_remove_key_all_users(filp, (void __user *)arg);
+}
+
+static int f2fs_ioc_get_encryption_key_status(struct file *filp,
+                                             unsigned long arg)
+{
+       if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
+               return -EOPNOTSUPP;
+
+       return fscrypt_ioctl_get_key_status(filp, (void __user *)arg);
+}
+
 static int f2fs_ioc_gc(struct file *filp, unsigned long arg)
 {
        struct inode *inode = file_inode(filp);
@@ -2866,52 +2909,32 @@ static inline u32 f2fs_xflags_to_iflags(u32 xflags)
        return iflags;
 }
 
-static int f2fs_ioc_fsgetxattr(struct file *filp, unsigned long arg)
+static void f2fs_fill_fsxattr(struct inode *inode, struct fsxattr *fa)
 {
-       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);
+       simple_fill_fsxattr(fa, 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);
-
-       if (copy_to_user((struct fsxattr __user *)arg, &fa, sizeof(fa)))
-               return -EFAULT;
-       return 0;
+               fa->fsx_projid = from_kprojid(&init_user_ns, fi->i_projid);
 }
 
-static int f2fs_ioctl_check_project(struct inode *inode, struct fsxattr *fa)
+static int f2fs_ioc_fsgetxattr(struct file *filp, unsigned long arg)
 {
-       /*
-        * 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;
+       struct inode *inode = file_inode(filp);
+       struct fsxattr fa;
 
-       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;
-       }
+       f2fs_fill_fsxattr(inode, &fa);
 
+       if (copy_to_user((struct fsxattr __user *)arg, &fa, sizeof(fa)))
+               return -EFAULT;
        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;
 
@@ -2934,9 +2957,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)
@@ -3144,6 +3170,16 @@ long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
                return f2fs_ioc_get_encryption_policy(filp, arg);
        case F2FS_IOC_GET_ENCRYPTION_PWSALT:
                return f2fs_ioc_get_encryption_pwsalt(filp, arg);
+       case FS_IOC_GET_ENCRYPTION_POLICY_EX:
+               return f2fs_ioc_get_encryption_policy_ex(filp, arg);
+       case FS_IOC_ADD_ENCRYPTION_KEY:
+               return f2fs_ioc_add_encryption_key(filp, arg);
+       case FS_IOC_REMOVE_ENCRYPTION_KEY:
+               return f2fs_ioc_remove_encryption_key(filp, arg);
+       case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
+               return f2fs_ioc_remove_encryption_key_all_users(filp, arg);
+       case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
+               return f2fs_ioc_get_encryption_key_status(filp, arg);
        case F2FS_IOC_GARBAGE_COLLECT:
                return f2fs_ioc_gc(filp, arg);
        case F2FS_IOC_GARBAGE_COLLECT_RANGE:
@@ -3275,6 +3311,11 @@ long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
        case F2FS_IOC_SET_ENCRYPTION_POLICY:
        case F2FS_IOC_GET_ENCRYPTION_PWSALT:
        case F2FS_IOC_GET_ENCRYPTION_POLICY:
+       case FS_IOC_GET_ENCRYPTION_POLICY_EX:
+       case FS_IOC_ADD_ENCRYPTION_KEY:
+       case FS_IOC_REMOVE_ENCRYPTION_KEY:
+       case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
+       case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
        case F2FS_IOC_GARBAGE_COLLECT:
        case F2FS_IOC_GARBAGE_COLLECT_RANGE:
        case F2FS_IOC_WRITE_CHECKPOINT: