Btrfs: Fix subvolume creation locking rules
[cascardo/linux.git] / fs / btrfs / ioctl.c
index 3932c7c..3d85f18 100644 (file)
@@ -21,6 +21,7 @@
 #include <linux/buffer_head.h>
 #include <linux/file.h>
 #include <linux/fs.h>
+#include <linux/fsnotify.h>
 #include <linux/pagemap.h>
 #include <linux/highmem.h>
 #include <linux/time.h>
 #include <linux/string.h>
 #include <linux/smp_lock.h>
 #include <linux/backing-dev.h>
+#include <linux/mount.h>
 #include <linux/mpage.h>
+#include <linux/namei.h>
 #include <linux/swap.h>
 #include <linux/writeback.h>
 #include <linux/statfs.h>
 #include <linux/compat.h>
 #include <linux/bit_spinlock.h>
+#include <linux/security.h>
 #include <linux/version.h>
 #include <linux/xattr.h>
+#include <linux/vmalloc.h>
 #include "ctree.h"
 #include "disk-io.h"
 #include "transaction.h"
@@ -47,8 +52,9 @@
 
 
 
-static noinline int create_subvol(struct btrfs_root *root, char *name,
-                                 int namelen)
+static noinline int create_subvol(struct btrfs_root *root,
+                                 struct dentry *dentry,
+                                 char *name, int namelen)
 {
        struct btrfs_trans_handle *trans;
        struct btrfs_key key;
@@ -75,9 +81,8 @@ static noinline int create_subvol(struct btrfs_root *root, char *name,
        if (ret)
                goto fail;
 
-       leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
-                                     objectid, trans->transid, 0, 0,
-                                     0, 0);
+       leaf = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
+                                     objectid, trans->transid, 0, 0, 0);
        if (IS_ERR(leaf)) {
                ret = PTR_ERR(leaf);
                goto fail;
@@ -99,7 +104,7 @@ static noinline int create_subvol(struct btrfs_root *root, char *name,
        inode_item->generation = cpu_to_le64(1);
        inode_item->size = cpu_to_le64(3);
        inode_item->nlink = cpu_to_le32(1);
-       inode_item->nblocks = cpu_to_le64(1);
+       inode_item->nbytes = cpu_to_le64(root->leafsize);
        inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
 
        btrfs_set_root_bytenr(&root_item, leaf->start);
@@ -151,14 +156,11 @@ static noinline int create_subvol(struct btrfs_root *root, char *name,
        trans = btrfs_start_transaction(new_root, 1);
        BUG_ON(!trans);
 
-       ret = btrfs_create_subvol_root(new_root, trans, new_dirid,
+       ret = btrfs_create_subvol_root(new_root, dentry, trans, new_dirid,
                                       BTRFS_I(dir)->block_group);
        if (ret)
                goto fail;
 
-       /* Invalidate existing dcache entry for new subvolume. */
-       btrfs_invalidate_dcache_root(root, name, namelen);
-
 fail:
        nr = trans->blocks_used;
        err = btrfs_commit_transaction(trans, new_root);
@@ -210,6 +212,79 @@ fail_unlock:
        return ret;
 }
 
+/* copy of may_create in fs/namei.c() */
+static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
+{
+       if (child->d_inode)
+               return -EEXIST;
+       if (IS_DEADDIR(dir))
+               return -ENOENT;
+       return inode_permission(dir, MAY_WRITE | MAY_EXEC);
+}
+
+/*
+ * Create a new subvolume below @parent.  This is largely modeled after
+ * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
+ * inside this filesystem so it's quite a bit simpler.
+ */
+static noinline int btrfs_mksubvol(struct path *parent, char *name,
+                                  int mode, int namelen)
+{
+       struct dentry *dentry;
+       int error;
+
+       mutex_lock_nested(&parent->dentry->d_inode->i_mutex, I_MUTEX_PARENT);
+
+       dentry = lookup_one_len(name, parent->dentry, namelen);
+       error = PTR_ERR(dentry);
+       if (IS_ERR(dentry))
+               goto out_unlock;
+
+       error = -EEXIST;
+       if (dentry->d_inode)
+               goto out_dput;
+
+       if (!IS_POSIXACL(parent->dentry->d_inode))
+               mode &= ~current->fs->umask;
+       error = mnt_want_write(parent->mnt);
+       if (error)
+               goto out_dput;
+
+       error = btrfs_may_create(parent->dentry->d_inode, dentry);
+       if (error)
+               goto out_drop_write;
+
+       mode &= (S_IRWXUGO|S_ISVTX);
+       error = security_inode_mkdir(parent->dentry->d_inode, dentry, mode);
+       if (error)
+               goto out_drop_write;
+
+       /*
+        * Actually perform the low-level subvolume creation after all
+        * this VFS fuzz.
+        *
+        * Eventually we want to pass in an inode under which we create this
+        * subvolume, but for now all are under the filesystem root.
+        *
+        * Also we should pass on the mode eventually to allow creating new
+        * subvolume with specific mode bits.
+        */
+       error = create_subvol(BTRFS_I(parent->dentry->d_inode)->root, dentry,
+                             name, namelen);
+       if (error)
+               goto out_drop_write;
+
+       fsnotify_mkdir(parent->dentry->d_inode, dentry);
+out_drop_write:
+       mnt_drop_write(parent->mnt);
+out_dput:
+       dput(dentry);
+out_unlock:
+       mutex_unlock(&parent->dentry->d_inode->i_mutex);
+       return error;
+}
+
+
 int btrfs_defrag_file(struct file *file)
 {
        struct inode *inode = fdentry(file)->d_inode;
@@ -395,9 +470,10 @@ out:
        return ret;
 }
 
-static noinline int btrfs_ioctl_snap_create(struct btrfs_root *root,
+static noinline int btrfs_ioctl_snap_create(struct file *file,
                                            void __user *arg)
 {
+       struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
        struct btrfs_ioctl_vol_args *vol_args;
        struct btrfs_dir_item *di;
        struct btrfs_path *path;
@@ -444,12 +520,14 @@ static noinline int btrfs_ioctl_snap_create(struct btrfs_root *root,
                goto out;
        }
 
-       mutex_lock(&root->fs_info->drop_mutex);
-       if (root == root->fs_info->tree_root)
-               ret = create_subvol(root, vol_args->name, namelen);
-       else
+       if (root == root->fs_info->tree_root) {
+               ret = btrfs_mksubvol(&file->f_path, vol_args->name,
+                                    file->f_path.dentry->d_inode->i_mode,
+                                    namelen);
+       } else {
                ret = create_snapshot(root, vol_args->name, namelen);
-       mutex_unlock(&root->fs_info->drop_mutex);
+       }
+
 out:
        kfree(vol_args);
        return ret;
@@ -524,13 +602,10 @@ long btrfs_ioctl_clone(struct file *file, unsigned long src_fd)
        struct file *src_file;
        struct inode *src;
        struct btrfs_trans_handle *trans;
-       struct btrfs_ordered_extent *ordered;
        struct btrfs_path *path;
        struct extent_buffer *leaf;
        char *buf;
        struct btrfs_key key;
-       struct btrfs_key new_key;
-       u32 size;
        u32 nritems;
        int slot;
        int ret;
@@ -575,6 +650,7 @@ long btrfs_ioctl_clone(struct file *file, unsigned long src_fd)
        /* do any pending delalloc/csum calc on src, one way or
           another, and lock file content */
        while (1) {
+               struct btrfs_ordered_extent *ordered;
                lock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS);
                ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
                if (BTRFS_I(src)->delalloc_bytes == 0 && !ordered)
@@ -618,6 +694,32 @@ long btrfs_ioctl_clone(struct file *file, unsigned long src_fd)
                    key.objectid != src->i_ino)
                        break;
 
+               if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY ||
+                   btrfs_key_type(&key) == BTRFS_CSUM_ITEM_KEY) {
+                       u32 size;
+                       struct btrfs_key new_key;
+
+                       size = btrfs_item_size_nr(leaf, slot);
+                       read_extent_buffer(leaf, buf,
+                                          btrfs_item_ptr_offset(leaf, slot),
+                                          size);
+                       btrfs_release_path(root, path);
+
+                       memcpy(&new_key, &key, sizeof(new_key));
+                       new_key.objectid = inode->i_ino;
+                       ret = btrfs_insert_empty_item(trans, root, path,
+                                                     &new_key, size);
+                       if (ret)
+                               goto out;
+
+                       leaf = path->nodes[0];
+                       slot = path->slots[0];
+                       write_extent_buffer(leaf, buf,
+                                           btrfs_item_ptr_offset(leaf, slot),
+                                           size);
+                       btrfs_mark_buffer_dirty(leaf);
+               }
+
                if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
                        struct btrfs_file_extent_item *extent;
                        int found_type;
@@ -633,31 +735,15 @@ long btrfs_ioctl_clone(struct file *file, unsigned long src_fd)
                                /* ds == 0 means there's a hole */
                                if (ds != 0) {
                                        ret = btrfs_inc_extent_ref(trans, root,
-                                                    ds, dl,
+                                                    ds, dl, leaf->start,
                                                     root->root_key.objectid,
                                                     trans->transid,
-                                                    inode->i_ino, key.offset);
-                                       if (ret)
-                                               goto out;
+                                                    inode->i_ino);
+                                       BUG_ON(ret);
                                }
                        }
                }
