Btrfs: Tune readahead during defrag to avoid reading too much at once
[linux-block.git] / fs / btrfs / extent-tree.c
index d6243c2afc73da4c6e2b0002878f8388512299a1..c4fe37791d3f6346d00831fb05378fc358c04c76 100644 (file)
@@ -33,6 +33,28 @@ 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 int find_previous_extent(struct btrfs_root *root,
+                               struct btrfs_path *path)
+{
+       struct btrfs_key found_key;
+       struct extent_buffer *leaf;
+       int ret;
+
+       while(1) {
+               if (path->slots[0] == 0) {
+                       ret = btrfs_prev_leaf(root, path);
+                       if (ret != 0)
+                               return ret;
+               } else {
+                       path->slots[0]--;
+               }
+               leaf = path->nodes[0];
+               btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
+               if (found_key.type == BTRFS_EXTENT_ITEM_KEY)
+                       return 0;
+       }
+       return 1;
+}
 
 static int cache_block_group(struct btrfs_root *root,
                             struct btrfs_block_group_cache *block_group)
@@ -41,7 +63,7 @@ static int cache_block_group(struct btrfs_root *root,
        int ret;
        struct btrfs_key key;
        struct extent_buffer *leaf;
-       struct extent_map_tree *free_space_cache;
+       struct extent_io_tree *free_space_cache;
        int slot;
        u64 last = 0;
        u64 hole_size;
@@ -65,16 +87,19 @@ static int cache_block_group(struct btrfs_root *root,
        first_free = block_group->key.objectid;
        key.objectid = block_group->key.objectid;
        key.offset = 0;
-
        btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
        ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
-
        if (ret < 0)
                return ret;
-
-       if (ret && path->slots[0] > 0)
-               path->slots[0]--;
-
+       ret = find_previous_extent(root, path);
+       if (ret < 0)
+               return ret;
+       if (ret == 0) {
+               leaf = path->nodes[0];
+               btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
+               if (key.objectid + key.offset > first_free)
+                       first_free = key.objectid + key.offset;
+       }
        while(1) {
                leaf = path->nodes[0];
                slot = path->slots[0];
@@ -88,15 +113,10 @@ static int cache_block_group(struct btrfs_root *root,
                                break;
                        }
                }
-
                btrfs_item_key_to_cpu(leaf, &key, slot);
                if (key.objectid < block_group->key.objectid) {
-                       if (btrfs_key_type(&key) != BTRFS_EXTENT_REF_KEY &&
-                           key.objectid + key.offset > first_free)
-                               first_free = key.objectid + key.offset;
                        goto next;
                }
-
                if (key.objectid >= block_group->key.objectid +
                    block_group->key.offset) {
                        break;
@@ -138,7 +158,7 @@ struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
                                                         btrfs_fs_info *info,
                                                         u64 bytenr)
 {
-       struct extent_map_tree *block_group_cache;
+       struct extent_io_tree *block_group_cache;
        struct btrfs_block_group_cache *block_group = NULL;
        u64 ptr;
        u64 start;
@@ -162,10 +182,9 @@ struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
                return block_group;
        return NULL;
 }
-static u64 find_search_start(struct btrfs_root *root,
-                            struct btrfs_block_group_cache **cache_ret,
-                            u64 search_start, int num,
-                            int data, int full_scan)
+static u64 noinline find_search_start(struct btrfs_root *root,
+                             struct btrfs_block_group_cache **cache_ret,
+                             u64 search_start, int num, int data)
 {
        int ret;
        struct btrfs_block_group_cache *cache = *cache_ret;
@@ -173,11 +192,13 @@ static u64 find_search_start(struct btrfs_root *root,
        u64 start = 0;
        u64 end = 0;
        u64 cache_miss = 0;
+       u64 total_fs_bytes;
        int wrapped = 0;
 
        if (!cache) {
                goto out;
        }
+       total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
 again:
        ret = cache_block_group(root, cache);
        if (ret)
@@ -204,6 +225,8 @@ again:
                if (data != BTRFS_BLOCK_GROUP_MIXED &&
                    start + num > cache->key.objectid + cache->key.offset)
                        goto new_group;
+               if (start + num  > total_fs_bytes)
+                       goto new_group;
                return start;
        }
 out:
@@ -220,7 +243,7 @@ new_group:
        last = cache->key.objectid + cache->key.offset;
 wrapped:
        cache = btrfs_lookup_block_group(root->fs_info, last);
-       if (!cache) {
+       if (!cache || cache->key.objectid >= total_fs_bytes) {
 no_cache:
                if (!wrapped) {
                        wrapped = 1;
@@ -258,7 +281,7 @@ struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
                                                 int data, int owner)
 {
        struct btrfs_block_group_cache *cache;
-       struct extent_map_tree *block_group_cache;
+       struct extent_io_tree *block_group_cache;
        struct btrfs_block_group_cache *found_group = NULL;
        struct btrfs_fs_info *info = root->fs_info;
        u64 used;
@@ -268,6 +291,7 @@ struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
        u64 end;
        u64 free_check;
        u64 ptr;
+       u64 total_fs_bytes;
        int bit;
        int ret;
        int full_search = 0;
@@ -275,6 +299,7 @@ struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
        int data_swap = 0;
 
        block_group_cache = &info->block_group_cache;
+       total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
 
        if (!owner)
                factor = 8;
@@ -287,7 +312,7 @@ struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
        else
                bit = BLOCK_GROUP_METADATA;
 
-       if (search_start) {
+       if (search_start && search_start < total_fs_bytes) {
                struct btrfs_block_group_cache *shint;
                shint = btrfs_lookup_block_group(info, search_start);
                if (shint && (shint->data == data ||
@@ -299,8 +324,8 @@ struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
                        }
                }
        }
-       if (hint && (hint->data == data ||
-                    hint->data == BTRFS_BLOCK_GROUP_MIXED)) {
+       if (hint && hint->key.objectid < total_fs_bytes &&
+           (hint->data == data || hint->data == BTRFS_BLOCK_GROUP_MIXED)) {
                used = btrfs_block_group_used(&hint->item);
                if (used + hint->pinned <
                    div_factor(hint->key.offset, factor)) {
@@ -314,6 +339,8 @@ struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
                else
                        hint_last = search_start;
 
+               if (hint_last >= total_fs_bytes)
+                       hint_last = search_start;
                last = hint_last;
        }
 again:
@@ -331,6 +358,9 @@ again:
                last = cache->key.objectid + cache->key.offset;
                used = btrfs_block_group_used(&cache->item);
 
+               if (cache->key.objectid > total_fs_bytes)
+                       break;
+
                if (full_search)
                        free_check = cache->key.offset;
                else
@@ -393,11 +423,12 @@ static int match_extent_ref(struct extent_buffer *leaf,
        return ret == 0;
 }
 
-static int lookup_extent_backref(struct btrfs_trans_handle *trans,
-                                struct btrfs_root *root,
-                                struct btrfs_path *path, u64 bytenr,
-                                u64 root_objectid, u64 ref_generation,
-                                u64 owner, u64 owner_offset, int del)
+static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
+                                         struct btrfs_root *root,
+                                         struct btrfs_path *path, u64 bytenr,
+                                         u64 root_objectid,
+                                         u64 ref_generation, u64 owner,
+                                         u64 owner_offset, int del)
 {
        u64 hash;
        struct btrfs_key key;
@@ -610,6 +641,7 @@ int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
        if (!path)
                return -ENOMEM;
 
+       path->reada = 0;
        key.objectid = bytenr;
        btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
        key.offset = num_bytes;
@@ -629,6 +661,7 @@ int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
 
        btrfs_release_path(root->fs_info->extent_root, path);
 
+       path->reada = 0;
        ret = btrfs_insert_extent_backref(trans, root->fs_info->extent_root,
                                          path, bytenr, root_objectid,
                                          ref_generation, owner, owner_offset);
@@ -660,6 +693,7 @@ static int lookup_extent_ref(struct btrfs_trans_handle *trans,
 
        WARN_ON(num_bytes < root->sectorsize);
        path = btrfs_alloc_path();
+       path->reada = 0;
        key.objectid = bytenr;
        key.offset = num_bytes;
        btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
@@ -688,10 +722,9 @@ u32 btrfs_count_snapshots_in_path(struct btrfs_root *root,
        struct btrfs_path *path;
        u64 bytenr;
        u64 found_objectid;
-       u64 root_objectid = 0;
+       u64 root_objectid = root->root_key.objectid;
        u32 total_count = 0;
        u32 cur_count;
-       u32 refs;
        u32 nritems;
        int ret;
        struct btrfs_key key;
@@ -727,8 +760,8 @@ again:
        }
 
        item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
-       refs = btrfs_extent_refs(l, item);
        while (1) {
+               l = path->nodes[0];
                nritems = btrfs_header_nritems(l);
                if (path->slots[0] >= nritems) {
                        ret = btrfs_next_leaf(extent_root, path);
@@ -739,6 +772,7 @@ again:
                btrfs_item_key_to_cpu(l, &found_key, path->slots[0]);
                if (found_key.objectid != bytenr)
                        break;
+
                if (found_key.type != BTRFS_EXTENT_REF_KEY) {
                        path->slots[0]++;
                        continue;
@@ -749,23 +783,17 @@ again:
                                          struct btrfs_extent_ref);
                found_objectid = btrfs_ref_root(l, ref_item);
 
-               if (found_objectid != root_objectid)
-                       total_count++;
-
-               if (total_count > 1)
+               if (found_objectid != root_objectid) {
+                       total_count = 2;
                        goto out;
-
-               if (root_objectid == 0)
-                       root_objectid = found_objectid;
-
+               }
+               total_count = 1;
                path->slots[0]++;
        }
        if (cur_count == 0) {
                total_count = 0;
                goto out;
        }
-       if (total_count > 1)
-               goto out;
        if (level >= 0 && root->node == count_path->nodes[level])
                goto out;
        level++;
@@ -775,9 +803,7 @@ again:
 out:
        btrfs_free_path(path);
        return total_count;
-
 }
-
 int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
                       struct btrfs_root *root, u64 owner_objectid)
 {
@@ -925,7 +951,7 @@ fail:
 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
                                   struct btrfs_root *root)
 {
-       struct extent_map_tree *block_group_cache;
+       struct extent_io_tree *block_group_cache;
        struct btrfs_block_group_cache *cache;
        int ret;
        int err = 0;
@@ -1081,12 +1107,12 @@ static int update_pinned_extents(struct btrfs_root *root,
        return 0;
 }
 
-int btrfs_copy_pinned(struct btrfs_root *root, struct extent_map_tree *copy)
+int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
 {
        u64 last = 0;
        u64 start;
        u64 end;
-       struct extent_map_tree *pinned_extents = &root->fs_info->pinned_extents;
+       struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
        int ret;
 
        while(1) {
@@ -1102,12 +1128,12 @@ int btrfs_copy_pinned(struct btrfs_root *root, struct extent_map_tree *copy)
 
 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
                               struct btrfs_root *root,
-                              struct extent_map_tree *unpin)
+                              struct extent_io_tree *unpin)
 {
        u64 start;
        u64 end;
        int ret;
-       struct extent_map_tree *free_space_cache;
+       struct extent_io_tree *free_space_cache;
        free_space_cache = &root->fs_info->free_space_cache;
 
        while(1) {
@@ -1122,8 +1148,8 @@ int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
        return 0;
 }
 
-static int finish_current_insert(struct btrfs_trans_handle *trans, struct
-                                btrfs_root *extent_root)
+static int finish_current_insert(struct btrfs_trans_handle *trans,
+                                struct btrfs_root *extent_root)
 {
        u64 start;
        u64 end;
@@ -1183,7 +1209,10 @@ static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
                        if (btrfs_buffer_uptodate(buf)) {
                                u64 transid =
                                    root->fs_info->running_transaction->transid;
-                               if (btrfs_header_generation(buf) == transid) {
+                               u64 header_transid =
+                                       btrfs_header_generation(buf);
+                               if (header_transid == transid) {
+                                       clean_tree_block(NULL, root, buf);
                                        free_extent_buffer(buf);
                                        return 1;
                                }
@@ -1221,11 +1250,11 @@ static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
        key.objectid = bytenr;
        btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
        key.offset = num_bytes;
-
        path = btrfs_alloc_path();
        if (!path)
                return -ENOMEM;
 
+       path->reada = 0;
        ret = lookup_extent_backref(trans, extent_root, path,
                                    bytenr, root_objectid,
                                    ref_generation,
@@ -1300,8 +1329,8 @@ static int del_pending_extents(struct btrfs_trans_handle *trans, struct
        int err = 0;
        u64 start;
        u64 end;
-       struct extent_map_tree *pending_del;
-       struct extent_map_tree *pinned_extents;
+       struct extent_io_tree *pending_del;
+       struct extent_io_tree *pinned_extents;
 
        pending_del = &extent_root->fs_info->pending_del;
        pinned_extents = &extent_root->fs_info->pinned_extents;
@@ -1366,11 +1395,13 @@ static u64 stripe_align(struct btrfs_root *root, u64 val)
  * ins->offset == number of blocks
  * 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_bytes, u64 empty_size,
-                           u64 search_start, u64 search_end, u64 hint_byte,
-                           struct btrfs_key *ins, u64 exclude_start,
-                           u64 exclude_nr, int data)
+static int noinline find_free_extent(struct btrfs_trans_handle *trans,
+                                    struct btrfs_root *orig_root,
+                                    u64 num_bytes, u64 empty_size,
+                                    u64 search_start, u64 search_end,
+                                    u64 hint_byte, struct btrfs_key *ins,
+                                    u64 exclude_start, u64 exclude_nr,
+                                    int data)
 {
        struct btrfs_path *path;
        struct btrfs_key key;
@@ -1400,8 +1431,21 @@ static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
                data = BTRFS_BLOCK_GROUP_MIXED;
        }
 
-       if (search_end == (u64)-1)
-               search_end = btrfs_super_total_bytes(&info->super_copy);
+       /* for SSD, cluster allocations together as much as possible */
+       if (btrfs_test_opt(root, SSD)) {
+               if (!data) {
+                       if (root->fs_info->last_alloc)
+                               hint_byte = root->fs_info->last_alloc;
+                       else {
+                               hint_byte = hint_byte &
+                                       ~((u64)BTRFS_BLOCK_GROUP_SIZE - 1);
+                               empty_size += 16 * 1024 * 1024;
+                       }
+               }
+       }
+
+       search_end = min(search_end,
+                        btrfs_super_total_bytes(&info->super_copy));
        if (hint_byte) {
                block_group = btrfs_lookup_block_group(info, hint_byte);
                if (!block_group)
@@ -1424,7 +1468,20 @@ check_failed:
                                                       orig_search_start);
        }
        search_start = find_search_start(root, &block_group, search_start,
-                                        total_needed, data, full_scan);
+                                        total_needed, data);
+
+       if (!data && btrfs_test_opt(root, SSD) && info->last_alloc &&
+           search_start != info->last_alloc) {
+               info->last_alloc = 0;
+               if (!empty_size) {
+                       empty_size += 16 * 1024 * 1024;
+                       total_needed += empty_size;
+               }
+               search_start = find_search_start(root, &block_group,
+                                                search_start, total_needed,
+                                                data);
+       }
+
        search_start = stripe_align(root, search_start);
        cached_start = search_start;
        btrfs_init_path(path);
@@ -1436,35 +1493,11 @@ check_failed:
        ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
        if (ret < 0)
                goto error;
-
-       if (path->slots[0] > 0) {
-               path->slots[0]--;
-       }
-
+       ret = find_previous_extent(root, path);
+       if (ret < 0)
+               goto error;
        l = path->nodes[0];
        btrfs_item_key_to_cpu(l, &key, path->slots[0]);
-
-       /*
-        * walk backwards to find the first extent item key
-        */
-       while(btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
-               if (path->slots[0] == 0) {
-                       ret = btrfs_prev_leaf(root, path);
-                       if (ret != 0) {
-                               ret = btrfs_search_slot(trans, root, ins,
-                                                       path, 0, 0);
-                               if (ret < 0)
-                                       goto error;
-                               if (path->slots[0] > 0)
-                                       path->slots[0]--;
-                               break;
-                       }
-               } else {
-                       path->slots[0]--;
-               }
-               l = path->nodes[0];
-               btrfs_item_key_to_cpu(l, &key, path->slots[0]);
-       }
        while (1) {
                l = path->nodes[0];
                slot = path->slots[0];
@@ -1603,6 +1636,8 @@ enospc:
 error:
        btrfs_release_path(root, path);
        btrfs_free_path(path);
+       if (btrfs_test_opt(root, SSD) && !ret && !data)
+               info->last_alloc = ins->objectid + ins->offset;
        return ret;
 }
 /*
@@ -1621,7 +1656,8 @@ int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
 {
        int ret;
        int pending_ret;
-       u64 super_used, root_used;
+       u64 super_used;
+       u64 root_used;
        u64 search_start = 0;
        u64 new_hint;
        struct btrfs_fs_info *info = root->fs_info;
@@ -1754,20 +1790,31 @@ struct extent_buffer *__btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
                                  0, 0, 0);
                return ERR_PTR(-ENOMEM);
        }
+       btrfs_set_header_generation(buf, trans->transid);
+       clean_tree_block(trans, root, buf);
+       wait_on_tree_block_writeback(root, buf);
        btrfs_set_buffer_uptodate(buf);
+
+       if (PageDirty(buf->first_page)) {
+               printk("page %lu dirty\n", buf->first_page->index);
+               WARN_ON(1);
+       }
+
        set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
                         buf->start + buf->len - 1, GFP_NOFS);
-       set_extent_bits(&BTRFS_I(root->fs_info->btree_inode)->extent_tree,
+       set_extent_bits(&BTRFS_I(root->fs_info->btree_inode)->io_tree,
                        buf->start, buf->start + buf->len - 1,
                        EXTENT_CSUM, GFP_NOFS);
        buf->flags |= EXTENT_CSUM;
-       btrfs_set_buffer_defrag(buf);
+       if (!btrfs_test_opt(root, SSD))
+               btrfs_set_buffer_defrag(buf);
        trans->blocks_used++;
        return buf;
 }
 
-static int drop_leaf_ref(struct btrfs_trans_handle *trans,
-                        struct btrfs_root *root, struct extent_buffer *leaf)
+static int noinline drop_leaf_ref(struct btrfs_trans_handle *trans,
+                                 struct btrfs_root *root,
+                                 struct extent_buffer *leaf)
 {
        u64 leaf_owner;
        u64 leaf_generation;
@@ -1808,28 +1855,45 @@ static int drop_leaf_ref(struct btrfs_trans_handle *trans,
        return 0;
 }
 
-static void reada_walk_down(struct btrfs_root *root,
-                           struct extent_buffer *node)
+static void noinline reada_walk_down(struct btrfs_root *root,
+                                    struct extent_buffer *node,
+                                    int slot)
 {
-       int i;
-       u32 nritems;
        u64 bytenr;
-       int ret;
+       u64 last = 0;
+       u32 nritems;
        u32 refs;
-       int level;
        u32 blocksize;
+       int ret;
+       int i;
+       int level;
+       int skipped = 0;
 
        nritems = btrfs_header_nritems(node);
        level = btrfs_header_level(node);
-       for (i = 0; i < nritems; i++) {
+       if (level)
+               return;
+
+       for (i = slot; i < nritems && skipped < 32; i++) {
                bytenr = btrfs_node_blockptr(node, i);
-               blocksize = btrfs_level_size(root, level - 1);
-               ret = lookup_extent_ref(NULL, root, bytenr, blocksize, &refs);
-               BUG_ON(ret);
-               if (refs != 1)
+               if (last && ((bytenr > last && bytenr - last > 32 * 1024) ||
+                            (last > bytenr && last - bytenr > 32 * 1024))) {
+                       skipped++;
                        continue;
+               }
+               blocksize = btrfs_level_size(root, level - 1);
+               if (i != slot) {
+                       ret = lookup_extent_ref(NULL, root, bytenr,
+                                               blocksize, &refs);
+                       BUG_ON(ret);
+                       if (refs != 1) {
+                               skipped++;
+                               continue;
+                       }
+               }
                mutex_unlock(&root->fs_info->fs_mutex);
                ret = readahead_tree_block(root, bytenr, blocksize);
+               last = bytenr + blocksize;
                cond_resched();
                mutex_lock(&root->fs_info->fs_mutex);
                if (ret)
@@ -1841,8 +1905,9 @@ static void reada_walk_down(struct btrfs_root *root,
  * helper function for drop_snapshot, this walks down the tree dropping ref
  * counts as it goes.
  */
-static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
-                         *root, struct btrfs_path *path, int *level)
+static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
+                                  struct btrfs_root *root,
+                                  struct btrfs_path *path, int *level)
 {
        u64 root_owner;
        u64 root_gen;
@@ -1871,9 +1936,6 @@ static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
                WARN_ON(*level >= BTRFS_MAX_LEVEL);
                cur = path->nodes[*level];
 
-               if (*level > 0 && path->slots[*level] == 0)
-                       reada_walk_down(root, cur);
-
                if (btrfs_header_level(cur) != *level)
                        WARN_ON(1);
 
@@ -1903,6 +1965,7 @@ static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
                next = btrfs_find_tree_block(root, bytenr, blocksize);
                if (!next || !btrfs_buffer_uptodate(next)) {
                        free_extent_buffer(next);
+                       reada_walk_down(root, cur, path->slots[*level]);
                        mutex_unlock(&root->fs_info->fs_mutex);
                        next = read_tree_block(root, bytenr, blocksize);
                        mutex_lock(&root->fs_info->fs_mutex);
@@ -1961,8 +2024,9 @@ out:
  * to find the first node higher up where we haven't yet gone through
  * all the slots
  */
-static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
-                       *root, struct btrfs_path *path, int *level)
+static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
+                                struct btrfs_root *root,
+                                struct btrfs_path *path, int *level)
 {
        u64 root_owner;
        u64 root_gen;
@@ -2106,29 +2170,31 @@ int btrfs_free_block_groups(struct btrfs_fs_info *info)
        return 0;
 }
 
-static int relocate_inode_pages(struct inode *inode, u64 start, u64 len)
+static int noinline relocate_inode_pages(struct inode *inode, u64 start,
+                                        u64 len)
 {
        u64 page_start;
        u64 page_end;
        u64 delalloc_start;
        u64 existing_delalloc;
        unsigned long last_index;
-       unsigned long first_index;
        unsigned long i;
        struct page *page;
        struct btrfs_root *root = BTRFS_I(inode)->root;
-       struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
-       struct file_ra_state ra;
+       struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
+       struct file_ra_state *ra;
+
+       ra = kzalloc(sizeof(*ra), GFP_NOFS);
 
        mutex_lock(&inode->i_mutex);
-       first_index = start >> PAGE_CACHE_SHIFT;
+       i = start >> PAGE_CACHE_SHIFT;
        last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
 
-       memset(&ra, 0, sizeof(ra));
-       file_ra_state_init(&ra, inode->i_mapping);
-       btrfs_force_ra(inode->i_mapping, &ra, NULL, first_index, last_index);
+       file_ra_state_init(ra, inode->i_mapping);
+       btrfs_force_ra(inode->i_mapping, ra, NULL, i, last_index);
+       kfree(ra);
 
-       for (i = first_index; i <= last_index; i++) {
+       for (; i <= last_index; i++) {
                page = grab_cache_page(inode->i_mapping, i);
                if (!page)
                        goto out_unlock;
@@ -2144,15 +2210,14 @@ static int relocate_inode_pages(struct inode *inode, u64 start, u64 len)
                page_start = (u64)page->index << PAGE_CACHE_SHIFT;
                page_end = page_start + PAGE_CACHE_SIZE - 1;
 
-               lock_extent(em_tree, page_start, page_end, GFP_NOFS);
+               lock_extent(io_tree, page_start, page_end, GFP_NOFS);
 
                delalloc_start = page_start;
-               existing_delalloc =
-                       count_range_bits(&BTRFS_I(inode)->extent_tree,
-                                        &delalloc_start, page_end,
-                                        PAGE_CACHE_SIZE, EXTENT_DELALLOC);
+               existing_delalloc = count_range_bits(io_tree,
+                                            &delalloc_start, page_end,
+                                            PAGE_CACHE_SIZE, EXTENT_DELALLOC);
 
-               set_extent_delalloc(em_tree, page_start,
+               set_extent_delalloc(io_tree, page_start,
                                    page_end, GFP_NOFS);
 
                spin_lock(&root->fs_info->delalloc_lock);
@@ -2160,7 +2225,7 @@ static int relocate_inode_pages(struct inode *inode, u64 start, u64 len)
                                                 existing_delalloc;
                spin_unlock(&root->fs_info->delalloc_lock);
 
-               unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
+               unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
                set_page_dirty(page);
                unlock_page(page);
                page_cache_release(page);
@@ -2171,27 +2236,43 @@ out_unlock:
        return 0;
 }
 
-static int relocate_one_reference(struct btrfs_root *extent_root,
+/*
+ * note, this releases the path
+ */
+static int noinline relocate_one_reference(struct btrfs_root *extent_root,
                                  struct btrfs_path *path,
-                                 struct btrfs_key *extent_key,
-                                 u64 ref_root, u64 ref_gen, u64 ref_objectid,
-                                 u64 ref_offset)
+                                 struct btrfs_key *extent_key)
 {
        struct inode *inode;
        struct btrfs_root *found_root;
-       struct btrfs_key root_location;
+       struct btrfs_key *root_location;
+       struct btrfs_extent_ref *ref;
+       u64 ref_root;
+       u64 ref_gen;
+       u64 ref_objectid;
+       u64 ref_offset;
        int ret;
 
-       root_location.objectid = ref_root;
+       ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
+                            struct btrfs_extent_ref);
+       ref_root = btrfs_ref_root(path->nodes[0], ref);
+       ref_gen = btrfs_ref_generation(path->nodes[0], ref);
+       ref_objectid = btrfs_ref_objectid(path->nodes[0], ref);
+       ref_offset = btrfs_ref_offset(path->nodes[0], ref);
+       btrfs_release_path(extent_root, path);
+
+       root_location = kmalloc(sizeof(*root_location), GFP_NOFS);
+       root_location->objectid = ref_root;
        if (ref_gen == 0)
-               root_location.offset = 0;
+               root_location->offset = 0;
        else
-               root_location.offset = (u64)-1;
-       root_location.type = BTRFS_ROOT_ITEM_KEY;
+               root_location->offset = (u64)-1;
+       root_location->type = BTRFS_ROOT_ITEM_KEY;
 
        found_root = btrfs_read_fs_root_no_name(extent_root->fs_info,
-                                               &root_location);
+                                               root_location);
        BUG_ON(!found_root);
+       kfree(root_location);
 
        if (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
                mutex_unlock(&extent_root->fs_info->fs_mutex);
@@ -2257,18 +2338,13 @@ out:
        return 0;
 }
 
-static int relocate_one_extent(struct btrfs_root *extent_root,
-                              struct btrfs_path *path,
-                              struct btrfs_key *extent_key)
+static int noinline relocate_one_extent(struct btrfs_root *extent_root,
+                                       struct btrfs_path *path,
+                                       struct btrfs_key *extent_key)
 {
        struct btrfs_key key;
        struct btrfs_key found_key;
-       struct btrfs_extent_ref *ref;
        struct extent_buffer *leaf;
-       u64 ref_root;
-       u64 ref_gen;
-       u64 ref_objectid;
-       u64 ref_offset;
        u32 nritems;
        u32 item_size;
        int ret = 0;
@@ -2280,8 +2356,6 @@ static int relocate_one_extent(struct btrfs_root *extent_root,
        while(1) {
                ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
 
-               BUG_ON(ret == 0);
-
                if (ret < 0)
                        goto out;
 
@@ -2301,17 +2375,7 @@ static int relocate_one_extent(struct btrfs_root *extent_root,
                key.offset = found_key.offset + 1;
                item_size = btrfs_item_size_nr(leaf, path->slots[0]);
 
-               ref = btrfs_item_ptr(leaf, path->slots[0],
-                                    struct btrfs_extent_ref);
-               ref_root = btrfs_ref_root(leaf, ref);
-               ref_gen = btrfs_ref_generation(leaf, ref);
-               ref_objectid = btrfs_ref_objectid(leaf, ref);
-               ref_offset = btrfs_ref_offset(leaf, ref);
-               btrfs_release_path(extent_root, path);
-
-               ret = relocate_one_reference(extent_root, path,
-                                            extent_key, ref_root, ref_gen,
-                                            ref_objectid, ref_offset);
+               ret = relocate_one_reference(extent_root, path, extent_key);
                if (ret)
                        goto out;
        }
@@ -2321,36 +2385,6 @@ out:
        return ret;
 }
 
-static int find_overlapping_extent(struct btrfs_root *root,
-                                  struct btrfs_path *path, u64 new_size)
-{
-       struct btrfs_key found_key;
-       struct extent_buffer *leaf;
-       int ret;
-
-       while(1) {
-               if (path->slots[0] == 0) {
-                       ret = btrfs_prev_leaf(root, path);
-                       if (ret == 1) {
-                               return 1;
-                       }
-                       if (ret < 0)
-                               return ret;
-               } else {
-                       path->slots[0]--;
-               }
-               leaf = path->nodes[0];
-               btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
-               if (found_key.type == BTRFS_EXTENT_ITEM_KEY) {
-                       if (found_key.objectid + found_key.offset > new_size)
-                               return 0;
-                       else
-                               return 1;
-               }
-       }
-       return 1;
-}
-
 int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 new_size)
 {
        struct btrfs_trans_handle *trans;
@@ -2358,17 +2392,18 @@ int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 new_size)
        struct btrfs_path *path;
        u64 cur_byte;
        u64 total_found;
-       u64 ptr;
        struct btrfs_fs_info *info = root->fs_info;
-       struct extent_map_tree *block_group_cache;
+       struct extent_io_tree *block_group_cache;
        struct btrfs_key key;
-       struct btrfs_key found_key = { 0, 0, 0 };
+       struct btrfs_key found_key;
        struct extent_buffer *leaf;
        u32 nritems;
        int ret;
-       int slot;
+       int progress = 0;
 
        btrfs_set_super_total_bytes(&info->super_copy, new_size);
+       clear_extent_dirty(&info->free_space_cache, new_size, (u64)-1,
+                          GFP_NOFS);
        block_group_cache = &info->block_group_cache;
        path = btrfs_alloc_path();
        root = root->fs_info->extent_root;
@@ -2377,47 +2412,67 @@ int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 new_size)
 again:
        total_found = 0;
        key.objectid = new_size;
-       cur_byte = key.objectid;
        key.offset = 0;
        key.type = 0;
+       cur_byte = key.objectid;
+
+       ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
+       if (ret < 0)
+               goto out;
+
+       ret = find_previous_extent(root, path);
+       if (ret < 0)
+               goto out;
+       if (ret == 0) {
+               leaf = path->nodes[0];
+               btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
+               if (found_key.objectid + found_key.offset > new_size) {
+                       cur_byte = found_key.objectid;
+                       key.objectid = cur_byte;
+               }
+       }
+       btrfs_release_path(root, path);
+
        while(1) {
                ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
                if (ret < 0)
                        goto out;
-next:
+
                leaf = path->nodes[0];
-               if (key.objectid == new_size - 1) {
-                       ret = find_overlapping_extent(root, path, new_size);
-                       if (ret != 0) {
-                               btrfs_release_path(root, path);
-                               ret = btrfs_search_slot(NULL, root, &key,
-                                                       path, 0, 0);
-                               if (ret < 0)
-                                       goto out;
+               nritems = btrfs_header_nritems(leaf);
+next:
+               if (path->slots[0] >= nritems) {
+                       ret = btrfs_next_leaf(root, path);
+                       if (ret < 0)
+                               goto out;
+                       if (ret == 1) {
+                               ret = 0;
+                               break;
                        }
+                       leaf = path->nodes[0];
+                       nritems = btrfs_header_nritems(leaf);
                }
-               nritems = btrfs_header_nritems(leaf);
-               ret = 0;
-               slot = path->slots[0];
-               if (slot < nritems)
-                       btrfs_item_key_to_cpu(leaf, &found_key, slot);
-               if (slot == nritems ||
-                   btrfs_key_type(&found_key) != BTRFS_EXTENT_ITEM_KEY) {
+
+               btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
+
+               if (progress && need_resched()) {
+                       memcpy(&key, &found_key, sizeof(key));
+                       mutex_unlock(&root->fs_info->fs_mutex);
+                       cond_resched();
+                       mutex_lock(&root->fs_info->fs_mutex);
+                       btrfs_release_path(root, path);
+                       btrfs_search_slot(NULL, root, &key, path, 0, 0);
+                       progress = 0;
+                       goto next;
+               }
+               progress = 1;
+
+               if (btrfs_key_type(&found_key) != BTRFS_EXTENT_ITEM_KEY ||
+                   found_key.objectid + found_key.offset <= cur_byte) {
                        path->slots[0]++;
-                       if (path->slots[0] >= nritems) {
-                               ret = btrfs_next_leaf(root, path);
-                               if (ret < 0)
-                                       goto out;
-                               if (ret == 1) {
-                                       ret = 0;
-                                       break;
-                               }
-                       }
                        goto next;
                }
-               btrfs_item_key_to_cpu(leaf, &found_key, slot);
-               if (found_key.objectid + found_key.offset <= cur_byte)
-                       continue;
+
                total_found++;
                cur_byte = found_key.objectid + found_key.offset;
                key.objectid = cur_byte;
@@ -2445,36 +2500,43 @@ next:
        key.offset = 0;
        key.type = 0;
        while(1) {
+               u64 ptr;
+
                ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
                if (ret < 0)
                        goto out;
-bg_next:
+
                leaf = path->nodes[0];
                nritems = btrfs_header_nritems(leaf);
-               ret = 0;
-               slot = path->slots[0];
-               if (slot < nritems)
-                       btrfs_item_key_to_cpu(leaf, &found_key, slot);
-               if (slot == nritems ||
-                   btrfs_key_type(&found_key) != BTRFS_BLOCK_GROUP_ITEM_KEY) {
-                       if (slot < nritems) {
-                               printk("shrinker found key %Lu %u %Lu\n",
-                                      found_key.objectid, found_key.type,
-                                      found_key.offset);
-                               path->slots[0]++;
-                       }
-                       if (path->slots[0] >= nritems) {
-                               ret = btrfs_next_leaf(root, path);
-                               if (ret < 0)
-                                       break;
-                               if (ret == 1) {
-                                       ret = 0;
-                                       break;
-                               }
+bg_next:
+               if (path->slots[0] >= nritems) {
+                       ret = btrfs_next_leaf(root, path);
+                       if (ret < 0)
+                               break;
+                       if (ret == 1) {
+                               ret = 0;
+                               break;
                        }
+                       leaf = path->nodes[0];
+                       btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
+
+                       /*
+                        * btrfs_next_leaf doesn't cow buffers, we have to
+                        * do the search again
+                        */
+                       memcpy(&key, &found_key, sizeof(key));
+                       btrfs_release_path(root, path);
+                       goto resched_check;
+               }
+
+               btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
+               if (btrfs_key_type(&found_key) != BTRFS_BLOCK_GROUP_ITEM_KEY) {
+                       printk("shrinker found key %Lu %u %Lu\n",
+                               found_key.objectid, found_key.type,
+                               found_key.offset);
+                       path->slots[0]++;
                        goto bg_next;
                }
-               btrfs_item_key_to_cpu(leaf, &found_key, slot);
                ret = get_state_private(&info->block_group_cache,
                                        found_key.objectid, &ptr);
                if (!ret)
@@ -2487,6 +2549,12 @@ bg_next:
                key.objectid = found_key.objectid + 1;
                btrfs_del_item(trans, root, path);
                btrfs_release_path(root, path);
+resched_check:
+               if (need_resched()) {
+                       mutex_unlock(&root->fs_info->fs_mutex);
+                       cond_resched();
+                       mutex_lock(&root->fs_info->fs_mutex);
+               }
        }
        clear_extent_dirty(&info->free_space_cache, new_size, (u64)-1,
                           GFP_NOFS);
@@ -2503,10 +2571,11 @@ int btrfs_grow_extent_tree(struct btrfs_trans_handle *trans,
        u64 nr = 0;
        u64 cur_byte;
        u64 old_size;
+       unsigned long rem;
        struct btrfs_block_group_cache *cache;
        struct btrfs_block_group_item *item;
        struct btrfs_fs_info *info = root->fs_info;
-       struct extent_map_tree *block_group_cache;
+       struct extent_io_tree *block_group_cache;
        struct btrfs_key key;
        struct extent_buffer *leaf;
        int ret;
@@ -2540,7 +2609,8 @@ int btrfs_grow_extent_tree(struct btrfs_trans_handle *trans,
                                      struct btrfs_block_group_item);
 
                btrfs_set_disk_block_group_used(leaf, item, 0);
-               if (nr % 3) {
+               div_long_long_rem(nr, 3, &rem);
+               if (rem) {
                        btrfs_set_disk_block_group_flags(leaf, item,
                                                 BTRFS_BLOCK_GROUP_DATA);
                } else {
@@ -2589,7 +2659,7 @@ int btrfs_read_block_groups(struct btrfs_root *root)
        int bit;
        struct btrfs_block_group_cache *cache;
        struct btrfs_fs_info *info = root->fs_info;
-       struct extent_map_tree *block_group_cache;
+       struct extent_io_tree *block_group_cache;
        struct btrfs_key key;
        struct btrfs_key found_key;
        struct extent_buffer *leaf;