All of lore.kernel.org
 help / color / mirror / Atom feed
* [GIT PULL v3] Btrfs: improve write ahead log with sub transaction
@ 2011-06-21  8:49 Liu Bo
  2011-06-21  8:49 ` [PATCH 01/12 v3] Btrfs: introduce sub transaction stuff Liu Bo
                   ` (12 more replies)
  0 siblings, 13 replies; 20+ messages in thread
From: Liu Bo @ 2011-06-21  8:49 UTC (permalink / raw)
  To: linux-btrfs; +Cc: dave, chris.mason, josef

I've been working to try to improve the write-ahead log's performance,
and I found that the bottleneck addresses in the checksum items,
especially when we want to make a random write on a large file, e.g a 4G file.

Then a idea for this suggested by Chris is to use sub transaction ids and just
to log the part of inode that had changed since either the last log commit or
the last transaction commit.  And as we also push the sub transid into the btree
blocks, we'll get much faster tree walks.  As a result, we abandon the original
brute force approach, which is "to delete all items of the inode in log",
to making sure we get the most uptodate copies of everything, and instead
we manage to "find and merge", i.e. finding extents in the log tree and merging
in the new extents from the file.

This patchset puts the above idea into code, and although the code is now more
complex, it brings us a great deal of performance improvement:

in my sysbench "write + fsync" test:

	451.01Kb/sec -> 4.3621Mb/sec

In v2, thanks to Chris, we worked together to solve 2 bugs, and after that it
works as expected.

Since there are some vital changes in recent rc, like "kill trans_mutex" and
"use cur_trans", as David asked, I rebase the patchset to the latest for-linus
branch.

More tests are welcome!

You can also get this patchset from:

	git://repo.or.cz/linux-btrfs-devel.git sub-trans

Liu Bo (12):
  Btrfs: introduce sub transaction stuff
  Btrfs: update block generation if should_cow_block fails
  Btrfs: modify btrfs_drop_extents API
  Btrfs: introduce first sub trans
  Btrfs: still update inode trans stuff when size remains unchanged
  Btrfs: improve log with sub transaction
  Btrfs: add checksum check for log
  Btrfs: fix a bug of log check
  Btrfs: kick off useless code
  Btrfs: deal with EEXIST after iput
  Btrfs: use the right generation number to read log_root_tree
  Revert "Btrfs: do not flush csum items of unchanged file data during
    treelog"

 fs/btrfs/btrfs_inode.h |   12 ++-
 fs/btrfs/ctree.c       |   69 +++++++++---
 fs/btrfs/ctree.h       |    5 +-
 fs/btrfs/disk-io.c     |   12 +-
 fs/btrfs/extent-tree.c |   10 +-
 fs/btrfs/file.c        |   22 ++---
 fs/btrfs/inode.c       |   33 ++++---
 fs/btrfs/ioctl.c       |    6 +-
 fs/btrfs/relocation.c  |    6 +-
 fs/btrfs/transaction.c |   14 ++-
 fs/btrfs/transaction.h |   19 +++-
 fs/btrfs/tree-defrag.c |    2 +-
 fs/btrfs/tree-log.c    |  272 ++++++++++++++++++++++++++++++++++-------------
 13 files changed, 331 insertions(+), 151 deletions(-)


^ permalink raw reply	[flat|nested] 20+ messages in thread

* [PATCH 01/12 v3] Btrfs: introduce sub transaction stuff
  2011-06-21  8:49 [GIT PULL v3] Btrfs: improve write ahead log with sub transaction Liu Bo
