Merge branch 'for-chris' of git://git.jan-o-sch.net/btrfs-unstable into for-linus
authorChris Mason <chris.mason@oracle.com>
Thu, 31 May 2012 20:50:28 +0000 (16:50 -0400)
committerChris Mason <chris.mason@oracle.com>
Thu, 31 May 2012 20:49:53 +0000 (16:49 -0400)
Conflicts:
fs/btrfs/ulist.h

Signed-off-by: Chris Mason <chris.mason@oracle.com>
1  2 
fs/btrfs/ctree.c
fs/btrfs/ctree.h
fs/btrfs/disk-io.c
fs/btrfs/extent-tree.c
fs/btrfs/extent_io.c
fs/btrfs/extent_io.h
fs/btrfs/ioctl.c
fs/btrfs/transaction.c
fs/btrfs/ulist.c
fs/btrfs/ulist.h

diff --combined fs/btrfs/ctree.c
index 99fcad631a216307579a209398e24fed59a3300f,b4534d918e4234f3a89f958159dd69649a236a34..d7a96cfdc50ae6a2d8afef1dad7ca3642248bbb8
@@@ -18,6 -18,7 +18,7 @@@
  
  #include <linux/sched.h>
  #include <linux/slab.h>
+ #include <linux/rbtree.h>
  #include "ctree.h"
  #include "disk-io.h"
  #include "transaction.h"
@@@ -37,7 -38,16 +38,16 @@@ static int balance_node_right(struct bt
                              struct extent_buffer *dst_buf,
                              struct extent_buffer *src_buf);
  static void del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
-                  struct btrfs_path *path, int level, int slot);
+                   struct btrfs_path *path, int level, int slot,
+                   int tree_mod_log);
+ static void tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
+                                struct extent_buffer *eb);
+ struct extent_buffer *read_old_tree_block(struct btrfs_root *root, u64 bytenr,
+                                         u32 blocksize, u64 parent_transid,
+                                         u64 time_seq);
+ struct extent_buffer *btrfs_find_old_tree_block(struct btrfs_root *root,
+                                               u64 bytenr, u32 blocksize,
+                                               u64 time_seq);
  
  struct btrfs_path *btrfs_alloc_path(void)
  {
@@@ -255,7 -265,7 +265,7 @@@ int btrfs_copy_root(struct btrfs_trans_
  
        cow = btrfs_alloc_free_block(trans, root, buf->len, 0,
                                     new_root_objectid, &disk_key, level,
-                                    buf->start, 0, 1);
+                                    buf->start, 0);
        if (IS_ERR(cow))
                return PTR_ERR(cow);
  
        return 0;
  }
  
+ enum mod_log_op {
+       MOD_LOG_KEY_REPLACE,
+       MOD_LOG_KEY_ADD,
+       MOD_LOG_KEY_REMOVE,
+       MOD_LOG_KEY_REMOVE_WHILE_FREEING,
+       MOD_LOG_KEY_REMOVE_WHILE_MOVING,
+       MOD_LOG_MOVE_KEYS,
+       MOD_LOG_ROOT_REPLACE,
+ };
+ struct tree_mod_move {
+       int dst_slot;
+       int nr_items;
+ };
+ struct tree_mod_root {
+       u64 logical;
+       u8 level;
+ };
+ struct tree_mod_elem {
+       struct rb_node node;
+       u64 index;              /* shifted logical */
+       struct seq_list elem;
+       enum mod_log_op op;
+       /* this is used for MOD_LOG_KEY_* and MOD_LOG_MOVE_KEYS operations */
+       int slot;
+       /* this is used for MOD_LOG_KEY* and MOD_LOG_ROOT_REPLACE */
+       u64 generation;
+       /* those are used for op == MOD_LOG_KEY_{REPLACE,REMOVE} */
+       struct btrfs_disk_key key;
+       u64 blockptr;
+       /* this is used for op == MOD_LOG_MOVE_KEYS */
+       struct tree_mod_move move;
+       /* this is used for op == MOD_LOG_ROOT_REPLACE */
+       struct tree_mod_root old_root;
+ };
+ static inline void
+ __get_tree_mod_seq(struct btrfs_fs_info *fs_info, struct seq_list *elem)
+ {
+       elem->seq = atomic_inc_return(&fs_info->tree_mod_seq);
+       list_add_tail(&elem->list, &fs_info->tree_mod_seq_list);
+ }
+ void btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info,
+                           struct seq_list *elem)
+ {
+       elem->flags = 1;
+       spin_lock(&fs_info->tree_mod_seq_lock);
+       __get_tree_mod_seq(fs_info, elem);
+       spin_unlock(&fs_info->tree_mod_seq_lock);
+ }
+ void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
+                           struct seq_list *elem)
+ {
+       struct rb_root *tm_root;
+       struct rb_node *node;
+       struct rb_node *next;
+       struct seq_list *cur_elem;
+       struct tree_mod_elem *tm;
+       u64 min_seq = (u64)-1;
+       u64 seq_putting = elem->seq;
+       if (!seq_putting)
+               return;
+       BUG_ON(!(elem->flags & 1));
+       spin_lock(&fs_info->tree_mod_seq_lock);
+       list_del(&elem->list);
+       list_for_each_entry(cur_elem, &fs_info->tree_mod_seq_list, list) {
+               if ((cur_elem->flags & 1) && cur_elem->seq < min_seq) {
+                       if (seq_putting > cur_elem->seq) {
+                               /*
+                                * blocker with lower sequence number exists, we
+                                * cannot remove anything from the log
+                                */
+                               goto out;
+                       }
+                       min_seq = cur_elem->seq;
+               }
+       }
+       /*
+        * anything that's lower than the lowest existing (read: blocked)
+        * sequence number can be removed from the tree.
+        */
+       write_lock(&fs_info->tree_mod_log_lock);
+       tm_root = &fs_info->tree_mod_log;
+       for (node = rb_first(tm_root); node; node = next) {
+               next = rb_next(node);
+               tm = container_of(node, struct tree_mod_elem, node);
+               if (tm->elem.seq > min_seq)
+                       continue;
+               rb_erase(node, tm_root);
+               list_del(&tm->elem.list);
+               kfree(tm);
+       }
+       write_unlock(&fs_info->tree_mod_log_lock);
+ out:
+       spin_unlock(&fs_info->tree_mod_seq_lock);
+ }
+ /*
+  * key order of the log:
+  *       index -> sequence
+  *
+  * the index is the shifted logical of the *new* root node for root replace
+  * operations, or the shifted logical of the affected block for all other
+  * operations.
+  */
+ static noinline int
+ __tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm)
+ {
+       struct rb_root *tm_root;
+       struct rb_node **new;
+       struct rb_node *parent = NULL;
+       struct tree_mod_elem *cur;
+       int ret = 0;
+       BUG_ON(!tm || !tm->elem.seq);
+       write_lock(&fs_info->tree_mod_log_lock);
+       tm_root = &fs_info->tree_mod_log;
+       new = &tm_root->rb_node;
+       while (*new) {
+               cur = container_of(*new, struct tree_mod_elem, node);
+               parent = *new;
+               if (cur->index < tm->index)
+                       new = &((*new)->rb_left);
+               else if (cur->index > tm->index)
+                       new = &((*new)->rb_right);
+               else if (cur->elem.seq < tm->elem.seq)
+                       new = &((*new)->rb_left);
+               else if (cur->elem.seq > tm->elem.seq)
+                       new = &((*new)->rb_right);
+               else {
+                       kfree(tm);
+                       ret = -EEXIST;
+                       goto unlock;
+               }
+       }
+       rb_link_node(&tm->node, parent, new);
+       rb_insert_color(&tm->node, tm_root);
+ unlock:
+       write_unlock(&fs_info->tree_mod_log_lock);
+       return ret;
+ }
+ static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info,
+                                   struct extent_buffer *eb) {
+       smp_mb();
+       if (list_empty(&(fs_info)->tree_mod_seq_list))
+               return 1;
+       if (!eb)
+               return 0;
+       if (btrfs_header_level(eb) == 0)
+               return 1;
+       return 0;
+ }
+ static inline int tree_mod_alloc(struct btrfs_fs_info *fs_info, gfp_t flags,
+                                struct tree_mod_elem **tm_ret)
+ {
+       struct tree_mod_elem *tm;
+       int seq;
+       if (tree_mod_dont_log(fs_info, NULL))
+               return 0;
+       tm = *tm_ret = kzalloc(sizeof(*tm), flags);
+       if (!tm)
+               return -ENOMEM;
+       tm->elem.flags = 0;
+       spin_lock(&fs_info->tree_mod_seq_lock);
+       if (list_empty(&fs_info->tree_mod_seq_list)) {
+               /*
+                * someone emptied the list while we were waiting for the lock.
+                * we must not add to the list, because no blocker exists. items
+                * are removed from the list only when the existing blocker is
+                * removed from the list.
+                */
+               kfree(tm);
+               seq = 0;
+       } else {
+               __get_tree_mod_seq(fs_info, &tm->elem);
+               seq = tm->elem.seq;
+       }
+       spin_unlock(&fs_info->tree_mod_seq_lock);
+       return seq;
+ }
+ static noinline int
+ tree_mod_log_insert_key_mask(struct btrfs_fs_info *fs_info,
+                            struct extent_buffer *eb, int slot,
+                            enum mod_log_op op, gfp_t flags)
+ {
+       struct tree_mod_elem *tm;
+       int ret;
+       ret = tree_mod_alloc(fs_info, flags, &tm);
+       if (ret <= 0)
+               return ret;
+       tm->index = eb->start >> PAGE_CACHE_SHIFT;
+       if (op != MOD_LOG_KEY_ADD) {
+               btrfs_node_key(eb, &tm->key, slot);
+               tm->blockptr = btrfs_node_blockptr(eb, slot);
+       }
+       tm->op = op;
+       tm->slot = slot;
+       tm->generation = btrfs_node_ptr_generation(eb, slot);
+       return __tree_mod_log_insert(fs_info, tm);
+ }
+ static noinline int
+ tree_mod_log_insert_key(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
+                       int slot, enum mod_log_op op)
+ {
+       return tree_mod_log_insert_key_mask(fs_info, eb, slot, op, GFP_NOFS);
+ }
+ static noinline int
+ tree_mod_log_insert_move(struct btrfs_fs_info *fs_info,
+                        struct extent_buffer *eb, int dst_slot, int src_slot,
+                        int nr_items, gfp_t flags)
+ {
+       struct tree_mod_elem *tm;
+       int ret;
+       int i;
+       if (tree_mod_dont_log(fs_info, eb))
+               return 0;
+       for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
+               ret = tree_mod_log_insert_key(fs_info, eb, i + dst_slot,
+                                             MOD_LOG_KEY_REMOVE_WHILE_MOVING);
+               BUG_ON(ret < 0);
+       }
+       ret = tree_mod_alloc(fs_info, flags, &tm);
+       if (ret <= 0)
+               return ret;
+       tm->index = eb->start >> PAGE_CACHE_SHIFT;
+       tm->slot = src_slot;
+       tm->move.dst_slot = dst_slot;
+       tm->move.nr_items = nr_items;
+       tm->op = MOD_LOG_MOVE_KEYS;
+       return __tree_mod_log_insert(fs_info, tm);
+ }
+ static noinline int
+ tree_mod_log_insert_root(struct btrfs_fs_info *fs_info,
+                        struct extent_buffer *old_root,
+                        struct extent_buffer *new_root, gfp_t flags)
+ {
+       struct tree_mod_elem *tm;
+       int ret;
+       ret = tree_mod_alloc(fs_info, flags, &tm);
+       if (ret <= 0)
+               return ret;
+       tm->index = new_root->start >> PAGE_CACHE_SHIFT;
+       tm->old_root.logical = old_root->start;
+       tm->old_root.level = btrfs_header_level(old_root);
+       tm->generation = btrfs_header_generation(old_root);
+       tm->op = MOD_LOG_ROOT_REPLACE;
+       return __tree_mod_log_insert(fs_info, tm);
+ }
+ static struct tree_mod_elem *
+ __tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq,
+                     int smallest)
+ {
+       struct rb_root *tm_root;
+       struct rb_node *node;
+       struct tree_mod_elem *cur = NULL;
+       struct tree_mod_elem *found = NULL;
+       u64 index = start >> PAGE_CACHE_SHIFT;
+       read_lock(&fs_info->tree_mod_log_lock);
+       tm_root = &fs_info->tree_mod_log;
+       node = tm_root->rb_node;
+       while (node) {
+               cur = container_of(node, struct tree_mod_elem, node);
+               if (cur->index < index) {
+                       node = node->rb_left;
+               } else if (cur->index > index) {
+                       node = node->rb_right;
+               } else if (cur->elem.seq < min_seq) {
+                       node = node->rb_left;
+               } else if (!smallest) {
+                       /* we want the node with the highest seq */
+                       if (found)
+                               BUG_ON(found->elem.seq > cur->elem.seq);
+                       found = cur;
+                       node = node->rb_left;
+               } else if (cur->elem.seq > min_seq) {
+                       /* we want the node with the smallest seq */
+                       if (found)
+                               BUG_ON(found->elem.seq < cur->elem.seq);
+                       found = cur;
+                       node = node->rb_right;
+               } else {
+                       found = cur;
+                       break;
+               }
+       }
+       read_unlock(&fs_info->tree_mod_log_lock);
+       return found;
+ }
+ /*
+  * this returns the element from the log with the smallest time sequence
+  * value that's in the log (the oldest log item). any element with a time
+  * sequence lower than min_seq will be ignored.
+  */
+ static struct tree_mod_elem *
+ tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start,
+                          u64 min_seq)
+ {
+       return __tree_mod_log_search(fs_info, start, min_seq, 1);
+ }
+ /*
+  * this returns the element from the log with the largest time sequence
+  * value that's in the log (the most recent log item). any element with
+  * a time sequence lower than min_seq will be ignored.
+  */
+ static struct tree_mod_elem *
+ tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq)
+ {
+       return __tree_mod_log_search(fs_info, start, min_seq, 0);
+ }
+ static inline void
+ tree_mod_log_eb_copy(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
+                    struct extent_buffer *src, unsigned long dst_offset,
+                    unsigned long src_offset, int nr_items)
+ {
+       int ret;
+       int i;
+       if (tree_mod_dont_log(fs_info, NULL))
+               return;
+       if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0)
+               return;
+       /* speed this up by single seq for all operations? */
+       for (i = 0; i < nr_items; i++) {
+               ret = tree_mod_log_insert_key(fs_info, src, i + src_offset,
+                                             MOD_LOG_KEY_REMOVE);
+               BUG_ON(ret < 0);
+               ret = tree_mod_log_insert_key(fs_info, dst, i + dst_offset,
+                                             MOD_LOG_KEY_ADD);
+               BUG_ON(ret < 0);
+       }
+ }
+ static inline void
+ tree_mod_log_eb_move(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
+                    int dst_offset, int src_offset, int nr_items)
+ {
+       int ret;
+       ret = tree_mod_log_insert_move(fs_info, dst, dst_offset, src_offset,
+                                      nr_items, GFP_NOFS);
+       BUG_ON(ret < 0);
+ }
+ static inline void
+ tree_mod_log_set_node_key(struct btrfs_fs_info *fs_info,
+                         struct extent_buffer *eb,
+                         struct btrfs_disk_key *disk_key, int slot, int atomic)
+ {
+       int ret;
+       ret = tree_mod_log_insert_key_mask(fs_info, eb, slot,
+                                          MOD_LOG_KEY_REPLACE,
+                                          atomic ? GFP_ATOMIC : GFP_NOFS);
+       BUG_ON(ret < 0);
+ }
+ static void tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
+                                struct extent_buffer *eb)
+ {
+       int i;
+       int ret;
+       u32 nritems;
+       if (tree_mod_dont_log(fs_info, eb))
+               return;
+       nritems = btrfs_header_nritems(eb);
+       for (i = nritems - 1; i >= 0; i--) {
+               ret = tree_mod_log_insert_key(fs_info, eb, i,
+                                             MOD_LOG_KEY_REMOVE_WHILE_FREEING);
+               BUG_ON(ret < 0);
+       }
+ }
+ static inline void
+ tree_mod_log_set_root_pointer(struct btrfs_root *root,
+                             struct extent_buffer *new_root_node)
+ {
+       int ret;
+       tree_mod_log_free_eb(root->fs_info, root->node);
+       ret = tree_mod_log_insert_root(root->fs_info, root->node,
+                                      new_root_node, GFP_NOFS);
+       BUG_ON(ret < 0);
+ }
  /*
   * check if the tree block can be shared by multiple trees
   */
