transaction.c 36 KB

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