transaction.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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. printk("deleting root %Lu %u %Lu\n", dirty->snap_key.objectid, dirty->snap_key.flags, dirty->snap_key.offset);
  285. ret = btrfs_drop_snapshot(trans, dirty->root,
  286. dirty->commit_root);
  287. BUG_ON(ret);
  288. ret = btrfs_del_root(trans, tree_root, &dirty->snap_key);
  289. if (ret)
  290. break;
  291. ret = btrfs_end_transaction(trans, tree_root);
  292. BUG_ON(ret);
  293. if (dirty->free_on_drop)
  294. kfree(dirty->root);
  295. kfree(dirty);
  296. mutex_unlock(&tree_root->fs_info->fs_mutex);
  297. btrfs_btree_balance_dirty(tree_root);
  298. }
  299. return ret;
  300. }
  301. int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
  302. struct btrfs_root *root)
  303. {
  304. int ret = 0;
  305. struct btrfs_transaction *cur_trans;
  306. struct btrfs_transaction *prev_trans = NULL;
  307. struct list_head dirty_fs_roots;
  308. DEFINE_WAIT(wait);
  309. INIT_LIST_HEAD(&dirty_fs_roots);
  310. mutex_lock(&root->fs_info->trans_mutex);
  311. if (trans->transaction->in_commit) {
  312. cur_trans = trans->transaction;
  313. trans->transaction->use_count++;
  314. btrfs_end_transaction(trans, root);
  315. ret = wait_for_commit(root, cur_trans);
  316. BUG_ON(ret);
  317. put_transaction(cur_trans);
  318. mutex_unlock(&root->fs_info->trans_mutex);
  319. return 0;
  320. }
  321. cur_trans = trans->transaction;
  322. trans->transaction->in_commit = 1;
  323. while (trans->transaction->num_writers > 1) {
  324. WARN_ON(cur_trans != trans->transaction);
  325. prepare_to_wait(&trans->transaction->writer_wait, &wait,
  326. TASK_UNINTERRUPTIBLE);
  327. if (trans->transaction->num_writers <= 1)
  328. break;
  329. mutex_unlock(&root->fs_info->trans_mutex);
  330. schedule();
  331. mutex_lock(&root->fs_info->trans_mutex);
  332. finish_wait(&trans->transaction->writer_wait, &wait);
  333. }
  334. finish_wait(&trans->transaction->writer_wait, &wait);
  335. WARN_ON(cur_trans != trans->transaction);
  336. ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
  337. &dirty_fs_roots);
  338. BUG_ON(ret);
  339. ret = btrfs_commit_tree_roots(trans, root);
  340. BUG_ON(ret);
  341. cur_trans = root->fs_info->running_transaction;
  342. root->fs_info->running_transaction = NULL;
  343. if (cur_trans->list.prev != &root->fs_info->trans_list) {
  344. prev_trans = list_entry(cur_trans->list.prev,
  345. struct btrfs_transaction, list);
  346. if (prev_trans->commit_done)
  347. prev_trans = NULL;
  348. else
  349. prev_trans->use_count++;
  350. }
  351. mutex_unlock(&root->fs_info->trans_mutex);
  352. mutex_unlock(&root->fs_info->fs_mutex);
  353. ret = btrfs_write_and_wait_transaction(trans, root);
  354. if (prev_trans) {
  355. mutex_lock(&root->fs_info->trans_mutex);
  356. wait_for_commit(root, prev_trans);
  357. put_transaction(prev_trans);
  358. mutex_unlock(&root->fs_info->trans_mutex);
  359. }
  360. btrfs_set_super_generation(root->fs_info->disk_super,
  361. cur_trans->transid);
  362. BUG_ON(ret);
  363. write_ctree_super(trans, root);
  364. mutex_lock(&root->fs_info->fs_mutex);
  365. btrfs_finish_extent_commit(trans, root);
  366. mutex_lock(&root->fs_info->trans_mutex);
  367. cur_trans->commit_done = 1;
  368. wake_up(&cur_trans->commit_wait);
  369. put_transaction(cur_trans);
  370. put_transaction(cur_trans);
  371. if (root->fs_info->closing)
  372. list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
  373. else
  374. list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
  375. mutex_unlock(&root->fs_info->trans_mutex);
  376. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  377. if (root->fs_info->closing) {
  378. mutex_unlock(&root->fs_info->fs_mutex);
  379. drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
  380. mutex_lock(&root->fs_info->fs_mutex);
  381. }
  382. return ret;
  383. }
  384. void btrfs_transaction_cleaner(struct work_struct *work)
  385. {
  386. struct btrfs_fs_info *fs_info = container_of(work,
  387. struct btrfs_fs_info,
  388. trans_work.work);
  389. struct btrfs_root *root = fs_info->tree_root;
  390. struct btrfs_transaction *cur;
  391. struct btrfs_trans_handle *trans;
  392. struct list_head dirty_roots;
  393. unsigned long now;
  394. unsigned long delay = HZ * 30;
  395. int ret;
  396. INIT_LIST_HEAD(&dirty_roots);
  397. mutex_lock(&root->fs_info->fs_mutex);
  398. mutex_lock(&root->fs_info->trans_mutex);
  399. cur = root->fs_info->running_transaction;
  400. if (!cur) {
  401. mutex_unlock(&root->fs_info->trans_mutex);
  402. goto out;
  403. }
  404. now = get_seconds();
  405. if (now < cur->start_time || now - cur->start_time < 30) {
  406. mutex_unlock(&root->fs_info->trans_mutex);
  407. delay = HZ * 5;
  408. goto out;
  409. }
  410. mutex_unlock(&root->fs_info->trans_mutex);
  411. trans = btrfs_start_transaction(root, 1);
  412. ret = btrfs_commit_transaction(trans, root);
  413. out:
  414. mutex_unlock(&root->fs_info->fs_mutex);
  415. mutex_lock(&root->fs_info->trans_mutex);
  416. list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
  417. mutex_unlock(&root->fs_info->trans_mutex);
  418. if (!list_empty(&dirty_roots)) {
  419. drop_dirty_roots(root, &dirty_roots);
  420. }
  421. btrfs_transaction_queue_work(root, delay);
  422. }
  423. void btrfs_transaction_queue_work(struct btrfs_root *root, int delay)
  424. {
  425. queue_delayed_work(trans_wq, &root->fs_info->trans_work, delay);
  426. }
  427. void btrfs_transaction_flush_work(struct btrfs_root *root)
  428. {
  429. cancel_rearming_delayed_workqueue(trans_wq, &root->fs_info->trans_work);
  430. flush_workqueue(trans_wq);
  431. }
  432. void __init btrfs_init_transaction_sys(void)
  433. {
  434. trans_wq = create_workqueue("btrfs");
  435. }
  436. void __exit btrfs_exit_transaction_sys(void)
  437. {
  438. destroy_workqueue(trans_wq);
  439. }