transaction.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  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. INIT_LIST_HEAD(&cur_trans->pending_snapshots);
  64. list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
  65. btrfs_ordered_inode_tree_init(&cur_trans->ordered_inode_tree);
  66. extent_map_tree_init(&cur_trans->dirty_pages,
  67. root->fs_info->btree_inode->i_mapping,
  68. GFP_NOFS);
  69. } else {
  70. cur_trans->num_writers++;
  71. cur_trans->num_joined++;
  72. }
  73. return 0;
  74. }
  75. static int record_root_in_trans(struct btrfs_root *root)
  76. {
  77. u64 running_trans_id = root->fs_info->running_transaction->transid;
  78. if (root->ref_cows && root->last_trans < running_trans_id) {
  79. WARN_ON(root == root->fs_info->extent_root);
  80. if (root->root_item.refs != 0) {
  81. radix_tree_tag_set(&root->fs_info->fs_roots_radix,
  82. (unsigned long)root->root_key.objectid,
  83. BTRFS_ROOT_TRANS_TAG);
  84. radix_tree_tag_set(&root->fs_info->fs_roots_radix,
  85. (unsigned long)root->root_key.objectid,
  86. BTRFS_ROOT_DEFRAG_TAG);
  87. root->commit_root = root->node;
  88. extent_buffer_get(root->node);
  89. } else {
  90. WARN_ON(1);
  91. }
  92. root->last_trans = running_trans_id;
  93. }
  94. return 0;
  95. }
  96. struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
  97. int num_blocks)
  98. {
  99. struct btrfs_trans_handle *h =
  100. kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
  101. int ret;
  102. mutex_lock(&root->fs_info->trans_mutex);
  103. ret = join_transaction(root);
  104. BUG_ON(ret);
  105. record_root_in_trans(root);
  106. h->transid = root->fs_info->running_transaction->transid;
  107. h->transaction = root->fs_info->running_transaction;
  108. h->blocks_reserved = num_blocks;
  109. h->blocks_used = 0;
  110. h->block_group = NULL;
  111. h->alloc_exclude_nr = 0;
  112. h->alloc_exclude_start = 0;
  113. root->fs_info->running_transaction->use_count++;
  114. mutex_unlock(&root->fs_info->trans_mutex);
  115. return h;
  116. }
  117. int btrfs_end_transaction(struct btrfs_trans_handle *trans,
  118. struct btrfs_root *root)
  119. {
  120. struct btrfs_transaction *cur_trans;
  121. mutex_lock(&root->fs_info->trans_mutex);
  122. cur_trans = root->fs_info->running_transaction;
  123. WARN_ON(cur_trans != trans->transaction);
  124. WARN_ON(cur_trans->num_writers < 1);
  125. cur_trans->num_writers--;
  126. if (waitqueue_active(&cur_trans->writer_wait))
  127. wake_up(&cur_trans->writer_wait);
  128. put_transaction(cur_trans);
  129. mutex_unlock(&root->fs_info->trans_mutex);
  130. memset(trans, 0, sizeof(*trans));
  131. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  132. return 0;
  133. }
  134. int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
  135. struct btrfs_root *root)
  136. {
  137. int ret;
  138. int err;
  139. int werr = 0;
  140. struct extent_map_tree *dirty_pages;
  141. struct page *page;
  142. struct inode *btree_inode = root->fs_info->btree_inode;
  143. u64 start;
  144. u64 end;
  145. unsigned long index;
  146. if (!trans || !trans->transaction) {
  147. return filemap_write_and_wait(btree_inode->i_mapping);
  148. }
  149. dirty_pages = &trans->transaction->dirty_pages;
  150. while(1) {
  151. ret = find_first_extent_bit(dirty_pages, 0, &start, &end,
  152. EXTENT_DIRTY);
  153. if (ret)
  154. break;
  155. clear_extent_dirty(dirty_pages, start, end, GFP_NOFS);
  156. while(start <= end) {
  157. index = start >> PAGE_CACHE_SHIFT;
  158. start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
  159. page = find_lock_page(btree_inode->i_mapping, index);
  160. if (!page)
  161. continue;
  162. if (PageWriteback(page)) {
  163. if (PageDirty(page))
  164. wait_on_page_writeback(page);
  165. else {
  166. unlock_page(page);
  167. page_cache_release(page);
  168. continue;
  169. }
  170. }
  171. err = write_one_page(page, 0);
  172. if (err)
  173. werr = err;
  174. page_cache_release(page);
  175. }
  176. }
  177. err = filemap_fdatawait(btree_inode->i_mapping);
  178. if (err)
  179. werr = err;
  180. return werr;
  181. }
  182. int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
  183. struct btrfs_root *root)
  184. {
  185. int ret;
  186. u64 old_extent_block;
  187. struct btrfs_fs_info *fs_info = root->fs_info;
  188. struct btrfs_root *tree_root = fs_info->tree_root;
  189. struct btrfs_root *extent_root = fs_info->extent_root;
  190. btrfs_write_dirty_block_groups(trans, extent_root);
  191. while(1) {
  192. old_extent_block = btrfs_root_bytenr(&extent_root->root_item);
  193. if (old_extent_block == extent_root->node->start)
  194. break;
  195. btrfs_set_root_bytenr(&extent_root->root_item,
  196. extent_root->node->start);
  197. btrfs_set_root_level(&extent_root->root_item,
  198. btrfs_header_level(extent_root->node));
  199. ret = btrfs_update_root(trans, tree_root,
  200. &extent_root->root_key,
  201. &extent_root->root_item);
  202. BUG_ON(ret);
  203. btrfs_write_dirty_block_groups(trans, extent_root);
  204. }
  205. return 0;
  206. }
  207. static int wait_for_commit(struct btrfs_root *root,
  208. struct btrfs_transaction *commit)
  209. {
  210. DEFINE_WAIT(wait);
  211. mutex_lock(&root->fs_info->trans_mutex);
  212. while(!commit->commit_done) {
  213. prepare_to_wait(&commit->commit_wait, &wait,
  214. TASK_UNINTERRUPTIBLE);
  215. if (commit->commit_done)
  216. break;
  217. mutex_unlock(&root->fs_info->trans_mutex);
  218. schedule();
  219. mutex_lock(&root->fs_info->trans_mutex);
  220. }
  221. mutex_unlock(&root->fs_info->trans_mutex);
  222. finish_wait(&commit->commit_wait, &wait);
  223. return 0;
  224. }
  225. struct dirty_root {
  226. struct list_head list;
  227. struct btrfs_root *root;
  228. struct btrfs_root *latest_root;
  229. };
  230. int btrfs_add_dead_root(struct btrfs_root *root,
  231. struct btrfs_root *latest,
  232. struct list_head *dead_list)
  233. {
  234. struct dirty_root *dirty;
  235. dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
  236. if (!dirty)
  237. return -ENOMEM;
  238. dirty->root = root;
  239. dirty->latest_root = latest;
  240. list_add(&dirty->list, dead_list);
  241. return 0;
  242. }
  243. static int add_dirty_roots(struct btrfs_trans_handle *trans,
  244. struct radix_tree_root *radix,
  245. struct list_head *list)
  246. {
  247. struct dirty_root *dirty;
  248. struct btrfs_root *gang[8];
  249. struct btrfs_root *root;
  250. int i;
  251. int ret;
  252. int err = 0;
  253. u32 refs;
  254. while(1) {
  255. ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
  256. ARRAY_SIZE(gang),
  257. BTRFS_ROOT_TRANS_TAG);
  258. if (ret == 0)
  259. break;
  260. for (i = 0; i < ret; i++) {
  261. root = gang[i];
  262. radix_tree_tag_clear(radix,
  263. (unsigned long)root->root_key.objectid,
  264. BTRFS_ROOT_TRANS_TAG);
  265. if (root->commit_root == root->node) {
  266. WARN_ON(root->node->start !=
  267. btrfs_root_bytenr(&root->root_item));
  268. free_extent_buffer(root->commit_root);
  269. root->commit_root = NULL;
  270. /* make sure to update the root on disk
  271. * so we get any updates to the block used
  272. * counts
  273. */
  274. err = btrfs_update_root(trans,
  275. root->fs_info->tree_root,
  276. &root->root_key,
  277. &root->root_item);
  278. continue;
  279. }
  280. dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
  281. BUG_ON(!dirty);
  282. dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS);
  283. BUG_ON(!dirty->root);
  284. memset(&root->root_item.drop_progress, 0,
  285. sizeof(struct btrfs_disk_key));
  286. root->root_item.drop_level = 0;
  287. memcpy(dirty->root, root, sizeof(*root));
  288. dirty->root->node = root->commit_root;
  289. dirty->latest_root = root;
  290. root->commit_root = NULL;
  291. root->root_key.offset = root->fs_info->generation;
  292. btrfs_set_root_bytenr(&root->root_item,
  293. root->node->start);
  294. btrfs_set_root_level(&root->root_item,
  295. btrfs_header_level(root->node));
  296. err = btrfs_insert_root(trans, root->fs_info->tree_root,
  297. &root->root_key,
  298. &root->root_item);
  299. if (err)
  300. break;
  301. refs = btrfs_root_refs(&dirty->root->root_item);
  302. btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
  303. err = btrfs_update_root(trans, root->fs_info->tree_root,
  304. &dirty->root->root_key,
  305. &dirty->root->root_item);
  306. BUG_ON(err);
  307. if (refs == 1) {
  308. list_add(&dirty->list, list);
  309. } else {
  310. WARN_ON(1);
  311. kfree(dirty->root);
  312. kfree(dirty);
  313. }
  314. }
  315. }
  316. return err;
  317. }
  318. int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
  319. {
  320. struct btrfs_fs_info *info = root->fs_info;
  321. int ret;
  322. struct btrfs_trans_handle *trans;
  323. unsigned long nr;
  324. if (root->defrag_running)
  325. return 0;
  326. trans = btrfs_start_transaction(root, 1);
  327. while (1) {
  328. root->defrag_running = 1;
  329. ret = btrfs_defrag_leaves(trans, root, cacheonly);
  330. nr = trans->blocks_used;
  331. btrfs_end_transaction(trans, root);
  332. mutex_unlock(&info->fs_mutex);
  333. btrfs_btree_balance_dirty(info->tree_root, nr);
  334. cond_resched();
  335. mutex_lock(&info->fs_mutex);
  336. trans = btrfs_start_transaction(root, 1);
  337. if (ret != -EAGAIN)
  338. break;
  339. }
  340. root->defrag_running = 0;
  341. radix_tree_tag_clear(&info->fs_roots_radix,
  342. (unsigned long)root->root_key.objectid,
  343. BTRFS_ROOT_DEFRAG_TAG);
  344. btrfs_end_transaction(trans, root);
  345. return 0;
  346. }
  347. int btrfs_defrag_dirty_roots(struct btrfs_fs_info *info)
  348. {
  349. struct btrfs_root *gang[1];
  350. struct btrfs_root *root;
  351. int i;
  352. int ret;
  353. int err = 0;
  354. u64 last = 0;
  355. while(1) {
  356. ret = radix_tree_gang_lookup_tag(&info->fs_roots_radix,
  357. (void **)gang, last,
  358. ARRAY_SIZE(gang),
  359. BTRFS_ROOT_DEFRAG_TAG);
  360. if (ret == 0)
  361. break;
  362. for (i = 0; i < ret; i++) {
  363. root = gang[i];
  364. last = root->root_key.objectid + 1;
  365. btrfs_defrag_root(root, 1);
  366. }
  367. }
  368. btrfs_defrag_root(info->extent_root, 1);
  369. return err;
  370. }
  371. static int drop_dirty_roots(struct btrfs_root *tree_root,
  372. struct list_head *list)
  373. {
  374. struct dirty_root *dirty;
  375. struct btrfs_trans_handle *trans;
  376. unsigned long nr;
  377. u64 num_bytes;
  378. u64 bytes_used;
  379. int ret = 0;
  380. int err;
  381. while(!list_empty(list)) {
  382. struct btrfs_root *root;
  383. mutex_lock(&tree_root->fs_info->fs_mutex);
  384. dirty = list_entry(list->next, struct dirty_root, list);
  385. list_del_init(&dirty->list);
  386. num_bytes = btrfs_root_used(&dirty->root->root_item);
  387. root = dirty->latest_root;
  388. while(1) {
  389. trans = btrfs_start_transaction(tree_root, 1);
  390. ret = btrfs_drop_snapshot(trans, dirty->root);
  391. if (ret != -EAGAIN) {
  392. break;
  393. }
  394. err = btrfs_update_root(trans,
  395. tree_root,
  396. &dirty->root->root_key,
  397. &dirty->root->root_item);
  398. if (err)
  399. ret = err;
  400. nr = trans->blocks_used;
  401. ret = btrfs_end_transaction(trans, tree_root);
  402. BUG_ON(ret);
  403. mutex_unlock(&tree_root->fs_info->fs_mutex);
  404. btrfs_btree_balance_dirty(tree_root, nr);
  405. cond_resched();
  406. mutex_lock(&tree_root->fs_info->fs_mutex);
  407. }
  408. BUG_ON(ret);
  409. num_bytes -= btrfs_root_used(&dirty->root->root_item);
  410. bytes_used = btrfs_root_used(&root->root_item);
  411. if (num_bytes) {
  412. record_root_in_trans(root);
  413. btrfs_set_root_used(&root->root_item,
  414. bytes_used - num_bytes);
  415. }
  416. ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
  417. if (ret) {
  418. BUG();
  419. break;
  420. }
  421. nr = trans->blocks_used;
  422. ret = btrfs_end_transaction(trans, tree_root);
  423. BUG_ON(ret);
  424. free_extent_buffer(dirty->root->node);
  425. kfree(dirty->root);
  426. kfree(dirty);
  427. mutex_unlock(&tree_root->fs_info->fs_mutex);
  428. btrfs_btree_balance_dirty(tree_root, nr);
  429. cond_resched();
  430. }
  431. return ret;
  432. }
  433. int btrfs_write_ordered_inodes(struct btrfs_trans_handle *trans,
  434. struct btrfs_root *root)
  435. {
  436. struct btrfs_transaction *cur_trans = trans->transaction;
  437. struct inode *inode;
  438. u64 root_objectid = 0;
  439. u64 objectid = 0;
  440. int ret;
  441. while(1) {
  442. ret = btrfs_find_first_ordered_inode(
  443. &cur_trans->ordered_inode_tree,
  444. &root_objectid, &objectid);
  445. if (!ret)
  446. break;
  447. mutex_unlock(&root->fs_info->trans_mutex);
  448. mutex_unlock(&root->fs_info->fs_mutex);
  449. inode = btrfs_ilookup(root->fs_info->sb, objectid,
  450. root_objectid);
  451. if (inode) {
  452. if (S_ISREG(inode->i_mode))
  453. filemap_fdatawrite(inode->i_mapping);
  454. iput(inode);
  455. }
  456. mutex_lock(&root->fs_info->fs_mutex);
  457. mutex_lock(&root->fs_info->trans_mutex);
  458. }
  459. while(1) {
  460. root_objectid = 0;
  461. objectid = 0;
  462. ret = btrfs_find_del_first_ordered_inode(
  463. &cur_trans->ordered_inode_tree,
  464. &root_objectid, &objectid);
  465. if (!ret)
  466. break;
  467. mutex_unlock(&root->fs_info->trans_mutex);
  468. mutex_unlock(&root->fs_info->fs_mutex);
  469. inode = btrfs_ilookup(root->fs_info->sb, objectid,
  470. root_objectid);
  471. if (inode) {
  472. if (S_ISREG(inode->i_mode))
  473. filemap_write_and_wait(inode->i_mapping);
  474. iput(inode);
  475. }
  476. mutex_lock(&root->fs_info->fs_mutex);
  477. mutex_lock(&root->fs_info->trans_mutex);
  478. }
  479. return 0;
  480. }
  481. static int create_pending_snapshot(struct btrfs_trans_handle *trans,
  482. struct btrfs_fs_info *fs_info,
  483. struct btrfs_pending_snapshot *pending)
  484. {
  485. struct btrfs_key key;
  486. struct btrfs_root_item new_root_item;
  487. struct btrfs_root *tree_root = fs_info->tree_root;
  488. struct btrfs_root *root = pending->root;
  489. struct extent_buffer *tmp;
  490. int ret;
  491. u64 objectid;
  492. ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
  493. if (ret)
  494. goto fail;
  495. memcpy(&new_root_item, &root->root_item, sizeof(new_root_item));
  496. key.objectid = objectid;
  497. key.offset = 1;
  498. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  499. extent_buffer_get(root->node);
  500. btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp);
  501. free_extent_buffer(tmp);
  502. btrfs_copy_root(trans, root, root->node, &tmp, objectid);
  503. btrfs_set_root_bytenr(&new_root_item, tmp->start);
  504. btrfs_set_root_level(&new_root_item, btrfs_header_level(tmp));
  505. ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
  506. &new_root_item);
  507. free_extent_buffer(tmp);
  508. if (ret)
  509. goto fail;
  510. /*
  511. * insert the directory item
  512. */
  513. key.offset = (u64)-1;
  514. ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
  515. pending->name, strlen(pending->name),
  516. root->fs_info->sb->s_root->d_inode->i_ino,
  517. &key, BTRFS_FT_DIR);
  518. if (ret)
  519. goto fail;
  520. ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
  521. pending->name, strlen(pending->name), objectid,
  522. root->fs_info->sb->s_root->d_inode->i_ino);
  523. fail:
  524. return ret;
  525. }
  526. static int create_pending_snapshots(struct btrfs_trans_handle *trans,
  527. struct btrfs_fs_info *fs_info)
  528. {
  529. struct btrfs_pending_snapshot *pending;
  530. struct list_head *head = &trans->transaction->pending_snapshots;
  531. int ret;
  532. while(!list_empty(head)) {
  533. pending = list_entry(head->next,
  534. struct btrfs_pending_snapshot, list);
  535. ret = create_pending_snapshot(trans, fs_info, pending);
  536. BUG_ON(ret);
  537. list_del(&pending->list);
  538. kfree(pending->name);
  539. kfree(pending);
  540. }
  541. return 0;
  542. }
  543. int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
  544. struct btrfs_root *root)
  545. {
  546. unsigned long joined = 0;
  547. unsigned long timeout = 1;
  548. struct btrfs_transaction *cur_trans;
  549. struct btrfs_transaction *prev_trans = NULL;
  550. struct list_head dirty_fs_roots;
  551. struct extent_map_tree *pinned_copy;
  552. DEFINE_WAIT(wait);
  553. int ret;
  554. INIT_LIST_HEAD(&dirty_fs_roots);
  555. mutex_lock(&root->fs_info->trans_mutex);
  556. if (trans->transaction->in_commit) {
  557. cur_trans = trans->transaction;
  558. trans->transaction->use_count++;
  559. mutex_unlock(&root->fs_info->trans_mutex);
  560. btrfs_end_transaction(trans, root);
  561. mutex_unlock(&root->fs_info->fs_mutex);
  562. ret = wait_for_commit(root, cur_trans);
  563. BUG_ON(ret);
  564. mutex_lock(&root->fs_info->trans_mutex);
  565. put_transaction(cur_trans);
  566. mutex_unlock(&root->fs_info->trans_mutex);
  567. mutex_lock(&root->fs_info->fs_mutex);
  568. return 0;
  569. }
  570. pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS);
  571. if (!pinned_copy)
  572. return -ENOMEM;
  573. extent_map_tree_init(pinned_copy,
  574. root->fs_info->btree_inode->i_mapping, GFP_NOFS);
  575. trans->transaction->in_commit = 1;
  576. cur_trans = trans->transaction;
  577. if (cur_trans->list.prev != &root->fs_info->trans_list) {
  578. prev_trans = list_entry(cur_trans->list.prev,
  579. struct btrfs_transaction, list);
  580. if (!prev_trans->commit_done) {
  581. prev_trans->use_count++;
  582. mutex_unlock(&root->fs_info->fs_mutex);
  583. mutex_unlock(&root->fs_info->trans_mutex);
  584. wait_for_commit(root, prev_trans);
  585. mutex_lock(&root->fs_info->fs_mutex);
  586. mutex_lock(&root->fs_info->trans_mutex);
  587. put_transaction(prev_trans);
  588. }
  589. }
  590. do {
  591. joined = cur_trans->num_joined;
  592. WARN_ON(cur_trans != trans->transaction);
  593. prepare_to_wait(&cur_trans->writer_wait, &wait,
  594. TASK_UNINTERRUPTIBLE);
  595. if (cur_trans->num_writers > 1)
  596. timeout = MAX_SCHEDULE_TIMEOUT;
  597. else
  598. timeout = 1;
  599. mutex_unlock(&root->fs_info->fs_mutex);
  600. mutex_unlock(&root->fs_info->trans_mutex);
  601. schedule_timeout(timeout);
  602. mutex_lock(&root->fs_info->fs_mutex);
  603. mutex_lock(&root->fs_info->trans_mutex);
  604. finish_wait(&cur_trans->writer_wait, &wait);
  605. ret = btrfs_write_ordered_inodes(trans, root);
  606. } while (cur_trans->num_writers > 1 ||
  607. (cur_trans->num_joined != joined));
  608. ret = create_pending_snapshots(trans, root->fs_info);
  609. BUG_ON(ret);
  610. WARN_ON(cur_trans != trans->transaction);
  611. ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
  612. &dirty_fs_roots);
  613. BUG_ON(ret);
  614. ret = btrfs_commit_tree_roots(trans, root);
  615. BUG_ON(ret);
  616. cur_trans = root->fs_info->running_transaction;
  617. root->fs_info->running_transaction = NULL;
  618. btrfs_set_super_generation(&root->fs_info->super_copy,
  619. cur_trans->transid);
  620. btrfs_set_super_root(&root->fs_info->super_copy,
  621. root->fs_info->tree_root->node->start);
  622. btrfs_set_super_root_level(&root->fs_info->super_copy,
  623. btrfs_header_level(root->fs_info->tree_root->node));
  624. write_extent_buffer(root->fs_info->sb_buffer,
  625. &root->fs_info->super_copy, 0,
  626. sizeof(root->fs_info->super_copy));
  627. btrfs_copy_pinned(root, pinned_copy);
  628. mutex_unlock(&root->fs_info->trans_mutex);
  629. mutex_unlock(&root->fs_info->fs_mutex);
  630. ret = btrfs_write_and_wait_transaction(trans, root);
  631. BUG_ON(ret);
  632. write_ctree_super(trans, root);
  633. mutex_lock(&root->fs_info->fs_mutex);
  634. btrfs_finish_extent_commit(trans, root, pinned_copy);
  635. mutex_lock(&root->fs_info->trans_mutex);
  636. kfree(pinned_copy);
  637. cur_trans->commit_done = 1;
  638. root->fs_info->last_trans_committed = cur_trans->transid;
  639. wake_up(&cur_trans->commit_wait);
  640. put_transaction(cur_trans);
  641. put_transaction(cur_trans);
  642. if (root->fs_info->closing)
  643. list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
  644. else
  645. list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
  646. mutex_unlock(&root->fs_info->trans_mutex);
  647. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  648. if (root->fs_info->closing) {
  649. mutex_unlock(&root->fs_info->fs_mutex);
  650. drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
  651. mutex_lock(&root->fs_info->fs_mutex);
  652. }
  653. return ret;
  654. }
  655. int btrfs_clean_old_snapshots(struct btrfs_root *root)
  656. {
  657. struct list_head dirty_roots;
  658. INIT_LIST_HEAD(&dirty_roots);
  659. mutex_lock(&root->fs_info->trans_mutex);
  660. list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
  661. mutex_unlock(&root->fs_info->trans_mutex);
  662. if (!list_empty(&dirty_roots)) {
  663. drop_dirty_roots(root, &dirty_roots);
  664. }
  665. return 0;
  666. }
  667. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
  668. void btrfs_transaction_cleaner(void *p)
  669. #else
  670. void btrfs_transaction_cleaner(struct work_struct *work)
  671. #endif
  672. {
  673. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
  674. struct btrfs_fs_info *fs_info = p;
  675. #else
  676. struct btrfs_fs_info *fs_info = container_of(work,
  677. struct btrfs_fs_info,
  678. trans_work.work);
  679. #endif
  680. struct btrfs_root *root = fs_info->tree_root;
  681. struct btrfs_transaction *cur;
  682. struct btrfs_trans_handle *trans;
  683. unsigned long now;
  684. unsigned long delay = HZ * 30;
  685. int ret;
  686. mutex_lock(&root->fs_info->fs_mutex);
  687. mutex_lock(&root->fs_info->trans_mutex);
  688. cur = root->fs_info->running_transaction;
  689. if (!cur) {
  690. mutex_unlock(&root->fs_info->trans_mutex);
  691. goto out;
  692. }
  693. now = get_seconds();
  694. if (now < cur->start_time || now - cur->start_time < 30) {
  695. mutex_unlock(&root->fs_info->trans_mutex);
  696. delay = HZ * 5;
  697. goto out;
  698. }
  699. mutex_unlock(&root->fs_info->trans_mutex);
  700. btrfs_defrag_dirty_roots(root->fs_info);
  701. trans = btrfs_start_transaction(root, 1);
  702. ret = btrfs_commit_transaction(trans, root);
  703. out:
  704. mutex_unlock(&root->fs_info->fs_mutex);
  705. btrfs_clean_old_snapshots(root);
  706. btrfs_transaction_queue_work(root, delay);
  707. }
  708. void btrfs_transaction_queue_work(struct btrfs_root *root, int delay)
  709. {
  710. queue_delayed_work(trans_wq, &root->fs_info->trans_work, delay);
  711. }
  712. void btrfs_transaction_flush_work(struct btrfs_root *root)
  713. {
  714. cancel_rearming_delayed_workqueue(trans_wq, &root->fs_info->trans_work);
  715. flush_workqueue(trans_wq);
  716. }
  717. void __init btrfs_init_transaction_sys(void)
  718. {
  719. trans_wq = create_workqueue("btrfs");
  720. }
  721. void btrfs_exit_transaction_sys(void)
  722. {
  723. destroy_workqueue(trans_wq);
  724. }