transaction.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*
  2. * Copyright (C) 2007 Oracle. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/fs.h>
  20. #include <linux/sched.h>
  21. #include "ctree.h"
  22. #include "disk-io.h"
  23. #include "transaction.h"
  24. static int total_trans = 0;
  25. extern struct kmem_cache *btrfs_trans_handle_cachep;
  26. extern struct kmem_cache *btrfs_transaction_cachep;
  27. static struct workqueue_struct *trans_wq;
  28. #define BTRFS_ROOT_TRANS_TAG 0
  29. static void put_transaction(struct btrfs_transaction *transaction)
  30. {
  31. WARN_ON(transaction->use_count == 0);
  32. transaction->use_count--;
  33. if (transaction->use_count == 0) {
  34. WARN_ON(total_trans == 0);
  35. total_trans--;
  36. list_del_init(&transaction->list);
  37. memset(transaction, 0, sizeof(*transaction));
  38. kmem_cache_free(btrfs_transaction_cachep, transaction);
  39. }
  40. }
  41. static int join_transaction(struct btrfs_root *root)
  42. {
  43. struct btrfs_transaction *cur_trans;
  44. cur_trans = root->fs_info->running_transaction;
  45. if (!cur_trans) {
  46. cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
  47. GFP_NOFS);
  48. total_trans++;
  49. BUG_ON(!cur_trans);
  50. root->fs_info->generation++;
  51. root->fs_info->running_transaction = cur_trans;
  52. cur_trans->num_writers = 0;
  53. cur_trans->transid = root->fs_info->generation;
  54. init_waitqueue_head(&cur_trans->writer_wait);
  55. init_waitqueue_head(&cur_trans->commit_wait);
  56. cur_trans->in_commit = 0;
  57. cur_trans->use_count = 1;
  58. cur_trans->commit_done = 0;
  59. cur_trans->start_time = get_seconds();
  60. list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
  61. init_bit_radix(&cur_trans->dirty_pages);
  62. }
  63. cur_trans->num_writers++;
  64. return 0;
  65. }
  66. struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
  67. int num_blocks)
  68. {
  69. struct btrfs_trans_handle *h =
  70. kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
  71. int ret;
  72. u64 running_trans_id;
  73. mutex_lock(&root->fs_info->trans_mutex);
  74. ret = join_transaction(root);
  75. BUG_ON(ret);
  76. running_trans_id = root->fs_info->running_transaction->transid;
  77. if (root != root->fs_info->tree_root && root->last_trans <
  78. running_trans_id) {
  79. radix_tree_tag_set(&root->fs_info->fs_roots_radix,
  80. (unsigned long)root->root_key.objectid,
  81. BTRFS_ROOT_TRANS_TAG);
  82. root->commit_root = root->node;
  83. get_bh(root->node);
  84. }
  85. root->last_trans = running_trans_id;
  86. h->transid = running_trans_id;
  87. h->transaction = root->fs_info->running_transaction;
  88. h->blocks_reserved = num_blocks;
  89. h->blocks_used = 0;
  90. h->block_group = NULL;
  91. root->fs_info->running_transaction->use_count++;
  92. mutex_unlock(&root->fs_info->trans_mutex);
  93. return h;
  94. }
  95. int btrfs_end_transaction(struct btrfs_trans_handle *trans,
  96. struct btrfs_root *root)
  97. {
  98. struct btrfs_transaction *cur_trans;
  99. mutex_lock(&root->fs_info->trans_mutex);
  100. cur_trans = root->fs_info->running_transaction;
  101. WARN_ON(cur_trans->num_writers < 1);
  102. if (waitqueue_active(&cur_trans->writer_wait))
  103. wake_up(&cur_trans->writer_wait);
  104. cur_trans->num_writers--;
  105. put_transaction(cur_trans);
  106. mutex_unlock(&root->fs_info->trans_mutex);
  107. memset(trans, 0, sizeof(*trans));
  108. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  109. return 0;
  110. }
  111. int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
  112. struct btrfs_root *root)
  113. {
  114. unsigned long gang[16];
  115. int ret;
  116. int i;
  117. int err;
  118. int werr = 0;
  119. struct page *page;
  120. struct radix_tree_root *dirty_pages;
  121. struct inode *btree_inode = root->fs_info->btree_inode;
  122. if (!trans || !trans->transaction) {
  123. return filemap_write_and_wait(btree_inode->i_mapping);
  124. }
  125. dirty_pages = &trans->transaction->dirty_pages;
  126. while(1) {
  127. ret = find_first_radix_bit(dirty_pages, gang,
  128. 0, ARRAY_SIZE(gang));
  129. if (!ret)
  130. break;
  131. for (i = 0; i < ret; i++) {
  132. /* FIXME EIO */
  133. clear_radix_bit(dirty_pages, gang[i]);
  134. page = find_lock_page(btree_inode->i_mapping,
  135. gang[i]);
  136. if (!page)
  137. continue;
  138. err = write_one_page(page, 0);
  139. if (err)
  140. werr = err;
  141. page_cache_release(page);
  142. }
  143. }
  144. err = filemap_fdatawait(btree_inode->i_mapping);
  145. if (err)
  146. werr = err;
  147. return werr;
  148. }
  149. int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
  150. struct btrfs_root *root)
  151. {
  152. int ret;
  153. u64 old_extent_block;
  154. struct btrfs_fs_info *fs_info = root->fs_info;
  155. struct btrfs_root *tree_root = fs_info->tree_root;
  156. struct btrfs_root *extent_root = fs_info->extent_root;
  157. btrfs_write_dirty_block_groups(trans, extent_root);
  158. while(1) {
  159. old_extent_block = btrfs_root_blocknr(&extent_root->root_item);
  160. if (old_extent_block == bh_blocknr(extent_root->node))
  161. break;
  162. btrfs_set_root_blocknr(&extent_root->root_item,
  163. bh_blocknr(extent_root->node));
  164. ret = btrfs_update_root(trans, tree_root,
  165. &extent_root->root_key,
  166. &extent_root->root_item);
  167. BUG_ON(ret);
  168. btrfs_write_dirty_block_groups(trans, extent_root);
  169. }
  170. return 0;
  171. }
  172. static int wait_for_commit(struct btrfs_root *root,
  173. struct btrfs_transaction *commit)
  174. {
  175. DEFINE_WAIT(wait);
  176. while(!commit->commit_done) {
  177. prepare_to_wait(&commit->commit_wait, &wait,
  178. TASK_UNINTERRUPTIBLE);
  179. if (commit->commit_done)
  180. break;
  181. mutex_unlock(&root->fs_info->trans_mutex);
  182. schedule();
  183. mutex_lock(&root->fs_info->trans_mutex);
  184. }
  185. finish_wait(&commit->commit_wait, &wait);
  186. return 0;
  187. }
  188. struct dirty_root {
  189. struct list_head list;
  190. struct btrfs_key snap_key;
  191. struct buffer_head *commit_root;
  192. struct btrfs_root *root;
  193. };
  194. static int add_dirty_roots(struct btrfs_trans_handle *trans,
  195. struct radix_tree_root *radix,
  196. struct list_head *list)
  197. {
  198. struct dirty_root *dirty;
  199. struct btrfs_root *gang[8];
  200. struct btrfs_root *root;
  201. int i;
  202. int ret;
  203. int err = 0;
  204. while(1) {
  205. ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
  206. ARRAY_SIZE(gang),
  207. BTRFS_ROOT_TRANS_TAG);
  208. if (ret == 0)
  209. break;
  210. for (i = 0; i < ret; i++) {
  211. root = gang[i];
  212. radix_tree_tag_clear(radix,
  213. (unsigned long)root->root_key.objectid,
  214. BTRFS_ROOT_TRANS_TAG);
  215. if (root->commit_root == root->node) {
  216. WARN_ON(bh_blocknr(root->node) !=
  217. btrfs_root_blocknr(&root->root_item));
  218. brelse(root->commit_root);
  219. root->commit_root = NULL;
  220. continue;
  221. }
  222. dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
  223. BUG_ON(!dirty);
  224. memcpy(&dirty->snap_key, &root->root_key,
  225. sizeof(root->root_key));
  226. dirty->commit_root = root->commit_root;
  227. root->commit_root = NULL;
  228. dirty->root = root;
  229. root->root_key.offset = root->fs_info->generation;
  230. btrfs_set_root_blocknr(&root->root_item,
  231. bh_blocknr(root->node));
  232. err = btrfs_insert_root(trans, root->fs_info->tree_root,
  233. &root->root_key,
  234. &root->root_item);
  235. if (err)
  236. break;
  237. list_add(&dirty->list, list);
  238. }
  239. }
  240. return err;
  241. }
  242. static int drop_dirty_roots(struct btrfs_root *tree_root,
  243. struct list_head *list)
  244. {
  245. struct dirty_root *dirty;
  246. struct btrfs_trans_handle *trans;
  247. int ret = 0;
  248. while(!list_empty(list)) {
  249. mutex_lock(&tree_root->fs_info->fs_mutex);
  250. dirty = list_entry(list->next, struct dirty_root, list);
  251. list_del_init(&dirty->list);
  252. trans = btrfs_start_transaction(tree_root, 1);
  253. ret = btrfs_drop_snapshot(trans, dirty->root,
  254. dirty->commit_root);
  255. BUG_ON(ret);
  256. ret = btrfs_del_root(trans, tree_root, &dirty->snap_key);
  257. if (ret)
  258. break;
  259. ret = btrfs_end_transaction(trans, tree_root);
  260. BUG_ON(ret);
  261. kfree(dirty);
  262. mutex_unlock(&tree_root->fs_info->fs_mutex);
  263. btrfs_btree_balance_dirty(tree_root);
  264. }
  265. return ret;
  266. }
  267. int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
  268. struct btrfs_root *root)
  269. {
  270. int ret = 0;
  271. struct btrfs_transaction *cur_trans;
  272. struct btrfs_transaction *prev_trans = NULL;
  273. struct list_head dirty_fs_roots;
  274. DEFINE_WAIT(wait);
  275. INIT_LIST_HEAD(&dirty_fs_roots);
  276. mutex_lock(&root->fs_info->trans_mutex);
  277. if (trans->transaction->in_commit) {
  278. cur_trans = trans->transaction;
  279. trans->transaction->use_count++;
  280. btrfs_end_transaction(trans, root);
  281. ret = wait_for_commit(root, cur_trans);
  282. BUG_ON(ret);
  283. put_transaction(cur_trans);
  284. mutex_unlock(&root->fs_info->trans_mutex);
  285. return 0;
  286. }
  287. cur_trans = trans->transaction;
  288. trans->transaction->in_commit = 1;
  289. while (trans->transaction->num_writers > 1) {
  290. WARN_ON(cur_trans != trans->transaction);
  291. prepare_to_wait(&trans->transaction->writer_wait, &wait,
  292. TASK_UNINTERRUPTIBLE);
  293. if (trans->transaction->num_writers <= 1)
  294. break;
  295. mutex_unlock(&root->fs_info->trans_mutex);
  296. schedule();
  297. mutex_lock(&root->fs_info->trans_mutex);
  298. finish_wait(&trans->transaction->writer_wait, &wait);
  299. }
  300. finish_wait(&trans->transaction->writer_wait, &wait);
  301. WARN_ON(cur_trans != trans->transaction);
  302. ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
  303. &dirty_fs_roots);
  304. BUG_ON(ret);
  305. ret = btrfs_commit_tree_roots(trans, root);
  306. BUG_ON(ret);
  307. cur_trans = root->fs_info->running_transaction;
  308. root->fs_info->running_transaction = NULL;
  309. if (cur_trans->list.prev != &root->fs_info->trans_list) {
  310. prev_trans = list_entry(cur_trans->list.prev,
  311. struct btrfs_transaction, list);
  312. if (prev_trans->commit_done)
  313. prev_trans = NULL;
  314. else
  315. prev_trans->use_count++;
  316. }
  317. mutex_unlock(&root->fs_info->trans_mutex);
  318. mutex_unlock(&root->fs_info->fs_mutex);
  319. ret = btrfs_write_and_wait_transaction(trans, root);
  320. if (prev_trans) {
  321. mutex_lock(&root->fs_info->trans_mutex);
  322. wait_for_commit(root, prev_trans);
  323. put_transaction(prev_trans);
  324. mutex_unlock(&root->fs_info->trans_mutex);
  325. }
  326. btrfs_set_super_generation(root->fs_info->disk_super,
  327. cur_trans->transid);
  328. BUG_ON(ret);
  329. write_ctree_super(trans, root);
  330. mutex_lock(&root->fs_info->fs_mutex);
  331. btrfs_finish_extent_commit(trans, root);
  332. mutex_lock(&root->fs_info->trans_mutex);
  333. cur_trans->commit_done = 1;
  334. wake_up(&cur_trans->commit_wait);
  335. put_transaction(cur_trans);
  336. put_transaction(cur_trans);
  337. if (root->fs_info->closing)
  338. list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
  339. else
  340. list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
  341. mutex_unlock(&root->fs_info->trans_mutex);
  342. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  343. if (root->fs_info->closing) {
  344. mutex_unlock(&root->fs_info->fs_mutex);
  345. drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
  346. mutex_lock(&root->fs_info->fs_mutex);
  347. }
  348. return ret;
  349. }
  350. void btrfs_transaction_cleaner(struct work_struct *work)
  351. {
  352. struct btrfs_fs_info *fs_info = container_of(work,
  353. struct btrfs_fs_info,
  354. trans_work.work);
  355. struct btrfs_root *root = fs_info->tree_root;
  356. struct btrfs_transaction *cur;
  357. struct btrfs_trans_handle *trans;
  358. struct list_head dirty_roots;
  359. unsigned long now;
  360. unsigned long delay = HZ * 30;
  361. int ret;
  362. INIT_LIST_HEAD(&dirty_roots);
  363. mutex_lock(&root->fs_info->fs_mutex);
  364. mutex_lock(&root->fs_info->trans_mutex);
  365. cur = root->fs_info->running_transaction;
  366. if (!cur) {
  367. mutex_unlock(&root->fs_info->trans_mutex);
  368. goto out;
  369. }
  370. now = get_seconds();
  371. if (now < cur->start_time || now - cur->start_time < 30) {
  372. mutex_unlock(&root->fs_info->trans_mutex);
  373. delay = HZ * 5;
  374. goto out;
  375. }
  376. mutex_unlock(&root->fs_info->trans_mutex);
  377. trans = btrfs_start_transaction(root, 1);
  378. ret = btrfs_commit_transaction(trans, root);
  379. out:
  380. mutex_unlock(&root->fs_info->fs_mutex);
  381. mutex_lock(&root->fs_info->trans_mutex);
  382. list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
  383. mutex_unlock(&root->fs_info->trans_mutex);
  384. if (!list_empty(&dirty_roots)) {
  385. drop_dirty_roots(root, &dirty_roots);
  386. }
  387. btrfs_transaction_queue_work(root, delay);
  388. }
  389. void btrfs_transaction_queue_work(struct btrfs_root *root, int delay)
  390. {
  391. queue_delayed_work(trans_wq, &root->fs_info->trans_work, delay);
  392. }
  393. void btrfs_transaction_flush_work(struct btrfs_root *root)
  394. {
  395. cancel_rearming_delayed_workqueue(trans_wq, &root->fs_info->trans_work);
  396. flush_workqueue(trans_wq);
  397. }
  398. void __init btrfs_init_transaction_sys(void)
  399. {
  400. trans_wq = create_workqueue("btrfs");
  401. }
  402. void __exit btrfs_exit_transaction_sys(void)
  403. {
  404. destroy_workqueue(trans_wq);
  405. }