[PATCH 1/2] btrfs: ioctl: Move the subvolume deleter code into a new function
From: Marcos Paulo de Souza
Date: Fri Jan 10 2020 - 23:42:52 EST
From: Marcos Paulo de Souza <mpdesouza@xxxxxxxx>
This new function will be used by the next patch.
Signed-off-by: Marcos Paulo de Souza <mpdesouza@xxxxxxxx>
---
fs/btrfs/ioctl.c | 66 ++++++++++++++++++++++++++++--------------------
1 file changed, 39 insertions(+), 27 deletions(-)
diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
index 0fa1c386d020..dcceae4c5d28 100644
--- a/fs/btrfs/ioctl.c
+++ b/fs/btrfs/ioctl.c
@@ -2835,44 +2835,27 @@ static int btrfs_ioctl_get_subvol_rootref(struct file *file, void __user *argp)
return ret;
}
-static noinline int btrfs_ioctl_snap_destroy(struct file *file,
- void __user *arg)
+static noinline int btrfs_subvolume_deleter(struct file *file,
+ struct dentry *parent, const char *subvol_name,
+ size_t namelen)
{
- struct dentry *parent = file->f_path.dentry;
- struct btrfs_fs_info *fs_info = btrfs_sb(parent->d_sb);
struct dentry *dentry;
- struct inode *dir = d_inode(parent);
+ struct btrfs_root *dest;
struct inode *inode;
+ struct inode *dir = d_inode(parent);
struct btrfs_root *root = BTRFS_I(dir)->root;
- struct btrfs_root *dest = NULL;
- struct btrfs_ioctl_vol_args *vol_args;
- int namelen;
+ struct btrfs_fs_info *fs_info = root->fs_info;
int err = 0;
- if (!S_ISDIR(dir->i_mode))
- return -ENOTDIR;
-
- vol_args = memdup_user(arg, sizeof(*vol_args));
- if (IS_ERR(vol_args))
- return PTR_ERR(vol_args);
-
- vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
- namelen = strlen(vol_args->name);
- if (strchr(vol_args->name, '/') ||
- strncmp(vol_args->name, "..", namelen) == 0) {
- err = -EINVAL;
- goto out;
- }
-
err = mnt_want_write_file(file);
if (err)
- goto out;
-
+ return err;
err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
if (err == -EINTR)
goto out_drop_write;
- dentry = lookup_one_len(vol_args->name, parent, namelen);
+
+ dentry = lookup_one_len(subvol_name, parent, namelen);
if (IS_ERR(dentry)) {
err = PTR_ERR(dentry);
goto out_unlock_dir;
@@ -2880,7 +2863,7 @@ static noinline int btrfs_ioctl_snap_destroy(struct file *file,
if (d_really_is_negative(dentry)) {
err = -ENOENT;
- goto out_dput;
+ goto out_unlock_dir;
}
inode = d_inode(dentry);
@@ -2943,6 +2926,35 @@ static noinline int btrfs_ioctl_snap_destroy(struct file *file,
inode_unlock(dir);
out_drop_write:
mnt_drop_write_file(file);
+
+ return err;
+}
+
+static noinline int btrfs_ioctl_snap_destroy(struct file *file,
+ void __user *arg)
+{
+ struct dentry *parent = file->f_path.dentry;
+ struct inode *dir = d_inode(parent);
+ struct btrfs_ioctl_vol_args *vol_args;
+ int namelen;
+ int err = 0;
+
+ if (!S_ISDIR(dir->i_mode))
+ return -ENOTDIR;
+
+ vol_args = memdup_user(arg, sizeof(*vol_args));
+ if (IS_ERR(vol_args))
+ return PTR_ERR(vol_args);
+
+ vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
+ namelen = strlen(vol_args->name);
+ if (strchr(vol_args->name, '/') ||
+ strncmp(vol_args->name, "..", namelen) == 0) {
+ err = -EINVAL;
+ goto out;
+ }
+
+ err = btrfs_subvolume_deleter(file, parent, vol_args->name, namelen);
out:
kfree(vol_args);
return err;
--
2.24.0