transaction.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  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. spin_lock_init(&dirty->root->node_lock);
  352. mutex_init(&dirty->root->objectid_mutex);
  353. root->commit_root = NULL;
  354. root->root_key.offset = root->fs_info->generation;
  355. btrfs_set_root_bytenr(&root->root_item,
  356. root->node->start);
  357. btrfs_set_root_level(&root->root_item,
  358. btrfs_header_level(root->node));
  359. err = btrfs_insert_root(trans, root->fs_info->tree_root,
  360. &root->root_key,
  361. &root->root_item);
  362. if (err)
  363. break;
  364. refs = btrfs_root_refs(&dirty->root->root_item);
  365. btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
  366. err = btrfs_update_root(trans, root->fs_info->tree_root,
  367. &dirty->root->root_key,
  368. &dirty->root->root_item);
  369. BUG_ON(err);
  370. if (refs == 1) {
  371. list_add(&dirty->list, list);
  372. } else {
  373. WARN_ON(1);
  374. kfree(dirty->root);
  375. kfree(dirty);
  376. }
  377. }
  378. }
  379. return err;
  380. }
  381. int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
  382. {
  383. struct btrfs_fs_info *info = root->fs_info;
  384. int ret;
  385. struct btrfs_trans_handle *trans;
  386. unsigned long nr;
  387. smp_mb();
  388. if (root->defrag_running)
  389. return 0;
  390. trans = btrfs_start_transaction(root, 1);
  391. while (1) {
  392. root->defrag_running = 1;
  393. ret = btrfs_defrag_leaves(trans, root, cacheonly);
  394. nr = trans->blocks_used;
  395. btrfs_end_transaction(trans, root);
  396. btrfs_btree_balance_dirty(info->tree_root, nr);
  397. cond_resched();
  398. trans = btrfs_start_transaction(root, 1);
  399. if (root->fs_info->closing || ret != -EAGAIN)
  400. break;
  401. }
  402. root->defrag_running = 0;
  403. smp_mb();
  404. btrfs_end_transaction(trans, root);
  405. return 0;
  406. }
  407. static noinline int drop_dirty_roots(struct btrfs_root *tree_root,
  408. struct list_head *list)
  409. {
  410. struct dirty_root *dirty;
  411. struct btrfs_trans_handle *trans;
  412. unsigned long nr;
  413. u64 num_bytes;
  414. u64 bytes_used;
  415. int ret = 0;
  416. int err;
  417. while(!list_empty(list)) {
  418. struct btrfs_root *root;
  419. dirty = list_entry(list->next, struct dirty_root, list);
  420. list_del_init(&dirty->list);
  421. num_bytes = btrfs_root_used(&dirty->root->root_item);
  422. root = dirty->latest_root;
  423. atomic_inc(&root->fs_info->throttles);
  424. mutex_lock(&root->fs_info->drop_mutex);
  425. while(1) {
  426. trans = btrfs_start_transaction(tree_root, 1);
  427. ret = btrfs_drop_snapshot(trans, dirty->root);
  428. if (ret != -EAGAIN) {
  429. break;
  430. }
  431. err = btrfs_update_root(trans,
  432. tree_root,
  433. &dirty->root->root_key,
  434. &dirty->root->root_item);
  435. if (err)
  436. ret = err;
  437. nr = trans->blocks_used;
  438. ret = btrfs_end_transaction_throttle(trans, tree_root);
  439. BUG_ON(ret);
  440. mutex_unlock(&root->fs_info->drop_mutex);
  441. btrfs_btree_balance_dirty(tree_root, nr);
  442. cond_resched();
  443. mutex_lock(&root->fs_info->drop_mutex);
  444. }
  445. BUG_ON(ret);
  446. atomic_dec(&root->fs_info->throttles);
  447. mutex_lock(&root->fs_info->alloc_mutex);
  448. num_bytes -= btrfs_root_used(&dirty->root->root_item);
  449. bytes_used = btrfs_root_used(&root->root_item);
  450. if (num_bytes) {
  451. record_root_in_trans(root);
  452. btrfs_set_root_used(&root->root_item,
  453. bytes_used - num_bytes);
  454. }
  455. mutex_unlock(&root->fs_info->alloc_mutex);
  456. ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
  457. if (ret) {
  458. BUG();
  459. break;
  460. }
  461. mutex_unlock(&root->fs_info->drop_mutex);
  462. nr = trans->blocks_used;
  463. ret = btrfs_end_transaction(trans, tree_root);
  464. BUG_ON(ret);
  465. free_extent_buffer(dirty->root->node);
  466. kfree(dirty->root);
  467. kfree(dirty);
  468. btrfs_btree_balance_dirty(tree_root, nr);
  469. cond_resched();
  470. }
  471. return ret;
  472. }
  473. static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
  474. struct btrfs_fs_info *fs_info,
  475. struct btrfs_pending_snapshot *pending)
  476. {
  477. struct btrfs_key key;
  478. struct btrfs_root_item *new_root_item;
  479. struct btrfs_root *tree_root = fs_info->tree_root;
  480. struct btrfs_root *root = pending->root;
  481. struct extent_buffer *tmp;
  482. struct extent_buffer *old;
  483. int ret;
  484. int namelen;
  485. u64 objectid;
  486. new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
  487. if (!new_root_item) {
  488. ret = -ENOMEM;
  489. goto fail;
  490. }
  491. ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
  492. if (ret)
  493. goto fail;
  494. memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
  495. key.objectid = objectid;
  496. key.offset = 1;
  497. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  498. old = btrfs_lock_root_node(root);
  499. btrfs_cow_block(trans, root, old, NULL, 0, &old);
  500. btrfs_copy_root(trans, root, old, &tmp, objectid);
  501. btrfs_tree_unlock(old);
  502. free_extent_buffer(old);
  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. btrfs_tree_unlock(tmp);
  508. free_extent_buffer(tmp);
  509. if (ret)
  510. goto fail;
  511. /*
  512. * insert the directory item
  513. */
  514. key.offset = (u64)-1;
  515. namelen = strlen(pending->name);
  516. ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
  517. pending->name, namelen,
  518. root->fs_info->sb->s_root->d_inode->i_ino,
  519. &key, BTRFS_FT_DIR, 0);
  520. if (ret)
  521. goto fail;
  522. ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
  523. pending->name, strlen(pending->name), objectid,
  524. root->fs_info->sb->s_root->d_inode->i_ino, 0);
  525. /* Invalidate existing dcache entry for new snapshot. */
  526. btrfs_invalidate_dcache_root(root, pending->name, namelen);
  527. fail:
  528. kfree(new_root_item);
  529. return ret;
  530. }
  531. static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
  532. struct btrfs_fs_info *fs_info)
  533. {
  534. struct btrfs_pending_snapshot *pending;
  535. struct list_head *head = &trans->transaction->pending_snapshots;
  536. int ret;
  537. while(!list_empty(head)) {
  538. pending = list_entry(head->next,
  539. struct btrfs_pending_snapshot, list);
  540. ret = create_pending_snapshot(trans, fs_info, pending);
  541. BUG_ON(ret);
  542. list_del(&pending->list);
  543. kfree(pending->name);
  544. kfree(pending);
  545. }
  546. return 0;
  547. }
  548. int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
  549. struct btrfs_root *root)
  550. {
  551. unsigned long joined = 0;
  552. unsigned long timeout = 1;
  553. struct btrfs_transaction *cur_trans;
  554. struct btrfs_transaction *prev_trans = NULL;
  555. struct btrfs_root *chunk_root = root->fs_info->chunk_root;
  556. struct list_head dirty_fs_roots;
  557. struct extent_io_tree *pinned_copy;
  558. DEFINE_WAIT(wait);
  559. int ret;
  560. INIT_LIST_HEAD(&dirty_fs_roots);
  561. mutex_lock(&root->fs_info->trans_mutex);
  562. if (trans->transaction->in_commit) {
  563. cur_trans = trans->transaction;
  564. trans->transaction->use_count++;
  565. mutex_unlock(&root->fs_info->trans_mutex);
  566. btrfs_end_transaction(trans, root);
  567. ret = wait_for_commit(root, cur_trans);
  568. BUG_ON(ret);
  569. mutex_lock(&root->fs_info->trans_mutex);
  570. put_transaction(cur_trans);
  571. mutex_unlock(&root->fs_info->trans_mutex);
  572. return 0;
  573. }
  574. pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS);
  575. if (!pinned_copy)
  576. return -ENOMEM;
  577. extent_io_tree_init(pinned_copy,
  578. root->fs_info->btree_inode->i_mapping, GFP_NOFS);
  579. trans->transaction->in_commit = 1;
  580. trans->transaction->blocked = 1;
  581. cur_trans = trans->transaction;
  582. if (cur_trans->list.prev != &root->fs_info->trans_list) {
  583. prev_trans = list_entry(cur_trans->list.prev,
  584. struct btrfs_transaction, list);
  585. if (!prev_trans->commit_done) {
  586. prev_trans->use_count++;
  587. mutex_unlock(&root->fs_info->trans_mutex);
  588. wait_for_commit(root, prev_trans);
  589. mutex_lock(&root->fs_info->trans_mutex);
  590. put_transaction(prev_trans);
  591. }
  592. }
  593. do {
  594. joined = cur_trans->num_joined;
  595. WARN_ON(cur_trans != trans->transaction);
  596. prepare_to_wait(&cur_trans->writer_wait, &wait,
  597. TASK_UNINTERRUPTIBLE);
  598. if (cur_trans->num_writers > 1)
  599. timeout = MAX_SCHEDULE_TIMEOUT;
  600. else
  601. timeout = 1;
  602. mutex_unlock(&root->fs_info->trans_mutex);
  603. schedule_timeout(timeout);
  604. mutex_lock(&root->fs_info->trans_mutex);
  605. finish_wait(&cur_trans->writer_wait, &wait);
  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. spin_lock(&root->fs_info->new_trans_lock);
  618. root->fs_info->running_transaction = NULL;
  619. spin_unlock(&root->fs_info->new_trans_lock);
  620. btrfs_set_super_generation(&root->fs_info->super_copy,
  621. cur_trans->transid);
  622. btrfs_set_super_root(&root->fs_info->super_copy,
  623. root->fs_info->tree_root->node->start);
  624. btrfs_set_super_root_level(&root->fs_info->super_copy,
  625. btrfs_header_level(root->fs_info->tree_root->node));
  626. btrfs_set_super_chunk_root(&root->fs_info->super_copy,
  627. chunk_root->node->start);
  628. btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
  629. btrfs_header_level(chunk_root->node));
  630. memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
  631. sizeof(root->fs_info->super_copy));
  632. btrfs_copy_pinned(root, pinned_copy);
  633. trans->transaction->blocked = 0;
  634. wake_up(&root->fs_info->transaction_throttle);
  635. wake_up(&root->fs_info->transaction_wait);
  636. mutex_unlock(&root->fs_info->trans_mutex);
  637. ret = btrfs_write_and_wait_transaction(trans, root);
  638. BUG_ON(ret);
  639. write_ctree_super(trans, root);
  640. btrfs_finish_extent_commit(trans, root, pinned_copy);
  641. mutex_lock(&root->fs_info->trans_mutex);
  642. kfree(pinned_copy);
  643. cur_trans->commit_done = 1;
  644. root->fs_info->last_trans_committed = cur_trans->transid;
  645. wake_up(&cur_trans->commit_wait);
  646. put_transaction(cur_trans);
  647. put_transaction(cur_trans);
  648. if (root->fs_info->closing)
  649. list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
  650. else
  651. list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
  652. mutex_unlock(&root->fs_info->trans_mutex);
  653. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  654. if (root->fs_info->closing) {
  655. drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
  656. }
  657. return ret;
  658. }
  659. int btrfs_clean_old_snapshots(struct btrfs_root *root)
  660. {
  661. struct list_head dirty_roots;
  662. INIT_LIST_HEAD(&dirty_roots);
  663. again:
  664. mutex_lock(&root->fs_info->trans_mutex);
  665. list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
  666. mutex_unlock(&root->fs_info->trans_mutex);
  667. if (!list_empty(&dirty_roots)) {
  668. drop_dirty_roots(root, &dirty_roots);
  669. goto again;
  670. }
  671. return 0;
  672. }