transaction.c 50 KB

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