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