@ 2011-06-21  8:49 ` Liu Bo
  2011-06-21  8:49 ` [PATCH 02/12 v3] Btrfs: update block generation if should_cow_block fails Liu Bo
                   ` (11 subsequent siblings)
  12 siblings, 0 replies; 20+ messages in thread
From: Liu Bo @ 2011-06-21  8:49 UTC (permalink / raw)
  To: linux-btrfs; +Cc: dave, chris.mason, josef

Introduce a new concept "sub transaction",
the relation between transaction and sub transaction is

transaction A       ---> transid = x
   sub trans a(1)   ---> sub_transid = x+1
   sub trans a(2)   ---> sub_transid = x+2
     ... ...
   sub trans a(n-1) ---> sub_transid = x+n-1
   sub trans a(n)   ---> sub_transid = x+n
transaction B       ---> transid = x+n+1
     ... ...

And the most important is
a) a trans handler's transid now gets value from sub transid instead of transid.
b) when a transaction commits, transid may not added by 1, but depend on the
   biggest sub_transaction of the last neighbour transaction,
   i.e.
        B->transid = a(n)->transid + 1,
        (B->transid - A->transid) >= 1
c) we start a new sub transaction after a fsync.

We also ship some 'trans->transid' to 'trans->transaction->transid' to
ensure btrfs works well and to get rid of WARNings.

These are used for the new log code.

Signed-off-by: Liu Bo <liubo2009@cn.fujitsu.com>
---
 fs/btrfs/ctree.c       |   35 ++++++++++++++++++-----------------
 fs/btrfs/ctree.h       |    1 +
 fs/btrfs/disk-io.c     |    7 ++++---
 fs/btrfs/extent-tree.c |   10 ++++++----
 fs/btrfs/inode.c       |    4 ++--
 fs/btrfs/ioctl.c       |    2 +-
 fs/btrfs/relocation.c  |    6 +++---
 fs/btrfs/transaction.c |   14 +++++++++-----
 fs/btrfs/transaction.h |    1 +
 fs/btrfs/tree-defrag.c |    2 +-
 fs/btrfs/tree-log.c    |   16 ++++++++++++++--
 11 files changed, 60 insertions(+), 38 deletions(-)

diff --git a/fs/btrfs/ctree.c b/fs/btrfs/ctree.c
index 2e66786..f35b517 100644
--- a/fs/btrfs/ctree.c
+++ b/fs/btrfs/ctree.c
@@ -194,9 +194,9 @@ int btrfs_copy_root(struct btrfs_trans_handle *trans,
 	int level;
 	struct btrfs_disk_key disk_key;
 
-	WARN_ON(root->ref_cows && trans->transid !=
+	WARN_ON(root->ref_cows && trans->transaction->transid !=
 		root->fs_info->running_transaction->transid);
-	WARN_ON(root->ref_cows && trans->transid != root->last_trans);
+	WARN_ON(root->ref_cows && trans->transid < root->last_trans);
 
 	level = btrfs_header_level(buf);
 	if (level == 0)
@@ -391,9 +391,9 @@ static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
 
 	btrfs_assert_tree_locked(buf);
 
-	WARN_ON(root->ref_cows && trans->transid !=
+	WARN_ON(root->ref_cows && trans->transaction->transid !=
 		root->fs_info->running_transaction->transid);
-	WARN_ON(root->ref_cows && trans->transid != root->last_trans);
+	WARN_ON(root->ref_cows && trans->transid < root->last_trans);
 
 	level = btrfs_header_level(buf);
 
@@ -459,7 +459,8 @@ static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
 		else
 			parent_start = 0;
 
-		WARN_ON(trans->transid != btrfs_header_generation(parent));
+		WARN_ON(btrfs_header_generation(parent) <
+						trans->transaction->transid);
 		btrfs_set_node_blockptr(parent, parent_slot,
 					cow->start);
 		btrfs_set_node_ptr_generation(parent, parent_slot,
@@ -480,7 +481,7 @@ static inline int should_cow_block(struct btrfs_trans_handle *trans,
 				   struct btrfs_root *root,
 				   struct extent_buffer *buf)
 {
-	if (btrfs_header_generation(buf) == trans->transid &&
+	if (btrfs_header_generation(buf) >= trans->transaction->transid &&
 	    !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
 	    !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
 	      btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
@@ -508,7 +509,7 @@ noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
 		       root->fs_info->running_transaction->transid);
 		WARN_ON(1);
 	}
-	if (trans->transid != root->fs_info->generation) {
+	if (trans->transaction->transid != root->fs_info->generation) {
 		printk(KERN_CRIT "trans %llu running %llu\n",
 		       (unsigned long long)trans->transid,
 		       (unsigned long long)root->fs_info->generation);
@@ -611,7 +612,7 @@ int btrfs_realloc_node(struct btrfs_trans_handle *trans,
 
 	if (trans->transaction != root->fs_info->running_transaction)
 		WARN_ON(1);
-	if (trans->transid != root->fs_info->generation)
+	if (trans->transaction->transid != root->fs_info->generation)
 		WARN_ON(1);
 
 	parent_nritems = btrfs_header_nritems(parent);
@@ -891,7 +892,7 @@ static noinline int balance_level(struct btrfs_trans_handle *trans,
 	mid = path->nodes[level];
 
 	WARN_ON(!path->locks[level]);
-	WARN_ON(btrfs_header_generation(mid) != trans->transid);
+	WARN_ON(btrfs_header_generation(mid) < trans->transaction->transid);
 
 	orig_ptr = btrfs_node_blockptr(mid, orig_slot);
 
@@ -1098,7 +1099,7 @@ static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
 		return 1;
 
 	mid = path->nodes[level];
-	WARN_ON(btrfs_header_generation(mid) != trans->transid);
+	WARN_ON(btrfs_header_generation(mid) < trans->transaction->transid);
 
 	if (level < BTRFS_MAX_LEVEL - 1)
 		parent = path->nodes[level + 1];
@@ -1855,8 +1856,8 @@ static int push_node_left(struct btrfs_trans_handle *trans,
 	src_nritems = btrfs_header_nritems(src);
 	dst_nritems = btrfs_header_nritems(dst);
 	push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
-	WARN_ON(btrfs_header_generation(src) != trans->transid);
-	WARN_ON(btrfs_header_generation(dst) != trans->transid);
+	WARN_ON(btrfs_header_generation(src) < trans->transaction->transid);
+	WARN_ON(btrfs_header_generation(dst) < trans->transaction->transid);
 
 	if (!empty && src_nritems <= 8)
 		return 1;
@@ -1918,8 +1919,8 @@ static int balance_node_right(struct btrfs_trans_handle *trans,
 	int dst_nritems;
 	int ret = 0;
 
-	WARN_ON(btrfs_header_generation(src) != trans->transid);
-	WARN_ON(btrfs_header_generation(dst) != trans->transid);
+	WARN_ON(btrfs_header_generation(src) < trans->transaction->transid);
+	WARN_ON(btrfs_header_generation(dst) < trans->transaction->transid);
 
 	src_nritems = btrfs_header_nritems(src);
 	dst_nritems = btrfs_header_nritems(dst);
@@ -2010,7 +2011,7 @@ static noinline int insert_new_root(struct btrfs_trans_handle *trans,
 	btrfs_set_node_key(c, &lower_key, 0);
 	btrfs_set_node_blockptr(c, 0, lower->start);
 	lower_gen = btrfs_header_generation(lower);
-	WARN_ON(lower_gen != trans->transid);
+	WARN_ON(lower_gen < trans->transaction->transid);
 
 	btrfs_set_node_ptr_generation(c, 0, lower_gen);
 
@@ -2090,7 +2091,7 @@ static noinline int split_node(struct btrfs_trans_handle *trans,
 	u32 c_nritems;
 
 	c = path->nodes[level];
-	WARN_ON(btrfs_header_generation(c) != trans->transid);
+	WARN_ON(btrfs_header_generation(c) < trans->transaction->transid);
 	if (c == root->node) {
 		/* trying to split the root, lets make a new one */
 		ret = insert_new_root(trans, root, path, level + 1);
@@ -3788,7 +3789,7 @@ static noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
 {
 	int ret;
 
-	WARN_ON(btrfs_header_generation(leaf) != trans->transid);
+	WARN_ON(btrfs_header_generation(leaf) < trans->transaction->transid);
 	ret = del_ptr(trans, root, path, 1, path->slots[1]);
 	if (ret)
 		return ret;
diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
index 8e948ec..54fcc62 100644
--- a/fs/btrfs/ctree.h
+++ b/fs/btrfs/ctree.h
@@ -923,6 +923,7 @@ struct btrfs_fs_info {
 	struct mutex durable_block_rsv_mutex;
 
 	u64 generation;
+	u64 sub_generation;
 	u64 last_trans_committed;
 
 	/*
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index 1ac8db5..b7e80c3 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -1001,7 +1001,7 @@ int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
 		     struct extent_buffer *buf)
 {
 	struct inode *btree_inode = root->fs_info->btree_inode;
-	if (btrfs_header_generation(buf) ==
+	if (btrfs_header_generation(buf) >=
 	    root->fs_info->running_transaction->transid) {
 		btrfs_assert_tree_locked(buf);
 
@@ -1525,7 +1525,7 @@ static int transaction_kthread(void *arg)
 
 		trans = btrfs_join_transaction(root);
 		BUG_ON(IS_ERR(trans));
-		if (transid == trans->transid) {
+		if (transid == trans->transaction->transid) {
 			ret = btrfs_commit_transaction(trans, root);
 			BUG_ON(ret);
 		} else {
@@ -1963,6 +1963,7 @@ struct btrfs_root *open_ctree(struct super_block *sb,
 	csum_root->track_dirty = 1;
 
 	fs_info->generation = generation;
+	fs_info->sub_generation = generation;
 	fs_info->last_trans_committed = generation;
 	fs_info->data_alloc_profile = (u64)-1;
 	fs_info->metadata_alloc_profile = (u64)-1;
@@ -2631,7 +2632,7 @@ void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
 	int was_dirty;
 
 	btrfs_assert_tree_locked(buf);
-	if (transid != root->fs_info->generation) {
+	if (transid < root->fs_info->generation) {
 		printk(KERN_CRIT "btrfs transid mismatch buffer %llu, "
 		       "found %llu running %llu\n",
 			(unsigned long long)buf->start,
diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
index 1f61bf5..dbd94c8 100644
--- a/fs/btrfs/extent-tree.c
+++ b/fs/btrfs/extent-tree.c
@@ -4345,7 +4345,7 @@ int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
 	list_for_each_entry_safe(block_rsv, next_rsv,
 				 &fs_info->durable_block_rsv_list, list) {
 
-		idx = trans->transid & 0x1;
+		idx = trans->transaction->transid & 0x1;
 		if (block_rsv->freed[idx] > 0) {
 			block_rsv_add_bytes(block_rsv,
 					    block_rsv->freed[idx], 0);
@@ -4660,7 +4660,7 @@ void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
 	if (block_rsv->space_info != cache->space_info)
 		goto out;
 
-	if (btrfs_header_generation(buf) == trans->transid) {
+	if (btrfs_header_generation(buf) >= trans->transaction->transid) {
 		if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
 			ret = check_ref_cleanup(trans, root, buf->start);
 			if (!ret)
@@ -4710,7 +4710,8 @@ pin:
 
 		if (ret) {
 			spin_lock(&block_rsv->lock);
-			block_rsv->freed[trans->transid & 0x1] += buf->len;
+			block_rsv->freed[trans->transaction->transid & 0x1] +=
+								       buf->len;
 			spin_unlock(&block_rsv->lock);
 		}
 	}
@@ -6147,7 +6148,8 @@ static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
 		}
 		/* make block locked assertion in clean_tree_block happy */
 		if (!path->locks[level] &&
-		    btrfs_header_generation(eb) == trans->transid) {
+		    btrfs_header_generation(eb) >=
+						 trans->transaction->transid) {
 			btrfs_tree_lock(eb);
 			btrfs_set_lock_blocking(eb);
 			path->locks[level] = 1;
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 5813dec..6291445 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -2107,7 +2107,7 @@ void btrfs_orphan_pre_snapshot(struct btrfs_trans_handle *trans,
 	 * space than it frees. So we should make sure there is enough
 	 * reserved space.
 	 */
-	index = trans->transid & 0x1;
+	index = trans->transaction->transid & 0x1;
 	if (block_rsv->reserved + block_rsv->freed[index] < block_rsv->size) {
 		num_bytes += block_rsv->size -
 			     (block_rsv->reserved + block_rsv->freed[index]);
@@ -2131,7 +2131,7 @@ void btrfs_orphan_post_snapshot(struct btrfs_trans_handle *trans,
 
 	/* refill source subvolume's orphan block reservation */
 	block_rsv = root->orphan_block_rsv;
-	index = trans->transid & 0x1;
+	index = trans->transaction->transid & 0x1;
 	if (block_rsv->reserved + block_rsv->freed[index] < block_rsv->size) {
 		num_bytes = block_rsv->size -
 			    (block_rsv->reserved + block_rsv->freed[index]);
diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
index a3c4751..29026a7 100644
--- a/fs/btrfs/ioctl.c
+++ b/fs/btrfs/ioctl.c
@@ -2757,7 +2757,7 @@ static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp
 	trans = btrfs_start_transaction(root, 0);
 	if (IS_ERR(trans))
 		return PTR_ERR(trans);
-	transid = trans->transid;
+	transid = trans->transaction->transid;
 	ret = btrfs_commit_transaction_async(trans, root, 0);
 	if (ret) {
 		btrfs_end_transaction(trans, root);
diff --git a/fs/btrfs/relocation.c b/fs/btrfs/relocation.c
index 086b1e6..759cc47 100644
--- a/fs/btrfs/relocation.c
+++ b/fs/btrfs/relocation.c
@@ -469,7 +469,7 @@ static int update_backref_cache(struct btrfs_trans_handle *trans,
 		return 0;
 	}
 
-	if (cache->last_trans == trans->transid)
+	if (cache->last_trans >= trans->transaction->transid)
 		return 0;
 
 	/*
@@ -1281,7 +1281,7 @@ static struct btrfs_root *create_reloc_root(struct btrfs_trans_handle *trans,
 		BUG_ON(ret);
 
 		btrfs_set_root_last_snapshot(&root->root_item,
-					     trans->transid - 1);
+					     trans->transaction->transid - 1);
 	} else {
 		/*
 		 * called by btrfs_reloc_post_snapshot_hook.
@@ -2271,7 +2271,7 @@ static int record_reloc_root_in_trans(struct btrfs_trans_handle *trans,
 {
 	struct btrfs_root *root;
 
-	if (reloc_root->last_trans == trans->transid)
+	if (reloc_root->last_trans >= trans->transaction->transid)
 		return 0;
 
 	root = read_fs_root(reloc_root->fs_info, reloc_root->root_key.offset);
diff --git a/fs/btrfs/transaction.c b/fs/btrfs/transaction.c
index 51dcec8..b59eb23 100644
--- a/fs/btrfs/transaction.c
+++ b/fs/btrfs/transaction.c
@@ -113,7 +113,9 @@ static noinline int join_transaction(struct btrfs_root *root, int nofail)
 	extent_io_tree_init(&cur_trans->dirty_pages,
 			     root->fs_info->btree_inode->i_mapping);
 	root->fs_info->generation++;
+	root->fs_info->sub_generation = root->fs_info->generation;
 	cur_trans->transid = root->fs_info->generation;
+	cur_trans->sub_transid = cur_trans->transid;
 	root->fs_info->running_transaction = cur_trans;
 	spin_unlock(&root->fs_info->trans_lock);
 
@@ -129,7 +131,7 @@ static noinline int join_transaction(struct btrfs_root *root, int nofail)
 static int record_root_in_trans(struct btrfs_trans_handle *trans,
 			       struct btrfs_root *root)
 {
-	if (root->ref_cows && root->last_trans < trans->transid) {
+	if (root->ref_cows && root->last_trans < trans->transaction->transid) {
 		WARN_ON(root == root->fs_info->extent_root);
 		WARN_ON(root->commit_root != root->node);
 
@@ -146,7 +148,7 @@ static int record_root_in_trans(struct btrfs_trans_handle *trans,
 		smp_wmb();
 
 		spin_lock(&root->fs_info->fs_roots_radix_lock);
-		if (root->last_trans == trans->transid) {
+		if (root->last_trans >= trans->transaction->transid) {
 			spin_unlock(&root->fs_info->fs_roots_radix_lock);
 			return 0;
 		}
@@ -194,7 +196,7 @@ int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
 	 * and barriers
 	 */
 	smp_rmb();
-	if (root->last_trans == trans->transid &&
+	if (root->last_trans >= trans->transaction->transid &&
 	    !root->in_trans_setup)
 		return 0;
 
@@ -295,7 +297,7 @@ again:
 
 	cur_trans = root->fs_info->running_transaction;
 
-	h->transid = cur_trans->transid;
+	h->transid = cur_trans->sub_transid;
 	h->transaction = cur_trans;
 	h->blocks_used = 0;
 	h->bytes_reserved = 0;
@@ -1368,6 +1370,8 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
 
 	cur_trans = root->fs_info->running_transaction;
 
+	root->fs_info->generation = cur_trans->sub_transid;
+
 	btrfs_set_root_node(&root->fs_info->tree_root->root_item,
 			    root->fs_info->tree_root->node);
 	switch_commit_root(root->fs_info->tree_root);
@@ -1409,7 +1413,7 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
 
 	cur_trans->commit_done = 1;
 
-	root->fs_info->last_trans_committed = cur_trans->transid;
+	root->fs_info->last_trans_committed = cur_trans->sub_transid;
 
 	wake_up(&cur_trans->commit_wait);
 
diff --git a/fs/btrfs/transaction.h b/fs/btrfs/transaction.h
index 02564e6..45876b0 100644
--- a/fs/btrfs/transaction.h
+++ b/fs/btrfs/transaction.h
@@ -23,6 +23,7 @@
 
 struct btrfs_transaction {
 	u64 transid;
+	u64 sub_transid;
 	/*
 	 * total writers in this transaction, it must be zero before the
 	 * transaction can end
diff --git a/fs/btrfs/tree-defrag.c b/fs/btrfs/tree-defrag.c
index 3b580ee..a2569af 100644
--- a/fs/btrfs/tree-defrag.c
+++ b/fs/btrfs/tree-defrag.c
@@ -139,7 +139,7 @@ done:
 	if (ret != -EAGAIN) {
 		memset(&root->defrag_progress, 0,
 		       sizeof(root->defrag_progress));
-		root->defrag_trans_start = trans->transid;
+		root->defrag_trans_start = trans->transaction->transid;
 	}
 	return ret;
 }
diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c
index 4ce8a9f..917fd07 100644
--- a/fs/btrfs/tree-log.c
+++ b/fs/btrfs/tree-log.c
@@ -134,9 +134,19 @@ static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
 static int start_log_trans(struct btrfs_trans_handle *trans,
 			   struct btrfs_root *root)
 {
+	struct btrfs_transaction *cur_trans;
 	int ret;
 	int err = 0;
 
+	/* start a new sub transaction */
+	spin_lock(&root->fs_info->trans_lock);
+
+	cur_trans = root->fs_info->running_transaction;
+	cur_trans->sub_transid++;
+	root->fs_info->sub_generation = cur_trans->sub_transid;
+
+	spin_unlock(&root->fs_info->trans_lock);
+
 	mutex_lock(&root->log_mutex);
 	if (root->log_root) {
 		if (!root->log_start_pid) {
@@ -2001,7 +2011,8 @@ int btrfs_sync_log(struct btrfs_trans_handle *trans,
 	}
 
 	/* bail out if we need to do a full commit */
-	if (root->fs_info->last_trans_log_full_commit == trans->transid) {
+	if (root->fs_info->last_trans_log_full_commit >=
+						trans->transaction->transid) {
 		ret = -EAGAIN;
 		mutex_unlock(&root->log_mutex);
 		goto out;
@@ -2078,7 +2089,8 @@ int btrfs_sync_log(struct btrfs_trans_handle *trans,
 	 * now that we've moved on to the tree of log tree roots,
 	 * check the full commit flag again
 	 */
-	if (root->fs_info->last_trans_log_full_commit == trans->transid) {
+	if (root->fs_info->last_trans_log_full_commit >=
+						trans->transaction->transid) {
 		btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
 		mutex_unlock(&log_root_tree->log_mutex);
 		ret = -EAGAIN;
-- 
1.6.5.2


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [PATCH 02/12 v3] Btrfs: update block generation if should_cow_block fails
  2011-06-21  8:49 [GIT PULL v3] Btrfs: improve write ahead log with sub transaction Liu Bo
  2011-06-21  8:49 ` [PATCH 01/12 v3] Btrfs: introduce sub transaction stuff Liu Bo
@ 2011-06-21  8:49 ` Liu Bo
  2011-06-21  8:49 ` [PATCH 03/12 v3] Btrfs: modify btrfs_drop_extents API Liu Bo
                   ` (10 subsequent siblings)
  12 siblings, 0 replies; 20+ messages in thread
From: Liu Bo @ 2011-06-21  8:49 UTC (permalink / raw)
  To: linux-btrfs; +Cc: dave, chris.mason, josef

Cause we've added sub transaction, if it do not want to cow a block, we also
need to get new sub transid recorded.  This is used for log code to find the
most uptodate file extents.

Signed-off-by: Liu Bo <liubo2009@cn.fujitsu.com>
---
 fs/btrfs/ctree.c |   34 +++++++++++++++++++++++++++++++++-
 1 files changed, 33 insertions(+), 1 deletions(-)

diff --git a/fs/btrfs/ctree.c b/fs/btrfs/ctree.c
index f35b517..cfddb05 100644
--- a/fs/btrfs/ctree.c
+++ b/fs/btrfs/ctree.c
@@ -477,6 +477,33 @@ static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
 	return 0;
 }
 
+static inline void update_block_generation(struct btrfs_trans_handle *trans,
+					   struct btrfs_root *root,
+					   struct extent_buffer *buf,
+					   struct extent_buffer *parent,
+					   int slot)
+{
+	/*
+	 * If it does not need to cow this block, we still need to
+	 * update the block's generation, for transid may have been
+	 * changed during fsync.
+	*/
+	if (btrfs_header_generation(buf) == trans->transid)
+		return;
+
+	if (buf == root->node) {
+		btrfs_set_header_generation(buf, trans->transid);
+		btrfs_mark_buffer_dirty(buf);
+		add_root_to_dirty_list(root);
+	} else {
+		btrfs_set_node_ptr_generation(parent, slot,
+					      trans->transid);
+		btrfs_set_header_generation(buf, trans->transid);
+		btrfs_mark_buffer_dirty(parent);
+		btrfs_mark_buffer_dirty(buf);
+	}
+}
+
 static inline int should_cow_block(struct btrfs_trans_handle *trans,
 				   struct btrfs_root *root,
 				   struct extent_buffer *buf)
@@ -517,6 +544,7 @@ noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
 	}
 
 	if (!should_cow_block(trans, root, buf)) {
+		update_block_generation(trans, root, buf, parent, parent_slot);
 		*cow_ret = buf;
 		return 0;
 	}
@@ -1655,8 +1683,12 @@ again:
 			 * then we don't want to set the path blocking,
 			 * so we test it here
 			 */
-			if (!should_cow_block(trans, root, b))
+			if (!should_cow_block(trans, root, b)) {
+				update_block_generation(trans, root, b,
+							p->nodes[level + 1],
+							p->slots[level + 1]);
 				goto cow_done;
+			}
 
 			btrfs_set_path_blocking(p);
 
-- 
1.6.5.2


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [PATCH 03/12 v3] Btrfs: modify btrfs_drop_extents API
  2011-06-21  8:49 [GIT PULL v3] Btrfs: improve write ahead log with sub transaction Liu Bo
  2011-06-21  8:49 ` [PATCH 01/12 v3] Btrfs: introduce sub transaction stuff Liu Bo
  2011-06-21  8:49 ` [PATCH 02/12 v3] Btrfs: update block generation if should_cow_block fails Liu Bo
@ 2011-06-21  8:49 ` Liu Bo
  2011-06-21  8:49 ` [PATCH 04/12 v3] Btrfs: introduce first sub trans Liu Bo
                   ` (9 subsequent siblings)
  12 siblings, 0 replies; 20+ messages in thread
