|
@@ -3440,8 +3440,8 @@ static long btrfs_ioctl_balance(struct file *file, void __user *arg)
|
|
struct btrfs_fs_info *fs_info = root->fs_info;
|
|
struct btrfs_fs_info *fs_info = root->fs_info;
|
|
struct btrfs_ioctl_balance_args *bargs;
|
|
struct btrfs_ioctl_balance_args *bargs;
|
|
struct btrfs_balance_control *bctl;
|
|
struct btrfs_balance_control *bctl;
|
|
|
|
+ bool need_unlock; /* for mut. excl. ops lock */
|
|
int ret;
|
|
int ret;
|
|
- int need_to_clear_lock = 0;
|
|
|
|
|
|
|
|
if (!capable(CAP_SYS_ADMIN))
|
|
if (!capable(CAP_SYS_ADMIN))
|
|
return -EPERM;
|
|
return -EPERM;
|
|
@@ -3450,14 +3450,61 @@ static long btrfs_ioctl_balance(struct file *file, void __user *arg)
|
|
if (ret)
|
|
if (ret)
|
|
return ret;
|
|
return ret;
|
|
|
|
|
|
- mutex_lock(&fs_info->volume_mutex);
|
|
|
|
|
|
+again:
|
|
|
|
+ if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) {
|
|
|
|
+ mutex_lock(&fs_info->volume_mutex);
|
|
|
|
+ mutex_lock(&fs_info->balance_mutex);
|
|
|
|
+ need_unlock = true;
|
|
|
|
+ goto locked;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * mut. excl. ops lock is locked. Three possibilites:
|
|
|
|
+ * (1) some other op is running
|
|
|
|
+ * (2) balance is running
|
|
|
|
+ * (3) balance is paused -- special case (think resume)
|
|
|
|
+ */
|
|
mutex_lock(&fs_info->balance_mutex);
|
|
mutex_lock(&fs_info->balance_mutex);
|
|
|
|
+ if (fs_info->balance_ctl) {
|
|
|
|
+ /* this is either (2) or (3) */
|
|
|
|
+ if (!atomic_read(&fs_info->balance_running)) {
|
|
|
|
+ mutex_unlock(&fs_info->balance_mutex);
|
|
|
|
+ if (!mutex_trylock(&fs_info->volume_mutex))
|
|
|
|
+ goto again;
|
|
|
|
+ mutex_lock(&fs_info->balance_mutex);
|
|
|
|
+
|
|
|
|
+ if (fs_info->balance_ctl &&
|
|
|
|
+ !atomic_read(&fs_info->balance_running)) {
|
|
|
|
+ /* this is (3) */
|
|
|
|
+ need_unlock = false;
|
|
|
|
+ goto locked;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ mutex_unlock(&fs_info->balance_mutex);
|
|
|
|
+ mutex_unlock(&fs_info->volume_mutex);
|
|
|
|
+ goto again;
|
|
|
|
+ } else {
|
|
|
|
+ /* this is (2) */
|
|
|
|
+ mutex_unlock(&fs_info->balance_mutex);
|
|
|
|
+ ret = -EINPROGRESS;
|
|
|
|
+ goto out;
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ /* this is (1) */
|
|
|
|
+ mutex_unlock(&fs_info->balance_mutex);
|
|
|
|
+ pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
|
|
|
|
+ ret = -EINVAL;
|
|
|
|
+ goto out;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+locked:
|
|
|
|
+ BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running));
|
|
|
|
|
|
if (arg) {
|
|
if (arg) {
|
|
bargs = memdup_user(arg, sizeof(*bargs));
|
|
bargs = memdup_user(arg, sizeof(*bargs));
|
|
if (IS_ERR(bargs)) {
|
|
if (IS_ERR(bargs)) {
|
|
ret = PTR_ERR(bargs);
|
|
ret = PTR_ERR(bargs);
|
|
- goto out;
|
|
|
|
|
|
+ goto out_unlock;
|
|
}
|
|
}
|
|
|
|
|
|
if (bargs->flags & BTRFS_BALANCE_RESUME) {
|
|
if (bargs->flags & BTRFS_BALANCE_RESUME) {
|
|
@@ -3477,13 +3524,10 @@ static long btrfs_ioctl_balance(struct file *file, void __user *arg)
|
|
bargs = NULL;
|
|
bargs = NULL;
|
|
}
|
|
}
|
|
|
|
|
|
- if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
|
|
|
|
- 1)) {
|
|
|
|
- pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
|
|
|
|
|
|
+ if (fs_info->balance_ctl) {
|
|
ret = -EINPROGRESS;
|
|
ret = -EINPROGRESS;
|
|
goto out_bargs;
|
|
goto out_bargs;
|
|
}
|
|
}
|
|
- need_to_clear_lock = 1;
|
|
|
|
|
|
|
|
bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
|
|
bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
|
|
if (!bctl) {
|
|
if (!bctl) {
|
|
@@ -3504,11 +3548,17 @@ static long btrfs_ioctl_balance(struct file *file, void __user *arg)
|
|
}
|
|
}
|
|
|
|
|
|
do_balance:
|
|
do_balance:
|
|
- ret = btrfs_balance(bctl, bargs);
|
|
|
|
/*
|
|
/*
|
|
- * bctl is freed in __cancel_balance or in free_fs_info if
|
|
|
|
- * restriper was paused all the way until unmount
|
|
|
|
|
|
+ * Ownership of bctl and mutually_exclusive_operation_running
|
|
|
|
+ * goes to to btrfs_balance. bctl is freed in __cancel_balance,
|
|
|
|
+ * or, if restriper was paused all the way until unmount, in
|
|
|
|
+ * free_fs_info. mutually_exclusive_operation_running is
|
|
|
|
+ * cleared in __cancel_balance.
|
|
*/
|
|
*/
|
|
|
|
+ need_unlock = false;
|
|
|
|
+
|
|
|
|
+ ret = btrfs_balance(bctl, bargs);
|
|
|
|
+
|
|
if (arg) {
|
|
if (arg) {
|
|
if (copy_to_user(arg, bargs, sizeof(*bargs)))
|
|
if (copy_to_user(arg, bargs, sizeof(*bargs)))
|
|
ret = -EFAULT;
|
|
ret = -EFAULT;
|
|
@@ -3516,12 +3566,12 @@ do_balance:
|
|
|
|
|
|
out_bargs:
|
|
out_bargs:
|
|
kfree(bargs);
|
|
kfree(bargs);
|
|
-out:
|
|
|
|
- if (need_to_clear_lock)
|
|
|
|
- atomic_set(&root->fs_info->mutually_exclusive_operation_running,
|
|
|
|
- 0);
|
|
|
|
|
|
+out_unlock:
|
|
mutex_unlock(&fs_info->balance_mutex);
|
|
mutex_unlock(&fs_info->balance_mutex);
|
|
mutex_unlock(&fs_info->volume_mutex);
|
|
mutex_unlock(&fs_info->volume_mutex);
|
|
|
|
+ if (need_unlock)
|
|
|
|
+ atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
|
|
|
|
+out:
|
|
mnt_drop_write_file(file);
|
|
mnt_drop_write_file(file);
|
|
return ret;
|
|
return ret;
|
|
}
|
|
}
|