transaction.c 41 KB

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