transaction.c 18 KB

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