From: Liu Bo @ 2011-06-21  8:49 UTC (permalink / raw)
  To: linux-btrfs; +Cc: dave, chris.mason, josef

We want to use btrfs_drop_extent() in log code.

Signed-off-by: Liu Bo <liubo2009@cn.fujitsu.com>
---
 fs/btrfs/ctree.h    |    3 ++-
 fs/btrfs/file.c     |    9 +++++++--
 fs/btrfs/inode.c    |    6 +++---
 fs/btrfs/ioctl.c    |    4 ++--
 fs/btrfs/tree-log.c |    2 +-
 5 files changed, 15 insertions(+), 9 deletions(-)

diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
index 54fcc62..073fb37 100644
--- a/fs/btrfs/ctree.h
+++ b/fs/btrfs/ctree.h
@@ -2604,7 +2604,8 @@ int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end,
 			    int skip_pinned);
 extern const struct file_operations btrfs_file_operations;
 int btrfs_drop_extents(struct btrfs_trans_handle *trans, struct inode *inode,
-		       u64 start, u64 end, u64 *hint_byte, int drop_cache);
+		       u64 start, u64 end, u64 *hint_byte, int drop_cache,
+		       int log);
 int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
 			      struct inode *inode, u64 start, u64 end);
 int btrfs_release_file(struct inode *inode, struct file *file);
diff --git a/fs/btrfs/file.c b/fs/btrfs/file.c
index fa4ef18..3c434bb 100644
--- a/fs/btrfs/file.c
+++ b/fs/btrfs/file.c
@@ -547,7 +547,8 @@ int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end,
  * is deleted from the tree.
  */
 int btrfs_drop_extents(struct btrfs_trans_handle *trans, struct inode *inode,
-		       u64 start, u64 end, u64 *hint_byte, int drop_cache)
+		       u64 start, u64 end, u64 *hint_byte, int drop_cache,
+		       int log)
 {
 	struct btrfs_root *root = BTRFS_I(inode)->root;
 	struct extent_buffer *leaf;
@@ -567,6 +568,10 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans, struct inode *inode,
 	int recow;
 	int ret;
 
+	/* drop the existed extents in log tree */
+	if (log)
+		root = root->log_root;
+
 	if (drop_cache)
 		btrfs_drop_extent_cache(inode, start, end - 1, 0);
 
@@ -747,7 +752,7 @@ next_slot:
 						extent_end - key.offset);
 				extent_end = ALIGN(extent_end,
 						   root->sectorsize);
-			} else if (disk_bytenr > 0) {
+			} else if (disk_bytenr > 0 && !log) {
 				ret = btrfs_free_extent(trans, root,
 						disk_bytenr, num_bytes, 0,
 						root->root_key.objectid,
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 6291445..ff9d4d1 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -246,7 +246,7 @@ static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
 	}
 
 	ret = btrfs_drop_extents(trans, inode, start, aligned_end,
-				 &hint_byte, 1);
+				 &hint_byte, 1, 0);
 	BUG_ON(ret);
 
 	if (isize > actual_end)
@@ -1658,7 +1658,7 @@ static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
 	 * with the others.
 	 */
 	ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes,
-				 &hint, 0);
+				 &hint, 0, 0);
 	BUG_ON(ret);
 
 	ins.objectid = btrfs_ino(inode);
@@ -3517,7 +3517,7 @@ int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
 
 			err = btrfs_drop_extents(trans, inode, cur_offset,
 						 cur_offset + hole_size,
-						 &hint_byte, 1);
+						 &hint_byte, 1, 0);
 			if (err)
 				break;
 
diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
index 29026a7..72e1a98 100644
--- a/fs/btrfs/ioctl.c
+++ b/fs/btrfs/ioctl.c
@@ -2339,7 +2339,7 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
 				ret = btrfs_drop_extents(trans, inode,
 							 new_key.offset,
 							 new_key.offset + datal,
-							 &hint_byte, 1);
+							 &hint_byte, 1, 0);
 				BUG_ON(ret);
 
 				ret = btrfs_insert_empty_item(trans, root, path,
@@ -2394,7 +2394,7 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
 				ret = btrfs_drop_extents(trans, inode,
 							 new_key.offset,
 							 new_key.offset + datal,
-							 &hint_byte, 1);
+							 &hint_byte, 1, 0);
 				BUG_ON(ret);
 
 				ret = btrfs_insert_empty_item(trans, root, path,
diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c
index 917fd07..b91fe8b 100644
--- a/fs/btrfs/tree-log.c
+++ b/fs/btrfs/tree-log.c
@@ -562,7 +562,7 @@ static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
 	saved_nbytes = inode_get_bytes(inode);
 	/* drop any overlapping extents */
 	ret = btrfs_drop_extents(trans, inode, start, extent_end,
-				 &alloc_hint, 1);
+				 &alloc_hint, 1, 0);
 	BUG_ON(ret);
 
 	if (found_type == BTRFS_FILE_EXTENT_REG ||
-- 
1.6.5.2


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [PATCH 04/12 v3] Btrfs: introduce first sub trans
  2011-06-21  8:49 [GIT PULL v3] Btrfs: improve write ahead log with sub transaction Liu Bo
                   ` (2 preceding siblings ...)
  2011-06-21  8:49 ` [PATCH 03/12 v3] Btrfs: modify btrfs_drop_extents API Liu Bo
@ 2011-06-21  8:49 ` Liu Bo
  2011-06-21  8:49 ` [PATCH 05/12 v3] Btrfs: still update inode trans stuff when size remains unchanged Liu Bo
                   ` (8 subsequent siblings)
  12 siblings, 0 replies; 20+ messages in thread
From: Liu Bo @ 2011-06-21  8:49 UTC (permalink / raw)
  To: linux-btrfs; +Cc: dave, chris.mason, josef

In multi-thread situations, writeback of a file may span across several
sub transactions, and we need to introduce first_sub_trans to get sub_transid of
teh first sub transaction recorded, so that log code can skip file extents which
have been logged or committed onto disk.

Signed-off-by: Liu Bo <liubo2009@cn.fujitsu.com>
---
 fs/btrfs/btrfs_inode.h |    9 +++++++++
 fs/btrfs/inode.c       |   13 ++++++++++++-
 fs/btrfs/transaction.h |   17 ++++++++++++++++-
 3 files changed, 37 insertions(+), 2 deletions(-)

diff --git a/fs/btrfs/btrfs_inode.h b/fs/btrfs/btrfs_inode.h
index 52d7eca..8eca5de 100644
--- a/fs/btrfs/btrfs_inode.h
+++ b/fs/btrfs/btrfs_inode.h
@@ -80,6 +80,15 @@ struct btrfs_inode {
 	/* sequence number for NFS changes */
 	u64 sequence;
 
+	/* used to avoid race of first_sub_trans */
+	spinlock_t sub_trans_lock;
+
+	/*
+	 * sub transid of the trans that first modified this inode before
+	 * a trans commit or a log sync
+	 */
+	u64 first_sub_trans;
+
 	/*
 	 * transid of the trans_handle that last modified this inode
 	 */
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index ff9d4d1..93c9d22 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -6468,7 +6468,16 @@ again:
 	set_page_dirty(page);
 	SetPageUptodate(page);
 
-	BTRFS_I(inode)->last_trans = root->fs_info->generation;
+	spin_lock(&BTRFS_I(inode)->sub_trans_lock);
+
+	if (BTRFS_I(inode)->first_sub_trans > root->fs_info->sub_generation ||
+	    BTRFS_I(inode)->last_trans <= BTRFS_I(inode)->logged_trans ||
+	    BTRFS_I(inode)->last_trans <= root->fs_info->last_trans_committed)
+		BTRFS_I(inode)->first_sub_trans = root->fs_info->sub_generation;
+
+	spin_unlock(&BTRFS_I(inode)->sub_trans_lock);
+
+	BTRFS_I(inode)->last_trans = root->fs_info->sub_generation;
 	BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
 
 	unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
@@ -6714,6 +6723,7 @@ struct inode *btrfs_alloc_inode(struct super_block *sb)
 	ei->space_info = NULL;
 	ei->generation = 0;
 	ei->sequence = 0;
+	ei->first_sub_trans = 0;
 	ei->last_trans = 0;
 	ei->last_sub_trans = 0;
 	ei->logged_trans = 0;
@@ -6740,6 +6750,7 @@ struct inode *btrfs_alloc_inode(struct super_block *sb)
 	extent_io_tree_init(&ei->io_tree, &inode->i_data);
 	extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
 	mutex_init(&ei->log_mutex);
+	spin_lock_init(&ei->sub_trans_lock);
 	btrfs_ordered_inode_tree_init(&ei->ordered_tree);
 	INIT_LIST_HEAD(&ei->i_orphan);
 	INIT_LIST_HEAD(&ei->delalloc_inodes);
diff --git a/fs/btrfs/transaction.h b/fs/btrfs/transaction.h
index 45876b0..f5ca0fd 100644
--- a/fs/btrfs/transaction.h
+++ b/fs/btrfs/transaction.h
@@ -73,7 +73,22 @@ struct btrfs_pending_snapshot {
 static inline void btrfs_set_inode_last_trans(struct btrfs_trans_handle *trans,
 					      struct inode *inode)
 {
-	BTRFS_I(inode)->last_trans = trans->transaction->transid;
+	spin_lock(&BTRFS_I(inode)->sub_trans_lock);
+
+	/*
+	 * We have joined in a transaction, so btrfs_commit_transaction will
+	 * definitely wait for us and it does not need to add a extra
+	 * trans_mutex lock here.
+	 */
+	if (BTRFS_I(inode)->first_sub_trans > trans->transid ||
+	    BTRFS_I(inode)->last_trans <= BTRFS_I(inode)->logged_trans ||
+	    BTRFS_I(inode)->last_trans <=
+			 BTRFS_I(inode)->root->fs_info->last_trans_committed)
+		BTRFS_I(inode)->first_sub_trans = trans->transid;
+
+	spin_unlock(&BTRFS_I(inode)->sub_trans_lock);
+
+	BTRFS_I(inode)->last_trans = trans->transid;
 	BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
 }
 
-- 
1.6.5.2


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [PATCH 05/12 v3] Btrfs: still update inode trans stuff when size remains unchanged
  2011-06-21  8:49 [GIT PULL v3] Btrfs: improve write ahead log with sub transaction Liu Bo
                   ` (3 preceding siblings ...)
  2011-06-21  8:49 ` [PATCH 04/12 v3] Btrfs: introduce first sub trans Liu Bo
@ 2011-06-21  8:49 ` Liu Bo
  2011-06-21  8:49 ` [PATCH 06/12 v3] Btrfs: improve log with sub transaction Liu Bo
                   ` (7 subsequent siblings)
  12 siblings, 0 replies; 20+ messages in thread
From: Liu Bo @ 2011-06-21  8:49 UTC (permalink / raw)
  To: linux-btrfs; +Cc: dave, chris.mason, josef

Due to DIO stuff, commit 1ef30be142d2cc60e2687ef267de864cf31be995 makes btrfs
not call btrfs_update_inode when it does not update i_disk_size, but in buffer
write case, we need to update btrfs internal inode's trans stuff, so that the
log code can find the inode's changes.

Signed-off-by: Liu Bo <liubo2009@cn.fujitsu.com>
---
 fs/btrfs/inode.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)

diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 93c9d22..47dcbc0 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -1790,7 +1790,8 @@ static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
 	if (!ret) {
 		ret = btrfs_update_inode(trans, root, inode);
 		BUG_ON(ret);
-	}
+	} else
+		btrfs_set_inode_last_trans(trans, inode);
 	ret = 0;
 out:
 	if (nolock) {
-- 
1.6.5.2


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [PATCH 06/12 v3] Btrfs: improve log with sub transaction
  2011-06-21  8:49 [GIT PULL v3] Btrfs: improve write ahead log with sub transaction Liu Bo
                   ` (4 preceding siblings ...)
  2011-06-21  8:49 ` [PATCH 05/12 v3] Btrfs: still update inode trans stuff when size remains unchanged Liu Bo
@ 2011-06-21  8:49 ` Liu Bo
  2011-06-21 13:46   ` Josef Bacik
  2011-06-21  8:49 ` [PATCH 07/12 v3] Btrfs: add checksum check for log Liu Bo
                   ` (6 subsequent siblings)
  12 siblings, 1 reply; 20+ messages in thread
From: Liu Bo @ 2011-06-21  8:49 UTC (permalink / raw)
  To: linux-btrfs; +Cc: dave, chris.mason, josef

When logging an inode _A_, current btrfs will
a) clear all items belonged to _A_ in log,
b) copy all items belonged to _A_ from fs/file tree to log tree,
and this just wastes a lot of time, especially when logging big files.