@@@ -409,6 -847,12 +847,12 @@@ static noinline int update_ref_for_cow(
                        ret = btrfs_dec_ref(trans, root, buf, 1, 1);
                        BUG_ON(ret); /* -ENOMEM */
                }
+               /*
+                * don't log freeing in case we're freeing the root node, this
+                * is done by tree_mod_log_set_root_pointer later
+                */
+               if (buf != root->node && btrfs_header_level(buf) != 0)
+                       tree_mod_log_free_eb(root->fs_info, buf);
                clean_tree_block(trans, root, buf);
                *last_ref = 1;
        }
@@@ -467,7 -911,7 +911,7 @@@ static noinline int __btrfs_cow_block(s
  
        cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
                                     root->root_key.objectid, &disk_key,
-                                    level, search_start, empty_size, 1);
+                                    level, search_start, empty_size);
        if (IS_ERR(cow))
                return PTR_ERR(cow);
  
                        parent_start = 0;
  
                extent_buffer_get(cow);
+               tree_mod_log_set_root_pointer(root, cow);
                rcu_assign_pointer(root->node, cow);
  
                btrfs_free_tree_block(trans, root, buf, parent_start,
-                                     last_ref, 1);
+                                     last_ref);
                free_extent_buffer(buf);
                add_root_to_dirty_list(root);
        } else {
                        parent_start = 0;
  
                WARN_ON(trans->transid != btrfs_header_generation(parent));
+               tree_mod_log_insert_key(root->fs_info, parent, parent_slot,
+                                       MOD_LOG_KEY_REPLACE);
                btrfs_set_node_blockptr(parent, parent_slot,
                                        cow->start);
                btrfs_set_node_ptr_generation(parent, parent_slot,
                                              trans->transid);
                btrfs_mark_buffer_dirty(parent);
                btrfs_free_tree_block(trans, root, buf, parent_start,
-                                     last_ref, 1);
+                                     last_ref);
        }
        if (unlock_orig)
                btrfs_tree_unlock(buf);
        return 0;
  }
  
+ /*
+  * returns the logical address of the oldest predecessor of the given root.
+  * entries older than time_seq are ignored.
+  */
+ static struct tree_mod_elem *
+ __tree_mod_log_oldest_root(struct btrfs_fs_info *fs_info,
+                          struct btrfs_root *root, u64 time_seq)
+ {
+       struct tree_mod_elem *tm;
+       struct tree_mod_elem *found = NULL;
+       u64 root_logical = root->node->start;
+       int looped = 0;
+       if (!time_seq)
+               return 0;
+       /*
+        * the very last operation that's logged for a root is the replacement
+        * operation (if it is replaced at all). this has the index of the *new*
+        * root, making it the very first operation that's logged for this root.
+        */
+       while (1) {
+               tm = tree_mod_log_search_oldest(fs_info, root_logical,
+                                               time_seq);
+               if (!looped && !tm)
+                       return 0;
+               /*
+                * we must have key remove operations in the log before the
+                * replace operation.
+                */
+               BUG_ON(!tm);
+               if (tm->op != MOD_LOG_ROOT_REPLACE)
+                       break;
+               found = tm;
+               root_logical = tm->old_root.logical;
+               BUG_ON(root_logical == root->node->start);
+               looped = 1;
+       }
+       return found;
+ }
+ /*
+  * tm is a pointer to the first operation to rewind within eb. then, all
+  * previous operations will be rewinded (until we reach something older than
+  * time_seq).
+  */
+ static void
+ __tree_mod_log_rewind(struct extent_buffer *eb, u64 time_seq,
+                     struct tree_mod_elem *first_tm)
+ {
+       u32 n;
+       struct rb_node *next;
+       struct tree_mod_elem *tm = first_tm;
+       unsigned long o_dst;
+       unsigned long o_src;
+       unsigned long p_size = sizeof(struct btrfs_key_ptr);
+       n = btrfs_header_nritems(eb);
+       while (tm && tm->elem.seq >= time_seq) {
+               /*
+                * all the operations are recorded with the operator used for
+                * the modification. as we're going backwards, we do the
+                * opposite of each operation here.
+                */
+               switch (tm->op) {
+               case MOD_LOG_KEY_REMOVE_WHILE_FREEING:
+                       BUG_ON(tm->slot < n);
+               case MOD_LOG_KEY_REMOVE_WHILE_MOVING:
+               case MOD_LOG_KEY_REMOVE:
+                       btrfs_set_node_key(eb, &tm->key, tm->slot);
+                       btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
+                       btrfs_set_node_ptr_generation(eb, tm->slot,
+                                                     tm->generation);
+                       n++;
+                       break;
+               case MOD_LOG_KEY_REPLACE:
+                       BUG_ON(tm->slot >= n);
+                       btrfs_set_node_key(eb, &tm->key, tm->slot);
+                       btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
+                       btrfs_set_node_ptr_generation(eb, tm->slot,
+                                                     tm->generation);
+                       break;
+               case MOD_LOG_KEY_ADD:
+                       if (tm->slot != n - 1) {
+                               o_dst = btrfs_node_key_ptr_offset(tm->slot);
+                               o_src = btrfs_node_key_ptr_offset(tm->slot + 1);
+                               memmove_extent_buffer(eb, o_dst, o_src, p_size);
+                       }
+                       n--;
+                       break;
+               case MOD_LOG_MOVE_KEYS:
+                       o_dst = btrfs_node_key_ptr_offset(tm->slot);
+                       o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot);
+                       memmove_extent_buffer(eb, o_dst, o_src,
+                                             tm->move.nr_items * p_size);
+                       break;
+               case MOD_LOG_ROOT_REPLACE:
+                       /*
+                        * this operation is special. for roots, this must be
+                        * handled explicitly before rewinding.
+                        * for non-roots, this operation may exist if the node
+                        * was a root: root A -> child B; then A gets empty and
+                        * B is promoted to the new root. in the mod log, we'll
+                        * have a root-replace operation for B, a tree block
+                        * that is no root. we simply ignore that operation.
+                        */
+                       break;
+               }
+               next = rb_next(&tm->node);
+               if (!next)
+                       break;
+               tm = container_of(next, struct tree_mod_elem, node);
+               if (tm->index != first_tm->index)
+                       break;
+       }
+       btrfs_set_header_nritems(eb, n);
+ }
+ static struct extent_buffer *
+ tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
+                   u64 time_seq)
+ {
+       struct extent_buffer *eb_rewin;
+       struct tree_mod_elem *tm;
+       if (!time_seq)
+               return eb;
+       if (btrfs_header_level(eb) == 0)
+               return eb;
+       tm = tree_mod_log_search(fs_info, eb->start, time_seq);
+       if (!tm)
+               return eb;
+       if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
+               BUG_ON(tm->slot != 0);
+               eb_rewin = alloc_dummy_extent_buffer(eb->start,
+                                               fs_info->tree_root->nodesize);
+               BUG_ON(!eb_rewin);
+               btrfs_set_header_bytenr(eb_rewin, eb->start);
+               btrfs_set_header_backref_rev(eb_rewin,
+                                            btrfs_header_backref_rev(eb));
+               btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
+               btrfs_set_header_level(eb_rewin, btrfs_header_level(eb));
+       } else {
+               eb_rewin = btrfs_clone_extent_buffer(eb);
+               BUG_ON(!eb_rewin);
+       }
+       extent_buffer_get(eb_rewin);
+       free_extent_buffer(eb);
+       __tree_mod_log_rewind(eb_rewin, time_seq, tm);
+       return eb_rewin;
+ }
+ static inline struct extent_buffer *
+ get_old_root(struct btrfs_root *root, u64 time_seq)
+ {
+       struct tree_mod_elem *tm;
+       struct extent_buffer *eb;
+       struct tree_mod_root *old_root;
+       u64 old_generation;
+       tm = __tree_mod_log_oldest_root(root->fs_info, root, time_seq);
+       if (!tm)
+               return root->node;
+       old_root = &tm->old_root;
+       old_generation = tm->generation;
+       tm = tree_mod_log_search(root->fs_info, old_root->logical, time_seq);
+       /*
+        * there was an item in the log when __tree_mod_log_oldest_root
+        * returned. this one must not go away, because the time_seq passed to
+        * us must be blocking its removal.
+        */
+       BUG_ON(!tm);
+       if (old_root->logical == root->node->start) {
+               /* there are logged operations for the current root */
+               eb = btrfs_clone_extent_buffer(root->node);
+       } else {
+               /* there's a root replace operation for the current root */
+               eb = alloc_dummy_extent_buffer(tm->index << PAGE_CACHE_SHIFT,
+                                              root->nodesize);
+               btrfs_set_header_bytenr(eb, eb->start);
+               btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
+               btrfs_set_header_owner(eb, root->root_key.objectid);
+       }
+       if (!eb)
+               return NULL;
+       btrfs_set_header_level(eb, old_root->level);
+       btrfs_set_header_generation(eb, old_generation);
+       __tree_mod_log_rewind(eb, time_seq, tm);
+       return eb;
+ }
  static inline int should_cow_block(struct btrfs_trans_handle *trans,
                                   struct btrfs_root *root,
                                   struct extent_buffer *buf)
