Btrfs: Seed device support
[safe/jmp/linux-2.6] / fs / btrfs / extent-tree.c
index 1eb69a9..197422c 100644 (file)
@@ -42,6 +42,8 @@ struct pending_extent_op {
        u64 generation;
        u64 orig_generation;
        int level;
+       struct list_head list;
+       int del;
 };
 
 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
@@ -52,6 +54,13 @@ static struct btrfs_block_group_cache *
 __btrfs_find_block_group(struct btrfs_root *root,
                         struct btrfs_block_group_cache *hint,
                         u64 search_start, int data, int owner);
+static int pin_down_bytes(struct btrfs_trans_handle *trans,
+                         struct btrfs_root *root,
+                         u64 bytenr, u64 num_bytes, int is_data);
+static int update_block_group(struct btrfs_trans_handle *trans,
+                             struct btrfs_root *root,
+                             u64 bytenr, u64 num_bytes, int alloc,
+                             int mark_free);
 
 static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
 {
@@ -346,7 +355,7 @@ __btrfs_find_block_group(struct btrfs_root *root,
        if (search_start) {
                struct btrfs_block_group_cache *shint;
                shint = btrfs_lookup_first_block_group(info, search_start);
-               if (shint && block_group_bits(shint, data) && !shint->ro) {
+               if (shint && block_group_bits(shint, data)) {
                        spin_lock(&shint->lock);
                        used = btrfs_block_group_used(&shint->item);
                        if (used + shint->pinned + shint->reserved <
@@ -357,7 +366,7 @@ __btrfs_find_block_group(struct btrfs_root *root,
                        spin_unlock(&shint->lock);
                }
        }
-       if (hint && !hint->ro && block_group_bits(hint, data)) {
+       if (hint && block_group_bits(hint, data)) {
                spin_lock(&hint->lock);
                used = btrfs_block_group_used(&hint->item);
                if (used + hint->pinned + hint->reserved <
@@ -383,7 +392,7 @@ again:
                last = cache->key.objectid + cache->key.offset;
                used = btrfs_block_group_used(&cache->item);
 
-               if (!cache->ro && block_group_bits(cache, data)) {
+               if (block_group_bits(cache, data)) {
                        free_check = div_factor(cache->key.offset, factor);
                        if (used + cache->pinned + cache->reserved <
                            free_check) {
@@ -559,6 +568,251 @@ out:
        return ret;
 }
 
+/*
+ * updates all the backrefs that are pending on update_list for the
+ * extent_root
+ */
+static int noinline update_backrefs(struct btrfs_trans_handle *trans,
+                                   struct btrfs_root *extent_root,
+                                   struct btrfs_path *path,
+                                   struct list_head *update_list)
+{
+       struct btrfs_key key;
+       struct btrfs_extent_ref *ref;
+       struct btrfs_fs_info *info = extent_root->fs_info;
+       struct pending_extent_op *op;
+       struct extent_buffer *leaf;
+       int ret = 0;
+       struct list_head *cur = update_list->next;
+       u64 ref_objectid;
+       u64 ref_root = extent_root->root_key.objectid;
+
+       op = list_entry(cur, struct pending_extent_op, list);
+
+search:
+       key.objectid = op->bytenr;
+       key.type = BTRFS_EXTENT_REF_KEY;
+       key.offset = op->orig_parent;
+
+       ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 1);
+       BUG_ON(ret);
+
+       leaf = path->nodes[0];
+
+loop:
+       ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
+
+       ref_objectid = btrfs_ref_objectid(leaf, ref);
+
+       if (btrfs_ref_root(leaf, ref) != ref_root ||
+           btrfs_ref_generation(leaf, ref) != op->orig_generation ||
+           (ref_objectid != op->level &&
+            ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
+               printk(KERN_ERR "couldn't find %Lu, parent %Lu, root %Lu, "
+                      "owner %u\n", op->bytenr, op->orig_parent,
+                      ref_root, op->level);
+               btrfs_print_leaf(extent_root, leaf);
+               BUG();
+       }
+
+       key.objectid = op->bytenr;
+       key.offset = op->parent;
+       key.type = BTRFS_EXTENT_REF_KEY;
+       ret = btrfs_set_item_key_safe(trans, extent_root, path, &key);
+       BUG_ON(ret);
+       ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
+       btrfs_set_ref_generation(leaf, ref, op->generation);
+
+       cur = cur->next;
+
+       list_del_init(&op->list);
+       unlock_extent(&info->extent_ins, op->bytenr,
+                     op->bytenr + op->num_bytes - 1, GFP_NOFS);
+       kfree(op);
+
+       if (cur == update_list) {
+               btrfs_mark_buffer_dirty(path->nodes[0]);
+               btrfs_release_path(extent_root, path);
+               goto out;
+       }
+
+       op = list_entry(cur, struct pending_extent_op, list);
+
+       path->slots[0]++;
+       while (path->slots[0] < btrfs_header_nritems(leaf)) {
+               btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
+               if (key.objectid == op->bytenr &&
+                   key.type == BTRFS_EXTENT_REF_KEY)
+                       goto loop;
+               path->slots[0]++;
+       }
+
+       btrfs_mark_buffer_dirty(path->nodes[0]);
+       btrfs_release_path(extent_root, path);
+       goto search;
+
+out:
+       return 0;
+}
+
+static int noinline insert_extents(struct btrfs_trans_handle *trans,
+                                  struct btrfs_root *extent_root,
+                                  struct btrfs_path *path,
+                                  struct list_head *insert_list, int nr)
+{
+       struct btrfs_key *keys;
+       u32 *data_size;
+       struct pending_extent_op *op;
+       struct extent_buffer *leaf;
+       struct list_head *cur = insert_list->next;
+       struct btrfs_fs_info *info = extent_root->fs_info;
+       u64 ref_root = extent_root->root_key.objectid;
+       int i = 0, last = 0, ret;
+       int total = nr * 2;
+
+       if (!nr)
+               return 0;
+
+       keys = kzalloc(total * sizeof(struct btrfs_key), GFP_NOFS);
+       if (!keys)
+               return -ENOMEM;
+
+       data_size = kzalloc(total * sizeof(u32), GFP_NOFS);
+       if (!data_size) {
+               kfree(keys);
+               return -ENOMEM;
+       }
+
+       list_for_each_entry(op, insert_list, list) {
+               keys[i].objectid = op->bytenr;
+               keys[i].offset = op->num_bytes;
+               keys[i].type = BTRFS_EXTENT_ITEM_KEY;
+               data_size[i] = sizeof(struct btrfs_extent_item);
+               i++;
+
+               keys[i].objectid = op->bytenr;
+               keys[i].offset = op->parent;
+               keys[i].type = BTRFS_EXTENT_REF_KEY;
+               data_size[i] = sizeof(struct btrfs_extent_ref);
+               i++;
+       }
+
+       op = list_entry(cur, struct pending_extent_op, list);
+       i = 0;
+       while (i < total) {
+               int c;
+               ret = btrfs_insert_some_items(trans, extent_root, path,
+                                             keys+i, data_size+i, total-i);
+               BUG_ON(ret < 0);
+
+               if (last && ret > 1)
+                       BUG();
+
+               leaf = path->nodes[0];
+               for (c = 0; c < ret; c++) {
+                       int ref_first = keys[i].type == BTRFS_EXTENT_REF_KEY;
+
+                       /*
+                        * if the first item we inserted was a backref, then
+                        * the EXTENT_ITEM will be the odd c's, else it will
+                        * be the even c's
+                        */
+                       if ((ref_first && (c % 2)) ||
+                           (!ref_first && !(c % 2))) {
+                               struct btrfs_extent_item *itm;
+
+                               itm = btrfs_item_ptr(leaf, path->slots[0] + c,
+                                                    struct btrfs_extent_item);
+                               btrfs_set_extent_refs(path->nodes[0], itm, 1);
+                               op->del++;
+                       } else {
+                               struct btrfs_extent_ref *ref;
+
+                               ref = btrfs_item_ptr(leaf, path->slots[0] + c,
+                                                    struct btrfs_extent_ref);
+                               btrfs_set_ref_root(leaf, ref, ref_root);
+                               btrfs_set_ref_generation(leaf, ref,
+                                                        op->generation);
+                               btrfs_set_ref_objectid(leaf, ref, op->level);
+                               btrfs_set_ref_num_refs(leaf, ref, 1);
+                               op->del++;
+                       }
+
+                       /*
+                        * using del to see when its ok to free up the
+                        * pending_extent_op.  In the case where we insert the
+                        * last item on the list in order to help do batching
+                        * we need to not free the extent op until we actually
+                        * insert the extent_item
+                        */
+                       if (op->del == 2) {
+                               unlock_extent(&info->extent_ins, op->bytenr,
+                                             op->bytenr + op->num_bytes - 1,
+                                             GFP_NOFS);
+                               cur = cur->next;
+                               list_del_init(&op->list);
+                               kfree(op);
+                               if (cur != insert_list)
+                                       op = list_entry(cur,
+                                               struct pending_extent_op,
+                                               list);
+                       }
+               }
+               btrfs_mark_buffer_dirty(leaf);
+               btrfs_release_path(extent_root, path);
+
+               /*
+                * Ok backref's and items usually go right next to eachother,
+                * but if we could only insert 1 item that means that we
+                * inserted on the end of a leaf, and we have no idea what may
+                * be on the next leaf so we just play it safe.  In order to
+                * try and help this case we insert the last thing on our
+                * insert list so hopefully it will end up being the last
+                * thing on the leaf and everything else will be before it,
+                * which will let us insert a whole bunch of items at the same
+                * time.
+                */
+               if (ret == 1 && !last && (i + ret < total)) {
+                       /*
+                        * last: where we will pick up the next time around
+                        * i: our current key to insert, will be total - 1
+                        * cur: the current op we are screwing with
+                        * op: duh
+                        */
+                       last = i + ret;
+                       i = total - 1;
+                       cur = insert_list->prev;
+                       op = list_entry(cur, struct pending_extent_op, list);
+               } else if (last) {
+                       /*
+                        * ok we successfully inserted the last item on the
+                        * list, lets reset everything
+                        *
+                        * i: our current key to insert, so where we left off
+                        *    last time
+                        * last: done with this
+                        * cur: the op we are messing with
+                        * op: duh
+                        * total: since we inserted the last key, we need to
+                        *        decrement total so we dont overflow
+                        */
+                       i = last;
+                       last = 0;
+                       cur = insert_list->next;
+                       op = list_entry(cur, struct pending_extent_op, list);
+                       total--;
+               } else {
+                       i += ret;
+               }
+
+               cond_resched();
+       }
+       ret = 0;
+       kfree(keys);
+       kfree(data_size);
+       return ret;
+}
+
 static int noinline insert_extent_backref(struct btrfs_trans_handle *trans,
                                          struct btrfs_root *root,
                                          struct btrfs_path *path,
@@ -642,6 +896,267 @@ static int noinline remove_extent_backref(struct btrfs_trans_handle *trans,
        return ret;
 }
 
+static int noinline free_extents(struct btrfs_trans_handle *trans,
+                                struct btrfs_root *extent_root,
+                                struct list_head *del_list)
+{
+       struct btrfs_fs_info *info = extent_root->fs_info;
+       struct btrfs_path *path;
+       struct btrfs_key key, found_key;
+       struct extent_buffer *leaf;
+       struct list_head *cur;
+       struct pending_extent_op *op;
+       struct btrfs_extent_item *ei;
+       int ret, num_to_del, extent_slot = 0, found_extent = 0;
+       u32 refs;
+       u64 bytes_freed = 0;
+
+       path = btrfs_alloc_path();
+       if (!path)
+               return -ENOMEM;
+       path->reada = 1;
+
+search:
+       /* search for the backref for the current ref we want to delete */
+       cur = del_list->next;
+       op = list_entry(cur, struct pending_extent_op, list);
+       ret = lookup_extent_backref(trans, extent_root, path, op->bytenr,
+                                   op->orig_parent,
+                                   extent_root->root_key.objectid,
+                                   op->orig_generation, op->level, 1);
+       if (ret) {
+               printk("Unable to find backref byte nr %Lu root %Lu gen %Lu "
+                      "owner %u\n", op->bytenr,
+                      extent_root->root_key.objectid, op->orig_generation,
+                      op->level);
+               btrfs_print_leaf(extent_root, path->nodes[0]);
+               WARN_ON(1);
+               goto out;
+       }
+
+       extent_slot = path->slots[0];
+       num_to_del = 1;
+       found_extent = 0;
+
+       /*
+        * if we aren't the first item on the leaf we can move back one and see
+        * if our ref is right next to our extent item
+        */
+       if (likely(extent_slot)) {
+               extent_slot--;
+               btrfs_item_key_to_cpu(path->nodes[0], &found_key,
+                                     extent_slot);
+               if (found_key.objectid == op->bytenr &&
+                   found_key.type == BTRFS_EXTENT_ITEM_KEY &&
+                   found_key.offset == op->num_bytes) {
+                       num_to_del++;
+                       found_extent = 1;
+               }
+       }
+
+       /*
+        * if we didn't find the extent we need to delete the backref and then
+        * search for the extent item key so we can update its ref count
+        */
+       if (!found_extent) {
+               key.objectid = op->bytenr;
+               key.type = BTRFS_EXTENT_ITEM_KEY;
+               key.offset = op->num_bytes;
+
+               ret = remove_extent_backref(trans, extent_root, path);
+               BUG_ON(ret);
+               btrfs_release_path(extent_root, path);
+               ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
+               BUG_ON(ret);
+               extent_slot = path->slots[0];
+       }
+
+       /* this is where we update the ref count for the extent */
+       leaf = path->nodes[0];
+       ei = btrfs_item_ptr(leaf, extent_slot, struct btrfs_extent_item);
+       refs = btrfs_extent_refs(leaf, ei);
+       BUG_ON(refs == 0);
+       refs--;
+       btrfs_set_extent_refs(leaf, ei, refs);
+
+       btrfs_mark_buffer_dirty(leaf);
+
+       /*
+        * This extent needs deleting.  The reason cur_slot is extent_slot +
+        * num_to_del is because extent_slot points to the slot where the extent
+        * is, and if the backref was not right next to the extent we will be
+        * deleting at least 1 item, and will want to start searching at the
+        * slot directly next to extent_slot.  However if we did find the
+        * backref next to the extent item them we will be deleting at least 2
+        * items and will want to start searching directly after the ref slot
+        */
+       if (!refs) {
+               struct list_head *pos, *n, *end;
+               int cur_slot = extent_slot+num_to_del;
+               u64 super_used;
+               u64 root_used;
+
+               path->slots[0] = extent_slot;
+               bytes_freed = op->num_bytes;
+
+               /*
+                * we need to see if we can delete multiple things at once, so
+                * start looping through the list of extents we are wanting to
+                * delete and see if their extent/backref's are right next to
+                * eachother and the extents only have 1 ref
+                */
+               for (pos = cur->next; pos != del_list; pos = pos->next) {
+                       struct pending_extent_op *tmp;
+
+                       tmp = list_entry(pos, struct pending_extent_op, list);
+
+                       /* we only want to delete extent+ref at this stage */
+                       if (cur_slot >= btrfs_header_nritems(leaf) - 1)
+                               break;
+
+                       btrfs_item_key_to_cpu(leaf, &found_key, cur_slot);
+                       if (found_key.objectid != tmp->bytenr ||
+                           found_key.type != BTRFS_EXTENT_ITEM_KEY ||
+                           found_key.offset != tmp->num_bytes)
+                               break;
+
+                       /* check to make sure this extent only has one ref */
+                       ei = btrfs_item_ptr(leaf, cur_slot,
+                                           struct btrfs_extent_item);
+                       if (btrfs_extent_refs(leaf, ei) != 1)
+                               break;
+
+                       btrfs_item_key_to_cpu(leaf, &found_key, cur_slot+1);
+                       if (found_key.objectid != tmp->bytenr ||
+                           found_key.type != BTRFS_EXTENT_REF_KEY ||
+                           found_key.offset != tmp->orig_parent)
+                               break;
+
+                       /*
+                        * the ref is right next to the extent, we can set the
+                        * ref count to 0 since we will delete them both now
+                        */
+                       btrfs_set_extent_refs(leaf, ei, 0);
+
+                       /* pin down the bytes for this extent */
+                       mutex_lock(&info->pinned_mutex);
+                       ret = pin_down_bytes(trans, extent_root, tmp->bytenr,
+                                            tmp->num_bytes, tmp->level >=
+                                            BTRFS_FIRST_FREE_OBJECTID);
+                       mutex_unlock(&info->pinned_mutex);
+                       BUG_ON(ret < 0);
+
+                       /*
+                        * use the del field to tell if we need to go ahead and
+                        * free up the extent when we delete the item or not.
+                        */
+                       tmp->del = ret;
+                       bytes_freed += tmp->num_bytes;
+
+                       num_to_del += 2;
+                       cur_slot += 2;
+               }
+               end = pos;
+
+               /* update the free space counters */
+               spin_lock_irq(&info->delalloc_lock);
+               super_used = btrfs_super_bytes_used(&info->super_copy);
+               btrfs_set_super_bytes_used(&info->super_copy,
+                                          super_used - bytes_freed);
+               spin_unlock_irq(&info->delalloc_lock);
+
+               root_used = btrfs_root_used(&extent_root->root_item);
+               btrfs_set_root_used(&extent_root->root_item,
+                                   root_used - bytes_freed);
+
+               /* delete the items */
+               ret = btrfs_del_items(trans, extent_root, path,
+                                     path->slots[0], num_to_del);
+               BUG_ON(ret);
+
+               /*
+                * loop through the extents we deleted and do the cleanup work
+                * on them
+                */
+               for (pos = cur, n = pos->next; pos != end;
+                    pos = n, n = pos->next) {
+                       struct pending_extent_op *tmp;
+#ifdef BIO_RW_DISCARD
+                       u64 map_length;
+                       struct btrfs_multi_bio *multi = NULL;
+#endif
+                       tmp = list_entry(pos, struct pending_extent_op, list);
+
+                       /*
+                        * remember tmp->del tells us wether or not we pinned
+                        * down the extent
+                        */
+                       ret = update_block_group(trans, extent_root,
+                                                tmp->bytenr, tmp->num_bytes, 0,
+                                                tmp->del);
+                       BUG_ON(ret);
+
+#ifdef BIO_RW_DISCARD
+                       ret = btrfs_map_block(&info->mapping_tree, READ,
+                                             tmp->bytenr, &map_length, &multi,
+                                             0);
+                       if (!ret) {
+                               struct btrfs_bio_stripe *stripe;
+                               int i;
+
+                               stripe = multi->stripe;
+
+                               if (map_length > tmp->num_bytes)
+                                       map_length = tmp->num_bytes;
+
+                               for (i = 0; i < multi->num_stripes;
+                                    i++, stripe++)
+                                       blkdev_issue_discard(stripe->dev->bdev,
+                                                       stripe->physical >> 9,
+                                                       map_length >> 9);
+                               kfree(multi);
+                       }
+#endif
+                       list_del_init(&tmp->list);
+                       unlock_extent(&info->extent_ins, tmp->bytenr,
+                                     tmp->bytenr + tmp->num_bytes - 1,
+                                     GFP_NOFS);
+                       kfree(tmp);
+               }
+       } else if (refs && found_extent) {
+               /*
+                * the ref and extent were right next to eachother, but the
+                * extent still has a ref, so just free the backref and keep
+                * going
+                */
+               ret = remove_extent_backref(trans, extent_root, path);
+               BUG_ON(ret);
+
+               list_del_init(&op->list);
+               unlock_extent(&info->extent_ins, op->bytenr,
+                             op->bytenr + op->num_bytes - 1, GFP_NOFS);
+               kfree(op);
+       } else {
+               /*
+                * the extent has multiple refs and the backref we were looking
+                * for was not right next to it, so just unlock and go next,
+                * we're good to go
+                */
+               list_del_init(&op->list);
+               unlock_extent(&info->extent_ins, op->bytenr,
+                             op->bytenr + op->num_bytes - 1, GFP_NOFS);
+               kfree(op);
+       }
+
+       btrfs_release_path(extent_root, path);
+       if (!list_empty(del_list))
+               goto search;
+
+out:
+       btrfs_free_path(path);
+       return ret;
+}
+
 static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
                                     struct btrfs_root *root, u64 bytenr,
                                     u64 orig_parent, u64 parent,
@@ -685,6 +1200,8 @@ static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
                        extent_op->generation = ref_generation;
                        extent_op->orig_generation = orig_generation;
                        extent_op->level = (int)owner_objectid;
+                       INIT_LIST_HEAD(&extent_op->list);
+                       extent_op->del = 0;
 
                        set_extent_bits(&root->fs_info->extent_ins,
                                        bytenr, bytenr + num_bytes - 1,
@@ -768,7 +1285,11 @@ static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
        l = path->nodes[0];
 
        btrfs_item_key_to_cpu(l, &key, path->slots[0]);
-       BUG_ON(key.objectid != bytenr);
+       if (key.objectid != bytenr) {
+               btrfs_print_leaf(root->fs_info->extent_root, path->nodes[0]);
+               printk("wanted %Lu found %Lu\n", bytenr, key.objectid);
+               BUG();
+       }
        BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY);
 
        item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
@@ -1273,7 +1794,7 @@ static int update_space_info(struct btrfs_fs_info *info, u64 flags,
                *space_info = found;
                return 0;
        }
-       found = kmalloc(sizeof(*found), GFP_NOFS);
+       found = kzalloc(sizeof(*found), GFP_NOFS);
        if (!found)
                return -ENOMEM;
 
@@ -1286,6 +1807,7 @@ static int update_space_info(struct btrfs_fs_info *info, u64 flags,
        found->bytes_used = bytes_used;
        found->bytes_pinned = 0;
        found->bytes_reserved = 0;
+       found->bytes_readonly = 0;
        found->full = 0;
        found->force_alloc = 0;
        *space_info = found;
@@ -1308,9 +1830,22 @@ static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
        }
 }
 
-static u64 reduce_alloc_profile(struct btrfs_root *root, u64 flags)
+static void set_block_group_readonly(struct btrfs_block_group_cache *cache)
+{
+       spin_lock(&cache->space_info->lock);
+       spin_lock(&cache->lock);
+       if (!cache->ro) {
+               cache->space_info->bytes_readonly += cache->key.offset -
+                                       btrfs_block_group_used(&cache->item);
+               cache->ro = 1;
+       }
+       spin_unlock(&cache->lock);
+       spin_unlock(&cache->space_info->lock);
+}
+
+u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
 {
-       u64 num_devices = root->fs_info->fs_devices->num_devices;
+       u64 num_devices = root->fs_info->fs_devices->rw_devices;
 
        if (num_devices == 1)
                flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
@@ -1342,11 +1877,11 @@ static int do_chunk_alloc(struct btrfs_trans_handle *trans,
 {
        struct btrfs_space_info *space_info;
        u64 thresh;
-       u64 start;
-       u64 num_bytes;
-       int ret = 0, waited = 0;
+       int ret = 0;
 
-       flags = reduce_alloc_profile(extent_root, flags);
+       mutex_lock(&extent_root->fs_info->chunk_mutex);
+
+       flags = btrfs_reduce_alloc_profile(extent_root, flags);
 
        space_info = __find_space_info(extent_root->fs_info, flags);
        if (!space_info) {
@@ -1366,46 +1901,23 @@ static int do_chunk_alloc(struct btrfs_trans_handle *trans,
                goto out;
        }
 
-       thresh = div_factor(space_info->total_bytes, 6);
+       thresh = space_info->total_bytes - space_info->bytes_readonly;
+       thresh = div_factor(thresh, 6);
        if (!force &&
           (space_info->bytes_used + space_info->bytes_pinned +
            space_info->bytes_reserved + alloc_bytes) < thresh) {
                spin_unlock(&space_info->lock);
                goto out;
        }
-
        spin_unlock(&space_info->lock);
 
-       ret = mutex_trylock(&extent_root->fs_info->chunk_mutex);
-       if (!ret && !force) {
-               goto out;
-       } else if (!ret) {
-               mutex_lock(&extent_root->fs_info->chunk_mutex);
-               waited = 1;
-       }
-
-       if (waited) {
-               spin_lock(&space_info->lock);
-               if (space_info->full) {
-                       spin_unlock(&space_info->lock);
-                       goto out_unlock;
-               }
-               spin_unlock(&space_info->lock);
-       }
-
-       ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes, flags);
+       ret = btrfs_alloc_chunk(trans, extent_root, flags);
        if (ret) {
 printk("space info full %Lu\n", flags);
                space_info->full = 1;
-               goto out_unlock;
        }
-
-       ret = btrfs_make_block_group(trans, extent_root, 0, flags,
-                    BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes);
-       BUG_ON(ret);
-out_unlock:
-       mutex_unlock(&extent_root->fs_info->chunk_mutex);
 out:
+       mutex_unlock(&extent_root->fs_info->chunk_mutex);
        return ret;
 }
 
@@ -1422,9 +1934,8 @@ static int update_block_group(struct btrfs_trans_handle *trans,
 
        while(total) {
                cache = btrfs_lookup_block_group(info, bytenr);
-               if (!cache) {
+               if (!cache)
                        return -1;
-               }
                byte_in_group = bytenr - cache->key.objectid;
                WARN_ON(byte_in_group > cache->key.offset);
 
@@ -1436,12 +1947,18 @@ static int update_block_group(struct btrfs_trans_handle *trans,
                if (alloc) {
                        old_val += num_bytes;
                        cache->space_info->bytes_used += num_bytes;
+                       if (cache->ro) {
+                               cache->space_info->bytes_readonly -= num_bytes;
+                               WARN_ON(1);
+                       }
                        btrfs_set_block_group_used(&cache->item, old_val);
                        spin_unlock(&cache->lock);
                        spin_unlock(&cache->space_info->lock);
                } else {
                        old_val -= num_bytes;
                        cache->space_info->bytes_used -= num_bytes;
+                       if (cache->ro)
+                               cache->space_info->bytes_readonly += num_bytes;
                        btrfs_set_block_group_used(&cache->item, old_val);
                        spin_unlock(&cache->lock);
                        spin_unlock(&cache->space_info->lock);
@@ -1601,102 +2118,176 @@ static int finish_current_insert(struct btrfs_trans_handle *trans,
        u64 end;
        u64 priv;
        u64 search = 0;
+       u64 skipped = 0;
        struct btrfs_fs_info *info = extent_root->fs_info;
        struct btrfs_path *path;
-       struct btrfs_extent_ref *ref;
-       struct pending_extent_op *extent_op;
-       struct btrfs_key key;
-       struct btrfs_extent_item extent_item;
+       struct pending_extent_op *extent_op, *tmp;
+       struct list_head insert_list, update_list;
        int ret;
-       int err = 0;
+       int num_inserts = 0, max_inserts;
 
-       btrfs_set_stack_extent_refs(&extent_item, 1);
        path = btrfs_alloc_path();
+       INIT_LIST_HEAD(&insert_list);
+       INIT_LIST_HEAD(&update_list);
 
-       while(1) {
-               mutex_lock(&info->extent_ins_mutex);
+       max_inserts = extent_root->leafsize /
+               (2 * sizeof(struct btrfs_key) + 2 * sizeof(struct btrfs_item) +
+                sizeof(struct btrfs_extent_ref) +
+                sizeof(struct btrfs_extent_item));
+again:
+       mutex_lock(&info->extent_ins_mutex);
+       while (1) {
                ret = find_first_extent_bit(&info->extent_ins, search, &start,
                                            &end, EXTENT_WRITEBACK);
                if (ret) {
-                       mutex_unlock(&info->extent_ins_mutex);
-                       if (search && all) {
-                               search = 0;
+                       if (skipped && all && !num_inserts) {
+                               skipped = 0;
                                continue;
                        }
+                       mutex_unlock(&info->extent_ins_mutex);
                        break;
                }
 
                ret = try_lock_extent(&info->extent_ins, start, end, GFP_NOFS);
                if (!ret) {
+                       skipped = 1;
                        search = end + 1;
-                       mutex_unlock(&info->extent_ins_mutex);
-                       cond_resched();
+                       if (need_resched()) {
+                               mutex_unlock(&info->extent_ins_mutex);
+                               cond_resched();
+                               mutex_lock(&info->extent_ins_mutex);
+                       }
                        continue;
                }
-               BUG_ON(ret < 0);
 
                ret = get_state_private(&info->extent_ins, start, &priv);
                BUG_ON(ret);
-               extent_op = (struct pending_extent_op *)(unsigned long)priv;
-
-               mutex_unlock(&info->extent_ins_mutex);
+               extent_op = (struct pending_extent_op *)(unsigned long) priv;
 
                if (extent_op->type == PENDING_EXTENT_INSERT) {
-                       key.objectid = start;
-                       key.offset = end + 1 - start;
-                       key.type = BTRFS_EXTENT_ITEM_KEY;
-                       err = btrfs_insert_item(trans, extent_root, &key,
-                                       &extent_item, sizeof(extent_item));
-                       BUG_ON(err);
+                       num_inserts++;
+                       list_add_tail(&extent_op->list, &insert_list);
+                       search = end + 1;
+                       if (num_inserts == max_inserts) {
+                               mutex_unlock(&info->extent_ins_mutex);
+                               break;
+                       }
+               } else if (extent_op->type == PENDING_BACKREF_UPDATE) {
+                       list_add_tail(&extent_op->list, &update_list);
+                       search = end + 1;
+               } else {
+                       BUG();
+               }
+       }
 
-                       mutex_lock(&info->extent_ins_mutex);
-                       clear_extent_bits(&info->extent_ins, start, end,
-                                         EXTENT_WRITEBACK, GFP_NOFS);
-                       mutex_unlock(&info->extent_ins_mutex);
+       /*
+        * process teh update list, clear the writeback bit for it, and if
+        * somebody marked this thing for deletion then just unlock it and be
+        * done, the free_extents will handle it
+        */
+       mutex_lock(&info->extent_ins_mutex);
+       list_for_each_entry_safe(extent_op, tmp, &update_list, list) {
+               clear_extent_bits(&info->extent_ins, extent_op->bytenr,
+                                 extent_op->bytenr + extent_op->num_bytes - 1,
+                                 EXTENT_WRITEBACK, GFP_NOFS);
+               if (extent_op->del) {
+                       list_del_init(&extent_op->list);
+                       unlock_extent(&info->extent_ins, extent_op->bytenr,
+                                     extent_op->bytenr + extent_op->num_bytes
+                                     - 1, GFP_NOFS);
+                       kfree(extent_op);
+               }
+       }
+       mutex_unlock(&info->extent_ins_mutex);
 
-                       err = insert_extent_backref(trans, extent_root, path,
-                                               start, extent_op->parent,
-                                               extent_root->root_key.objectid,
-                                               extent_op->generation,
-                                               extent_op->level);
-                       BUG_ON(err);
-               } else if (extent_op->type == PENDING_BACKREF_UPDATE) {
-                       err = lookup_extent_backref(trans, extent_root, path,
-                                               start, extent_op->orig_parent,
-                                               extent_root->root_key.objectid,
-                                               extent_op->orig_generation,
-                                               extent_op->level, 0);
-                       BUG_ON(err);
+       /*
+        * still have things left on the update list, go ahead an update
+        * everything
+        */
+       if (!list_empty(&update_list)) {
+               ret = update_backrefs(trans, extent_root, path, &update_list);
+               BUG_ON(ret);
+       }
 
-                       mutex_lock(&info->extent_ins_mutex);
-                       clear_extent_bits(&info->extent_ins, start, end,
-                                         EXTENT_WRITEBACK, GFP_NOFS);
-                       mutex_unlock(&info->extent_ins_mutex);
+       /*
+        * if no inserts need to be done, but we skipped some extents and we
+        * need to make sure everything is cleaned then reset everything and
+        * go back to the beginning
+        */
+       if (!num_inserts && all && skipped) {
+               search = 0;
+               skipped = 0;
+               INIT_LIST_HEAD(&update_list);
+               INIT_LIST_HEAD(&insert_list);
+               goto again;
+       } else if (!num_inserts) {
+               goto out;
+       }
 
-                       key.objectid = start;
-                       key.offset = extent_op->parent;
-                       key.type = BTRFS_EXTENT_REF_KEY;
-                       err = btrfs_set_item_key_safe(trans, extent_root, path,
-                                                     &key);
-                       BUG_ON(err);
-                       ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
-                                            struct btrfs_extent_ref);
-                       btrfs_set_ref_generation(path->nodes[0], ref,
-                                                extent_op->generation);
-                       btrfs_mark_buffer_dirty(path->nodes[0]);
-                       btrfs_release_path(extent_root, path);
-               } else {
-                       BUG_ON(1);
+       /*
+        * process the insert extents list.  Again if we are deleting this
+        * extent, then just unlock it, pin down the bytes if need be, and be
+        * done with it.  Saves us from having to actually insert the extent
+        * into the tree and then subsequently come along and delete it
+        */
+       mutex_lock(&info->extent_ins_mutex);
+       list_for_each_entry_safe(extent_op, tmp, &insert_list, list) {
+               clear_extent_bits(&info->extent_ins, extent_op->bytenr,
+                                 extent_op->bytenr + extent_op->num_bytes - 1,
+                                 EXTENT_WRITEBACK, GFP_NOFS);
+               if (extent_op->del) {
+                       list_del_init(&extent_op->list);
+                       unlock_extent(&info->extent_ins, extent_op->bytenr,
+                                     extent_op->bytenr + extent_op->num_bytes
+                                     - 1, GFP_NOFS);
+
+                       mutex_lock(&extent_root->fs_info->pinned_mutex);
+                       ret = pin_down_bytes(trans, extent_root,
+                                            extent_op->bytenr,
+                                            extent_op->num_bytes, 0);
+                       mutex_unlock(&extent_root->fs_info->pinned_mutex);
+
+                       ret = update_block_group(trans, extent_root,
+                                                extent_op->bytenr,
+                                                extent_op->num_bytes,
+                                                0, ret > 0);
+                       BUG_ON(ret);
+                       kfree(extent_op);
+                       num_inserts--;
                }
-               kfree(extent_op);
-               unlock_extent(&info->extent_ins, start, end, GFP_NOFS);
-               if (all)
-                       search = 0;
-               else
-                       search = end + 1;
+       }
+       mutex_unlock(&info->extent_ins_mutex);
 
-               cond_resched();
+       ret = insert_extents(trans, extent_root, path, &insert_list,
+                            num_inserts);
+       BUG_ON(ret);
+
+       /*
+        * if we broke out of the loop in order to insert stuff because we hit
+        * the maximum number of inserts at a time we can handle, then loop
+        * back and pick up where we left off
+        */
+       if (num_inserts == max_inserts) {
+               INIT_LIST_HEAD(&insert_list);
+               INIT_LIST_HEAD(&update_list);
+               num_inserts = 0;
+               goto again;
        }
+
+       /*
+        * again, if we need to make absolutely sure there are no more pending
+        * extent operations left and we know that we skipped some, go back to
+        * the beginning and do it all again
+        */
+       if (all && skipped) {
+               INIT_LIST_HEAD(&insert_list);
+               INIT_LIST_HEAD(&update_list);
+               search = 0;
+               skipped = 0;
+               num_inserts = 0;
+               goto again;
+       }
+out:
        btrfs_free_path(path);
        return 0;
 }
@@ -1798,6 +2389,12 @@ static int __free_extent(struct btrfs_trans_handle *trans,
                        btrfs_release_path(extent_root, path);
                        ret = btrfs_search_slot(trans, extent_root,
                                                &key, path, -1, 1);
+                       if (ret) {
+                               printk(KERN_ERR "umm, got %d back from search"
+                                      ", was looking for %Lu\n", ret,
+                                      bytenr);
+                               btrfs_print_leaf(extent_root, path->nodes[0]);
+                       }
                        BUG_ON(ret);
                        extent_slot = path->slots[0];
                }
@@ -1917,32 +2514,42 @@ static int del_pending_extents(struct btrfs_trans_handle *trans, struct
        u64 end;
        u64 priv;
        u64 search = 0;
+       int nr = 0, skipped = 0;
        struct extent_io_tree *pending_del;
        struct extent_io_tree *extent_ins;
        struct pending_extent_op *extent_op;
        struct btrfs_fs_info *info = extent_root->fs_info;
+       struct list_head delete_list;
 
+       INIT_LIST_HEAD(&delete_list);
        extent_ins = &extent_root->fs_info->extent_ins;
        pending_del = &extent_root->fs_info->pending_del;
 
+again:
+       mutex_lock(&info->extent_ins_mutex);
        while(1) {
-               mutex_lock(&info->extent_ins_mutex);
                ret = find_first_extent_bit(pending_del, search, &start, &end,
                                            EXTENT_WRITEBACK);
                if (ret) {
-                       mutex_unlock(&info->extent_ins_mutex);
-                       if (all && search) {
+                       if (all && skipped && !nr) {
                                search = 0;
                                continue;
                        }
+                       mutex_unlock(&info->extent_ins_mutex);
                        break;
                }
 
                ret = try_lock_extent(extent_ins, start, end, GFP_NOFS);
                if (!ret) {
                        search = end+1;
-                       mutex_unlock(&info->extent_ins_mutex);
-                       cond_resched();
+                       skipped = 1;
+
+                       if (need_resched()) {
+                               mutex_unlock(&info->extent_ins_mutex);
+                               cond_resched();
+                               mutex_lock(&info->extent_ins_mutex);
+                       }
+
                        continue;
                }
                BUG_ON(ret < 0);
@@ -1955,15 +2562,8 @@ static int del_pending_extents(struct btrfs_trans_handle *trans, struct
                                  GFP_NOFS);
                if (!test_range_bit(extent_ins, start, end,
                                    EXTENT_WRITEBACK, 0)) {
-                       mutex_unlock(&info->extent_ins_mutex);
-free_extent:
-                       ret = __free_extent(trans, extent_root,
-                                           start, end + 1 - start,
-                                           extent_op->orig_parent,
-                                           extent_root->root_key.objectid,
-                                           extent_op->orig_generation,
-                                           extent_op->level, 1, 0);
-                       kfree(extent_op);
+                       list_add_tail(&extent_op->list, &delete_list);
+                       nr++;
                } else {
                        kfree(extent_op);
 
@@ -1976,10 +2576,12 @@ free_extent:
                        clear_extent_bits(&info->extent_ins, start, end,
                                          EXTENT_WRITEBACK, GFP_NOFS);
 
-                       mutex_unlock(&info->extent_ins_mutex);
-
-                       if (extent_op->type == PENDING_BACKREF_UPDATE)
-                               goto free_extent;
+                       if (extent_op->type == PENDING_BACKREF_UPDATE) {
+                               list_add_tail(&extent_op->list, &delete_list);
+                               search = end + 1;
+                               nr++;
+                               continue;
+                       }
 
                        mutex_lock(&extent_root->fs_info->pinned_mutex);
                        ret = pin_down_bytes(trans, extent_root, start,
@@ -1989,19 +2591,34 @@ free_extent:
                        ret = update_block_group(trans, extent_root, start,
                                                end + 1 - start, 0, ret > 0);
 
+                       unlock_extent(extent_ins, start, end, GFP_NOFS);
                        BUG_ON(ret);
                        kfree(extent_op);
                }
                if (ret)
                        err = ret;
-               unlock_extent(extent_ins, start, end, GFP_NOFS);
 
-               if (all)
-                       search = 0;
-               else
-                       search = end + 1;
-               cond_resched();
+               search = end + 1;
+
+               if (need_resched()) {
+                       mutex_unlock(&info->extent_ins_mutex);
+                       cond_resched();
+                       mutex_lock(&info->extent_ins_mutex);
+               }
        }
+
+       if (nr) {
+               ret = free_extents(trans, extent_root, &delete_list);
+               BUG_ON(ret);
+       }
+
+       if (all && skipped) {
+               INIT_LIST_HEAD(&delete_list);
+               search = 0;
+               nr = 0;
+               goto again;
+       }
+
        return err;
 }
 
@@ -2020,7 +2637,29 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
 
        WARN_ON(num_bytes < root->sectorsize);
        if (root == extent_root) {
-               struct pending_extent_op *extent_op;
+               struct pending_extent_op *extent_op = NULL;
+
+               mutex_lock(&root->fs_info->extent_ins_mutex);
+               if (test_range_bit(&root->fs_info->extent_ins, bytenr,
+                               bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) {
+                       u64 priv;
+                       ret = get_state_private(&root->fs_info->extent_ins,
+                                               bytenr, &priv);
+                       BUG_ON(ret);
+                       extent_op = (struct pending_extent_op *)
+                                               (unsigned long)priv;
+
+                       extent_op->del = 1;
+                       if (extent_op->type == PENDING_EXTENT_INSERT) {
+                               mutex_unlock(&root->fs_info->extent_ins_mutex);
+                               return 0;
+                       }
+               }
+
+               if (extent_op) {
+                       ref_generation = extent_op->orig_generation;
+                       parent = extent_op->orig_parent;
+               }
 
                extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
                BUG_ON(!extent_op);
@@ -2033,8 +2672,9 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
                extent_op->generation = ref_generation;
                extent_op->orig_generation = ref_generation;
                extent_op->level = (int)owner_objectid;
+               INIT_LIST_HEAD(&extent_op->list);
+               extent_op->del = 0;
 
-               mutex_lock(&root->fs_info->extent_ins_mutex);
                set_extent_bits(&root->fs_info->pending_del,
                                bytenr, bytenr + num_bytes - 1,
                                EXTENT_WRITEBACK, GFP_NOFS);
@@ -2112,12 +2752,14 @@ static int noinline find_free_extent(struct btrfs_trans_handle *trans,
        struct btrfs_root * root = orig_root->fs_info->extent_root;
        u64 total_needed = num_bytes;
        u64 *last_ptr = NULL;
+       u64 last_wanted = 0;
        struct btrfs_block_group_cache *block_group = NULL;
        int chunk_alloc_done = 0;
        int empty_cluster = 2 * 1024 * 1024;
        int allowed_chunk_alloc = 0;
        struct list_head *head = NULL, *cur = NULL;
        int loop = 0;
+       int extra_loop = 0;
        struct btrfs_space_info *space_info;
 
        WARN_ON(num_bytes < root->sectorsize);
@@ -2130,23 +2772,34 @@ static int noinline find_free_extent(struct btrfs_trans_handle *trans,
 
        if (data & BTRFS_BLOCK_GROUP_METADATA) {
                last_ptr = &root->fs_info->last_alloc;
-               empty_cluster = 256 * 1024;
+               empty_cluster = 64 * 1024;
        }
 
        if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD))
                last_ptr = &root->fs_info->last_data_alloc;
 
        if (last_ptr) {
-               if (*last_ptr)
+               if (*last_ptr) {
                        hint_byte = *last_ptr;
-               else
+                       last_wanted = *last_ptr;
+               } else
                        empty_size += empty_cluster;
+       } else {
+               empty_cluster = 0;
        }
        search_start = max(search_start, first_logical_byte(root, 0));
        search_start = max(search_start, hint_byte);
-       total_needed += empty_size;
 
+       if (last_wanted && search_start != last_wanted) {
+               last_wanted = 0;
+               empty_size += empty_cluster;
+       }
+
+       total_needed += empty_size;
        block_group = btrfs_lookup_block_group(root->fs_info, search_start);
+       if (!block_group)
+               block_group = btrfs_lookup_first_block_group(root->fs_info,
+                                                            search_start);
        space_info = __find_space_info(root->fs_info, data);
 
        down_read(&space_info->groups_sem);
@@ -2157,7 +2810,12 @@ static int noinline find_free_extent(struct btrfs_trans_handle *trans,
                 * group thats not of the proper type, while looping this
                 * should never happen
                 */
-               WARN_ON(!block_group);
+               if (empty_size)
+                       extra_loop = 1;
+
+               if (!block_group)
+                       goto new_group_no_lock;
+
                mutex_lock(&block_group->alloc_mutex);
                if (unlikely(!block_group_bits(block_group, data)))
                        goto new_group;
@@ -2188,6 +2846,24 @@ static int noinline find_free_extent(struct btrfs_trans_handle *trans,
                        if (search_start + num_bytes > end)
                                goto new_group;
 
+                       if (last_wanted && search_start != last_wanted) {
+                               total_needed += empty_cluster;
+                               empty_size += empty_cluster;
+                               last_wanted = 0;
+                               /*
+                                * if search_start is still in this block group
+                                * then we just re-search this block group
+                                */
+                               if (search_start >= start &&
+                                   search_start < end) {
+                                       mutex_unlock(&block_group->alloc_mutex);
+                                       continue;
+                               }
+
+                               /* else we go to the next block group */
+                               goto new_group;
+                       }
+
                        if (exclude_nr > 0 &&
                            (search_start + num_bytes > exclude_start &&
                             search_start < exclude_start + exclude_nr)) {
@@ -2199,6 +2875,7 @@ static int noinline find_free_extent(struct btrfs_trans_handle *trans,
                                if (search_start >= start &&
                                    search_start < end) {
                                        mutex_unlock(&block_group->alloc_mutex);
+                                       last_wanted = 0;
                                        continue;
                                }
 
@@ -2217,6 +2894,12 @@ static int noinline find_free_extent(struct btrfs_trans_handle *trans,
                }
 new_group:
                mutex_unlock(&block_group->alloc_mutex);
+new_group_no_lock:
+               /* don't try to compare new allocations against the
+                * last allocation any more
+                */
+               last_wanted = 0;
+
                /*
                 * Here's how this works.
                 * loop == 0: we were searching a block group via a hint
@@ -2232,27 +2915,45 @@ new_group:
                if (loop == 0) {
                        head = &space_info->block_groups;
                        cur = head->next;
-
-                       if (last_ptr && *last_ptr) {
-                               total_needed += empty_cluster;
-                               *last_ptr = 0;
-                       }
                        loop++;
                } else if (loop == 1 && cur == head) {
+                       int keep_going;
+
+                       /* at this point we give up on the empty_size
+                        * allocations and just try to allocate the min
+                        * space.
+                        *
+                        * The extra_loop field was set if an empty_size
+                        * allocation was attempted above, and if this
+                        * is try we need to try the loop again without
+                        * the additional empty_size.
+                        */
+                       total_needed -= empty_size;
+                       empty_size = 0;
+                       keep_going = extra_loop;
+                       loop++;
+
                        if (allowed_chunk_alloc && !chunk_alloc_done) {
                                up_read(&space_info->groups_sem);
                                ret = do_chunk_alloc(trans, root, num_bytes +
                                                     2 * 1024 * 1024, data, 1);
-                               if (ret < 0)
-                                       break;
                                down_read(&space_info->groups_sem);
-                               loop++;
+                               if (ret < 0)
+                                       goto loop_check;
                                head = &space_info->block_groups;
-                               cur = head->next;
+                               /*
+                                * we've allocated a new chunk, keep
+                                * trying
+                                */
+                               keep_going = 1;
                                chunk_alloc_done = 1;
                        } else if (!allowed_chunk_alloc) {
                                space_info->force_alloc = 1;
-                               break;
+                       }
+loop_check:
+                       if (keep_going) {
+                               cur = head->next;
+                               extra_loop = 0;
                        } else {
                                break;
                        }
@@ -2332,7 +3033,7 @@ static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
                data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
        }
 again:
-       data = reduce_alloc_profile(root, data);
+       data = btrfs_reduce_alloc_profile(root, data);
        /*
         * the only place that sets empty_size is btrfs_realloc_node, which
         * is not called recursively on allocations
@@ -2450,6 +3151,8 @@ static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
                extent_op->generation = ref_generation;
                extent_op->orig_generation = 0;
                extent_op->level = (int)owner;
+               INIT_LIST_HEAD(&extent_op->list);
+               extent_op->del = 0;
 
                mutex_lock(&root->fs_info->extent_ins_mutex);
                set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
@@ -3426,9 +4129,7 @@ walk_down:
 next:
                level--;
                btrfs_release_path(extent_root, path);
-               if (need_resched()) {
-                       cond_resched();
-               }
+               cond_resched();
        }
        /* reached lowest level */
        ret = 1;
@@ -3539,9 +4240,7 @@ found:
                }
 
                btrfs_release_path(extent_root, path);
-               if (need_resched()) {
-                       cond_resched();
-               }
+               cond_resched();
        }
        /* reached max tree level, but no tree root found. */
        BUG();
@@ -3654,8 +4353,9 @@ static int noinline get_new_locations(struct inode *reloc_inode,
                exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
                exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
                                                                           fi);
-               WARN_ON(exts[nr].offset > 0);
-               WARN_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
+               BUG_ON(exts[nr].offset > 0);
+               BUG_ON(exts[nr].compression || exts[nr].encryption);
+               BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
 
                cur_pos += exts[nr].num_bytes;
                nr++;
@@ -3709,6 +4409,7 @@ static int noinline replace_one_extent(struct btrfs_trans_handle *trans,
        u32 nritems;
        int nr_scaned = 0;
        int extent_locked = 0;
+       int extent_type;
        int ret;
 
        memcpy(&key, leaf_key, sizeof(key));
@@ -3781,8 +4482,9 @@ next:
                }
                fi = btrfs_item_ptr(leaf, path->slots[0],
                                    struct btrfs_file_extent_item);
-               if ((btrfs_file_extent_type(leaf, fi) !=
-                    BTRFS_FILE_EXTENT_REG) ||
+               extent_type = btrfs_file_extent_type(leaf, fi);
+               if ((extent_type != BTRFS_FILE_EXTENT_REG &&
+                    extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
                    (btrfs_file_extent_disk_bytenr(leaf, fi) !=
                     extent_key->objectid)) {
                        path->slots[0]++;
@@ -3865,16 +4567,10 @@ next:
 
                if (nr_extents == 1) {
                        /* update extent pointer in place */
-                       btrfs_set_file_extent_generation(leaf, fi,
-                                               trans->transid);
                        btrfs_set_file_extent_disk_bytenr(leaf, fi,
                                                new_extents[0].disk_bytenr);
                        btrfs_set_file_extent_disk_num_bytes(leaf, fi,
                                                new_extents[0].disk_num_bytes);
-                       btrfs_set_file_extent_ram_bytes(leaf, fi,
-                                               new_extents[0].ram_bytes);
-                       ext_offset += new_extents[0].offset;
-                       btrfs_set_file_extent_offset(leaf, fi, ext_offset);
                        btrfs_mark_buffer_dirty(leaf);
 
                        btrfs_drop_extent_cache(inode, key.offset,
@@ -3901,6 +4597,8 @@ next:
                        btrfs_release_path(root, path);
                        key.offset += num_bytes;
                } else {
+                       BUG_ON(1);
+#if 0
                        u64 alloc_hint;
                        u64 extent_len;
                        int i;
@@ -3977,6 +4675,7 @@ next:
                                        break;
                        }
                        BUG_ON(i >= nr_extents);
+#endif
                }
 
                if (extent_locked) {
@@ -4156,15 +4855,10 @@ static int noinline replace_extents_in_leaf(struct btrfs_trans_handle *trans,
                ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
                ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
 
-               btrfs_set_file_extent_generation(leaf, fi, trans->transid);
-               btrfs_set_file_extent_ram_bytes(leaf, fi,
-                                               new_extent->ram_bytes);
                btrfs_set_file_extent_disk_bytenr(leaf, fi,
                                                new_extent->disk_bytenr);
                btrfs_set_file_extent_disk_num_bytes(leaf, fi,
                                                new_extent->disk_num_bytes);
-               new_extent->offset += btrfs_file_extent_offset(leaf, fi);
-               btrfs_set_file_extent_offset(leaf, fi, new_extent->offset);
                btrfs_mark_buffer_dirty(leaf);
 
                ret = btrfs_inc_extent_ref(trans, root,
@@ -4435,7 +5129,8 @@ static int noinline relocate_one_path(struct btrfs_trans_handle *trans,
                        else
                                btrfs_node_key_to_cpu(eb, &keys[level], 0);
                }
-               if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
+               if (nodes[0] &&
+                   ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
                        eb = path->nodes[0];
                        ret = replace_extents_in_leaf(trans, reloc_root, eb,
                                                      group, reloc_inode);
@@ -4625,12 +5320,15 @@ static int noinline relocate_one_extent(struct btrfs_root *extent_root,
                         */
                        if (!new_extents) {
                                u64 group_start = group->key.objectid;
+                               new_extents = kmalloc(sizeof(*new_extents),
+                                                     GFP_NOFS);
+                               nr_extents = 1;
                                ret = get_new_locations(reloc_inode,
                                                        extent_key,
-                                                       group_start, 0,
+                                                       group_start, 1,
                                                        &new_extents,
                                                        &nr_extents);
-                               if (ret < 0)
+                               if (ret)
                                        goto out;
                        }
                        btrfs_record_root_in_trans(found_root);
@@ -4673,7 +5371,7 @@ static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
        u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
                BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
 
-       num_devices = root->fs_info->fs_devices->num_devices;
+       num_devices = root->fs_info->fs_devices->rw_devices;
        if (num_devices == 1) {
                stripped |= BTRFS_BLOCK_GROUP_DUP;
                stripped = flags & ~stripped;
@@ -4762,7 +5460,8 @@ static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
        btrfs_set_inode_generation(leaf, item, 1);
        btrfs_set_inode_size(leaf, item, size);
        btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
-       btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NODATASUM);
+       btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NODATASUM |
+                                         BTRFS_INODE_NOCOMPRESS);
        btrfs_mark_buffer_dirty(leaf);
        btrfs_release_path(root, path);
 out:
@@ -4835,6 +5534,7 @@ int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start)
        struct inode *reloc_inode;
        struct btrfs_block_group_cache *block_group;
        struct btrfs_key key;
+       u64 skipped;
        u64 cur_byte;
        u64 total_found;
        u32 nritems;
@@ -4858,12 +5558,12 @@ int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start)
        BUG_ON(IS_ERR(reloc_inode));
 
        __alloc_chunk_for_shrink(root, block_group, 1);
-       block_group->ro = 1;
-       block_group->space_info->total_bytes -= block_group->key.offset;
+       set_block_group_readonly(block_group);
 
        btrfs_start_delalloc_inodes(info->tree_root);
        btrfs_wait_ordered_extents(info->tree_root, 0);
 again:
+       skipped = 0;
        total_found = 0;
        progress = 0;
        key.objectid = block_group->key.objectid;
@@ -4926,6 +5626,8 @@ next:
                ret = relocate_one_extent(root, path, &key, block_group,
                                          reloc_inode, pass);
                BUG_ON(ret < 0);
+               if (ret > 0)
+                       skipped++;
 
                key.objectid = cur_byte;
                key.type = 0;
@@ -4944,6 +5646,11 @@ next:
                printk("btrfs found %llu extents in pass %d\n",
                       (unsigned long long)total_found, pass);
                pass++;
+               if (total_found == skipped && pass > 2) {
+                       iput(reloc_inode);
+                       reloc_inode = create_reloc_inode(info, block_group);
+                       pass = 0;
+               }
                goto again;
        }
 
@@ -5011,17 +5718,17 @@ int btrfs_free_block_groups(struct btrfs_fs_info *info)
        while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
                block_group = rb_entry(n, struct btrfs_block_group_cache,
                                       cache_node);
-
-               spin_unlock(&info->block_group_cache_lock);
-               btrfs_remove_free_space_cache(block_group);
-               spin_lock(&info->block_group_cache_lock);
-
                rb_erase(&block_group->cache_node,
                         &info->block_group_cache_tree);
+               spin_unlock(&info->block_group_cache_lock);
+
+               btrfs_remove_free_space_cache(block_group);
                down_write(&block_group->space_info->groups_sem);
                list_del(&block_group->list);
                up_write(&block_group->space_info->groups_sem);
                kfree(block_group);
+
+               spin_lock(&info->block_group_cache_lock);
        }
        spin_unlock(&info->block_group_cache_lock);
        return 0;
@@ -5088,6 +5795,8 @@ int btrfs_read_block_groups(struct btrfs_root *root)
                BUG_ON(ret);
 
                set_avail_alloc_bits(root->fs_info, cache->flags);
+               if (btrfs_chunk_readonly(root, cache->key.objectid))
+                       set_block_group_readonly(cache);
        }
        ret = 0;
 error:
@@ -5158,6 +5867,7 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
 
        block_group = btrfs_lookup_block_group(root->fs_info, group_start);
        BUG_ON(!block_group);
+       BUG_ON(!block_group->ro);
 
        memcpy(&key, &block_group->key, sizeof(key));
 
@@ -5171,6 +5881,12 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
        list_del(&block_group->list);
        up_write(&block_group->space_info->groups_sem);
 
+       spin_lock(&block_group->space_info->lock);
+       block_group->space_info->total_bytes -= block_group->key.offset;
+       block_group->space_info->bytes_readonly -= block_group->key.offset;
+       spin_unlock(&block_group->space_info->lock);
+       block_group->space_info->full = 0;
+
        /*
        memset(shrink_block_group, 0, sizeof(*shrink_block_group));
        kfree(shrink_block_group);