transaction.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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,
  114. 0, ARRAY_SIZE(gang));
  115. if (!ret)
  116. break;
  117. for (i = 0; i < ret; i++) {
  118. /* FIXME EIO */
  119. clear_radix_bit(dirty_pages, gang[i]);
  120. page = find_lock_page(btree_inode->i_mapping,
  121. gang[i]);
  122. if (!page)
  123. continue;
  124. err = write_one_page(page, 0);
  125. if (err)
  126. werr = err;
  127. page_cache_release(page);
  128. }
  129. }
  130. err = filemap_fdatawait(btree_inode->i_mapping);
  131. if (err)
  132. werr = err;
  133. return werr;
  134. }
  135. int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
  136. struct btrfs_root *root)
  137. {
  138. int ret;
  139. u64 old_extent_block;
  140. struct btrfs_fs_info *fs_info = root->fs_info;
  141. struct btrfs_root *tree_root = fs_info->tree_root;
  142. struct btrfs_root *extent_root = fs_info->extent_root;
  143. struct btrfs_root *dev_root = fs_info->dev_root;
  144. if (btrfs_super_device_root(fs_info->disk_super) !=
  145. bh_blocknr(dev_root->node)) {
  146. btrfs_set_super_device_root(fs_info->disk_super,
  147. bh_blocknr(dev_root->node));
  148. }
  149. btrfs_write_dirty_block_groups(trans, extent_root);
  150. while(1) {
  151. old_extent_block = btrfs_root_blocknr(&extent_root->root_item);
  152. if (old_extent_block == bh_blocknr(extent_root->node))
  153. break;
  154. btrfs_set_root_blocknr(&extent_root->root_item,
  155. bh_blocknr(extent_root->node));
  156. ret = btrfs_update_root(trans, tree_root,
  157. &extent_root->root_key,
  158. &extent_root->root_item);
  159. BUG_ON(ret);
  160. btrfs_write_dirty_block_groups(trans, extent_root);
  161. }
  162. return 0;
  163. }
  164. static int wait_for_commit(struct btrfs_root *root,
  165. struct btrfs_transaction *commit)
  166. {
  167. DEFINE_WAIT(wait);
  168. while(!commit->commit_done) {
  169. prepare_to_wait(&commit->commit_wait, &wait,
  170. TASK_UNINTERRUPTIBLE);
  171. if (commit->commit_done)
  172. break;
  173. mutex_unlock(&root->fs_info->trans_mutex);
  174. schedule();
  175. mutex_lock(&root->fs_info->trans_mutex);
  176. }
  177. finish_wait(&commit->commit_wait, &wait);
  178. return 0;
  179. }
  180. struct dirty_root {
  181. struct list_head list;
  182. struct btrfs_key snap_key;
  183. struct buffer_head *commit_root;
  184. struct btrfs_root *root;
  185. };
  186. static int add_dirty_roots(struct btrfs_trans_handle *trans,
  187. struct radix_tree_root *radix,
  188. struct list_head *list)
  189. {
  190. struct dirty_root *dirty;
  191. struct btrfs_root *gang[8];
  192. struct btrfs_root *root;
  193. int i;
  194. int ret;
  195. int err;
  196. while(1) {
  197. ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
  198. ARRAY_SIZE(gang),
  199. BTRFS_ROOT_TRANS_TAG);
  200. if (ret == 0)
  201. break;
  202. for (i = 0; i < ret; i++) {
  203. root = gang[i];
  204. radix_tree_tag_clear(radix,
  205. (unsigned long)root->root_key.objectid,
  206. BTRFS_ROOT_TRANS_TAG);
  207. if (root->commit_root == root->node) {
  208. WARN_ON(bh_blocknr(root->node) !=
  209. btrfs_root_blocknr(&root->root_item));
  210. brelse(root->commit_root);
  211. root->commit_root = NULL;
  212. continue;
  213. }
  214. dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
  215. BUG_ON(!dirty);
  216. memcpy(&dirty->snap_key, &root->root_key,
  217. sizeof(root->root_key));
  218. dirty->commit_root = root->commit_root;
  219. root->commit_root = NULL;
  220. dirty->root = root;
  221. root->root_key.offset = root->fs_info->generation;
  222. btrfs_set_root_blocknr(&root->root_item,
  223. bh_blocknr(root->node));
  224. err = btrfs_insert_root(trans, root->fs_info->tree_root,
  225. &root->root_key,
  226. &root->root_item);
  227. BUG_ON(err);
  228. list_add(&dirty->list, list);
  229. }
  230. }
  231. return 0;
  232. }
  233. static int drop_dirty_roots(struct btrfs_root *tree_root,
  234. struct list_head *list)
  235. {
  236. struct dirty_root *dirty;
  237. struct btrfs_trans_handle *trans;
  238. int ret;
  239. while(!list_empty(list)) {
  240. dirty = list_entry(list->next, struct dirty_root, list);
  241. list_del_init(&dirty->list);
  242. trans = btrfs_start_transaction(tree_root, 1);
  243. ret = btrfs_drop_snapshot(trans, dirty->root,
  244. dirty->commit_root);
  245. BUG_ON(ret);
  246. ret = btrfs_del_root(trans, tree_root, &dirty->snap_key);
  247. BUG_ON(ret);
  248. ret = btrfs_end_transaction(trans, tree_root);
  249. BUG_ON(ret);
  250. kfree(dirty);
  251. }
  252. return 0;
  253. }
  254. int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
  255. struct btrfs_root *root)
  256. {
  257. int ret = 0;
  258. struct btrfs_transaction *cur_trans;
  259. struct btrfs_transaction *prev_trans = NULL;
  260. struct list_head dirty_fs_roots;
  261. DEFINE_WAIT(wait);
  262. INIT_LIST_HEAD(&dirty_fs_roots);
  263. mutex_lock(&root->fs_info->trans_mutex);
  264. if (trans->transaction->in_commit) {
  265. cur_trans = trans->transaction;
  266. trans->transaction->use_count++;
  267. btrfs_end_transaction(trans, root);
  268. ret = wait_for_commit(root, cur_trans);
  269. BUG_ON(ret);
  270. put_transaction(cur_trans);
  271. mutex_unlock(&root->fs_info->trans_mutex);
  272. return 0;
  273. }
  274. cur_trans = trans->transaction;
  275. trans->transaction->in_commit = 1;
  276. while (trans->transaction->num_writers > 1) {
  277. WARN_ON(cur_trans != trans->transaction);
  278. prepare_to_wait(&trans->transaction->writer_wait, &wait,
  279. TASK_UNINTERRUPTIBLE);
  280. if (trans->transaction->num_writers <= 1)
  281. break;
  282. mutex_unlock(&root->fs_info->trans_mutex);
  283. schedule();
  284. mutex_lock(&root->fs_info->trans_mutex);
  285. finish_wait(&trans->transaction->writer_wait, &wait);
  286. }
  287. finish_wait(&trans->transaction->writer_wait, &wait);
  288. WARN_ON(cur_trans != trans->transaction);
  289. add_dirty_roots(trans, &root->fs_info->fs_roots_radix, &dirty_fs_roots);
  290. ret = btrfs_commit_tree_roots(trans, root);
  291. BUG_ON(ret);
  292. cur_trans = root->fs_info->running_transaction;
  293. root->fs_info->running_transaction = NULL;
  294. if (cur_trans->list.prev != &root->fs_info->trans_list) {
  295. prev_trans = list_entry(cur_trans->list.prev,
  296. struct btrfs_transaction, list);
  297. if (prev_trans->commit_done)
  298. prev_trans = NULL;
  299. else
  300. prev_trans->use_count++;
  301. }
  302. mutex_unlock(&root->fs_info->trans_mutex);
  303. mutex_unlock(&root->fs_info->fs_mutex);
  304. ret = btrfs_write_and_wait_transaction(trans, root);
  305. if (prev_trans) {
  306. mutex_lock(&root->fs_info->trans_mutex);
  307. wait_for_commit(root, prev_trans);
  308. put_transaction(prev_trans);
  309. mutex_unlock(&root->fs_info->trans_mutex);
  310. }
  311. btrfs_set_super_generation(root->fs_info->disk_super,
  312. cur_trans->transid);
  313. BUG_ON(ret);
  314. write_ctree_super(trans, root);
  315. mutex_lock(&root->fs_info->fs_mutex);
  316. btrfs_finish_extent_commit(trans, root);
  317. mutex_lock(&root->fs_info->trans_mutex);
  318. cur_trans->commit_done = 1;
  319. wake_up(&cur_trans->commit_wait);
  320. put_transaction(cur_trans);
  321. put_transaction(cur_trans);
  322. mutex_unlock(&root->fs_info->trans_mutex);
  323. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  324. drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
  325. return ret;
  326. }