@@@ -739,11 -1390,7 +1390,11 @@@ int btrfs_realloc_node(struct btrfs_tra
                                if (!cur)
                                        return -EIO;
                        } else if (!uptodate) {
 -                              btrfs_read_buffer(cur, gen);
 +                              err = btrfs_read_buffer(cur, gen);
 +                              if (err) {
 +                                      free_extent_buffer(cur);
 +                                      return err;
 +                              }
                        }
                }
                if (search_start == 0)
@@@ -858,18 -1505,20 +1509,18 @@@ static noinline int generic_bin_search(
  static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
                      int level, int *slot)
  {
 -      if (level == 0) {
 +      if (level == 0)
                return generic_bin_search(eb,
                                          offsetof(struct btrfs_leaf, items),
                                          sizeof(struct btrfs_item),
                                          key, btrfs_header_nritems(eb),
                                          slot);
 -      } else {
 +      else
                return generic_bin_search(eb,
                                          offsetof(struct btrfs_node, ptrs),
                                          sizeof(struct btrfs_key_ptr),
                                          key, btrfs_header_nritems(eb),
                                          slot);
 -      }
 -      return -1;
  }
  
  int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
@@@ -976,6 -1625,7 +1627,7 @@@ static noinline int balance_level(struc
                        goto enospc;
                }
  
+               tree_mod_log_set_root_pointer(root, child);
                rcu_assign_pointer(root->node, child);
  
                add_root_to_dirty_list(root);
                free_extent_buffer(mid);
  
                root_sub_used(root, mid->len);
-               btrfs_free_tree_block(trans, root, mid, 0, 1, 0);
+               btrfs_free_tree_block(trans, root, mid, 0, 1);
                /* once for the root ptr */
                free_extent_buffer_stale(mid);
                return 0;
                if (btrfs_header_nritems(right) == 0) {
                        clean_tree_block(trans, root, right);
                        btrfs_tree_unlock(right);
-                       del_ptr(trans, root, path, level + 1, pslot + 1);
+                       del_ptr(trans, root, path, level + 1, pslot + 1, 1);
                        root_sub_used(root, right->len);
-                       btrfs_free_tree_block(trans, root, right, 0, 1, 0);
+                       btrfs_free_tree_block(trans, root, right, 0, 1);
                        free_extent_buffer_stale(right);
                        right = NULL;
                } else {
                        struct btrfs_disk_key right_key;
                        btrfs_node_key(right, &right_key, 0);
+                       tree_mod_log_set_node_key(root->fs_info, parent,
+                                                 &right_key, pslot + 1, 0);
                        btrfs_set_node_key(parent, &right_key, pslot + 1);
                        btrfs_mark_buffer_dirty(parent);
                }
        if (btrfs_header_nritems(mid) == 0) {
                clean_tree_block(trans, root, mid);
                btrfs_tree_unlock(mid);
-               del_ptr(trans, root, path, level + 1, pslot);
+               del_ptr(trans, root, path, level + 1, pslot, 1);
                root_sub_used(root, mid->len);
-               btrfs_free_tree_block(trans, root, mid, 0, 1, 0);
+               btrfs_free_tree_block(trans, root, mid, 0, 1);
                free_extent_buffer_stale(mid);
                mid = NULL;
        } else {
                /* update the parent key to reflect our changes */
                struct btrfs_disk_key mid_key;
                btrfs_node_key(mid, &mid_key, 0);
+               tree_mod_log_set_node_key(root->fs_info, parent, &mid_key,
+                                         pslot, 0);
                btrfs_set_node_key(parent, &mid_key, pslot);
                btrfs_mark_buffer_dirty(parent);
        }
@@@ -1190,6 -1844,8 +1846,8 @@@ static noinline int push_nodes_for_inse
                        struct btrfs_disk_key disk_key;
                        orig_slot += left_nr;
                        btrfs_node_key(mid, &disk_key, 0);
+                       tree_mod_log_set_node_key(root->fs_info, parent,
+                                                 &disk_key, pslot, 0);
                        btrfs_set_node_key(parent, &disk_key, pslot);
                        btrfs_mark_buffer_dirty(parent);
                        if (btrfs_header_nritems(left) > orig_slot) {
                        struct btrfs_disk_key disk_key;
  
                        btrfs_node_key(right, &disk_key, 0);
+                       tree_mod_log_set_node_key(root->fs_info, parent,
+                                                 &disk_key, pslot + 1, 0);
                        btrfs_set_node_key(parent, &disk_key, pslot + 1);
                        btrfs_mark_buffer_dirty(parent);
  
@@@ -1498,7 -2156,7 +2158,7 @@@ static in
  read_block_for_search(struct btrfs_trans_handle *trans,
                       struct btrfs_root *root, struct btrfs_path *p,
                       struct extent_buffer **eb_ret, int level, int slot,
-                      struct btrfs_key *key)
+                      struct btrfs_key *key, u64 time_seq)
  {
        u64 blocknr;
        u64 gen;
@@@ -1852,7 -2510,7 +2512,7 @@@ cow_done
                        }
  
                        err = read_block_for_search(trans, root, p,
-                                                   &b, level, slot, key);
+                                                   &b, level, slot, key, 0);
                        if (err == -EAGAIN)
                                goto again;
                        if (err) {
        return ret;
  }
  
