transaction.c 40 KB

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