transaction.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904
  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. if (trans->qgroup_reserved) {
  553. /*
  554. * the same root has to be passed here between start_transaction
  555. * and end_transaction. Subvolume quota depends on this.
  556. */
  557. btrfs_qgroup_free(trans->root, trans->qgroup_reserved);
  558. trans->qgroup_reserved = 0;
  559. }
  560. if (!list_empty(&trans->new_bgs))
  561. btrfs_create_pending_block_groups(trans, root);
  562. while (count < 1) {
  563. unsigned long cur = trans->delayed_ref_updates;
  564. trans->delayed_ref_updates = 0;
  565. if (cur &&
  566. trans->transaction->delayed_refs.num_heads_ready > 64) {
  567. trans->delayed_ref_updates = 0;
  568. btrfs_run_delayed_refs(trans, root, cur);
  569. } else {
  570. break;
  571. }
  572. count++;
  573. }
  574. btrfs_trans_release_metadata(trans, root);
  575. trans->block_rsv = NULL;
  576. if (!list_empty(&trans->new_bgs))
  577. btrfs_create_pending_block_groups(trans, root);
  578. if (lock && !atomic_read(&root->fs_info->open_ioctl_trans) &&
  579. should_end_transaction(trans, root)) {
  580. trans->transaction->blocked = 1;
  581. smp_wmb();
  582. }
  583. if (lock && cur_trans->blocked && !cur_trans->in_commit) {
  584. if (throttle) {
  585. /*
  586. * We may race with somebody else here so end up having
  587. * to call end_transaction on ourselves again, so inc
  588. * our use_count.
  589. */
  590. trans->use_count++;
  591. return btrfs_commit_transaction(trans, root);
  592. } else {
  593. wake_up_process(info->transaction_kthread);
  594. }
  595. }
  596. if (trans->type < TRANS_JOIN_NOLOCK)
  597. sb_end_intwrite(root->fs_info->sb);
  598. WARN_ON(cur_trans != info->running_transaction);
  599. WARN_ON(atomic_read(&cur_trans->num_writers) < 1);
  600. atomic_dec(&cur_trans->num_writers);
  601. smp_mb();
  602. if (waitqueue_active(&cur_trans->writer_wait))
  603. wake_up(&cur_trans->writer_wait);
  604. put_transaction(cur_trans);
  605. if (current->journal_info == trans)
  606. current->journal_info = NULL;
  607. if (throttle)
  608. btrfs_run_delayed_iputs(root);
  609. if (trans->aborted ||
  610. test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
  611. err = -EIO;
  612. assert_qgroups_uptodate(trans);
  613. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  614. return err;
  615. }
  616. int btrfs_end_transaction(struct btrfs_trans_handle *trans,
  617. struct btrfs_root *root)
  618. {
  619. int ret;
  620. ret = __btrfs_end_transaction(trans, root, 0);
  621. if (ret)
  622. return ret;
  623. return 0;
  624. }
  625. int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
  626. struct btrfs_root *root)
  627. {
  628. int ret;
  629. ret = __btrfs_end_transaction(trans, root, 1);
  630. if (ret)
  631. return ret;
  632. return 0;
  633. }
  634. int btrfs_end_transaction_dmeta(struct btrfs_trans_handle *trans,
  635. struct btrfs_root *root)
  636. {
  637. return __btrfs_end_transaction(trans, root, 1);
  638. }
  639. /*
  640. * when btree blocks are allocated, they have some corresponding bits set for
  641. * them in one of two extent_io trees. This is used to make sure all of
  642. * those extents are sent to disk but does not wait on them
  643. */
  644. int btrfs_write_marked_extents(struct btrfs_root *root,
  645. struct extent_io_tree *dirty_pages, int mark)
  646. {
  647. int err = 0;
  648. int werr = 0;
  649. struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
  650. struct extent_state *cached_state = NULL;
  651. u64 start = 0;
  652. u64 end;
  653. struct blk_plug plug;
  654. blk_start_plug(&plug);
  655. while (!find_first_extent_bit(dirty_pages, start, &start, &end,
  656. mark, &cached_state)) {
  657. convert_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT,
  658. mark, &cached_state, GFP_NOFS);
  659. cached_state = NULL;
  660. err = filemap_fdatawrite_range(mapping, start, end);
  661. if (err)
  662. werr = err;
  663. cond_resched();
  664. start = end + 1;
  665. }
  666. if (err)
  667. werr = err;
  668. blk_finish_plug(&plug);
  669. return werr;
  670. }
  671. /*
  672. * when btree blocks are allocated, they have some corresponding bits set for
  673. * them in one of two extent_io trees. This is used to make sure all of
  674. * those extents are on disk for transaction or log commit. We wait
  675. * on all the pages and clear them from the dirty pages state tree
  676. */
  677. int btrfs_wait_marked_extents(struct btrfs_root *root,
  678. struct extent_io_tree *dirty_pages, int mark)
  679. {
  680. int err = 0;
  681. int werr = 0;
  682. struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
  683. struct extent_state *cached_state = NULL;
  684. u64 start = 0;
  685. u64 end;
  686. while (!find_first_extent_bit(dirty_pages, start, &start, &end,
  687. EXTENT_NEED_WAIT, &cached_state)) {
  688. clear_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT,
  689. 0, 0, &cached_state, GFP_NOFS);
  690. err = filemap_fdatawait_range(mapping, start, end);
  691. if (err)
  692. werr = err;
  693. cond_resched();
  694. start = end + 1;
  695. }
  696. if (err)
  697. werr = err;
  698. return werr;
  699. }
  700. /*
  701. * when btree blocks are allocated, they have some corresponding bits set for
  702. * them in one of two extent_io trees. This is used to make sure all of
  703. * those extents are on disk for transaction or log commit
  704. */
  705. int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
  706. struct extent_io_tree *dirty_pages, int mark)
  707. {
  708. int ret;
  709. int ret2;
  710. ret = btrfs_write_marked_extents(root, dirty_pages, mark);
  711. ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark);
  712. if (ret)
  713. return ret;
  714. if (ret2)
  715. return ret2;
  716. return 0;
  717. }
  718. int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
  719. struct btrfs_root *root)
  720. {
  721. if (!trans || !trans->transaction) {
  722. struct inode *btree_inode;
  723. btree_inode = root->fs_info->btree_inode;
  724. return filemap_write_and_wait(btree_inode->i_mapping);
  725. }
  726. return btrfs_write_and_wait_marked_extents(root,
  727. &trans->transaction->dirty_pages,
  728. EXTENT_DIRTY);
  729. }
  730. /*
  731. * this is used to update the root pointer in the tree of tree roots.
  732. *
  733. * But, in the case of the extent allocation tree, updating the root
  734. * pointer may allocate blocks which may change the root of the extent
  735. * allocation tree.
  736. *
  737. * So, this loops and repeats and makes sure the cowonly root didn't
  738. * change while the root pointer was being updated in the metadata.
  739. */
  740. static int update_cowonly_root(struct btrfs_trans_handle *trans,
  741. struct btrfs_root *root)
  742. {
  743. int ret;
  744. u64 old_root_bytenr;
  745. u64 old_root_used;
  746. struct btrfs_root *tree_root = root->fs_info->tree_root;
  747. old_root_used = btrfs_root_used(&root->root_item);
  748. btrfs_write_dirty_block_groups(trans, root);
  749. while (1) {
  750. old_root_bytenr = btrfs_root_bytenr(&root->root_item);
  751. if (old_root_bytenr == root->node->start &&
  752. old_root_used == btrfs_root_used(&root->root_item))
  753. break;
  754. btrfs_set_root_node(&root->root_item, root->node);
  755. ret = btrfs_update_root(trans, tree_root,
  756. &root->root_key,
  757. &root->root_item);
  758. if (ret)
  759. return ret;
  760. old_root_used = btrfs_root_used(&root->root_item);
  761. ret = btrfs_write_dirty_block_groups(trans, root);
  762. if (ret)
  763. return ret;
  764. }
  765. if (root != root->fs_info->extent_root)
  766. switch_commit_root(root);
  767. return 0;
  768. }
  769. /*
  770. * update all the cowonly tree roots on disk
  771. *
  772. * The error handling in this function may not be obvious. Any of the
  773. * failures will cause the file system to go offline. We still need
  774. * to clean up the delayed refs.
  775. */
  776. static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans,
  777. struct btrfs_root *root)
  778. {
  779. struct btrfs_fs_info *fs_info = root->fs_info;
  780. struct list_head *next;
  781. struct extent_buffer *eb;
  782. int ret;
  783. ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
  784. if (ret)
  785. return ret;
  786. eb = btrfs_lock_root_node(fs_info->tree_root);
  787. ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL,
  788. 0, &eb);
  789. btrfs_tree_unlock(eb);
  790. free_extent_buffer(eb);
  791. if (ret)
  792. return ret;
  793. ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
  794. if (ret)
  795. return ret;
  796. ret = btrfs_run_dev_stats(trans, root->fs_info);
  797. WARN_ON(ret);
  798. ret = btrfs_run_dev_replace(trans, root->fs_info);
  799. WARN_ON(ret);
  800. ret = btrfs_run_qgroups(trans, root->fs_info);
  801. BUG_ON(ret);
  802. /* run_qgroups might have added some more refs */
  803. ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
  804. BUG_ON(ret);
  805. while (!list_empty(&fs_info->dirty_cowonly_roots)) {
  806. next = fs_info->dirty_cowonly_roots.next;
  807. list_del_init(next);
  808. root = list_entry(next, struct btrfs_root, dirty_list);
  809. ret = update_cowonly_root(trans, root);
  810. if (ret)
  811. return ret;
  812. }
  813. down_write(&fs_info->extent_commit_sem);
  814. switch_commit_root(fs_info->extent_root);
  815. up_write(&fs_info->extent_commit_sem);
  816. btrfs_after_dev_replace_commit(fs_info);
  817. return 0;
  818. }
  819. /*
  820. * dead roots are old snapshots that need to be deleted. This allocates
  821. * a dirty root struct and adds it into the list of dead roots that need to
  822. * be deleted
  823. */
  824. int btrfs_add_dead_root(struct btrfs_root *root)
  825. {
  826. spin_lock(&root->fs_info->trans_lock);
  827. list_add(&root->root_list, &root->fs_info->dead_roots);
  828. spin_unlock(&root->fs_info->trans_lock);
  829. return 0;
  830. }
  831. /*
  832. * update all the cowonly tree roots on disk
  833. */
  834. static noinline int commit_fs_roots(struct btrfs_trans_handle *trans,
  835. struct btrfs_root *root)
  836. {
  837. struct btrfs_root *gang[8];
  838. struct btrfs_fs_info *fs_info = root->fs_info;
  839. int i;
  840. int ret;
  841. int err = 0;
  842. spin_lock(&fs_info->fs_roots_radix_lock);
  843. while (1) {
  844. ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
  845. (void **)gang, 0,
  846. ARRAY_SIZE(gang),
  847. BTRFS_ROOT_TRANS_TAG);
  848. if (ret == 0)
  849. break;
  850. for (i = 0; i < ret; i++) {
  851. root = gang[i];
  852. radix_tree_tag_clear(&fs_info->fs_roots_radix,
  853. (unsigned long)root->root_key.objectid,
  854. BTRFS_ROOT_TRANS_TAG);
  855. spin_unlock(&fs_info->fs_roots_radix_lock);
  856. btrfs_free_log(trans, root);
  857. btrfs_update_reloc_root(trans, root);
  858. btrfs_orphan_commit_root(trans, root);
  859. btrfs_save_ino_cache(root, trans);
  860. /* see comments in should_cow_block() */
  861. root->force_cow = 0;
  862. smp_wmb();
  863. if (root->commit_root != root->node) {
  864. mutex_lock(&root->fs_commit_mutex);
  865. switch_commit_root(root);
  866. btrfs_unpin_free_ino(root);
  867. mutex_unlock(&root->fs_commit_mutex);
  868. btrfs_set_root_node(&root->root_item,
  869. root->node);
  870. }
  871. err = btrfs_update_root(trans, fs_info->tree_root,
  872. &root->root_key,
  873. &root->root_item);
  874. spin_lock(&fs_info->fs_roots_radix_lock);
  875. if (err)
  876. break;
  877. }
  878. }
  879. spin_unlock(&fs_info->fs_roots_radix_lock);
  880. return err;
  881. }
  882. /*
  883. * defrag a given btree.
  884. * Every leaf in the btree is read and defragged.
  885. */
  886. int btrfs_defrag_root(struct btrfs_root *root)
  887. {
  888. struct btrfs_fs_info *info = root->fs_info;
  889. struct btrfs_trans_handle *trans;
  890. int ret;
  891. if (xchg(&root->defrag_running, 1))
  892. return 0;
  893. while (1) {
  894. trans = btrfs_start_transaction(root, 0);
  895. if (IS_ERR(trans))
  896. return PTR_ERR(trans);
  897. ret = btrfs_defrag_leaves(trans, root);
  898. btrfs_end_transaction(trans, root);
  899. btrfs_btree_balance_dirty(info->tree_root);
  900. cond_resched();
  901. if (btrfs_fs_closing(root->fs_info) || ret != -EAGAIN)
  902. break;
  903. if (btrfs_defrag_cancelled(root->fs_info)) {
  904. printk(KERN_DEBUG "btrfs: defrag_root cancelled\n");
  905. ret = -EAGAIN;
  906. break;
  907. }
  908. }
  909. root->defrag_running = 0;
  910. return ret;
  911. }
  912. /*
  913. * new snapshots need to be created at a very specific time in the
  914. * transaction commit. This does the actual creation.
  915. *
  916. * Note:
  917. * If the error which may affect the commitment of the current transaction
  918. * happens, we should return the error number. If the error which just affect
  919. * the creation of the pending snapshots, just return 0.
  920. */
  921. static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
  922. struct btrfs_fs_info *fs_info,
  923. struct btrfs_pending_snapshot *pending)
  924. {
  925. struct btrfs_key key;
  926. struct btrfs_root_item *new_root_item;
  927. struct btrfs_root *tree_root = fs_info->tree_root;
  928. struct btrfs_root *root = pending->root;
  929. struct btrfs_root *parent_root;
  930. struct btrfs_block_rsv *rsv;
  931. struct inode *parent_inode;
  932. struct btrfs_path *path;
  933. struct btrfs_dir_item *dir_item;
  934. struct dentry *dentry;
  935. struct extent_buffer *tmp;
  936. struct extent_buffer *old;
  937. struct timespec cur_time = CURRENT_TIME;
  938. int ret = 0;
  939. u64 to_reserve = 0;
  940. u64 index = 0;
  941. u64 objectid;
  942. u64 root_flags;
  943. uuid_le new_uuid;
  944. path = btrfs_alloc_path();
  945. if (!path) {
  946. pending->error = -ENOMEM;
  947. return 0;
  948. }
  949. new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
  950. if (!new_root_item) {
  951. pending->error = -ENOMEM;
  952. goto root_item_alloc_fail;
  953. }
  954. pending->error = btrfs_find_free_objectid(tree_root, &objectid);
  955. if (pending->error)
  956. goto no_free_objectid;
  957. btrfs_reloc_pre_snapshot(trans, pending, &to_reserve);
  958. if (to_reserve > 0) {
  959. pending->error = btrfs_block_rsv_add(root,
  960. &pending->block_rsv,
  961. to_reserve,
  962. BTRFS_RESERVE_NO_FLUSH);
  963. if (pending->error)
  964. goto no_free_objectid;
  965. }
  966. pending->error = btrfs_qgroup_inherit(trans, fs_info,
  967. root->root_key.objectid,
  968. objectid, pending->inherit);
  969. if (pending->error)
  970. goto no_free_objectid;
  971. key.objectid = objectid;
  972. key.offset = (u64)-1;
  973. key.type = BTRFS_ROOT_ITEM_KEY;
  974. rsv = trans->block_rsv;
  975. trans->block_rsv = &pending->block_rsv;
  976. trans->bytes_reserved = trans->block_rsv->reserved;
  977. dentry = pending->dentry;
  978. parent_inode = pending->dir;
  979. parent_root = BTRFS_I(parent_inode)->root;
  980. record_root_in_trans(trans, parent_root);
  981. /*
  982. * insert the directory item
  983. */
  984. ret = btrfs_set_inode_index(parent_inode, &index);
  985. BUG_ON(ret); /* -ENOMEM */
  986. /* check if there is a file/dir which has the same name. */
  987. dir_item = btrfs_lookup_dir_item(NULL, parent_root, path,
  988. btrfs_ino(parent_inode),
  989. dentry->d_name.name,
  990. dentry->d_name.len, 0);
  991. if (dir_item != NULL && !IS_ERR(dir_item)) {
  992. pending->error = -EEXIST;
  993. goto dir_item_existed;
  994. } else if (IS_ERR(dir_item)) {
  995. ret = PTR_ERR(dir_item);
  996. btrfs_abort_transaction(trans, root, ret);
  997. goto fail;
  998. }
  999. btrfs_release_path(path);
  1000. /*
  1001. * pull in the delayed directory update
  1002. * and the delayed inode item
  1003. * otherwise we corrupt the FS during
  1004. * snapshot
  1005. */
  1006. ret = btrfs_run_delayed_items(trans, root);
  1007. if (ret) { /* Transaction aborted */
  1008. btrfs_abort_transaction(trans, root, ret);
  1009. goto fail;
  1010. }
  1011. record_root_in_trans(trans, root);
  1012. btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
  1013. memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
  1014. btrfs_check_and_init_root_item(new_root_item);
  1015. root_flags = btrfs_root_flags(new_root_item);
  1016. if (pending->readonly)
  1017. root_flags |= BTRFS_ROOT_SUBVOL_RDONLY;
  1018. else
  1019. root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY;
  1020. btrfs_set_root_flags(new_root_item, root_flags);
  1021. btrfs_set_root_generation_v2(new_root_item,
  1022. trans->transid);
  1023. uuid_le_gen(&new_uuid);
  1024. memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
  1025. memcpy(new_root_item->parent_uuid, root->root_item.uuid,
  1026. BTRFS_UUID_SIZE);
  1027. new_root_item->otime.sec = cpu_to_le64(cur_time.tv_sec);
  1028. new_root_item->otime.nsec = cpu_to_le32(cur_time.tv_nsec);
  1029. btrfs_set_root_otransid(new_root_item, trans->transid);
  1030. memset(&new_root_item->stime, 0, sizeof(new_root_item->stime));
  1031. memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime));
  1032. btrfs_set_root_stransid(new_root_item, 0);
  1033. btrfs_set_root_rtransid(new_root_item, 0);
  1034. old = btrfs_lock_root_node(root);
  1035. ret = btrfs_cow_block(trans, root, old, NULL, 0, &old);
  1036. if (ret) {
  1037. btrfs_tree_unlock(old);
  1038. free_extent_buffer(old);
  1039. btrfs_abort_transaction(trans, root, ret);
  1040. goto fail;
  1041. }
  1042. btrfs_set_lock_blocking(old);
  1043. ret = btrfs_copy_root(trans, root, old, &tmp, objectid);
  1044. /* clean up in any case */
  1045. btrfs_tree_unlock(old);
  1046. free_extent_buffer(old);
  1047. if (ret) {
  1048. btrfs_abort_transaction(trans, root, ret);
  1049. goto fail;
  1050. }
  1051. /* see comments in should_cow_block() */
  1052. root->force_cow = 1;
  1053. smp_wmb();
  1054. btrfs_set_root_node(new_root_item, tmp);
  1055. /* record when the snapshot was created in key.offset */
  1056. key.offset = trans->transid;
  1057. ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
  1058. btrfs_tree_unlock(tmp);
  1059. free_extent_buffer(tmp);
  1060. if (ret) {
  1061. btrfs_abort_transaction(trans, root, ret);
  1062. goto fail;
  1063. }
  1064. /*
  1065. * insert root back/forward references
  1066. */
  1067. ret = btrfs_add_root_ref(trans, tree_root, objectid,
  1068. parent_root->root_key.objectid,
  1069. btrfs_ino(parent_inode), index,
  1070. dentry->d_name.name, dentry->d_name.len);
  1071. if (ret) {
  1072. btrfs_abort_transaction(trans, root, ret);
  1073. goto fail;
  1074. }
  1075. key.offset = (u64)-1;
  1076. pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key);
  1077. if (IS_ERR(pending->snap)) {
  1078. ret = PTR_ERR(pending->snap);
  1079. btrfs_abort_transaction(trans, root, ret);
  1080. goto fail;
  1081. }
  1082. ret = btrfs_reloc_post_snapshot(trans, pending);
  1083. if (ret) {
  1084. btrfs_abort_transaction(trans, root, ret);
  1085. goto fail;
  1086. }
  1087. ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
  1088. if (ret) {
  1089. btrfs_abort_transaction(trans, root, ret);
  1090. goto fail;
  1091. }
  1092. ret = btrfs_insert_dir_item(trans, parent_root,
  1093. dentry->d_name.name, dentry->d_name.len,
  1094. parent_inode, &key,
  1095. BTRFS_FT_DIR, index);
  1096. /* We have check then name at the beginning, so it is impossible. */
  1097. BUG_ON(ret == -EEXIST || ret == -EOVERFLOW);
  1098. if (ret) {
  1099. btrfs_abort_transaction(trans, root, ret);
  1100. goto fail;
  1101. }
  1102. btrfs_i_size_write(parent_inode, parent_inode->i_size +
  1103. dentry->d_name.len * 2);
  1104. parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
  1105. ret = btrfs_update_inode_fallback(trans, parent_root, parent_inode);
  1106. if (ret)
  1107. btrfs_abort_transaction(trans, root, ret);
  1108. fail:
  1109. pending->error = ret;
  1110. dir_item_existed:
  1111. trans->block_rsv = rsv;
  1112. trans->bytes_reserved = 0;
  1113. no_free_objectid:
  1114. kfree(new_root_item);
  1115. root_item_alloc_fail:
  1116. btrfs_free_path(path);
  1117. return ret;
  1118. }
  1119. /*
  1120. * create all the snapshots we've scheduled for creation
  1121. */
  1122. static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
  1123. struct btrfs_fs_info *fs_info)
  1124. {
  1125. struct btrfs_pending_snapshot *pending, *next;
  1126. struct list_head *head = &trans->transaction->pending_snapshots;
  1127. int ret = 0;
  1128. list_for_each_entry_safe(pending, next, head, list) {
  1129. list_del(&pending->list);
  1130. ret = create_pending_snapshot(trans, fs_info, pending);
  1131. if (ret)
  1132. break;
  1133. }
  1134. return ret;
  1135. }
  1136. static void update_super_roots(struct btrfs_root *root)
  1137. {
  1138. struct btrfs_root_item *root_item;
  1139. struct btrfs_super_block *super;
  1140. super = root->fs_info->super_copy;
  1141. root_item = &root->fs_info->chunk_root->root_item;
  1142. super->chunk_root = root_item->bytenr;
  1143. super->chunk_root_generation = root_item->generation;
  1144. super->chunk_root_level = root_item->level;
  1145. root_item = &root->fs_info->tree_root->root_item;
  1146. super->root = root_item->bytenr;
  1147. super->generation = root_item->generation;
  1148. super->root_level = root_item->level;
  1149. if (btrfs_test_opt(root, SPACE_CACHE))
  1150. super->cache_generation = root_item->generation;
  1151. }
  1152. int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
  1153. {
  1154. int ret = 0;
  1155. spin_lock(&info->trans_lock);
  1156. if (info->running_transaction)
  1157. ret = info->running_transaction->in_commit;
  1158. spin_unlock(&info->trans_lock);
  1159. return ret;
  1160. }
  1161. int btrfs_transaction_blocked(struct btrfs_fs_info *info)
  1162. {
  1163. int ret = 0;
  1164. spin_lock(&info->trans_lock);
  1165. if (info->running_transaction)
  1166. ret = info->running_transaction->blocked;
  1167. spin_unlock(&info->trans_lock);
  1168. return ret;
  1169. }
  1170. /*
  1171. * wait for the current transaction commit to start and block subsequent
  1172. * transaction joins
  1173. */
  1174. static void wait_current_trans_commit_start(struct btrfs_root *root,
  1175. struct btrfs_transaction *trans)
  1176. {
  1177. wait_event(root->fs_info->transaction_blocked_wait, trans->in_commit);
  1178. }
  1179. /*
  1180. * wait for the current transaction to start and then become unblocked.
  1181. * caller holds ref.
  1182. */
  1183. static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root,
  1184. struct btrfs_transaction *trans)
  1185. {
  1186. wait_event(root->fs_info->transaction_wait,
  1187. trans->commit_done || (trans->in_commit && !trans->blocked));
  1188. }
  1189. /*
  1190. * commit transactions asynchronously. once btrfs_commit_transaction_async
  1191. * returns, any subsequent transaction will not be allowed to join.
  1192. */
  1193. struct btrfs_async_commit {
  1194. struct btrfs_trans_handle *newtrans;
  1195. struct btrfs_root *root;
  1196. struct work_struct work;
  1197. };
  1198. static void do_async_commit(struct work_struct *work)
  1199. {
  1200. struct btrfs_async_commit *ac =
  1201. container_of(work, struct btrfs_async_commit, work);
  1202. /*
  1203. * We've got freeze protection passed with the transaction.
  1204. * Tell lockdep about it.
  1205. */
  1206. if (ac->newtrans->type < TRANS_JOIN_NOLOCK)
  1207. rwsem_acquire_read(
  1208. &ac->root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1],
  1209. 0, 1, _THIS_IP_);
  1210. current->journal_info = ac->newtrans;
  1211. btrfs_commit_transaction(ac->newtrans, ac->root);
  1212. kfree(ac);
  1213. }
  1214. int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
  1215. struct btrfs_root *root,
  1216. int wait_for_unblock)
  1217. {
  1218. struct btrfs_async_commit *ac;
  1219. struct btrfs_transaction *cur_trans;
  1220. ac = kmalloc(sizeof(*ac), GFP_NOFS);
  1221. if (!ac)
  1222. return -ENOMEM;
  1223. INIT_WORK(&ac->work, do_async_commit);
  1224. ac->root = root;
  1225. ac->newtrans = btrfs_join_transaction(root);
  1226. if (IS_ERR(ac->newtrans)) {
  1227. int err = PTR_ERR(ac->newtrans);
  1228. kfree(ac);
  1229. return err;
  1230. }
  1231. /* take transaction reference */
  1232. cur_trans = trans->transaction;
  1233. atomic_inc(&cur_trans->use_count);
  1234. btrfs_end_transaction(trans, root);
  1235. /*
  1236. * Tell lockdep we've released the freeze rwsem, since the
  1237. * async commit thread will be the one to unlock it.
  1238. */
  1239. if (trans->type < TRANS_JOIN_NOLOCK)
  1240. rwsem_release(
  1241. &root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1],
  1242. 1, _THIS_IP_);
  1243. schedule_work(&ac->work);
  1244. /* wait for transaction to start and unblock */
  1245. if (wait_for_unblock)
  1246. wait_current_trans_commit_start_and_unblock(root, cur_trans);
  1247. else
  1248. wait_current_trans_commit_start(root, cur_trans);
  1249. if (current->journal_info == trans)
  1250. current->journal_info = NULL;
  1251. put_transaction(cur_trans);
  1252. return 0;
  1253. }
  1254. static void cleanup_transaction(struct btrfs_trans_handle *trans,
  1255. struct btrfs_root *root, int err)
  1256. {
  1257. struct btrfs_transaction *cur_trans = trans->transaction;
  1258. DEFINE_WAIT(wait);
  1259. WARN_ON(trans->use_count > 1);
  1260. btrfs_abort_transaction(trans, root, err);
  1261. spin_lock(&root->fs_info->trans_lock);
  1262. if (list_empty(&cur_trans->list)) {
  1263. spin_unlock(&root->fs_info->trans_lock);
  1264. btrfs_end_transaction(trans, root);
  1265. return;
  1266. }
  1267. list_del_init(&cur_trans->list);
  1268. if (cur_trans == root->fs_info->running_transaction) {
  1269. root->fs_info->trans_no_join = 1;
  1270. spin_unlock(&root->fs_info->trans_lock);
  1271. wait_event(cur_trans->writer_wait,
  1272. atomic_read(&cur_trans->num_writers) == 1);
  1273. spin_lock(&root->fs_info->trans_lock);
  1274. root->fs_info->running_transaction = NULL;
  1275. }
  1276. spin_unlock(&root->fs_info->trans_lock);
  1277. btrfs_cleanup_one_transaction(trans->transaction, root);
  1278. put_transaction(cur_trans);
  1279. put_transaction(cur_trans);
  1280. trace_btrfs_transaction_commit(root);
  1281. btrfs_scrub_continue(root);
  1282. if (current->journal_info == trans)
  1283. current->journal_info = NULL;
  1284. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  1285. }
  1286. static int btrfs_flush_all_pending_stuffs(struct btrfs_trans_handle *trans,
  1287. struct btrfs_root *root)
  1288. {
  1289. int flush_on_commit = btrfs_test_opt(root, FLUSHONCOMMIT);
  1290. int snap_pending = 0;
  1291. int ret;
  1292. if (!flush_on_commit) {
  1293. spin_lock(&root->fs_info->trans_lock);
  1294. if (!list_empty(&trans->transaction->pending_snapshots))
  1295. snap_pending = 1;
  1296. spin_unlock(&root->fs_info->trans_lock);
  1297. }
  1298. if (flush_on_commit || snap_pending) {
  1299. ret = btrfs_start_delalloc_inodes(root, 1);
  1300. if (ret)
  1301. return ret;
  1302. btrfs_wait_ordered_extents(root, 1);
  1303. }
  1304. ret = btrfs_run_delayed_items(trans, root);
  1305. if (ret)
  1306. return ret;
  1307. /*
  1308. * running the delayed items may have added new refs. account
  1309. * them now so that they hinder processing of more delayed refs
  1310. * as little as possible.
  1311. */
  1312. btrfs_delayed_refs_qgroup_accounting(trans, root->fs_info);
  1313. /*
  1314. * rename don't use btrfs_join_transaction, so, once we
  1315. * set the transaction to blocked above, we aren't going
  1316. * to get any new ordered operations. We can safely run
  1317. * it here and no for sure that nothing new will be added
  1318. * to the list
  1319. */
  1320. ret = btrfs_run_ordered_operations(trans, root, 1);
  1321. return ret;
  1322. }
  1323. /*
  1324. * btrfs_transaction state sequence:
  1325. * in_commit = 0, blocked = 0 (initial)
  1326. * in_commit = 1, blocked = 1
  1327. * blocked = 0
  1328. * commit_done = 1
  1329. */
  1330. int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
  1331. struct btrfs_root *root)
  1332. {
  1333. unsigned long joined = 0;
  1334. struct btrfs_transaction *cur_trans = trans->transaction;
  1335. struct btrfs_transaction *prev_trans = NULL;
  1336. DEFINE_WAIT(wait);
  1337. int ret;
  1338. int should_grow = 0;
  1339. unsigned long now = get_seconds();
  1340. ret = btrfs_run_ordered_operations(trans, root, 0);
  1341. if (ret) {
  1342. btrfs_abort_transaction(trans, root, ret);
  1343. btrfs_end_transaction(trans, root);
  1344. return ret;
  1345. }
  1346. /* Stop the commit early if ->aborted is set */
  1347. if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
  1348. ret = cur_trans->aborted;
  1349. btrfs_end_transaction(trans, root);
  1350. return ret;
  1351. }
  1352. /* make a pass through all the delayed refs we have so far
  1353. * any runnings procs may add more while we are here
  1354. */
  1355. ret = btrfs_run_delayed_refs(trans, root, 0);
  1356. if (ret) {
  1357. btrfs_end_transaction(trans, root);
  1358. return ret;
  1359. }
  1360. btrfs_trans_release_metadata(trans, root);
  1361. trans->block_rsv = NULL;
  1362. if (trans->qgroup_reserved) {
  1363. btrfs_qgroup_free(root, trans->qgroup_reserved);
  1364. trans->qgroup_reserved = 0;
  1365. }
  1366. cur_trans = trans->transaction;
  1367. /*
  1368. * set the flushing flag so procs in this transaction have to
  1369. * start sending their work down.
  1370. */
  1371. cur_trans->delayed_refs.flushing = 1;
  1372. if (!list_empty(&trans->new_bgs))
  1373. btrfs_create_pending_block_groups(trans, root);
  1374. ret = btrfs_run_delayed_refs(trans, root, 0);
  1375. if (ret) {
  1376. btrfs_end_transaction(trans, root);
  1377. return ret;
  1378. }
  1379. spin_lock(&cur_trans->commit_lock);
  1380. if (cur_trans->in_commit) {
  1381. spin_unlock(&cur_trans->commit_lock);
  1382. atomic_inc(&cur_trans->use_count);
  1383. ret = btrfs_end_transaction(trans, root);
  1384. wait_for_commit(root, cur_trans);
  1385. put_transaction(cur_trans);
  1386. return ret;
  1387. }
  1388. trans->transaction->in_commit = 1;
  1389. trans->transaction->blocked = 1;
  1390. spin_unlock(&cur_trans->commit_lock);
  1391. wake_up(&root->fs_info->transaction_blocked_wait);
  1392. spin_lock(&root->fs_info->trans_lock);
  1393. if (cur_trans->list.prev != &root->fs_info->trans_list) {
  1394. prev_trans = list_entry(cur_trans->list.prev,
  1395. struct btrfs_transaction, list);
  1396. if (!prev_trans->commit_done) {
  1397. atomic_inc(&prev_trans->use_count);
  1398. spin_unlock(&root->fs_info->trans_lock);
  1399. wait_for_commit(root, prev_trans);
  1400. put_transaction(prev_trans);
  1401. } else {
  1402. spin_unlock(&root->fs_info->trans_lock);
  1403. }
  1404. } else {
  1405. spin_unlock(&root->fs_info->trans_lock);
  1406. }
  1407. if (!btrfs_test_opt(root, SSD) &&
  1408. (now < cur_trans->start_time || now - cur_trans->start_time < 1))
  1409. should_grow = 1;
  1410. do {
  1411. joined = cur_trans->num_joined;
  1412. WARN_ON(cur_trans != trans->transaction);
  1413. ret = btrfs_flush_all_pending_stuffs(trans, root);
  1414. if (ret)
  1415. goto cleanup_transaction;
  1416. prepare_to_wait(&cur_trans->writer_wait, &wait,
  1417. TASK_UNINTERRUPTIBLE);
  1418. if (atomic_read(&cur_trans->num_writers) > 1)
  1419. schedule_timeout(MAX_SCHEDULE_TIMEOUT);
  1420. else if (should_grow)
  1421. schedule_timeout(1);
  1422. finish_wait(&cur_trans->writer_wait, &wait);
  1423. } while (atomic_read(&cur_trans->num_writers) > 1 ||
  1424. (should_grow && cur_trans->num_joined != joined));
  1425. ret = btrfs_flush_all_pending_stuffs(trans, root);
  1426. if (ret)
  1427. goto cleanup_transaction;
  1428. /*
  1429. * Ok now we need to make sure to block out any other joins while we
  1430. * commit the transaction. We could have started a join before setting
  1431. * no_join so make sure to wait for num_writers to == 1 again.
  1432. */
  1433. spin_lock(&root->fs_info->trans_lock);
  1434. root->fs_info->trans_no_join = 1;
  1435. spin_unlock(&root->fs_info->trans_lock);
  1436. wait_event(cur_trans->writer_wait,
  1437. atomic_read(&cur_trans->num_writers) == 1);
  1438. /* ->aborted might be set after the previous check, so check it */
  1439. if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
  1440. ret = cur_trans->aborted;
  1441. goto cleanup_transaction;
  1442. }
  1443. /*
  1444. * the reloc mutex makes sure that we stop
  1445. * the balancing code from coming in and moving
  1446. * extents around in the middle of the commit
  1447. */
  1448. mutex_lock(&root->fs_info->reloc_mutex);
  1449. /*
  1450. * We needn't worry about the delayed items because we will
  1451. * deal with them in create_pending_snapshot(), which is the
  1452. * core function of the snapshot creation.
  1453. */
  1454. ret = create_pending_snapshots(trans, root->fs_info);
  1455. if (ret) {
  1456. mutex_unlock(&root->fs_info->reloc_mutex);
  1457. goto cleanup_transaction;
  1458. }
  1459. /*
  1460. * We insert the dir indexes of the snapshots and update the inode
  1461. * of the snapshots' parents after the snapshot creation, so there
  1462. * are some delayed items which are not dealt with. Now deal with
  1463. * them.
  1464. *
  1465. * We needn't worry that this operation will corrupt the snapshots,
  1466. * because all the tree which are snapshoted will be forced to COW
  1467. * the nodes and leaves.
  1468. */
  1469. ret = btrfs_run_delayed_items(trans, root);
  1470. if (ret) {
  1471. mutex_unlock(&root->fs_info->reloc_mutex);
  1472. goto cleanup_transaction;
  1473. }
  1474. ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
  1475. if (ret) {
  1476. mutex_unlock(&root->fs_info->reloc_mutex);
  1477. goto cleanup_transaction;
  1478. }
  1479. /*
  1480. * make sure none of the code above managed to slip in a
  1481. * delayed item
  1482. */
  1483. btrfs_assert_delayed_root_empty(root);
  1484. WARN_ON(cur_trans != trans->transaction);
  1485. btrfs_scrub_pause(root);
  1486. /* btrfs_commit_tree_roots is responsible for getting the
  1487. * various roots consistent with each other. Every pointer
  1488. * in the tree of tree roots has to point to the most up to date
  1489. * root for every subvolume and other tree. So, we have to keep
  1490. * the tree logging code from jumping in and changing any
  1491. * of the trees.
  1492. *
  1493. * At this point in the commit, there can't be any tree-log
  1494. * writers, but a little lower down we drop the trans mutex
  1495. * and let new people in. By holding the tree_log_mutex
  1496. * from now until after the super is written, we avoid races
  1497. * with the tree-log code.
  1498. */
  1499. mutex_lock(&root->fs_info->tree_log_mutex);
  1500. ret = commit_fs_roots(trans, root);
  1501. if (ret) {
  1502. mutex_unlock(&root->fs_info->tree_log_mutex);
  1503. mutex_unlock(&root->fs_info->reloc_mutex);
  1504. goto cleanup_transaction;
  1505. }
  1506. /* commit_fs_roots gets rid of all the tree log roots, it is now
  1507. * safe to free the root of tree log roots
  1508. */
  1509. btrfs_free_log_root_tree(trans, root->fs_info);
  1510. ret = commit_cowonly_roots(trans, root);
  1511. if (ret) {
  1512. mutex_unlock(&root->fs_info->tree_log_mutex);
  1513. mutex_unlock(&root->fs_info->reloc_mutex);
  1514. goto cleanup_transaction;
  1515. }
  1516. /*
  1517. * The tasks which save the space cache and inode cache may also
  1518. * update ->aborted, check it.
  1519. */
  1520. if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
  1521. ret = cur_trans->aborted;
  1522. mutex_unlock(&root->fs_info->tree_log_mutex);
  1523. mutex_unlock(&root->fs_info->reloc_mutex);
  1524. goto cleanup_transaction;
  1525. }
  1526. btrfs_prepare_extent_commit(trans, root);
  1527. cur_trans = root->fs_info->running_transaction;
  1528. btrfs_set_root_node(&root->fs_info->tree_root->root_item,
  1529. root->fs_info->tree_root->node);
  1530. switch_commit_root(root->fs_info->tree_root);
  1531. btrfs_set_root_node(&root->fs_info->chunk_root->root_item,
  1532. root->fs_info->chunk_root->node);
  1533. switch_commit_root(root->fs_info->chunk_root);
  1534. assert_qgroups_uptodate(trans);
  1535. update_super_roots(root);
  1536. if (!root->fs_info->log_root_recovering) {
  1537. btrfs_set_super_log_root(root->fs_info->super_copy, 0);
  1538. btrfs_set_super_log_root_level(root->fs_info->super_copy, 0);
  1539. }
  1540. memcpy(root->fs_info->super_for_commit, root->fs_info->super_copy,
  1541. sizeof(*root->fs_info->super_copy));
  1542. trans->transaction->blocked = 0;
  1543. spin_lock(&root->fs_info->trans_lock);
  1544. root->fs_info->running_transaction = NULL;
  1545. root->fs_info->trans_no_join = 0;
  1546. spin_unlock(&root->fs_info->trans_lock);
  1547. mutex_unlock(&root->fs_info->reloc_mutex);
  1548. wake_up(&root->fs_info->transaction_wait);
  1549. ret = btrfs_write_and_wait_transaction(trans, root);
  1550. if (ret) {
  1551. btrfs_error(root->fs_info, ret,
  1552. "Error while writing out transaction.");
  1553. mutex_unlock(&root->fs_info->tree_log_mutex);
  1554. goto cleanup_transaction;
  1555. }
  1556. ret = write_ctree_super(trans, root, 0);
  1557. if (ret) {
  1558. mutex_unlock(&root->fs_info->tree_log_mutex);
  1559. goto cleanup_transaction;
  1560. }
  1561. /*
  1562. * the super is written, we can safely allow the tree-loggers
  1563. * to go about their business
  1564. */
  1565. mutex_unlock(&root->fs_info->tree_log_mutex);
  1566. btrfs_finish_extent_commit(trans, root);
  1567. cur_trans->commit_done = 1;
  1568. root->fs_info->last_trans_committed = cur_trans->transid;
  1569. wake_up(&cur_trans->commit_wait);
  1570. spin_lock(&root->fs_info->trans_lock);
  1571. list_del_init(&cur_trans->list);
  1572. spin_unlock(&root->fs_info->trans_lock);
  1573. put_transaction(cur_trans);
  1574. put_transaction(cur_trans);
  1575. if (trans->type < TRANS_JOIN_NOLOCK)
  1576. sb_end_intwrite(root->fs_info->sb);
  1577. trace_btrfs_transaction_commit(root);
  1578. btrfs_scrub_continue(root);
  1579. if (current->journal_info == trans)
  1580. current->journal_info = NULL;
  1581. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  1582. if (current != root->fs_info->transaction_kthread)
  1583. btrfs_run_delayed_iputs(root);
  1584. return ret;
  1585. cleanup_transaction:
  1586. btrfs_trans_release_metadata(trans, root);
  1587. trans->block_rsv = NULL;
  1588. if (trans->qgroup_reserved) {
  1589. btrfs_qgroup_free(root, trans->qgroup_reserved);
  1590. trans->qgroup_reserved = 0;
  1591. }
  1592. btrfs_printk(root->fs_info, "Skipping commit of aborted transaction.\n");
  1593. // WARN_ON(1);
  1594. if (current->journal_info == trans)
  1595. current->journal_info = NULL;
  1596. cleanup_transaction(trans, root, ret);
  1597. return ret;
  1598. }
  1599. /*
  1600. * interface function to delete all the snapshots we have scheduled for deletion
  1601. */
  1602. int btrfs_clean_old_snapshots(struct btrfs_root *root)
  1603. {
  1604. LIST_HEAD(list);
  1605. struct btrfs_fs_info *fs_info = root->fs_info;
  1606. spin_lock(&fs_info->trans_lock);
  1607. list_splice_init(&fs_info->dead_roots, &list);
  1608. spin_unlock(&fs_info->trans_lock);
  1609. while (!list_empty(&list)) {
  1610. int ret;
  1611. root = list_entry(list.next, struct btrfs_root, root_list);
  1612. list_del(&root->root_list);
  1613. btrfs_kill_all_delayed_nodes(root);
  1614. if (btrfs_header_backref_rev(root->node) <
  1615. BTRFS_MIXED_BACKREF_REV)
  1616. ret = btrfs_drop_snapshot(root, NULL, 0, 0);
  1617. else
  1618. ret =btrfs_drop_snapshot(root, NULL, 1, 0);
  1619. BUG_ON(ret < 0);
  1620. }
  1621. return 0;
  1622. }