transaction.c 52 KB

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