transaction.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. struct btrfs_root *dev_root = fs_info->dev_root;
  108. if (btrfs_super_device_root(fs_info->disk_super) !=
  109. bh_blocknr(dev_root->node)) {
  110. btrfs_set_super_device_root(fs_info->disk_super,
  111. bh_blocknr(dev_root->node));
  112. }
  113. while(1) {
  114. old_extent_block = btrfs_root_blocknr(&extent_root->root_item);
  115. if (old_extent_block == bh_blocknr(extent_root->node))
  116. break;
  117. btrfs_set_root_blocknr(&extent_root->root_item,
  118. bh_blocknr(extent_root->node));
  119. ret = btrfs_update_root(trans, tree_root,
  120. &extent_root->root_key,
  121. &extent_root->root_item);
  122. BUG_ON(ret);
  123. }
  124. return 0;
  125. }
  126. static int wait_for_commit(struct btrfs_root *root,
  127. struct btrfs_transaction *commit)
  128. {
  129. DEFINE_WAIT(wait);
  130. while(!commit->commit_done) {
  131. prepare_to_wait(&commit->commit_wait, &wait,
  132. TASK_UNINTERRUPTIBLE);
  133. if (commit->commit_done)
  134. break;
  135. mutex_unlock(&root->fs_info->trans_mutex);
  136. schedule();
  137. mutex_lock(&root->fs_info->trans_mutex);
  138. }
  139. finish_wait(&commit->commit_wait, &wait);
  140. return 0;
  141. }
  142. struct dirty_root {
  143. struct list_head list;
  144. struct btrfs_key snap_key;
  145. struct buffer_head *commit_root;
  146. struct btrfs_root *root;
  147. };
  148. int add_dirty_roots(struct btrfs_trans_handle *trans,
  149. struct radix_tree_root *radix, struct list_head *list)
  150. {
  151. struct dirty_root *dirty;
  152. struct btrfs_root *gang[8];
  153. struct btrfs_root *root;
  154. int i;
  155. int ret;
  156. int err;
  157. while(1) {
  158. ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
  159. ARRAY_SIZE(gang),
  160. BTRFS_ROOT_TRANS_TAG);
  161. if (ret == 0)
  162. break;
  163. for (i = 0; i < ret; i++) {
  164. root = gang[i];
  165. radix_tree_tag_clear(radix,
  166. (unsigned long)root->root_key.objectid,
  167. BTRFS_ROOT_TRANS_TAG);
  168. if (root->commit_root == root->node) {
  169. WARN_ON(bh_blocknr(root->node) !=
  170. btrfs_root_blocknr(&root->root_item));
  171. brelse(root->commit_root);
  172. root->commit_root = NULL;
  173. continue;
  174. }
  175. dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
  176. BUG_ON(!dirty);
  177. memcpy(&dirty->snap_key, &root->root_key,
  178. sizeof(root->root_key));
  179. dirty->commit_root = root->commit_root;
  180. root->commit_root = NULL;
  181. dirty->root = root;
  182. root->root_key.offset = root->fs_info->generation;
  183. btrfs_set_root_blocknr(&root->root_item,
  184. bh_blocknr(root->node));
  185. err = btrfs_insert_root(trans, root->fs_info->tree_root,
  186. &root->root_key,
  187. &root->root_item);
  188. BUG_ON(err);
  189. list_add(&dirty->list, list);
  190. }
  191. }
  192. return 0;
  193. }
  194. int drop_dirty_roots(struct btrfs_root *tree_root, struct list_head *list)
  195. {
  196. struct dirty_root *dirty;
  197. struct btrfs_trans_handle *trans;
  198. int ret;
  199. while(!list_empty(list)) {
  200. dirty = list_entry(list->next, struct dirty_root, list);
  201. list_del_init(&dirty->list);
  202. trans = btrfs_start_transaction(tree_root, 1);
  203. ret = btrfs_drop_snapshot(trans, dirty->root,
  204. dirty->commit_root);
  205. BUG_ON(ret);
  206. ret = btrfs_del_root(trans, tree_root, &dirty->snap_key);
  207. BUG_ON(ret);
  208. ret = btrfs_end_transaction(trans, tree_root);
  209. BUG_ON(ret);
  210. kfree(dirty);
  211. }
  212. return 0;
  213. }
  214. int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
  215. struct btrfs_root *root)
  216. {
  217. int ret = 0;
  218. struct btrfs_transaction *cur_trans;
  219. struct list_head dirty_fs_roots;
  220. DEFINE_WAIT(wait);
  221. INIT_LIST_HEAD(&dirty_fs_roots);
  222. mutex_lock(&root->fs_info->trans_mutex);
  223. if (trans->transaction->in_commit) {
  224. cur_trans = trans->transaction;
  225. trans->transaction->use_count++;
  226. btrfs_end_transaction(trans, root);
  227. ret = wait_for_commit(root, cur_trans);
  228. BUG_ON(ret);
  229. put_transaction(cur_trans);
  230. mutex_unlock(&root->fs_info->trans_mutex);
  231. return 0;
  232. }
  233. cur_trans = trans->transaction;
  234. trans->transaction->in_commit = 1;
  235. while (trans->transaction->num_writers > 1) {
  236. WARN_ON(cur_trans != trans->transaction);
  237. prepare_to_wait(&trans->transaction->writer_wait, &wait,
  238. TASK_UNINTERRUPTIBLE);
  239. if (trans->transaction->num_writers <= 1)
  240. break;
  241. mutex_unlock(&root->fs_info->trans_mutex);
  242. schedule();
  243. mutex_lock(&root->fs_info->trans_mutex);
  244. finish_wait(&trans->transaction->writer_wait, &wait);
  245. }
  246. finish_wait(&trans->transaction->writer_wait, &wait);
  247. WARN_ON(cur_trans != trans->transaction);
  248. add_dirty_roots(trans, &root->fs_info->fs_roots_radix, &dirty_fs_roots);
  249. ret = btrfs_commit_tree_roots(trans, root);
  250. BUG_ON(ret);
  251. cur_trans = root->fs_info->running_transaction;
  252. root->fs_info->running_transaction = NULL;
  253. btrfs_set_super_generation(root->fs_info->disk_super,
  254. root->fs_info->generation + 1);
  255. mutex_unlock(&root->fs_info->trans_mutex);
  256. ret = btrfs_write_and_wait_transaction(trans, root);
  257. BUG_ON(ret);
  258. write_ctree_super(trans, root);
  259. btrfs_finish_extent_commit(trans, root);
  260. mutex_lock(&root->fs_info->trans_mutex);
  261. cur_trans->commit_done = 1;
  262. wake_up(&cur_trans->commit_wait);
  263. put_transaction(cur_trans);
  264. put_transaction(cur_trans);
  265. mutex_unlock(&root->fs_info->trans_mutex);
  266. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  267. drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
  268. return ret;
  269. }