transaction.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. list_del_init(&transaction->list);
  20. memset(transaction, 0, sizeof(*transaction));
  21. kmem_cache_free(btrfs_transaction_cachep, transaction);
  22. }
  23. }
  24. static int join_transaction(struct btrfs_root *root)
  25. {
  26. struct btrfs_transaction *cur_trans;
  27. cur_trans = root->fs_info->running_transaction;
  28. if (!cur_trans) {
  29. cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
  30. GFP_NOFS);
  31. total_trans++;
  32. BUG_ON(!cur_trans);
  33. root->fs_info->generation++;
  34. root->fs_info->running_transaction = cur_trans;
  35. cur_trans->num_writers = 0;
  36. cur_trans->transid = root->fs_info->generation;
  37. init_waitqueue_head(&cur_trans->writer_wait);
  38. init_waitqueue_head(&cur_trans->commit_wait);
  39. cur_trans->magic = TRANS_MAGIC;
  40. cur_trans->in_commit = 0;
  41. cur_trans->use_count = 1;
  42. cur_trans->commit_done = 0;
  43. list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
  44. init_bit_radix(&cur_trans->dirty_pages);
  45. }
  46. cur_trans->num_writers++;
  47. return 0;
  48. }
  49. struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
  50. int num_blocks)
  51. {
  52. struct btrfs_trans_handle *h =
  53. kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
  54. int ret;
  55. u64 running_trans_id;
  56. mutex_lock(&root->fs_info->trans_mutex);
  57. ret = join_transaction(root);
  58. BUG_ON(ret);
  59. running_trans_id = root->fs_info->running_transaction->transid;
  60. if (root != root->fs_info->tree_root && root->last_trans <
  61. running_trans_id) {
  62. radix_tree_tag_set(&root->fs_info->fs_roots_radix,
  63. (unsigned long)root->root_key.objectid,
  64. BTRFS_ROOT_TRANS_TAG);
  65. root->commit_root = root->node;
  66. get_bh(root->node);
  67. }
  68. root->last_trans = running_trans_id;
  69. h->transid = running_trans_id;
  70. h->transaction = root->fs_info->running_transaction;
  71. h->blocks_reserved = num_blocks;
  72. h->blocks_used = 0;
  73. h->block_group = NULL;
  74. root->fs_info->running_transaction->use_count++;
  75. mutex_unlock(&root->fs_info->trans_mutex);
  76. h->magic = h->magic2 = TRANS_MAGIC;
  77. return h;
  78. }
  79. int btrfs_end_transaction(struct btrfs_trans_handle *trans,
  80. struct btrfs_root *root)
  81. {
  82. struct btrfs_transaction *cur_trans;
  83. WARN_ON(trans->magic != TRANS_MAGIC);
  84. WARN_ON(trans->magic2 != TRANS_MAGIC);
  85. mutex_lock(&root->fs_info->trans_mutex);
  86. cur_trans = root->fs_info->running_transaction;
  87. WARN_ON(cur_trans->num_writers < 1);
  88. if (waitqueue_active(&cur_trans->writer_wait))
  89. wake_up(&cur_trans->writer_wait);
  90. cur_trans->num_writers--;
  91. put_transaction(cur_trans);
  92. mutex_unlock(&root->fs_info->trans_mutex);
  93. memset(trans, 0, sizeof(*trans));
  94. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  95. return 0;
  96. }
  97. int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
  98. struct btrfs_root *root)
  99. {
  100. unsigned long gang[16];
  101. int ret;
  102. int i;
  103. int err;
  104. int werr = 0;
  105. struct page *page;
  106. struct radix_tree_root *dirty_pages;
  107. struct inode *btree_inode = root->fs_info->btree_inode;
  108. if (!trans || !trans->transaction) {
  109. return filemap_write_and_wait(btree_inode->i_mapping);
  110. }
  111. dirty_pages = &trans->transaction->dirty_pages;
  112. while(1) {
  113. ret = find_first_radix_bit(dirty_pages, gang, ARRAY_SIZE(gang));
  114. if (!ret)
  115. break;
  116. for (i = 0; i < ret; i++) {
  117. /* FIXME EIO */
  118. clear_radix_bit(dirty_pages, gang[i]);
  119. page = find_lock_page(btree_inode->i_mapping,
  120. gang[i]);
  121. if (!page)
  122. continue;
  123. err = write_one_page(page, 0);
  124. if (err)
  125. werr = err;
  126. page_cache_release(page);
  127. }
  128. }
  129. err = filemap_fdatawait(btree_inode->i_mapping);
  130. if (err)
  131. werr = err;
  132. return werr;
  133. }
  134. int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
  135. struct btrfs_root *root)
  136. {
  137. int ret;
  138. u64 old_extent_block;
  139. struct btrfs_fs_info *fs_info = root->fs_info;
  140. struct btrfs_root *tree_root = fs_info->tree_root;
  141. struct btrfs_root *extent_root = fs_info->extent_root;
  142. struct btrfs_root *dev_root = fs_info->dev_root;
  143. if (btrfs_super_device_root(fs_info->disk_super) !=
  144. bh_blocknr(dev_root->node)) {
  145. btrfs_set_super_device_root(fs_info->disk_super,
  146. bh_blocknr(dev_root->node));
  147. }
  148. btrfs_write_dirty_block_groups(trans, extent_root);
  149. while(1) {
  150. old_extent_block = btrfs_root_blocknr(&extent_root->root_item);
  151. if (old_extent_block == bh_blocknr(extent_root->node))
  152. break;
  153. btrfs_set_root_blocknr(&extent_root->root_item,
  154. bh_blocknr(extent_root->node));
  155. ret = btrfs_update_root(trans, tree_root,
  156. &extent_root->root_key,
  157. &extent_root->root_item);
  158. BUG_ON(ret);
  159. btrfs_write_dirty_block_groups(trans, extent_root);
  160. }
  161. return 0;
  162. }
  163. static int wait_for_commit(struct btrfs_root *root,
  164. struct btrfs_transaction *commit)
  165. {
  166. DEFINE_WAIT(wait);
  167. while(!commit->commit_done) {
  168. prepare_to_wait(&commit->commit_wait, &wait,
  169. TASK_UNINTERRUPTIBLE);
  170. if (commit->commit_done)
  171. break;
  172. mutex_unlock(&root->fs_info->trans_mutex);
  173. schedule();
  174. mutex_lock(&root->fs_info->trans_mutex);
  175. }
  176. finish_wait(&commit->commit_wait, &wait);
  177. return 0;
  178. }
  179. struct dirty_root {
  180. struct list_head list;
  181. struct btrfs_key snap_key;
  182. struct buffer_head *commit_root;
  183. struct btrfs_root *root;
  184. };
  185. static int add_dirty_roots(struct btrfs_trans_handle *trans,
  186. struct radix_tree_root *radix,
  187. struct list_head *list)
  188. {
  189. struct dirty_root *dirty;
  190. struct btrfs_root *gang[8];
  191. struct btrfs_root *root;
  192. int i;
  193. int ret;
  194. int err;
  195. while(1) {
  196. ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
  197. ARRAY_SIZE(gang),
  198. BTRFS_ROOT_TRANS_TAG);
  199. if (ret == 0)
  200. break;
  201. for (i = 0; i < ret; i++) {
  202. root = gang[i];
  203. radix_tree_tag_clear(radix,
  204. (unsigned long)root->root_key.objectid,
  205. BTRFS_ROOT_TRANS_TAG);
  206. if (root->commit_root == root->node) {
  207. WARN_ON(bh_blocknr(root->node) !=
  208. btrfs_root_blocknr(&root->root_item));
  209. brelse(root->commit_root);
  210. root->commit_root = NULL;
  211. continue;
  212. }
  213. dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
  214. BUG_ON(!dirty);
  215. memcpy(&dirty->snap_key, &root->root_key,
  216. sizeof(root->root_key));
  217. dirty->commit_root = root->commit_root;
  218. root->commit_root = NULL;
  219. dirty->root = root;
  220. root->root_key.offset = root->fs_info->generation;
  221. btrfs_set_root_blocknr(&root->root_item,
  222. bh_blocknr(root->node));
  223. err = btrfs_insert_root(trans, root->fs_info->tree_root,
  224. &root->root_key,
  225. &root->root_item);
  226. BUG_ON(err);
  227. list_add(&dirty->list, list);
  228. }
  229. }
  230. return 0;
  231. }
  232. static int drop_dirty_roots(struct btrfs_root *tree_root,
  233. struct list_head *list)
  234. {
  235. struct dirty_root *dirty;
  236. struct btrfs_trans_handle *trans;
  237. int ret;
  238. while(!list_empty(list)) {
  239. dirty = list_entry(list->next, struct dirty_root, list);
  240. list_del_init(&dirty->list);
  241. trans = btrfs_start_transaction(tree_root, 1);
  242. ret = btrfs_drop_snapshot(trans, dirty->root,
  243. dirty->commit_root);
  244. BUG_ON(ret);
  245. ret = btrfs_del_root(trans, tree_root, &dirty->snap_key);
  246. BUG_ON(ret);
  247. ret = btrfs_end_transaction(trans, tree_root);
  248. BUG_ON(ret);
  249. kfree(dirty);
  250. }
  251. return 0;
  252. }
  253. int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
  254. struct btrfs_root *root)
  255. {
  256. int ret = 0;
  257. struct btrfs_transaction *cur_trans;
  258. struct btrfs_transaction *prev_trans = NULL;
  259. struct list_head dirty_fs_roots;
  260. DEFINE_WAIT(wait);
  261. INIT_LIST_HEAD(&dirty_fs_roots);
  262. mutex_lock(&root->fs_info->trans_mutex);
  263. if (trans->transaction->in_commit) {
  264. cur_trans = trans->transaction;
  265. trans->transaction->use_count++;
  266. btrfs_end_transaction(trans, root);
  267. ret = wait_for_commit(root, cur_trans);
  268. BUG_ON(ret);
  269. put_transaction(cur_trans);
  270. mutex_unlock(&root->fs_info->trans_mutex);
  271. return 0;
  272. }
  273. cur_trans = trans->transaction;
  274. trans->transaction->in_commit = 1;
  275. while (trans->transaction->num_writers > 1) {
  276. WARN_ON(cur_trans != trans->transaction);
  277. prepare_to_wait(&trans->transaction->writer_wait, &wait,
  278. TASK_UNINTERRUPTIBLE);
  279. if (trans->transaction->num_writers <= 1)
  280. break;
  281. mutex_unlock(&root->fs_info->trans_mutex);
  282. schedule();
  283. mutex_lock(&root->fs_info->trans_mutex);
  284. finish_wait(&trans->transaction->writer_wait, &wait);
  285. }
  286. finish_wait(&trans->transaction->writer_wait, &wait);
  287. WARN_ON(cur_trans != trans->transaction);
  288. add_dirty_roots(trans, &root->fs_info->fs_roots_radix, &dirty_fs_roots);
  289. ret = btrfs_commit_tree_roots(trans, root);
  290. BUG_ON(ret);
  291. cur_trans = root->fs_info->running_transaction;
  292. root->fs_info->running_transaction = NULL;
  293. if (cur_trans->list.prev != &root->fs_info->trans_list) {
  294. prev_trans = list_entry(cur_trans->list.prev,
  295. struct btrfs_transaction, list);
  296. if (prev_trans->commit_done)
  297. prev_trans = NULL;
  298. else
  299. prev_trans->use_count++;
  300. }
  301. mutex_unlock(&root->fs_info->trans_mutex);
  302. mutex_unlock(&root->fs_info->fs_mutex);
  303. ret = btrfs_write_and_wait_transaction(trans, root);
  304. if (prev_trans) {
  305. mutex_lock(&root->fs_info->trans_mutex);
  306. wait_for_commit(root, prev_trans);
  307. put_transaction(prev_trans);
  308. mutex_unlock(&root->fs_info->trans_mutex);
  309. }
  310. btrfs_set_super_generation(root->fs_info->disk_super,
  311. cur_trans->transid);
  312. BUG_ON(ret);
  313. write_ctree_super(trans, root);
  314. mutex_lock(&root->fs_info->fs_mutex);
  315. btrfs_finish_extent_commit(trans, root);
  316. mutex_lock(&root->fs_info->trans_mutex);
  317. cur_trans->commit_done = 1;
  318. wake_up(&cur_trans->commit_wait);
  319. put_transaction(cur_trans);
  320. put_transaction(cur_trans);
  321. mutex_unlock(&root->fs_info->trans_mutex);
  322. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  323. drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
  324. return ret;
  325. }