transaction.c 21 KB

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