transaction.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. static struct workqueue_struct *trans_wq;
  10. #define BTRFS_ROOT_TRANS_TAG 0
  11. static void put_transaction(struct btrfs_transaction *transaction)
  12. {
  13. WARN_ON(transaction->use_count == 0);
  14. transaction->use_count--;
  15. if (transaction->use_count == 0) {
  16. WARN_ON(total_trans == 0);
  17. total_trans--;
  18. list_del_init(&transaction->list);
  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->in_commit = 0;
  39. cur_trans->use_count = 1;
  40. cur_trans->commit_done = 0;
  41. cur_trans->start_time = get_seconds();
  42. list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
  43. init_bit_radix(&cur_trans->dirty_pages);
  44. }
  45. cur_trans->num_writers++;
  46. return 0;
  47. }
  48. struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
  49. int num_blocks)
  50. {
  51. struct btrfs_trans_handle *h =
  52. kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
  53. int ret;
  54. u64 running_trans_id;
  55. mutex_lock(&root->fs_info->trans_mutex);
  56. ret = join_transaction(root);
  57. BUG_ON(ret);
  58. running_trans_id = root->fs_info->running_transaction->transid;
  59. if (root != root->fs_info->tree_root && root->last_trans <
  60. running_trans_id) {
  61. radix_tree_tag_set(&root->fs_info->fs_roots_radix,
  62. (unsigned long)root->root_key.objectid,
  63. BTRFS_ROOT_TRANS_TAG);
  64. root->commit_root = root->node;
  65. get_bh(root->node);
  66. }
  67. root->last_trans = running_trans_id;
  68. h->transid = running_trans_id;
  69. h->transaction = root->fs_info->running_transaction;
  70. h->blocks_reserved = num_blocks;
  71. h->blocks_used = 0;
  72. h->block_group = NULL;
  73. root->fs_info->running_transaction->use_count++;
  74. mutex_unlock(&root->fs_info->trans_mutex);
  75. return h;
  76. }
  77. int btrfs_end_transaction(struct btrfs_trans_handle *trans,
  78. struct btrfs_root *root)
  79. {
  80. struct btrfs_transaction *cur_trans;
  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. unsigned long gang[16];
  97. int ret;
  98. int i;
  99. int err;
  100. int werr = 0;
  101. struct page *page;
  102. struct radix_tree_root *dirty_pages;
  103. struct inode *btree_inode = root->fs_info->btree_inode;
  104. if (!trans || !trans->transaction) {
  105. return filemap_write_and_wait(btree_inode->i_mapping);
  106. }
  107. dirty_pages = &trans->transaction->dirty_pages;
  108. while(1) {
  109. ret = find_first_radix_bit(dirty_pages, gang,
  110. 0, ARRAY_SIZE(gang));
  111. if (!ret)
  112. break;
  113. for (i = 0; i < ret; i++) {
  114. /* FIXME EIO */
  115. clear_radix_bit(dirty_pages, gang[i]);
  116. page = find_lock_page(btree_inode->i_mapping,
  117. gang[i]);
  118. if (!page)
  119. continue;
  120. err = write_one_page(page, 0);
  121. if (err)
  122. werr = err;
  123. page_cache_release(page);
  124. }
  125. }
  126. err = filemap_fdatawait(btree_inode->i_mapping);
  127. if (err)
  128. werr = err;
  129. return werr;
  130. }
  131. int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
  132. struct btrfs_root *root)
  133. {
  134. int ret;
  135. u64 old_extent_block;
  136. struct btrfs_fs_info *fs_info = root->fs_info;
  137. struct btrfs_root *tree_root = fs_info->tree_root;
  138. struct btrfs_root *extent_root = fs_info->extent_root;
  139. struct btrfs_root *dev_root = fs_info->dev_root;
  140. if (btrfs_super_device_root(fs_info->disk_super) !=
  141. bh_blocknr(dev_root->node)) {
  142. btrfs_set_super_device_root(fs_info->disk_super,
  143. bh_blocknr(dev_root->node));
  144. }
  145. btrfs_write_dirty_block_groups(trans, extent_root);
  146. while(1) {
  147. old_extent_block = btrfs_root_blocknr(&extent_root->root_item);
  148. if (old_extent_block == bh_blocknr(extent_root->node))
  149. break;
  150. btrfs_set_root_blocknr(&extent_root->root_item,
  151. bh_blocknr(extent_root->node));
  152. ret = btrfs_update_root(trans, tree_root,
  153. &extent_root->root_key,
  154. &extent_root->root_item);
  155. BUG_ON(ret);
  156. btrfs_write_dirty_block_groups(trans, extent_root);
  157. }
  158. return 0;
  159. }
  160. static int wait_for_commit(struct btrfs_root *root,
  161. struct btrfs_transaction *commit)
  162. {
  163. DEFINE_WAIT(wait);
  164. while(!commit->commit_done) {
  165. prepare_to_wait(&commit->commit_wait, &wait,
  166. TASK_UNINTERRUPTIBLE);
  167. if (commit->commit_done)
  168. break;
  169. mutex_unlock(&root->fs_info->trans_mutex);
  170. schedule();
  171. mutex_lock(&root->fs_info->trans_mutex);
  172. }
  173. finish_wait(&commit->commit_wait, &wait);
  174. return 0;
  175. }
  176. struct dirty_root {
  177. struct list_head list;
  178. struct btrfs_key snap_key;
  179. struct buffer_head *commit_root;
  180. struct btrfs_root *root;
  181. };
  182. static int add_dirty_roots(struct btrfs_trans_handle *trans,
  183. struct radix_tree_root *radix,
  184. struct list_head *list)
  185. {
  186. struct dirty_root *dirty;
  187. struct btrfs_root *gang[8];
  188. struct btrfs_root *root;
  189. int i;
  190. int ret;
  191. int err;
  192. while(1) {
  193. ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
  194. ARRAY_SIZE(gang),
  195. BTRFS_ROOT_TRANS_TAG);
  196. if (ret == 0)
  197. break;
  198. for (i = 0; i < ret; i++) {
  199. root = gang[i];
  200. radix_tree_tag_clear(radix,
  201. (unsigned long)root->root_key.objectid,
  202. BTRFS_ROOT_TRANS_TAG);
  203. if (root->commit_root == root->node) {
  204. WARN_ON(bh_blocknr(root->node) !=
  205. btrfs_root_blocknr(&root->root_item));
  206. brelse(root->commit_root);
  207. root->commit_root = NULL;
  208. continue;
  209. }
  210. dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
  211. BUG_ON(!dirty);
  212. memcpy(&dirty->snap_key, &root->root_key,
  213. sizeof(root->root_key));
  214. dirty->commit_root = root->commit_root;
  215. root->commit_root = NULL;
  216. dirty->root = root;
  217. root->root_key.offset = root->fs_info->generation;
  218. btrfs_set_root_blocknr(&root->root_item,
  219. bh_blocknr(root->node));
  220. err = btrfs_insert_root(trans, root->fs_info->tree_root,
  221. &root->root_key,
  222. &root->root_item);
  223. BUG_ON(err);
  224. list_add(&dirty->list, list);
  225. }
  226. }
  227. return 0;
  228. }
  229. static int drop_dirty_roots(struct btrfs_root *tree_root,
  230. struct list_head *list)
  231. {
  232. struct dirty_root *dirty;
  233. struct btrfs_trans_handle *trans;
  234. int ret;
  235. while(!list_empty(list)) {
  236. mutex_lock(&tree_root->fs_info->fs_mutex);
  237. dirty = list_entry(list->next, struct dirty_root, list);
  238. list_del_init(&dirty->list);
  239. trans = btrfs_start_transaction(tree_root, 1);
  240. ret = btrfs_drop_snapshot(trans, dirty->root,
  241. dirty->commit_root);
  242. BUG_ON(ret);
  243. ret = btrfs_del_root(trans, tree_root, &dirty->snap_key);
  244. BUG_ON(ret);
  245. ret = btrfs_end_transaction(trans, tree_root);
  246. BUG_ON(ret);
  247. kfree(dirty);
  248. mutex_unlock(&tree_root->fs_info->fs_mutex);
  249. }
  250. return 0;
  251. }
  252. int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
  253. struct btrfs_root *root)
  254. {
  255. int ret = 0;
  256. struct btrfs_transaction *cur_trans;
  257. struct btrfs_transaction *prev_trans = NULL;
  258. struct list_head dirty_fs_roots;
  259. DEFINE_WAIT(wait);
  260. INIT_LIST_HEAD(&dirty_fs_roots);
  261. mutex_lock(&root->fs_info->trans_mutex);
  262. if (trans->transaction->in_commit) {
  263. cur_trans = trans->transaction;
  264. trans->transaction->use_count++;
  265. btrfs_end_transaction(trans, root);
  266. ret = wait_for_commit(root, cur_trans);
  267. BUG_ON(ret);
  268. put_transaction(cur_trans);
  269. mutex_unlock(&root->fs_info->trans_mutex);
  270. return 0;
  271. }
  272. cur_trans = trans->transaction;
  273. trans->transaction->in_commit = 1;
  274. while (trans->transaction->num_writers > 1) {
  275. WARN_ON(cur_trans != trans->transaction);
  276. prepare_to_wait(&trans->transaction->writer_wait, &wait,
  277. TASK_UNINTERRUPTIBLE);
  278. if (trans->transaction->num_writers <= 1)
  279. break;
  280. mutex_unlock(&root->fs_info->trans_mutex);
  281. schedule();
  282. mutex_lock(&root->fs_info->trans_mutex);
  283. finish_wait(&trans->transaction->writer_wait, &wait);
  284. }
  285. finish_wait(&trans->transaction->writer_wait, &wait);
  286. WARN_ON(cur_trans != trans->transaction);
  287. add_dirty_roots(trans, &root->fs_info->fs_roots_radix, &dirty_fs_roots);
  288. ret = btrfs_commit_tree_roots(trans, root);
  289. BUG_ON(ret);
  290. cur_trans = root->fs_info->running_transaction;
  291. root->fs_info->running_transaction = NULL;
  292. if (cur_trans->list.prev != &root->fs_info->trans_list) {
  293. prev_trans = list_entry(cur_trans->list.prev,
  294. struct btrfs_transaction, list);
  295. if (prev_trans->commit_done)
  296. prev_trans = NULL;
  297. else
  298. prev_trans->use_count++;
  299. }
  300. mutex_unlock(&root->fs_info->trans_mutex);
  301. mutex_unlock(&root->fs_info->fs_mutex);
  302. ret = btrfs_write_and_wait_transaction(trans, root);
  303. if (prev_trans) {
  304. mutex_lock(&root->fs_info->trans_mutex);
  305. wait_for_commit(root, prev_trans);
  306. put_transaction(prev_trans);
  307. mutex_unlock(&root->fs_info->trans_mutex);
  308. }
  309. btrfs_set_super_generation(root->fs_info->disk_super,
  310. cur_trans->transid);
  311. BUG_ON(ret);
  312. write_ctree_super(trans, root);
  313. mutex_lock(&root->fs_info->fs_mutex);
  314. btrfs_finish_extent_commit(trans, root);
  315. mutex_lock(&root->fs_info->trans_mutex);
  316. cur_trans->commit_done = 1;
  317. wake_up(&cur_trans->commit_wait);
  318. put_transaction(cur_trans);
  319. put_transaction(cur_trans);
  320. if (root->fs_info->closing)
  321. list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
  322. else
  323. list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
  324. mutex_unlock(&root->fs_info->trans_mutex);
  325. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  326. if (root->fs_info->closing) {
  327. mutex_unlock(&root->fs_info->fs_mutex);
  328. drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
  329. mutex_lock(&root->fs_info->fs_mutex);
  330. }
  331. return ret;
  332. }
  333. void btrfs_transaction_cleaner(struct work_struct *work)
  334. {
  335. struct btrfs_fs_info *fs_info = container_of(work,
  336. struct btrfs_fs_info,
  337. trans_work.work);
  338. struct btrfs_root *root = fs_info->tree_root;
  339. struct btrfs_transaction *cur;
  340. struct btrfs_trans_handle *trans;
  341. struct list_head dirty_roots;
  342. unsigned long now;
  343. unsigned long delay = HZ * 30;
  344. int ret;
  345. INIT_LIST_HEAD(&dirty_roots);
  346. mutex_lock(&root->fs_info->trans_mutex);
  347. list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
  348. mutex_unlock(&root->fs_info->trans_mutex);
  349. if (!list_empty(&dirty_roots)) {
  350. drop_dirty_roots(root, &dirty_roots);
  351. }
  352. mutex_lock(&root->fs_info->fs_mutex);
  353. mutex_lock(&root->fs_info->trans_mutex);
  354. cur = root->fs_info->running_transaction;
  355. if (!cur) {
  356. mutex_unlock(&root->fs_info->trans_mutex);
  357. goto out;
  358. }
  359. now = get_seconds();
  360. if (now < cur->start_time || now - cur->start_time < 30) {
  361. mutex_unlock(&root->fs_info->trans_mutex);
  362. delay = HZ * 5;
  363. goto out;
  364. }
  365. mutex_unlock(&root->fs_info->trans_mutex);
  366. trans = btrfs_start_transaction(root, 1);
  367. ret = btrfs_commit_transaction(trans, root);
  368. out:
  369. mutex_unlock(&root->fs_info->fs_mutex);
  370. btrfs_transaction_queue_work(root, delay);
  371. }
  372. void btrfs_transaction_queue_work(struct btrfs_root *root, int delay)
  373. {
  374. queue_delayed_work(trans_wq, &root->fs_info->trans_work, delay);
  375. }
  376. void btrfs_transaction_flush_work(struct btrfs_root *root)
  377. {
  378. cancel_rearming_delayed_workqueue(trans_wq, &root->fs_info->trans_work);
  379. flush_workqueue(trans_wq);
  380. }
  381. void __init btrfs_init_transaction_sys(void)
  382. {
  383. trans_wq = create_workqueue("btrfs");
  384. }
  385. void __exit btrfs_exit_transaction_sys(void)
  386. {
  387. destroy_workqueue(trans_wq);
  388. }