transaction.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  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/fs.h>
  19. #include <linux/sched.h>
  20. #include <linux/writeback.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. #define BTRFS_ROOT_DEFRAG_TAG 1
  30. static void put_transaction(struct btrfs_transaction *transaction)
  31. {
  32. WARN_ON(transaction->use_count == 0);
  33. transaction->use_count--;
  34. if (transaction->use_count == 0) {
  35. WARN_ON(total_trans == 0);
  36. total_trans--;
  37. list_del_init(&transaction->list);
  38. memset(transaction, 0, sizeof(*transaction));
  39. kmem_cache_free(btrfs_transaction_cachep, transaction);
  40. }
  41. }
  42. static int join_transaction(struct btrfs_root *root)
  43. {
  44. struct btrfs_transaction *cur_trans;
  45. cur_trans = root->fs_info->running_transaction;
  46. if (!cur_trans) {
  47. cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
  48. GFP_NOFS);
  49. total_trans++;
  50. BUG_ON(!cur_trans);
  51. root->fs_info->generation++;
  52. root->fs_info->running_transaction = cur_trans;
  53. cur_trans->num_writers = 1;
  54. cur_trans->num_joined = 0;
  55. cur_trans->transid = root->fs_info->generation;
  56. init_waitqueue_head(&cur_trans->writer_wait);
  57. init_waitqueue_head(&cur_trans->commit_wait);
  58. cur_trans->in_commit = 0;
  59. cur_trans->use_count = 1;
  60. cur_trans->commit_done = 0;
  61. cur_trans->start_time = get_seconds();
  62. list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
  63. init_bit_radix(&cur_trans->dirty_pages);
  64. } else {
  65. cur_trans->num_writers++;
  66. cur_trans->num_joined++;
  67. }
  68. return 0;
  69. }
  70. static int record_root_in_trans(struct btrfs_root *root)
  71. {
  72. u64 running_trans_id = root->fs_info->running_transaction->transid;
  73. if (root->ref_cows && root->last_trans < running_trans_id) {
  74. WARN_ON(root == root->fs_info->extent_root);
  75. if (root->root_item.refs != 0) {
  76. radix_tree_tag_set(&root->fs_info->fs_roots_radix,
  77. (unsigned long)root->root_key.objectid,
  78. BTRFS_ROOT_TRANS_TAG);
  79. radix_tree_tag_set(&root->fs_info->fs_roots_radix,
  80. (unsigned long)root->root_key.objectid,
  81. BTRFS_ROOT_DEFRAG_TAG);
  82. root->commit_root = root->node;
  83. get_bh(root->node);
  84. } else {
  85. WARN_ON(1);
  86. }
  87. root->last_trans = running_trans_id;
  88. }
  89. return 0;
  90. }
  91. struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
  92. int num_blocks)
  93. {
  94. struct btrfs_trans_handle *h =
  95. kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
  96. int ret;
  97. mutex_lock(&root->fs_info->trans_mutex);
  98. ret = join_transaction(root);
  99. BUG_ON(ret);
  100. record_root_in_trans(root);
  101. h->transid = root->fs_info->running_transaction->transid;
  102. h->transaction = root->fs_info->running_transaction;
  103. h->blocks_reserved = num_blocks;
  104. h->blocks_used = 0;
  105. h->block_group = NULL;
  106. h->alloc_exclude_nr = 0;
  107. h->alloc_exclude_start = 0;
  108. root->fs_info->running_transaction->use_count++;
  109. mutex_unlock(&root->fs_info->trans_mutex);
  110. return h;
  111. }
  112. int btrfs_end_transaction(struct btrfs_trans_handle *trans,
  113. struct btrfs_root *root)
  114. {
  115. struct btrfs_transaction *cur_trans;
  116. mutex_lock(&root->fs_info->trans_mutex);
  117. cur_trans = root->fs_info->running_transaction;
  118. WARN_ON(cur_trans != trans->transaction);
  119. WARN_ON(cur_trans->num_writers < 1);
  120. cur_trans->num_writers--;
  121. if (waitqueue_active(&cur_trans->writer_wait))
  122. wake_up(&cur_trans->writer_wait);
  123. put_transaction(cur_trans);
  124. mutex_unlock(&root->fs_info->trans_mutex);
  125. memset(trans, 0, sizeof(*trans));
  126. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  127. return 0;
  128. }
  129. int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
  130. struct btrfs_root *root)
  131. {
  132. unsigned long gang[16];
  133. int ret;
  134. int i;
  135. int err;
  136. int werr = 0;
  137. struct page *page;
  138. struct radix_tree_root *dirty_pages;
  139. struct inode *btree_inode = root->fs_info->btree_inode;
  140. if (!trans || !trans->transaction) {
  141. return filemap_write_and_wait(btree_inode->i_mapping);
  142. }
  143. dirty_pages = &trans->transaction->dirty_pages;
  144. while(1) {
  145. ret = find_first_radix_bit(dirty_pages, gang,
  146. 0, ARRAY_SIZE(gang));
  147. if (!ret)
  148. break;
  149. for (i = 0; i < ret; i++) {
  150. /* FIXME EIO */
  151. clear_radix_bit(dirty_pages, gang[i]);
  152. page = find_lock_page(btree_inode->i_mapping,
  153. gang[i]);
  154. if (!page)
  155. continue;
  156. if (PageWriteback(page)) {
  157. if (PageDirty(page))
  158. wait_on_page_writeback(page);
  159. else {
  160. unlock_page(page);
  161. page_cache_release(page);
  162. continue;
  163. }
  164. }
  165. err = write_one_page(page, 0);
  166. if (err)
  167. werr = err;
  168. page_cache_release(page);
  169. }
  170. }
  171. err = filemap_fdatawait(btree_inode->i_mapping);
  172. if (err)
  173. werr = err;
  174. return werr;
  175. }
  176. int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
  177. struct btrfs_root *root)
  178. {
  179. int ret;
  180. u64 old_extent_block;
  181. struct btrfs_fs_info *fs_info = root->fs_info;
  182. struct btrfs_root *tree_root = fs_info->tree_root;
  183. struct btrfs_root *extent_root = fs_info->extent_root;
  184. btrfs_write_dirty_block_groups(trans, extent_root);
  185. while(1) {
  186. old_extent_block = btrfs_root_blocknr(&extent_root->root_item);
  187. if (old_extent_block == bh_blocknr(extent_root->node))
  188. break;
  189. btrfs_set_root_blocknr(&extent_root->root_item,
  190. bh_blocknr(extent_root->node));
  191. ret = btrfs_update_root(trans, tree_root,
  192. &extent_root->root_key,
  193. &extent_root->root_item);
  194. BUG_ON(ret);
  195. btrfs_write_dirty_block_groups(trans, extent_root);
  196. }
  197. return 0;
  198. }
  199. static int wait_for_commit(struct btrfs_root *root,
  200. struct btrfs_transaction *commit)
  201. {
  202. DEFINE_WAIT(wait);
  203. mutex_lock(&root->fs_info->trans_mutex);
  204. while(!commit->commit_done) {
  205. prepare_to_wait(&commit->commit_wait, &wait,
  206. TASK_UNINTERRUPTIBLE);
  207. if (commit->commit_done)
  208. break;
  209. mutex_unlock(&root->fs_info->trans_mutex);
  210. schedule();
  211. mutex_lock(&root->fs_info->trans_mutex);
  212. }
  213. mutex_unlock(&root->fs_info->trans_mutex);
  214. finish_wait(&commit->commit_wait, &wait);
  215. return 0;
  216. }
  217. struct dirty_root {
  218. struct list_head list;
  219. struct btrfs_root *root;
  220. struct btrfs_root *latest_root;
  221. };
  222. int btrfs_add_dead_root(struct btrfs_root *root,
  223. struct btrfs_root *latest,
  224. struct list_head *dead_list)
  225. {
  226. struct dirty_root *dirty;
  227. dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
  228. if (!dirty)
  229. return -ENOMEM;
  230. dirty->root = root;
  231. dirty->latest_root = latest;
  232. list_add(&dirty->list, dead_list);
  233. return 0;
  234. }
  235. static int add_dirty_roots(struct btrfs_trans_handle *trans,
  236. struct radix_tree_root *radix,
  237. struct list_head *list)
  238. {
  239. struct dirty_root *dirty;
  240. struct btrfs_root *gang[8];
  241. struct btrfs_root *root;
  242. int i;
  243. int ret;
  244. int err = 0;
  245. u32 refs;
  246. while(1) {
  247. ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
  248. ARRAY_SIZE(gang),
  249. BTRFS_ROOT_TRANS_TAG);
  250. if (ret == 0)
  251. break;
  252. for (i = 0; i < ret; i++) {
  253. root = gang[i];
  254. radix_tree_tag_clear(radix,
  255. (unsigned long)root->root_key.objectid,
  256. BTRFS_ROOT_TRANS_TAG);
  257. if (root->commit_root == root->node) {
  258. WARN_ON(bh_blocknr(root->node) !=
  259. btrfs_root_blocknr(&root->root_item));
  260. brelse(root->commit_root);
  261. root->commit_root = NULL;
  262. /* make sure to update the root on disk
  263. * so we get any updates to the block used
  264. * counts
  265. */
  266. err = btrfs_update_root(trans,
  267. root->fs_info->tree_root,
  268. &root->root_key,
  269. &root->root_item);
  270. continue;
  271. }
  272. dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
  273. BUG_ON(!dirty);
  274. dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS);
  275. BUG_ON(!dirty->root);
  276. memset(&root->root_item.drop_progress, 0,
  277. sizeof(struct btrfs_disk_key));
  278. root->root_item.drop_level = 0;
  279. memcpy(dirty->root, root, sizeof(*root));
  280. dirty->root->node = root->commit_root;
  281. dirty->latest_root = root;
  282. root->commit_root = NULL;
  283. root->root_key.offset = root->fs_info->generation;
  284. btrfs_set_root_blocknr(&root->root_item,
  285. bh_blocknr(root->node));
  286. err = btrfs_insert_root(trans, root->fs_info->tree_root,
  287. &root->root_key,
  288. &root->root_item);
  289. if (err)
  290. break;
  291. refs = btrfs_root_refs(&dirty->root->root_item);
  292. btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
  293. err = btrfs_update_root(trans, root->fs_info->tree_root,
  294. &dirty->root->root_key,
  295. &dirty->root->root_item);
  296. BUG_ON(err);
  297. if (refs == 1) {
  298. list_add(&dirty->list, list);
  299. } else {
  300. WARN_ON(1);
  301. kfree(dirty->root);
  302. kfree(dirty);
  303. }
  304. }
  305. }
  306. return err;
  307. }
  308. int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
  309. {
  310. struct btrfs_fs_info *info = root->fs_info;
  311. int ret;
  312. struct btrfs_trans_handle *trans;
  313. unsigned long nr;
  314. if (root->defrag_running)
  315. return 0;
  316. trans = btrfs_start_transaction(root, 1);
  317. while (1) {
  318. root->defrag_running = 1;
  319. ret = btrfs_defrag_leaves(trans, root, cacheonly);
  320. nr = trans->blocks_used;
  321. btrfs_end_transaction(trans, root);
  322. mutex_unlock(&info->fs_mutex);
  323. btrfs_btree_balance_dirty(info->tree_root, nr);
  324. cond_resched();
  325. mutex_lock(&info->fs_mutex);
  326. trans = btrfs_start_transaction(root, 1);
  327. if (ret != -EAGAIN)
  328. break;
  329. }
  330. root->defrag_running = 0;
  331. radix_tree_tag_clear(&info->fs_roots_radix,
  332. (unsigned long)root->root_key.objectid,
  333. BTRFS_ROOT_DEFRAG_TAG);
  334. btrfs_end_transaction(trans, root);
  335. return 0;
  336. }
  337. int btrfs_defrag_dirty_roots(struct btrfs_fs_info *info)
  338. {
  339. struct btrfs_root *gang[1];
  340. struct btrfs_root *root;
  341. int i;
  342. int ret;
  343. int err = 0;
  344. u64 last = 0;
  345. while(1) {
  346. ret = radix_tree_gang_lookup_tag(&info->fs_roots_radix,
  347. (void **)gang, last,
  348. ARRAY_SIZE(gang),
  349. BTRFS_ROOT_DEFRAG_TAG);
  350. if (ret == 0)
  351. break;
  352. for (i = 0; i < ret; i++) {
  353. root = gang[i];
  354. last = root->root_key.objectid + 1;
  355. btrfs_defrag_root(root, 1);
  356. }
  357. }
  358. btrfs_defrag_root(info->extent_root, 1);
  359. return err;
  360. }
  361. static int drop_dirty_roots(struct btrfs_root *tree_root,
  362. struct list_head *list)
  363. {
  364. struct dirty_root *dirty;
  365. struct btrfs_trans_handle *trans;
  366. unsigned long nr;
  367. u64 num_blocks;
  368. u64 blocks_used;
  369. int ret = 0;
  370. int err;
  371. while(!list_empty(list)) {
  372. struct btrfs_root *root;
  373. mutex_lock(&tree_root->fs_info->fs_mutex);
  374. dirty = list_entry(list->next, struct dirty_root, list);
  375. list_del_init(&dirty->list);
  376. num_blocks = btrfs_root_blocks_used(&dirty->root->root_item);
  377. root = dirty->latest_root;
  378. while(1) {
  379. trans = btrfs_start_transaction(tree_root, 1);
  380. ret = btrfs_drop_snapshot(trans, dirty->root);
  381. if (ret != -EAGAIN) {
  382. break;
  383. }
  384. err = btrfs_update_root(trans,
  385. tree_root,
  386. &dirty->root->root_key,
  387. &dirty->root->root_item);
  388. if (err)
  389. ret = err;
  390. nr = trans->blocks_used;
  391. ret = btrfs_end_transaction(trans, tree_root);
  392. BUG_ON(ret);
  393. mutex_unlock(&tree_root->fs_info->fs_mutex);
  394. btrfs_btree_balance_dirty(tree_root, nr);
  395. schedule();
  396. mutex_lock(&tree_root->fs_info->fs_mutex);
  397. }
  398. BUG_ON(ret);
  399. num_blocks -= btrfs_root_blocks_used(&dirty->root->root_item);
  400. blocks_used = btrfs_root_blocks_used(&root->root_item);
  401. if (num_blocks) {
  402. record_root_in_trans(root);
  403. btrfs_set_root_blocks_used(&root->root_item,
  404. blocks_used - num_blocks);
  405. }
  406. ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
  407. if (ret) {
  408. BUG();
  409. break;
  410. }
  411. nr = trans->blocks_used;
  412. ret = btrfs_end_transaction(trans, tree_root);
  413. BUG_ON(ret);
  414. kfree(dirty->root);
  415. kfree(dirty);
  416. mutex_unlock(&tree_root->fs_info->fs_mutex);
  417. btrfs_btree_balance_dirty(tree_root, nr);
  418. schedule();
  419. }
  420. return ret;
  421. }
  422. int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
  423. struct btrfs_root *root)
  424. {
  425. unsigned long joined = 0;
  426. unsigned long timeout = 1;
  427. struct btrfs_transaction *cur_trans;
  428. struct btrfs_transaction *prev_trans = NULL;
  429. struct list_head dirty_fs_roots;
  430. struct radix_tree_root pinned_copy;
  431. DEFINE_WAIT(wait);
  432. int ret;
  433. init_bit_radix(&pinned_copy);
  434. INIT_LIST_HEAD(&dirty_fs_roots);
  435. mutex_lock(&root->fs_info->trans_mutex);
  436. if (trans->transaction->in_commit) {
  437. cur_trans = trans->transaction;
  438. trans->transaction->use_count++;
  439. mutex_unlock(&root->fs_info->trans_mutex);
  440. btrfs_end_transaction(trans, root);
  441. mutex_unlock(&root->fs_info->fs_mutex);
  442. ret = wait_for_commit(root, cur_trans);
  443. BUG_ON(ret);
  444. mutex_lock(&root->fs_info->trans_mutex);
  445. put_transaction(cur_trans);
  446. mutex_unlock(&root->fs_info->trans_mutex);
  447. mutex_lock(&root->fs_info->fs_mutex);
  448. return 0;
  449. }
  450. trans->transaction->in_commit = 1;
  451. cur_trans = trans->transaction;
  452. if (cur_trans->list.prev != &root->fs_info->trans_list) {
  453. prev_trans = list_entry(cur_trans->list.prev,
  454. struct btrfs_transaction, list);
  455. if (!prev_trans->commit_done) {
  456. prev_trans->use_count++;
  457. mutex_unlock(&root->fs_info->fs_mutex);
  458. mutex_unlock(&root->fs_info->trans_mutex);
  459. wait_for_commit(root, prev_trans);
  460. mutex_lock(&root->fs_info->fs_mutex);
  461. mutex_lock(&root->fs_info->trans_mutex);
  462. put_transaction(prev_trans);
  463. }
  464. }
  465. do {
  466. joined = cur_trans->num_joined;
  467. WARN_ON(cur_trans != trans->transaction);
  468. prepare_to_wait(&cur_trans->writer_wait, &wait,
  469. TASK_UNINTERRUPTIBLE);
  470. if (cur_trans->num_writers > 1)
  471. timeout = MAX_SCHEDULE_TIMEOUT;
  472. else
  473. timeout = 1;
  474. mutex_unlock(&root->fs_info->fs_mutex);
  475. mutex_unlock(&root->fs_info->trans_mutex);
  476. schedule_timeout(timeout);
  477. mutex_lock(&root->fs_info->fs_mutex);
  478. mutex_lock(&root->fs_info->trans_mutex);
  479. finish_wait(&cur_trans->writer_wait, &wait);
  480. } while (cur_trans->num_writers > 1 ||
  481. (cur_trans->num_joined != joined));
  482. WARN_ON(cur_trans != trans->transaction);
  483. ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
  484. &dirty_fs_roots);
  485. BUG_ON(ret);
  486. ret = btrfs_commit_tree_roots(trans, root);
  487. BUG_ON(ret);
  488. cur_trans = root->fs_info->running_transaction;
  489. root->fs_info->running_transaction = NULL;
  490. btrfs_set_super_generation(&root->fs_info->super_copy,
  491. cur_trans->transid);
  492. btrfs_set_super_root(&root->fs_info->super_copy,
  493. bh_blocknr(root->fs_info->tree_root->node));
  494. memcpy(root->fs_info->disk_super, &root->fs_info->super_copy,
  495. sizeof(root->fs_info->super_copy));
  496. btrfs_copy_pinned(root, &pinned_copy);
  497. mutex_unlock(&root->fs_info->trans_mutex);
  498. mutex_unlock(&root->fs_info->fs_mutex);
  499. ret = btrfs_write_and_wait_transaction(trans, root);
  500. BUG_ON(ret);
  501. write_ctree_super(trans, root);
  502. mutex_lock(&root->fs_info->fs_mutex);
  503. btrfs_finish_extent_commit(trans, root, &pinned_copy);
  504. mutex_lock(&root->fs_info->trans_mutex);
  505. cur_trans->commit_done = 1;
  506. root->fs_info->last_trans_committed = cur_trans->transid;
  507. wake_up(&cur_trans->commit_wait);
  508. put_transaction(cur_trans);
  509. put_transaction(cur_trans);
  510. if (root->fs_info->closing)
  511. list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
  512. else
  513. list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
  514. mutex_unlock(&root->fs_info->trans_mutex);
  515. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  516. if (root->fs_info->closing) {
  517. mutex_unlock(&root->fs_info->fs_mutex);
  518. drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
  519. mutex_lock(&root->fs_info->fs_mutex);
  520. }
  521. return ret;
  522. }
  523. int btrfs_clean_old_snapshots(struct btrfs_root *root)
  524. {
  525. struct list_head dirty_roots;
  526. INIT_LIST_HEAD(&dirty_roots);
  527. mutex_lock(&root->fs_info->trans_mutex);
  528. list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
  529. mutex_unlock(&root->fs_info->trans_mutex);
  530. if (!list_empty(&dirty_roots)) {
  531. drop_dirty_roots(root, &dirty_roots);
  532. }
  533. return 0;
  534. }
  535. void btrfs_transaction_cleaner(struct work_struct *work)
  536. {
  537. struct btrfs_fs_info *fs_info = container_of(work,
  538. struct btrfs_fs_info,
  539. trans_work.work);
  540. struct btrfs_root *root = fs_info->tree_root;
  541. struct btrfs_transaction *cur;
  542. struct btrfs_trans_handle *trans;
  543. unsigned long now;
  544. unsigned long delay = HZ * 30;
  545. int ret;
  546. mutex_lock(&root->fs_info->fs_mutex);
  547. mutex_lock(&root->fs_info->trans_mutex);
  548. cur = root->fs_info->running_transaction;
  549. if (!cur) {
  550. mutex_unlock(&root->fs_info->trans_mutex);
  551. goto out;
  552. }
  553. now = get_seconds();
  554. if (now < cur->start_time || now - cur->start_time < 30) {
  555. mutex_unlock(&root->fs_info->trans_mutex);
  556. delay = HZ * 5;
  557. goto out;
  558. }
  559. mutex_unlock(&root->fs_info->trans_mutex);
  560. btrfs_defrag_dirty_roots(root->fs_info);
  561. trans = btrfs_start_transaction(root, 1);
  562. ret = btrfs_commit_transaction(trans, root);
  563. out:
  564. mutex_unlock(&root->fs_info->fs_mutex);
  565. btrfs_clean_old_snapshots(root);
  566. btrfs_transaction_queue_work(root, delay);
  567. }
  568. void btrfs_transaction_queue_work(struct btrfs_root *root, int delay)
  569. {
  570. queue_delayed_work(trans_wq, &root->fs_info->trans_work, delay);
  571. }
  572. void btrfs_transaction_flush_work(struct btrfs_root *root)
  573. {
  574. cancel_rearming_delayed_workqueue(trans_wq, &root->fs_info->trans_work);
  575. flush_workqueue(trans_wq);
  576. }
  577. void __init btrfs_init_transaction_sys(void)
  578. {
  579. trans_wq = create_workqueue("btrfs");
  580. }
  581. void __exit btrfs_exit_transaction_sys(void)
  582. {
  583. destroy_workqueue(trans_wq);
  584. }