So we want to use a smarter approach, i.e. "find and merge".
The amount of file extent items is the largest, so we focus on it.
Thanks to sub transaction, now we can find those file extent items which
are changed after last _transaction commit_ or last _log commit_, and
then merge them with the existed ones in log tree.

It will be great helpful on fsync performance, cause the common case is
"make changes on a _part_ of inode".

Signed-off-by: Liu Bo <liubo2009@cn.fujitsu.com>
---
 fs/btrfs/tree-log.c |  179 ++++++++++++++++++++++++++++++++++++---------------
 1 files changed, 127 insertions(+), 52 deletions(-)

diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c
index b91fe8b..6193e27 100644
--- a/fs/btrfs/tree-log.c
+++ b/fs/btrfs/tree-log.c
@@ -2581,61 +2581,106 @@ again:
 }
 
 /*
- * a helper function to drop items from the log before we relog an
- * inode.  max_key_type indicates the highest item type to remove.
- * This cannot be run for file data extents because it does not
- * free the extents they point to.
+ * a helper function to drop items from the log before we merge
+ * the uptodate items into the log tree.
  */
-static int drop_objectid_items(struct btrfs_trans_handle *trans,
-				  struct btrfs_root *log,
-				  struct btrfs_path *path,
-				  u64 objectid, int max_key_type)
+static  int prepare_for_merge_items(struct btrfs_trans_handle *trans,
+				    struct inode *inode,
+				    struct extent_buffer *eb,
+				    int slot, int nr)
 {
-	int ret;
-	struct btrfs_key key;
+	struct btrfs_root *log = BTRFS_I(inode)->root->log_root;
+	struct btrfs_path *path;
 	struct btrfs_key found_key;
+	struct btrfs_key key;
+	int i;
+	int ret;
 
-	key.objectid = objectid;
-	key.type = max_key_type;
-	key.offset = (u64)-1;
+	/* There are no relative items of the inode in log. */
+	if (BTRFS_I(inode)->logged_trans < trans->transaction->transid)
+		return 0;
 
-	while (1) {
+	path = btrfs_alloc_path();
+	if (!path)
+		return -ENOMEM;
+
+	for (i = 0; i < nr; i++) {
+		btrfs_item_key_to_cpu(eb, &key, i + slot);
+
+		if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
+			struct btrfs_file_extent_item *fi;
+			int found_type;
+			u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
+			u64 start = key.offset;
+			u64 extent_end;
+			u64 hint;
+			unsigned long size;
+
+			fi = btrfs_item_ptr(eb, slot + i,
+					    struct btrfs_file_extent_item);
+			found_type = btrfs_file_extent_type(eb, fi);
+
+			if (found_type == BTRFS_FILE_EXTENT_REG ||
+			    found_type == BTRFS_FILE_EXTENT_PREALLOC)
+				extent_end = start +
+					    btrfs_file_extent_num_bytes(eb, fi);
+			else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
+				size = btrfs_file_extent_inline_len(eb, fi);
+				extent_end = (start + size + mask) & ~mask;
+			} else
+				BUG_ON(1);
+
+			/* drop any overlapping extents */
+			ret = btrfs_drop_extents(trans, inode, start,
+						 extent_end, &hint, 0, 1);
+			BUG_ON(ret);
+
+			continue;
+		}
+
+		/* non file extent */
 		ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
-		BUG_ON(ret == 0);
 		if (ret < 0)
 			break;
 
-		if (path->slots[0] == 0)
+		/* empty log! */
+		if (ret > 0 && path->slots[0] == 0)
 			break;
 
-		path->slots[0]--;
+		if (ret > 0) {
+			btrfs_release_path(path);
+			continue;
+		}
+
 		btrfs_item_key_to_cpu(path->nodes[0], &found_key,
 				      path->slots[0]);
 
-		if (found_key.objectid != objectid)
-			break;
+		if (btrfs_comp_cpu_keys(&found_key, &key))
+			BUG_ON(1);
 
 		ret = btrfs_del_item(trans, log, path);
-		if (ret)
-			break;
+		BUG_ON(ret);
 		btrfs_release_path(path);
 	}
 	btrfs_release_path(path);
