transaction.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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, BTRFS_ROOT_TRANS_TAG);
  61. root->commit_root = root->node;
  62. get_bh(root->node);
  63. }
  64. root->last_trans = running_trans_id;
  65. h->transid = running_trans_id;
  66. h->transaction = root->fs_info->running_transaction;
  67. h->blocks_reserved = num_blocks;
  68. h->blocks_used = 0;
  69. root->fs_info->running_transaction->use_count++;
  70. mutex_unlock(&root->fs_info->trans_mutex);
  71. h->magic = h->magic2 = TRANS_MAGIC;
  72. return h;
  73. }
  74. int btrfs_end_transaction(struct btrfs_trans_handle *trans,
  75. struct btrfs_root *root)
  76. {
  77. struct btrfs_transaction *cur_trans;
  78. WARN_ON(trans->magic != TRANS_MAGIC);
  79. WARN_ON(trans->magic2 != TRANS_MAGIC);
  80. mutex_lock(&root->fs_info->trans_mutex);
  81. cur_trans = root->fs_info->running_transaction;
  82. WARN_ON(cur_trans->num_writers < 1);
  83. if (waitqueue_active(&cur_trans->writer_wait))
  84. wake_up(&cur_trans->writer_wait);
  85. cur_trans->num_writers--;
  86. put_transaction(cur_trans);
  87. mutex_unlock(&root->fs_info->trans_mutex);
  88. memset(trans, 0, sizeof(*trans));
  89. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  90. return 0;
  91. }
  92. int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
  93. struct btrfs_root *root)
  94. {
  95. filemap_write_and_wait(root->fs_info->btree_inode->i_mapping);
  96. return 0;
  97. }
  98. int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
  99. struct btrfs_root *root)
  100. {
  101. int ret;
  102. u64 old_extent_block;
  103. struct btrfs_fs_info *fs_info = root->fs_info;
  104. struct btrfs_root *tree_root = fs_info->tree_root;
  105. struct btrfs_root *extent_root = fs_info->extent_root;
  106. while(1) {
  107. old_extent_block = btrfs_root_blocknr(&extent_root->root_item);
  108. if (old_extent_block == extent_root->node->b_blocknr)
  109. break;
  110. btrfs_set_root_blocknr(&extent_root->root_item,
  111. extent_root->node->b_blocknr);
  112. ret = btrfs_update_root(trans, tree_root,
  113. &extent_root->root_key,
  114. &extent_root->root_item);
  115. BUG_ON(ret);
  116. }
  117. return 0;
  118. }
  119. static int wait_for_commit(struct btrfs_root *root,
  120. struct btrfs_transaction *commit)
  121. {
  122. DEFINE_WAIT(wait);
  123. while(!commit->commit_done) {
  124. prepare_to_wait(&commit->commit_wait, &wait,
  125. TASK_UNINTERRUPTIBLE);
  126. if (commit->commit_done)
  127. break;
  128. mutex_unlock(&root->fs_info->trans_mutex);
  129. schedule();
  130. mutex_lock(&root->fs_info->trans_mutex);
  131. }
  132. finish_wait(&commit->commit_wait, &wait);
  133. return 0;
  134. }
  135. struct dirty_root {
  136. struct list_head list;
  137. struct btrfs_key snap_key;
  138. struct buffer_head *commit_root;
  139. struct btrfs_root *root;
  140. };
  141. int add_dirty_roots(struct btrfs_trans_handle *trans,
  142. struct radix_tree_root *radix, struct list_head *list)
  143. {
  144. struct dirty_root *dirty;
  145. struct btrfs_root *gang[8];
  146. struct btrfs_root *root;
  147. int i;
  148. int ret;
  149. int err;
  150. printk("add dirty\n");
  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, (unsigned long)root,
  160. BTRFS_ROOT_TRANS_TAG);
  161. if (root->commit_root == root->node) {
  162. WARN_ON(root->node->b_blocknr !=
  163. btrfs_root_blocknr(&root->root_item));
  164. brelse(root->commit_root);
  165. root->commit_root = NULL;
  166. continue;
  167. }
  168. dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
  169. BUG_ON(!dirty);
  170. memcpy(&dirty->snap_key, &root->root_key,
  171. sizeof(root->root_key));
  172. dirty->commit_root = root->commit_root;
  173. root->commit_root = NULL;
  174. dirty->root = root;
  175. printk("adding dirty root %Lu gen %Lu blocknr %Lu\n", root->root_key.objectid, root->root_key.offset, dirty->commit_root->b_blocknr);
  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. printk("add dirty done\n");
  187. return 0;
  188. }
  189. int drop_dirty_roots(struct btrfs_root *tree_root, struct list_head *list)
  190. {
  191. struct dirty_root *dirty;
  192. struct btrfs_trans_handle *trans;
  193. int ret;
  194. while(!list_empty(list)) {
  195. dirty = list_entry(list->next, struct dirty_root, list);
  196. list_del_init(&dirty->list);
  197. trans = btrfs_start_transaction(tree_root, 1);
  198. printk("drop snapshot root %p, commit_root blocknr %Lu generation %Lu\n", dirty->root, dirty->commit_root->b_blocknr, dirty->snap_key.offset);
  199. ret = btrfs_drop_snapshot(trans, dirty->root,
  200. dirty->commit_root);
  201. BUG_ON(ret);
  202. printk("del root objectid %Lu, offset %Lu\n", dirty->snap_key.objectid, dirty->snap_key.offset);
  203. ret = btrfs_del_root(trans, tree_root, &dirty->snap_key);
  204. BUG_ON(ret);
  205. ret = btrfs_end_transaction(trans, tree_root);
  206. BUG_ON(ret);
  207. kfree(dirty);
  208. }
  209. return 0;
  210. }
  211. int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
  212. struct btrfs_root *root)
  213. {
  214. int ret = 0;
  215. struct btrfs_transaction *cur_trans;
  216. struct list_head dirty_fs_roots;
  217. DEFINE_WAIT(wait);
  218. INIT_LIST_HEAD(&dirty_fs_roots);
  219. mutex_lock(&root->fs_info->trans_mutex);
  220. if (trans->transaction->in_commit) {
  221. printk("already in commit!, waiting\n");
  222. cur_trans = trans->transaction;
  223. trans->transaction->use_count++;
  224. btrfs_end_transaction(trans, root);
  225. ret = wait_for_commit(root, cur_trans);
  226. BUG_ON(ret);
  227. put_transaction(cur_trans);
  228. mutex_unlock(&root->fs_info->trans_mutex);
  229. return 0;
  230. }
  231. cur_trans = trans->transaction;
  232. trans->transaction->in_commit = 1;
  233. while (trans->transaction->num_writers > 1) {
  234. WARN_ON(cur_trans != trans->transaction);
  235. prepare_to_wait(&trans->transaction->writer_wait, &wait,
  236. TASK_UNINTERRUPTIBLE);
  237. if (trans->transaction->num_writers <= 1)
  238. break;
  239. mutex_unlock(&root->fs_info->trans_mutex);
  240. schedule();
  241. mutex_lock(&root->fs_info->trans_mutex);
  242. finish_wait(&trans->transaction->writer_wait, &wait);
  243. }
  244. finish_wait(&trans->transaction->writer_wait, &wait);
  245. WARN_ON(cur_trans != trans->transaction);
  246. add_dirty_roots(trans, &root->fs_info->fs_roots_radix, &dirty_fs_roots);
  247. ret = btrfs_commit_tree_roots(trans, root);
  248. BUG_ON(ret);
  249. cur_trans = root->fs_info->running_transaction;
  250. root->fs_info->running_transaction = NULL;
  251. btrfs_set_super_generation(root->fs_info->disk_super,
  252. root->fs_info->generation + 1);
  253. mutex_unlock(&root->fs_info->trans_mutex);
  254. ret = btrfs_write_and_wait_transaction(trans, root);
  255. BUG_ON(ret);
  256. write_ctree_super(trans, root);
  257. btrfs_finish_extent_commit(trans, root);
  258. mutex_lock(&root->fs_info->trans_mutex);
  259. cur_trans->commit_done = 1;
  260. wake_up(&cur_trans->commit_wait);
  261. put_transaction(cur_trans);
  262. put_transaction(cur_trans);
  263. mutex_unlock(&root->fs_info->trans_mutex);
  264. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  265. drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
  266. return ret;
  267. }