transaction.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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;
  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. BUG_ON(err);
  236. list_add(&dirty->list, list);
  237. }
  238. }
  239. return 0;
  240. }
  241. static int drop_dirty_roots(struct btrfs_root *tree_root,
  242. struct list_head *list)
  243. {
  244. struct dirty_root *dirty;
  245. struct btrfs_trans_handle *trans;
  246. int ret;
  247. while(!list_empty(list)) {
  248. mutex_lock(&tree_root->fs_info->fs_mutex);
  249. dirty = list_entry(list->next, struct dirty_root, list);
  250. list_del_init(&dirty->list);
  251. trans = btrfs_start_transaction(tree_root, 1);
  252. ret = btrfs_drop_snapshot(trans, dirty->root,
  253. dirty->commit_root);
  254. BUG_ON(ret);
  255. ret = btrfs_del_root(trans, tree_root, &dirty->snap_key);
  256. BUG_ON(ret);
  257. ret = btrfs_end_transaction(trans, tree_root);
  258. BUG_ON(ret);
  259. kfree(dirty);
  260. mutex_unlock(&tree_root->fs_info->fs_mutex);
  261. btrfs_btree_balance_dirty(tree_root);
  262. }
  263. return 0;
  264. }
  265. int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
  266. struct btrfs_root *root)
  267. {
  268. int ret = 0;
  269. struct btrfs_transaction *cur_trans;
  270. struct btrfs_transaction *prev_trans = NULL;
  271. struct list_head dirty_fs_roots;
  272. DEFINE_WAIT(wait);
  273. INIT_LIST_HEAD(&dirty_fs_roots);
  274. mutex_lock(&root->fs_info->trans_mutex);
  275. if (trans->transaction->in_commit) {
  276. cur_trans = trans->transaction;
  277. trans->transaction->use_count++;
  278. btrfs_end_transaction(trans, root);
  279. ret = wait_for_commit(root, cur_trans);
  280. BUG_ON(ret);
  281. put_transaction(cur_trans);
  282. mutex_unlock(&root->fs_info->trans_mutex);
  283. return 0;
  284. }
  285. cur_trans = trans->transaction;
  286. trans->transaction->in_commit = 1;
  287. while (trans->transaction->num_writers > 1) {
  288. WARN_ON(cur_trans != trans->transaction);
  289. prepare_to_wait(&trans->transaction->writer_wait, &wait,
  290. TASK_UNINTERRUPTIBLE);
  291. if (trans->transaction->num_writers <= 1)
  292. break;
  293. mutex_unlock(&root->fs_info->trans_mutex);
  294. schedule();
  295. mutex_lock(&root->fs_info->trans_mutex);
  296. finish_wait(&trans->transaction->writer_wait, &wait);
  297. }
  298. finish_wait(&trans->transaction->writer_wait, &wait);
  299. WARN_ON(cur_trans != trans->transaction);
  300. add_dirty_roots(trans, &root->fs_info->fs_roots_radix, &dirty_fs_roots);
  301. ret = btrfs_commit_tree_roots(trans, root);
  302. BUG_ON(ret);
  303. cur_trans = root->fs_info->running_transaction;
  304. root->fs_info->running_transaction = NULL;
  305. if (cur_trans->list.prev != &root->fs_info->trans_list) {
  306. prev_trans = list_entry(cur_trans->list.prev,
  307. struct btrfs_transaction, list);
  308. if (prev_trans->commit_done)
  309. prev_trans = NULL;
  310. else
  311. prev_trans->use_count++;
  312. }
  313. mutex_unlock(&root->fs_info->trans_mutex);
  314. mutex_unlock(&root->fs_info->fs_mutex);
  315. ret = btrfs_write_and_wait_transaction(trans, root);
  316. if (prev_trans) {
  317. mutex_lock(&root->fs_info->trans_mutex);
  318. wait_for_commit(root, prev_trans);
  319. put_transaction(prev_trans);
  320. mutex_unlock(&root->fs_info->trans_mutex);
  321. }
  322. btrfs_set_super_generation(root->fs_info->disk_super,
  323. cur_trans->transid);
  324. BUG_ON(ret);
  325. write_ctree_super(trans, root);
  326. mutex_lock(&root->fs_info->fs_mutex);
  327. btrfs_finish_extent_commit(trans, root);
  328. mutex_lock(&root->fs_info->trans_mutex);
  329. cur_trans->commit_done = 1;
  330. wake_up(&cur_trans->commit_wait);
  331. put_transaction(cur_trans);
  332. put_transaction(cur_trans);
  333. if (root->fs_info->closing)
  334. list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
  335. else
  336. list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
  337. mutex_unlock(&root->fs_info->trans_mutex);
  338. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  339. if (root->fs_info->closing) {
  340. mutex_unlock(&root->fs_info->fs_mutex);
  341. drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
  342. mutex_lock(&root->fs_info->fs_mutex);
  343. }
  344. return ret;
  345. }
  346. void btrfs_transaction_cleaner(struct work_struct *work)
  347. {
  348. struct btrfs_fs_info *fs_info = container_of(work,
  349. struct btrfs_fs_info,
  350. trans_work.work);
  351. struct btrfs_root *root = fs_info->tree_root;
  352. struct btrfs_transaction *cur;
  353. struct btrfs_trans_handle *trans;
  354. struct list_head dirty_roots;
  355. unsigned long now;
  356. unsigned long delay = HZ * 30;
  357. int ret;
  358. INIT_LIST_HEAD(&dirty_roots);
  359. mutex_lock(&root->fs_info->fs_mutex);
  360. mutex_lock(&root->fs_info->trans_mutex);
  361. cur = root->fs_info->running_transaction;
  362. if (!cur) {
  363. mutex_unlock(&root->fs_info->trans_mutex);
  364. goto out;
  365. }
  366. now = get_seconds();
  367. if (now < cur->start_time || now - cur->start_time < 30) {
  368. mutex_unlock(&root->fs_info->trans_mutex);
  369. delay = HZ * 5;
  370. goto out;
  371. }
  372. mutex_unlock(&root->fs_info->trans_mutex);
  373. trans = btrfs_start_transaction(root, 1);
  374. ret = btrfs_commit_transaction(trans, root);
  375. out:
  376. mutex_unlock(&root->fs_info->fs_mutex);
  377. mutex_lock(&root->fs_info->trans_mutex);
  378. list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
  379. mutex_unlock(&root->fs_info->trans_mutex);
  380. if (!list_empty(&dirty_roots)) {
  381. drop_dirty_roots(root, &dirty_roots);
  382. }
  383. btrfs_transaction_queue_work(root, delay);
  384. }
  385. void btrfs_transaction_queue_work(struct btrfs_root *root, int delay)
  386. {
  387. queue_delayed_work(trans_wq, &root->fs_info->trans_work, delay);
  388. }
  389. void btrfs_transaction_flush_work(struct btrfs_root *root)
  390. {
  391. cancel_rearming_delayed_workqueue(trans_wq, &root->fs_info->trans_work);
  392. flush_workqueue(trans_wq);
  393. }
  394. void __init btrfs_init_transaction_sys(void)
  395. {
  396. trans_wq = create_workqueue("btrfs");
  397. }
  398. void __exit btrfs_exit_transaction_sys(void)
  399. {
  400. destroy_workqueue(trans_wq);
  401. }