-	return ret;
+	btrfs_free_path(path);
+
+	return 0;
 }
 
 static noinline int copy_items(struct btrfs_trans_handle *trans,
-			       struct btrfs_root *log,
+			       struct inode *inode,
 			       struct btrfs_path *dst_path,
 			       struct extent_buffer *src,
 			       int start_slot, int nr, int inode_only)
 {
 	unsigned long src_offset;
 	unsigned long dst_offset;
+	struct btrfs_root *log = BTRFS_I(inode)->root->log_root;
 	struct btrfs_file_extent_item *extent;
 	struct btrfs_inode_item *inode_item;
-	int ret;
 	struct btrfs_key *ins_keys;
+	int ret;
 	u32 *ins_sizes;
 	char *ins_data;
 	int i;
@@ -2643,6 +2688,10 @@ static noinline int copy_items(struct btrfs_trans_handle *trans,
 
 	INIT_LIST_HEAD(&ordered_sums);
 
+	ret = prepare_for_merge_items(trans, inode, src, start_slot, nr);
+	if (ret)
+		return ret;
+
 	ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
 			   nr * sizeof(u32), GFP_NOFS);
 	if (!ins_data)
@@ -2749,6 +2798,34 @@ static noinline int copy_items(struct btrfs_trans_handle *trans,
 	return ret;
 }
 
+/*
+ * a helper function to filter the old file extent items by checking their
+ * generation.
+ */
+static inline int is_extent_uptodate(struct btrfs_path *path, u64 min_trans)
+{
+	struct btrfs_file_extent_item *fi;
+	struct btrfs_key key;
+	struct extent_buffer *eb;
+	int slot;
+	u64 gen;
+
+	eb = path->nodes[0];
+	slot = path->slots[0];
+
+	btrfs_item_key_to_cpu(eb, &key, slot);
+
+	if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
+		return 1;
+
+	fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
+	gen = btrfs_file_extent_generation(eb, fi);
+	if (gen < min_trans)
+		return 0;
+
+	return 1;
+}
+
 /* log a single inode in the tree log.
  * At least one parent directory for this inode must exist in the tree
  * or be logged already.
@@ -2779,6 +2856,16 @@ static int btrfs_log_inode(struct btrfs_trans_handle *trans,
 	int ins_start_slot = 0;
 	int ins_nr;
 	u64 ino = btrfs_ino(inode);
+	u64 transid;
+
+	/*
+	* We use transid in btrfs_search_forward() as a filter, in order to
+	* find the uptodate block (node or leaf).
+	*/
+	if (BTRFS_I(inode)->first_sub_trans > trans->transaction->transid)
+		transid = BTRFS_I(inode)->first_sub_trans;
+	else
+		transid = trans->transaction->transid;
 
 	log = root->log_root;
 
@@ -2816,29 +2903,12 @@ static int btrfs_log_inode(struct btrfs_trans_handle *trans,
 
 	mutex_lock(&BTRFS_I(inode)->log_mutex);
 
-	/*
-	 * a brute force approach to making sure we get the most uptodate
-	 * copies of everything.
-	 */
-	if (S_ISDIR(inode->i_mode)) {
-		int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
-
-		if (inode_only == LOG_INODE_EXISTS)
-			max_key_type = BTRFS_XATTR_ITEM_KEY;
-		ret = drop_objectid_items(trans, log, path, ino, max_key_type);
-	} else {
-		ret = btrfs_truncate_inode_items(trans, log, inode, 0, 0);
-	}
-	if (ret) {
-		err = ret;
-		goto out_unlock;
-	}
 	path->keep_locks = 1;
 
 	while (1) {
 		ins_nr = 0;
 		ret = btrfs_search_forward(root, &min_key, &max_key,
-					   path, 0, trans->transid);
+					   path, 0, transid);
 		if (ret != 0)
 			break;
 again:
@@ -2849,6 +2919,9 @@ again:
 			break;
 
 		src = path->nodes[0];
+		if (!is_extent_uptodate(path, transid))
+			goto filter;
+
 		if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
 			ins_nr++;
 			goto next_slot;
@@ -2857,15 +2930,17 @@ again:
 			ins_nr = 1;
 			goto next_slot;
 		}
-
-		ret = copy_items(trans, log, dst_path, src, ins_start_slot,
-				 ins_nr, inode_only);
-		if (ret) {
-			err = ret;
-			goto out_unlock;
+filter:
+		if (ins_nr) {
+			ret = copy_items(trans, inode, dst_path, src,
+					 ins_start_slot,
+					 ins_nr, inode_only);
+			if (ret) {
+				err = ret;
+				goto out_unlock;
+			}
+			ins_nr = 0;
 		}
-		ins_nr = 1;
-		ins_start_slot = path->slots[0];
 next_slot:
 
 		nritems = btrfs_header_nritems(path->nodes[0]);
@@ -2876,7 +2951,7 @@ next_slot:
 			goto again;
 		}
 		if (ins_nr) {
-			ret = copy_items(trans, log, dst_path, src,
+			ret = copy_items(trans, inode, dst_path, src,
 					 ins_start_slot,
 					 ins_nr, inode_only);
 			if (ret) {
@@ -2897,7 +2972,7 @@ next_slot:
 			break;
 	}
 	if (ins_nr) {
-		ret = copy_items(trans, log, dst_path, src,
+		ret = copy_items(trans, inode, dst_path, src,
 				 ins_start_slot,
 				 ins_nr, inode_only);
 		if (ret) {
-- 
1.6.5.2


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [PATCH 07/12 v3] Btrfs: add checksum check for log
  2011-06-21  8:49 [GIT PULL v3] Btrfs: improve write ahead log with sub transaction Liu Bo
                   ` (5 preceding siblings ...)
  2011-06-21  8:49 ` [PATCH 06/12 v3] Btrfs: improve log with sub transaction Liu Bo
@ 2011-06-21  8:49 ` Liu Bo
  2011-06-21 13:49   ` Josef Bacik
  2011-06-21  8:49 ` [PATCH 08/12 v3] Btrfs: fix a bug of log check Liu Bo
                   ` (5 subsequent siblings)
  12 siblings, 1 reply; 20+ messages in thread
From: Liu Bo @ 2011-06-21  8:49 UTC (permalink / raw)
  To: linux-btrfs; +Cc: dave, chris.mason, josef

If a inode is a BTRFS_INODE_NODATASUM one, it need not to look for csum items
any more.

Signed-off-by: Liu Bo <liubo2009@cn.fujitsu.com>
---
 fs/btrfs/tree-log.c |   13 ++++++++-----
 1 files changed, 8 insertions(+), 5 deletions(-)

diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c
index 6193e27..1eca1d4 100644
--- a/fs/btrfs/tree-log.c
+++ b/fs/btrfs/tree-log.c
@@ -2672,7 +2672,8 @@ static noinline int copy_items(struct btrfs_trans_handle *trans,
 			       struct inode *inode,
 			       struct btrfs_path *dst_path,
 			       struct extent_buffer *src,
-			       int start_slot, int nr, int inode_only)
+			       int start_slot, int nr, int inode_only,
+			       int csum)
 {
 	unsigned long src_offset;
 	unsigned long dst_offset;
@@ -2739,7 +2740,8 @@ static noinline int copy_items(struct btrfs_trans_handle *trans,
 		 * or deletes of this inode don't have to relog the inode
 		 * again
 		 */
-		if (btrfs_key_type(ins_keys + i) == BTRFS_EXTENT_DATA_KEY) {
+		if (btrfs_key_type(ins_keys + i) ==
+						BTRFS_EXTENT_DATA_KEY && csum) {
 			int found_type;
 			extent = btrfs_item_ptr(src, start_slot + i,
 						struct btrfs_file_extent_item);
@@ -2857,6 +2859,7 @@ static int btrfs_log_inode(struct btrfs_trans_handle *trans,
 	int ins_nr;
 	u64 ino = btrfs_ino(inode);
 	u64 transid;
+	int csum = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) ? 0 : 1;
 
 	/*
 	* We use transid in btrfs_search_forward() as a filter, in order to
@@ -2934,7 +2937,7 @@ filter:
 		if (ins_nr) {
 			ret = copy_items(trans, inode, dst_path, src,
 					 ins_start_slot,
-					 ins_nr, inode_only);
+					 ins_nr, inode_only, csum);
 			if (ret) {
 				err = ret;
 				goto out_unlock;
@@ -2953,7 +2956,7 @@ next_slot:
 		if (ins_nr) {
 			ret = copy_items(trans, inode, dst_path, src,
 					 ins_start_slot,
-					 ins_nr, inode_only);
+					 ins_nr, inode_only, csum);
 			if (ret) {
 				err = ret;
 				goto out_unlock;
@@ -2974,7 +2977,7 @@ next_slot:
 	if (ins_nr) {
 		ret = copy_items(trans, inode, dst_path, src,
 				 ins_start_slot,
-				 ins_nr, inode_only);
+				 ins_nr, inode_only, csum);
 		if (ret) {
 			err = ret;
 			goto out_unlock;
-- 
1.6.5.2


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [PATCH 08/12 v3] Btrfs: fix a bug of log check
  2011-06-21  8:49 [GIT PULL v3] Btrfs: improve write ahead log with sub transaction Liu Bo
                   ` (6 preceding siblings ...)
  2011-06-21  8:49 ` [PATCH 07/12 v3] Btrfs: add checksum check for log Liu Bo
@ 2011-06-21  8:49 ` Liu Bo
  2011-06-21  8:49 ` [PATCH 09/12 v3] Btrfs: kick off useless code Liu Bo
                   ` (4 subsequent siblings)
  12 siblings, 0 replies; 20+ messages in thread
From: Liu Bo @ 2011-06-21  8:49 UTC (permalink / raw)
  To: linux-btrfs; +Cc: dave, chris.mason, josef

The current code uses struct root's last_log_commit to check if an inode
has been logged, but the problem is that this root->last_log_commit is
shared among files.  Say we have N inodes to be logged, after the first
inode, root-last_log_commit is updated and the N-1 remains will not be
logged.

As we've introduce sub transaction and filled inode's last_trans and
logged_trans with sub_transid instead of transaction id, we can just
compare last_trans with logged_trans to determine if the processing inode
is logged.  And the more important thing is these two values are
inode-individual, so it will not interfere with others.

Signed-off-by: Liu Bo <liubo2009@cn.fujitsu.com>
---
 fs/btrfs/btrfs_inode.h |    5 -----
 fs/btrfs/ctree.h       |    1 -
 fs/btrfs/disk-io.c     |    2 --
 fs/btrfs/inode.c       |    2 --
 fs/btrfs/transaction.h |    1 -
 fs/btrfs/tree-log.c    |   16 +++-------------
 6 files changed, 3 insertions(+), 24 deletions(-)

diff --git a/fs/btrfs/btrfs_inode.h b/fs/btrfs/btrfs_inode.h
index 8eca5de..a85161e 100644
--- a/fs/btrfs/btrfs_inode.h
+++ b/fs/btrfs/btrfs_inode.h
@@ -95,11 +95,6 @@ struct btrfs_inode {
 	u64 last_trans;
 
 	/*
-	 * log transid when this inode was last modified
-	 */
-	u64 last_sub_trans;
-
-	/*
 	 * transid that last logged this inode
 	 */
 	u64 logged_trans;
diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
index 073fb37..da3c769 100644
--- a/fs/btrfs/ctree.h
+++ b/fs/btrfs/ctree.h
@@ -1157,7 +1157,6 @@ struct btrfs_root {
 	atomic_t log_writers;
 	atomic_t log_commit[2];
 	unsigned long log_transid;
-	unsigned long last_log_commit;
 	unsigned long log_batch;
 	pid_t log_start_pid;
 	bool log_multiple_pids;
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index b7e80c3..d6d71f2 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -1065,7 +1065,6 @@ static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
 	atomic_set(&root->log_writers, 0);
 	root->log_batch = 0;
 	root->log_transid = 0;
-	root->last_log_commit = 0;
 	extent_io_tree_init(&root->dirty_log_pages,
 			     fs_info->btree_inode->i_mapping);
 
@@ -1202,7 +1201,6 @@ int btrfs_add_log_tree(struct btrfs_trans_handle *trans,
 	WARN_ON(root->log_root);
 	root->log_root = log_root;
 	root->log_transid = 0;
-	root->last_log_commit = 0;
 	return 0;
 }
 
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 47dcbc0..0007ae3 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -6479,7 +6479,6 @@ again:
 	spin_unlock(&BTRFS_I(inode)->sub_trans_lock);
 
 	BTRFS_I(inode)->last_trans = root->fs_info->sub_generation;
-	BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
 
 	unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
 
@@ -6726,7 +6725,6 @@ struct inode *btrfs_alloc_inode(struct super_block *sb)
 	ei->sequence = 0;
 	ei->first_sub_trans = 0;
 	ei->last_trans = 0;
-	ei->last_sub_trans = 0;
 	ei->logged_trans = 0;
 	ei->delalloc_bytes = 0;
 	ei->reserved_bytes = 0;
diff --git a/fs/btrfs/transaction.h b/fs/btrfs/transaction.h
index f5ca0fd..fd2474a 100644
--- a/fs/btrfs/transaction.h
+++ b/fs/btrfs/transaction.h
@@ -89,7 +89,6 @@ static inline void btrfs_set_inode_last_trans(struct btrfs_trans_handle *trans,
 	spin_unlock(&BTRFS_I(inode)->sub_trans_lock);
 
 	BTRFS_I(inode)->last_trans = trans->transid;
-	BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
 }
 
 int btrfs_end_transaction(struct btrfs_trans_handle *trans,
diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c
index 1eca1d4..f1e95bf 100644
--- a/fs/btrfs/tree-log.c
+++ b/fs/btrfs/tree-log.c
@@ -1983,7 +1983,6 @@ int btrfs_sync_log(struct btrfs_trans_handle *trans,
 	int ret;
 	struct btrfs_root *log = root->log_root;
 	struct btrfs_root *log_root_tree = root->fs_info->log_root_tree;
-	unsigned long log_transid = 0;
 
 	mutex_lock(&root->log_mutex);
 	index1 = root->log_transid % 2;
@@ -2018,8 +2017,7 @@ int btrfs_sync_log(struct btrfs_trans_handle *trans,
 		goto out;
 	}
 
-	log_transid = root->log_transid;
-	if (log_transid % 2 == 0)
+	if (root->log_transid % 2 == 0)
 		mark = EXTENT_DIRTY;
 	else
 		mark = EXTENT_NEW;
@@ -2126,11 +2124,6 @@ int btrfs_sync_log(struct btrfs_trans_handle *trans,
 	btrfs_scrub_continue_super(root);
 	ret = 0;
 
-	mutex_lock(&root->log_mutex);
-	if (root->last_log_commit < log_transid)
-		root->last_log_commit = log_transid;
-	mutex_unlock(&root->log_mutex);
-
 out_wake_log_root:
 	atomic_set(&log_root_tree->log_commit[index2], 0);
 	smp_mb();
@@ -3073,14 +3066,11 @@ out:
 static int inode_in_log(struct btrfs_trans_handle *trans,
 		 struct inode *inode)
 {
-	struct btrfs_root *root = BTRFS_I(inode)->root;
 	int ret = 0;
 
-	mutex_lock(&root->log_mutex);
-	if (BTRFS_I(inode)->logged_trans == trans->transid &&
-	    BTRFS_I(inode)->last_sub_trans <= root->last_log_commit)
+	if (BTRFS_I(inode)->logged_trans >= trans->transaction->transid &&
+	    BTRFS_I(inode)->last_trans <= BTRFS_I(inode)->logged_trans)
 		ret = 1;
-	mutex_unlock(&root->log_mutex);
 	return ret;
 }
 
-- 
1.6.5.2


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [PATCH 09/12 v3] Btrfs: kick off useless code
  2011-06-21  8:49 [GIT PULL v3] Btrfs: improve write ahead log with sub transaction Liu Bo
                   ` (7 preceding siblings ...)
  2011-06-21  8:49 ` [PATCH 08/12 v3] Btrfs: fix a bug of log check Liu Bo
@ 2011-06-21  8:49 ` Liu Bo
  2011-06-21  8:49 ` [PATCH 10/12 v3] Btrfs: deal with EEXIST after iput Liu Bo
                   ` (3 subsequent siblings)
  12 siblings, 0 replies; 20+ messages in thread
From: Liu Bo @ 2011-06-21  8:49 UTC (permalink / raw)
  To: linux-btrfs; +Cc: dave, chris.mason, josef

fsync will wait for writeback till it finishes, and last_trans will get the real
transid recorded in writeback, so it does not need an extra +1 to ensure fsync's
process on the file.

Signed-off-by: Liu Bo <liubo2009@cn.fujitsu.com>
---
 fs/btrfs/file.c |   13 -------------
 1 files changed, 0 insertions(+), 13 deletions(-)

diff --git a/fs/btrfs/file.c b/fs/btrfs/file.c
index 3c434bb..8f27007 100644
--- a/fs/btrfs/file.c
+++ b/fs/btrfs/file.c
@@ -1404,19 +1404,6 @@ static ssize_t btrfs_file_aio_write(struct kiocb *iocb,
 
 	mutex_unlock(&inode->i_mutex);
 
-	/*
-	 * we want to make sure fsync finds this change
-	 * but we haven't joined a transaction running right now.
-	 *
-	 * Later on, someone is sure to update the inode and get the
-	 * real transid recorded.
-	 *
-	 * We set last_trans now to the fs_info generation + 1,
-	 * this will either be one more than the running transaction
-	 * or the generation used for the next transaction if there isn't
-	 * one running right now.
-	 */
-	BTRFS_I(inode)->last_trans = root->fs_info->generation + 1;
 	if (num_written > 0 || num_written == -EIOCBQUEUED) {
 		err = generic_write_sync(file, pos, num_written);
 		if (err < 0 && num_written > 0)
-- 
1.6.5.2


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [PATCH 10/12 v3] Btrfs: deal with EEXIST after iput
  2011-06-21  8:49 [GIT PULL v3] Btrfs: improve write ahead log with sub transaction Liu Bo
                   ` (8 preceding siblings ...)
  2011-06-21  8:49 ` [PATCH 09/12 v3] Btrfs: kick off useless code Liu Bo
@ 2011-06-21  8:49 ` Liu Bo
  2011-06-21 14:00   ` Josef Bacik
  2011-06-21  8:49 ` [PATCH 11/12 v3] Btrfs: use the right generation number to read log_root_tree Liu Bo
                   ` (2 subsequent siblings)
  12 siblings, 1 reply; 20+ messages in thread
From: Liu Bo @ 2011-06-21  8:49 UTC (permalink / raw)
  To: linux-btrfs; +Cc: dave, chris.mason, josef

There are two cases when BTRFS_I(inode)->logged_trans is zero:
a) an inode is just allocated;
b) iput an inode and reread it.

However, in b) if btrfs is not committed yet, and this inode _may_ still remain
in log tree.

So we need to check the log tree to get logged_trans a right value
in case it hits a EEXIST while logging.

Signed-off-by: Liu Bo <liubo2009@cn.fujitsu.com>
---
 fs/btrfs/inode.c    |    9 +++------
 fs/btrfs/tree-log.c |   43 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 46 insertions(+), 6 deletions(-)

diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 0007ae3..6eff806 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -1786,12 +1786,9 @@ static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
 	add_pending_csums(trans, inode, ordered_extent->file_offset,
 			  &ordered_extent->list);
 
-	ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
-	if (!ret) {
-		ret = btrfs_update_inode(trans, root, inode);
-		BUG_ON(ret);
-	} else
-		btrfs_set_inode_last_trans(trans, inode);
+	btrfs_ordered_update_i_size(inode, 0, ordered_extent);
+	ret = btrfs_update_inode(trans, root, inode);
+	BUG_ON(ret);
 	ret = 0;
 out:
 	if (nolock) {
diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c
index f1e95bf..3cf7ed2 100644
--- a/fs/btrfs/tree-log.c
+++ b/fs/btrfs/tree-log.c
@@ -3063,6 +3063,37 @@ out:
 	return ret;
 }
 
+static int check_logged_trans(struct btrfs_trans_handle *trans,
+			      struct btrfs_root *root, struct inode *inode)
+{
+	struct btrfs_inode_item *inode_item;
+	struct btrfs_path *path;
+	int ret;
+
+	path = btrfs_alloc_path();
+	if (!path)
+		return -ENOMEM;
+
+	ret = btrfs_search_slot(trans, root,
+				&BTRFS_I(inode)->location, path, 0, 0);
+	if (ret) {
+		if (ret > 0)
+			ret = 0;
+		goto out;
+	}
+
+	btrfs_unlock_up_safe(path, 1);
+	inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
+				    struct btrfs_inode_item);
+
+	BTRFS_I(inode)->logged_trans = btrfs_inode_transid(path->nodes[0],
+							   inode_item);
+out:
+	btrfs_free_path(path);
+	return ret;
+}
+
+
 static int inode_in_log(struct btrfs_trans_handle *trans,
 		 struct inode *inode)
 {
@@ -3115,6 +3146,18 @@ int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
 	if (ret)
 		goto end_no_trans;
 
+	/*
+	 * After we iput a inode and reread it from disk, logged_trans is 0.
+	 * However, this inode _may_ still remain in log tree and not be
+	 * committed yet.
+	 * So we need to check the log tree to get logged_trans a right value.
+	 */
+	if (!BTRFS_I(inode)->logged_trans && root->log_root) {
+		ret = check_logged_trans(trans, root->log_root, inode);
+		if (ret)
+			goto end_no_trans;
+	}
+
 	if (inode_in_log(trans, inode)) {
 		ret = BTRFS_NO_LOG_SYNC;
 		goto end_no_trans;
-- 
1.6.5.2


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [PATCH 11/12 v3] Btrfs: use the right generation number to read log_root_tree
  2011-06-21  8:49 [GIT PULL v3] Btrfs: improve write ahead log with sub transaction Liu Bo
                   ` (9 preceding siblings ...)
  2011-06-21  8:49 ` [PATCH 10/12 v3] Btrfs: deal with EEXIST after iput Liu Bo
@ 2011-06-21  8:49 ` Liu Bo
  2011-06-21  8:49 ` [PATCH 12/12 v3] Revert "Btrfs: do not flush csum items of unchanged file data during treelog" Liu Bo
  2011-08-04 13:57 ` [GIT PULL v3] Btrfs: improve write ahead log with sub transaction Chris Mason
  12 siblings, 0 replies; 20+ messages in thread
From: Liu Bo @ 2011-06-21  8:49 UTC (permalink / raw)
  To: linux-btrfs; +Cc: dave, chris.mason, josef

Currently we use the generation number of the super to read in the log
tree root after a crash.  This doesn't always match the sub trans id and
so it doesn't always match the transid stored in the btree blocks.

We can use log_root_transid to record the log_root_tree's generation
so that when we recover from crash, we can match log_root_tree's btree blocks.

Signed-off-by: Liu Bo <liubo2009@cn.fujitsu.com>
---
 fs/btrfs/disk-io.c  |    3 ++-
 fs/btrfs/tree-log.c |    2 ++
 2 files changed, 4 insertions(+), 1 deletions(-)

diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index d6d71f2..4568317 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -2002,6 +2002,7 @@ struct btrfs_root *open_ctree(struct super_block *sb,
 	if (btrfs_super_log_root(disk_super) != 0 &&
 	    !(fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR)) {
 		u64 bytenr = btrfs_super_log_root(disk_super);
+		u64 log_root_transid = btrfs_super_log_root_transid(disk_super);
 
 		if (fs_devices->rw_devices == 0) {
 			printk(KERN_WARNING "Btrfs log replay required "
@@ -2024,7 +2025,7 @@ struct btrfs_root *open_ctree(struct super_block *sb,
 
 		log_tree_root->node = read_tree_block(tree_root, bytenr,
 						      blocksize,
-						      generation + 1);
+						      log_root_transid);
 		ret = btrfs_recover_log_trees(log_tree_root);
 		BUG_ON(ret);
 
diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c
index 3cf7ed2..166fda5 100644
--- a/fs/btrfs/tree-log.c
+++ b/fs/btrfs/tree-log.c
@@ -2105,6 +2105,8 @@ int btrfs_sync_log(struct btrfs_trans_handle *trans,
 				log_root_tree->node->start);
 	btrfs_set_super_log_root_level(&root->fs_info->super_for_commit,
 				btrfs_header_level(log_root_tree->node));
+	btrfs_set_super_log_root_transid(&root->fs_info->super_for_commit,
+					 trans->transid);
 
 	log_root_tree->log_batch = 0;
 	log_root_tree->log_transid++;
-- 
1.6.5.2


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [PATCH 12/12 v3] Revert "Btrfs: do not flush csum items of unchanged file data during treelog"
  2011-06-21  8:49 [GIT PULL v3] Btrfs: improve write ahead log with sub transaction Liu Bo
                   ` (10 preceding siblings ...)
  2011-06-21  8:49 ` [PATCH 11/12 v3] Btrfs: use the right generation number to read log_root_tree Liu Bo
@ 2011-06-21  8:49 ` Liu Bo
  2011-08-04 13:57 ` [GIT PULL v3] Btrfs: improve write ahead log with sub transaction Chris Mason
  12 siblings, 0 replies; 20+ messages in thread
From: Liu Bo @ 2011-06-21  8:49 UTC (permalink / raw)
  To: linux-btrfs; +Cc: dave, chris.mason, josef

This reverts commit 8e531cdfeb75269c6c5aae33651cca39707848da.

Signed-off-by: Liu Bo <liubo2009@cn.fujitsu.com>
---
 fs/btrfs/tree-log.c |    3 ---
 1 files changed, 0 insertions(+), 3 deletions(-)

diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c
index 166fda5..d4ccf91 100644
--- a/fs/btrfs/tree-log.c
+++ b/fs/btrfs/tree-log.c
@@ -2741,9 +2741,6 @@ static noinline int copy_items(struct btrfs_trans_handle *trans,
 			extent = btrfs_item_ptr(src, start_slot + i,
 						struct btrfs_file_extent_item);
 
-			if (btrfs_file_extent_generation(src, extent) < trans->transid)
-				continue;
-
 			found_type = btrfs_file_extent_type(src, extent);
 			if (found_type == BTRFS_FILE_EXTENT_REG ||
 			    found_type == BTRFS_FILE_EXTENT_PREALLOC) {
-- 
1.6.5.2


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* Re: [PATCH 06/12 v3] Btrfs: improve log with sub transaction
  2011-06-21  8:49 ` [PATCH 06/12 v3] Btrfs: improve log with sub transaction Liu Bo
@ 2011-06-21 13:46   ` Josef Bacik
  0 siblings, 0 replies; 20+ messages in thread
From: Josef Bacik @ 2011-06-21 13:46 UTC (permalink / raw)
  To: Liu Bo; +Cc: linux-btrfs, dave, chris.mason

On 06/21/2011 04:49 AM, Liu Bo wrote:
> When logging an inode _A_, current btrfs will
> a) clear all items belonged to _A_ in log,
> b) copy all items belonged to _A_ from fs/file tree to log tree,
> and this just wastes a lot of time, especially when logging big files.
>
> So we want to use a smarter approach, i.e. "find and merge".
> The amount of file extent items is the largest, so we focus on it.
> Thanks to sub transaction, now we can find those file extent items which
> are changed after last _transaction commit_ or last _log commit_, and
> then merge them with the existed ones in log tree.
>
> It will be great helpful on fsync performance, cause the common case is
> "make changes on a _part_ of inode".
>
> Signed-off-by: Liu Bo<liubo2009@cn.fujitsu.com>
> ---
>   fs/btrfs/tree-log.c |  179 ++++++++++++++++++++++++++++++++++++---------------
>   1 files changed, 127 insertions(+), 52 deletions(-)
>
> diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c
> index b91fe8b..6193e27 100644
> --- a/fs/btrfs/tree-log.c
> +++ b/fs/btrfs/tree-log.c
> @@ -2581,61 +2581,106 @@ again:
>   }
>
>   /*
> - * a helper function to drop items from the log before we relog an
> - * inode.  max_key_type indicates the highest item type to remove.
> - * This cannot be run for file data extents because it does not
> - * free the extents they point to.
> + * a helper function to drop items from the log before we merge
> + * the uptodate items into the log tree.
>    */
> -static int drop_objectid_items(struct btrfs_trans_handle *trans,
> -				  struct btrfs_root *log,
> -				  struct btrfs_path *path,
> -				  u64 objectid, int max_key_type)
> +static  int prepare_for_merge_items(struct btrfs_trans_handle *trans,
> +				    struct inode *inode,
> +				    struct extent_buffer *eb,
> +				    int slot, int nr)
>   {
> -	int ret;
> -	struct btrfs_key key;
> +	struct btrfs_root *log = BTRFS_I(inode)->root->log_root;
> +	struct btrfs_path *path;
>   	struct btrfs_key found_key;
> +	struct btrfs_key key;
> +	int i;
> +	int ret;
>
> -	key.objectid = objectid;
> -	key.type = max_key_type;
> -	key.offset = (u64)-1;
> +	/* There are no relative items of the inode in log. */
> +	if (BTRFS_I(inode)->logged_trans<  trans->transaction->transid)
> +		return 0;
>
> -	while (1) {
> +	path = btrfs_alloc_path();
> +	if (!path)
> +		return -ENOMEM;
> +
> +	for (i = 0; i<  nr; i++) {
> +		btrfs_item_key_to_cpu(eb,&key, i + slot);
> +

Just a nit, but it would be less of a pain to do

for (i = slot; i < slot+nr; i++)

that way you don't have a bunch of getter(eb, slot + i) things scattered 
around.

> +		if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
> +			struct btrfs_file_extent_item *fi;
> +			int found_type;
> +			u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
> +			u64 start = key.offset;
> +			u64 extent_end;
> +			u64 hint;
> +			unsigned long size;
> +
> +			fi = btrfs_item_ptr(eb, slot + i,
> +					    struct btrfs_file_extent_item);
> +			found_type = btrfs_file_extent_type(eb, fi);
> +
> +			if (found_type == BTRFS_FILE_EXTENT_REG ||
> +			    found_type == BTRFS_FILE_EXTENT_PREALLOC)
> +				extent_end = start +
> +					    btrfs_file_extent_num_bytes(eb, fi);
> +			else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
> +				size = btrfs_file_extent_inline_len(eb, fi);
> +				extent_end = (start + size + mask)&  ~mask;
> +			} else
> +				BUG_ON(1);
> +

Style screw up, if one chunk of an if/else block has braces, they all 
need it.

> +			/* drop any overlapping extents */
> +			ret = btrfs_drop_extents(trans, inode, start,
> +						 extent_end,&hint, 0, 1);

There are lot's of places where you've missed a space, like here where 
you have extent_end,&hint

> +			BUG_ON(ret);
> +
> +			continue;
> +		}
> +
> +		/* non file extent */
>   		ret = btrfs_search_slot(trans, log,&key, path, -1, 1);
> -		BUG_ON(ret == 0);
>   		if (ret<  0)
>   			break;
>
> -		if (path->slots[0] == 0)
> +		/* empty log! */
> +		if (ret>  0&&  path->slots[0] == 0)

Here too.

>   			break;
>
> -		path->slots[0]--;
> +		if (ret>  0) {

Here.

> +			btrfs_release_path(path);
> +			continue;
> +		}
> +
>   		btrfs_item_key_to_cpu(path->nodes[0],&found_key,
>   				      path->slots[0]);
>
> -		if (found_key.objectid != objectid)
> -			break;
> +		if (btrfs_comp_cpu_keys(&found_key,&key))
> +			BUG_ON(1);
>
>   		ret = btrfs_del_item(trans, log, path);
> -		if (ret)
> -			break;
> +		BUG_ON(ret);
>   		btrfs_release_path(path);
>   	}
>   	btrfs_release_path(path);
> -	return ret;
> +	btrfs_free_path(path);
> +
> +	return 0;
>   }
>
>   static noinline int copy_items(struct btrfs_trans_handle *trans,
> -			       struct btrfs_root *log,
> +			       struct inode *inode,
>   			       struct btrfs_path *dst_path,
>   			       struct extent_buffer *src,
>   			       int start_slot, int nr, int inode_only)
>   {
>   	unsigned long src_offset;
>   	unsigned long dst_offset;
> +	struct btrfs_root *log = BTRFS_I(inode)->root->log_root;
>   	struct btrfs_file_extent_item *extent;
>   	struct btrfs_inode_item *inode_item;
> -	int ret;
>   	struct btrfs_key *ins_keys;
> +	int ret;
>   	u32 *ins_sizes;
>   	char *ins_data;
>   	int i;
> @@ -2643,6 +2688,10 @@ static noinline int copy_items(struct btrfs_trans_handle *trans,
>
>   	INIT_LIST_HEAD(&ordered_sums);
>
> +	ret = prepare_for_merge_items(trans, inode, src, start_slot, nr);
> +	if (ret)
> +		return ret;
> +
>   	ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
>   			   nr * sizeof(u32), GFP_NOFS);
>   	if (!ins_data)
> @@ -2749,6 +2798,34 @@ static noinline int copy_items(struct btrfs_trans_handle *trans,
>   	return ret;
>   }
>
> +/*
> + * a helper function to filter the old file extent items by checking their
> + * generation.
> + */
> +static inline int is_extent_uptodate(struct btrfs_path *path, u64 min_trans)
> +{
> +	struct btrfs_file_extent_item *fi;
> +	struct btrfs_key key;
> +	struct extent_buffer *eb;
> +	int slot;
> +	u64 gen;
> +
> +	eb = path->nodes[0];
> +	slot = path->slots[0];
> +
> +	btrfs_item_key_to_cpu(eb,&key, slot);
> +
> +	if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
> +		return 1;
> +
> +	fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
> +	gen = btrfs_file_extent_generation(eb, fi);
> +	if (gen<  min_trans)

Here.

> +		return 0;
> +
> +	return 1;
> +}
> +
>   /* log a single inode in the tree log.
>    * At least one parent directory for this inode must exist in the tree
>    * or be logged already.
> @@ -2779,6 +2856,16 @@ static int btrfs_log_inode(struct btrfs_trans_handle *trans,
>   	int ins_start_slot = 0;
>   	int ins_nr;
>   	u64 ino = btrfs_ino(inode);
> +	u64 transid;
> +
> +	/*
> +	* We use transid in btrfs_search_forward() as a filter, in order to
> +	* find the uptodate block (node or leaf).
> +	*/
> +	if (BTRFS_I(inode)->first_sub_trans>  trans->transaction->transid)

Here.

> +		transid = BTRFS_I(inode)->first_sub_trans;
> +	else
> +		transid = trans->transaction->transid;
>
>   	log = root->log_root;
>
> @@ -2816,29 +2903,12 @@ static int btrfs_log_inode(struct btrfs_trans_handle *trans,
>
>   	mutex_lock(&BTRFS_I(inode)->log_mutex);
>
> -	/*
> -	 * a brute force approach to making sure we get the most uptodate
> -	 * copies of everything.
> -	 */
> -	if (S_ISDIR(inode->i_mode)) {
> -		int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
> -
> -		if (inode_only == LOG_INODE_EXISTS)
> -			max_key_type = BTRFS_XATTR_ITEM_KEY;
> -		ret = drop_objectid_items(trans, log, path, ino, max_key_type);
> -	} else {
> -		ret = btrfs_truncate_inode_items(trans, log, inode, 0, 0);
> -	}
> -	if (ret) {
> -		err = ret;
> -		goto out_unlock;
> -	}
>   	path->keep_locks = 1;
>
>   	while (1) {
>   		ins_nr = 0;
>   		ret = btrfs_search_forward(root,&min_key,&max_key,
> -					   path, 0, trans->transid);
> +					   path, 0, transid);
>   		if (ret != 0)
>   			break;
>   again:
> @@ -2849,6 +2919,9 @@ again:
>   			break;
>
>   		src = path->nodes[0];
> +		if (!is_extent_uptodate(path, transid))
> +			goto filter;
> +
>   		if (ins_nr&&  ins_start_slot + ins_nr == path->slots[0]) {
>   			ins_nr++;
>   			goto next_slot;
> @@ -2857,15 +2930,17 @@ again:
>   			ins_nr = 1;
>   			goto next_slot;
>   		}
> -
> -		ret = copy_items(trans, log, dst_path, src, ins_start_slot,
> -				 ins_nr, inode_only);
> -		if (ret) {
> -			err = ret;
> -			goto out_unlock;
> +filter:
> +		if (ins_nr) {
> +			ret = copy_items(trans, inode, dst_path, src,
> +					 ins_start_slot,
> +					 ins_nr, inode_only);
> +			if (ret) {
> +				err = ret;
> +				goto out_unlock;
> +			}
> +			ins_nr = 0;
>   		}
> -		ins_nr = 1;
> -		ins_start_slot = path->slots[0];
>   next_slot:
>
>   		nritems = btrfs_header_nritems(path->nodes[0]);
> @@ -2876,7 +2951,7 @@ next_slot:
>   			goto again;
>   		}
>   		if (ins_nr) {
> -			ret = copy_items(trans, log, dst_path, src,
> +			ret = copy_items(trans, inode, dst_path, src,
>   					 ins_start_slot,
>   					 ins_nr, inode_only);
>   			if (ret) {
> @@ -2897,7 +2972,7 @@ next_slot:
>   			break;
>   	}
>   	if (ins_nr) {
> -		ret = copy_items(trans, log, dst_path, src,
> +		ret = copy_items(trans, inode, dst_path, src,
>   				 ins_start_slot,
>   				 ins_nr, inode_only);
>   		if (ret) {


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 07/12 v3] Btrfs: add checksum check for log
  2011-06-21  8:49 ` [PATCH 07/12 v3] Btrfs: add checksum check for log Liu Bo
@ 2011-06-21 13:49   ` Josef Bacik
  0 siblings, 0 replies; 20+ messages in thread
From: Josef Bacik @ 2011-06-21 13:49 UTC (permalink / raw)
  To: Liu Bo; +Cc: linux-btrfs, dave, chris.mason

On 06/21/2011 04:49 AM, Liu Bo wrote:
> If a inode is a BTRFS_INODE_NODATASUM one, it need not to look for csum items
> any more.
>
> Signed-off-by: Liu Bo<liubo2009@cn.fujitsu.com>
> ---
>   fs/btrfs/tree-log.c |   13 ++++++++-----
>   1 files changed, 8 insertions(+), 5 deletions(-)
>
> diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c
> index 6193e27..1eca1d4 100644
> --- a/fs/btrfs/tree-log.c
> +++ b/fs/btrfs/tree-log.c
> @@ -2672,7 +2672,8 @@ static noinline int copy_items(struct btrfs_trans_handle *trans,
>   			       struct inode *inode,
>   			       struct btrfs_path *dst_path,
>   			       struct extent_buffer *src,
> -			       int start_slot, int nr, int inode_only)
> +			       int start_slot, int nr, int inode_only,
> +			       int csum)

Don't add another parameter, we're already passing in the inode, just 
check for nodatasum in copy_items instead of making all the callers do 
it.  Thanks,

Josef

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 10/12 v3] Btrfs: deal with EEXIST after iput
  2011-06-21  8:49 ` [PATCH 10/12 v3] Btrfs: deal with EEXIST after iput Liu Bo
@ 2011-06-21 14:00   ` Josef Bacik
  2011-06-22  1:57     ` liubo
  0 siblings, 1 reply; 20+ messages in thread
From: Josef Bacik @ 2011-06-21 14:00 UTC (permalink / raw)
  To: Liu Bo; +Cc: linux-btrfs, dave, chris.mason

On 06/21/2011 04:49 AM, Liu Bo wrote:
> There are two cases when BTRFS_I(inode)->logged_trans is zero:
> a) an inode is just allocated;
> b) iput an inode and reread it.
>
> However, in b) if btrfs is not committed yet, and this inode _may_ still remain
> in log tree.
>
> So we need to check the log tree to get logged_trans a right value
> in case it hits a EEXIST while logging.

Instead of doing this why not just check and see if the inode has been 
logged but the transaction has not yet been committed in 
btrfs_drop_inode?  That way the inode doesn't get evicted from cache 
until after we know it's ok and that way we don't have to waste a tree 
lookup.  Thanks,

Josef

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 10/12 v3] Btrfs: deal with EEXIST after iput
  2011-06-21 14:00   ` Josef Bacik
@ 2011-06-22  1:57     ` liubo
  0 siblings, 0 replies; 20+ messages in thread
From: liubo @ 2011-06-22  1:57 UTC (permalink / raw)
  To: Josef Bacik; +Cc: linux-btrfs, dave, chris.mason

On 06/21/2011 10:00 PM, Josef Bacik wrote:
> On 06/21/2011 04:49 AM, Liu Bo wrote:
>> There are two cases when BTRFS_I(inode)->logged_trans is zero:
>> a) an inode is just allocated;
>> b) iput an inode and reread it.
>>
>> However, in b) if btrfs is not committed yet, and this inode _may_
>> still remain
>> in log tree.
>>
>> So we need to check the log tree to get logged_trans a right value
>> in case it hits a EEXIST while logging.
> 
> Instead of doing this why not just check and see if the inode has been
> logged but the transaction has not yet been committed in
> btrfs_drop_inode?  That way the inode doesn't get evicted from cache
> until after we know it's ok and that way we don't have to waste a tree
> lookup.  Thanks,
> 

Good idea, I'll follow it.

thanks,
liubo

> Josef
> 


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [GIT PULL v3] Btrfs: improve write ahead log with sub transaction
  2011-06-21  8:49 [GIT PULL v3] Btrfs: improve write ahead log with sub transaction Liu Bo
                   ` (11 preceding siblings ...)
  2011-06-21  8:49 ` [PATCH 12/12 v3] Revert "Btrfs: do not flush csum items of unchanged file data during treelog" Liu Bo
@ 2011-08-04 13:57 ` Chris Mason
  2011-08-06  3:44   ` liubo
  12 siblings, 1 reply; 20+ messages in thread
From: Chris Mason @ 2011-08-04 13:57 UTC (permalink / raw)
  To: Liu Bo; +Cc: linux-btrfs, dave, josef

Excerpts from Liu Bo's message of 2011-06-21 04:49:41 -0400:
> I've been working to try to improve the write-ahead log's performance,
> and I found that the bottleneck addresses in the checksum items,
> especially when we want to make a random write on a large file, e.g a 4G file.

I spent some time last week on this code, because I really wanted to
be able to include it.  But I hit two problems.

Recording the transid of the log tree root doesn't completely solve
problems with later mounts expecting generation + 1.  If an older kernel
were to try and mount a log created by our new code, it wouldn't
understand the transid and the mount would fail.

I think we just need to force the transid of the root block to
generation + 1.  It is slightly less optimal but still much better than
what we have.

The second problem was that I consistently hit crashes during log replay
after a crash.  The test was just to use synctest:

http://oss.oracle.com/~mason/synctest/

synctest -t 32 -f -F -u -n 100 /mnt

I waited about 45 seconds and reset the machine.  Later mounts would
crash during log replay.

-chris

> 
> Then a idea for this suggested by Chris is to use sub transaction ids and just
> to log the part of inode that had changed since either the last log commit or
> the last transaction commit.  And as we also push the sub transid into the btree
> blocks, we'll get much faster tree walks.  As a result, we abandon the original
> brute force approach, which is "to delete all items of the inode in log",
> to making sure we get the most uptodate copies of everything, and instead
> we manage to "find and merge", i.e. finding extents in the log tree and merging
> in the new extents from the file.
> 
> This patchset puts the above idea into code, and although the code is now more
> complex, it brings us a great deal of performance improvement:
> 
> in my sysbench "write + fsync" test:
> 
>     451.01Kb/sec -> 4.3621Mb/sec
> 
> In v2, thanks to Chris, we worked together to solve 2 bugs, and after that it
> works as expected.
> 
> Since there are some vital changes in recent rc, like "kill trans_mutex" and
> "use cur_trans", as David asked, I rebase the patchset to the latest for-linus
> branch.
> 
> More tests are welcome!
> 
> You can also get this patchset from:
> 
>     git://repo.or.cz/linux-btrfs-devel.git sub-trans
> 
> Liu Bo (12):
>   Btrfs: introduce sub transaction stuff
>   Btrfs: update block generation if should_cow_block fails
>   Btrfs: modify btrfs_drop_extents API
>   Btrfs: introduce first sub trans
>   Btrfs: still update inode trans stuff when size remains unchanged
>   Btrfs: improve log with sub transaction
>   Btrfs: add checksum check for log
>   Btrfs: fix a bug of log check
>   Btrfs: kick off useless code
>   Btrfs: deal with EEXIST after iput
>   Btrfs: use the right generation number to read log_root_tree
>   Revert "Btrfs: do not flush csum items of unchanged file data during
>     treelog"
> 
>  fs/btrfs/btrfs_inode.h |   12 ++-
>  fs/btrfs/ctree.c       |   69 +++++++++---
>  fs/btrfs/ctree.h       |    5 +-
>  fs/btrfs/disk-io.c     |   12 +-
>  fs/btrfs/extent-tree.c |   10 +-
>  fs/btrfs/file.c        |   22 ++---
>  fs/btrfs/inode.c       |   33 ++++---
>  fs/btrfs/ioctl.c       |    6 +-
>  fs/btrfs/relocation.c  |    6 +-
>  fs/btrfs/transaction.c |   14 ++-
>  fs/btrfs/transaction.h |   19 +++-
>  fs/btrfs/tree-defrag.c |    2 +-
>  fs/btrfs/tree-log.c    |  272 ++++++++++++++++++++++++++++++++++-------------
>  13 files changed, 331 insertions(+), 151 deletions(-)

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [GIT PULL v3] Btrfs: improve write ahead log with sub transaction
  2011-08-04 13:57 ` [GIT PULL v3] Btrfs: improve write ahead log with sub transaction Chris Mason
@ 2011-08-06  3:44   ` liubo
  2011-08-06  9:23     ` liubo
  0 siblings, 1 reply; 20+ messages in thread
From: liubo @ 2011-08-06  3:44 UTC (permalink / raw)
  To: Chris Mason; +Cc: linux-btrfs, dave, josef

Hi, Chris,

On 08/04/2011 09:57 PM, Chris Mason wrote:
> Excerpts from Liu Bo's message of 2011-06-21 04:49:41 -0400:
>> I've been working to try to improve the write-ahead log's performance,
>> and I found that the bottleneck addresses in the checksum items,
>> especially when we want to make a random write on a large file, e.g a 4G file.
> 
> I spent some time last week on this code, because I really wanted to
> be able to include it.  But I hit two problems.
> 
> Recording the transid of the log tree root doesn't completely solve
> problems with later mounts expecting generation + 1.  If an older kernel
> were to try and mount a log created by our new code, it wouldn't
> understand the transid and the mount would fail.
> 
> I think we just need to force the transid of the root block to
> generation + 1.  It is slightly less optimal but still much better than
> what we have.
> 


ohh, I forgot to fix this, sorry.


> The second problem was that I consistently hit crashes during log replay
> after a crash.  The test was just to use synctest:
> 
> http://oss.oracle.com/~mason/synctest/
> 
> synctest -t 32 -f -F -u -n 100 /mnt
> 
> I waited about 45 seconds and reset the machine.  Later mounts would
> crash during log replay.
> 


I've reproduced as your tips and hit a crash.

But I'm not sure if the following bug is just what is happening on your box?

And if it is, it is a bug from the original add_inode_ref() code, and I've been working it out.
Otherwise, if your crash is _another_ bug, plz let me know ASAP.

------------[ cut here ]------------
kernel BUG at fs/btrfs/inode.c:4580!
Pid: 2124, comm: mount Not tainted 3.0.0-for-linus+ #13 LENOVO QiTianM7150/To be filled by O.E.M.
RIP: 0010:[<ffffffffa03df251>]  [<ffffffffa03df251>] btrfs_add_link+0x161/0x1c0 [btrfs]
[...]
Call Trace:
 [<ffffffffa03e7b31>] ? btrfs_inode_ref_index+0x31/0x80 [btrfs]
 [<ffffffffa04054e9>] add_inode_ref+0x319/0x3f0 [btrfs]
 [<ffffffffa0407087>] replay_one_buffer+0x2c7/0x390 [btrfs]
 [<ffffffffa040444a>] walk_down_log_tree+0x32a/0x480 [btrfs]
 [<ffffffffa0404695>] walk_log_tree+0xf5/0x240 [btrfs]
 [<ffffffffa0406cc0>] btrfs_recover_log_trees+0x250/0x350 [btrfs]
 [<ffffffffa0406dc0>] ? btrfs_recover_log_trees+0x350/0x350 [btrfs]
 [<ffffffffa03d18b2>] open_ctree+0x1442/0x17d0 [btrfs]
 [<ffffffff811aad34>] ? disk_name+0x64/0xc0
 [<ffffffffa03afd9e>] btrfs_mount+0x3de/0x570 [btrfs]
 [<ffffffff81149293>] mount_fs+0x43/0x1a0
 [<ffffffff8110f920>] ? __alloc_percpu+0x10/0x20
 [<ffffffff81163873>] vfs_kern_mount+0x63/0xd0
 [<ffffffff81163952>] do_kern_mount+0x52/0x110
 [<ffffffff811d226a>] ? security_capable+0x2a/0x30
 [<ffffffff81165597>] do_mount+0x257/0x7e0
 [<ffffffff8110a25b>] ? memdup_user+0x4b/0x90
 [<ffffffff8110a2fb>] ? strndup_user+0x5b/0x80
 [<ffffffff81165bb0>] sys_mount+0x90/0xe0
 [<ffffffff8147142b>] system_call_fastpath+0x16/0x1b


thanks,
liubo

> -chris
> 
>> Then a idea for this suggested by Chris is to use sub transaction ids and just
>> to log the part of inode that had changed since either the last log commit or
>> the last transaction commit.  And as we also push the sub transid into the btree
>> blocks, we'll get much faster tree walks.  As a result, we abandon the original
>> brute force approach, which is "to delete all items of the inode in log",
>> to making sure we get the most uptodate copies of everything, and instead
>> we manage to "find and merge", i.e. finding extents in the log tree and merging
>> in the new extents from the file.
>>
>> This patchset puts the above idea into code, and although the code is now more
>> complex, it brings us a great deal of performance improvement:
>>
>> in my sysbench "write + fsync" test:
>>
>>     451.01Kb/sec -> 4.3621Mb/sec
>>
>> In v2, thanks to Chris, we worked together to solve 2 bugs, and after that it
>> works as expected.
>>
>> Since there are some vital changes in recent rc, like "kill trans_mutex" and
>> "use cur_trans", as David asked, I rebase the patchset to the latest for-linus
>> branch.
>>
>> More tests are welcome!
>>
>> You can also get this patchset from:
>>
>>     git://repo.or.cz/linux-btrfs-devel.git sub-trans
>>
>> Liu Bo (12):
>>   Btrfs: introduce sub transaction stuff
>>   Btrfs: update block generation if should_cow_block fails
>>   Btrfs: modify btrfs_drop_extents API
>>   Btrfs: introduce first sub trans
>>   Btrfs: still update inode trans stuff when size remains unchanged
>>   Btrfs: improve log with sub transaction
>>   Btrfs: add checksum check for log
>>   Btrfs: fix a bug of log check
>>   Btrfs: kick off useless code
>>   Btrfs: deal with EEXIST after iput
>>   Btrfs: use the right generation number to read log_root_tree
>>   Revert "Btrfs: do not flush csum items of unchanged file data during
>>     treelog"
>>
>>  fs/btrfs/btrfs_inode.h |   12 ++-
>>  fs/btrfs/ctree.c       |   69 +++++++++---
>>  fs/btrfs/ctree.h       |    5 +-
>>  fs/btrfs/disk-io.c     |   12 +-
>>  fs/btrfs/extent-tree.c |   10 +-
>>  fs/btrfs/file.c        |   22 ++---
>>  fs/btrfs/inode.c       |   33 ++++---
>>  fs/btrfs/ioctl.c       |    6 +-
>>  fs/btrfs/relocation.c  |    6 +-
>>  fs/btrfs/transaction.c |   14 ++-
>>  fs/btrfs/transaction.h |   19 +++-
>>  fs/btrfs/tree-defrag.c |    2 +-
>>  fs/btrfs/tree-log.c    |  272 ++++++++++++++++++++++++++++++++++-------------
>>  13 files changed, 331 insertions(+), 151 deletions(-)
> --
> To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> 


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [GIT PULL v3] Btrfs: improve write ahead log with sub transaction
  2011-08-06  3:44   ` liubo
@ 2011-08-06  9:23     ` liubo
  0 siblings, 0 replies; 20+ messages in thread
From: liubo @ 2011-08-06  9:23 UTC (permalink / raw)
  To: Chris Mason; +Cc: linux-btrfs, dave, josef

On 08/06/2011 11:44 AM, liubo wrote:
> Hi, Chris,
> 
> On 08/04/2011 09:57 PM, Chris Mason wrote:
>> Excerpts from Liu Bo's message of 2011-06-21 04:49:41 -0400:
>>> I've been working to try to improve the write-ahead log's performance,
>>> and I found that the bottleneck addresses in the checksum items,
>>> especially when we want to make a random write on a large file, e.g a 4G file.
>> I spent some time last week on this code, because I really wanted to
>> be able to include it.  But I hit two problems.
>>
>> Recording the transid of the log tree root doesn't completely solve
>> problems with later mounts expecting generation + 1.  If an older kernel
>> were to try and mount a log created by our new code, it wouldn't
>> understand the transid and the mount would fail.
>>
>> I think we just need to force the transid of the root block to
>> generation + 1.  It is slightly less optimal but still much better than
>> what we have.
>>
> 
> 
> ohh, I forgot to fix this, sorry.
> 
> 
>> The second problem was that I consistently hit crashes during log replay
>> after a crash.  The test was just to use synctest:
>>
>> http://oss.oracle.com/~mason/synctest/
>>
>> synctest -t 32 -f -F -u -n 100 /mnt
>>
>> I waited about 45 seconds and reset the machine.  Later mounts would
>> crash during log replay.
>>
> 
> 
> I've reproduced as your tips and hit a crash.
> 
> But I'm not sure if the following bug is just what is happening on your box?
> 
> And if it is, it is a bug from the original add_inode_ref() code, and I've been working it out.


I've just posted a patch to solve this:

[PATCH] Btrfs: fix an oops of log replay

This patch should be helpful then.

thanks,
liubo

> Otherwise, if your crash is _another_ bug, plz let me know ASAP.
> 
> ------------[ cut here ]------------
> kernel BUG at fs/btrfs/inode.c:4580!
> Pid: 2124, comm: mount Not tainted 3.0.0-for-linus+ #13 LENOVO QiTianM7150/To be filled by O.E.M.
> RIP: 0010:[<ffffffffa03df251>]  [<ffffffffa03df251>] btrfs_add_link+0x161/0x1c0 [btrfs]
> [...]
> Call Trace:
>  [<ffffffffa03e7b31>] ? btrfs_inode_ref_index+0x31/0x80 [btrfs]
>  [<ffffffffa04054e9>] add_inode_ref+0x319/0x3f0 [btrfs]
>  [<ffffffffa0407087>] replay_one_buffer+0x2c7/0x390 [btrfs]
>  [<ffffffffa040444a>] walk_down_log_tree+0x32a/0x480 [btrfs]
>  [<ffffffffa0404695>] walk_log_tree+0xf5/0x240 [btrfs]
>  [<ffffffffa0406cc0>] btrfs_recover_log_trees+0x250/0x350 [btrfs]
>  [<ffffffffa0406dc0>] ? btrfs_recover_log_trees+0x350/0x350 [btrfs]
>  [<ffffffffa03d18b2>] open_ctree+0x1442/0x17d0 [btrfs]
>  [<ffffffff811aad34>] ? disk_name+0x64/0xc0
>  [<ffffffffa03afd9e>] btrfs_mount+0x3de/0x570 [btrfs]
>  [<ffffffff81149293>] mount_fs+0x43/0x1a0
>  [<ffffffff8110f920>] ? __alloc_percpu+0x10/0x20
>  [<ffffffff81163873>] vfs_kern_mount+0x63/0xd0
>  [<ffffffff81163952>] do_kern_mount+0x52/0x110
>  [<ffffffff811d226a>] ? security_capable+0x2a/0x30
>  [<ffffffff81165597>] do_mount+0x257/0x7e0
>  [<ffffffff8110a25b>] ? memdup_user+0x4b/0x90
>  [<ffffffff8110a2fb>] ? strndup_user+0x5b/0x80
>  [<ffffffff81165bb0>] sys_mount+0x90/0xe0
>  [<ffffffff8147142b>] system_call_fastpath+0x16/0x1b
> 
> 
> thanks,
> liubo
> 
>> -chris
>>
>>> Then a idea for this suggested by Chris is to use sub transaction ids and just
>>> to log the part of inode that had changed since either the last log commit or
>>> the last transaction commit.  And as we also push the sub transid into the btree
>>> blocks, we'll get much faster tree walks.  As a result, we abandon the original
>>> brute force approach, which is "to delete all items of the inode in log",
>>> to making sure we get the most uptodate copies of everything, and instead
>>> we manage to "find and merge", i.e. finding extents in the log tree and merging
>>> in the new extents from the file.
>>>
>>> This patchset puts the above idea into code, and although the code is now more
>>> complex, it brings us a great deal of performance improvement:
>>>
>>> in my sysbench "write + fsync" test:
>>>
>>>     451.01Kb/sec -> 4.3621Mb/sec
>>>
>>> In v2, thanks to Chris, we worked together to solve 2 bugs, and after that it
>>> works as expected.
>>>
>>> Since there are some vital changes in recent rc, like "kill trans_mutex" and
>>> "use cur_trans", as David asked, I rebase the patchset to the latest for-linus
>>> branch.
>>>
>>> More tests are welcome!
>>>
>>> You can also get this patchset from:
>>>
>>>     git://repo.or.cz/linux-btrfs-devel.git sub-trans
>>>
>>> Liu Bo (12):
>>>   Btrfs: introduce sub transaction stuff
>>>   Btrfs: update block generation if should_cow_block fails
>>>   Btrfs: modify btrfs_drop_extents API
>>>   Btrfs: introduce first sub trans
>>>   Btrfs: still update inode trans stuff when size remains unchanged
>>>   Btrfs: improve log with sub transaction
>>>   Btrfs: add checksum check for log
>>>   Btrfs: fix a bug of log check
>>>   Btrfs: kick off useless code
>>>   Btrfs: deal with EEXIST after iput
>>>   Btrfs: use the right generation number to read log_root_tree
>>>   Revert "Btrfs: do not flush csum items of unchanged file data during
>>>     treelog"
>>>
>>>  fs/btrfs/btrfs_inode.h |   12 ++-
>>>  fs/btrfs/ctree.c       |   69 +++++++++---
>>>  fs/btrfs/ctree.h       |    5 +-
>>>  fs/btrfs/disk-io.c     |   12 +-
>>>  fs/btrfs/extent-tree.c |   10 +-
>>>  fs/btrfs/file.c        |   22 ++---
>>>  fs/btrfs/inode.c       |   33 ++++---
>>>  fs/btrfs/ioctl.c       |    6 +-
>>>  fs/btrfs/relocation.c  |    6 +-
>>>  fs/btrfs/transaction.c |   14 ++-
>>>  fs/btrfs/transaction.h |   19 +++-
>>>  fs/btrfs/tree-defrag.c |    2 +-
>>>  fs/btrfs/tree-log.c    |  272 ++++++++++++++++++++++++++++++++++-------------
>>>  13 files changed, 331 insertions(+), 151 deletions(-)
>> --
>> To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
>> the body of a message to majordomo@vger.kernel.org
>> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>>
> 
> --
> To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> 


^ permalink raw reply	[flat|nested] 20+ messages in thread

end of thread, other threads:[~2011-08-06  9:23 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-06-21  8:49 [GIT PULL v3] Btrfs: improve write ahead log with sub transaction Liu Bo
2011-06-21  8:49 ` [PATCH 01/12 v3] Btrfs: introduce sub transaction stuff Liu Bo
2011-06-21  8:49 ` [PATCH 02/12 v3] Btrfs: update block generation if should_cow_block fails Liu Bo
2011-06-21  8:49 ` [PATCH 03/12 v3] Btrfs: modify btrfs_drop_extents API Liu Bo
2011-06-21  8:49 ` [PATCH 04/12 v3] Btrfs: introduce first sub trans Liu Bo
2011-06-21  8:49 ` [PATCH 05/12 v3] Btrfs: still update inode trans stuff when size remains unchanged Liu Bo
2011-06-21  8:49 ` [PATCH 06/12 v3] Btrfs: improve log with sub transaction Liu Bo
2011-06-21 13:46   ` Josef Bacik
2011-06-21  8:49 ` [PATCH 07/12 v3] Btrfs: add checksum check for log Liu Bo
2011-06-21 13:49   ` Josef Bacik
2011-06-21  8:49 ` [PATCH 08/12 v3] Btrfs: fix a bug of log check Liu Bo
2011-06-21  8:49 ` [PATCH 09/12 v3] Btrfs: kick off useless code Liu Bo
2011-06-21  8:49 ` [PATCH 10/12 v3] Btrfs: deal with EEXIST after iput Liu Bo
2011-06-21 14:00   ` Josef Bacik
2011-06-22  1:57     ` liubo
2011-06-21  8:49 ` [PATCH 11/12 v3] Btrfs: use the right generation number to read log_root_tree Liu Bo
2011-06-21  8:49 ` [PATCH 12/12 v3] Revert "Btrfs: do not flush csum items of unchanged file data during treelog" Liu Bo
2011-08-04 13:57 ` [GIT PULL v3] Btrfs: improve write ahead log with sub transaction Chris Mason
2011-08-06  3:44   ` liubo
2011-08-06  9:23     ` liubo

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.