transaction.c 50 KB

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