transaction.c 53 KB

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