Btrfs: Let some locks go during defrag and snapshot dropping
[linux-block.git] / fs / btrfs / extent-tree.c
index 8025e9f8ef194b0ff089d2fe4a74c8528548f76a..f7d76d34fc3752a22d26761d4b8f487c8166d6b4 100644 (file)
  * Boston, MA 021110-1307, USA.
  */
 
-#include <linux/module.h>
+#include <linux/sched.h>
 #include "ctree.h"
 #include "disk-io.h"
 #include "print-tree.h"
 #include "transaction.h"
 
 static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
-                           *orig_root, u64 num_blocks, u64 search_start,
+                           *orig_root, u64 num_blocks, u64 empty_size,
+                           u64 search_start,
                            u64 search_end, u64 hint_block,
-                           struct btrfs_key *ins, int data);
+                           struct btrfs_key *ins, u64 exclude_start,
+                           u64 exclude_nr, int data);
 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
                                 btrfs_root *extent_root);
 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
                               btrfs_root *extent_root);
 
-static void reada_extent_leaves(struct btrfs_root *root,
-                               struct btrfs_path *path, u64 limit)
-{
-       struct btrfs_node *node;
-       int i;
-       int nritems;
-       u64 item_objectid;
-       u64 blocknr;
-       int slot;
-       int ret;
-
-       if (!path->nodes[1])
-               return;
-       node = btrfs_buffer_node(path->nodes[1]);
-       slot = path->slots[1] + 1;
-       nritems = btrfs_header_nritems(&node->header);
-       for (i = slot; i < nritems && i < slot + 8; i++) {
-               item_objectid = btrfs_disk_key_objectid(&node->ptrs[i].key);
-               if (item_objectid > limit)
-                       break;
-               blocknr = btrfs_node_blockptr(node, i);
-               ret = readahead_tree_block(root, blocknr);
-               if (ret)
-                       break;
-       }
-}
-
 static int cache_block_group(struct btrfs_root *root,
                             struct btrfs_block_group_cache *block_group)
 {
@@ -83,6 +58,7 @@ static int cache_block_group(struct btrfs_root *root,
        path = btrfs_alloc_path();
        if (!path)
                return -ENOMEM;
+       path->reada = 1;
        key.objectid = block_group->key.objectid;
        key.flags = 0;
        key.offset = 0;
@@ -93,12 +69,10 @@ static int cache_block_group(struct btrfs_root *root,
        if (ret && path->slots[0] > 0)
                path->slots[0]--;
        limit = block_group->key.objectid + block_group->key.offset;
-       reada_extent_leaves(root, path, limit);
        while(1) {
                leaf = btrfs_buffer_leaf(path->nodes[0]);
                slot = path->slots[0];
                if (slot >= btrfs_header_nritems(&leaf->header)) {
-                       reada_extent_leaves(root, path, limit);
                        ret = btrfs_next_leaf(root, path);
                        if (ret < 0)
                                goto err;
@@ -406,8 +380,8 @@ int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
        path = btrfs_alloc_path();
        if (!path)
                return -ENOMEM;
-       ret = find_free_extent(trans, root->fs_info->extent_root, 0, 0,
-                              (u64)-1, 0, &ins, 0);
+       ret = find_free_extent(trans, root->fs_info->extent_root, 0, 0, 0,
+                              (u64)-1, 0, &ins, 0, 0, 0);
        if (ret) {
                btrfs_free_path(path);
                return ret;
@@ -522,6 +496,7 @@ int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
        }
        return 0;
 fail:
+       WARN_ON(1);
        for (i =0; i < faili; i++) {
                if (leaf) {
                        u64 disk_blocknr;
@@ -559,7 +534,8 @@ static int write_one_cache_group(struct btrfs_trans_handle *trans,
        struct btrfs_block_group_item *bi;
        struct btrfs_key ins;
 
-       ret = find_free_extent(trans, extent_root, 0, 0, (u64)-1, 0, &ins, 0);
+       ret = find_free_extent(trans, extent_root, 0, 0, 0, (u64)-1, 0, &ins,
+                              0, 0, 0);
        /* FIXME, set bit to recalc cache groups on next mount */
        if (ret)
                return ret;
@@ -570,7 +546,7 @@ static int write_one_cache_group(struct btrfs_trans_handle *trans,
        bi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
                            struct btrfs_block_group_item);
        memcpy(bi, &cache->item, sizeof(*bi));
-       mark_buffer_dirty(path->nodes[0]);
+       btrfs_mark_buffer_dirty(path->nodes[0]);
        btrfs_release_path(extent_root, path);
 fail:
        finish_current_insert(trans, extent_root);
@@ -733,12 +709,35 @@ static int update_block_group(struct btrfs_trans_handle *trans,
 static int try_remove_page(struct address_space *mapping, unsigned long index)
 {
        int ret;
+       return 0;
        ret = invalidate_mapping_pages(mapping, index, index);
        return ret;
 }
 
-int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
-                              btrfs_root *root)
+int btrfs_copy_pinned(struct btrfs_root *root, struct radix_tree_root *copy)
+{
+       unsigned long gang[8];
+       u64 last = 0;
+       struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
+       int ret;
+       int i;
+
+       while(1) {
+               ret = find_first_radix_bit(pinned_radix, gang, last,
+                                          ARRAY_SIZE(gang));
+               if (!ret)
+                       break;
+               for (i = 0 ; i < ret; i++) {
+                       set_radix_bit(copy, gang[i]);
+                       last = gang[i] + 1;
+               }
+       }
+       return 0;
+}
+
+int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
+                              struct btrfs_root *root,
+                              struct radix_tree_root *unpin_radix)
 {
        unsigned long gang[8];
        struct inode *btree_inode = root->fs_info->btree_inode;
@@ -750,7 +749,7 @@ int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
        struct radix_tree_root *extent_radix = &root->fs_info->extent_map_radix;
 
        while(1) {
-               ret = find_first_radix_bit(pinned_radix, gang, 0,
+               ret = find_first_radix_bit(unpin_radix, gang, 0,
                                           ARRAY_SIZE(gang));
                if (!ret)
                        break;
@@ -758,6 +757,7 @@ int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
                        first = gang[0];
                for (i = 0; i < ret; i++) {
                        clear_radix_bit(pinned_radix, gang[i]);
+                       clear_radix_bit(unpin_radix, gang[i]);
                        block_group = btrfs_lookup_block_group(root->fs_info,
                                                               gang[i]);
                        if (block_group) {
@@ -796,8 +796,8 @@ static int finish_current_insert(struct btrfs_trans_handle *trans, struct
 
        for (i = 0; i < extent_root->fs_info->extent_tree_insert_nr; i++) {
                ins.objectid = extent_root->fs_info->extent_tree_insert[i];
-               super_blocks_used = btrfs_super_blocks_used(info->disk_super);
-               btrfs_set_super_blocks_used(info->disk_super,
+               super_blocks_used = btrfs_super_blocks_used(&info->super_copy);
+               btrfs_set_super_blocks_used(&info->super_copy,
                                            super_blocks_used + 1);
                ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
                                        sizeof(extent_item));
@@ -868,7 +868,7 @@ static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
        if (!path)
                return -ENOMEM;
 
-       ret = find_free_extent(trans, root, 0, 0, (u64)-1, 0, &ins, 0);
+       ret = find_free_extent(trans, root, 0, 0, 0, (u64)-1, 0, &ins, 0, 0, 0);
        if (ret) {
                btrfs_free_path(path);
                return ret;
@@ -892,8 +892,8 @@ static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
                        BUG_ON(ret);
                }
 
-               super_blocks_used = btrfs_super_blocks_used(info->disk_super);
-               btrfs_set_super_blocks_used(info->disk_super,
+               super_blocks_used = btrfs_super_blocks_used(&info->super_copy);
+               btrfs_set_super_blocks_used(&info->super_copy,
                                            super_blocks_used - num_blocks);
                ret = btrfs_del_item(trans, extent_root, path);
                if (ret) {
@@ -985,9 +985,10 @@ int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
  * Any available blocks before search_start are skipped.
  */
 static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
-                           *orig_root, u64 num_blocks, u64 search_start, u64
-                           search_end, u64 hint_block,
-                           struct btrfs_key *ins, int data)
+                           *orig_root, u64 num_blocks, u64 empty_size,
+                           u64 search_start, u64 search_end, u64 hint_block,
+                           struct btrfs_key *ins, u64 exclude_start,
+                           u64 exclude_nr, int data)
 {
        struct btrfs_path *path;
        struct btrfs_key key;
@@ -1017,7 +1018,7 @@ static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
        if (num_blocks == 0) {
                fill_prealloc = 1;
                num_blocks = 1;
-               total_needed = (min(level + 1, BTRFS_MAX_LEVEL) + 2) * 3;
+               total_needed = (min(level + 1, BTRFS_MAX_LEVEL)) * 6;
        }
        if (fill_prealloc) {
                u64 first;
@@ -1032,7 +1033,7 @@ static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
                info->extent_tree_prealloc_nr = 0;
        }
        if (search_end == (u64)-1)
-               search_end = btrfs_super_total_blocks(info->disk_super);
+               search_end = btrfs_super_total_blocks(&info->super_copy);
        if (hint_block) {
                block_group = btrfs_lookup_block_group(info, hint_block);
                block_group = btrfs_find_block_group(root, block_group,
@@ -1043,6 +1044,7 @@ static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
                                                     data, 1);
        }
 
+       total_needed += empty_size;
        path = btrfs_alloc_path();
 
 check_failed:
@@ -1158,9 +1160,11 @@ check_pending:
                        goto error;
                }
                search_start = orig_search_start;
-               if (wrapped)
+               if (wrapped) {
+                       if (!full_scan)
+                               total_needed -= empty_size;
                        full_scan = 1;
-               else
+               else
                        wrapped = 1;
                goto new_group;
        }
@@ -1191,6 +1195,11 @@ check_pending:
                        goto new_group;
                }
        }
+       if (exclude_nr > 0 && (ins->objectid + num_blocks > exclude_start &&
+           ins->objectid < exclude_start + exclude_nr)) {
+               search_start = exclude_start + exclude_nr;
+               goto new_group;
+       }
        if (fill_prealloc) {
                int nr;
                test_block = ins->objectid;
@@ -1234,9 +1243,11 @@ new_group:
                        ret = -ENOSPC;
                        goto error;
                }
-               if (wrapped)
+               if (wrapped) {
+                       if (!full_scan)
+                               total_needed -= empty_size;
                        full_scan = 1;
-               else
+               else
                        wrapped = 1;
        }
        block_group = btrfs_lookup_block_group(info, search_start);
@@ -1260,13 +1271,15 @@ error:
  */
 int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
                       struct btrfs_root *root, u64 owner,
-                      u64 num_blocks, u64 hint_block,
+                      u64 num_blocks, u64 empty_size, u64 hint_block,
                       u64 search_end, struct btrfs_key *ins, int data)
 {
        int ret;
        int pending_ret;
        u64 super_blocks_used;
        u64 search_start = 0;
+       u64 exclude_start = 0;
+       u64 exclude_nr = 0;
        struct btrfs_fs_info *info = root->fs_info;
        struct btrfs_root *extent_root = info->extent_root;
        struct btrfs_extent_item extent_item;
@@ -1287,6 +1300,8 @@ int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
                        ins->objectid;
                ret = update_block_group(trans, root,
                                         ins->objectid, ins->offset, 1, 0, 0);
+               WARN_ON(info->extent_tree_insert_nr >
+                       ARRAY_SIZE(info->extent_tree_insert));
                BUG_ON(ret);
                return 0;
        }
@@ -1297,35 +1312,23 @@ int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
         * in the correct block group.
         */
        if (data) {
-               ret = find_free_extent(trans, root, 0, 0,
-                                      search_end, 0, &prealloc_key, 0);
-               if (ret) {
-                       return ret;
-               }
-               if (prealloc_key.objectid + prealloc_key.offset >= search_end) {
-                       int nr = info->extent_tree_prealloc_nr;
-                       search_end = info->extent_tree_prealloc[nr - 1] - 1;
-               } else {
-                       search_start = info->extent_tree_prealloc[0] + 1;
-               }
-       }
-       if (hint_block < search_start)
-               hint_block = search_start;
-       /* do the real allocation */
-       ret = find_free_extent(trans, root, num_blocks, search_start,
-                              search_end, hint_block, ins, data);
-       if (ret) {
-               if (search_start == 0)
-                       return ret;
-               search_end = search_start - 1;
-               search_start = 0;
-               hint_block = search_start;
-               ret = find_free_extent(trans, root, num_blocks, search_start,
-                                      search_end, hint_block, ins, data);
+               ret = find_free_extent(trans, root, 0, 0, 0,
+                                      search_end, 0, &prealloc_key, 0, 0, 0);
+               BUG_ON(ret);
                if (ret)
                        return ret;
+               exclude_nr = info->extent_tree_prealloc_nr;
+               exclude_start = info->extent_tree_prealloc[exclude_nr - 1];
        }
 
+       /* do the real allocation */
+       ret = find_free_extent(trans, root, num_blocks, empty_size,
+                              search_start, search_end, hint_block, ins,
+                              exclude_start, exclude_nr, data);
+       BUG_ON(ret);
+       if (ret)
+               return ret;
+
        /*
         * if we're doing a metadata allocation, preallocate space in the
         * extent tree second.  This way, we don't create a tiny hole
@@ -1336,37 +1339,25 @@ int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
         * The unused prealloc will get reused the next time around.
         */
        if (!data) {
-               if (ins->objectid + ins->offset >= search_end)
-                       search_end = ins->objectid - 1;
-               else
-                       search_start = ins->objectid + ins->offset;
-
-               if (hint_block < search_start)
-                       hint_block = search_start;
-
-               ret = find_free_extent(trans, root, 0, search_start,
+               exclude_start = ins->objectid;
+               exclude_nr = ins->offset;
+               hint_block = exclude_start + exclude_nr;
+               ret = find_free_extent(trans, root, 0, 0, search_start,
                                       search_end, hint_block,
-                                      &prealloc_key, 0);
-               if (ret) {
-                       if (search_start == 0)
-                               return ret;
-                       search_end = search_start - 1;
-                       search_start = 0;
-                       hint_block = search_start;
-                       ret = find_free_extent(trans, root, 0, search_start,
-                                              search_end, hint_block,
-                                              &prealloc_key, 0);
-                       if (ret)
-                               return ret;
-               }
+                                      &prealloc_key, exclude_start,
+                                      exclude_nr, 0);
+               BUG_ON(ret);
+               if (ret)
+                       return ret;
        }
 
-       super_blocks_used = btrfs_super_blocks_used(info->disk_super);
-       btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
+       super_blocks_used = btrfs_super_blocks_used(&info->super_copy);
+       btrfs_set_super_blocks_used(&info->super_copy, super_blocks_used +
                                    num_blocks);
        ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
                                sizeof(extent_item));
 
+       BUG_ON(ret);
        finish_current_insert(trans, extent_root);
        pending_ret = del_pending_extents(trans, extent_root);
        if (ret) {
@@ -1386,14 +1377,16 @@ int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
  * returns the tree buffer or NULL.
  */
 struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
-                                          struct btrfs_root *root, u64 hint)
+                                          struct btrfs_root *root, u64 hint,
+                                          u64 empty_size)
 {
        struct btrfs_key ins;
        int ret;
        struct buffer_head *buf;
 
        ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
-                                1, hint, (unsigned long)-1, &ins, 0);
+                                1, empty_size, hint,
+                                (unsigned long)-1, &ins, 0);
        if (ret) {
                BUG_ON(ret > 0);
                return ERR_PTR(ret);
@@ -1403,6 +1396,7 @@ struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
                btrfs_free_extent(trans, root, ins.objectid, 1, 0);
                return ERR_PTR(-ENOMEM);
        }
+       WARN_ON(buffer_dirty(buf));
        set_buffer_uptodate(buf);
        set_buffer_checked(buf);
        set_radix_bit(&trans->transaction->dirty_pages, buf->b_page->index);
@@ -1551,12 +1545,21 @@ static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
        int i;
        int slot;
        int ret;
+       struct btrfs_root_item *root_item = &root->root_item;
+
        for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
                slot = path->slots[i];
                if (slot < btrfs_header_nritems(
                    btrfs_buffer_header(path->nodes[i])) - 1) {
+                       struct btrfs_node *node;
+                       node = btrfs_buffer_node(path->nodes[i]);
                        path->slots[i]++;
                        *level = i;
+                       WARN_ON(*level == 0);
+                       memcpy(&root_item->drop_progress,
+                              &node->ptrs[path->slots[i]].key,
+                              sizeof(root_item->drop_progress));
+                       root_item->drop_level = i;
                        return 0;
                } else {
                        ret = btrfs_free_extent(trans, root,
@@ -1577,7 +1580,7 @@ static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
  * decremented.
  */
 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
-                       *root, struct buffer_head *snap)
+                       *root)
 {
        int ret = 0;
        int wret;
@@ -1585,14 +1588,35 @@ int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
        struct btrfs_path *path;
        int i;
        int orig_level;
+       int num_walks = 0;
+       struct btrfs_root_item *root_item = &root->root_item;
 
        path = btrfs_alloc_path();
        BUG_ON(!path);
 
-       level = btrfs_header_level(btrfs_buffer_header(snap));
+       level = btrfs_header_level(btrfs_buffer_header(root->node));
        orig_level = level;
-       path->nodes[level] = snap;
-       path->slots[level] = 0;
+       if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
+               path->nodes[level] = root->node;
+               path->slots[level] = 0;
+       } else {
+               struct btrfs_key key;
+               struct btrfs_disk_key *found_key;
+               struct btrfs_node *node;
+
+               btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
+               level = root_item->drop_level;
+               path->lowest_level = level;
+               wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
+               if (wret < 0) {
+                       ret = wret;
+                       goto out;
+               }
+               node = btrfs_buffer_node(path->nodes[level]);
+               found_key = &node->ptrs[path->slots[level]].key;
+               WARN_ON(memcmp(found_key, &root_item->drop_progress,
+                              sizeof(*found_key)));
+       }
        while(1) {
                wret = walk_down_tree(trans, root, path, &level);
                if (wret > 0)
@@ -1605,12 +1629,20 @@ int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
                        break;
                if (wret < 0)
                        ret = wret;
+               num_walks++;
+               if (num_walks > 2) {
+                       ret = -EAGAIN;
+                       get_bh(root->node);
+                       break;
+               }
        }
        for (i = 0; i <= orig_level; i++) {
                if (path->nodes[i]) {
                        btrfs_block_release(root, path->nodes[i]);
+                       path->nodes[i] = 0;
                }
        }
+out:
        btrfs_free_path(path);
        return ret;
 }
@@ -1737,7 +1769,7 @@ int btrfs_read_block_groups(struct btrfs_root *root)
                                           BTRFS_BLOCK_GROUP_AVAIL);
                }
                if (key.objectid >=
-                   btrfs_super_total_blocks(info->disk_super))
+                   btrfs_super_total_blocks(&info->super_copy))
                        break;
        }