+ /*
+  * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
+  * current state of the tree together with the operations recorded in the tree
+  * modification log to search for the key in a previous version of this tree, as
+  * denoted by the time_seq parameter.
+  *
+  * Naturally, there is no support for insert, delete or cow operations.
+  *
+  * The resulting path and return value will be set up as if we called
+  * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
+  */
+ int btrfs_search_old_slot(struct btrfs_root *root, struct btrfs_key *key,
+                         struct btrfs_path *p, u64 time_seq)
+ {
+       struct extent_buffer *b;
+       int slot;
+       int ret;
+       int err;
+       int level;
+       int lowest_unlock = 1;
+       u8 lowest_level = 0;
+       lowest_level = p->lowest_level;
+       WARN_ON(p->nodes[0] != NULL);
+       if (p->search_commit_root) {
+               BUG_ON(time_seq);
+               return btrfs_search_slot(NULL, root, key, p, 0, 0);
+       }
+ again:
+       b = get_old_root(root, time_seq);
+       extent_buffer_get(b);
+       level = btrfs_header_level(b);
+       btrfs_tree_read_lock(b);
+       p->locks[level] = BTRFS_READ_LOCK;
+       while (b) {
+               level = btrfs_header_level(b);
+               p->nodes[level] = b;
+               btrfs_clear_path_blocking(p, NULL, 0);
+               /*
+                * we have a lock on b and as long as we aren't changing
+                * the tree, there is no way to for the items in b to change.
+                * It is safe to drop the lock on our parent before we
+                * go through the expensive btree search on b.
+                */
+               btrfs_unlock_up_safe(p, level + 1);
+               ret = bin_search(b, key, level, &slot);
+               if (level != 0) {
+                       int dec = 0;
+                       if (ret && slot > 0) {
+                               dec = 1;
+                               slot -= 1;
+                       }
+                       p->slots[level] = slot;
+                       unlock_up(p, level, lowest_unlock, 0, NULL);
+                       if (level == lowest_level) {
+                               if (dec)
+                                       p->slots[level]++;
+                               goto done;
+                       }
+                       err = read_block_for_search(NULL, root, p, &b, level,
+                                                   slot, key, time_seq);
+                       if (err == -EAGAIN)
+                               goto again;
+                       if (err) {
+                               ret = err;
+                               goto done;
+                       }
+                       level = btrfs_header_level(b);
+                       err = btrfs_try_tree_read_lock(b);
+                       if (!err) {
+                               btrfs_set_path_blocking(p);
+                               btrfs_tree_read_lock(b);
+                               btrfs_clear_path_blocking(p, b,
+                                                         BTRFS_READ_LOCK);
+                       }
+                       p->locks[level] = BTRFS_READ_LOCK;
+                       p->nodes[level] = b;
+                       b = tree_mod_log_rewind(root->fs_info, b, time_seq);
+                       if (b != p->nodes[level]) {
+                               btrfs_tree_unlock_rw(p->nodes[level],
+                                                    p->locks[level]);
+                               p->locks[level] = 0;
+                               p->nodes[level] = b;
+                       }
+               } else {
+                       p->slots[level] = slot;
+                       unlock_up(p, level, lowest_unlock, 0, NULL);
+                       goto done;
+               }
+       }
+       ret = 1;
+ done:
+       if (!p->leave_spinning)
+               btrfs_set_path_blocking(p);
+       if (ret < 0)
+               btrfs_release_path(p);
+       return ret;
+ }
  /*
   * adjust the pointers going up the tree, starting at level
   * making sure the right key of each node is points to 'key'.
@@@ -1943,6 -2710,7 +2712,7 @@@ static void fixup_low_keys(struct btrfs
                if (!path->nodes[i])
                        break;
                t = path->nodes[i];
+               tree_mod_log_set_node_key(root->fs_info, t, key, tslot, 1);
                btrfs_set_node_key(t, key, tslot);
                btrfs_mark_buffer_dirty(path->nodes[i]);
                if (tslot != 0)
@@@ -2025,12 -2793,16 +2795,16 @@@ static int push_node_left(struct btrfs_
        } else
                push_items = min(src_nritems - 8, push_items);
  
+       tree_mod_log_eb_copy(root->fs_info, dst, src, dst_nritems, 0,
+                            push_items);
        copy_extent_buffer(dst, src,
                           btrfs_node_key_ptr_offset(dst_nritems),
                           btrfs_node_key_ptr_offset(0),
                           push_items * sizeof(struct btrfs_key_ptr));
  
        if (push_items < src_nritems) {
+               tree_mod_log_eb_move(root->fs_info, src, 0, push_items,
+                                    src_nritems - push_items);
                memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
                                      btrfs_node_key_ptr_offset(push_items),
                                      (src_nritems - push_items) *
@@@ -2084,11 -2856,14 +2858,14 @@@ static int balance_node_right(struct bt
        if (max_push < push_items)
                push_items = max_push;
  
+       tree_mod_log_eb_move(root->fs_info, dst, push_items, 0, dst_nritems);
        memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
                                      btrfs_node_key_ptr_offset(0),
                                      (dst_nritems) *
                                      sizeof(struct btrfs_key_ptr));
  
+       tree_mod_log_eb_copy(root->fs_info, dst, src, 0,
+                            src_nritems - push_items, push_items);
        copy_extent_buffer(dst, src,
                           btrfs_node_key_ptr_offset(0),
                           btrfs_node_key_ptr_offset(src_nritems - push_items),
@@@ -2131,7 -2906,7 +2908,7 @@@ static noinline int insert_new_root(str
  
        c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
                                   root->root_key.objectid, &lower_key,
-                                  level, root->node->start, 0, 0);
+                                  level, root->node->start, 0);
        if (IS_ERR(c))
                return PTR_ERR(c);
  
        btrfs_mark_buffer_dirty(c);
  
        old = root->node;
+       tree_mod_log_set_root_pointer(root, c);
        rcu_assign_pointer(root->node, c);
  
        /* the super has an extra ref to root->node */
  static void insert_ptr(struct btrfs_trans_handle *trans,
                       struct btrfs_root *root, struct btrfs_path *path,
                       struct btrfs_disk_key *key, u64 bytenr,
-                      int slot, int level)
+                      int slot, int level, int tree_mod_log)
  {
        struct extent_buffer *lower;
        int nritems;
+       int ret;
  
        BUG_ON(!path->nodes[level]);
        btrfs_assert_tree_locked(path->nodes[level]);
        BUG_ON(slot > nritems);
        BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
        if (slot != nritems) {
+               if (tree_mod_log && level)
+                       tree_mod_log_eb_move(root->fs_info, lower, slot + 1,
+                                            slot, nritems - slot);
                memmove_extent_buffer(lower,
                              btrfs_node_key_ptr_offset(slot + 1),
                              btrfs_node_key_ptr_offset(slot),
                              (nritems - slot) * sizeof(struct btrfs_key_ptr));
        }
+       if (tree_mod_log && level) {
+               ret = tree_mod_log_insert_key(root->fs_info, lower, slot,
+                                             MOD_LOG_KEY_ADD);
+               BUG_ON(ret < 0);
+       }
        btrfs_set_node_key(lower, key, slot);
        btrfs_set_node_blockptr(lower, slot, bytenr);
        WARN_ON(trans->transid == 0);
@@@ -2254,7 -3039,7 +3041,7 @@@ static noinline int split_node(struct b
  
        split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
                                        root->root_key.objectid,
-                                       &disk_key, level, c->start, 0, 0);
+                                       &disk_key, level, c->start, 0);
        if (IS_ERR(split))
                return PTR_ERR(split);
  
                            (unsigned long)btrfs_header_chunk_tree_uuid(split),
                            BTRFS_UUID_SIZE);
  
+       tree_mod_log_eb_copy(root->fs_info, split, c, 0, mid, c_nritems - mid);
        copy_extent_buffer(split, c,
                           btrfs_node_key_ptr_offset(0),
                           btrfs_node_key_ptr_offset(mid),
        btrfs_mark_buffer_dirty(split);
  
        insert_ptr(trans, root, path, &disk_key, split->start,
-                  path->slots[level + 1] + 1, level + 1);
+                  path->slots[level + 1] + 1, level + 1, 1);
  
        if (path->slots[level] >= mid) {
                path->slots[level] -= mid;
@@@ -2823,7 -3608,7 +3610,7 @@@ static noinline void copy_for_split(str
        btrfs_set_header_nritems(l, mid);
        btrfs_item_key(right, &disk_key, 0);
        insert_ptr(trans, root, path, &disk_key, right->start,
-                  path->slots[1] + 1, 1);
+                  path->slots[1] + 1, 1, 0);
  
        btrfs_mark_buffer_dirty(right);
        btrfs_mark_buffer_dirty(l);
@@@ -3006,7 -3791,7 +3793,7 @@@ again
  
        right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
                                        root->root_key.objectid,
-                                       &disk_key, 0, l->start, 0, 0);
+                                       &disk_key, 0, l->start, 0);
        if (IS_ERR(right))
                return PTR_ERR(right);
  
                if (mid <= slot) {
                        btrfs_set_header_nritems(right, 0);
                        insert_ptr(trans, root, path, &disk_key, right->start,
-                                  path->slots[1] + 1, 1);
+                                  path->slots[1] + 1, 1, 0);
                        btrfs_tree_unlock(path->nodes[0]);
                        free_extent_buffer(path->nodes[0]);
                        path->nodes[0] = right;
                } else {
                        btrfs_set_header_nritems(right, 0);
                        insert_ptr(trans, root, path, &disk_key, right->start,
-                                         path->slots[1], 1);
+                                         path->slots[1], 1, 0);
                        btrfs_tree_unlock(path->nodes[0]);
                        free_extent_buffer(path->nodes[0]);
                        path->nodes[0] = right;
@@@ -3751,19 -4536,29 +4538,29 @@@ int btrfs_insert_item(struct btrfs_tran
   * empty a node.
   */
  static void del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
-                   struct btrfs_path *path, int level, int slot)
+                   struct btrfs_path *path, int level, int slot,
+                   int tree_mod_log)
  {
        struct extent_buffer *parent = path->nodes[level];
        u32 nritems;
+       int ret;
  
        nritems = btrfs_header_nritems(parent);
        if (slot != nritems - 1) {
+               if (tree_mod_log && level)
+                       tree_mod_log_eb_move(root->fs_info, parent, slot,
+                                            slot + 1, nritems - slot - 1);
                memmove_extent_buffer(parent,
                              btrfs_node_key_ptr_offset(slot),
                              btrfs_node_key_ptr_offset(slot + 1),
                              sizeof(struct btrfs_key_ptr) *
                              (nritems - slot - 1));
+       } else if (tree_mod_log && level) {
+               ret = tree_mod_log_insert_key(root->fs_info, parent, slot,
+                                             MOD_LOG_KEY_REMOVE);
+               BUG_ON(ret < 0);
        }
        nritems--;
        btrfs_set_header_nritems(parent, nritems);
        if (nritems == 0 && parent == root->node) {
@@@ -3795,7 -4590,7 +4592,7 @@@ static noinline void btrfs_del_leaf(str
                                    struct extent_buffer *leaf)
  {
        WARN_ON(btrfs_header_generation(leaf) != trans->transid);
-       del_ptr(trans, root, path, 1, path->slots[1]);
+       del_ptr(trans, root, path, 1, path->slots[1], 1);
  
        /*
         * btrfs_free_extent is expensive, we want to make sure we
        root_sub_used(root, leaf->len);
  
        extent_buffer_get(leaf);
-       btrfs_free_tree_block(trans, root, leaf, 0, 1, 0);
+       btrfs_free_tree_block(trans, root, leaf, 0, 1);
        free_extent_buffer_stale(leaf);
  }
  /*
@@@ -4273,7 -5068,7 +5070,7 @@@ again
                next = c;
                next_rw_lock = path->locks[level];
                ret = read_block_for_search(NULL, root, path, &next, level,
-                                           slot, &key);
+                                           slot, &key, 0);
                if (ret == -EAGAIN)
                        goto again;
  
                        break;
  
                ret = read_block_for_search(NULL, root, path, &next, level,
-                                           0, &key);
+                                           0, &key, 0);
                if (ret == -EAGAIN)
                        goto again;
  
diff --combined fs/btrfs/ctree.h
index 1c665ebe47e052565b66042433564df977f720bd,f5f11a6c5e9203a089b05123fbed0562bbedd230..0151ca1ac65715f38cd7ca49609effa879ea7d81
@@@ -173,9 -173,6 +173,9 @@@ static int btrfs_csum_sizes[] = { 4, 0 
  #define BTRFS_FT_XATTR                8
  #define BTRFS_FT_MAX          9
  
 +/* ioprio of readahead is set to idle */
 +#define BTRFS_IOPRIO_READA (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0))
 +
  /*
   * The key defines the order in the tree, and so it also defines (optimal)
   * block layout.
@@@ -826,14 -823,6 +826,14 @@@ struct btrfs_csum_item 
        u8 csum;
  } __attribute__ ((__packed__));
  
 +struct btrfs_dev_stats_item {
 +      /*
 +       * grow this item struct at the end for future enhancements and keep
 +       * the existing values unchanged
 +       */
 +      __le64 values[BTRFS_DEV_STAT_VALUES_MAX];
 +} __attribute__ ((__packed__));
 +
  /* different types of block groups (and chunks) */
  #define BTRFS_BLOCK_GROUP_DATA                (1ULL << 0)
  #define BTRFS_BLOCK_GROUP_SYSTEM      (1ULL << 1)
@@@ -1140,6 -1129,15 +1140,15 @@@ struct btrfs_fs_info 
        spinlock_t delayed_iput_lock;
        struct list_head delayed_iputs;
  
+       /* this protects tree_mod_seq_list */
+       spinlock_t tree_mod_seq_lock;
+       atomic_t tree_mod_seq;
+       struct list_head tree_mod_seq_list;
+       /* this protects tree_mod_log */
+       rwlock_t tree_mod_log_lock;
+       struct rb_root tree_mod_log;
        atomic_t nr_async_submits;
        atomic_t async_submit_draining;
        atomic_t nr_async_bios;
@@@ -1386,7 -1384,7 +1395,7 @@@ struct btrfs_root 
        struct list_head root_list;
  
        spinlock_t orphan_lock;
 -      struct list_head orphan_list;
 +      atomic_t orphan_inodes;
        struct btrfs_block_rsv *orphan_block_rsv;
        int orphan_item_inserted;
        int orphan_cleanup_state;
@@@ -1518,12 -1516,6 +1527,12 @@@ struct btrfs_ioctl_defrag_range_args 
  
  #define BTRFS_BALANCE_ITEM_KEY        248
  
 +/*
 + * Persistantly stores the io stats in the device tree.
 + * One key for all stats, (0, BTRFS_DEV_STATS_KEY, devid).
 + */
 +#define BTRFS_DEV_STATS_KEY   249
 +
  /*
   * string items are for debugging.  They just store a short string of
   * data in the FS
@@@ -2183,7 -2175,7 +2192,7 @@@ BTRFS_SETGET_STACK_FUNCS(root_last_snap
  
  static inline bool btrfs_root_readonly(struct btrfs_root *root)
  {
 -      return root->root_item.flags & BTRFS_ROOT_SUBVOL_RDONLY;
 +      return (root->root_item.flags & cpu_to_le64(BTRFS_ROOT_SUBVOL_RDONLY)) != 0;
  }
  
  /* struct btrfs_root_backup */
@@@ -2432,30 -2424,6 +2441,30 @@@ static inline u32 btrfs_file_extent_inl
        return btrfs_item_size(eb, e) - offset;
  }
  
 +/* btrfs_dev_stats_item */
 +static inline u64 btrfs_dev_stats_value(struct extent_buffer *eb,
 +                                      struct btrfs_dev_stats_item *ptr,
 +                                      int index)
 +{
 +      u64 val;
 +
 +      read_extent_buffer(eb, &val,
 +                         offsetof(struct btrfs_dev_stats_item, values) +
 +                          ((unsigned long)ptr) + (index * sizeof(u64)),
 +                         sizeof(val));
 +      return val;
 +}
 +
 +static inline void btrfs_set_dev_stats_value(struct extent_buffer *eb,
 +                                           struct btrfs_dev_stats_item *ptr,
 +                                           int index, u64 val)
 +{
 +      write_extent_buffer(eb, &val,
 +                          offsetof(struct btrfs_dev_stats_item, values) +
 +                           ((unsigned long)ptr) + (index * sizeof(u64)),
 +                          sizeof(val));
 +}
 +
  static inline struct btrfs_fs_info *btrfs_sb(struct super_block *sb)
  {
        return sb->s_fs_info;
@@@ -2537,11 -2505,11 +2546,11 @@@ struct extent_buffer *btrfs_alloc_free_
                                        struct btrfs_root *root, u32 blocksize,
                                        u64 parent, u64 root_objectid,
                                        struct btrfs_disk_key *key, int level,
-                                       u64 hint, u64 empty_size, int for_cow);
+                                       u64 hint, u64 empty_size);
  void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
                           struct btrfs_root *root,
                           struct extent_buffer *buf,
