Merge tag 'sunxi-fixes-for-3.10' of git://github.com/mripard/linux into fixes
[firefly-linux-kernel-4.4.55.git] / fs / btrfs / ioctl.c
index 2c02310ff2d96320ebf2d348ed339dd0ea047e0f..0f81d67cdc8da651890df1a89c01df9188667d97 100644 (file)
@@ -723,7 +723,9 @@ static noinline int btrfs_mksubvol(struct path *parent,
        struct dentry *dentry;
        int error;
 
-       mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
+       error = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
+       if (error == -EINTR)
+               return error;
 
        dentry = lookup_one_len(name, parent->dentry, namelen);
        error = PTR_ERR(dentry);
@@ -1152,8 +1154,11 @@ int btrfs_defrag_file(struct inode *inode, struct file *file,
        u64 new_align = ~((u64)128 * 1024 - 1);
        struct page **pages = NULL;
 
-       if (extent_thresh == 0)
-               extent_thresh = 256 * 1024;
+       if (isize == 0)
+               return 0;
+
+       if (range->start >= isize)
+               return -EINVAL;
 
        if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
                if (range->compress_type > BTRFS_COMPRESS_TYPES)
@@ -1162,8 +1167,8 @@ int btrfs_defrag_file(struct inode *inode, struct file *file,
                        compress_type = range->compress_type;
        }
 
-       if (isize == 0)
-               return 0;
+       if (extent_thresh == 0)
+               extent_thresh = 256 * 1024;
 
        /*
         * if we were not given a file, allocate a readahead
@@ -1796,7 +1801,11 @@ static noinline int copy_to_sk(struct btrfs_root *root,
                item_off = btrfs_item_ptr_offset(leaf, i);
                item_len = btrfs_item_size_nr(leaf, i);
 
-               if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
+               btrfs_item_key_to_cpu(leaf, key, i);
+               if (!key_in_sk(key, sk))
+                       continue;
+
+               if (sizeof(sh) + item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
                        item_len = 0;
 
                if (sizeof(sh) + item_len + *sk_offset >
@@ -1805,10 +1814,6 @@ static noinline int copy_to_sk(struct btrfs_root *root,
                        goto overflow;
                }
 
-               btrfs_item_key_to_cpu(leaf, key, i);
-               if (!key_in_sk(key, sk))
-                       continue;
-
                sh.objectid = key->objectid;
                sh.offset = key->offset;
                sh.type = key->type;
@@ -2086,7 +2091,9 @@ static noinline int btrfs_ioctl_snap_destroy(struct file *file,
        if (err)
                goto out;
 
-       mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
+       err = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
+       if (err == -EINTR)
+               goto out;
        dentry = lookup_one_len(vol_args->name, parent, namelen);
        if (IS_ERR(dentry)) {
                err = PTR_ERR(dentry);
@@ -2425,7 +2432,6 @@ static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
 
        mutex_lock(&fs_devices->device_list_mutex);
        dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL);
-       mutex_unlock(&fs_devices->device_list_mutex);
 
        if (!dev) {
                ret = -ENODEV;
@@ -2449,6 +2455,7 @@ static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
        }
 
 out:
+       mutex_unlock(&fs_devices->device_list_mutex);
        if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
                ret = -EFAULT;
 
@@ -3003,7 +3010,7 @@ void btrfs_get_block_group_info(struct list_head *groups_list,
        }
 }
 
-long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
+static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
 {
        struct btrfs_ioctl_space_args space_args;
        struct btrfs_ioctl_space_info space;
@@ -3693,12 +3700,11 @@ static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
                goto drop_write;
        }
 
-       if (sa->cmd != BTRFS_QUOTA_CTL_RESCAN) {
-               trans = btrfs_start_transaction(root, 2);
-               if (IS_ERR(trans)) {
-                       ret = PTR_ERR(trans);
-                       goto out;
-               }
+       down_write(&root->fs_info->subvol_sem);
+       trans = btrfs_start_transaction(root->fs_info->tree_root, 2);
+       if (IS_ERR(trans)) {
+               ret = PTR_ERR(trans);
+               goto out;
        }
 
        switch (sa->cmd) {
@@ -3708,9 +3714,6 @@ static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
        case BTRFS_QUOTA_CTL_DISABLE:
                ret = btrfs_quota_disable(trans, root->fs_info);
                break;
-       case BTRFS_QUOTA_CTL_RESCAN:
-               ret = btrfs_quota_rescan(root->fs_info);
-               break;
        default:
                ret = -EINVAL;
                break;
@@ -3719,13 +3722,12 @@ static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
        if (copy_to_user(arg, sa, sizeof(*sa)))
                ret = -EFAULT;
 
-       if (trans) {
-               err = btrfs_commit_transaction(trans, root);
-               if (err && !ret)
-                       ret = err;
-       }
+       err = btrfs_commit_transaction(trans, root->fs_info->tree_root);
+       if (err && !ret)
+               ret = err;
 out:
        kfree(sa);
+       up_write(&root->fs_info->subvol_sem);
 drop_write:
        mnt_drop_write_file(file);
        return ret;
@@ -3877,6 +3879,64 @@ drop_write:
        return ret;
 }
 
+static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
+{
+       struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
+       struct btrfs_ioctl_quota_rescan_args *qsa;
+       int ret;
+
+       if (!capable(CAP_SYS_ADMIN))
+               return -EPERM;
+
+       ret = mnt_want_write_file(file);
+       if (ret)
+               return ret;
+
+       qsa = memdup_user(arg, sizeof(*qsa));
+       if (IS_ERR(qsa)) {
+               ret = PTR_ERR(qsa);
+               goto drop_write;
+       }
+
+       if (qsa->flags) {
+               ret = -EINVAL;
+               goto out;
+       }
+
+       ret = btrfs_qgroup_rescan(root->fs_info);
+
+out:
+       kfree(qsa);
+drop_write:
+       mnt_drop_write_file(file);
+       return ret;
+}
+
+static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
+{
+       struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
+       struct btrfs_ioctl_quota_rescan_args *qsa;
+       int ret = 0;
+
+       if (!capable(CAP_SYS_ADMIN))
+               return -EPERM;
+
+       qsa = kzalloc(sizeof(*qsa), GFP_NOFS);
+       if (!qsa)
+               return -ENOMEM;
+
+       if (root->fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
+               qsa->flags = 1;
+               qsa->progress = root->fs_info->qgroup_rescan_progress.objectid;
+       }
+
+       if (copy_to_user(arg, qsa, sizeof(*qsa)))
+               ret = -EFAULT;
+
+       kfree(qsa);
+       return ret;
+}
+
 static long btrfs_ioctl_set_received_subvol(struct file *file,
                                            void __user *arg)
 {
@@ -4115,6 +4175,10 @@ long btrfs_ioctl(struct file *file, unsigned int
                return btrfs_ioctl_qgroup_create(file, argp);
        case BTRFS_IOC_QGROUP_LIMIT:
                return btrfs_ioctl_qgroup_limit(file, argp);
+       case BTRFS_IOC_QUOTA_RESCAN:
+               return btrfs_ioctl_quota_rescan(file, argp);
+       case BTRFS_IOC_QUOTA_RESCAN_STATUS:
+               return btrfs_ioctl_quota_rescan_status(file, argp);
        case BTRFS_IOC_DEV_REPLACE:
                return btrfs_ioctl_dev_replace(root, argp);
        case BTRFS_IOC_GET_FSLABEL: