mm: task dirty accounting fix
[safe/jmp/linux-2.6] / fs / btrfs / tree-log.c
index 33eee25..9c462fb 100644 (file)
@@ -50,6 +50,9 @@
 static int __btrfs_log_inode(struct btrfs_trans_handle *trans,
                             struct btrfs_root *root, struct inode *inode,
                             int inode_only);
+static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
+                            struct btrfs_root *root,
+                            struct btrfs_path *path, u64 objectid);
 
 /*
  * tree logging is a special write ahead log used to make sure that
@@ -75,91 +78,6 @@ static int __btrfs_log_inode(struct btrfs_trans_handle *trans,
  */
 
 /*
- * btrfs_add_log_tree adds a new per-subvolume log tree into the
- * tree of log tree roots.  This must be called with a tree log transaction
- * running (see start_log_trans).
- */
-static int btrfs_add_log_tree(struct btrfs_trans_handle *trans,
-                     struct btrfs_root *root)
-{
-       struct btrfs_key key;
-       struct btrfs_root_item root_item;
-       struct btrfs_inode_item *inode_item;
-       struct extent_buffer *leaf;
-       struct btrfs_root *new_root = root;
-       int ret;
-       u64 objectid = root->root_key.objectid;
-
-       leaf = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
-                                     BTRFS_TREE_LOG_OBJECTID,
-                                     trans->transid, 0, 0, 0);
-       if (IS_ERR(leaf)) {
-               ret = PTR_ERR(leaf);
-               return ret;
-       }
-
-       btrfs_set_header_nritems(leaf, 0);
-       btrfs_set_header_level(leaf, 0);
-       btrfs_set_header_bytenr(leaf, leaf->start);
-       btrfs_set_header_generation(leaf, trans->transid);
-       btrfs_set_header_owner(leaf, BTRFS_TREE_LOG_OBJECTID);
-
-       write_extent_buffer(leaf, root->fs_info->fsid,
-                           (unsigned long)btrfs_header_fsid(leaf),
-                           BTRFS_FSID_SIZE);
-       btrfs_mark_buffer_dirty(leaf);
-
-       inode_item = &root_item.inode;
-       memset(inode_item, 0, sizeof(*inode_item));
-       inode_item->generation = cpu_to_le64(1);
-       inode_item->size = cpu_to_le64(3);
-       inode_item->nlink = cpu_to_le32(1);
-       inode_item->nbytes = cpu_to_le64(root->leafsize);
-       inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
-
-       btrfs_set_root_bytenr(&root_item, leaf->start);
-       btrfs_set_root_generation(&root_item, trans->transid);
-       btrfs_set_root_level(&root_item, 0);
-       btrfs_set_root_refs(&root_item, 0);
-       btrfs_set_root_used(&root_item, 0);
-
-       memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
-       root_item.drop_level = 0;
-
-       btrfs_tree_unlock(leaf);
-       free_extent_buffer(leaf);
-       leaf = NULL;
-
-       btrfs_set_root_dirid(&root_item, 0);
-
-       key.objectid = BTRFS_TREE_LOG_OBJECTID;
-       key.offset = objectid;
-       btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
-       ret = btrfs_insert_root(trans, root->fs_info->log_root_tree, &key,
-                               &root_item);
-       if (ret)
-               goto fail;
-
-       new_root = btrfs_read_fs_root_no_radix(root->fs_info->log_root_tree,
-                                              &key);
-       BUG_ON(!new_root);
-
-       WARN_ON(root->log_root);
-       root->log_root = new_root;
-
-       /*
-        * log trees do not get reference counted because they go away
-        * before a real commit is actually done.  They do store pointers
-        * to file data extents, and those reference counts still get
-        * updated (along with back refs to the log tree).
-        */
-       new_root->ref_cows = 0;
-       new_root->last_trans = trans->transid;
-fail:
-       return ret;
-}
-
-/*
  * start a sub transaction and setup the log tree
  * this increments the log tree writer count to make the people
  * syncing the tree wait for us to finish
@@ -168,6 +86,14 @@ static int start_log_trans(struct btrfs_trans_handle *trans,
                           struct btrfs_root *root)
 {
        int ret;
+
+       mutex_lock(&root->log_mutex);
+       if (root->log_root) {
+               root->log_batch++;
+               atomic_inc(&root->log_writers);
+               mutex_unlock(&root->log_mutex);
+               return 0;
+       }
        mutex_lock(&root->fs_info->tree_log_mutex);
        if (!root->fs_info->log_root_tree) {
                ret = btrfs_init_log_root_tree(trans, root->fs_info);
@@ -177,9 +103,10 @@ static int start_log_trans(struct btrfs_trans_handle *trans,
                ret = btrfs_add_log_tree(trans, root);
                BUG_ON(ret);
        }
-       atomic_inc(&root->fs_info->tree_log_writers);
-       root->fs_info->tree_log_batch++;
        mutex_unlock(&root->fs_info->tree_log_mutex);
+       root->log_batch++;
+       atomic_inc(&root->log_writers);
+       mutex_unlock(&root->log_mutex);
        return 0;
 }
 
@@ -196,13 +123,12 @@ static int join_running_log_trans(struct btrfs_root *root)
        if (!root->log_root)
                return -ENOENT;
 
-       mutex_lock(&root->fs_info->tree_log_mutex);
+       mutex_lock(&root->log_mutex);
        if (root->log_root) {
                ret = 0;
-               atomic_inc(&root->fs_info->tree_log_writers);
-               root->fs_info->tree_log_batch++;
+               atomic_inc(&root->log_writers);
        }
-       mutex_unlock(&root->fs_info->tree_log_mutex);
+       mutex_unlock(&root->log_mutex);
        return ret;
 }
 
@@ -212,10 +138,11 @@ static int join_running_log_trans(struct btrfs_root *root)
  */
 static int end_log_trans(struct btrfs_root *root)
 {
-       atomic_dec(&root->fs_info->tree_log_writers);
-       smp_mb();
-       if (waitqueue_active(&root->fs_info->tree_log_wait))
-               wake_up(&root->fs_info->tree_log_wait);
+       if (atomic_dec_and_test(&root->log_writers)) {
+               smp_mb();
+               if (waitqueue_active(&root->log_writer_wait))
+                       wake_up(&root->log_writer_wait);
+       }
        return 0;
 }
 
@@ -430,49 +357,6 @@ insert:
                                                   trans->transid);
                }
        }
-
-       if (overwrite_root &&
-           key->type == BTRFS_EXTENT_DATA_KEY) {
-               int extent_type;
-               struct btrfs_file_extent_item *fi;
-
-               fi = (struct btrfs_file_extent_item *)dst_ptr;
-               extent_type = btrfs_file_extent_type(path->nodes[0], fi);
-               if (extent_type == BTRFS_FILE_EXTENT_REG ||
-                   extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
-                       struct btrfs_key ins;
-                       ins.objectid = btrfs_file_extent_disk_bytenr(
-                                                       path->nodes[0], fi);
-                       ins.offset = btrfs_file_extent_disk_num_bytes(
-                                                       path->nodes[0], fi);
-                       ins.type = BTRFS_EXTENT_ITEM_KEY;
-
-                       /*
-                        * is this extent already allocated in the extent
-                        * allocation tree?  If so, just add a reference
-                        */
-                       ret = btrfs_lookup_extent(root, ins.objectid,
-                                                 ins.offset);
-                       if (ret == 0) {
-                               ret = btrfs_inc_extent_ref(trans, root,
-                                               ins.objectid, ins.offset,
-                                               path->nodes[0]->start,
-                                               root->root_key.objectid,
-                                               trans->transid, key->objectid);
-                       } else {
-                               /*
-                                * insert the extent pointer in the extent
-                                * allocation tree
-                                */
-                               ret = btrfs_alloc_logged_extent(trans, root,
-                                               path->nodes[0]->start,
-                                               root->root_key.objectid,
-                                               trans->transid, key->objectid,
-                                               &ins);
-                               BUG_ON(ret);
-                       }
-               }
-       }
 no_copy:
        btrfs_mark_buffer_dirty(path->nodes[0]);
        btrfs_release_path(root, path);
@@ -527,6 +411,7 @@ static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
        u64 extent_end;
        u64 alloc_hint;
        u64 start = key->offset;
+       u64 saved_nbytes;
        struct btrfs_file_extent_item *item;
        struct inode *inode = NULL;
        unsigned long size;
@@ -588,17 +473,95 @@ static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
        }
        btrfs_release_path(root, path);
 
+       saved_nbytes = inode_get_bytes(inode);
        /* drop any overlapping extents */
        ret = btrfs_drop_extents(trans, root, inode,
                         start, extent_end, start, &alloc_hint);
        BUG_ON(ret);
 
-       /* insert the extent */
-       ret = overwrite_item(trans, root, path, eb, slot, key);
-       BUG_ON(ret);
+       if (found_type == BTRFS_FILE_EXTENT_REG ||
+           found_type == BTRFS_FILE_EXTENT_PREALLOC) {
+               unsigned long dest_offset;
+               struct btrfs_key ins;
 
-       /* btrfs_drop_extents changes i_bytes & i_blocks, update it here */
-       inode_add_bytes(inode, extent_end - start);
+               ret = btrfs_insert_empty_item(trans, root, path, key,
+                                             sizeof(*item));
+               BUG_ON(ret);
+               dest_offset = btrfs_item_ptr_offset(path->nodes[0],
+                                                   path->slots[0]);
+               copy_extent_buffer(path->nodes[0], eb, dest_offset,
+                               (unsigned long)item,  sizeof(*item));
+
+               ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
+               ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
+               ins.type = BTRFS_EXTENT_ITEM_KEY;
+
+               if (ins.objectid > 0) {
+                       u64 csum_start;
+                       u64 csum_end;
+                       LIST_HEAD(ordered_sums);
+                       /*
+                        * is this extent already allocated in the extent
+                        * allocation tree?  If so, just add a reference
+                        */
+                       ret = btrfs_lookup_extent(root, ins.objectid,
+                                               ins.offset);
+                       if (ret == 0) {
+                               ret = btrfs_inc_extent_ref(trans, root,
+                                               ins.objectid, ins.offset,
+                                               path->nodes[0]->start,
+                                               root->root_key.objectid,
+                                               trans->transid, key->objectid);
+                       } else {
+                               /*
+                                * insert the extent pointer in the extent
+                                * allocation tree
+                                */
+                               ret = btrfs_alloc_logged_extent(trans, root,
+                                               path->nodes[0]->start,
+                                               root->root_key.objectid,
+                                               trans->transid, key->objectid,
+                                               &ins);
+                               BUG_ON(ret);
+                       }
+                       btrfs_release_path(root, path);
+
+                       if (btrfs_file_extent_compression(eb, item)) {
+                               csum_start = ins.objectid;
+                               csum_end = csum_start + ins.offset;
+                       } else {
+                               csum_start = ins.objectid +
+                                       btrfs_file_extent_offset(eb, item);
+                               csum_end = csum_start +
+                                       btrfs_file_extent_num_bytes(eb, item);
+                       }
+
+                       ret = btrfs_lookup_csums_range(root->log_root,
+                                               csum_start, csum_end - 1,
+                                               &ordered_sums);
+                       BUG_ON(ret);
+                       while (!list_empty(&ordered_sums)) {
+                               struct btrfs_ordered_sum *sums;
+                               sums = list_entry(ordered_sums.next,
+                                               struct btrfs_ordered_sum,
+                                               list);
+                               ret = btrfs_csum_file_blocks(trans,
+                                               root->fs_info->csum_root,
+                                               sums);
+                               BUG_ON(ret);
+                               list_del(&sums->list);
+                               kfree(sums);
+                       }
+               } else {
+                       btrfs_release_path(root, path);
+               }
+       } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
+               /* inline extents are easy, we just overwrite them */
+               ret = overwrite_item(trans, root, path, eb, slot, key);
+               BUG_ON(ret);
+       }
+
+       inode_set_bytes(inode, saved_nbytes);
        btrfs_update_inode(trans, root, inode);
 out:
        if (inode)
@@ -638,8 +601,10 @@ static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
        inode = read_one_inode(root, location.objectid);
        BUG_ON(!inode);
 
-       btrfs_inc_nlink(inode);
+       ret = link_to_fixup_dir(trans, root, path, location.objectid);
+       BUG_ON(ret);
        ret = btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
+       BUG_ON(ret);
        kfree(name);
 
        iput(inode);
@@ -824,7 +789,7 @@ conflict_again:
                 */
                ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
                ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
-               while(ptr < ptr_end) {
+               while (ptr < ptr_end) {
                        victim_ref = (struct btrfs_inode_ref *)ptr;
                        victim_name_len = btrfs_inode_ref_name_len(leaf,
                                                                   victim_ref);
@@ -898,71 +863,6 @@ out_nowrite:
 }
 
 /*
- * replay one csum item from the log tree into the subvolume 'root'
- * eb, slot and key all refer to the log tree
- * path is for temp use by this function and should be released on return
- *
- * This copies the checksums out of the log tree and inserts them into
- * the subvolume.  Any existing checksums for this range in the file
- * are overwritten, and new items are added where required.
- *
- * We keep this simple by reusing the btrfs_ordered_sum code from
- * the data=ordered mode.  This basically means making a copy
- * of all the checksums in ram, which we have to do anyway for kmap
- * rules.
- *
- * The copy is then sent down to btrfs_csum_file_blocks, which
- * does all the hard work of finding existing items in the file
- * or adding new ones.
- */
-static noinline int replay_one_csum(struct btrfs_trans_handle *trans,
-                                     struct btrfs_root *root,
-                                     struct btrfs_path *path,
-                                     struct extent_buffer *eb, int slot,
-                                     struct btrfs_key *key)
-{
-       int ret;
-       u32 item_size = btrfs_item_size_nr(eb, slot);
-       u64 cur_offset;
-       u16 csum_size =
-               btrfs_super_csum_size(&root->fs_info->super_copy);
-       unsigned long file_bytes;
-       struct btrfs_ordered_sum *sums;
-       struct btrfs_sector_sum *sector_sum;
-       unsigned long ptr;
-
-       file_bytes = (item_size / csum_size) * root->sectorsize;
-       sums = kzalloc(btrfs_ordered_sum_size(root, file_bytes), GFP_NOFS);
-       if (!sums) {
-               return -ENOMEM;
-       }
-
-       INIT_LIST_HEAD(&sums->list);
-       sums->len = file_bytes;
-       sums->bytenr = key->offset;
-
-       /*
-        * copy all the sums into the ordered sum struct
-        */
-       sector_sum = sums->sums;
-       cur_offset = key->offset;
-       ptr = btrfs_item_ptr_offset(eb, slot);
-       while(item_size > 0) {
-               sector_sum->bytenr = cur_offset;
-               read_extent_buffer(eb, &sector_sum->sum, ptr, csum_size);
-               sector_sum++;
-               item_size -= csum_size;
-               ptr += csum_size;
-               cur_offset += root->sectorsize;
-       }
-
-       /* let btrfs_csum_file_blocks add them into the file */
-       ret = btrfs_csum_file_blocks(trans, root->fs_info->csum_root, sums);
-       BUG_ON(ret);
-       kfree(sums);
-       return 0;
-}
-/*
  * There are a few corners where the link count of the file can't
  * be properly maintained during replay.  So, instead of adding
  * lots of complexity to the log code, we just scan the backrefs
@@ -990,7 +890,7 @@ static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
 
        path = btrfs_alloc_path();
 
-       while(1) {
+       while (1) {
                ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
                if (ret < 0)
                        break;
@@ -1007,7 +907,7 @@ static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
                ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
                ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
                                                   path->slots[0]);
-               while(ptr < ptr_end) {
+               while (ptr < ptr_end) {
                        struct btrfs_inode_ref *ref;
 
                        ref = (struct btrfs_inode_ref *)ptr;
@@ -1043,7 +943,7 @@ static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
        key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
        key.type = BTRFS_ORPHAN_ITEM_KEY;
        key.offset = (u64)-1;
-       while(1) {
+       while (1) {
                ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
                if (ret < 0)
                        break;
@@ -1201,8 +1101,7 @@ static noinline int replay_one_name(struct btrfs_trans_handle *trans,
        if (key->type == BTRFS_DIR_ITEM_KEY) {
                dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
                                       name, name_len, 1);
-       }
-       else if (key->type == BTRFS_DIR_INDEX_KEY) {
+       } else if (key->type == BTRFS_DIR_INDEX_KEY) {
                dst_di = btrfs_lookup_dir_index_item(trans, root, path,
                                                     key->objectid,
                                                     key->offset, name,
@@ -1277,7 +1176,7 @@ static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
 
        ptr = btrfs_item_ptr_offset(eb, slot);
        ptr_end = ptr + item_size;
-       while(ptr < ptr_end) {
+       while (ptr < ptr_end) {
                di = (struct btrfs_dir_item *)ptr;
                name_len = btrfs_dir_name_len(eb, di);
                ret = replay_one_name(trans, root, path, eb, di, key);
@@ -1403,7 +1302,7 @@ again:
        item_size = btrfs_item_size_nr(eb, slot);
        ptr = btrfs_item_ptr_offset(eb, slot);
        ptr_end = ptr + item_size;
-       while(ptr < ptr_end) {
+       while (ptr < ptr_end) {
                di = (struct btrfs_dir_item *)ptr;
                name_len = btrfs_dir_name_len(eb, di);
                name = kmalloc(name_len, GFP_NOFS);
@@ -1508,14 +1407,14 @@ static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
 again:
        range_start = 0;
        range_end = 0;
-       while(1) {
+       while (1) {
                ret = find_dir_range(log, path, dirid, key_type,
                                     &range_start, &range_end);
                if (ret != 0)
                        break;
 
                dir_key.offset = range_start;
-               while(1) {
+               while (1) {
                        int nritems;
                        ret = btrfs_search_slot(NULL, root, &dir_key, path,
                                                0, 0);
@@ -1656,10 +1555,6 @@ static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
                        ret = replay_one_extent(wc->trans, root, path,
                                                eb, i, &key);
                        BUG_ON(ret);
-               } else if (key.type == BTRFS_EXTENT_CSUM_KEY) {
-                       ret = replay_one_csum(wc->trans, root, path,
-                                             eb, i, &key);
-                       BUG_ON(ret);
                } else if (key.type == BTRFS_DIR_ITEM_KEY ||
                           key.type == BTRFS_DIR_INDEX_KEY) {
                        ret = replay_one_dir_item(wc->trans, root, path,
@@ -1671,7 +1566,7 @@ static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
        return 0;
 }
 
-static int noinline walk_down_log_tree(struct btrfs_trans_handle *trans,
+static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
                                   struct btrfs_root *root,
                                   struct btrfs_path *path, int *level,
                                   struct walk_control *wc)
@@ -1689,7 +1584,7 @@ static int noinline walk_down_log_tree(struct btrfs_trans_handle *trans,
        WARN_ON(*level < 0);
        WARN_ON(*level >= BTRFS_MAX_LEVEL);
 
-       while(*level > 0) {
+       while (*level > 0) {
                WARN_ON(*level < 0);
                WARN_ON(*level >= BTRFS_MAX_LEVEL);
                cur = path->nodes[*level];
@@ -1720,6 +1615,7 @@ static int noinline walk_down_log_tree(struct btrfs_trans_handle *trans,
 
                                btrfs_tree_lock(next);
                                clean_tree_block(trans, root, next);
+                               btrfs_set_lock_blocking(next);
                                btrfs_wait_tree_block_writeback(next);
                                btrfs_tree_unlock(next);
 
@@ -1748,11 +1644,11 @@ static int noinline walk_down_log_tree(struct btrfs_trans_handle *trans,
        WARN_ON(*level < 0);
        WARN_ON(*level >= BTRFS_MAX_LEVEL);
 
-       if (path->nodes[*level] == root->node) {
+       if (path->nodes[*level] == root->node)
                parent = path->nodes[*level];
-       } else {
+       else
                parent = path->nodes[*level + 1];
-       }
+
        bytenr = path->nodes[*level]->start;
 
        blocksize = btrfs_level_size(root, *level);
@@ -1766,6 +1662,7 @@ static int noinline walk_down_log_tree(struct btrfs_trans_handle *trans,
                next = path->nodes[*level];
                btrfs_tree_lock(next);
                clean_tree_block(trans, root, next);
+               btrfs_set_lock_blocking(next);
                btrfs_wait_tree_block_writeback(next);
                btrfs_tree_unlock(next);
 
@@ -1785,7 +1682,7 @@ static int noinline walk_down_log_tree(struct btrfs_trans_handle *trans,
        return 0;
 }
 
-static int noinline walk_up_log_tree(struct btrfs_trans_handle *trans,
+static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
                                 struct btrfs_root *root,
                                 struct btrfs_path *path, int *level,
                                 struct walk_control *wc)
@@ -1796,7 +1693,7 @@ static int noinline walk_up_log_tree(struct btrfs_trans_handle *trans,
        int slot;
        int ret;
 
-       for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
+       for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
                slot = path->slots[i];
                if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
                        struct extent_buffer *node;
@@ -1823,6 +1720,7 @@ static int noinline walk_up_log_tree(struct btrfs_trans_handle *trans,
 
                                btrfs_tree_lock(next);
                                clean_tree_block(trans, root, next);
+                               btrfs_set_lock_blocking(next);
                                btrfs_wait_tree_block_writeback(next);
                                btrfs_tree_unlock(next);
 
@@ -1870,7 +1768,7 @@ static int walk_log_tree(struct btrfs_trans_handle *trans,
        extent_buffer_get(log->node);
        path->slots[level] = 0;
 
-       while(1) {
+       while (1) {
                wret = walk_down_log_tree(trans, log, path, &level, wc);
                if (wret > 0)
                        break;
@@ -1895,6 +1793,7 @@ static int walk_log_tree(struct btrfs_trans_handle *trans,
 
                        btrfs_tree_lock(next);
                        clean_tree_block(trans, log, next);
+                       btrfs_set_lock_blocking(next);
                        btrfs_wait_tree_block_writeback(next);
                        btrfs_tree_unlock(next);
 
@@ -1918,26 +1817,65 @@ static int walk_log_tree(struct btrfs_trans_handle *trans,
                }
        }
        btrfs_free_path(path);
-       if (wc->free)
-               free_extent_buffer(log->node);
        return ret;
 }
 
-static int wait_log_commit(struct btrfs_root *log)
+/*
+ * helper function to update the item for a given subvolumes log root
+ * in the tree of log roots
+ */
+static int update_log_root(struct btrfs_trans_handle *trans,
+                          struct btrfs_root *log)
+{
+       int ret;
+
+       if (log->log_transid == 1) {
+               /* insert root item on the first sync */
+               ret = btrfs_insert_root(trans, log->fs_info->log_root_tree,
+                               &log->root_key, &log->root_item);
+       } else {
+               ret = btrfs_update_root(trans, log->fs_info->log_root_tree,
+                               &log->root_key, &log->root_item);
+       }
+       return ret;
+}
+
+static int wait_log_commit(struct btrfs_root *root, unsigned long transid)
 {
        DEFINE_WAIT(wait);
-       u64 transid = log->fs_info->tree_log_transid;
+       int index = transid % 2;
 
+       /*
+        * we only allow two pending log transactions at a time,
+        * so we know that if ours is more than 2 older than the
+        * current transaction, we're done
+        */
        do {
-               prepare_to_wait(&log->fs_info->tree_log_wait, &wait,
-                               TASK_UNINTERRUPTIBLE);
-               mutex_unlock(&log->fs_info->tree_log_mutex);
-               if (atomic_read(&log->fs_info->tree_log_commit))
+               prepare_to_wait(&root->log_commit_wait[index],
+                               &wait, TASK_UNINTERRUPTIBLE);
+               mutex_unlock(&root->log_mutex);
+               if (root->log_transid < transid + 2 &&
+                   atomic_read(&root->log_commit[index]))
                        schedule();
-               finish_wait(&log->fs_info->tree_log_wait, &wait);
-               mutex_lock(&log->fs_info->tree_log_mutex);
-       } while(transid == log->fs_info->tree_log_transid &&
-               atomic_read(&log->fs_info->tree_log_commit));
+               finish_wait(&root->log_commit_wait[index], &wait);
+               mutex_lock(&root->log_mutex);
+       } while (root->log_transid < transid + 2 &&
+                atomic_read(&root->log_commit[index]));
+       return 0;
+}
+
+static int wait_for_writer(struct btrfs_root *root)
+{
+       DEFINE_WAIT(wait);
+       while (atomic_read(&root->log_writers)) {
+               prepare_to_wait(&root->log_writer_wait,
+                               &wait, TASK_UNINTERRUPTIBLE);
+               mutex_unlock(&root->log_mutex);
+               if (atomic_read(&root->log_writers))
+                       schedule();
+               mutex_lock(&root->log_mutex);
+               finish_wait(&root->log_writer_wait, &wait);
+       }
        return 0;
 }
 
@@ -1949,57 +1887,114 @@ static int wait_log_commit(struct btrfs_root *log)
 int btrfs_sync_log(struct btrfs_trans_handle *trans,
                   struct btrfs_root *root)
 {
+       int index1;
+       int index2;
        int ret;
-       unsigned long batch;
        struct btrfs_root *log = root->log_root;
+       struct btrfs_root *log_root_tree = root->fs_info->log_root_tree;
 
-       mutex_lock(&log->fs_info->tree_log_mutex);
-       if (atomic_read(&log->fs_info->tree_log_commit)) {
-               wait_log_commit(log);
-               goto out;
+       mutex_lock(&root->log_mutex);
+       index1 = root->log_transid % 2;
+       if (atomic_read(&root->log_commit[index1])) {
+               wait_log_commit(root, root->log_transid);
+               mutex_unlock(&root->log_mutex);
+               return 0;
        }
-       atomic_set(&log->fs_info->tree_log_commit, 1);
+       atomic_set(&root->log_commit[index1], 1);
+
+       /* wait for previous tree log sync to complete */
+       if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
+               wait_log_commit(root, root->log_transid - 1);
 
-       while(1) {
-               batch = log->fs_info->tree_log_batch;
-               mutex_unlock(&log->fs_info->tree_log_mutex);
+       while (1) {
+               unsigned long batch = root->log_batch;
+               mutex_unlock(&root->log_mutex);
                schedule_timeout_uninterruptible(1);
-               mutex_lock(&log->fs_info->tree_log_mutex);
-
-               while(atomic_read(&log->fs_info->tree_log_writers)) {
-                       DEFINE_WAIT(wait);
-                       prepare_to_wait(&log->fs_info->tree_log_wait, &wait,
-                                       TASK_UNINTERRUPTIBLE);
-                       mutex_unlock(&log->fs_info->tree_log_mutex);
-                       if (atomic_read(&log->fs_info->tree_log_writers))
-                               schedule();
-                       mutex_lock(&log->fs_info->tree_log_mutex);
-                       finish_wait(&log->fs_info->tree_log_wait, &wait);
-               }
-               if (batch == log->fs_info->tree_log_batch)
+               mutex_lock(&root->log_mutex);
+               wait_for_writer(root);
+               if (batch == root->log_batch)
                        break;
        }
 
        ret = btrfs_write_and_wait_marked_extents(log, &log->dirty_log_pages);
        BUG_ON(ret);
-       ret = btrfs_write_and_wait_marked_extents(root->fs_info->log_root_tree,
-                              &root->fs_info->log_root_tree->dirty_log_pages);
+
+       btrfs_set_root_bytenr(&log->root_item, log->node->start);
+       btrfs_set_root_generation(&log->root_item, trans->transid);
+       btrfs_set_root_level(&log->root_item, btrfs_header_level(log->node));
+
+       root->log_batch = 0;
+       root->log_transid++;
+       log->log_transid = root->log_transid;
+       smp_mb();
+       /*
+        * log tree has been flushed to disk, new modifications of
+        * the log will be written to new positions. so it's safe to
+        * allow log writers to go in.
+        */
+       mutex_unlock(&root->log_mutex);
+
+       mutex_lock(&log_root_tree->log_mutex);
+       log_root_tree->log_batch++;
+       atomic_inc(&log_root_tree->log_writers);
+       mutex_unlock(&log_root_tree->log_mutex);
+
+       ret = update_log_root(trans, log);
+       BUG_ON(ret);
+
+       mutex_lock(&log_root_tree->log_mutex);
+       if (atomic_dec_and_test(&log_root_tree->log_writers)) {
+               smp_mb();
+               if (waitqueue_active(&log_root_tree->log_writer_wait))
+                       wake_up(&log_root_tree->log_writer_wait);
+       }
+
+       index2 = log_root_tree->log_transid % 2;
+       if (atomic_read(&log_root_tree->log_commit[index2])) {
+               wait_log_commit(log_root_tree, log_root_tree->log_transid);
+               mutex_unlock(&log_root_tree->log_mutex);
+               goto out;
+       }
+       atomic_set(&log_root_tree->log_commit[index2], 1);
+
+       if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2]))
+               wait_log_commit(log_root_tree, log_root_tree->log_transid - 1);
+
+       wait_for_writer(log_root_tree);
+
+       ret = btrfs_write_and_wait_marked_extents(log_root_tree,
+                               &log_root_tree->dirty_log_pages);
        BUG_ON(ret);
 
        btrfs_set_super_log_root(&root->fs_info->super_for_commit,
-                                log->fs_info->log_root_tree->node->start);
+                               log_root_tree->node->start);
        btrfs_set_super_log_root_level(&root->fs_info->super_for_commit,
-                      btrfs_header_level(log->fs_info->log_root_tree->node));
+                               btrfs_header_level(log_root_tree->node));
+
+       log_root_tree->log_batch = 0;
+       log_root_tree->log_transid++;
+       smp_mb();
+
+       mutex_unlock(&log_root_tree->log_mutex);
+
+       /*
+        * nobody else is going to jump in and write the the ctree
+        * super here because the log_commit atomic below is protecting
+        * us.  We must be called with a transaction handle pinning
+        * the running transaction open, so a full commit can't hop
+        * in and cause problems either.
+        */
+       write_ctree_super(trans, root->fs_info->tree_root, 2);
 
