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