From: Wang Shilong <wangsl-f...@cn.fujitsu.com>

Add a necessary check before updating qgroup limit,
if the relative qgroup dosen't exsit, do not join transaction.

Signed-off-by: Wang Shilong <wangsl-f...@cn.fujitsu.com>
Reviewed-by: Miao Xie <mi...@cn.fujitsu.com>
---
 fs/btrfs/ioctl.c |   43 ++++++++++++++++++++++++++++++++++++-------
 1 files changed, 36 insertions(+), 7 deletions(-)

diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
index 0b8ab81..3c4fde0 100644
--- a/fs/btrfs/ioctl.c
+++ b/fs/btrfs/ioctl.c
@@ -3837,6 +3837,33 @@ drop_write:
        return ret;
 }
 
+static int may_qgroup_limit(struct btrfs_root *root, u64 qgroupid)
+{
+       int ret = 0;
+       struct btrfs_key key;
+       struct btrfs_path *path;
+
+       if (!root->fs_info->quota_root)
+               return -EINVAL;
+
+       path = btrfs_alloc_path();
+       if (!path) {
+               ret = -ENOMEM;
+               return ret;
+       }
+
+       key.objectid = 0;
+       key.type = BTRFS_QGROUP_INFO_KEY;
+       key.offset = qgroupid;
+
+       ret = btrfs_search_slot(NULL, root->fs_info->quota_root,
+                               &key, path, 0, 0);
+       if (ret > 0)
+               ret = -ENOENT;
+       btrfs_free_path(path);
+       return ret;
+}
+
 static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
 {
        struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
@@ -3859,19 +3886,21 @@ static long btrfs_ioctl_qgroup_limit(struct file *file, 
void __user *arg)
                goto drop_write;
        }
        mutex_lock(&root->fs_info->quota_lock);
-       trans = btrfs_join_transaction(root);
-       if (IS_ERR(trans)) {
-               ret = PTR_ERR(trans);
-               goto out;
-       }
-
        qgroupid = sa->qgroupid;
        if (!qgroupid) {
                /* take the current subvol as qgroup */
                qgroupid = root->root_key.objectid;
        }
+       ret = may_qgroup_limit(root, qgroupid);
+       if (ret)
+               goto out;
+
+       trans = btrfs_join_transaction(root);
+       if (IS_ERR(trans)) {
+               ret = PTR_ERR(trans);
+               goto out;
+       }
 
-       /* FIXME: check if the IDs really exist */
        ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
 
        err = btrfs_end_transaction(trans, root);
-- 
1.7.7.6

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to