-       write_ctree_super(trans, log->fs_info->tree_root, 2);
-       log->fs_info->tree_log_transid++;
-       log->fs_info->tree_log_batch = 0;
-       atomic_set(&log->fs_info->tree_log_commit, 0);
+       atomic_set(&log_root_tree->log_commit[index2], 0);
        smp_mb();
-       if (waitqueue_active(&log->fs_info->tree_log_wait))
-               wake_up(&log->fs_info->tree_log_wait);
+       if (waitqueue_active(&log_root_tree->log_commit_wait[index2]))
+               wake_up(&log_root_tree->log_commit_wait[index2]);
 out:
-       mutex_unlock(&log->fs_info->tree_log_mutex);
+       atomic_set(&root->log_commit[index1], 0);
+       smp_mb();
+       if (waitqueue_active(&root->log_commit_wait[index1]))
+               wake_up(&root->log_commit_wait[index1]);
        return 0;
 }
 
@@ -2018,14 +2013,14 @@ int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
                .process_func = process_one_buffer
        };
 
-       if (!root->log_root)
+       if (!root->log_root || root->fs_info->log_root_recovering)
                return 0;
 
        log = root->log_root;
        ret = walk_log_tree(trans, log, &wc);
        BUG_ON(ret);
 
-       while(1) {
+       while (1) {
                ret = find_first_extent_bit(&log->dirty_log_pages,
                                    0, &start, &end, EXTENT_DIRTY);
                if (ret)
@@ -2035,38 +2030,18 @@ int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
                                   start, end, GFP_NOFS);
        }
 
