transaction.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  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. #include "locking.h"
  26. static int total_trans = 0;
  27. extern struct kmem_cache *btrfs_trans_handle_cachep;
  28. extern struct kmem_cache *btrfs_transaction_cachep;
  29. #define BTRFS_ROOT_TRANS_TAG 0
  30. static noinline void put_transaction(struct btrfs_transaction *transaction)
  31. {
  32. WARN_ON(transaction->use_count == 0);
  33. transaction->use_count--;
  34. if (transaction->use_count == 0) {
  35. WARN_ON(total_trans == 0);
  36. total_trans--;
  37. list_del_init(&transaction->list);
  38. memset(transaction, 0, sizeof(*transaction));
  39. kmem_cache_free(btrfs_transaction_cachep, transaction);
  40. }
  41. }
  42. static noinline int join_transaction(struct btrfs_root *root)
  43. {
  44. struct btrfs_transaction *cur_trans;
  45. cur_trans = root->fs_info->running_transaction;
  46. if (!cur_trans) {
  47. cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
  48. GFP_NOFS);
  49. total_trans++;
  50. BUG_ON(!cur_trans);
  51. root->fs_info->generation++;
  52. root->fs_info->last_alloc = 0;
  53. root->fs_info->last_data_alloc = 0;
  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->blocked = 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. extent_io_tree_init(&cur_trans->dirty_pages,
  67. root->fs_info->btree_inode->i_mapping,
  68. GFP_NOFS);
  69. spin_lock(&root->fs_info->new_trans_lock);
  70. root->fs_info->running_transaction = cur_trans;
  71. spin_unlock(&root->fs_info->new_trans_lock);
  72. } else {
  73. cur_trans->num_writers++;
  74. cur_trans->num_joined++;
  75. }
  76. return 0;
  77. }
  78. static noinline int record_root_in_trans(struct btrfs_root *root)
  79. {
  80. u64 running_trans_id = root->fs_info->running_transaction->transid;
  81. if (root->ref_cows && root->last_trans < running_trans_id) {
  82. WARN_ON(root == root->fs_info->extent_root);
  83. if (root->root_item.refs != 0) {
  84. radix_tree_tag_set(&root->fs_info->fs_roots_radix,
  85. (unsigned long)root->root_key.objectid,
  86. BTRFS_ROOT_TRANS_TAG);
  87. root->commit_root = btrfs_root_node(root);
  88. } else {
  89. WARN_ON(1);
  90. }
  91. root->last_trans = running_trans_id;
  92. }
  93. return 0;
  94. }
  95. struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
  96. int num_blocks, int join)
  97. {
  98. struct btrfs_trans_handle *h =
  99. kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
  100. struct btrfs_transaction *cur_trans;
  101. int ret;
  102. mutex_lock(&root->fs_info->trans_mutex);
  103. cur_trans = root->fs_info->running_transaction;
  104. if (cur_trans && cur_trans->blocked && !join) {
  105. DEFINE_WAIT(wait);
  106. cur_trans->use_count++;
  107. while(1) {
  108. prepare_to_wait(&root->fs_info->transaction_wait, &wait,
  109. TASK_UNINTERRUPTIBLE);
  110. if (cur_trans->blocked) {
  111. mutex_unlock(&root->fs_info->trans_mutex);
  112. schedule();
  113. mutex_lock(&root->fs_info->trans_mutex);
  114. finish_wait(&root->fs_info->transaction_wait,
  115. &wait);
  116. } else {
  117. finish_wait(&root->fs_info->transaction_wait,
  118. &wait);
  119. break;
  120. }
  121. }
  122. put_transaction(cur_trans);
  123. }
  124. ret = join_transaction(root);
  125. BUG_ON(ret);
  126. record_root_in_trans(root);
  127. h->transid = root->fs_info->running_transaction->transid;
  128. h->transaction = root->fs_info->running_transaction;
  129. h->blocks_reserved = num_blocks;
  130. h->blocks_used = 0;
  131. h->block_group = NULL;
  132. h->alloc_exclude_nr = 0;
  133. h->alloc_exclude_start = 0;
  134. root->fs_info->running_transaction->use_count++;
  135. mutex_unlock(&root->fs_info->trans_mutex);
  136. return h;
  137. }
  138. struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
  139. int num_blocks)
  140. {
  141. return start_transaction(root, num_blocks, 0);
  142. }
  143. struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root,
  144. int num_blocks)
  145. {
  146. return start_transaction(root, num_blocks, 1);
  147. }
  148. static noinline int wait_for_commit(struct btrfs_root *root,
  149. struct btrfs_transaction *commit)
  150. {
  151. DEFINE_WAIT(wait);
  152. mutex_lock(&root->fs_info->trans_mutex);
  153. while(!commit->commit_done) {
  154. prepare_to_wait(&commit->commit_wait, &wait,
  155. TASK_UNINTERRUPTIBLE);
  156. if (commit->commit_done)
  157. break;
  158. mutex_unlock(&root->fs_info->trans_mutex);
  159. schedule();
  160. mutex_lock(&root->fs_info->trans_mutex);
  161. }
  162. mutex_unlock(&root->fs_info->trans_mutex);
  163. finish_wait(&commit->commit_wait, &wait);
  164. return 0;
  165. }
  166. static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
  167. struct btrfs_root *root, int throttle)
  168. {
  169. struct btrfs_transaction *cur_trans;
  170. mutex_lock(&root->fs_info->trans_mutex);
  171. cur_trans = root->fs_info->running_transaction;
  172. WARN_ON(cur_trans != trans->transaction);
  173. WARN_ON(cur_trans->num_writers < 1);
  174. cur_trans->num_writers--;
  175. if (waitqueue_active(&cur_trans->writer_wait))
  176. wake_up(&cur_trans->writer_wait);
  177. if (0 && cur_trans->in_commit && throttle) {
  178. DEFINE_WAIT(wait);
  179. mutex_unlock(&root->fs_info->trans_mutex);
  180. prepare_to_wait(&root->fs_info->transaction_throttle, &wait,
  181. TASK_UNINTERRUPTIBLE);
  182. schedule();
  183. finish_wait(&root->fs_info->transaction_throttle, &wait);
  184. mutex_lock(&root->fs_info->trans_mutex);
  185. }
  186. put_transaction(cur_trans);
  187. mutex_unlock(&root->fs_info->trans_mutex);
  188. memset(trans, 0, sizeof(*trans));
  189. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  190. return 0;
  191. }
  192. int btrfs_end_transaction(struct btrfs_trans_handle *trans,
  193. struct btrfs_root *root)
  194. {
  195. return __btrfs_end_transaction(trans, root, 0);
  196. }
  197. int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
  198. struct btrfs_root *root)
  199. {
  200. return __btrfs_end_transaction(trans, root, 1);
  201. }
  202. int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
  203. struct btrfs_root *root)
  204. {
  205. int ret;
  206. int err;
  207. int werr = 0;
  208. struct extent_io_tree *dirty_pages;
  209. struct page *page;
  210. struct inode *btree_inode = root->fs_info->btree_inode;
  211. u64 start;
  212. u64 end;
  213. unsigned long index;
  214. if (!trans || !trans->transaction) {
  215. return filemap_write_and_wait(btree_inode->i_mapping);
  216. }
  217. dirty_pages = &trans->transaction->dirty_pages;
  218. while(1) {
  219. ret = find_first_extent_bit(dirty_pages, 0, &start, &end,
  220. EXTENT_DIRTY);
  221. if (ret)
  222. break;
  223. clear_extent_dirty(dirty_pages, start, end, GFP_NOFS);
  224. while(start <= end) {
  225. index = start >> PAGE_CACHE_SHIFT;
  226. start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
  227. page = find_lock_page(btree_inode->i_mapping, index);
  228. if (!page)
  229. continue;
  230. if (PageWriteback(page)) {
  231. if (PageDirty(page))
  232. wait_on_page_writeback(page);
  233. else {
  234. unlock_page(page);
  235. page_cache_release(page);
  236. continue;
  237. }
  238. }
  239. err = write_one_page(page, 0);
  240. if (err)
  241. werr = err;
  242. page_cache_release(page);
  243. }
  244. }
  245. err = filemap_fdatawait(btree_inode->i_mapping);
  246. if (err)
  247. werr = err;
  248. return werr;
  249. }
  250. static int update_cowonly_root(struct btrfs_trans_handle *trans,
  251. struct btrfs_root *root)
  252. {
  253. int ret;
  254. u64 old_root_bytenr;
  255. struct btrfs_root *tree_root = root->fs_info->tree_root;
  256. btrfs_write_dirty_block_groups(trans, root);
  257. while(1) {
  258. old_root_bytenr = btrfs_root_bytenr(&root->root_item);
  259. if (old_root_bytenr == root->node->start)
  260. break;
  261. btrfs_set_root_bytenr(&root->root_item,
  262. root->node->start);
  263. btrfs_set_root_level(&root->root_item,
  264. btrfs_header_level(root->node));
  265. ret = btrfs_update_root(trans, tree_root,
  266. &root->root_key,
  267. &root->root_item);
  268. BUG_ON(ret);
  269. btrfs_write_dirty_block_groups(trans, root);
  270. }
  271. return 0;
  272. }
  273. int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
  274. struct btrfs_root *root)
  275. {
  276. struct btrfs_fs_info *fs_info = root->fs_info;
  277. struct list_head *next;
  278. while(!list_empty(&fs_info->dirty_cowonly_roots)) {
  279. next = fs_info->dirty_cowonly_roots.next;
  280. list_del_init(next);
  281. root = list_entry(next, struct btrfs_root, dirty_list);
  282. update_cowonly_root(trans, root);
  283. }
  284. return 0;
  285. }
  286. struct dirty_root {
  287. struct list_head list;
  288. struct btrfs_root *root;
  289. struct btrfs_root *latest_root;
  290. };
  291. int btrfs_add_dead_root(struct btrfs_root *root,
  292. struct btrfs_root *latest,
  293. struct list_head *dead_list)
  294. {
  295. struct dirty_root *dirty;
  296. dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
  297. if (!dirty)
  298. return -ENOMEM;
  299. dirty->root = root;
  300. dirty->latest_root = latest;
  301. list_add(&dirty->list, dead_list);
  302. return 0;
  303. }
  304. static noinline int add_dirty_roots(struct btrfs_trans_handle *trans,
  305. struct radix_tree_root *radix,
  306. struct list_head *list)
  307. {
  308. struct dirty_root *dirty;
  309. struct btrfs_root *gang[8];
  310. struct btrfs_root *root;
  311. int i;
  312. int ret;
  313. int err = 0;
  314. u32 refs;
  315. while(1) {
  316. ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
  317. ARRAY_SIZE(gang),
  318. BTRFS_ROOT_TRANS_TAG);
  319. if (ret == 0)
  320. break;
  321. for (i = 0; i < ret; i++) {
  322. root = gang[i];
  323. radix_tree_tag_clear(radix,
  324. (unsigned long)root->root_key.objectid,
  325. BTRFS_ROOT_TRANS_TAG);
  326. if (root->commit_root == root->node) {
  327. WARN_ON(root->node->start !=
  328. btrfs_root_bytenr(&root->root_item));
  329. free_extent_buffer(root->commit_root);
  330. root->commit_root = NULL;
  331. /* make sure to update the root on disk
  332. * so we get any updates to the block used
  333. * counts
  334. */
  335. err = btrfs_update_root(trans,
  336. root->fs_info->tree_root,
  337. &root->root_key,
  338. &root->root_item);
  339. continue;
  340. }
  341. dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
  342. BUG_ON(!dirty);
  343. dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS);
  344. BUG_ON(!dirty->root);
  345. memset(&root->root_item.drop_progress, 0,
  346. sizeof(struct btrfs_disk_key));
  347. root->root_item.drop_level = 0;
  348. memcpy(dirty->root, root, sizeof(*root));
  349. dirty->root->node = root->commit_root;
  350. dirty->latest_root = root;
  351. root->commit_root = NULL;
  352. root->root_key.offset = root->fs_info->generation;
  353. btrfs_set_root_bytenr(&root->root_item,
  354. root->node->start);
  355. btrfs_set_root_level(&root->root_item,
  356. btrfs_header_level(root->node));
  357. err = btrfs_insert_root(trans, root->fs_info->tree_root,
  358. &root->root_key,
  359. &root->root_item);
  360. if (err)
  361. break;
  362. refs = btrfs_root_refs(&dirty->root->root_item);
  363. btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
  364. err = btrfs_update_root(trans, root->fs_info->tree_root,
  365. &dirty->root->root_key,
  366. &dirty->root->root_item);
  367. BUG_ON(err);
  368. if (refs == 1) {
  369. list_add(&dirty->list, list);
  370. } else {
  371. WARN_ON(1);
  372. kfree(dirty->root);
  373. kfree(dirty);
  374. }
  375. }
  376. }
  377. return err;
  378. }
  379. int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
  380. {
  381. struct btrfs_fs_info *info = root->fs_info;
  382. int ret;
  383. struct btrfs_trans_handle *trans;
  384. unsigned long nr;
  385. smp_mb();
  386. if (root->defrag_running)
  387. return 0;
  388. trans = btrfs_start_transaction(root, 1);
  389. while (1) {
  390. root->defrag_running = 1;
  391. ret = btrfs_defrag_leaves(trans, root, cacheonly);
  392. nr = trans->blocks_used;
  393. btrfs_end_transaction(trans, root);
  394. btrfs_btree_balance_dirty(info->tree_root, nr);
  395. cond_resched();
  396. trans = btrfs_start_transaction(root, 1);
  397. if (root->fs_info->closing || ret != -EAGAIN)
  398. break;
  399. }
  400. root->defrag_running = 0;
  401. smp_mb();
  402. btrfs_end_transaction(trans, root);
  403. return 0;
  404. }
  405. static noinline int drop_dirty_roots(struct btrfs_root *tree_root,
  406. struct list_head *list)
  407. {
  408. struct dirty_root *dirty;
  409. struct btrfs_trans_handle *trans;
  410. unsigned long nr;
  411. u64 num_bytes;
  412. u64 bytes_used;
  413. int ret = 0;
  414. int err;
  415. while(!list_empty(list)) {
  416. struct btrfs_root *root;
  417. dirty = list_entry(list->next, struct dirty_root, list);
  418. list_del_init(&dirty->list);
  419. num_bytes = btrfs_root_used(&dirty->root->root_item);
  420. root = dirty->latest_root;
  421. atomic_inc(&root->fs_info->throttles);
  422. mutex_lock(&root->fs_info->drop_mutex);
  423. while(1) {
  424. trans = btrfs_start_transaction(tree_root, 1);
  425. ret = btrfs_drop_snapshot(trans, dirty->root);
  426. if (ret != -EAGAIN) {
  427. break;
  428. }
  429. err = btrfs_update_root(trans,
  430. tree_root,
  431. &dirty->root->root_key,
  432. &dirty->root->root_item);
  433. if (err)
  434. ret = err;
  435. nr = trans->blocks_used;
  436. ret = btrfs_end_transaction_throttle(trans, tree_root);
  437. BUG_ON(ret);
  438. mutex_unlock(&root->fs_info->drop_mutex);
  439. btrfs_btree_balance_dirty(tree_root, nr);
  440. cond_resched();
  441. mutex_lock(&root->fs_info->drop_mutex);
  442. }
  443. BUG_ON(ret);
  444. atomic_dec(&root->fs_info->throttles);
  445. mutex_lock(&root->fs_info->alloc_mutex);
  446. num_bytes -= btrfs_root_used(&dirty->root->root_item);
  447. bytes_used = btrfs_root_used(&root->root_item);
  448. if (num_bytes) {
  449. record_root_in_trans(root);
  450. btrfs_set_root_used(&root->root_item,
  451. bytes_used - num_bytes);
  452. }
  453. mutex_unlock(&root->fs_info->alloc_mutex);
  454. ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
  455. if (ret) {
  456. BUG();
  457. break;
  458. }
  459. mutex_unlock(&root->fs_info->drop_mutex);
  460. nr = trans->blocks_used;
  461. ret = btrfs_end_transaction(trans, tree_root);
  462. BUG_ON(ret);
  463. free_extent_buffer(dirty->root->node);
  464. kfree(dirty->root);
  465. kfree(dirty);
  466. btrfs_btree_balance_dirty(tree_root, nr);
  467. cond_resched();
  468. }
  469. return ret;
  470. }
  471. static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
  472. struct btrfs_fs_info *fs_info,
  473. struct btrfs_pending_snapshot *pending)
  474. {
  475. struct btrfs_key key;
  476. struct btrfs_root_item *new_root_item;
  477. struct btrfs_root *tree_root = fs_info->tree_root;
  478. struct btrfs_root *root = pending->root;
  479. struct extent_buffer *tmp;
  480. struct extent_buffer *old;
  481. int ret;
  482. int namelen;
  483. u64 objectid;
  484. new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
  485. if (!new_root_item) {
  486. ret = -ENOMEM;
  487. goto fail;
  488. }
  489. ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
  490. if (ret)
  491. goto fail;
  492. memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
  493. key.objectid = objectid;
  494. key.offset = 1;
  495. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  496. old = btrfs_lock_root_node(root);
  497. btrfs_cow_block(trans, root, old, NULL, 0, &old);
  498. btrfs_copy_root(trans, root, old, &tmp, objectid);
  499. btrfs_tree_unlock(old);
  500. free_extent_buffer(old);
  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. btrfs_tree_unlock(tmp);
  506. free_extent_buffer(tmp);
  507. if (ret)
  508. goto fail;
  509. /*
  510. * insert the directory item
  511. */
  512. key.offset = (u64)-1;
  513. namelen = strlen(pending->name);
  514. ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
  515. pending->name, namelen,
  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. /* Invalidate existing dcache entry for new snapshot. */
  524. btrfs_invalidate_dcache_root(root, pending->name, namelen);
  525. fail:
  526. kfree(new_root_item);
  527. return ret;
  528. }
  529. static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
  530. struct btrfs_fs_info *fs_info)
  531. {
  532. struct btrfs_pending_snapshot *pending;
  533. struct list_head *head = &trans->transaction->pending_snapshots;
  534. int ret;
  535. while(!list_empty(head)) {
  536. pending = list_entry(head->next,
  537. struct btrfs_pending_snapshot, list);
  538. ret = create_pending_snapshot(trans, fs_info, pending);
  539. BUG_ON(ret);
  540. list_del(&pending->list);
  541. kfree(pending->name);
  542. kfree(pending);
  543. }
  544. return 0;
  545. }
  546. int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
  547. struct btrfs_root *root)
  548. {
  549. unsigned long joined = 0;
  550. unsigned long timeout = 1;
  551. struct btrfs_transaction *cur_trans;
  552. struct btrfs_transaction *prev_trans = NULL;
  553. struct btrfs_root *chunk_root = root->fs_info->chunk_root;
  554. struct list_head dirty_fs_roots;
  555. struct extent_io_tree *pinned_copy;
  556. DEFINE_WAIT(wait);
  557. int ret;
  558. INIT_LIST_HEAD(&dirty_fs_roots);
  559. mutex_lock(&root->fs_info->trans_mutex);
  560. if (trans->transaction->in_commit) {
  561. cur_trans = trans->transaction;
  562. trans->transaction->use_count++;
  563. mutex_unlock(&root->fs_info->trans_mutex);
  564. btrfs_end_transaction(trans, root);
  565. ret = wait_for_commit(root, cur_trans);
  566. BUG_ON(ret);
  567. mutex_lock(&root->fs_info->trans_mutex);
  568. put_transaction(cur_trans);
  569. mutex_unlock(&root->fs_info->trans_mutex);
  570. return 0;
  571. }
  572. pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS);
  573. if (!pinned_copy)
  574. return -ENOMEM;
  575. extent_io_tree_init(pinned_copy,
  576. root->fs_info->btree_inode->i_mapping, GFP_NOFS);
  577. trans->transaction->in_commit = 1;
  578. trans->transaction->blocked = 1;
  579. cur_trans = trans->transaction;
  580. if (cur_trans->list.prev != &root->fs_info->trans_list) {
  581. prev_trans = list_entry(cur_trans->list.prev,
  582. struct btrfs_transaction, list);
  583. if (!prev_trans->commit_done) {
  584. prev_trans->use_count++;
  585. mutex_unlock(&root->fs_info->trans_mutex);
  586. wait_for_commit(root, prev_trans);
  587. mutex_lock(&root->fs_info->trans_mutex);
  588. put_transaction(prev_trans);
  589. }
  590. }
  591. do {
  592. joined = cur_trans->num_joined;
  593. WARN_ON(cur_trans != trans->transaction);
  594. prepare_to_wait(&cur_trans->writer_wait, &wait,
  595. TASK_UNINTERRUPTIBLE);
  596. if (cur_trans->num_writers > 1)
  597. timeout = MAX_SCHEDULE_TIMEOUT;
  598. else
  599. timeout = 1;
  600. mutex_unlock(&root->fs_info->trans_mutex);
  601. schedule_timeout(timeout);
  602. mutex_lock(&root->fs_info->trans_mutex);
  603. finish_wait(&cur_trans->writer_wait, &wait);
  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. btrfs_set_super_chunk_root(&root->fs_info->super_copy,
  625. chunk_root->node->start);
  626. btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
  627. btrfs_header_level(chunk_root->node));
  628. memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
  629. sizeof(root->fs_info->super_copy));
  630. btrfs_copy_pinned(root, pinned_copy);
  631. trans->transaction->blocked = 0;
  632. wake_up(&root->fs_info->transaction_throttle);
  633. wake_up(&root->fs_info->transaction_wait);
  634. mutex_unlock(&root->fs_info->trans_mutex);
  635. ret = btrfs_write_and_wait_transaction(trans, root);
  636. BUG_ON(ret);
  637. write_ctree_super(trans, root);
  638. btrfs_finish_extent_commit(trans, root, pinned_copy);
  639. mutex_lock(&root->fs_info->trans_mutex);
  640. kfree(pinned_copy);
  641. cur_trans->commit_done = 1;
  642. root->fs_info->last_trans_committed = cur_trans->transid;
  643. wake_up(&cur_trans->commit_wait);
  644. put_transaction(cur_trans);
  645. put_transaction(cur_trans);
  646. if (root->fs_info->closing)
  647. list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
  648. else
  649. list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
  650. mutex_unlock(&root->fs_info->trans_mutex);
  651. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  652. if (root->fs_info->closing) {
  653. drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
  654. }
  655. return ret;
  656. }
  657. int btrfs_clean_old_snapshots(struct btrfs_root *root)
  658. {
  659. struct list_head dirty_roots;
  660. INIT_LIST_HEAD(&dirty_roots);
  661. again:
  662. mutex_lock(&root->fs_info->trans_mutex);
  663. list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
  664. mutex_unlock(&root->fs_info->trans_mutex);
  665. if (!list_empty(&dirty_roots)) {
  666. drop_dirty_roots(root, &dirty_roots);
  667. goto again;
  668. }
  669. return 0;
  670. }