transaction.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. if (root->root_item.refs != 0) {
  80. radix_tree_tag_set(&root->fs_info->fs_roots_radix,
  81. (unsigned long)root->root_key.objectid,
  82. BTRFS_ROOT_TRANS_TAG);
  83. root->commit_root = root->node;
  84. get_bh(root->node);
  85. } else {
  86. WARN_ON(1);
  87. }
  88. }
  89. root->last_trans = running_trans_id;
  90. h->transid = running_trans_id;
  91. h->transaction = root->fs_info->running_transaction;
  92. h->blocks_reserved = num_blocks;
  93. h->blocks_used = 0;
  94. h->block_group = NULL;
  95. root->fs_info->running_transaction->use_count++;
  96. mutex_unlock(&root->fs_info->trans_mutex);
  97. return h;
  98. }
  99. int btrfs_end_transaction(struct btrfs_trans_handle *trans,
  100. struct btrfs_root *root)
  101. {
  102. struct btrfs_transaction *cur_trans;
  103. mutex_lock(&root->fs_info->trans_mutex);
  104. cur_trans = root->fs_info->running_transaction;
  105. WARN_ON(cur_trans->num_writers < 1);
  106. if (waitqueue_active(&cur_trans->writer_wait))
  107. wake_up(&cur_trans->writer_wait);
  108. cur_trans->num_writers--;
  109. put_transaction(cur_trans);
  110. mutex_unlock(&root->fs_info->trans_mutex);
  111. memset(trans, 0, sizeof(*trans));
  112. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  113. return 0;
  114. }
  115. int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
  116. struct btrfs_root *root)
  117. {
  118. unsigned long gang[16];
  119. int ret;
  120. int i;
  121. int err;
  122. int werr = 0;
  123. struct page *page;
  124. struct radix_tree_root *dirty_pages;
  125. struct inode *btree_inode = root->fs_info->btree_inode;
  126. if (!trans || !trans->transaction) {
  127. return filemap_write_and_wait(btree_inode->i_mapping);
  128. }
  129. dirty_pages = &trans->transaction->dirty_pages;
  130. while(1) {
  131. ret = find_first_radix_bit(dirty_pages, gang,
  132. 0, ARRAY_SIZE(gang));
  133. if (!ret)
  134. break;
  135. for (i = 0; i < ret; i++) {
  136. /* FIXME EIO */
  137. clear_radix_bit(dirty_pages, gang[i]);
  138. page = find_lock_page(btree_inode->i_mapping,
  139. gang[i]);
  140. if (!page)
  141. continue;
  142. err = write_one_page(page, 0);
  143. if (err)
  144. werr = err;
  145. page_cache_release(page);
  146. }
  147. }
  148. err = filemap_fdatawait(btree_inode->i_mapping);
  149. if (err)
  150. werr = err;
  151. return werr;
  152. }
  153. int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
  154. struct btrfs_root *root)
  155. {
  156. int ret;
  157. u64 old_extent_block;
  158. struct btrfs_fs_info *fs_info = root->fs_info;
  159. struct btrfs_root *tree_root = fs_info->tree_root;
  160. struct btrfs_root *extent_root = fs_info->extent_root;
  161. btrfs_write_dirty_block_groups(trans, extent_root);
  162. while(1) {
  163. old_extent_block = btrfs_root_blocknr(&extent_root->root_item);
  164. if (old_extent_block == bh_blocknr(extent_root->node))
  165. break;
  166. btrfs_set_root_blocknr(&extent_root->root_item,
  167. bh_blocknr(extent_root->node));
  168. ret = btrfs_update_root(trans, tree_root,
  169. &extent_root->root_key,
  170. &extent_root->root_item);
  171. BUG_ON(ret);
  172. btrfs_write_dirty_block_groups(trans, extent_root);
  173. }
  174. return 0;
  175. }
  176. static int wait_for_commit(struct btrfs_root *root,
  177. struct btrfs_transaction *commit)
  178. {
  179. DEFINE_WAIT(wait);
  180. while(!commit->commit_done) {
  181. prepare_to_wait(&commit->commit_wait, &wait,
  182. TASK_UNINTERRUPTIBLE);
  183. if (commit->commit_done)
  184. break;
  185. mutex_unlock(&root->fs_info->trans_mutex);
  186. schedule();
  187. mutex_lock(&root->fs_info->trans_mutex);
  188. }
  189. finish_wait(&commit->commit_wait, &wait);
  190. return 0;
  191. }
  192. struct dirty_root {
  193. struct list_head list;
  194. struct btrfs_key snap_key;
  195. struct buffer_head *commit_root;
  196. struct btrfs_root *root;
  197. int free_on_drop;
  198. };
  199. int btrfs_add_dead_root(struct btrfs_root *root, struct list_head *dead_list)
  200. {
  201. struct dirty_root *dirty;
  202. dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
  203. if (!dirty)
  204. return -ENOMEM;
  205. memcpy(&dirty->snap_key, &root->root_key, sizeof(root->root_key));
  206. dirty->commit_root = root->node;
  207. dirty->root = root;
  208. dirty->free_on_drop = 1;
  209. list_add(&dirty->list, dead_list);
  210. return 0;
  211. }
  212. static int add_dirty_roots(struct btrfs_trans_handle *trans,
  213. struct radix_tree_root *radix,
  214. struct list_head *list)
  215. {
  216. struct dirty_root *dirty;
  217. struct btrfs_root *gang[8];
  218. struct btrfs_root *root;
  219. struct btrfs_root_item tmp_item;
  220. int i;
  221. int ret;
  222. int err = 0;
  223. u32 refs;
  224. while(1) {
  225. ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
  226. ARRAY_SIZE(gang),
  227. BTRFS_ROOT_TRANS_TAG);
  228. if (ret == 0)
  229. break;
  230. for (i = 0; i < ret; i++) {
  231. root = gang[i];
  232. radix_tree_tag_clear(radix,
  233. (unsigned long)root->root_key.objectid,
  234. BTRFS_ROOT_TRANS_TAG);
  235. if (root->commit_root == root->node) {
  236. WARN_ON(bh_blocknr(root->node) !=
  237. btrfs_root_blocknr(&root->root_item));
  238. brelse(root->commit_root);
  239. root->commit_root = NULL;
  240. continue;
  241. }
  242. dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
  243. BUG_ON(!dirty);
  244. memcpy(&dirty->snap_key, &root->root_key,
  245. sizeof(root->root_key));
  246. dirty->commit_root = root->commit_root;
  247. root->commit_root = NULL;
  248. dirty->root = root;
  249. dirty->free_on_drop = 0;
  250. memcpy(&tmp_item, &root->root_item, sizeof(tmp_item));
  251. root->root_key.offset = root->fs_info->generation;
  252. btrfs_set_root_blocknr(&root->root_item,
  253. bh_blocknr(root->node));
  254. err = btrfs_insert_root(trans, root->fs_info->tree_root,
  255. &root->root_key,
  256. &root->root_item);
  257. if (err)
  258. break;
  259. refs = btrfs_root_refs(&tmp_item);
  260. btrfs_set_root_refs(&tmp_item, refs - 1);
  261. err = btrfs_update_root(trans, root->fs_info->tree_root,
  262. &dirty->snap_key,
  263. &tmp_item);
  264. BUG_ON(err);
  265. if (refs == 1)
  266. list_add(&dirty->list, list);
  267. else
  268. kfree(dirty);
  269. }
  270. }
  271. return err;
  272. }
  273. static int drop_dirty_roots(struct btrfs_root *tree_root,
  274. struct list_head *list)
  275. {
  276. struct dirty_root *dirty;
  277. struct btrfs_trans_handle *trans;
  278. int ret = 0;
  279. while(!list_empty(list)) {
  280. mutex_lock(&tree_root->fs_info->fs_mutex);
  281. dirty = list_entry(list->next, struct dirty_root, list);
  282. list_del_init(&dirty->list);
  283. trans = btrfs_start_transaction(tree_root, 1);
  284. ret = btrfs_drop_snapshot(trans, dirty->root,
  285. dirty->commit_root);
  286. BUG_ON(ret);
  287. ret = btrfs_del_root(trans, tree_root, &dirty->snap_key);
  288. if (ret)
  289. break;
  290. ret = btrfs_end_transaction(trans, tree_root);
  291. BUG_ON(ret);
  292. if (dirty->free_on_drop)
  293. kfree(dirty->root);
  294. kfree(dirty);
  295. mutex_unlock(&tree_root->fs_info->fs_mutex);
  296. btrfs_btree_balance_dirty(tree_root);
  297. }
  298. return ret;
  299. }
  300. int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
  301. struct btrfs_root *root)
  302. {
  303. int ret = 0;
  304. struct btrfs_transaction *cur_trans;
  305. struct btrfs_transaction *prev_trans = NULL;
  306. struct list_head dirty_fs_roots;
  307. DEFINE_WAIT(wait);
  308. INIT_LIST_HEAD(&dirty_fs_roots);
  309. mutex_lock(&root->fs_info->trans_mutex);
  310. if (trans->transaction->in_commit) {
  311. cur_trans = trans->transaction;
  312. trans->transaction->use_count++;
  313. btrfs_end_transaction(trans, root);
  314. ret = wait_for_commit(root, cur_trans);
  315. BUG_ON(ret);
  316. put_transaction(cur_trans);
  317. mutex_unlock(&root->fs_info->trans_mutex);
  318. return 0;
  319. }
  320. cur_trans = trans->transaction;
  321. trans->transaction->in_commit = 1;
  322. while (trans->transaction->num_writers > 1) {
  323. WARN_ON(cur_trans != trans->transaction);
  324. prepare_to_wait(&trans->transaction->writer_wait, &wait,
  325. TASK_UNINTERRUPTIBLE);
  326. if (trans->transaction->num_writers <= 1)
  327. break;
  328. mutex_unlock(&root->fs_info->trans_mutex);
  329. schedule();
  330. mutex_lock(&root->fs_info->trans_mutex);
  331. finish_wait(&trans->transaction->writer_wait, &wait);
  332. }
  333. finish_wait(&trans->transaction->writer_wait, &wait);
  334. WARN_ON(cur_trans != trans->transaction);
  335. ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
  336. &dirty_fs_roots);
  337. BUG_ON(ret);
  338. ret = btrfs_commit_tree_roots(trans, root);
  339. BUG_ON(ret);
  340. cur_trans = root->fs_info->running_transaction;
  341. root->fs_info->running_transaction = NULL;
  342. if (cur_trans->list.prev != &root->fs_info->trans_list) {
  343. prev_trans = list_entry(cur_trans->list.prev,
  344. struct btrfs_transaction, list);
  345. if (prev_trans->commit_done)
  346. prev_trans = NULL;
  347. else
  348. prev_trans->use_count++;
  349. }
  350. btrfs_set_super_generation(&root->fs_info->super_copy,
  351. cur_trans->transid);
  352. btrfs_set_super_root(&root->fs_info->super_copy,
  353. bh_blocknr(root->fs_info->tree_root->node));
  354. memcpy(root->fs_info->disk_super, &root->fs_info->super_copy,
  355. sizeof(root->fs_info->super_copy));
  356. mutex_unlock(&root->fs_info->trans_mutex);
  357. mutex_unlock(&root->fs_info->fs_mutex);
  358. ret = btrfs_write_and_wait_transaction(trans, root);
  359. if (prev_trans) {
  360. mutex_lock(&root->fs_info->trans_mutex);
  361. wait_for_commit(root, prev_trans);
  362. put_transaction(prev_trans);
  363. mutex_unlock(&root->fs_info->trans_mutex);
  364. }
  365. BUG_ON(ret);
  366. write_ctree_super(trans, root);
  367. mutex_lock(&root->fs_info->fs_mutex);
  368. btrfs_finish_extent_commit(trans, root);
  369. mutex_lock(&root->fs_info->trans_mutex);
  370. cur_trans->commit_done = 1;
  371. wake_up(&cur_trans->commit_wait);
  372. put_transaction(cur_trans);
  373. put_transaction(cur_trans);
  374. if (root->fs_info->closing)
  375. list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
  376. else
  377. list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
  378. mutex_unlock(&root->fs_info->trans_mutex);
  379. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  380. if (root->fs_info->closing) {
  381. mutex_unlock(&root->fs_info->fs_mutex);
  382. drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
  383. mutex_lock(&root->fs_info->fs_mutex);
  384. }
  385. return ret;
  386. }
  387. void btrfs_transaction_cleaner(struct work_struct *work)
  388. {
  389. struct btrfs_fs_info *fs_info = container_of(work,
  390. struct btrfs_fs_info,
  391. trans_work.work);
  392. struct btrfs_root *root = fs_info->tree_root;
  393. struct btrfs_transaction *cur;
  394. struct btrfs_trans_handle *trans;
  395. struct list_head dirty_roots;
  396. unsigned long now;
  397. unsigned long delay = HZ * 30;
  398. int ret;
  399. INIT_LIST_HEAD(&dirty_roots);
  400. mutex_lock(&root->fs_info->fs_mutex);
  401. mutex_lock(&root->fs_info->trans_mutex);
  402. cur = root->fs_info->running_transaction;
  403. if (!cur) {
  404. mutex_unlock(&root->fs_info->trans_mutex);
  405. goto out;
  406. }
  407. now = get_seconds();
  408. if (now < cur->start_time || now - cur->start_time < 30) {
  409. mutex_unlock(&root->fs_info->trans_mutex);
  410. delay = HZ * 5;
  411. goto out;
  412. }
  413. mutex_unlock(&root->fs_info->trans_mutex);
  414. trans = btrfs_start_transaction(root, 1);
  415. ret = btrfs_commit_transaction(trans, root);
  416. out:
  417. mutex_unlock(&root->fs_info->fs_mutex);
  418. mutex_lock(&root->fs_info->trans_mutex);
  419. list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
  420. mutex_unlock(&root->fs_info->trans_mutex);
  421. if (!list_empty(&dirty_roots)) {
  422. drop_dirty_roots(root, &dirty_roots);
  423. }
  424. btrfs_transaction_queue_work(root, delay);
  425. }
  426. void btrfs_transaction_queue_work(struct btrfs_root *root, int delay)
  427. {
  428. queue_delayed_work(trans_wq, &root->fs_info->trans_work, delay);
  429. }
  430. void btrfs_transaction_flush_work(struct btrfs_root *root)
  431. {
  432. cancel_rearming_delayed_workqueue(trans_wq, &root->fs_info->trans_work);
  433. flush_workqueue(trans_wq);
  434. }
  435. void __init btrfs_init_transaction_sys(void)
  436. {
  437. trans_wq = create_workqueue("btrfs");
  438. }
  439. void __exit btrfs_exit_transaction_sys(void)
  440. {
  441. destroy_workqueue(trans_wq);
  442. }