transaction.c 52 KB

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