-       log = root->log_root;
-       ret = btrfs_del_root(trans, root->fs_info->log_root_tree,
-                            &log->root_key);
-       BUG_ON(ret);
+       if (log->log_transid > 0) {
+               ret = btrfs_del_root(trans, root->fs_info->log_root_tree,
+                                    &log->root_key);
+               BUG_ON(ret);
+       }
        root->log_root = NULL;
-       kfree(root->log_root);
+       free_extent_buffer(log->node);
+       kfree(log);
        return 0;
 }
 
 /*
- * helper function to update the item for a given subvolumes log root
- * in the tree of log roots
- */
-static int update_log_root(struct btrfs_trans_handle *trans,
-                          struct btrfs_root *log)
-{
-       u64 bytenr = btrfs_root_bytenr(&log->root_item);
-       int ret;
-
-       if (log->node->start == bytenr)
-               return 0;
-
-       btrfs_set_root_bytenr(&log->root_item, log->node->start);
-       btrfs_set_root_generation(&log->root_item, trans->transid);
-       btrfs_set_root_level(&log->root_item, btrfs_header_level(log->node));
-       ret = btrfs_update_root(trans, log->fs_info->log_root_tree,
-                               &log->root_key, &log->root_item);
-       BUG_ON(ret);
-       return ret;
-}
-
-/*
  * If both a file and directory are logged, and unlinks or renames are
  * mixed in, we have a few interesting corners:
  *
@@ -2282,9 +2257,8 @@ static noinline int log_dir_items(struct btrfs_trans_handle *trans,
                        struct btrfs_key tmp;
                        btrfs_item_key_to_cpu(path->nodes[0], &tmp,
                                              path->slots[0]);
-                       if (key_type == tmp.type) {
+                       if (key_type == tmp.type)
                                first_offset = max(min_offset, tmp.offset) + 1;
-                       }
                }
                goto done;
        }
@@ -2314,7 +2288,7 @@ static noinline int log_dir_items(struct btrfs_trans_handle *trans,
         * we have a block from this transaction, log every item in it
         * from our directory
         */
