transaction.c 22 KB

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