transaction.c 21 KB

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