-                          u64 parent, int last_ref, int for_cow);
+                          u64 parent, int last_ref);
  struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
                                            struct btrfs_root *root,
                                            u64 bytenr, u32 blocksize,
@@@ -2700,6 -2668,8 +2709,8 @@@ int btrfs_duplicate_item(struct btrfs_t
  int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
                      *root, struct btrfs_key *key, struct btrfs_path *p, int
                      ins_len, int cow);
+ int btrfs_search_old_slot(struct btrfs_root *root, struct btrfs_key *key,
+                         struct btrfs_path *p, u64 time_seq);
  int btrfs_realloc_node(struct btrfs_trans_handle *trans,
                       struct btrfs_root *root, struct extent_buffer *parent,
                       int start_slot, int cache_only, u64 *last_ret,
@@@ -3139,4 -3109,23 +3150,23 @@@ void btrfs_reada_detach(void *handle)
  int btree_readahead_hook(struct btrfs_root *root, struct extent_buffer *eb,
                         u64 start, int err);
  
+ /* delayed seq elem */
+ struct seq_list {
+       struct list_head list;
+       u64 seq;
+       u32 flags;
+ };
+ void btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info,
+                           struct seq_list *elem);
+ void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
+                           struct seq_list *elem);
+ static inline int is_fstree(u64 rootid)
+ {
+       if (rootid == BTRFS_FS_TREE_OBJECTID ||
+           (s64)rootid >= (s64)BTRFS_FIRST_FREE_OBJECTID)
+               return 1;
+       return 0;
+ }
  #endif
diff --combined fs/btrfs/disk-io.c
index b0d49e21b0b10a0b9f90440512f22aa529f1120b,f51ad8477f18842a4cd0c1f6ef7257391059300b..b99d5127ba18997a6aab35f8946711ae0b3ff5b4
@@@ -1153,6 -1153,7 +1153,6 @@@ static void __setup_root(u32 nodesize, 
        root->orphan_block_rsv = NULL;
  
        INIT_LIST_HEAD(&root->dirty_list);
 -      INIT_LIST_HEAD(&root->orphan_list);
        INIT_LIST_HEAD(&root->root_list);
        spin_lock_init(&root->orphan_lock);
        spin_lock_init(&root->inode_lock);
        atomic_set(&root->log_commit[0], 0);
        atomic_set(&root->log_commit[1], 0);
        atomic_set(&root->log_writers, 0);
 +      atomic_set(&root->orphan_inodes, 0);
        root->log_batch = 0;
        root->log_transid = 0;
        root->last_log_commit = 0;
@@@ -1252,7 -1252,7 +1252,7 @@@ static struct btrfs_root *alloc_log_tre
  
        leaf = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
                                      BTRFS_TREE_LOG_OBJECTID, NULL,
-                                     0, 0, 0, 0);
+                                     0, 0, 0);
        if (IS_ERR(leaf)) {
                kfree(root);
                return ERR_CAST(leaf);
@@@ -1914,11 -1914,14 +1914,14 @@@ int open_ctree(struct super_block *sb
        spin_lock_init(&fs_info->delayed_iput_lock);
        spin_lock_init(&fs_info->defrag_inodes_lock);
        spin_lock_init(&fs_info->free_chunk_lock);
+       spin_lock_init(&fs_info->tree_mod_seq_lock);
+       rwlock_init(&fs_info->tree_mod_log_lock);
        mutex_init(&fs_info->reloc_mutex);
  
        init_completion(&fs_info->kobj_unregister);
        INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
        INIT_LIST_HEAD(&fs_info->space_info);
+       INIT_LIST_HEAD(&fs_info->tree_mod_seq_list);
        btrfs_mapping_init(&fs_info->mapping_tree);
        btrfs_init_block_rsv(&fs_info->global_block_rsv);
        btrfs_init_block_rsv(&fs_info->delalloc_block_rsv);
        atomic_set(&fs_info->async_submit_draining, 0);
        atomic_set(&fs_info->nr_async_bios, 0);
        atomic_set(&fs_info->defrag_running, 0);
+       atomic_set(&fs_info->tree_mod_seq, 0);
        fs_info->sb = sb;
        fs_info->max_inline = 8192 * 1024;
        fs_info->metadata_ratio = 0;
        fs_info->defrag_inodes = RB_ROOT;
        fs_info->trans_no_join = 0;
        fs_info->free_chunk_space = 0;
+       fs_info->tree_mod_log = RB_ROOT;
  
        /* readahead state */
        INIT_RADIX_TREE(&fs_info->reada_tree, GFP_NOFS & ~__GFP_WAIT);
        BTRFS_I(fs_info->btree_inode)->root = tree_root;
        memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
               sizeof(struct btrfs_key));
 -      BTRFS_I(fs_info->btree_inode)->dummy_inode = 1;
 +      set_bit(BTRFS_INODE_DUMMY,
 +              &BTRFS_I(fs_info->btree_inode)->runtime_flags);
        insert_inode_hash(fs_info->btree_inode);
  
        spin_lock_init(&fs_info->block_group_cache_lock);
@@@ -2354,13 -2358,6 +2359,13 @@@ retry_root_backup
        fs_info->generation = generation;
        fs_info->last_trans_committed = generation;
  
 +      ret = btrfs_init_dev_stats(fs_info);
 +      if (ret) {
 +              printk(KERN_ERR "btrfs: failed to init dev_stats: %d\n",
 +                     ret);
 +              goto fail_block_groups;
 +      }
 +
        ret = btrfs_init_space_info(fs_info);
        if (ret) {
                printk(KERN_ERR "Failed to initial space info: %d\n", ret);
@@@ -2564,19 -2561,18 +2569,19 @@@ recovery_tree_root
  
  static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate)
  {
 -      char b[BDEVNAME_SIZE];
 -
        if (uptodate) {
                set_buffer_uptodate(bh);
        } else {
 +              struct btrfs_device *device = (struct btrfs_device *)
 +                      bh->b_private;
 +
                printk_ratelimited(KERN_WARNING "lost page write due to "
 -                                      "I/O error on %s\n",
 -                                     bdevname(bh->b_bdev, b));
 +                                 "I/O error on %s\n", device->name);
                /* note, we dont' set_buffer_write_io_error because we have
                 * our own ways of dealing with the IO errors
                 */
                clear_buffer_uptodate(bh);
 +              btrfs_dev_stat_inc_and_print(device, BTRFS_DEV_STAT_WRITE_ERRS);
        }
        unlock_buffer(bh);
        put_bh(bh);
@@@ -2691,7 -2687,6 +2696,7 @@@ static int write_dev_supers(struct btrf
                        set_buffer_uptodate(bh);
                        lock_buffer(bh);
                        bh->b_end_io = btrfs_end_buffer_write_sync;
 +                      bh->b_private = device;
                }
  
                /*
@@@ -2750,9 -2745,6 +2755,9 @@@ static int write_dev_flush(struct btrfs
                }
                if (!bio_flagged(bio, BIO_UPTODATE)) {
                        ret = -EIO;
 +                      if (!bio_flagged(bio, BIO_EOPNOTSUPP))
 +                              btrfs_dev_stat_inc_and_print(device,
 +                                      BTRFS_DEV_STAT_FLUSH_ERRS);
                }
  
                /* drop the reference from the wait == 0 run */
@@@ -2915,6 -2907,19 +2920,6 @@@ int write_ctree_super(struct btrfs_tran
        return ret;
  }
  
 -/* Kill all outstanding I/O */
 -void btrfs_abort_devices(struct btrfs_root *root)
 -{
 -      struct list_head *head;
 -      struct btrfs_device *dev;
 -      mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
 -      head = &root->fs_info->fs_devices->devices;
 -      list_for_each_entry_rcu(dev, head, dev_list) {
 -              blk_abort_queue(dev->bdev->bd_disk->queue);
 -      }
 -      mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
 -}
 -
  void btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
  {
        spin_lock(&fs_info->fs_roots_radix_lock);
@@@ -3671,6 -3676,17 +3676,6 @@@ int btrfs_cleanup_transaction(struct bt
        return 0;
  }
  
 -static int btree_writepage_io_failed_hook(struct bio *bio, struct page *page,
 -                                        u64 start, u64 end,
 -                                        struct extent_state *state)
 -{
 -      struct super_block *sb = page->mapping->host->i_sb;
 -      struct btrfs_fs_info *fs_info = btrfs_sb(sb);
 -      btrfs_error(fs_info, -EIO,
 -                  "Error occured while writing out btree at %llu", start);
 -      return -EIO;
 -}
 -
  static struct extent_io_ops btree_extent_io_ops = {
        .write_cache_pages_lock_hook = btree_lock_page_hook,
        .readpage_end_io_hook = btree_readpage_end_io_hook,
        .submit_bio_hook = btree_submit_bio_hook,
        /* note we're sharing with inode.c for the merge bio hook */
        .merge_bio_hook = btrfs_merge_bio_hook,
 -      .writepage_io_failed_hook = btree_writepage_io_failed_hook,
  };
diff --combined fs/btrfs/extent-tree.c
index 1902726fa70a08d39535bc6c1c919f5ed4a58dd3,b68eb7ad05a71e4815a4ed493fef832e2dd2fa97..4b5a1e1bdefbe095c239b464e55c9699e865175b
@@@ -3578,7 -3578,7 +3578,7 @@@ again
        space_info->chunk_alloc = 0;
        spin_unlock(&space_info->lock);
  out:
 -      mutex_unlock(&extent_root->fs_info->chunk_mutex);
 +      mutex_unlock(&fs_info->chunk_mutex);
        return ret;
  }
  
@@@ -4355,9 -4355,10 +4355,9 @@@ static unsigned drop_outstanding_extent
        BTRFS_I(inode)->outstanding_extents--;
  
        if (BTRFS_I(inode)->outstanding_extents == 0 &&
 -          BTRFS_I(inode)->delalloc_meta_reserved) {
 +          test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
 +                             &BTRFS_I(inode)->runtime_flags))
                drop_inode_space = 1;
 -              BTRFS_I(inode)->delalloc_meta_reserved = 0;
 -      }
  
        /*
         * If we have more or the same amount of outsanding extents than we have
@@@ -4464,8 -4465,7 +4464,8 @@@ int btrfs_delalloc_reserve_metadata(str
         * Add an item to reserve for updating the inode when we complete the
         * delalloc io.
         */
 -      if (!BTRFS_I(inode)->delalloc_meta_reserved) {
 +      if (!test_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
 +                    &BTRFS_I(inode)->runtime_flags)) {
                nr_extents++;
                extra_reserve = 1;
        }
  
        spin_lock(&BTRFS_I(inode)->lock);
        if (extra_reserve) {
 -              BTRFS_I(inode)->delalloc_meta_reserved = 1;
 +              set_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
 +                      &BTRFS_I(inode)->runtime_flags);
                nr_extents--;
        }
        BTRFS_I(inode)->reserved_extents += nr_extents;
