transaction.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  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 = (u64)(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_bytenr(&extent_root->root_item);
  191. if (old_extent_block == extent_root->node->start)
  192. break;
  193. btrfs_set_root_bytenr(&extent_root->root_item,
  194. extent_root->node->start);
  195. btrfs_set_root_level(&extent_root->root_item,
  196. btrfs_header_level(extent_root->node));
  197. ret = btrfs_update_root(trans, tree_root,
  198. &extent_root->root_key,
  199. &extent_root->root_item);
  200. BUG_ON(ret);
  201. btrfs_write_dirty_block_groups(trans, extent_root);
  202. }
  203. return 0;
  204. }
  205. static int wait_for_commit(struct btrfs_root *root,
  206. struct btrfs_transaction *commit)
  207. {
  208. DEFINE_WAIT(wait);
  209. mutex_lock(&root->fs_info->trans_mutex);
  210. while(!commit->commit_done) {
  211. prepare_to_wait(&commit->commit_wait, &wait,
  212. TASK_UNINTERRUPTIBLE);
  213. if (commit->commit_done)
  214. break;
  215. mutex_unlock(&root->fs_info->trans_mutex);
  216. schedule();
  217. mutex_lock(&root->fs_info->trans_mutex);
  218. }
  219. mutex_unlock(&root->fs_info->trans_mutex);
  220. finish_wait(&commit->commit_wait, &wait);
  221. return 0;
  222. }
  223. struct dirty_root {
  224. struct list_head list;
  225. struct btrfs_root *root;
  226. struct btrfs_root *latest_root;
  227. };
  228. int btrfs_add_dead_root(struct btrfs_root *root,
  229. struct btrfs_root *latest,
  230. struct list_head *dead_list)
  231. {
  232. struct dirty_root *dirty;
  233. dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
  234. if (!dirty)
  235. return -ENOMEM;
  236. dirty->root = root;
  237. dirty->latest_root = latest;
  238. list_add(&dirty->list, dead_list);
  239. return 0;
  240. }
  241. static int add_dirty_roots(struct btrfs_trans_handle *trans,
  242. struct radix_tree_root *radix,
  243. struct list_head *list)
  244. {
  245. struct dirty_root *dirty;
  246. struct btrfs_root *gang[8];
  247. struct btrfs_root *root;
  248. int i;
  249. int ret;
  250. int err = 0;
  251. u32 refs;
  252. while(1) {
  253. ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
  254. ARRAY_SIZE(gang),
  255. BTRFS_ROOT_TRANS_TAG);
  256. if (ret == 0)
  257. break;
  258. for (i = 0; i < ret; i++) {
  259. root = gang[i];
  260. radix_tree_tag_clear(radix,
  261. (unsigned long)root->root_key.objectid,
  262. BTRFS_ROOT_TRANS_TAG);
  263. if (root->commit_root == root->node) {
  264. WARN_ON(root->node->start !=
  265. btrfs_root_bytenr(&root->root_item));
  266. free_extent_buffer(root->commit_root);
  267. root->commit_root = NULL;
  268. /* make sure to update the root on disk
  269. * so we get any updates to the block used
  270. * counts
  271. */
  272. err = btrfs_update_root(trans,
  273. root->fs_info->tree_root,
  274. &root->root_key,
  275. &root->root_item);
  276. continue;
  277. }
  278. dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
  279. BUG_ON(!dirty);
  280. dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS);
  281. BUG_ON(!dirty->root);
  282. memset(&root->root_item.drop_progress, 0,
  283. sizeof(struct btrfs_disk_key));
  284. root->root_item.drop_level = 0;
  285. memcpy(dirty->root, root, sizeof(*root));
  286. dirty->root->node = root->commit_root;
  287. dirty->latest_root = root;
  288. root->commit_root = NULL;
  289. root->root_key.offset = root->fs_info->generation;
  290. btrfs_set_root_bytenr(&root->root_item,
  291. root->node->start);
  292. btrfs_set_root_level(&root->root_item,
  293. btrfs_header_level(root->node));
  294. err = btrfs_insert_root(trans, root->fs_info->tree_root,
  295. &root->root_key,
  296. &root->root_item);
  297. if (err)
  298. break;
  299. refs = btrfs_root_refs(&dirty->root->root_item);
  300. btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
  301. err = btrfs_update_root(trans, root->fs_info->tree_root,
  302. &dirty->root->root_key,
  303. &dirty->root->root_item);
  304. BUG_ON(err);
  305. if (refs == 1) {
  306. list_add(&dirty->list, list);
  307. } else {
  308. WARN_ON(1);
  309. kfree(dirty->root);
  310. kfree(dirty);
  311. }
  312. }
  313. }
  314. return err;
  315. }
  316. int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
  317. {
  318. struct btrfs_fs_info *info = root->fs_info;
  319. int ret;
  320. struct btrfs_trans_handle *trans;
  321. unsigned long nr;
  322. if (root->defrag_running)
  323. return 0;
  324. trans = btrfs_start_transaction(root, 1);
  325. while (1) {
  326. root->defrag_running = 1;
  327. ret = btrfs_defrag_leaves(trans, root, cacheonly);
  328. nr = trans->blocks_used;
  329. btrfs_end_transaction(trans, root);
  330. mutex_unlock(&info->fs_mutex);
  331. btrfs_btree_balance_dirty(info->tree_root, nr);
  332. cond_resched();
  333. mutex_lock(&info->fs_mutex);
  334. trans = btrfs_start_transaction(root, 1);
  335. if (ret != -EAGAIN)
  336. break;
  337. }
  338. root->defrag_running = 0;
  339. radix_tree_tag_clear(&info->fs_roots_radix,
  340. (unsigned long)root->root_key.objectid,
  341. BTRFS_ROOT_DEFRAG_TAG);
  342. btrfs_end_transaction(trans, root);
  343. return 0;
  344. }
  345. int btrfs_defrag_dirty_roots(struct btrfs_fs_info *info)
  346. {
  347. struct btrfs_root *gang[1];
  348. struct btrfs_root *root;
  349. int i;
  350. int ret;
  351. int err = 0;
  352. u64 last = 0;
  353. while(1) {
  354. ret = radix_tree_gang_lookup_tag(&info->fs_roots_radix,
  355. (void **)gang, last,
  356. ARRAY_SIZE(gang),
  357. BTRFS_ROOT_DEFRAG_TAG);
  358. if (ret == 0)
  359. break;
  360. for (i = 0; i < ret; i++) {
  361. root = gang[i];
  362. last = root->root_key.objectid + 1;
  363. btrfs_defrag_root(root, 1);
  364. }
  365. }
  366. btrfs_defrag_root(info->extent_root, 1);
  367. return err;
  368. }
  369. static int drop_dirty_roots(struct btrfs_root *tree_root,
  370. struct list_head *list)
  371. {
  372. struct dirty_root *dirty;
  373. struct btrfs_trans_handle *trans;
  374. unsigned long nr;
  375. u64 num_bytes;
  376. u64 bytes_used;
  377. int ret = 0;
  378. int err;
  379. while(!list_empty(list)) {
  380. struct btrfs_root *root;
  381. mutex_lock(&tree_root->fs_info->fs_mutex);
  382. dirty = list_entry(list->next, struct dirty_root, list);
  383. list_del_init(&dirty->list);
  384. num_bytes = btrfs_root_used(&dirty->root->root_item);
  385. root = dirty->latest_root;
  386. while(1) {
  387. trans = btrfs_start_transaction(tree_root, 1);
  388. ret = btrfs_drop_snapshot(trans, dirty->root);
  389. if (ret != -EAGAIN) {
  390. break;
  391. }
  392. err = btrfs_update_root(trans,
  393. tree_root,
  394. &dirty->root->root_key,
  395. &dirty->root->root_item);
  396. if (err)
  397. ret = err;
  398. nr = trans->blocks_used;
  399. ret = btrfs_end_transaction(trans, tree_root);
  400. BUG_ON(ret);
  401. mutex_unlock(&tree_root->fs_info->fs_mutex);
  402. btrfs_btree_balance_dirty(tree_root, nr);
  403. cond_resched();
  404. mutex_lock(&tree_root->fs_info->fs_mutex);
  405. }
  406. BUG_ON(ret);
  407. num_bytes -= btrfs_root_used(&dirty->root->root_item);
  408. bytes_used = btrfs_root_used(&root->root_item);
  409. if (num_bytes) {
  410. record_root_in_trans(root);
  411. btrfs_set_root_used(&root->root_item,
  412. bytes_used - num_bytes);
  413. }
  414. ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
  415. if (ret) {
  416. BUG();
  417. break;
  418. }
  419. nr = trans->blocks_used;
  420. ret = btrfs_end_transaction(trans, tree_root);
  421. BUG_ON(ret);
  422. free_extent_buffer(dirty->root->node);
  423. kfree(dirty->root);
  424. kfree(dirty);
  425. mutex_unlock(&tree_root->fs_info->fs_mutex);
  426. btrfs_btree_balance_dirty(tree_root, nr);
  427. cond_resched();
  428. }
  429. return ret;
  430. }
  431. int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
  432. struct btrfs_root *root)
  433. {
  434. unsigned long joined = 0;
  435. unsigned long timeout = 1;
  436. struct btrfs_transaction *cur_trans;
  437. struct btrfs_transaction *prev_trans = NULL;
  438. struct list_head dirty_fs_roots;
  439. struct extent_map_tree pinned_copy;
  440. DEFINE_WAIT(wait);
  441. int ret;
  442. extent_map_tree_init(&pinned_copy,
  443. root->fs_info->btree_inode->i_mapping, GFP_NOFS);
  444. INIT_LIST_HEAD(&dirty_fs_roots);
  445. mutex_lock(&root->fs_info->trans_mutex);
  446. if (trans->transaction->in_commit) {
  447. cur_trans = trans->transaction;
  448. trans->transaction->use_count++;
  449. mutex_unlock(&root->fs_info->trans_mutex);
  450. btrfs_end_transaction(trans, root);
  451. mutex_unlock(&root->fs_info->fs_mutex);
  452. ret = wait_for_commit(root, cur_trans);
  453. BUG_ON(ret);
  454. mutex_lock(&root->fs_info->trans_mutex);
  455. put_transaction(cur_trans);
  456. mutex_unlock(&root->fs_info->trans_mutex);
  457. mutex_lock(&root->fs_info->fs_mutex);
  458. return 0;
  459. }
  460. trans->transaction->in_commit = 1;
  461. cur_trans = trans->transaction;
  462. if (cur_trans->list.prev != &root->fs_info->trans_list) {
  463. prev_trans = list_entry(cur_trans->list.prev,
  464. struct btrfs_transaction, list);
  465. if (!prev_trans->commit_done) {
  466. prev_trans->use_count++;
  467. mutex_unlock(&root->fs_info->fs_mutex);
  468. mutex_unlock(&root->fs_info->trans_mutex);
  469. wait_for_commit(root, prev_trans);
  470. mutex_lock(&root->fs_info->fs_mutex);
  471. mutex_lock(&root->fs_info->trans_mutex);
  472. put_transaction(prev_trans);
  473. }
  474. }
  475. do {
  476. joined = cur_trans->num_joined;
  477. WARN_ON(cur_trans != trans->transaction);
  478. prepare_to_wait(&cur_trans->writer_wait, &wait,
  479. TASK_UNINTERRUPTIBLE);
  480. if (cur_trans->num_writers > 1)
  481. timeout = MAX_SCHEDULE_TIMEOUT;
  482. else
  483. timeout = 1;
  484. mutex_unlock(&root->fs_info->fs_mutex);
  485. mutex_unlock(&root->fs_info->trans_mutex);
  486. schedule_timeout(timeout);
  487. mutex_lock(&root->fs_info->fs_mutex);
  488. mutex_lock(&root->fs_info->trans_mutex);
  489. finish_wait(&cur_trans->writer_wait, &wait);
  490. } while (cur_trans->num_writers > 1 ||
  491. (cur_trans->num_joined != joined));
  492. WARN_ON(cur_trans != trans->transaction);
  493. ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
  494. &dirty_fs_roots);
  495. BUG_ON(ret);
  496. ret = btrfs_commit_tree_roots(trans, root);
  497. BUG_ON(ret);
  498. cur_trans = root->fs_info->running_transaction;
  499. root->fs_info->running_transaction = NULL;
  500. btrfs_set_super_generation(&root->fs_info->super_copy,
  501. cur_trans->transid);
  502. btrfs_set_super_root(&root->fs_info->super_copy,
  503. root->fs_info->tree_root->node->start);
  504. btrfs_set_super_root_level(&root->fs_info->super_copy,
  505. btrfs_header_level(root->fs_info->tree_root->node));
  506. write_extent_buffer(root->fs_info->sb_buffer,
  507. &root->fs_info->super_copy, 0,
  508. sizeof(root->fs_info->super_copy));
  509. btrfs_copy_pinned(root, &pinned_copy);
  510. mutex_unlock(&root->fs_info->trans_mutex);
  511. mutex_unlock(&root->fs_info->fs_mutex);
  512. ret = btrfs_write_and_wait_transaction(trans, root);
  513. BUG_ON(ret);
  514. write_ctree_super(trans, root);
  515. mutex_lock(&root->fs_info->fs_mutex);
  516. btrfs_finish_extent_commit(trans, root, &pinned_copy);
  517. mutex_lock(&root->fs_info->trans_mutex);
  518. cur_trans->commit_done = 1;
  519. root->fs_info->last_trans_committed = cur_trans->transid;
  520. wake_up(&cur_trans->commit_wait);
  521. put_transaction(cur_trans);
  522. put_transaction(cur_trans);
  523. if (root->fs_info->closing)
  524. list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
  525. else
  526. list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
  527. mutex_unlock(&root->fs_info->trans_mutex);
  528. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  529. if (root->fs_info->closing) {
  530. mutex_unlock(&root->fs_info->fs_mutex);
  531. drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
  532. mutex_lock(&root->fs_info->fs_mutex);
  533. }
  534. return ret;
  535. }
  536. int btrfs_clean_old_snapshots(struct btrfs_root *root)
  537. {
  538. struct list_head dirty_roots;
  539. INIT_LIST_HEAD(&dirty_roots);
  540. mutex_lock(&root->fs_info->trans_mutex);
  541. list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
  542. mutex_unlock(&root->fs_info->trans_mutex);
  543. if (!list_empty(&dirty_roots)) {
  544. drop_dirty_roots(root, &dirty_roots);
  545. }
  546. return 0;
  547. }
  548. void btrfs_transaction_cleaner(struct work_struct *work)
  549. {
  550. struct btrfs_fs_info *fs_info = container_of(work,
  551. struct btrfs_fs_info,
  552. trans_work.work);
  553. struct btrfs_root *root = fs_info->tree_root;
  554. struct btrfs_transaction *cur;
  555. struct btrfs_trans_handle *trans;
  556. unsigned long now;
  557. unsigned long delay = HZ * 30;
  558. int ret;
  559. mutex_lock(&root->fs_info->fs_mutex);
  560. mutex_lock(&root->fs_info->trans_mutex);
  561. cur = root->fs_info->running_transaction;
  562. if (!cur) {
  563. mutex_unlock(&root->fs_info->trans_mutex);
  564. goto out;
  565. }
  566. now = get_seconds();
  567. if (now < cur->start_time || now - cur->start_time < 30) {
  568. mutex_unlock(&root->fs_info->trans_mutex);
  569. delay = HZ * 5;
  570. goto out;
  571. }
  572. mutex_unlock(&root->fs_info->trans_mutex);
  573. btrfs_defrag_dirty_roots(root->fs_info);
  574. trans = btrfs_start_transaction(root, 1);
  575. ret = btrfs_commit_transaction(trans, root);
  576. out:
  577. mutex_unlock(&root->fs_info->fs_mutex);
  578. btrfs_clean_old_snapshots(root);
  579. btrfs_transaction_queue_work(root, delay);
  580. }
  581. void btrfs_transaction_queue_work(struct btrfs_root *root, int delay)
  582. {
  583. queue_delayed_work(trans_wq, &root->fs_info->trans_work, delay);
  584. }
  585. void btrfs_transaction_flush_work(struct btrfs_root *root)
  586. {
  587. cancel_rearming_delayed_workqueue(trans_wq, &root->fs_info->trans_work);
  588. flush_workqueue(trans_wq);
  589. }
  590. void __init btrfs_init_transaction_sys(void)
  591. {
  592. trans_wq = create_workqueue("btrfs");
  593. }
  594. void btrfs_exit_transaction_sys(void)
  595. {
  596. destroy_workqueue(trans_wq);
  597. }