Btrfs: add snapshot/subvolume destroy ioctl
[cascardo/linux.git] / fs / btrfs / transaction.c
index 2dbf1c1..88f866f 100644 (file)
@@ -40,6 +40,12 @@ static noinline void put_transaction(struct btrfs_transaction *transaction)
        }
 }
 
+static noinline void switch_commit_root(struct btrfs_root *root)
+{
+       free_extent_buffer(root->commit_root);
+       root->commit_root = btrfs_root_node(root);
+}
+
 /*
  * either allocate a new transaction or hop into the existing one
  */
@@ -98,7 +104,6 @@ static noinline int record_root_in_trans(struct btrfs_trans_handle *trans,
 {
        if (root->ref_cows && root->last_trans < trans->transid) {
                WARN_ON(root == root->fs_info->extent_root);
-               WARN_ON(root->root_item.refs == 0);
                WARN_ON(root->commit_root != root->node);
 
                radix_tree_tag_set(&root->fs_info->fs_roots_radix,
@@ -444,9 +449,6 @@ static int update_cowonly_root(struct btrfs_trans_handle *trans,
 
        btrfs_write_dirty_block_groups(trans, root);
 
-       ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
-       BUG_ON(ret);
-
        while (1) {
                old_root_bytenr = btrfs_root_bytenr(&root->root_item);
                if (old_root_bytenr == root->node->start)
@@ -457,13 +459,14 @@ static int update_cowonly_root(struct btrfs_trans_handle *trans,
                                        &root->root_key,
                                        &root->root_item);
                BUG_ON(ret);
-               btrfs_write_dirty_block_groups(trans, root);
 
-               ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
+               ret = btrfs_write_dirty_block_groups(trans, root);
                BUG_ON(ret);
        }
-       free_extent_buffer(root->commit_root);
-       root->commit_root = btrfs_root_node(root);
+
+       if (root != root->fs_info->extent_root)
+               switch_commit_root(root);
+
        return 0;
 }
 
@@ -495,10 +498,12 @@ static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans,
                root = list_entry(next, struct btrfs_root, dirty_list);
 
                update_cowonly_root(trans, root);
-
-               ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
-               BUG_ON(ret);
        }
+
+       down_write(&fs_info->extent_commit_sem);
+       switch_commit_root(fs_info->extent_root);
+       up_write(&fs_info->extent_commit_sem);
+
        return 0;
 }
 
@@ -544,8 +549,7 @@ static noinline int commit_fs_roots(struct btrfs_trans_handle *trans,
                        btrfs_update_reloc_root(trans, root);
 
                        if (root->commit_root != root->node) {
-                               free_extent_buffer(root->commit_root);
-                               root->commit_root = btrfs_root_node(root);
+                               switch_commit_root(root);
                                btrfs_set_root_node(&root->root_item,
                                                    root->node);
                        }
@@ -715,7 +719,8 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
        memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
 
        key.objectid = objectid;
-       key.offset = 0;
+       /* record when the snapshot was created in key.offset */
+       key.offset = trans->transid;
        btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
 
        old = btrfs_lock_root_node(root);
@@ -773,24 +778,14 @@ static noinline int finish_pending_snapshot(struct btrfs_fs_info *fs_info,
        ret = btrfs_update_inode(trans, parent_root, parent_inode);
        BUG_ON(ret);
 
-       /* add the backref first */
        ret = btrfs_add_root_ref(trans, parent_root->fs_info->tree_root,
                                 pending->root_key.objectid,
-                                BTRFS_ROOT_BACKREF_KEY,
                                 parent_root->root_key.objectid,
                                 parent_inode->i_ino, index, pending->name,
                                 namelen);
 
        BUG_ON(ret);
 
-       /* now add the forward ref */
-       ret = btrfs_add_root_ref(trans, parent_root->fs_info->tree_root,
-                                parent_root->root_key.objectid,
-                                BTRFS_ROOT_REF_KEY,
-                                pending->root_key.objectid,
-                                parent_inode->i_ino, index, pending->name,
-                                namelen);
-
        inode = btrfs_lookup_dentry(parent_inode, pending->dentry);
        d_instantiate(pending->dentry, inode);
 fail:
@@ -852,6 +847,16 @@ static void update_super_roots(struct btrfs_root *root)
        super->root_level = root_item->level;
 }
 
+int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
+{
+       int ret = 0;
+       spin_lock(&info->new_trans_lock);
+       if (info->running_transaction)
+               ret = info->running_transaction->in_commit;
+       spin_unlock(&info->new_trans_lock);
+       return ret;
+}
+
 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
                             struct btrfs_root *root)
 {
@@ -859,7 +864,6 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
        unsigned long timeout = 1;
        struct btrfs_transaction *cur_trans;
        struct btrfs_transaction *prev_trans = NULL;
-       struct extent_io_tree *pinned_copy;
        DEFINE_WAIT(wait);
        int ret;
        int should_grow = 0;
@@ -900,13 +904,6 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
                return 0;
        }
 
-       pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS);
-       if (!pinned_copy)
-               return -ENOMEM;
-
-       extent_io_tree_init(pinned_copy,
-                            root->fs_info->btree_inode->i_mapping, GFP_NOFS);
-
        trans->transaction->in_commit = 1;
        trans->transaction->blocked = 1;
        if (cur_trans->list.prev != &root->fs_info->trans_list) {
@@ -943,9 +940,11 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
 
                mutex_unlock(&root->fs_info->trans_mutex);
 
-               if (flush_on_commit || snap_pending) {
-                       if (flush_on_commit)
-                               btrfs_start_delalloc_inodes(root);
+               if (flush_on_commit) {
+                       btrfs_start_delalloc_inodes(root);
+                       ret = btrfs_wait_ordered_extents(root, 0);
+                       BUG_ON(ret);
+               } else if (snap_pending) {
                        ret = btrfs_wait_ordered_extents(root, 1);
                        BUG_ON(ret);
                }
@@ -1002,6 +1001,8 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
        ret = commit_cowonly_roots(trans, root);
        BUG_ON(ret);
 
+       btrfs_prepare_extent_commit(trans, root);
+
        cur_trans = root->fs_info->running_transaction;
        spin_lock(&root->fs_info->new_trans_lock);
        root->fs_info->running_transaction = NULL;
@@ -1009,15 +1010,11 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
 
        btrfs_set_root_node(&root->fs_info->tree_root->root_item,
                            root->fs_info->tree_root->node);
-       free_extent_buffer(root->fs_info->tree_root->commit_root);
-       root->fs_info->tree_root->commit_root =
-                               btrfs_root_node(root->fs_info->tree_root);
+       switch_commit_root(root->fs_info->tree_root);
 
        btrfs_set_root_node(&root->fs_info->chunk_root->root_item,
                            root->fs_info->chunk_root->node);
-       free_extent_buffer(root->fs_info->chunk_root->commit_root);
-       root->fs_info->chunk_root->commit_root =
-                               btrfs_root_node(root->fs_info->chunk_root);
+       switch_commit_root(root->fs_info->chunk_root);
 
        update_super_roots(root);
 
@@ -1029,8 +1026,6 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
        memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
               sizeof(root->fs_info->super_copy));
 
-       btrfs_copy_pinned(root, pinned_copy);
-
        trans->transaction->blocked = 0;
 
        wake_up(&root->fs_info->transaction_wait);
@@ -1046,8 +1041,7 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
         */
        mutex_unlock(&root->fs_info->tree_log_mutex);
 
-       btrfs_finish_extent_commit(trans, root, pinned_copy);
-       kfree(pinned_copy);
+       btrfs_finish_extent_commit(trans, root);
 
        /* do the directory inserts of any pending snapshot creations */
        finish_pending_snapshots(trans, root->fs_info);
@@ -1057,6 +1051,7 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
        cur_trans->commit_done = 1;
 
        root->fs_info->last_trans_committed = cur_trans->transid;
+
        wake_up(&cur_trans->commit_wait);
 
        put_transaction(cur_trans);
@@ -1082,8 +1077,13 @@ int btrfs_clean_old_snapshots(struct btrfs_root *root)
 
        while (!list_empty(&list)) {
                root = list_entry(list.next, struct btrfs_root, root_list);
-               list_del_init(&root->root_list);
-               btrfs_drop_snapshot(root, 0);
+               list_del(&root->root_list);
+
+               if (btrfs_header_backref_rev(root->node) <
+                   BTRFS_MIXED_BACKREF_REV)
+                       btrfs_drop_snapshot(root, 0);
+               else
+                       btrfs_drop_snapshot(root, 1);
        }
        return 0;
 }