===================================================================
@@ -806,6 +806,7 @@
struct btrfs_balance_info {
u64 expected;
u64 completed;
+ int cancel_pending;
};
struct reloc_control;
===================================================================
@@ -2016,6 +2016,29 @@
return ret;
}
+/*
+ * Cancel a running balance operation
+ */
+long btrfs_ioctl_balance_cancel(struct btrfs_fs_info *fs_info)
+{
+ int err = 0;
+
+ spin_lock(&fs_info->balance_info_lock);
+ if(!fs_info->balance_info) {
+ err = -EINVAL;
+ goto error;
+ }
+ if(fs_info->balance_info->cancel_pending) {
+ err = -ECANCELED;
+ goto error;
+ }
+ fs_info->balance_info->cancel_pending = 1;
+
+error:
+ spin_unlock(&fs_info->balance_info_lock);
+ return err;
+}
+
long btrfs_ioctl(struct file *file, unsigned int
cmd, unsigned long arg)
{
@@ -2051,6 +2074,8 @@
return btrfs_balance(root->fs_info->dev_root);
case BTRFS_IOC_BALANCE_PROGRESS:
return btrfs_ioctl_balance_progress(root->fs_info, argp);
+ case BTRFS_IOC_BALANCE_CANCEL:
+ return btrfs_ioctl_balance_cancel(root->fs_info);
case BTRFS_IOC_CLONE:
return btrfs_ioctl_clone(file, arg, 0, 0, 0);
case BTRFS_IOC_CLONE_RANGE:
===================================================================
@@ -185,4 +185,5 @@
struct btrfs_ioctl_space_args)
#define BTRFS_IOC_BALANCE_PROGRESS _IOR(BTRFS_IOCTL_MAGIC, 21, \
struct btrfs_ioctl_balance_progress)
+#define BTRFS_IOC_BALANCE_CANCEL _IO(BTRFS_IOCTL_MAGIC, 22)
#endif
===================================================================
@@ -1921,6 +1921,7 @@
bal_info->expected = -1; /* One less than actually counted,
because chunk 0 is special */
bal_info->completed = 0;
+ bal_info->cancel_pending = 0;
/* step one make some room on all the devices */
list_for_each_entry(device, devices, dev_list) {
@@ -1983,7 +1984,7 @@
key.offset = (u64)-1;
key.type = BTRFS_CHUNK_ITEM_KEY;
- while (1) {
+ while (!bal_info->cancel_pending) {
ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
if (ret < 0)
goto error;
@@ -2024,6 +2025,10 @@
bal_info->completed, bal_info->expected);
}
ret = 0;
+ if(bal_info->cancel_pending) {
+ printk(KERN_INFO "btrfs: balance cancelled\n");
+ ret = -EINTR;
+ }
error:
btrfs_free_path(path);
spin_lock(&dev_root->fs_info->balance_info_lock);