-       while(1) {
+       while (1) {
                struct btrfs_key tmp;
                src = path->nodes[0];
                nritems = btrfs_header_nritems(src);
@@ -2391,7 +2365,7 @@ static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
 again:
        min_key = 0;
        max_key = 0;
-       while(1) {
+       while (1) {
                ret = log_dir_items(trans, root, inode, path,
                                    dst_path, key_type, min_key,
                                    &max_key);
@@ -2427,7 +2401,7 @@ static int drop_objectid_items(struct btrfs_trans_handle *trans,
        key.type = max_key_type;
        key.offset = (u64)-1;
 
-       while(1) {
+       while (1) {
                ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
 
                if (ret != 1)
@@ -2451,85 +2425,6 @@ static int drop_objectid_items(struct btrfs_trans_handle *trans,
        return 0;
 }
 
-static noinline int copy_extent_csums(struct btrfs_trans_handle *trans,
-                                     struct list_head *list,
-                                     struct btrfs_root *root,
-                                     u64 disk_bytenr, u64 len)
-{
-       struct btrfs_ordered_sum *sums;
-       struct btrfs_sector_sum *sector_sum;
-       int ret;
-       struct btrfs_path *path;
-       struct btrfs_csum_item *item = NULL;
-       u64 end = disk_bytenr + len;
-       u64 item_start_offset = 0;
-       u64 item_last_offset = 0;
-       u32 diff;
-       u32 sum;
-       u16 csum_size = btrfs_super_csum_size(&root->fs_info->super_copy);
-
-       sums = kzalloc(btrfs_ordered_sum_size(root, len), GFP_NOFS);
-
-       sector_sum = sums->sums;
-       sums->bytenr = disk_bytenr;
-       sums->len = len;
-       list_add_tail(&sums->list, list);
-
-       path = btrfs_alloc_path();
-       while(disk_bytenr < end) {
-               if (!item || disk_bytenr < item_start_offset ||
-                   disk_bytenr >= item_last_offset) {
-                       struct btrfs_key found_key;
-                       u32 item_size;
-
-                       if (item)
-                               btrfs_release_path(root, path);
-                       item = btrfs_lookup_csum(NULL, root, path,
-                                                disk_bytenr, 0);
-                       if (IS_ERR(item)) {
-                               ret = PTR_ERR(item);
-                               if (ret == -ENOENT || ret == -EFBIG)
-                                       ret = 0;
-                               sum = 0;
-                               printk("log no csum found for byte %llu\n",
-                                      (unsigned long long)disk_bytenr);
-                               item = NULL;
-                               btrfs_release_path(root, path);
-                               goto found;
-                       }
-                       btrfs_item_key_to_cpu(path->nodes[0], &found_key,
-                                             path->slots[0]);
-
-                       item_start_offset = found_key.offset;
-                       item_size = btrfs_item_size_nr(path->nodes[0],
-                                                      path->slots[0]);
-                       item_last_offset = item_start_offset +
-                               (item_size / csum_size) *
-                               root->sectorsize;
-                       item = btrfs_item_ptr(path->nodes[0], path->slots[0],
-                                             struct btrfs_csum_item);
-               }
-               /*
-                * this byte range must be able to fit inside
-                * a single leaf so it will also fit inside a u32
-                */
-               diff = disk_bytenr - item_start_offset;
-               diff = diff / root->sectorsize;
-               diff = diff * csum_size;
-
-               read_extent_buffer(path->nodes[0], &sum,
-                                  ((unsigned long)item) + diff,
-                                  csum_size);
-found:
-               sector_sum->bytenr = disk_bytenr;
-               sector_sum->sum = sum;
-               disk_bytenr += root->sectorsize;
-               sector_sum++;
-       }
-       btrfs_free_path(path);
-       return 0;
-}
-
 static noinline int copy_items(struct btrfs_trans_handle *trans,
                               struct btrfs_root *log,
                               struct btrfs_path *dst_path,
@@ -2619,10 +2514,10 @@ static noinline int copy_items(struct btrfs_trans_handle *trans,
                                                   trans->transid,
                                                   ins_keys[i].objectid);
                                        BUG_ON(ret);
-                                       ret = copy_extent_csums(trans,
-                                               &ordered_sums,
-                                               log->fs_info->csum_root,
-                                               ds + cs, cl);
+                                       ret = btrfs_lookup_csums_range(
+                                                  log->fs_info->csum_root,
+                                                  ds + cs, ds + cs + cl - 1,
+                                                  &ordered_sums);
                                        BUG_ON(ret);
                                }
                        }
@@ -2638,7 +2533,7 @@ static noinline int copy_items(struct btrfs_trans_handle *trans,
         * we have to do this after the loop above to avoid changing the
         * log tree while trying to change the log tree.
         */
-       while(!list_empty(&ordered_sums)) {
+       while (!list_empty(&ordered_sums)) {
                struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
                                                   struct btrfs_ordered_sum,
                                                   list);
@@ -2731,7 +2626,7 @@ static int __btrfs_log_inode(struct btrfs_trans_handle *trans,
        BUG_ON(ret);
        path->keep_locks = 1;
 
-       while(1) {
+       while (1) {
                ins_nr = 0;
                ret = btrfs_search_forward(root, &min_key, &max_key,
                                           path, 0, trans->transid);
@@ -2807,11 +2702,6 @@ next_slot:
 
        btrfs_free_path(path);
        btrfs_free_path(dst_path);
-
-       mutex_lock(&root->fs_info->tree_log_mutex);
-       ret = update_log_root(trans, log);
-       BUG_ON(ret);
-       mutex_unlock(&root->fs_info->tree_log_mutex);
 out:
        return 0;
 }
@@ -2843,7 +2733,7 @@ int btrfs_log_dentry(struct btrfs_trans_handle *trans,
 
        start_log_trans(trans, root);
        sb = dentry->d_inode->i_sb;
-       while(1) {
+       while (1) {
                ret = __btrfs_log_inode(trans, root, dentry->d_inode,
                                        inode_only);
                BUG_ON(ret);
@@ -2914,7 +2804,7 @@ again:
        key.offset = (u64)-1;
        btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
 
-       while(1) {
+       while (1) {
                ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
                if (ret < 0)
                        break;
@@ -2939,10 +2829,12 @@ again:
                tmp_key.offset = (u64)-1;
 
                wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
-
                BUG_ON(!wc.replay_dest);
 
+               wc.replay_dest->log_root = log;
+               mutex_lock(&fs_info->trans_mutex);
                btrfs_record_root_in_trans(wc.replay_dest);
+               mutex_unlock(&fs_info->trans_mutex);
                ret = walk_log_tree(trans, log, &wc);
                BUG_ON(ret);
 
@@ -2958,6 +2850,7 @@ again:
                }
 
                key.offset = found_key.offset - 1;
+               wc.replay_dest->log_root = NULL;
                free_extent_buffer(log->node);
                kfree(log);