transaction.c 14 KB

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