transaction.c 17 KB

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