transaction.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #include <linux/module.h>
  2. #include <linux/fs.h>
  3. #include "ctree.h"
  4. #include "disk-io.h"
  5. #include "transaction.h"
  6. static void put_transaction(struct btrfs_transaction *transaction)
  7. {
  8. transaction->use_count--;
  9. if (transaction->use_count == 0)
  10. kfree(transaction);
  11. }
  12. static int join_transaction(struct btrfs_root *root)
  13. {
  14. struct btrfs_transaction *cur_trans;
  15. cur_trans = root->fs_info->running_transaction;
  16. if (!cur_trans) {
  17. cur_trans = kmalloc(sizeof(*cur_trans), GFP_NOFS);
  18. BUG_ON(!cur_trans);
  19. root->fs_info->running_transaction = cur_trans;
  20. cur_trans->num_writers = 0;
  21. cur_trans->transid = root->root_key.offset + 1;
  22. init_waitqueue_head(&cur_trans->writer_wait);
  23. init_waitqueue_head(&cur_trans->commit_wait);
  24. cur_trans->in_commit = 0;
  25. cur_trans->use_count = 1;
  26. cur_trans->commit_done = 0;
  27. }
  28. cur_trans->num_writers++;
  29. return 0;
  30. }
  31. struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
  32. int num_blocks)
  33. {
  34. struct btrfs_trans_handle *h = kmalloc(sizeof(*h), GFP_NOFS);
  35. int ret;
  36. mutex_lock(&root->fs_info->trans_mutex);
  37. ret = join_transaction(root);
  38. BUG_ON(ret);
  39. h->transid = root->fs_info->running_transaction->transid;
  40. h->transaction = root->fs_info->running_transaction;
  41. h->blocks_reserved = num_blocks;
  42. h->blocks_used = 0;
  43. root->fs_info->running_transaction->use_count++;
  44. mutex_unlock(&root->fs_info->trans_mutex);
  45. return h;
  46. }
  47. int btrfs_end_transaction(struct btrfs_trans_handle *trans,
  48. struct btrfs_root *root)
  49. {
  50. struct btrfs_transaction *cur_trans;
  51. mutex_lock(&root->fs_info->trans_mutex);
  52. cur_trans = root->fs_info->running_transaction;
  53. WARN_ON(cur_trans->num_writers < 1);
  54. if (waitqueue_active(&cur_trans->writer_wait))
  55. wake_up(&cur_trans->writer_wait);
  56. cur_trans->num_writers--;
  57. put_transaction(cur_trans);
  58. mutex_unlock(&root->fs_info->trans_mutex);
  59. kfree(trans);
  60. return 0;
  61. }
  62. int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
  63. struct btrfs_root *root)
  64. {
  65. filemap_write_and_wait(root->fs_info->sb->s_bdev->bd_inode->i_mapping);
  66. return 0;
  67. }
  68. int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
  69. struct btrfs_root *root)
  70. {
  71. int ret;
  72. u64 old_extent_block;
  73. struct btrfs_fs_info *fs_info = root->fs_info;
  74. struct btrfs_root *tree_root = fs_info->tree_root;
  75. struct btrfs_root *extent_root = fs_info->extent_root;
  76. struct btrfs_root *inode_root = fs_info->inode_root;
  77. btrfs_set_root_blocknr(&inode_root->root_item,
  78. inode_root->node->b_blocknr);
  79. ret = btrfs_update_root(trans, tree_root,
  80. &inode_root->root_key,
  81. &inode_root->root_item);
  82. BUG_ON(ret);
  83. while(1) {
  84. old_extent_block = btrfs_root_blocknr(&extent_root->root_item);
  85. if (old_extent_block == extent_root->node->b_blocknr)
  86. break;
  87. btrfs_set_root_blocknr(&extent_root->root_item,
  88. extent_root->node->b_blocknr);
  89. ret = btrfs_update_root(trans, tree_root,
  90. &extent_root->root_key,
  91. &extent_root->root_item);
  92. BUG_ON(ret);
  93. }
  94. return 0;
  95. }
  96. static int wait_for_commit(struct btrfs_root *root,
  97. struct btrfs_transaction *commit)
  98. {
  99. DEFINE_WAIT(wait);
  100. commit->use_count++;
  101. while(!commit->commit_done) {
  102. prepare_to_wait(&commit->commit_wait, &wait,
  103. TASK_UNINTERRUPTIBLE);
  104. if (commit->commit_done)
  105. break;
  106. mutex_unlock(&root->fs_info->trans_mutex);
  107. schedule();
  108. mutex_lock(&root->fs_info->trans_mutex);
  109. }
  110. finish_wait(&commit->commit_wait, &wait);
  111. return 0;
  112. }
  113. int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
  114. struct btrfs_root *root)
  115. {
  116. int ret = 0;
  117. struct buffer_head *snap = root->commit_root;
  118. struct btrfs_key snap_key;
  119. struct btrfs_transaction *cur_trans;
  120. DEFINE_WAIT(wait);
  121. mutex_lock(&root->fs_info->trans_mutex);
  122. if (trans->transaction->in_commit) {
  123. cur_trans = trans->transaction;
  124. trans->transaction->use_count++;
  125. btrfs_end_transaction(trans, root);
  126. ret = wait_for_commit(root, cur_trans);
  127. BUG_ON(ret);
  128. put_transaction(cur_trans);
  129. mutex_unlock(&root->fs_info->trans_mutex);
  130. return 0;
  131. }
  132. while (trans->transaction->num_writers > 1) {
  133. prepare_to_wait(&trans->transaction->writer_wait, &wait,
  134. TASK_UNINTERRUPTIBLE);
  135. if (trans->transaction->num_writers <= 1)
  136. break;
  137. mutex_unlock(&root->fs_info->trans_mutex);
  138. schedule();
  139. mutex_lock(&root->fs_info->trans_mutex);
  140. }
  141. finish_wait(&trans->transaction->writer_wait, &wait);
  142. cur_trans = root->fs_info->running_transaction;
  143. root->fs_info->running_transaction = NULL;
  144. if (root->node != root->commit_root) {
  145. memcpy(&snap_key, &root->root_key, sizeof(snap_key));
  146. root->root_key.offset++;
  147. }
  148. mutex_unlock(&root->fs_info->trans_mutex);
  149. if (btrfs_root_blocknr(&root->root_item) != root->node->b_blocknr) {
  150. btrfs_set_root_blocknr(&root->root_item, root->node->b_blocknr);
  151. ret = btrfs_insert_root(trans, root->fs_info->tree_root,
  152. &root->root_key, &root->root_item);
  153. BUG_ON(ret);
  154. }
  155. ret = btrfs_commit_tree_roots(trans, root);
  156. BUG_ON(ret);
  157. ret = btrfs_write_and_wait_transaction(trans, root);
  158. BUG_ON(ret);
  159. write_ctree_super(trans, root);
  160. btrfs_finish_extent_commit(trans, root->fs_info->extent_root);
  161. btrfs_finish_extent_commit(trans, root->fs_info->tree_root);
  162. put_transaction(cur_trans);
  163. kfree(trans);
  164. if (root->node != root->commit_root) {
  165. trans = btrfs_start_transaction(root, 1);
  166. root->commit_root = root->node;
  167. get_bh(root->node);
  168. ret = btrfs_drop_snapshot(trans, root, snap);
  169. BUG_ON(ret);
  170. ret = btrfs_del_root(trans, root->fs_info->tree_root,
  171. &snap_key);
  172. BUG_ON(ret);
  173. root->fs_info->generation = root->root_key.offset + 1;
  174. ret = btrfs_end_transaction(trans, root);
  175. BUG_ON(ret);
  176. }
  177. return ret;
  178. }