transaction.c 38 KB

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