aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs/transaction.c
diff options
context:
space:
mode:
authorMiao Xie <miaox@cn.fujitsu.com>2011-04-22 06:12:22 -0400
committerChris Mason <chris.mason@oracle.com>2011-05-21 09:30:56 -0400
commit16cdcec736cd214350cdb591bf1091f8beedefa0 (patch)
tree5598d4561660c4d7a1d4de8b3703d6dd3cc7f9e7 /fs/btrfs/transaction.c
parent61c4f2c81c61f73549928dfd9f3e8f26aa36a8cf (diff)
btrfs: implement delayed inode items operation
Changelog V5 -> V6: - Fix oom when the memory load is high, by storing the delayed nodes into the root's radix tree, and letting btrfs inodes go. Changelog V4 -> V5: - Fix the race on adding the delayed node to the inode, which is spotted by Chris Mason. - Merge Chris Mason's incremental patch into this patch. - Fix deadlock between readdir() and memory fault, which is reported by Itaru Kitayama. Changelog V3 -> V4: - Fix nested lock, which is reported by Itaru Kitayama, by updating space cache inode in time. Changelog V2 -> V3: - Fix the race between the delayed worker and the task which does delayed items balance, which is reported by Tsutomu Itoh. - Modify the patch address David Sterba's comment. - Fix the bug of the cpu recursion spinlock, reported by Chris Mason Changelog V1 -> V2: - break up the global rb-tree, use a list to manage the delayed nodes, which is created for every directory and file, and used to manage the delayed directory name index items and the delayed inode item. - introduce a worker to deal with the delayed nodes. Compare with Ext3/4, the performance of file creation and deletion on btrfs is very poor. the reason is that btrfs must do a lot of b+ tree insertions, such as inode item, directory name item, directory name index and so on. If we can do some delayed b+ tree insertion or deletion, we can improve the performance, so we made this patch which implemented delayed directory name index insertion/deletion and delayed inode update. Implementation: - introduce a delayed root object into the filesystem, that use two lists to manage the delayed nodes which are created for every file/directory. One is used to manage all the delayed nodes that have delayed items. And the other is used to manage the delayed nodes which is waiting to be dealt with by the work thread. - Every delayed node has two rb-tree, one is used to manage the directory name index which is going to be inserted into b+ tree, and the other is used to manage the directory name index which is going to be deleted from b+ tree. - introduce a worker to deal with the delayed operation. This worker is used to deal with the works of the delayed directory name index items insertion and deletion and the delayed inode update. When the delayed items is beyond the lower limit, we create works for some delayed nodes and insert them into the work queue of the worker, and then go back. When the delayed items is beyond the upper bound, we create works for all the delayed nodes that haven't been dealt with, and insert them into the work queue of the worker, and then wait for that the untreated items is below some threshold value. - When we want to insert a directory name index into b+ tree, we just add the information into the delayed inserting rb-tree. And then we check the number of the delayed items and do delayed items balance. (The balance policy is above.) - When we want to delete a directory name index from the b+ tree, we search it in the inserting rb-tree at first. If we look it up, just drop it. If not, add the key of it into the delayed deleting rb-tree. Similar to the delayed inserting rb-tree, we also check the number of the delayed items and do delayed items balance. (The same to inserting manipulation) - When we want to update the metadata of some inode, we cached the data of the inode into the delayed node. the worker will flush it into the b+ tree after dealing with the delayed insertion and deletion. - We will move the delayed node to the tail of the list after we access the delayed node, By this way, we can cache more delayed items and merge more inode updates. - If we want to commit transaction, we will deal with all the delayed node. - the delayed node will be freed when we free the btrfs inode. - Before we log the inode items, we commit all the directory name index items and the delayed inode update. I did a quick test by the benchmark tool[1] and found we can improve the performance of file creation by ~15%, and file deletion by ~20%. Before applying this patch: Create files: Total files: 50000 Total time: 1.096108 Average time: 0.000022 Delete files: Total files: 50000 Total time: 1.510403 Average time: 0.000030 After applying this patch: Create files: Total files: 50000 Total time: 0.932899 Average time: 0.000019 Delete files: Total files: 50000 Total time: 1.215732 Average time: 0.000024 [1] http://marc.info/?l=linux-btrfs&m=128212635122920&q=p3 Many thanks for Kitayama-san's help! Signed-off-by: Miao Xie <miaox@cn.fujitsu.com> Reviewed-by: David Sterba <dave@jikos.cz> Tested-by: Tsutomu Itoh <t-itoh@jp.fujitsu.com> Tested-by: Itaru Kitayama <kitayama@cl.bb4u.ne.jp> Signed-off-by: Chris Mason <chris.mason@oracle.com>
Diffstat (limited to 'fs/btrfs/transaction.c')
-rw-r--r--fs/btrfs/transaction.c45
1 files changed, 41 insertions, 4 deletions
diff --git a/fs/btrfs/transaction.c b/fs/btrfs/transaction.c
index c571734d5e5a..b83ed5e64a32 100644
--- a/fs/btrfs/transaction.c
+++ b/fs/btrfs/transaction.c
@@ -487,19 +487,40 @@ static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
487int btrfs_end_transaction(struct btrfs_trans_handle *trans, 487int btrfs_end_transaction(struct btrfs_trans_handle *trans,
488 struct btrfs_root *root) 488 struct btrfs_root *root)
489{ 489{
490 return __btrfs_end_transaction(trans, root, 0, 1); 490 int ret;
491
492 ret = __btrfs_end_transaction(trans, root, 0, 1);
493 if (ret)
494 return ret;
495 return 0;
491} 496}
492 497
493int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans, 498int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
494 struct btrfs_root *root) 499 struct btrfs_root *root)
495{ 500{
496 return __btrfs_end_transaction(trans, root, 1, 1); 501 int ret;
502
503 ret = __btrfs_end_transaction(trans, root, 1, 1);
504 if (ret)
505 return ret;
506 return 0;
497} 507}
498 508
499int btrfs_end_transaction_nolock(struct btrfs_trans_handle *trans, 509int btrfs_end_transaction_nolock(struct btrfs_trans_handle *trans,
500 struct btrfs_root *root) 510 struct btrfs_root *root)
501{ 511{
502 return __btrfs_end_transaction(trans, root, 0, 0); 512 int ret;
513
514 ret = __btrfs_end_transaction(trans, root, 0, 0);
515 if (ret)
516 return ret;
517 return 0;
518}
519
520int btrfs_end_transaction_dmeta(struct btrfs_trans_handle *trans,
521 struct btrfs_root *root)
522{
523 return __btrfs_end_transaction(trans, root, 1, 1);
503} 524}
504 525
505/* 526/*
@@ -967,7 +988,7 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
967 BUG_ON(ret); 988 BUG_ON(ret);
968 ret = btrfs_insert_dir_item(trans, parent_root, 989 ret = btrfs_insert_dir_item(trans, parent_root,
969 dentry->d_name.name, dentry->d_name.len, 990 dentry->d_name.name, dentry->d_name.len,
970 parent_inode->i_ino, &key, 991 parent_inode, &key,
971 BTRFS_FT_DIR, index); 992 BTRFS_FT_DIR, index);
972 BUG_ON(ret); 993 BUG_ON(ret);
973 994
@@ -1037,6 +1058,14 @@ static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
1037 int ret; 1058 int ret;
1038 1059
1039 list_for_each_entry(pending, head, list) { 1060 list_for_each_entry(pending, head, list) {
1061 /*
1062 * We must deal with the delayed items before creating
1063 * snapshots, or we will create a snapthot with inconsistent
1064 * information.
1065 */
1066 ret = btrfs_run_delayed_items(trans, fs_info->fs_root);
1067 BUG_ON(ret);
1068
1040 ret = create_pending_snapshot(trans, fs_info, pending); 1069 ret = create_pending_snapshot(trans, fs_info, pending);
1041 BUG_ON(ret); 1070 BUG_ON(ret);
1042 } 1071 }
@@ -1290,6 +1319,9 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
1290 BUG_ON(ret); 1319 BUG_ON(ret);
1291 } 1320 }
1292 1321
1322 ret = btrfs_run_delayed_items(trans, root);
1323 BUG_ON(ret);
1324
1293 /* 1325 /*
1294 * rename don't use btrfs_join_transaction, so, once we 1326 * rename don't use btrfs_join_transaction, so, once we
1295 * set the transaction to blocked above, we aren't going 1327 * set the transaction to blocked above, we aren't going
@@ -1316,6 +1348,9 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
1316 ret = create_pending_snapshots(trans, root->fs_info); 1348 ret = create_pending_snapshots(trans, root->fs_info);
1317 BUG_ON(ret); 1349 BUG_ON(ret);
1318 1350
1351 ret = btrfs_run_delayed_items(trans, root);
1352 BUG_ON(ret);
1353
1319 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 1354 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
1320 BUG_ON(ret); 1355 BUG_ON(ret);
1321 1356
@@ -1432,6 +1467,8 @@ int btrfs_clean_old_snapshots(struct btrfs_root *root)
1432 root = list_entry(list.next, struct btrfs_root, root_list); 1467 root = list_entry(list.next, struct btrfs_root, root_list);
1433 list_del(&root->root_list); 1468 list_del(&root->root_list);
1434 1469
1470 btrfs_kill_all_delayed_nodes(root);
1471
1435 if (btrfs_header_backref_rev(root->node) < 1472 if (btrfs_header_backref_rev(root->node) <
1436 BTRFS_MIXED_BACKREF_REV) 1473 BTRFS_MIXED_BACKREF_REV)
1437 btrfs_drop_snapshot(root, NULL, 0); 1474 btrfs_drop_snapshot(root, NULL, 0);