aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs/transaction.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/btrfs/transaction.c')
-rw-r--r--fs/btrfs/transaction.c162
1 files changed, 64 insertions, 98 deletions
diff --git a/fs/btrfs/transaction.c b/fs/btrfs/transaction.c
index c207e8c32c9b..2cb116099b90 100644
--- a/fs/btrfs/transaction.c
+++ b/fs/btrfs/transaction.c
@@ -17,6 +17,7 @@
17 */ 17 */
18 18
19#include <linux/fs.h> 19#include <linux/fs.h>
20#include <linux/slab.h>
20#include <linux/sched.h> 21#include <linux/sched.h>
21#include <linux/writeback.h> 22#include <linux/writeback.h>
22#include <linux/pagemap.h> 23#include <linux/pagemap.h>
@@ -69,7 +70,7 @@ static noinline int join_transaction(struct btrfs_root *root)
69 cur_trans->commit_done = 0; 70 cur_trans->commit_done = 0;
70 cur_trans->start_time = get_seconds(); 71 cur_trans->start_time = get_seconds();
71 72
72 cur_trans->delayed_refs.root.rb_node = NULL; 73 cur_trans->delayed_refs.root = RB_ROOT;
73 cur_trans->delayed_refs.num_entries = 0; 74 cur_trans->delayed_refs.num_entries = 0;
74 cur_trans->delayed_refs.num_heads_ready = 0; 75 cur_trans->delayed_refs.num_heads_ready = 0;
75 cur_trans->delayed_refs.num_heads = 0; 76 cur_trans->delayed_refs.num_heads = 0;
@@ -147,18 +148,13 @@ static void wait_current_trans(struct btrfs_root *root)
147 while (1) { 148 while (1) {
148 prepare_to_wait(&root->fs_info->transaction_wait, &wait, 149 prepare_to_wait(&root->fs_info->transaction_wait, &wait,
149 TASK_UNINTERRUPTIBLE); 150 TASK_UNINTERRUPTIBLE);
150 if (cur_trans->blocked) { 151 if (!cur_trans->blocked)
151 mutex_unlock(&root->fs_info->trans_mutex);
152 schedule();
153 mutex_lock(&root->fs_info->trans_mutex);
154 finish_wait(&root->fs_info->transaction_wait,
155 &wait);
156 } else {
157 finish_wait(&root->fs_info->transaction_wait,
158 &wait);
159 break; 152 break;
160 } 153 mutex_unlock(&root->fs_info->trans_mutex);
154 schedule();
155 mutex_lock(&root->fs_info->trans_mutex);
161 } 156 }
157 finish_wait(&root->fs_info->transaction_wait, &wait);
162 put_transaction(cur_trans); 158 put_transaction(cur_trans);
163 } 159 }
164} 160}
@@ -333,6 +329,9 @@ static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
333 memset(trans, 0, sizeof(*trans)); 329 memset(trans, 0, sizeof(*trans));
334 kmem_cache_free(btrfs_trans_handle_cachep, trans); 330 kmem_cache_free(btrfs_trans_handle_cachep, trans);
335 331
332 if (throttle)
333 btrfs_run_delayed_iputs(root);
334
336 return 0; 335 return 0;
337} 336}
338 337
@@ -354,7 +353,7 @@ int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
354 * those extents are sent to disk but does not wait on them 353 * those extents are sent to disk but does not wait on them
355 */ 354 */
356int btrfs_write_marked_extents(struct btrfs_root *root, 355int btrfs_write_marked_extents(struct btrfs_root *root,
357 struct extent_io_tree *dirty_pages) 356 struct extent_io_tree *dirty_pages, int mark)
358{ 357{
359 int ret; 358 int ret;
360 int err = 0; 359 int err = 0;
@@ -367,7 +366,7 @@ int btrfs_write_marked_extents(struct btrfs_root *root,
367 366
368 while (1) { 367 while (1) {
369 ret = find_first_extent_bit(dirty_pages, start, &start, &end, 368 ret = find_first_extent_bit(dirty_pages, start, &start, &end,
370 EXTENT_DIRTY); 369 mark);
371 if (ret) 370 if (ret)
372 break; 371 break;
373 while (start <= end) { 372 while (start <= end) {
@@ -413,7 +412,7 @@ int btrfs_write_marked_extents(struct btrfs_root *root,
413 * on all the pages and clear them from the dirty pages state tree 412 * on all the pages and clear them from the dirty pages state tree
414 */ 413 */
415int btrfs_wait_marked_extents(struct btrfs_root *root, 414int btrfs_wait_marked_extents(struct btrfs_root *root,
416 struct extent_io_tree *dirty_pages) 415 struct extent_io_tree *dirty_pages, int mark)
417{ 416{
418 int ret; 417 int ret;
419 int err = 0; 418 int err = 0;
@@ -425,12 +424,12 @@ int btrfs_wait_marked_extents(struct btrfs_root *root,
425 unsigned long index; 424 unsigned long index;
426 425
427 while (1) { 426 while (1) {
428 ret = find_first_extent_bit(dirty_pages, 0, &start, &end, 427 ret = find_first_extent_bit(dirty_pages, start, &start, &end,
429 EXTENT_DIRTY); 428 mark);
430 if (ret) 429 if (ret)
431 break; 430 break;
432 431
433 clear_extent_dirty(dirty_pages, start, end, GFP_NOFS); 432 clear_extent_bits(dirty_pages, start, end, mark, GFP_NOFS);
434 while (start <= end) { 433 while (start <= end) {
435 index = start >> PAGE_CACHE_SHIFT; 434 index = start >> PAGE_CACHE_SHIFT;
436 start = (u64)(index + 1) << PAGE_CACHE_SHIFT; 435 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
@@ -460,13 +459,13 @@ int btrfs_wait_marked_extents(struct btrfs_root *root,
460 * those extents are on disk for transaction or log commit 459 * those extents are on disk for transaction or log commit
461 */ 460 */
462int btrfs_write_and_wait_marked_extents(struct btrfs_root *root, 461int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
463 struct extent_io_tree *dirty_pages) 462 struct extent_io_tree *dirty_pages, int mark)
464{ 463{
465 int ret; 464 int ret;
466 int ret2; 465 int ret2;
467 466
468 ret = btrfs_write_marked_extents(root, dirty_pages); 467 ret = btrfs_write_marked_extents(root, dirty_pages, mark);
469 ret2 = btrfs_wait_marked_extents(root, dirty_pages); 468 ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark);
470 return ret || ret2; 469 return ret || ret2;
471} 470}
472 471
@@ -479,7 +478,8 @@ int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
479 return filemap_write_and_wait(btree_inode->i_mapping); 478 return filemap_write_and_wait(btree_inode->i_mapping);
480 } 479 }
481 return btrfs_write_and_wait_marked_extents(root, 480 return btrfs_write_and_wait_marked_extents(root,
482 &trans->transaction->dirty_pages); 481 &trans->transaction->dirty_pages,
482 EXTENT_DIRTY);
483} 483}
484 484
485/* 485/*
@@ -497,13 +497,16 @@ static int update_cowonly_root(struct btrfs_trans_handle *trans,
497{ 497{
498 int ret; 498 int ret;
499 u64 old_root_bytenr; 499 u64 old_root_bytenr;
500 u64 old_root_used;
500 struct btrfs_root *tree_root = root->fs_info->tree_root; 501 struct btrfs_root *tree_root = root->fs_info->tree_root;
501 502
503 old_root_used = btrfs_root_used(&root->root_item);
502 btrfs_write_dirty_block_groups(trans, root); 504 btrfs_write_dirty_block_groups(trans, root);
503 505
504 while (1) { 506 while (1) {
505 old_root_bytenr = btrfs_root_bytenr(&root->root_item); 507 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
506 if (old_root_bytenr == root->node->start) 508 if (old_root_bytenr == root->node->start &&
509 old_root_used == btrfs_root_used(&root->root_item))
507 break; 510 break;
508 511
509 btrfs_set_root_node(&root->root_item, root->node); 512 btrfs_set_root_node(&root->root_item, root->node);
@@ -512,6 +515,7 @@ static int update_cowonly_root(struct btrfs_trans_handle *trans,
512 &root->root_item); 515 &root->root_item);
513 BUG_ON(ret); 516 BUG_ON(ret);
514 517
518 old_root_used = btrfs_root_used(&root->root_item);
515 ret = btrfs_write_dirty_block_groups(trans, root); 519 ret = btrfs_write_dirty_block_groups(trans, root);
516 BUG_ON(ret); 520 BUG_ON(ret);
517 } 521 }
@@ -752,10 +756,17 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
752 struct btrfs_root_item *new_root_item; 756 struct btrfs_root_item *new_root_item;
753 struct btrfs_root *tree_root = fs_info->tree_root; 757 struct btrfs_root *tree_root = fs_info->tree_root;
754 struct btrfs_root *root = pending->root; 758 struct btrfs_root *root = pending->root;
759 struct btrfs_root *parent_root;
760 struct inode *parent_inode;
755 struct extent_buffer *tmp; 761 struct extent_buffer *tmp;
756 struct extent_buffer *old; 762 struct extent_buffer *old;
757 int ret; 763 int ret;
758 u64 objectid; 764 u64 objectid;
765 int namelen;
766 u64 index = 0;
767
768 parent_inode = pending->dentry->d_parent->d_inode;
769 parent_root = BTRFS_I(parent_inode)->root;
759 770
760 new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS); 771 new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
761 if (!new_root_item) { 772 if (!new_root_item) {
@@ -766,83 +777,59 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
766 if (ret) 777 if (ret)
767 goto fail; 778 goto fail;
768 779
769 record_root_in_trans(trans, root);
770 btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
771 memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
772
773 key.objectid = objectid; 780 key.objectid = objectid;
774 /* record when the snapshot was created in key.offset */ 781 /* record when the snapshot was created in key.offset */
775 key.offset = trans->transid; 782 key.offset = trans->transid;
776 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); 783 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
777 784
778 old = btrfs_lock_root_node(root);
779 btrfs_cow_block(trans, root, old, NULL, 0, &old);
780 btrfs_set_lock_blocking(old);
781
782 btrfs_copy_root(trans, root, old, &tmp, objectid);
783 btrfs_tree_unlock(old);
784 free_extent_buffer(old);
785
786 btrfs_set_root_node(new_root_item, tmp);
787 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
788 new_root_item);
789 btrfs_tree_unlock(tmp);
790 free_extent_buffer(tmp);
791 if (ret)
792 goto fail;
793
794 key.offset = (u64)-1;
795 memcpy(&pending->root_key, &key, sizeof(key)); 785 memcpy(&pending->root_key, &key, sizeof(key));
796fail: 786 pending->root_key.offset = (u64)-1;
797 kfree(new_root_item);
798 btrfs_unreserve_metadata_space(root, 6);
799 return ret;
800}
801
802static noinline int finish_pending_snapshot(struct btrfs_fs_info *fs_info,
803 struct btrfs_pending_snapshot *pending)
804{
805 int ret;
806 int namelen;
807 u64 index = 0;
808 struct btrfs_trans_handle *trans;
809 struct inode *parent_inode;
810 struct inode *inode;
811 struct btrfs_root *parent_root;
812
813 parent_inode = pending->dentry->d_parent->d_inode;
814 parent_root = BTRFS_I(parent_inode)->root;
815 trans = btrfs_join_transaction(parent_root, 1);
816 787
788 record_root_in_trans(trans, parent_root);
817 /* 789 /*
818 * insert the directory item 790 * insert the directory item
819 */ 791 */
820 namelen = strlen(pending->name); 792 namelen = strlen(pending->name);
821 ret = btrfs_set_inode_index(parent_inode, &index); 793 ret = btrfs_set_inode_index(parent_inode, &index);
794 BUG_ON(ret);
822 ret = btrfs_insert_dir_item(trans, parent_root, 795 ret = btrfs_insert_dir_item(trans, parent_root,
823 pending->name, namelen, 796 pending->name, namelen,
824 parent_inode->i_ino, 797 parent_inode->i_ino,
825 &pending->root_key, BTRFS_FT_DIR, index); 798 &pending->root_key, BTRFS_FT_DIR, index);
826 799 BUG_ON(ret);
827 if (ret)
828 goto fail;
829 800
830 btrfs_i_size_write(parent_inode, parent_inode->i_size + namelen * 2); 801 btrfs_i_size_write(parent_inode, parent_inode->i_size + namelen * 2);
831 ret = btrfs_update_inode(trans, parent_root, parent_inode); 802 ret = btrfs_update_inode(trans, parent_root, parent_inode);
832 BUG_ON(ret); 803 BUG_ON(ret);
833 804
805 record_root_in_trans(trans, root);
806 btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
807 memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
808
809 old = btrfs_lock_root_node(root);
810 btrfs_cow_block(trans, root, old, NULL, 0, &old);
811 btrfs_set_lock_blocking(old);
812
813 btrfs_copy_root(trans, root, old, &tmp, objectid);
814 btrfs_tree_unlock(old);
815 free_extent_buffer(old);
816
817 btrfs_set_root_node(new_root_item, tmp);
818 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
819 new_root_item);
820 BUG_ON(ret);
821 btrfs_tree_unlock(tmp);
822 free_extent_buffer(tmp);
823
834 ret = btrfs_add_root_ref(trans, parent_root->fs_info->tree_root, 824 ret = btrfs_add_root_ref(trans, parent_root->fs_info->tree_root,
835 pending->root_key.objectid, 825 pending->root_key.objectid,
836 parent_root->root_key.objectid, 826 parent_root->root_key.objectid,
837 parent_inode->i_ino, index, pending->name, 827 parent_inode->i_ino, index, pending->name,
838 namelen); 828 namelen);
839
840 BUG_ON(ret); 829 BUG_ON(ret);
841 830
842 inode = btrfs_lookup_dentry(parent_inode, pending->dentry);
843 d_instantiate(pending->dentry, inode);
844fail: 831fail:
845 btrfs_end_transaction(trans, fs_info->fs_root); 832 kfree(new_root_item);
846 return ret; 833 return ret;
847} 834}
848 835
@@ -863,25 +850,6 @@ static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
863 return 0; 850 return 0;
864} 851}
865 852
866static noinline int finish_pending_snapshots(struct btrfs_trans_handle *trans,
867 struct btrfs_fs_info *fs_info)
868{
869 struct btrfs_pending_snapshot *pending;
870 struct list_head *head = &trans->transaction->pending_snapshots;
871 int ret;
872
873 while (!list_empty(head)) {
874 pending = list_entry(head->next,
875 struct btrfs_pending_snapshot, list);
876 ret = finish_pending_snapshot(fs_info, pending);
877 BUG_ON(ret);
878 list_del(&pending->list);
879 kfree(pending->name);
880 kfree(pending);
881 }
882 return 0;
883}
884
885static void update_super_roots(struct btrfs_root *root) 853static void update_super_roots(struct btrfs_root *root)
886{ 854{
887 struct btrfs_root_item *root_item; 855 struct btrfs_root_item *root_item;
@@ -993,12 +961,9 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
993 961
994 mutex_unlock(&root->fs_info->trans_mutex); 962 mutex_unlock(&root->fs_info->trans_mutex);
995 963
996 if (flush_on_commit) { 964 if (flush_on_commit || snap_pending) {
997 btrfs_start_delalloc_inodes(root); 965 btrfs_start_delalloc_inodes(root, 1);
998 ret = btrfs_wait_ordered_extents(root, 0); 966 ret = btrfs_wait_ordered_extents(root, 0, 1);
999 BUG_ON(ret);
1000 } else if (snap_pending) {
1001 ret = btrfs_wait_ordered_extents(root, 1);
1002 BUG_ON(ret); 967 BUG_ON(ret);
1003 } 968 }
1004 969
@@ -1096,9 +1061,6 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
1096 1061
1097 btrfs_finish_extent_commit(trans, root); 1062 btrfs_finish_extent_commit(trans, root);
1098 1063
1099 /* do the directory inserts of any pending snapshot creations */
1100 finish_pending_snapshots(trans, root->fs_info);
1101
1102 mutex_lock(&root->fs_info->trans_mutex); 1064 mutex_lock(&root->fs_info->trans_mutex);
1103 1065
1104 cur_trans->commit_done = 1; 1066 cur_trans->commit_done = 1;
@@ -1116,6 +1078,10 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
1116 current->journal_info = NULL; 1078 current->journal_info = NULL;
1117 1079
1118 kmem_cache_free(btrfs_trans_handle_cachep, trans); 1080 kmem_cache_free(btrfs_trans_handle_cachep, trans);
1081
1082 if (current != root->fs_info->transaction_kthread)
1083 btrfs_run_delayed_iputs(root);
1084
1119 return ret; 1085 return ret;
1120} 1086}
1121 1087