@@@ -5218,7 -5217,7 +5218,7 @@@ out
  void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
                           struct btrfs_root *root,
                           struct extent_buffer *buf,
-                          u64 parent, int last_ref, int for_cow)
+                          u64 parent, int last_ref)
  {
        struct btrfs_block_group_cache *cache = NULL;
        int ret;
                                        buf->start, buf->len,
                                        parent, root->root_key.objectid,
                                        btrfs_header_level(buf),
-                                       BTRFS_DROP_DELAYED_REF, NULL, for_cow);
+                                       BTRFS_DROP_DELAYED_REF, NULL, 0);
                BUG_ON(ret); /* -ENOMEM */
        }
  
@@@ -6250,7 -6249,7 +6250,7 @@@ struct extent_buffer *btrfs_alloc_free_
                                        struct btrfs_root *root, u32 blocksize,
                                        u64 parent, u64 root_objectid,
                                        struct btrfs_disk_key *key, int level,
-                                       u64 hint, u64 empty_size, int for_cow)
+                                       u64 hint, u64 empty_size)
  {
        struct btrfs_key ins;
        struct btrfs_block_rsv *block_rsv;
                                        ins.objectid,
                                        ins.offset, parent, root_objectid,
                                        level, BTRFS_ADD_DELAYED_EXTENT,
-                                       extent_op, for_cow);
+                                       extent_op, 0);
                BUG_ON(ret); /* -ENOMEM */
        }
        return buf;
@@@ -6716,7 -6715,7 +6716,7 @@@ static noinline int walk_up_proc(struc
                               btrfs_header_owner(path->nodes[level + 1]));
        }
  
-       btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1, 0);
+       btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
  out:
        wc->refs[level] = 0;
        wc->flags[level] = 0;
diff --combined fs/btrfs/extent_io.c
index b3692c1373aa5e803882dbb8f54163865040cdeb,3daed70a401abe7d5cc8b0bdaee4a976a2a2760a..2c8f7b2046173954f720125a6e53e96de3c7727e
@@@ -186,6 -186,7 +186,6 @@@ static struct rb_node *tree_insert(stru
                        return parent;
        }
  
 -      entry = rb_entry(node, struct tree_entry, rb_node);
        rb_link_node(node, parent, p);
        rb_insert_color(node, root);
        return NULL;
@@@ -412,7 -413,7 +412,7 @@@ static struct extent_state *next_state(
  
  /*
   * utility function to clear some bits in an extent state struct.
 - * it will optionally wake up any one waiting on this state (wake == 1)
 + * it will optionally wake up any one waiting on this state (wake == 1).
   *
   * If no bits are set on the state struct after clearing things, the
   * struct is freed and removed from the tree
@@@ -569,8 -570,10 +569,8 @@@ hit_next
                if (err)
                        goto out;
                if (state->end <= end) {
 -                      clear_state_bit(tree, state, &bits, wake);
 -                      if (last_end == (u64)-1)
 -                              goto out;
 -                      start = last_end + 1;
 +                      state = clear_state_bit(tree, state, &bits, wake);
 +                      goto next;
                }
                goto search_again;
        }
@@@ -778,6 -781,7 +778,6 @@@ hit_next
         * Just lock what we found and keep going
         */
        if (state->start == start && state->end <= end) {
 -              struct rb_node *next_node;
                if (state->state & exclusive_bits) {
                        *failed_start = state->start;
                        err = -EEXIST;
                }
  
                set_state_bits(tree, state, &bits);
 -
                cache_state(state, cached_state);
                merge_state(tree, state);
                if (last_end == (u64)-1)
                        goto out;
 -
                start = last_end + 1;
 -              next_node = rb_next(&state->rb_node);
 -              if (next_node && start < end && prealloc && !need_resched()) {
 -                      state = rb_entry(next_node, struct extent_state,
 -                                       rb_node);
 -                      if (state->start == start)
 -                              goto hit_next;
 -              }
 +              state = next_state(state);
 +              if (start < end && state && state->start == start &&
 +                  !need_resched())
 +                      goto hit_next;
                goto search_again;
        }
  
                        if (last_end == (u64)-1)
                                goto out;
                        start = last_end + 1;
 +                      state = next_state(state);
 +                      if (start < end && state && state->start == start &&
 +                          !need_resched())
 +                              goto hit_next;
                }
                goto search_again;
        }
@@@ -989,14 -994,21 +989,14 @@@ hit_next
         * Just lock what we found and keep going
         */
        if (state->start == start && state->end <= end) {
 -              struct rb_node *next_node;
 -
                set_state_bits(tree, state, &bits);
 -              clear_state_bit(tree, state, &clear_bits, 0);
 +              state = clear_state_bit(tree, state, &clear_bits, 0);
                if (last_end == (u64)-1)
                        goto out;
 -
                start = last_end + 1;
 -              next_node = rb_next(&state->rb_node);
 -              if (next_node && start < end && prealloc && !need_resched()) {
 -                      state = rb_entry(next_node, struct extent_state,
 -                                       rb_node);
 -                      if (state->start == start)
 -                              goto hit_next;
 -              }
 +              if (start < end && state && state->start == start &&
 +                  !need_resched())
 +                      goto hit_next;
                goto search_again;
        }
  
                        goto out;
                if (state->end <= end) {
                        set_state_bits(tree, state, &bits);
 -                      clear_state_bit(tree, state, &clear_bits, 0);
 +                      state = clear_state_bit(tree, state, &clear_bits, 0);
                        if (last_end == (u64)-1)
                                goto out;
                        start = last_end + 1;
 +                      if (start < end && state && state->start == start &&
 +                          !need_resched())
 +                              goto hit_next;
                }
                goto search_again;
        }
@@@ -1164,8 -1173,9 +1164,8 @@@ int set_extent_uptodate(struct extent_i
                              cached_state, mask);
  }
  
 -static int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
 -                               u64 end, struct extent_state **cached_state,
 -                               gfp_t mask)
 +int clear_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
 +                        struct extent_state **cached_state, gfp_t mask)
  {
        return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
                                cached_state, mask);
@@@ -1283,7 -1293,7 +1283,7 @@@ out
   * returned if we find something, and *start_ret and *end_ret are
   * set to reflect the state struct that was found.
   *
 - * If nothing was found, 1 is returned, < 0 on error
 + * If nothing was found, 1 is returned. If found something, return 0.
   */
  int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
                          u64 *start_ret, u64 *end_ret, int bits)
@@@ -1913,7 -1923,6 +1913,7 @@@ int repair_io_failure(struct btrfs_mapp
        if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
                /* try to remap that extent elsewhere? */
                bio_put(bio);
 +              btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
                return -EIO;
        }
  
@@@ -2213,7 -2222,17 +2213,7 @@@ int end_extent_writepage(struct page *p
                        uptodate = 0;
        }
  
 -      if (!uptodate && tree->ops &&
 -          tree->ops->writepage_io_failed_hook) {
 -              ret = tree->ops->writepage_io_failed_hook(NULL, page,
 -                                               start, end, NULL);
 -              /* Writeback already completed */
 -              if (ret == 0)
 -                      return 1;
 -      }
 -
        if (!uptodate) {
 -              clear_extent_uptodate(tree, start, end, NULL, GFP_NOFS);
                ClearPageUptodate(page);
                SetPageError(page);
        }
