transaction.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380
  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/slab.h>
  20. #include <linux/sched.h>
  21. #include <linux/writeback.h>
  22. #include <linux/pagemap.h>
  23. #include <linux/blkdev.h>
  24. #include "ctree.h"
  25. #include "disk-io.h"
  26. #include "transaction.h"
  27. #include "locking.h"
  28. #include "tree-log.h"
  29. #include "inode-map.h"
  30. #define BTRFS_ROOT_TRANS_TAG 0
  31. static noinline void put_transaction(struct btrfs_transaction *transaction)
  32. {
  33. WARN_ON(atomic_read(&transaction->use_count) == 0);
  34. if (atomic_dec_and_test(&transaction->use_count)) {
  35. BUG_ON(!list_empty(&transaction->list));
  36. memset(transaction, 0, sizeof(*transaction));
  37. kmem_cache_free(btrfs_transaction_cachep, transaction);
  38. }
  39. }
  40. static noinline void switch_commit_root(struct btrfs_root *root)
  41. {
  42. free_extent_buffer(root->commit_root);
  43. root->commit_root = btrfs_root_node(root);
  44. }
  45. /*
  46. * either allocate a new transaction or hop into the existing one
  47. */
  48. static noinline int join_transaction(struct btrfs_root *root, int nofail)
  49. {
  50. struct btrfs_transaction *cur_trans;
  51. spin_lock(&root->fs_info->trans_lock);
  52. if (root->fs_info->trans_no_join) {
  53. if (!nofail) {
  54. spin_unlock(&root->fs_info->trans_lock);
  55. return -EBUSY;
  56. }
  57. }
  58. cur_trans = root->fs_info->running_transaction;
  59. if (cur_trans) {
  60. atomic_inc(&cur_trans->use_count);
  61. atomic_inc(&cur_trans->num_writers);
  62. cur_trans->num_joined++;
  63. spin_unlock(&root->fs_info->trans_lock);
  64. return 0;
  65. }
  66. spin_unlock(&root->fs_info->trans_lock);
  67. cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, GFP_NOFS);
  68. if (!cur_trans)
  69. return -ENOMEM;
  70. spin_lock(&root->fs_info->trans_lock);
  71. if (root->fs_info->running_transaction) {
  72. kmem_cache_free(btrfs_transaction_cachep, cur_trans);
  73. cur_trans = root->fs_info->running_transaction;
  74. atomic_inc(&cur_trans->use_count);
  75. atomic_inc(&cur_trans->num_writers);
  76. cur_trans->num_joined++;
  77. spin_unlock(&root->fs_info->trans_lock);
  78. return 0;
  79. }
  80. atomic_set(&cur_trans->num_writers, 1);
  81. cur_trans->num_joined = 0;
  82. init_waitqueue_head(&cur_trans->writer_wait);
  83. init_waitqueue_head(&cur_trans->commit_wait);
  84. cur_trans->in_commit = 0;
  85. cur_trans->blocked = 0;
  86. /*
  87. * One for this trans handle, one so it will live on until we
  88. * commit the transaction.
  89. */
  90. atomic_set(&cur_trans->use_count, 2);
  91. cur_trans->commit_done = 0;
  92. cur_trans->start_time = get_seconds();
  93. cur_trans->delayed_refs.root = RB_ROOT;
  94. cur_trans->delayed_refs.num_entries = 0;
  95. cur_trans->delayed_refs.num_heads_ready = 0;
  96. cur_trans->delayed_refs.num_heads = 0;
  97. cur_trans->delayed_refs.flushing = 0;
  98. cur_trans->delayed_refs.run_delayed_start = 0;
  99. spin_lock_init(&cur_trans->commit_lock);
  100. spin_lock_init(&cur_trans->delayed_refs.lock);
  101. INIT_LIST_HEAD(&cur_trans->pending_snapshots);
  102. list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
  103. extent_io_tree_init(&cur_trans->dirty_pages,
  104. root->fs_info->btree_inode->i_mapping);
  105. root->fs_info->generation++;
  106. cur_trans->transid = root->fs_info->generation;
  107. root->fs_info->running_transaction = cur_trans;
  108. spin_unlock(&root->fs_info->trans_lock);
  109. return 0;
  110. }
  111. /*
  112. * this does all the record keeping required to make sure that a reference
  113. * counted root is properly recorded in a given transaction. This is required
  114. * to make sure the old root from before we joined the transaction is deleted
  115. * when the transaction commits
  116. */
  117. int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
  118. struct btrfs_root *root)
  119. {
  120. if (root->ref_cows && root->last_trans < trans->transid) {
  121. WARN_ON(root == root->fs_info->extent_root);
  122. WARN_ON(root->commit_root != root->node);
  123. spin_lock(&root->fs_info->fs_roots_radix_lock);
  124. if (root->last_trans == trans->transid) {
  125. spin_unlock(&root->fs_info->fs_roots_radix_lock);
  126. return 0;
  127. }
  128. root->last_trans = trans->transid;
  129. radix_tree_tag_set(&root->fs_info->fs_roots_radix,
  130. (unsigned long)root->root_key.objectid,
  131. BTRFS_ROOT_TRANS_TAG);
  132. spin_unlock(&root->fs_info->fs_roots_radix_lock);
  133. btrfs_init_reloc_root(trans, root);
  134. }
  135. return 0;
  136. }
  137. /* wait for commit against the current transaction to become unblocked
  138. * when this is done, it is safe to start a new transaction, but the current
  139. * transaction might not be fully on disk.
  140. */
  141. static void wait_current_trans(struct btrfs_root *root)
  142. {
  143. struct btrfs_transaction *cur_trans;
  144. spin_lock(&root->fs_info->trans_lock);
  145. cur_trans = root->fs_info->running_transaction;
  146. if (cur_trans && cur_trans->blocked) {
  147. DEFINE_WAIT(wait);
  148. atomic_inc(&cur_trans->use_count);
  149. spin_unlock(&root->fs_info->trans_lock);
  150. while (1) {
  151. prepare_to_wait(&root->fs_info->transaction_wait, &wait,
  152. TASK_UNINTERRUPTIBLE);
  153. if (!cur_trans->blocked)
  154. break;
  155. schedule();
  156. }
  157. finish_wait(&root->fs_info->transaction_wait, &wait);
  158. put_transaction(cur_trans);
  159. } else {
  160. spin_unlock(&root->fs_info->trans_lock);
  161. }
  162. }
  163. enum btrfs_trans_type {
  164. TRANS_START,
  165. TRANS_JOIN,
  166. TRANS_USERSPACE,
  167. TRANS_JOIN_NOLOCK,
  168. };
  169. static int may_wait_transaction(struct btrfs_root *root, int type)
  170. {
  171. if (root->fs_info->log_root_recovering)
  172. return 0;
  173. if (type == TRANS_USERSPACE)
  174. return 1;
  175. if (type == TRANS_START &&
  176. !atomic_read(&root->fs_info->open_ioctl_trans))
  177. return 1;
  178. return 0;
  179. }
  180. static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
  181. u64 num_items, int type)
  182. {
  183. struct btrfs_trans_handle *h;
  184. struct btrfs_transaction *cur_trans;
  185. int retries = 0;
  186. int ret;
  187. if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR)
  188. return ERR_PTR(-EROFS);
  189. if (current->journal_info) {
  190. WARN_ON(type != TRANS_JOIN && type != TRANS_JOIN_NOLOCK);
  191. h = current->journal_info;
  192. h->use_count++;
  193. h->orig_rsv = h->block_rsv;
  194. h->block_rsv = NULL;
  195. goto got_it;
  196. }
  197. again:
  198. h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
  199. if (!h)
  200. return ERR_PTR(-ENOMEM);
  201. if (may_wait_transaction(root, type))
  202. wait_current_trans(root);
  203. do {
  204. ret = join_transaction(root, type == TRANS_JOIN_NOLOCK);
  205. if (ret == -EBUSY)
  206. wait_current_trans(root);
  207. } while (ret == -EBUSY);
  208. if (ret < 0) {
  209. kmem_cache_free(btrfs_trans_handle_cachep, h);
  210. return ERR_PTR(ret);
  211. }
  212. cur_trans = root->fs_info->running_transaction;
  213. h->transid = cur_trans->transid;
  214. h->transaction = cur_trans;
  215. h->blocks_used = 0;
  216. h->bytes_reserved = 0;
  217. h->delayed_ref_updates = 0;
  218. h->use_count = 1;
  219. h->block_rsv = NULL;
  220. h->orig_rsv = NULL;
  221. smp_mb();
  222. if (cur_trans->blocked && may_wait_transaction(root, type)) {
  223. btrfs_commit_transaction(h, root);
  224. goto again;
  225. }
  226. if (num_items > 0) {
  227. ret = btrfs_trans_reserve_metadata(h, root, num_items);
  228. if (ret == -EAGAIN && !retries) {
  229. retries++;
  230. btrfs_commit_transaction(h, root);
  231. goto again;
  232. } else if (ret == -EAGAIN) {
  233. /*
  234. * We have already retried and got EAGAIN, so really we
  235. * don't have space, so set ret to -ENOSPC.
  236. */
  237. ret = -ENOSPC;
  238. }
  239. if (ret < 0) {
  240. btrfs_end_transaction(h, root);
  241. return ERR_PTR(ret);
  242. }
  243. }
  244. got_it:
  245. btrfs_record_root_in_trans(h, root);
  246. if (!current->journal_info && type != TRANS_USERSPACE)
  247. current->journal_info = h;
  248. return h;
  249. }
  250. struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
  251. int num_items)
  252. {
  253. return start_transaction(root, num_items, TRANS_START);
  254. }
  255. struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root)
  256. {
  257. return start_transaction(root, 0, TRANS_JOIN);
  258. }
  259. struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root)
  260. {
  261. return start_transaction(root, 0, TRANS_JOIN_NOLOCK);
  262. }
  263. struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root)
  264. {
  265. return start_transaction(root, 0, TRANS_USERSPACE);
  266. }
  267. /* wait for a transaction commit to be fully complete */
  268. static noinline int wait_for_commit(struct btrfs_root *root,
  269. struct btrfs_transaction *commit)
  270. {
  271. DEFINE_WAIT(wait);
  272. while (!commit->commit_done) {
  273. prepare_to_wait(&commit->commit_wait, &wait,
  274. TASK_UNINTERRUPTIBLE);
  275. if (commit->commit_done)
  276. break;
  277. schedule();
  278. }
  279. finish_wait(&commit->commit_wait, &wait);
  280. return 0;
  281. }
  282. int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid)
  283. {
  284. struct btrfs_transaction *cur_trans = NULL, *t;
  285. int ret;
  286. ret = 0;
  287. if (transid) {
  288. if (transid <= root->fs_info->last_trans_committed)
  289. goto out;
  290. /* find specified transaction */
  291. spin_lock(&root->fs_info->trans_lock);
  292. list_for_each_entry(t, &root->fs_info->trans_list, list) {
  293. if (t->transid == transid) {
  294. cur_trans = t;
  295. atomic_inc(&cur_trans->use_count);
  296. break;
  297. }
  298. if (t->transid > transid)
  299. break;
  300. }
  301. spin_unlock(&root->fs_info->trans_lock);
  302. ret = -EINVAL;
  303. if (!cur_trans)
  304. goto out; /* bad transid */
  305. } else {
  306. /* find newest transaction that is committing | committed */
  307. spin_lock(&root->fs_info->trans_lock);
  308. list_for_each_entry_reverse(t, &root->fs_info->trans_list,
  309. list) {
  310. if (t->in_commit) {
  311. if (t->commit_done)
  312. goto out;
  313. cur_trans = t;
  314. atomic_inc(&cur_trans->use_count);
  315. break;
  316. }
  317. }
  318. spin_unlock(&root->fs_info->trans_lock);
  319. if (!cur_trans)
  320. goto out; /* nothing committing|committed */
  321. }
  322. wait_for_commit(root, cur_trans);
  323. put_transaction(cur_trans);
  324. ret = 0;
  325. out:
  326. return ret;
  327. }
  328. void btrfs_throttle(struct btrfs_root *root)
  329. {
  330. if (!atomic_read(&root->fs_info->open_ioctl_trans))
  331. wait_current_trans(root);
  332. }
  333. static int should_end_transaction(struct btrfs_trans_handle *trans,
  334. struct btrfs_root *root)
  335. {
  336. int ret;
  337. ret = btrfs_block_rsv_check(trans, root,
  338. &root->fs_info->global_block_rsv, 0, 5);
  339. return ret ? 1 : 0;
  340. }
  341. int btrfs_should_end_transaction(struct btrfs_trans_handle *trans,
  342. struct btrfs_root *root)
  343. {
  344. struct btrfs_transaction *cur_trans = trans->transaction;
  345. int updates;
  346. smp_mb();
  347. if (cur_trans->blocked || cur_trans->delayed_refs.flushing)
  348. return 1;
  349. updates = trans->delayed_ref_updates;
  350. trans->delayed_ref_updates = 0;
  351. if (updates)
  352. btrfs_run_delayed_refs(trans, root, updates);
  353. return should_end_transaction(trans, root);
  354. }
  355. static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
  356. struct btrfs_root *root, int throttle, int lock)
  357. {
  358. struct btrfs_transaction *cur_trans = trans->transaction;
  359. struct btrfs_fs_info *info = root->fs_info;
  360. int count = 0;
  361. if (--trans->use_count) {
  362. trans->block_rsv = trans->orig_rsv;
  363. return 0;
  364. }
  365. while (count < 4) {
  366. unsigned long cur = trans->delayed_ref_updates;
  367. trans->delayed_ref_updates = 0;
  368. if (cur &&
  369. trans->transaction->delayed_refs.num_heads_ready > 64) {
  370. trans->delayed_ref_updates = 0;
  371. /*
  372. * do a full flush if the transaction is trying
  373. * to close
  374. */
  375. if (trans->transaction->delayed_refs.flushing)
  376. cur = 0;
  377. btrfs_run_delayed_refs(trans, root, cur);
  378. } else {
  379. break;
  380. }
  381. count++;
  382. }
  383. btrfs_trans_release_metadata(trans, root);
  384. if (lock && !atomic_read(&root->fs_info->open_ioctl_trans) &&
  385. should_end_transaction(trans, root)) {
  386. trans->transaction->blocked = 1;
  387. smp_wmb();
  388. }
  389. if (lock && cur_trans->blocked && !cur_trans->in_commit) {
  390. if (throttle)
  391. return btrfs_commit_transaction(trans, root);
  392. else
  393. wake_up_process(info->transaction_kthread);
  394. }
  395. WARN_ON(cur_trans != info->running_transaction);
  396. WARN_ON(atomic_read(&cur_trans->num_writers) < 1);
  397. atomic_dec(&cur_trans->num_writers);
  398. smp_mb();
  399. if (waitqueue_active(&cur_trans->writer_wait))
  400. wake_up(&cur_trans->writer_wait);
  401. put_transaction(cur_trans);
  402. if (current->journal_info == trans)
  403. current->journal_info = NULL;
  404. memset(trans, 0, sizeof(*trans));
  405. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  406. if (throttle)
  407. btrfs_run_delayed_iputs(root);
  408. return 0;
  409. }
  410. int btrfs_end_transaction(struct btrfs_trans_handle *trans,
  411. struct btrfs_root *root)
  412. {
  413. int ret;
  414. ret = __btrfs_end_transaction(trans, root, 0, 1);
  415. if (ret)
  416. return ret;
  417. return 0;
  418. }
  419. int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
  420. struct btrfs_root *root)
  421. {
  422. int ret;
  423. ret = __btrfs_end_transaction(trans, root, 1, 1);
  424. if (ret)
  425. return ret;
  426. return 0;
  427. }
  428. int btrfs_end_transaction_nolock(struct btrfs_trans_handle *trans,
  429. struct btrfs_root *root)
  430. {
  431. int ret;
  432. ret = __btrfs_end_transaction(trans, root, 0, 0);
  433. if (ret)
  434. return ret;
  435. return 0;
  436. }
  437. int btrfs_end_transaction_dmeta(struct btrfs_trans_handle *trans,
  438. struct btrfs_root *root)
  439. {
  440. return __btrfs_end_transaction(trans, root, 1, 1);
  441. }
  442. /*
  443. * when btree blocks are allocated, they have some corresponding bits set for
  444. * them in one of two extent_io trees. This is used to make sure all of
  445. * those extents are sent to disk but does not wait on them
  446. */
  447. int btrfs_write_marked_extents(struct btrfs_root *root,
  448. struct extent_io_tree *dirty_pages, int mark)
  449. {
  450. int ret;
  451. int err = 0;
  452. int werr = 0;
  453. struct page *page;
  454. struct inode *btree_inode = root->fs_info->btree_inode;
  455. u64 start = 0;
  456. u64 end;
  457. unsigned long index;
  458. while (1) {
  459. ret = find_first_extent_bit(dirty_pages, start, &start, &end,
  460. mark);
  461. if (ret)
  462. break;
  463. while (start <= end) {
  464. cond_resched();
  465. index = start >> PAGE_CACHE_SHIFT;
  466. start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
  467. page = find_get_page(btree_inode->i_mapping, index);
  468. if (!page)
  469. continue;
  470. btree_lock_page_hook(page);
  471. if (!page->mapping) {
  472. unlock_page(page);
  473. page_cache_release(page);
  474. continue;
  475. }
  476. if (PageWriteback(page)) {
  477. if (PageDirty(page))
  478. wait_on_page_writeback(page);
  479. else {
  480. unlock_page(page);
  481. page_cache_release(page);
  482. continue;
  483. }
  484. }
  485. err = write_one_page(page, 0);
  486. if (err)
  487. werr = err;
  488. page_cache_release(page);
  489. }
  490. }
  491. if (err)
  492. werr = err;
  493. return werr;
  494. }
  495. /*
  496. * when btree blocks are allocated, they have some corresponding bits set for
  497. * them in one of two extent_io trees. This is used to make sure all of
  498. * those extents are on disk for transaction or log commit. We wait
  499. * on all the pages and clear them from the dirty pages state tree
  500. */
  501. int btrfs_wait_marked_extents(struct btrfs_root *root,
  502. struct extent_io_tree *dirty_pages, int mark)
  503. {
  504. int ret;
  505. int err = 0;
  506. int werr = 0;
  507. struct page *page;
  508. struct inode *btree_inode = root->fs_info->btree_inode;
  509. u64 start = 0;
  510. u64 end;
  511. unsigned long index;
  512. while (1) {
  513. ret = find_first_extent_bit(dirty_pages, start, &start, &end,
  514. mark);
  515. if (ret)
  516. break;
  517. clear_extent_bits(dirty_pages, start, end, mark, GFP_NOFS);
  518. while (start <= end) {
  519. index = start >> PAGE_CACHE_SHIFT;
  520. start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
  521. page = find_get_page(btree_inode->i_mapping, index);
  522. if (!page)
  523. continue;
  524. if (PageDirty(page)) {
  525. btree_lock_page_hook(page);
  526. wait_on_page_writeback(page);
  527. err = write_one_page(page, 0);
  528. if (err)
  529. werr = err;
  530. }
  531. wait_on_page_writeback(page);
  532. page_cache_release(page);
  533. cond_resched();
  534. }
  535. }
  536. if (err)
  537. werr = err;
  538. return werr;
  539. }
  540. /*
  541. * when btree blocks are allocated, they have some corresponding bits set for
  542. * them in one of two extent_io trees. This is used to make sure all of
  543. * those extents are on disk for transaction or log commit
  544. */
  545. int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
  546. struct extent_io_tree *dirty_pages, int mark)
  547. {
  548. int ret;
  549. int ret2;
  550. ret = btrfs_write_marked_extents(root, dirty_pages, mark);
  551. ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark);
  552. return ret || ret2;
  553. }
  554. int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
  555. struct btrfs_root *root)
  556. {
  557. if (!trans || !trans->transaction) {
  558. struct inode *btree_inode;
  559. btree_inode = root->fs_info->btree_inode;
  560. return filemap_write_and_wait(btree_inode->i_mapping);
  561. }
  562. return btrfs_write_and_wait_marked_extents(root,
  563. &trans->transaction->dirty_pages,
  564. EXTENT_DIRTY);
  565. }
  566. /*
  567. * this is used to update the root pointer in the tree of tree roots.
  568. *
  569. * But, in the case of the extent allocation tree, updating the root
  570. * pointer may allocate blocks which may change the root of the extent
  571. * allocation tree.
  572. *
  573. * So, this loops and repeats and makes sure the cowonly root didn't
  574. * change while the root pointer was being updated in the metadata.
  575. */
  576. static int update_cowonly_root(struct btrfs_trans_handle *trans,
  577. struct btrfs_root *root)
  578. {
  579. int ret;
  580. u64 old_root_bytenr;
  581. u64 old_root_used;
  582. struct btrfs_root *tree_root = root->fs_info->tree_root;
  583. old_root_used = btrfs_root_used(&root->root_item);
  584. btrfs_write_dirty_block_groups(trans, root);
  585. while (1) {
  586. old_root_bytenr = btrfs_root_bytenr(&root->root_item);
  587. if (old_root_bytenr == root->node->start &&
  588. old_root_used == btrfs_root_used(&root->root_item))
  589. break;
  590. btrfs_set_root_node(&root->root_item, root->node);
  591. ret = btrfs_update_root(trans, tree_root,
  592. &root->root_key,
  593. &root->root_item);
  594. BUG_ON(ret);
  595. old_root_used = btrfs_root_used(&root->root_item);
  596. ret = btrfs_write_dirty_block_groups(trans, root);
  597. BUG_ON(ret);
  598. }
  599. if (root != root->fs_info->extent_root)
  600. switch_commit_root(root);
  601. return 0;
  602. }
  603. /*
  604. * update all the cowonly tree roots on disk
  605. */
  606. static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans,
  607. struct btrfs_root *root)
  608. {
  609. struct btrfs_fs_info *fs_info = root->fs_info;
  610. struct list_head *next;
  611. struct extent_buffer *eb;
  612. int ret;
  613. ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
  614. BUG_ON(ret);
  615. eb = btrfs_lock_root_node(fs_info->tree_root);
  616. btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb);
  617. btrfs_tree_unlock(eb);
  618. free_extent_buffer(eb);
  619. ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
  620. BUG_ON(ret);
  621. while (!list_empty(&fs_info->dirty_cowonly_roots)) {
  622. next = fs_info->dirty_cowonly_roots.next;
  623. list_del_init(next);
  624. root = list_entry(next, struct btrfs_root, dirty_list);
  625. update_cowonly_root(trans, root);
  626. }
  627. down_write(&fs_info->extent_commit_sem);
  628. switch_commit_root(fs_info->extent_root);
  629. up_write(&fs_info->extent_commit_sem);
  630. return 0;
  631. }
  632. /*
  633. * dead roots are old snapshots that need to be deleted. This allocates
  634. * a dirty root struct and adds it into the list of dead roots that need to
  635. * be deleted
  636. */
  637. int btrfs_add_dead_root(struct btrfs_root *root)
  638. {
  639. spin_lock(&root->fs_info->trans_lock);
  640. list_add(&root->root_list, &root->fs_info->dead_roots);
  641. spin_unlock(&root->fs_info->trans_lock);
  642. return 0;
  643. }
  644. /*
  645. * update all the cowonly tree roots on disk
  646. */
  647. static noinline int commit_fs_roots(struct btrfs_trans_handle *trans,
  648. struct btrfs_root *root)
  649. {
  650. struct btrfs_root *gang[8];
  651. struct btrfs_fs_info *fs_info = root->fs_info;
  652. int i;
  653. int ret;
  654. int err = 0;
  655. spin_lock(&fs_info->fs_roots_radix_lock);
  656. while (1) {
  657. ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
  658. (void **)gang, 0,
  659. ARRAY_SIZE(gang),
  660. BTRFS_ROOT_TRANS_TAG);
  661. if (ret == 0)
  662. break;
  663. for (i = 0; i < ret; i++) {
  664. root = gang[i];
  665. radix_tree_tag_clear(&fs_info->fs_roots_radix,
  666. (unsigned long)root->root_key.objectid,
  667. BTRFS_ROOT_TRANS_TAG);
  668. spin_unlock(&fs_info->fs_roots_radix_lock);
  669. btrfs_free_log(trans, root);
  670. btrfs_update_reloc_root(trans, root);
  671. btrfs_orphan_commit_root(trans, root);
  672. btrfs_save_ino_cache(root, trans);
  673. if (root->commit_root != root->node) {
  674. mutex_lock(&root->fs_commit_mutex);
  675. switch_commit_root(root);
  676. btrfs_unpin_free_ino(root);
  677. mutex_unlock(&root->fs_commit_mutex);
  678. btrfs_set_root_node(&root->root_item,
  679. root->node);
  680. }
  681. err = btrfs_update_root(trans, fs_info->tree_root,
  682. &root->root_key,
  683. &root->root_item);
  684. spin_lock(&fs_info->fs_roots_radix_lock);
  685. if (err)
  686. break;
  687. }
  688. }
  689. spin_unlock(&fs_info->fs_roots_radix_lock);
  690. return err;
  691. }
  692. /*
  693. * defrag a given btree. If cacheonly == 1, this won't read from the disk,
  694. * otherwise every leaf in the btree is read and defragged.
  695. */
  696. int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
  697. {
  698. struct btrfs_fs_info *info = root->fs_info;
  699. struct btrfs_trans_handle *trans;
  700. int ret;
  701. unsigned long nr;
  702. if (xchg(&root->defrag_running, 1))
  703. return 0;
  704. while (1) {
  705. trans = btrfs_start_transaction(root, 0);
  706. if (IS_ERR(trans))
  707. return PTR_ERR(trans);
  708. ret = btrfs_defrag_leaves(trans, root, cacheonly);
  709. nr = trans->blocks_used;
  710. btrfs_end_transaction(trans, root);
  711. btrfs_btree_balance_dirty(info->tree_root, nr);
  712. cond_resched();
  713. if (btrfs_fs_closing(root->fs_info) || ret != -EAGAIN)
  714. break;
  715. }
  716. root->defrag_running = 0;
  717. return ret;
  718. }
  719. /*
  720. * new snapshots need to be created at a very specific time in the
  721. * transaction commit. This does the actual creation
  722. */
  723. static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
  724. struct btrfs_fs_info *fs_info,
  725. struct btrfs_pending_snapshot *pending)
  726. {
  727. struct btrfs_key key;
  728. struct btrfs_root_item *new_root_item;
  729. struct btrfs_root *tree_root = fs_info->tree_root;
  730. struct btrfs_root *root = pending->root;
  731. struct btrfs_root *parent_root;
  732. struct inode *parent_inode;
  733. struct dentry *parent;
  734. struct dentry *dentry;
  735. struct extent_buffer *tmp;
  736. struct extent_buffer *old;
  737. int ret;
  738. u64 to_reserve = 0;
  739. u64 index = 0;
  740. u64 objectid;
  741. u64 root_flags;
  742. new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
  743. if (!new_root_item) {
  744. pending->error = -ENOMEM;
  745. goto fail;
  746. }
  747. ret = btrfs_find_free_objectid(tree_root, &objectid);
  748. if (ret) {
  749. pending->error = ret;
  750. goto fail;
  751. }
  752. btrfs_reloc_pre_snapshot(trans, pending, &to_reserve);
  753. btrfs_orphan_pre_snapshot(trans, pending, &to_reserve);
  754. if (to_reserve > 0) {
  755. ret = btrfs_block_rsv_add(trans, root, &pending->block_rsv,
  756. to_reserve);
  757. if (ret) {
  758. pending->error = ret;
  759. goto fail;
  760. }
  761. }
  762. key.objectid = objectid;
  763. key.offset = (u64)-1;
  764. key.type = BTRFS_ROOT_ITEM_KEY;
  765. trans->block_rsv = &pending->block_rsv;
  766. dentry = pending->dentry;
  767. parent = dget_parent(dentry);
  768. parent_inode = parent->d_inode;
  769. parent_root = BTRFS_I(parent_inode)->root;
  770. btrfs_record_root_in_trans(trans, parent_root);
  771. /*
  772. * insert the directory item
  773. */
  774. ret = btrfs_set_inode_index(parent_inode, &index);
  775. BUG_ON(ret);
  776. ret = btrfs_insert_dir_item(trans, parent_root,
  777. dentry->d_name.name, dentry->d_name.len,
  778. parent_inode, &key,
  779. BTRFS_FT_DIR, index);
  780. BUG_ON(ret);
  781. btrfs_i_size_write(parent_inode, parent_inode->i_size +
  782. dentry->d_name.len * 2);
  783. ret = btrfs_update_inode(trans, parent_root, parent_inode);
  784. BUG_ON(ret);
  785. btrfs_record_root_in_trans(trans, root);
  786. btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
  787. memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
  788. btrfs_check_and_init_root_item(new_root_item);
  789. root_flags = btrfs_root_flags(new_root_item);
  790. if (pending->readonly)
  791. root_flags |= BTRFS_ROOT_SUBVOL_RDONLY;
  792. else
  793. root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY;
  794. btrfs_set_root_flags(new_root_item, root_flags);
  795. old = btrfs_lock_root_node(root);
  796. btrfs_cow_block(trans, root, old, NULL, 0, &old);
  797. btrfs_set_lock_blocking(old);
  798. btrfs_copy_root(trans, root, old, &tmp, objectid);
  799. btrfs_tree_unlock(old);
  800. free_extent_buffer(old);
  801. btrfs_set_root_node(new_root_item, tmp);
  802. /* record when the snapshot was created in key.offset */
  803. key.offset = trans->transid;
  804. ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
  805. btrfs_tree_unlock(tmp);
  806. free_extent_buffer(tmp);
  807. BUG_ON(ret);
  808. /*
  809. * insert root back/forward references
  810. */
  811. ret = btrfs_add_root_ref(trans, tree_root, objectid,
  812. parent_root->root_key.objectid,
  813. btrfs_ino(parent_inode), index,
  814. dentry->d_name.name, dentry->d_name.len);
  815. BUG_ON(ret);
  816. dput(parent);
  817. key.offset = (u64)-1;
  818. pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key);
  819. BUG_ON(IS_ERR(pending->snap));
  820. btrfs_reloc_post_snapshot(trans, pending);
  821. btrfs_orphan_post_snapshot(trans, pending);
  822. fail:
  823. kfree(new_root_item);
  824. btrfs_block_rsv_release(root, &pending->block_rsv, (u64)-1);
  825. return 0;
  826. }
  827. /*
  828. * create all the snapshots we've scheduled for creation
  829. */
  830. static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
  831. struct btrfs_fs_info *fs_info)
  832. {
  833. struct btrfs_pending_snapshot *pending;
  834. struct list_head *head = &trans->transaction->pending_snapshots;
  835. int ret;
  836. list_for_each_entry(pending, head, list) {
  837. /*
  838. * We must deal with the delayed items before creating
  839. * snapshots, or we will create a snapthot with inconsistent
  840. * information.
  841. */
  842. ret = btrfs_run_delayed_items(trans, fs_info->fs_root);
  843. BUG_ON(ret);
  844. ret = create_pending_snapshot(trans, fs_info, pending);
  845. BUG_ON(ret);
  846. }
  847. return 0;
  848. }
  849. static void update_super_roots(struct btrfs_root *root)
  850. {
  851. struct btrfs_root_item *root_item;
  852. struct btrfs_super_block *super;
  853. super = &root->fs_info->super_copy;
  854. root_item = &root->fs_info->chunk_root->root_item;
  855. super->chunk_root = root_item->bytenr;
  856. super->chunk_root_generation = root_item->generation;
  857. super->chunk_root_level = root_item->level;
  858. root_item = &root->fs_info->tree_root->root_item;
  859. super->root = root_item->bytenr;
  860. super->generation = root_item->generation;
  861. super->root_level = root_item->level;
  862. if (super->cache_generation != 0 || btrfs_test_opt(root, SPACE_CACHE))
  863. super->cache_generation = root_item->generation;
  864. }
  865. int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
  866. {
  867. int ret = 0;
  868. spin_lock(&info->trans_lock);
  869. if (info->running_transaction)
  870. ret = info->running_transaction->in_commit;
  871. spin_unlock(&info->trans_lock);
  872. return ret;
  873. }
  874. int btrfs_transaction_blocked(struct btrfs_fs_info *info)
  875. {
  876. int ret = 0;
  877. spin_lock(&info->trans_lock);
  878. if (info->running_transaction)
  879. ret = info->running_transaction->blocked;
  880. spin_unlock(&info->trans_lock);
  881. return ret;
  882. }
  883. /*
  884. * wait for the current transaction commit to start and block subsequent
  885. * transaction joins
  886. */
  887. static void wait_current_trans_commit_start(struct btrfs_root *root,
  888. struct btrfs_transaction *trans)
  889. {
  890. DEFINE_WAIT(wait);
  891. if (trans->in_commit)
  892. return;
  893. while (1) {
  894. prepare_to_wait(&root->fs_info->transaction_blocked_wait, &wait,
  895. TASK_UNINTERRUPTIBLE);
  896. if (trans->in_commit) {
  897. finish_wait(&root->fs_info->transaction_blocked_wait,
  898. &wait);
  899. break;
  900. }
  901. schedule();
  902. finish_wait(&root->fs_info->transaction_blocked_wait, &wait);
  903. }
  904. }
  905. /*
  906. * wait for the current transaction to start and then become unblocked.
  907. * caller holds ref.
  908. */
  909. static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root,
  910. struct btrfs_transaction *trans)
  911. {
  912. DEFINE_WAIT(wait);
  913. if (trans->commit_done || (trans->in_commit && !trans->blocked))
  914. return;
  915. while (1) {
  916. prepare_to_wait(&root->fs_info->transaction_wait, &wait,
  917. TASK_UNINTERRUPTIBLE);
  918. if (trans->commit_done ||
  919. (trans->in_commit && !trans->blocked)) {
  920. finish_wait(&root->fs_info->transaction_wait,
  921. &wait);
  922. break;
  923. }
  924. schedule();
  925. finish_wait(&root->fs_info->transaction_wait,
  926. &wait);
  927. }
  928. }
  929. /*
  930. * commit transactions asynchronously. once btrfs_commit_transaction_async
  931. * returns, any subsequent transaction will not be allowed to join.
  932. */
  933. struct btrfs_async_commit {
  934. struct btrfs_trans_handle *newtrans;
  935. struct btrfs_root *root;
  936. struct delayed_work work;
  937. };
  938. static void do_async_commit(struct work_struct *work)
  939. {
  940. struct btrfs_async_commit *ac =
  941. container_of(work, struct btrfs_async_commit, work.work);
  942. btrfs_commit_transaction(ac->newtrans, ac->root);
  943. kfree(ac);
  944. }
  945. int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
  946. struct btrfs_root *root,
  947. int wait_for_unblock)
  948. {
  949. struct btrfs_async_commit *ac;
  950. struct btrfs_transaction *cur_trans;
  951. ac = kmalloc(sizeof(*ac), GFP_NOFS);
  952. if (!ac)
  953. return -ENOMEM;
  954. INIT_DELAYED_WORK(&ac->work, do_async_commit);
  955. ac->root = root;
  956. ac->newtrans = btrfs_join_transaction(root);
  957. if (IS_ERR(ac->newtrans)) {
  958. int err = PTR_ERR(ac->newtrans);
  959. kfree(ac);
  960. return err;
  961. }
  962. /* take transaction reference */
  963. cur_trans = trans->transaction;
  964. atomic_inc(&cur_trans->use_count);
  965. btrfs_end_transaction(trans, root);
  966. schedule_delayed_work(&ac->work, 0);
  967. /* wait for transaction to start and unblock */
  968. if (wait_for_unblock)
  969. wait_current_trans_commit_start_and_unblock(root, cur_trans);
  970. else
  971. wait_current_trans_commit_start(root, cur_trans);
  972. put_transaction(cur_trans);
  973. return 0;
  974. }
  975. /*
  976. * btrfs_transaction state sequence:
  977. * in_commit = 0, blocked = 0 (initial)
  978. * in_commit = 1, blocked = 1
  979. * blocked = 0
  980. * commit_done = 1
  981. */
  982. int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
  983. struct btrfs_root *root)
  984. {
  985. unsigned long joined = 0;
  986. struct btrfs_transaction *cur_trans;
  987. struct btrfs_transaction *prev_trans = NULL;
  988. DEFINE_WAIT(wait);
  989. int ret;
  990. int should_grow = 0;
  991. unsigned long now = get_seconds();
  992. int flush_on_commit = btrfs_test_opt(root, FLUSHONCOMMIT);
  993. btrfs_run_ordered_operations(root, 0);
  994. /* make a pass through all the delayed refs we have so far
  995. * any runnings procs may add more while we are here
  996. */
  997. ret = btrfs_run_delayed_refs(trans, root, 0);
  998. BUG_ON(ret);
  999. btrfs_trans_release_metadata(trans, root);
  1000. cur_trans = trans->transaction;
  1001. /*
  1002. * set the flushing flag so procs in this transaction have to
  1003. * start sending their work down.
  1004. */
  1005. cur_trans->delayed_refs.flushing = 1;
  1006. ret = btrfs_run_delayed_refs(trans, root, 0);
  1007. BUG_ON(ret);
  1008. spin_lock(&cur_trans->commit_lock);
  1009. if (cur_trans->in_commit) {
  1010. spin_unlock(&cur_trans->commit_lock);
  1011. atomic_inc(&cur_trans->use_count);
  1012. btrfs_end_transaction(trans, root);
  1013. ret = wait_for_commit(root, cur_trans);
  1014. BUG_ON(ret);
  1015. put_transaction(cur_trans);
  1016. return 0;
  1017. }
  1018. trans->transaction->in_commit = 1;
  1019. trans->transaction->blocked = 1;
  1020. spin_unlock(&cur_trans->commit_lock);
  1021. wake_up(&root->fs_info->transaction_blocked_wait);
  1022. spin_lock(&root->fs_info->trans_lock);
  1023. if (cur_trans->list.prev != &root->fs_info->trans_list) {
  1024. prev_trans = list_entry(cur_trans->list.prev,
  1025. struct btrfs_transaction, list);
  1026. if (!prev_trans->commit_done) {
  1027. atomic_inc(&prev_trans->use_count);
  1028. spin_unlock(&root->fs_info->trans_lock);
  1029. wait_for_commit(root, prev_trans);
  1030. put_transaction(prev_trans);
  1031. } else {
  1032. spin_unlock(&root->fs_info->trans_lock);
  1033. }
  1034. } else {
  1035. spin_unlock(&root->fs_info->trans_lock);
  1036. }
  1037. if (now < cur_trans->start_time || now - cur_trans->start_time < 1)
  1038. should_grow = 1;
  1039. do {
  1040. int snap_pending = 0;
  1041. joined = cur_trans->num_joined;
  1042. if (!list_empty(&trans->transaction->pending_snapshots))
  1043. snap_pending = 1;
  1044. WARN_ON(cur_trans != trans->transaction);
  1045. if (flush_on_commit || snap_pending) {
  1046. btrfs_start_delalloc_inodes(root, 1);
  1047. ret = btrfs_wait_ordered_extents(root, 0, 1);
  1048. BUG_ON(ret);
  1049. }
  1050. ret = btrfs_run_delayed_items(trans, root);
  1051. BUG_ON(ret);
  1052. /*
  1053. * rename don't use btrfs_join_transaction, so, once we
  1054. * set the transaction to blocked above, we aren't going
  1055. * to get any new ordered operations. We can safely run
  1056. * it here and no for sure that nothing new will be added
  1057. * to the list
  1058. */
  1059. btrfs_run_ordered_operations(root, 1);
  1060. prepare_to_wait(&cur_trans->writer_wait, &wait,
  1061. TASK_UNINTERRUPTIBLE);
  1062. if (atomic_read(&cur_trans->num_writers) > 1)
  1063. schedule_timeout(MAX_SCHEDULE_TIMEOUT);
  1064. else if (should_grow)
  1065. schedule_timeout(1);
  1066. finish_wait(&cur_trans->writer_wait, &wait);
  1067. spin_lock(&root->fs_info->trans_lock);
  1068. root->fs_info->trans_no_join = 1;
  1069. spin_unlock(&root->fs_info->trans_lock);
  1070. } while (atomic_read(&cur_trans->num_writers) > 1 ||
  1071. (should_grow && cur_trans->num_joined != joined));
  1072. ret = create_pending_snapshots(trans, root->fs_info);
  1073. BUG_ON(ret);
  1074. ret = btrfs_run_delayed_items(trans, root);
  1075. BUG_ON(ret);
  1076. ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
  1077. BUG_ON(ret);
  1078. WARN_ON(cur_trans != trans->transaction);
  1079. btrfs_scrub_pause(root);
  1080. /* btrfs_commit_tree_roots is responsible for getting the
  1081. * various roots consistent with each other. Every pointer
  1082. * in the tree of tree roots has to point to the most up to date
  1083. * root for every subvolume and other tree. So, we have to keep
  1084. * the tree logging code from jumping in and changing any
  1085. * of the trees.
  1086. *
  1087. * At this point in the commit, there can't be any tree-log
  1088. * writers, but a little lower down we drop the trans mutex
  1089. * and let new people in. By holding the tree_log_mutex
  1090. * from now until after the super is written, we avoid races
  1091. * with the tree-log code.
  1092. */
  1093. mutex_lock(&root->fs_info->tree_log_mutex);
  1094. ret = commit_fs_roots(trans, root);
  1095. BUG_ON(ret);
  1096. /* commit_fs_roots gets rid of all the tree log roots, it is now
  1097. * safe to free the root of tree log roots
  1098. */
  1099. btrfs_free_log_root_tree(trans, root->fs_info);
  1100. ret = commit_cowonly_roots(trans, root);
  1101. BUG_ON(ret);
  1102. btrfs_prepare_extent_commit(trans, root);
  1103. cur_trans = root->fs_info->running_transaction;
  1104. btrfs_set_root_node(&root->fs_info->tree_root->root_item,
  1105. root->fs_info->tree_root->node);
  1106. switch_commit_root(root->fs_info->tree_root);
  1107. btrfs_set_root_node(&root->fs_info->chunk_root->root_item,
  1108. root->fs_info->chunk_root->node);
  1109. switch_commit_root(root->fs_info->chunk_root);
  1110. update_super_roots(root);
  1111. if (!root->fs_info->log_root_recovering) {
  1112. btrfs_set_super_log_root(&root->fs_info->super_copy, 0);
  1113. btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0);
  1114. }
  1115. memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
  1116. sizeof(root->fs_info->super_copy));
  1117. trans->transaction->blocked = 0;
  1118. spin_lock(&root->fs_info->trans_lock);
  1119. root->fs_info->running_transaction = NULL;
  1120. root->fs_info->trans_no_join = 0;
  1121. spin_unlock(&root->fs_info->trans_lock);
  1122. wake_up(&root->fs_info->transaction_wait);
  1123. ret = btrfs_write_and_wait_transaction(trans, root);
  1124. BUG_ON(ret);
  1125. write_ctree_super(trans, root, 0);
  1126. /*
  1127. * the super is written, we can safely allow the tree-loggers
  1128. * to go about their business
  1129. */
  1130. mutex_unlock(&root->fs_info->tree_log_mutex);
  1131. btrfs_finish_extent_commit(trans, root);
  1132. cur_trans->commit_done = 1;
  1133. root->fs_info->last_trans_committed = cur_trans->transid;
  1134. wake_up(&cur_trans->commit_wait);
  1135. spin_lock(&root->fs_info->trans_lock);
  1136. list_del_init(&cur_trans->list);
  1137. spin_unlock(&root->fs_info->trans_lock);
  1138. put_transaction(cur_trans);
  1139. put_transaction(cur_trans);
  1140. trace_btrfs_transaction_commit(root);
  1141. btrfs_scrub_continue(root);
  1142. if (current->journal_info == trans)
  1143. current->journal_info = NULL;
  1144. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  1145. if (current != root->fs_info->transaction_kthread)
  1146. btrfs_run_delayed_iputs(root);
  1147. return ret;
  1148. }
  1149. /*
  1150. * interface function to delete all the snapshots we have scheduled for deletion
  1151. */
  1152. int btrfs_clean_old_snapshots(struct btrfs_root *root)
  1153. {
  1154. LIST_HEAD(list);
  1155. struct btrfs_fs_info *fs_info = root->fs_info;
  1156. spin_lock(&fs_info->trans_lock);
  1157. list_splice_init(&fs_info->dead_roots, &list);
  1158. spin_unlock(&fs_info->trans_lock);
  1159. while (!list_empty(&list)) {
  1160. root = list_entry(list.next, struct btrfs_root, root_list);
  1161. list_del(&root->root_list);
  1162. btrfs_kill_all_delayed_nodes(root);
  1163. if (btrfs_header_backref_rev(root->node) <
  1164. BTRFS_MIXED_BACKREF_REV)
  1165. btrfs_drop_snapshot(root, NULL, 0);
  1166. else
  1167. btrfs_drop_snapshot(root, NULL, 1);
  1168. }
  1169. return 0;
  1170. }