transaction.c 37 KB

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