transaction.c 48 KB

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