@@@ -2328,23 -2347,10 +2328,23 @@@ static void end_bio_extent_readpage(str
                if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
                        ret = tree->ops->readpage_end_io_hook(page, start, end,
                                                              state, mirror);
 -                      if (ret)
 +                      if (ret) {
 +                              /* no IO indicated but software detected errors
 +                               * in the block, either checksum errors or
 +                               * issues with the contents */
 +                              struct btrfs_root *root =
 +                                      BTRFS_I(page->mapping->host)->root;
 +                              struct btrfs_device *device;
 +
                                uptodate = 0;
 -                      else
 +                              device = btrfs_find_device_for_logical(
 +                                              root, start, mirror);
 +                              if (device)
 +                                      btrfs_dev_stat_inc_and_print(device,
 +                                              BTRFS_DEV_STAT_CORRUPTION_ERRS);
 +                      } else {
                                clean_io_failure(start, page);
 +                      }
                }
  
                if (!uptodate && tree->ops && tree->ops->readpage_io_failed_hook) {
@@@ -2606,10 -2612,10 +2606,10 @@@ static int __extent_read_full_page(stru
  
                if (zero_offset) {
                        iosize = PAGE_CACHE_SIZE - zero_offset;
 -                      userpage = kmap_atomic(page, KM_USER0);
 +                      userpage = kmap_atomic(page);
                        memset(userpage + zero_offset, 0, iosize);
                        flush_dcache_page(page);
 -                      kunmap_atomic(userpage, KM_USER0);
 +                      kunmap_atomic(userpage);
                }
        }
        while (cur <= end) {
                        struct extent_state *cached = NULL;
  
                        iosize = PAGE_CACHE_SIZE - pg_offset;
 -                      userpage = kmap_atomic(page, KM_USER0);
 +                      userpage = kmap_atomic(page);
                        memset(userpage + pg_offset, 0, iosize);
                        flush_dcache_page(page);
 -                      kunmap_atomic(userpage, KM_USER0);
 +                      kunmap_atomic(userpage);
                        set_extent_uptodate(tree, cur, cur + iosize - 1,
                                            &cached, GFP_NOFS);
                        unlock_extent_cached(tree, cur, cur + iosize - 1,
                        char *userpage;
                        struct extent_state *cached = NULL;
  
 -                      userpage = kmap_atomic(page, KM_USER0);
 +                      userpage = kmap_atomic(page);
                        memset(userpage + pg_offset, 0, iosize);
                        flush_dcache_page(page);
 -                      kunmap_atomic(userpage, KM_USER0);
 +                      kunmap_atomic(userpage);
  
                        set_extent_uptodate(tree, cur, cur + iosize - 1,
                                            &cached, GFP_NOFS);
@@@ -2817,10 -2823,10 +2817,10 @@@ static int __extent_writepage(struct pa
        if (page->index == end_index) {
                char *userpage;
  
 -              userpage = kmap_atomic(page, KM_USER0);
 +              userpage = kmap_atomic(page);
                memset(userpage + pg_offset, 0,
                       PAGE_CACHE_SIZE - pg_offset);
 -              kunmap_atomic(userpage, KM_USER0);
 +              kunmap_atomic(userpage);
                flush_dcache_page(page);
        }
        pg_offset = 0;
@@@ -3158,7 -3164,7 +3158,7 @@@ static int write_one_eb(struct extent_b
        u64 offset = eb->start;
        unsigned long i, num_pages;
        int rw = (epd->sync_io ? WRITE_SYNC : WRITE);
 -      int ret;
 +      int ret = 0;
  
        clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
        num_pages = num_extent_pages(eb->start, eb->len);
@@@ -3924,6 -3930,7 +3924,7 @@@ static struct extent_buffer *__alloc_ex
        eb->start = start;
        eb->len = len;
        eb->tree = tree;
+       eb->bflags = 0;
        rwlock_init(&eb->lock);
        atomic_set(&eb->write_locks, 0);
        atomic_set(&eb->read_locks, 0);
        return eb;
  }
  
+ struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
+ {
+       unsigned long i;
+       struct page *p;
+       struct extent_buffer *new;
+       unsigned long num_pages = num_extent_pages(src->start, src->len);
+       new = __alloc_extent_buffer(NULL, src->start, src->len, GFP_ATOMIC);
+       if (new == NULL)
+               return NULL;
+       for (i = 0; i < num_pages; i++) {
+               p = alloc_page(GFP_ATOMIC);
+               BUG_ON(!p);
+               attach_extent_buffer_page(new, p);
+               WARN_ON(PageDirty(p));
+               SetPageUptodate(p);
+               new->pages[i] = p;
+       }
+       copy_extent_buffer(new, src, 0, 0, src->len);
+       set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
+       set_bit(EXTENT_BUFFER_DUMMY, &new->bflags);
+       return new;
+ }
+ struct extent_buffer *alloc_dummy_extent_buffer(u64 start, unsigned long len)
+ {
+       struct extent_buffer *eb;
+       unsigned long num_pages = num_extent_pages(0, len);
+       unsigned long i;
+       eb = __alloc_extent_buffer(NULL, start, len, GFP_ATOMIC);
+       if (!eb)
+               return NULL;
+       for (i = 0; i < num_pages; i++) {
+               eb->pages[i] = alloc_page(GFP_ATOMIC);
+               if (!eb->pages[i])
+                       goto err;
+       }
+       set_extent_buffer_uptodate(eb);
+       btrfs_set_header_nritems(eb, 0);
+       set_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
+       return eb;
+ err:
+       for (i--; i > 0; i--)
+               __free_page(eb->pages[i]);
+       __free_extent_buffer(eb);
+       return NULL;
+ }
  static int extent_buffer_under_io(struct extent_buffer *eb)
  {
        return (atomic_read(&eb->io_pages) ||
@@@ -3975,20 -4036,19 +4030,21 @@@ static void btrfs_release_extent_buffer
                                                unsigned long start_idx)
  {
        unsigned long index;
 +      unsigned long num_pages;
        struct page *page;
+       int mapped = !test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
  
        BUG_ON(extent_buffer_under_io(eb));
  
 -      index = num_extent_pages(eb->start, eb->len);
 +      num_pages = num_extent_pages(eb->start, eb->len);
 +      index = start_idx + num_pages;
        if (start_idx >= index)
                return;
  
        do {
                index--;
                page = extent_buffer_page(eb, index);
-               if (page) {
+               if (page && mapped) {
                        spin_lock(&page->mapping->private_lock);
                        /*
                         * We do this since we'll remove the pages after we've
                        }
                        spin_unlock(&page->mapping->private_lock);
  
+               }
+               if (page) {
                        /* One for when we alloced the page */
                        page_cache_release(page);
                }
@@@ -4231,14 -4293,18 +4289,18 @@@ static void release_extent_buffer(struc
  {
        WARN_ON(atomic_read(&eb->refs) == 0);
        if (atomic_dec_and_test(&eb->refs)) {
-               struct extent_io_tree *tree = eb->tree;
+               if (test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags)) {
+                       spin_unlock(&eb->refs_lock);
+               } else {
+                       struct extent_io_tree *tree = eb->tree;
  
-               spin_unlock(&eb->refs_lock);
+                       spin_unlock(&eb->refs_lock);
  
-               spin_lock(&tree->buffer_lock);
-               radix_tree_delete(&tree->buffer,
-                                 eb->start >> PAGE_CACHE_SHIFT);
-               spin_unlock(&tree->buffer_lock);
+                       spin_lock(&tree->buffer_lock);
+                       radix_tree_delete(&tree->buffer,
+                                         eb->start >> PAGE_CACHE_SHIFT);
+                       spin_unlock(&tree->buffer_lock);
+               }
  
                /* Should be safe to release our pages at this point */
                btrfs_release_extent_buffer_page(eb, 0);
@@@ -4255,6 -4321,10 +4317,10 @@@ void free_extent_buffer(struct extent_b
                return;
  
        spin_lock(&eb->refs_lock);
+       if (atomic_read(&eb->refs) == 2 &&
+           test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))
+               atomic_dec(&eb->refs);
        if (atomic_read(&eb->refs) == 2 &&
            test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
            !extent_buffer_under_io(eb) &&
diff --combined fs/btrfs/extent_io.h
index 4d8124b645777295f98781a73fd6ffc575623d99,96434a61d7c2a41e1aa580c8a3dccf7f923f7c58..25900af5b15d43e6bdfe0cef7c865ac2aa81bd36
@@@ -39,6 -39,7 +39,7 @@@
  #define EXTENT_BUFFER_STALE 6
  #define EXTENT_BUFFER_WRITEBACK 7
  #define EXTENT_BUFFER_IOERR 8
+ #define EXTENT_BUFFER_DUMMY 9
  
  /* these are flags for extent_clear_unlock_delalloc */
  #define EXTENT_CLEAR_UNLOCK_PAGE 0x1
@@@ -75,6 -76,9 +76,6 @@@ struct extent_io_ops 
                              unsigned long bio_flags);
        int (*readpage_io_hook)(struct page *page, u64 start, u64 end);
        int (*readpage_io_failed_hook)(struct page *page, int failed_mirror);
 -      int (*writepage_io_failed_hook)(struct bio *bio, struct page *page,
 -                                      u64 start, u64 end,
 -                                     struct extent_state *state);
        int (*readpage_end_io_hook)(struct page *page, u64 start, u64 end,
                                    struct extent_state *state, int mirror);
        int (*writepage_end_io_hook)(struct page *page, u64 start, u64 end,
@@@ -222,8 -226,6 +223,8 @@@ int set_extent_bit(struct extent_io_tre
                   struct extent_state **cached_state, gfp_t mask);
  int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
                        struct extent_state **cached_state, gfp_t mask);
 +int clear_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
 +                        struct extent_state **cached_state, gfp_t mask);
  int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
                   gfp_t mask);
  int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
@@@ -264,6 -266,8 +265,8 @@@ void set_page_extent_mapped(struct pag
  
  struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
                                          u64 start, unsigned long len);
+ struct extent_buffer *alloc_dummy_extent_buffer(u64 start, unsigned long len);
+ struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src);
  struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
                                         u64 start, unsigned long len);
  void free_extent_buffer(struct extent_buffer *eb);
diff --combined fs/btrfs/ioctl.c
index 0f8c354c4c769521dbd5122cb249c57bb09dccef,7f3a91367d77c1412cfc7648db25c8f83a9ef67e..24b776c08d99f7bbb621076f68500464b6829435
@@@ -261,7 -261,6 +261,7 @@@ static int btrfs_ioctl_setflags(struct 
        }
  
        btrfs_update_iflags(inode);
 +      inode_inc_iversion(inode);
        inode->i_ctime = CURRENT_TIME;
        ret = btrfs_update_inode(trans, root, inode);
  
@@@ -368,7 -367,7 +368,7 @@@ static noinline int create_subvol(struc
                return PTR_ERR(trans);
  
        leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
-                                     0, objectid, NULL, 0, 0, 0, 0);
+                                     0, objectid, NULL, 0, 0, 0);
        if (IS_ERR(leaf)) {
                ret = PTR_ERR(leaf);
                goto fail;
@@@ -2263,12 -2262,10 +2263,12 @@@ static long btrfs_ioctl_dev_info(struc
        di_args->bytes_used = dev->bytes_used;
        di_args->total_bytes = dev->total_bytes;
        memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
 -      if (dev->name)
 +      if (dev->name) {
                strncpy(di_args->path, dev->name, sizeof(di_args->path));
 -      else
 +              di_args->path[sizeof(di_args->path) - 1] = 0;
 +      } else {
                di_args->path[0] = '\0';
 +      }
  
  out:
        if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
@@@ -2625,7 -2622,6 +2625,7 @@@ static noinline long btrfs_ioctl_clone(
                        btrfs_mark_buffer_dirty(leaf);
                        btrfs_release_path(path);
  
 +                      inode_inc_iversion(inode);
                        inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  
                        /*
@@@ -2918,7 -2914,7 +2918,7 @@@ long btrfs_ioctl_space_info(struct btrf
                up_read(&info->groups_sem);
        }
  
 -      user_dest = (struct btrfs_ioctl_space_info *)
 +      user_dest = (struct btrfs_ioctl_space_info __user *)
                (arg + sizeof(struct btrfs_ioctl_space_args));
  
        if (copy_to_user(user_dest, dest_orig, alloc_size))
@@@ -3046,28 -3042,6 +3046,28 @@@ static long btrfs_ioctl_scrub_progress(
        return ret;
  }
  
 +static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
 +                                    void __user *arg, int reset_after_read)
 +{
 +      struct btrfs_ioctl_get_dev_stats *sa;
 +      int ret;
 +
 +      if (reset_after_read && !capable(CAP_SYS_ADMIN))
 +              return -EPERM;
 +
 +      sa = memdup_user(arg, sizeof(*sa));
 +      if (IS_ERR(sa))
 +              return PTR_ERR(sa);
 +
 +      ret = btrfs_get_dev_stats(root, sa, reset_after_read);
 +
 +      if (copy_to_user(arg, sa, sizeof(*sa)))
 +              ret = -EFAULT;
 +
 +      kfree(sa);
 +      return ret;
 +}
 +
  static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
  {
        int ret = 0;
@@@ -3238,9 -3212,8 +3238,9 @@@ void update_ioctl_balance_args(struct b
        }
  }
  
 -static long btrfs_ioctl_balance(struct btrfs_root *root, void __user *arg)
 +static long btrfs_ioctl_balance(struct file *file, void __user *arg)
  {
 +      struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
        struct btrfs_fs_info *fs_info = root->fs_info;
        struct btrfs_ioctl_balance_args *bargs;
        struct btrfs_balance_control *bctl;
        if (fs_info->sb->s_flags & MS_RDONLY)
                return -EROFS;
  
 +      ret = mnt_want_write(file->f_path.mnt);
 +      if (ret)
 +              return ret;
 +
        mutex_lock(&fs_info->volume_mutex);
        mutex_lock(&fs_info->balance_mutex);
  
@@@ -3322,7 -3291,6 +3322,7 @@@ out_bargs
  out:
        mutex_unlock(&fs_info->balance_mutex);
        mutex_unlock(&fs_info->volume_mutex);
 +      mnt_drop_write(file->f_path.mnt);
        return ret;
  }
  
@@@ -3418,7 -3386,7 +3418,7 @@@ long btrfs_ioctl(struct file *file, uns
        case BTRFS_IOC_DEV_INFO:
                return btrfs_ioctl_dev_info(root, argp);
        case BTRFS_IOC_BALANCE:
 -              return btrfs_ioctl_balance(root, NULL);
 +              return btrfs_ioctl_balance(file, NULL);
        case BTRFS_IOC_CLONE:
                return btrfs_ioctl_clone(file, arg, 0, 0, 0);
        case BTRFS_IOC_CLONE_RANGE:
        case BTRFS_IOC_SCRUB_PROGRESS:
                return btrfs_ioctl_scrub_progress(root, argp);
        case BTRFS_IOC_BALANCE_V2:
 -              return btrfs_ioctl_balance(root, argp);
 +              return btrfs_ioctl_balance(file, argp);
        case BTRFS_IOC_BALANCE_CTL:
                return btrfs_ioctl_balance_ctl(root, arg);
        case BTRFS_IOC_BALANCE_PROGRESS:
                return btrfs_ioctl_balance_progress(root, argp);
 +      case BTRFS_IOC_GET_DEV_STATS:
 +              return btrfs_ioctl_get_dev_stats(root, argp, 0);
 +      case BTRFS_IOC_GET_AND_RESET_DEV_STATS:
 +              return btrfs_ioctl_get_dev_stats(root, argp, 1);
        }
  
        return -ENOTTY;
diff --combined fs/btrfs/transaction.c
index 82b03afcbd928016c6e0eec54bacabbe3fb35453,667735fb45e6f851b0fe56e4af2ded66e77ae9b6..1791c6e3d83487d82c9ffe80ab0239976cfd1c96
@@@ -28,7 -28,6 +28,7 @@@
  #include "locking.h"
  #include "tree-log.h"
  #include "inode-map.h"
 +#include "volumes.h"
  
  #define BTRFS_ROOT_TRANS_TAG 0
  
@@@ -56,48 -55,49 +56,49 @@@ static noinline void switch_commit_root
  static noinline int join_transaction(struct btrfs_root *root, int nofail)
  {
        struct btrfs_transaction *cur_trans;
+       struct btrfs_fs_info *fs_info = root->fs_info;
  
-       spin_lock(&root->fs_info->trans_lock);
+       spin_lock(&fs_info->trans_lock);
  loop:
        /* The file system has been taken offline. No new transactions. */
-       if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
-               spin_unlock(&root->fs_info->trans_lock);
+       if (fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
+               spin_unlock(&fs_info->trans_lock);
                return -EROFS;
        }
  
-       if (root->fs_info->trans_no_join) {
+       if (fs_info->trans_no_join) {
                if (!nofail) {
-                       spin_unlock(&root->fs_info->trans_lock);
+                       spin_unlock(&fs_info->trans_lock);
                        return -EBUSY;
                }
        }
  
-       cur_trans = root->fs_info->running_transaction;
+       cur_trans = fs_info->running_transaction;
        if (cur_trans) {
                if (cur_trans->aborted) {
-                       spin_unlock(&root->fs_info->trans_lock);
+                       spin_unlock(&fs_info->trans_lock);
                        return cur_trans->aborted;
                }
                atomic_inc(&cur_trans->use_count);
                atomic_inc(&cur_trans->num_writers);
                cur_trans->num_joined++;
-               spin_unlock(&root->fs_info->trans_lock);
+               spin_unlock(&fs_info->trans_lock);
                return 0;
        }
-       spin_unlock(&root->fs_info->trans_lock);
+       spin_unlock(&fs_info->trans_lock);
  
        cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, GFP_NOFS);
        if (!cur_trans)
                return -ENOMEM;
  
-       spin_lock(&root->fs_info->trans_lock);
-       if (root->fs_info->running_transaction) {
+       spin_lock(&fs_info->trans_lock);
+       if (fs_info->running_transaction) {
                /*
                 * someone started a transaction after we unlocked.  Make sure
                 * to redo the trans_no_join checks above
                 */
                kmem_cache_free(btrfs_transaction_cachep, cur_trans);
-               cur_trans = root->fs_info->running_transaction;
+               cur_trans = fs_info->running_transaction;
                goto loop;
        }
  
        cur_trans->delayed_refs.flushing = 0;
        cur_trans->delayed_refs.run_delayed_start = 0;
        cur_trans->delayed_refs.seq = 1;
+       /*
+        * although the tree mod log is per file system and not per transaction,
+        * the log must never go across transaction boundaries.
+        */
+       smp_mb();
+       if (!list_empty(&fs_info->tree_mod_seq_list)) {
+               printk(KERN_ERR "btrfs: tree_mod_seq_list not empty when "
+                       "creating a fresh transaction\n");
+               WARN_ON(1);
+       }
+       if (!RB_EMPTY_ROOT(&fs_info->tree_mod_log)) {
+               printk(KERN_ERR "btrfs: tree_mod_log rb tree not empty when "
+                       "creating a fresh transaction\n");
+               WARN_ON(1);
+       }
+       atomic_set(&fs_info->tree_mod_seq, 0);
        init_waitqueue_head(&cur_trans->delayed_refs.seq_wait);
        spin_lock_init(&cur_trans->commit_lock);
        spin_lock_init(&cur_trans->delayed_refs.lock);
        INIT_LIST_HEAD(&cur_trans->delayed_refs.seq_head);
  
        INIT_LIST_HEAD(&cur_trans->pending_snapshots);
-       list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
+       list_add_tail(&cur_trans->list, &fs_info->trans_list);
        extent_io_tree_init(&cur_trans->dirty_pages,
-                            root->fs_info->btree_inode->i_mapping);
-       root->fs_info->generation++;
-       cur_trans->transid = root->fs_info->generation;
-       root->fs_info->running_transaction = cur_trans;
+                            fs_info->btree_inode->i_mapping);
+       fs_info->generation++;
+       cur_trans->transid = fs_info->generation;
+       fs_info->running_transaction = cur_trans;
        cur_trans->aborted = 0;
-       spin_unlock(&root->fs_info->trans_lock);
+       spin_unlock(&fs_info->trans_lock);
  
        return 0;
  }
@@@ -759,9 -777,6 +778,9 @@@ static noinline int commit_cowonly_root
        if (ret)
                return ret;
  
 +      ret = btrfs_run_dev_stats(trans, root->fs_info);
 +      BUG_ON(ret);
 +
        while (!list_empty(&fs_info->dirty_cowonly_roots)) {
                next = fs_info->dirty_cowonly_roots.next;
                list_del_init(next);
diff --combined fs/btrfs/ulist.c
index ad993bc2df93eb3d5b0cef4473c09ef96591bc5d,2ef59400ad6eb38bef17d3967f33fb46dd4287ac..ab942f46b3dd81e06348c4950901f3e4eef87016
@@@ -23,9 -23,9 +23,9 @@@
   *
   * ulist = ulist_alloc();
   * ulist_add(ulist, root);
-  * elem = NULL;
+  * ULIST_ITER_INIT(&uiter);
   *
-  * while ((elem = ulist_next(ulist, elem)) {
+  * while ((elem = ulist_next(ulist, &uiter)) {
   *    for (all child nodes n in elem)
   *            ulist_add(ulist, n);
   *    do something useful with the node;
@@@ -95,7 -95,7 +95,7 @@@ EXPORT_SYMBOL(ulist_reinit)
   *
   * The allocated ulist will be returned in an initialized state.
   */
 -struct ulist *ulist_alloc(unsigned long gfp_mask)
 +struct ulist *ulist_alloc(gfp_t gfp_mask)
  {
        struct ulist *ulist = kmalloc(sizeof(*ulist), gfp_mask);
  
@@@ -144,13 -144,22 +144,22 @@@ EXPORT_SYMBOL(ulist_free)
   * unaltered.
   */
  int ulist_add(struct ulist *ulist, u64 val, unsigned long aux,
 -            unsigned long gfp_mask)
 +            gfp_t gfp_mask)
+ {
+       return ulist_add_merge(ulist, val, aux, NULL, gfp_mask);
+ }
+ int ulist_add_merge(struct ulist *ulist, u64 val, unsigned long aux,
 -                  unsigned long *old_aux, unsigned long gfp_mask)
++                  unsigned long *old_aux, gfp_t gfp_mask)
  {
        int i;
  
        for (i = 0; i < ulist->nnodes; ++i) {
-               if (ulist->nodes[i].val == val)
+               if (ulist->nodes[i].val == val) {
+                       if (old_aux)
+                               *old_aux = ulist->nodes[i].aux;
                        return 0;
+               }
        }
  
        if (ulist->nnodes >= ulist->nodes_alloced) {
@@@ -188,33 -197,26 +197,26 @@@ EXPORT_SYMBOL(ulist_add)
  /**
   * ulist_next - iterate ulist
   * @ulist:    ulist to iterate
-  * @prev:     previously returned element or %NULL to start iteration
+  * @uiter:    iterator variable, initialized with ULIST_ITER_INIT(&iterator)
   *
   * Note: locking must be provided by the caller. In case of rwlocks only read
   *       locking is needed
   *
-  * This function is used to iterate an ulist. The iteration is started with
-  * @prev = %NULL. It returns the next element from the ulist or %NULL when the
+  * This function is used to iterate an ulist.
+  * It returns the next element from the ulist or %NULL when the
   * end is reached. No guarantee is made with respect to the order in which
   * the elements are returned. They might neither be returned in order of
   * addition nor in ascending order.
   * It is allowed to call ulist_add during an enumeration. Newly added items
   * are guaranteed to show up in the running enumeration.
   */
- struct ulist_node *ulist_next(struct ulist *ulist, struct ulist_node *prev)
+ struct ulist_node *ulist_next(struct ulist *ulist, struct ulist_iterator *uiter)
  {
-       int next;
        if (ulist->nnodes == 0)
                return NULL;
-       if (!prev)
-               return &ulist->nodes[0];
-       next = (prev - ulist->nodes) + 1;
-       if (next < 0 || next >= ulist->nnodes)
+       if (uiter->i < 0 || uiter->i >= ulist->nnodes)
                return NULL;
  
-       return &ulist->nodes[next];
+       return &ulist->nodes[uiter->i++];
  }
  EXPORT_SYMBOL(ulist_next);
diff --combined fs/btrfs/ulist.h
index 6568c3527732e2f83fce5d99ac2112f716ffd1a5,f1b1bf00c5a93fb1add21da0e8dd1cbbdd2ba448..21bdc8ec813046ac56e3c7db0739bcdba7ac188a
   */
  #define ULIST_SIZE 16
  
+ struct ulist_iterator {
+       int i;
+ };
  /*
   * element of the list
   */
@@@ -59,9 -63,15 +63,15 @@@ struct ulist 
  void ulist_init(struct ulist *ulist);
  void ulist_fini(struct ulist *ulist);
  void ulist_reinit(struct ulist *ulist);
 -struct ulist *ulist_alloc(unsigned long gfp_mask);
 +struct ulist *ulist_alloc(gfp_t gfp_mask);
  void ulist_free(struct ulist *ulist);
- int ulist_add(struct ulist *ulist, u64 val, unsigned long aux, gfp_t gfp_mask);
- struct ulist_node *ulist_next(struct ulist *ulist, struct ulist_node *prev);
+ int ulist_add(struct ulist *ulist, u64 val, unsigned long aux,
 -            unsigned long gfp_mask);
++            gfp_t gfp_mask);
+ int ulist_add_merge(struct ulist *ulist, u64 val, unsigned long aux,
 -                  unsigned long *old_aux, unsigned long gfp_mask);
++                  unsigned long *old_aux, gfp_t gfp_mask);
+ struct ulist_node *ulist_next(struct ulist *ulist,
+                             struct ulist_iterator *uiter);
+ #define ULIST_ITER_INIT(uiter) ((uiter)->i = 0)
  
  #endif