btrfs: Handle error from btrfs_uuid_tree_rem call in _btrfs_ioctl_set_received_subvol
[sfrench/cifs-2.6.git] / fs / btrfs / ioctl.c
index 111ee282b7772a85c46dd4cf746c59cf313803cd..94bcc1bf71caba5307c169b060fab0401c1fd46a 100644 (file)
@@ -2600,7 +2600,7 @@ static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
                        range->len = (u64)-1;
                }
                ret = btrfs_defrag_file(file_inode(file), file,
-                                       range, 0, 0);
+                                       range, BTRFS_OLDEST_GENERATION, 0);
                if (ret > 0)
                        ret = 0;
                kfree(range);
@@ -3936,73 +3936,6 @@ int btrfs_clone_file_range(struct file *src_file, loff_t off,
        return btrfs_clone_files(dst_file, src_file, off, len, destoff);
 }
 
-/*
- * there are many ways the trans_start and trans_end ioctls can lead
- * to deadlocks.  They should only be used by applications that
- * basically own the machine, and have a very in depth understanding
- * of all the possible deadlocks and enospc problems.
- */
-static long btrfs_ioctl_trans_start(struct file *file)
-{
-       struct inode *inode = file_inode(file);
-       struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
-       struct btrfs_root *root = BTRFS_I(inode)->root;
-       struct btrfs_trans_handle *trans;
-       struct btrfs_file_private *private;
-       int ret;
-       static bool warned = false;
-
-       ret = -EPERM;
-       if (!capable(CAP_SYS_ADMIN))
-               goto out;
-
-       if (!warned) {
-               btrfs_warn(fs_info,
-                       "Userspace transaction mechanism is considered "
-                       "deprecated and slated to be removed in 4.17. "
-                       "If you have a valid use case please "
-                       "speak up on the mailing list");
-               WARN_ON(1);
-               warned = true;
-       }
-
-       ret = -EINPROGRESS;
-       private = file->private_data;
-       if (private && private->trans)
-               goto out;
-       if (!private) {
-               private = kzalloc(sizeof(struct btrfs_file_private),
-                                 GFP_KERNEL);
-               if (!private)
-                       return -ENOMEM;
-               file->private_data = private;
-       }
-
-       ret = -EROFS;
-       if (btrfs_root_readonly(root))
-               goto out;
-
-       ret = mnt_want_write_file(file);
-       if (ret)
-               goto out;
-
-       atomic_inc(&fs_info->open_ioctl_trans);
-
-       ret = -ENOMEM;
-       trans = btrfs_start_ioctl_transaction(root);
-       if (IS_ERR(trans))
-               goto out_drop;
-
-       private->trans = trans;
-       return 0;
-
-out_drop:
-       atomic_dec(&fs_info->open_ioctl_trans);
-       mnt_drop_write_file(file);
-out:
-       return ret;
-}
-
 static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
 {
        struct inode *inode = file_inode(file);
@@ -4244,30 +4177,6 @@ out:
        return ret;
 }
 
-/*
- * there are many ways the trans_start and trans_end ioctls can lead
- * to deadlocks.  They should only be used by applications that
- * basically own the machine, and have a very in depth understanding
- * of all the possible deadlocks and enospc problems.
- */
-long btrfs_ioctl_trans_end(struct file *file)
-{
-       struct inode *inode = file_inode(file);
-       struct btrfs_root *root = BTRFS_I(inode)->root;
-       struct btrfs_file_private *private = file->private_data;
-
-       if (!private || !private->trans)
-               return -EINVAL;
-
-       btrfs_end_transaction(private->trans);
-       private->trans = NULL;
-
-       atomic_dec(&root->fs_info->open_ioctl_trans);
-
-       mnt_drop_write_file(file);
-       return 0;
-}
-
 static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
                                            void __user *argp)
 {
@@ -4429,7 +4338,8 @@ static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info,
                ret = 0;
                break;
        case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
-               ret = btrfs_dev_replace_cancel(fs_info, p);
+               p->result = btrfs_dev_replace_cancel(fs_info);
+               ret = 0;
                break;
        default:
                ret = -EINVAL;
@@ -5138,10 +5048,17 @@ static long _btrfs_ioctl_set_received_subvol(struct file *file,
        received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
                                       BTRFS_UUID_SIZE);
        if (received_uuid_changed &&
-           !btrfs_is_empty_uuid(root_item->received_uuid))
-               btrfs_uuid_tree_rem(trans, fs_info, root_item->received_uuid,
-                                   BTRFS_UUID_KEY_RECEIVED_SUBVOL,
-                                   root->root_key.objectid);
+           !btrfs_is_empty_uuid(root_item->received_uuid)) {
+               ret = btrfs_uuid_tree_rem(trans, fs_info,
+                                         root_item->received_uuid,
+                                         BTRFS_UUID_KEY_RECEIVED_SUBVOL,
+                                         root->root_key.objectid);
+               if (ret && ret != -ENOENT) {
+                       btrfs_abort_transaction(trans, ret);
+                       btrfs_end_transaction(trans);
+                       goto out;
+               }
+       }
        memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
        btrfs_set_root_stransid(root_item, sa->stransid);
        btrfs_set_root_rtransid(root_item, sa->rtransid);
@@ -5574,10 +5491,6 @@ long btrfs_ioctl(struct file *file, unsigned int
                return btrfs_ioctl_dev_info(fs_info, argp);
        case BTRFS_IOC_BALANCE:
                return btrfs_ioctl_balance(file, NULL);
-       case BTRFS_IOC_TRANS_START:
-               return btrfs_ioctl_trans_start(file);
-       case BTRFS_IOC_TRANS_END:
-               return btrfs_ioctl_trans_end(file);
        case BTRFS_IOC_TREE_SEARCH:
                return btrfs_ioctl_tree_search(file, argp);
        case BTRFS_IOC_TREE_SEARCH_V2: