transaction.c 37 KB

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