transaction.c 37 KB

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