transaction.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. struct btrfs_root *inode_root = fs_info->inode_root;
  107. btrfs_set_root_blocknr(&inode_root->root_item,
  108. inode_root->node->b_blocknr);
  109. ret = btrfs_update_root(trans, tree_root,
  110. &inode_root->root_key,
  111. &inode_root->root_item);
  112. BUG_ON(ret);
  113. while(1) {
  114. old_extent_block = btrfs_root_blocknr(&extent_root->root_item);
  115. if (old_extent_block == extent_root->node->b_blocknr)
  116. break;
  117. btrfs_set_root_blocknr(&extent_root->root_item,
  118. extent_root->node->b_blocknr);
  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. printk("add dirty\n");
  158. while(1) {
  159. ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
  160. ARRAY_SIZE(gang),
  161. BTRFS_ROOT_TRANS_TAG);
  162. if (ret == 0)
  163. break;
  164. for (i = 0; i < ret; i++) {
  165. root = gang[i];
  166. radix_tree_tag_clear(radix, (unsigned long)root,
  167. BTRFS_ROOT_TRANS_TAG);
  168. if (root->commit_root == root->node) {
  169. WARN_ON(root->node->b_blocknr !=
  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. printk("adding dirty root %Lu gen %Lu blocknr %Lu\n", root->root_key.objectid, root->root_key.offset, dirty->commit_root->b_blocknr);
  183. root->root_key.offset = root->fs_info->generation;
  184. btrfs_set_root_blocknr(&root->root_item,
  185. root->node->b_blocknr);
  186. err = btrfs_insert_root(trans, root->fs_info->tree_root,
  187. &root->root_key,
  188. &root->root_item);
  189. BUG_ON(err);
  190. list_add(&dirty->list, list);
  191. }
  192. }
  193. printk("add dirty done\n");
  194. return 0;
  195. }
  196. int drop_dirty_roots(struct btrfs_root *tree_root, struct list_head *list)
  197. {
  198. struct dirty_root *dirty;
  199. struct btrfs_trans_handle *trans;
  200. int ret;
  201. while(!list_empty(list)) {
  202. dirty = list_entry(list->next, struct dirty_root, list);
  203. list_del_init(&dirty->list);
  204. trans = btrfs_start_transaction(tree_root, 1);
  205. printk("drop snapshot root %p, commit_root blocknr %Lu generation %Lu\n", dirty->root, dirty->commit_root->b_blocknr, dirty->snap_key.offset);
  206. ret = btrfs_drop_snapshot(trans, dirty->root,
  207. dirty->commit_root);
  208. BUG_ON(ret);
  209. printk("del root objectid %Lu, offset %Lu\n", dirty->snap_key.objectid, dirty->snap_key.offset);
  210. ret = btrfs_del_root(trans, tree_root, &dirty->snap_key);
  211. BUG_ON(ret);
  212. ret = btrfs_end_transaction(trans, tree_root);
  213. BUG_ON(ret);
  214. kfree(dirty);
  215. }
  216. return 0;
  217. }
  218. int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
  219. struct btrfs_root *root)
  220. {
  221. int ret = 0;
  222. struct btrfs_transaction *cur_trans;
  223. struct list_head dirty_fs_roots;
  224. DEFINE_WAIT(wait);
  225. INIT_LIST_HEAD(&dirty_fs_roots);
  226. mutex_lock(&root->fs_info->trans_mutex);
  227. if (trans->transaction->in_commit) {
  228. printk("already in commit!, waiting\n");
  229. cur_trans = trans->transaction;
  230. trans->transaction->use_count++;
  231. btrfs_end_transaction(trans, root);
  232. ret = wait_for_commit(root, cur_trans);
  233. BUG_ON(ret);
  234. put_transaction(cur_trans);
  235. mutex_unlock(&root->fs_info->trans_mutex);
  236. return 0;
  237. }
  238. cur_trans = trans->transaction;
  239. trans->transaction->in_commit = 1;
  240. while (trans->transaction->num_writers > 1) {
  241. WARN_ON(cur_trans != trans->transaction);
  242. prepare_to_wait(&trans->transaction->writer_wait, &wait,
  243. TASK_UNINTERRUPTIBLE);
  244. if (trans->transaction->num_writers <= 1)
  245. break;
  246. mutex_unlock(&root->fs_info->trans_mutex);
  247. schedule();
  248. mutex_lock(&root->fs_info->trans_mutex);
  249. finish_wait(&trans->transaction->writer_wait, &wait);
  250. }
  251. finish_wait(&trans->transaction->writer_wait, &wait);
  252. WARN_ON(cur_trans != trans->transaction);
  253. add_dirty_roots(trans, &root->fs_info->fs_roots_radix, &dirty_fs_roots);
  254. ret = btrfs_commit_tree_roots(trans, root);
  255. BUG_ON(ret);
  256. cur_trans = root->fs_info->running_transaction;
  257. root->fs_info->running_transaction = NULL;
  258. btrfs_set_super_generation(root->fs_info->disk_super,
  259. root->fs_info->generation + 1);
  260. mutex_unlock(&root->fs_info->trans_mutex);
  261. ret = btrfs_write_and_wait_transaction(trans, root);
  262. BUG_ON(ret);
  263. write_ctree_super(trans, root);
  264. btrfs_finish_extent_commit(trans, root);
  265. mutex_lock(&root->fs_info->trans_mutex);
  266. cur_trans->commit_done = 1;
  267. wake_up(&cur_trans->commit_wait);
  268. put_transaction(cur_trans);
  269. put_transaction(cur_trans);
  270. mutex_unlock(&root->fs_info->trans_mutex);
  271. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  272. drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
  273. return ret;
  274. }