transaction.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 int total_trans = 0;
  7. extern struct kmem_cache *btrfs_trans_handle_cachep;
  8. extern struct kmem_cache *btrfs_transaction_cachep;
  9. #define BTRFS_ROOT_TRANS_TAG 0
  10. #define TRANS_MAGIC 0xE1E10E
  11. static void put_transaction(struct btrfs_transaction *transaction)
  12. {
  13. WARN_ON(transaction->use_count == 0);
  14. transaction->use_count--;
  15. WARN_ON(transaction->magic != TRANS_MAGIC);
  16. if (transaction->use_count == 0) {
  17. WARN_ON(total_trans == 0);
  18. total_trans--;
  19. memset(transaction, 0, sizeof(*transaction));
  20. kmem_cache_free(btrfs_transaction_cachep, transaction);
  21. }
  22. }
  23. static int join_transaction(struct btrfs_root *root)
  24. {
  25. struct btrfs_transaction *cur_trans;
  26. cur_trans = root->fs_info->running_transaction;
  27. if (!cur_trans) {
  28. cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
  29. GFP_NOFS);
  30. total_trans++;
  31. BUG_ON(!cur_trans);
  32. root->fs_info->generation++;
  33. root->fs_info->running_transaction = cur_trans;
  34. cur_trans->num_writers = 0;
  35. cur_trans->transid = root->fs_info->generation;
  36. init_waitqueue_head(&cur_trans->writer_wait);
  37. init_waitqueue_head(&cur_trans->commit_wait);
  38. cur_trans->magic = TRANS_MAGIC;
  39. cur_trans->in_commit = 0;
  40. cur_trans->use_count = 1;
  41. cur_trans->commit_done = 0;
  42. }
  43. cur_trans->num_writers++;
  44. return 0;
  45. }
  46. struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
  47. int num_blocks)
  48. {
  49. struct btrfs_trans_handle *h =
  50. kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
  51. int ret;
  52. u64 running_trans_id;
  53. mutex_lock(&root->fs_info->trans_mutex);
  54. ret = join_transaction(root);
  55. BUG_ON(ret);
  56. running_trans_id = root->fs_info->running_transaction->transid;
  57. if (root != root->fs_info->tree_root && root->last_trans <
  58. running_trans_id) {
  59. radix_tree_tag_set(&root->fs_info->fs_roots_radix,
  60. (unsigned long)root->root_key.objectid,
  61. BTRFS_ROOT_TRANS_TAG);
  62. root->commit_root = root->node;
  63. get_bh(root->node);
  64. }
  65. root->last_trans = running_trans_id;
  66. h->transid = running_trans_id;
  67. h->transaction = root->fs_info->running_transaction;
  68. h->blocks_reserved = num_blocks;
  69. h->blocks_used = 0;
  70. root->fs_info->running_transaction->use_count++;
  71. mutex_unlock(&root->fs_info->trans_mutex);
  72. h->magic = h->magic2 = TRANS_MAGIC;
  73. return h;
  74. }
  75. int btrfs_end_transaction(struct btrfs_trans_handle *trans,
  76. struct btrfs_root *root)
  77. {
  78. struct btrfs_transaction *cur_trans;
  79. WARN_ON(trans->magic != TRANS_MAGIC);
  80. WARN_ON(trans->magic2 != TRANS_MAGIC);
  81. mutex_lock(&root->fs_info->trans_mutex);
  82. cur_trans = root->fs_info->running_transaction;
  83. WARN_ON(cur_trans->num_writers < 1);
  84. if (waitqueue_active(&cur_trans->writer_wait))
  85. wake_up(&cur_trans->writer_wait);
  86. cur_trans->num_writers--;
  87. put_transaction(cur_trans);
  88. mutex_unlock(&root->fs_info->trans_mutex);
  89. memset(trans, 0, sizeof(*trans));
  90. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  91. return 0;
  92. }
  93. int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
  94. struct btrfs_root *root)
  95. {
  96. filemap_write_and_wait(root->fs_info->btree_inode->i_mapping);
  97. return 0;
  98. }
  99. int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
  100. struct btrfs_root *root)
  101. {
  102. int ret;
  103. u64 old_extent_block;
  104. struct btrfs_fs_info *fs_info = root->fs_info;
  105. struct btrfs_root *tree_root = fs_info->tree_root;
  106. struct btrfs_root *extent_root = fs_info->extent_root;
  107. while(1) {
  108. old_extent_block = btrfs_root_blocknr(&extent_root->root_item);
  109. if (old_extent_block == extent_root->node->b_blocknr)
  110. break;
  111. btrfs_set_root_blocknr(&extent_root->root_item,
  112. extent_root->node->b_blocknr);
  113. ret = btrfs_update_root(trans, tree_root,
  114. &extent_root->root_key,
  115. &extent_root->root_item);
  116. BUG_ON(ret);
  117. }
  118. return 0;
  119. }
  120. static int wait_for_commit(struct btrfs_root *root,
  121. struct btrfs_transaction *commit)
  122. {
  123. DEFINE_WAIT(wait);
  124. while(!commit->commit_done) {
  125. prepare_to_wait(&commit->commit_wait, &wait,
  126. TASK_UNINTERRUPTIBLE);
  127. if (commit->commit_done)
  128. break;
  129. mutex_unlock(&root->fs_info->trans_mutex);
  130. schedule();
  131. mutex_lock(&root->fs_info->trans_mutex);
  132. }
  133. finish_wait(&commit->commit_wait, &wait);
  134. return 0;
  135. }
  136. struct dirty_root {
  137. struct list_head list;
  138. struct btrfs_key snap_key;
  139. struct buffer_head *commit_root;
  140. struct btrfs_root *root;
  141. };
  142. int add_dirty_roots(struct btrfs_trans_handle *trans,
  143. struct radix_tree_root *radix, struct list_head *list)
  144. {
  145. struct dirty_root *dirty;
  146. struct btrfs_root *gang[8];
  147. struct btrfs_root *root;
  148. int i;
  149. int ret;
  150. int err;
  151. while(1) {
  152. ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
  153. ARRAY_SIZE(gang),
  154. BTRFS_ROOT_TRANS_TAG);
  155. if (ret == 0)
  156. break;
  157. for (i = 0; i < ret; i++) {
  158. root = gang[i];
  159. radix_tree_tag_clear(radix,
  160. (unsigned long)root->root_key.objectid,
  161. BTRFS_ROOT_TRANS_TAG);
  162. if (root->commit_root == root->node) {
  163. WARN_ON(root->node->b_blocknr !=
  164. btrfs_root_blocknr(&root->root_item));
  165. brelse(root->commit_root);
  166. root->commit_root = NULL;
  167. continue;
  168. }
  169. dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
  170. BUG_ON(!dirty);
  171. memcpy(&dirty->snap_key, &root->root_key,
  172. sizeof(root->root_key));
  173. dirty->commit_root = root->commit_root;
  174. root->commit_root = NULL;
  175. dirty->root = root;
  176. root->root_key.offset = root->fs_info->generation;
  177. btrfs_set_root_blocknr(&root->root_item,
  178. root->node->b_blocknr);
  179. err = btrfs_insert_root(trans, root->fs_info->tree_root,
  180. &root->root_key,
  181. &root->root_item);
  182. BUG_ON(err);
  183. list_add(&dirty->list, list);
  184. }
  185. }
  186. return 0;
  187. }
  188. int drop_dirty_roots(struct btrfs_root *tree_root, struct list_head *list)
  189. {
  190. struct dirty_root *dirty;
  191. struct btrfs_trans_handle *trans;
  192. int ret;
  193. while(!list_empty(list)) {
  194. dirty = list_entry(list->next, struct dirty_root, list);
  195. list_del_init(&dirty->list);
  196. trans = btrfs_start_transaction(tree_root, 1);
  197. ret = btrfs_drop_snapshot(trans, dirty->root,
  198. dirty->commit_root);
  199. BUG_ON(ret);
  200. ret = btrfs_del_root(trans, tree_root, &dirty->snap_key);
  201. BUG_ON(ret);
  202. ret = btrfs_end_transaction(trans, tree_root);
  203. BUG_ON(ret);
  204. kfree(dirty);
  205. }
  206. return 0;
  207. }
  208. int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
  209. struct btrfs_root *root)
  210. {
  211. int ret = 0;
  212. struct btrfs_transaction *cur_trans;
  213. struct list_head dirty_fs_roots;
  214. DEFINE_WAIT(wait);
  215. INIT_LIST_HEAD(&dirty_fs_roots);
  216. mutex_lock(&root->fs_info->trans_mutex);
  217. if (trans->transaction->in_commit) {
  218. cur_trans = trans->transaction;
  219. trans->transaction->use_count++;
  220. btrfs_end_transaction(trans, root);
  221. ret = wait_for_commit(root, cur_trans);
  222. BUG_ON(ret);
  223. put_transaction(cur_trans);
  224. mutex_unlock(&root->fs_info->trans_mutex);
  225. return 0;
  226. }
  227. cur_trans = trans->transaction;
  228. trans->transaction->in_commit = 1;
  229. while (trans->transaction->num_writers > 1) {
  230. WARN_ON(cur_trans != trans->transaction);
  231. prepare_to_wait(&trans->transaction->writer_wait, &wait,
  232. TASK_UNINTERRUPTIBLE);
  233. if (trans->transaction->num_writers <= 1)
  234. break;
  235. mutex_unlock(&root->fs_info->trans_mutex);
  236. schedule();
  237. mutex_lock(&root->fs_info->trans_mutex);
  238. finish_wait(&trans->transaction->writer_wait, &wait);
  239. }
  240. finish_wait(&trans->transaction->writer_wait, &wait);
  241. WARN_ON(cur_trans != trans->transaction);
  242. add_dirty_roots(trans, &root->fs_info->fs_roots_radix, &dirty_fs_roots);
  243. ret = btrfs_commit_tree_roots(trans, root);
  244. BUG_ON(ret);
  245. cur_trans = root->fs_info->running_transaction;
  246. root->fs_info->running_transaction = NULL;
  247. btrfs_set_super_generation(root->fs_info->disk_super,
  248. root->fs_info->generation + 1);
  249. mutex_unlock(&root->fs_info->trans_mutex);
  250. ret = btrfs_write_and_wait_transaction(trans, root);
  251. BUG_ON(ret);
  252. write_ctree_super(trans, root);
  253. btrfs_finish_extent_commit(trans, root);
  254. mutex_lock(&root->fs_info->trans_mutex);
  255. cur_trans->commit_done = 1;
  256. wake_up(&cur_trans->commit_wait);
  257. put_transaction(cur_trans);
  258. put_transaction(cur_trans);
  259. mutex_unlock(&root->fs_info->trans_mutex);
  260. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  261. drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
  262. return ret;
  263. }