-
-               if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY ||
-                   btrfs_key_type(&key) == BTRFS_CSUM_ITEM_KEY) {
-                       size = btrfs_item_size_nr(leaf, slot);
-                       read_extent_buffer(leaf, buf,
-                                          btrfs_item_ptr_offset(leaf, slot),
-                                          size);
-                       btrfs_release_path(root, path);
-                       memcpy(&new_key, &key, sizeof(new_key));
-                       new_key.objectid = inode->i_ino;
-                       ret = btrfs_insert_item(trans, root, &new_key,
-                                               buf, size);
-                       BUG_ON(ret);
-               } else {
-                       btrfs_release_path(root, path);
-               }
+               btrfs_release_path(root, path);
                key.offset++;
        }
        ret = 0;
@@ -665,7 +751,7 @@ out:
        btrfs_release_path(root, path);
        if (ret == 0) {
                inode->i_mtime = inode->i_ctime = CURRENT_TIME;
-               inode->i_blocks = src->i_blocks;
+               inode_set_bytes(inode, inode_get_bytes(src));
                btrfs_i_size_write(inode, src->i_size);
                BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
                ret = btrfs_update_inode(trans, root, inode);
@@ -738,7 +824,7 @@ long btrfs_ioctl_trans_end(struct file *file)
                goto out;
        }
        btrfs_end_transaction(trans, root);
-       file->private_data = 0;
+       file->private_data = NULL;
 
        mutex_lock(&root->fs_info->trans_mutex);
        root->fs_info->open_ioctl_trans--;
@@ -755,7 +841,7 @@ long btrfs_ioctl(struct file *file, unsigned int
 
        switch (cmd) {
        case BTRFS_IOC_SNAP_CREATE:
-               return btrfs_ioctl_snap_create(root, (void __user *)arg);
+               return btrfs_ioctl_snap_create(file, (void __user *)arg);
        case BTRFS_IOC_DEFRAG:
                return btrfs_ioctl_defrag(file);
        case BTRFS_IOC_RESIZE: