transaction.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2007 Oracle. All rights reserved.
  4. */
  5. #include <linux/fs.h>
  6. #include <linux/slab.h>
  7. #include <linux/sched.h>
  8. #include <linux/writeback.h>
  9. #include <linux/pagemap.h>
  10. #include <linux/blkdev.h>
  11. #include <linux/uuid.h>
  12. #include "ctree.h"
  13. #include "disk-io.h"
  14. #include "transaction.h"
  15. #include "locking.h"
  16. #include "tree-log.h"
  17. #include "inode-map.h"
  18. #include "volumes.h"
  19. #include "dev-replace.h"
  20. #include "qgroup.h"
  21. #define BTRFS_ROOT_TRANS_TAG 0
  22. static const unsigned int btrfs_blocked_trans_types[TRANS_STATE_MAX] = {
  23. [TRANS_STATE_RUNNING] = 0U,
  24. [TRANS_STATE_BLOCKED] = __TRANS_START,
  25. [TRANS_STATE_COMMIT_START] = (__TRANS_START | __TRANS_ATTACH),
  26. [TRANS_STATE_COMMIT_DOING] = (__TRANS_START |
  27. __TRANS_ATTACH |
  28. __TRANS_JOIN),
  29. [TRANS_STATE_UNBLOCKED] = (__TRANS_START |
  30. __TRANS_ATTACH |
  31. __TRANS_JOIN |
  32. __TRANS_JOIN_NOLOCK),
  33. [TRANS_STATE_COMPLETED] = (__TRANS_START |
  34. __TRANS_ATTACH |
  35. __TRANS_JOIN |
  36. __TRANS_JOIN_NOLOCK),
  37. };
  38. void btrfs_put_transaction(struct btrfs_transaction *transaction)
  39. {
  40. WARN_ON(refcount_read(&transaction->use_count) == 0);
  41. if (refcount_dec_and_test(&transaction->use_count)) {
  42. BUG_ON(!list_empty(&transaction->list));
  43. WARN_ON(!RB_EMPTY_ROOT(&transaction->delayed_refs.href_root));
  44. if (transaction->delayed_refs.pending_csums)
  45. btrfs_err(transaction->fs_info,
  46. "pending csums is %llu",
  47. transaction->delayed_refs.pending_csums);
  48. while (!list_empty(&transaction->pending_chunks)) {
  49. struct extent_map *em;
  50. em = list_first_entry(&transaction->pending_chunks,
  51. struct extent_map, list);
  52. list_del_init(&em->list);
  53. free_extent_map(em);
  54. }
  55. /*
  56. * If any block groups are found in ->deleted_bgs then it's
  57. * because the transaction was aborted and a commit did not
  58. * happen (things failed before writing the new superblock
  59. * and calling btrfs_finish_extent_commit()), so we can not
  60. * discard the physical locations of the block groups.
  61. */
  62. while (!list_empty(&transaction->deleted_bgs)) {
  63. struct btrfs_block_group_cache *cache;
  64. cache = list_first_entry(&transaction->deleted_bgs,
  65. struct btrfs_block_group_cache,
  66. bg_list);
  67. list_del_init(&cache->bg_list);
  68. btrfs_put_block_group_trimming(cache);
  69. btrfs_put_block_group(cache);
  70. }
  71. kfree(transaction);
  72. }
  73. }
  74. static void clear_btree_io_tree(struct extent_io_tree *tree)
  75. {
  76. spin_lock(&tree->lock);
  77. /*
  78. * Do a single barrier for the waitqueue_active check here, the state
  79. * of the waitqueue should not change once clear_btree_io_tree is
  80. * called.
  81. */
  82. smp_mb();
  83. while (!RB_EMPTY_ROOT(&tree->state)) {
  84. struct rb_node *node;
  85. struct extent_state *state;
  86. node = rb_first(&tree->state);
  87. state = rb_entry(node, struct extent_state, rb_node);
  88. rb_erase(&state->rb_node, &tree->state);
  89. RB_CLEAR_NODE(&state->rb_node);
  90. /*
  91. * btree io trees aren't supposed to have tasks waiting for
  92. * changes in the flags of extent states ever.
  93. */
  94. ASSERT(!waitqueue_active(&state->wq));
  95. free_extent_state(state);
  96. cond_resched_lock(&tree->lock);
  97. }
  98. spin_unlock(&tree->lock);
  99. }
  100. static noinline void switch_commit_roots(struct btrfs_transaction *trans)
  101. {
  102. struct btrfs_fs_info *fs_info = trans->fs_info;
  103. struct btrfs_root *root, *tmp;
  104. down_write(&fs_info->commit_root_sem);
  105. list_for_each_entry_safe(root, tmp, &trans->switch_commits,
  106. dirty_list) {
  107. list_del_init(&root->dirty_list);
  108. free_extent_buffer(root->commit_root);
  109. root->commit_root = btrfs_root_node(root);
  110. if (is_fstree(root->objectid))
  111. btrfs_unpin_free_ino(root);
  112. clear_btree_io_tree(&root->dirty_log_pages);
  113. }
  114. /* We can free old roots now. */
  115. spin_lock(&trans->dropped_roots_lock);
  116. while (!list_empty(&trans->dropped_roots)) {
  117. root = list_first_entry(&trans->dropped_roots,
  118. struct btrfs_root, root_list);
  119. list_del_init(&root->root_list);
  120. spin_unlock(&trans->dropped_roots_lock);
  121. btrfs_drop_and_free_fs_root(fs_info, root);
  122. spin_lock(&trans->dropped_roots_lock);
  123. }
  124. spin_unlock(&trans->dropped_roots_lock);
  125. up_write(&fs_info->commit_root_sem);
  126. }
  127. static inline void extwriter_counter_inc(struct btrfs_transaction *trans,
  128. unsigned int type)
  129. {
  130. if (type & TRANS_EXTWRITERS)
  131. atomic_inc(&trans->num_extwriters);
  132. }
  133. static inline void extwriter_counter_dec(struct btrfs_transaction *trans,
  134. unsigned int type)
  135. {
  136. if (type & TRANS_EXTWRITERS)
  137. atomic_dec(&trans->num_extwriters);
  138. }
  139. static inline void extwriter_counter_init(struct btrfs_transaction *trans,
  140. unsigned int type)
  141. {
  142. atomic_set(&trans->num_extwriters, ((type & TRANS_EXTWRITERS) ? 1 : 0));
  143. }
  144. static inline int extwriter_counter_read(struct btrfs_transaction *trans)
  145. {
  146. return atomic_read(&trans->num_extwriters);
  147. }
  148. /*
  149. * either allocate a new transaction or hop into the existing one
  150. */
  151. static noinline int join_transaction(struct btrfs_fs_info *fs_info,
  152. unsigned int type)
  153. {
  154. struct btrfs_transaction *cur_trans;
  155. spin_lock(&fs_info->trans_lock);
  156. loop:
  157. /* The file system has been taken offline. No new transactions. */
  158. if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
  159. spin_unlock(&fs_info->trans_lock);
  160. return -EROFS;
  161. }
  162. cur_trans = fs_info->running_transaction;
  163. if (cur_trans) {
  164. if (cur_trans->aborted) {
  165. spin_unlock(&fs_info->trans_lock);
  166. return cur_trans->aborted;
  167. }
  168. if (btrfs_blocked_trans_types[cur_trans->state] & type) {
  169. spin_unlock(&fs_info->trans_lock);
  170. return -EBUSY;
  171. }
  172. refcount_inc(&cur_trans->use_count);
  173. atomic_inc(&cur_trans->num_writers);
  174. extwriter_counter_inc(cur_trans, type);
  175. spin_unlock(&fs_info->trans_lock);
  176. return 0;
  177. }
  178. spin_unlock(&fs_info->trans_lock);
  179. /*
  180. * If we are ATTACH, we just want to catch the current transaction,
  181. * and commit it. If there is no transaction, just return ENOENT.
  182. */
  183. if (type == TRANS_ATTACH)
  184. return -ENOENT;
  185. /*
  186. * JOIN_NOLOCK only happens during the transaction commit, so
  187. * it is impossible that ->running_transaction is NULL
  188. */
  189. BUG_ON(type == TRANS_JOIN_NOLOCK);
  190. cur_trans = kmalloc(sizeof(*cur_trans), GFP_NOFS);
  191. if (!cur_trans)
  192. return -ENOMEM;
  193. spin_lock(&fs_info->trans_lock);
  194. if (fs_info->running_transaction) {
  195. /*
  196. * someone started a transaction after we unlocked. Make sure
  197. * to redo the checks above
  198. */
  199. kfree(cur_trans);
  200. goto loop;
  201. } else if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
  202. spin_unlock(&fs_info->trans_lock);
  203. kfree(cur_trans);
  204. return -EROFS;
  205. }
  206. cur_trans->fs_info = fs_info;
  207. atomic_set(&cur_trans->num_writers, 1);
  208. extwriter_counter_init(cur_trans, type);
  209. init_waitqueue_head(&cur_trans->writer_wait);
  210. init_waitqueue_head(&cur_trans->commit_wait);
  211. init_waitqueue_head(&cur_trans->pending_wait);
  212. cur_trans->state = TRANS_STATE_RUNNING;
  213. /*
  214. * One for this trans handle, one so it will live on until we
  215. * commit the transaction.
  216. */
  217. refcount_set(&cur_trans->use_count, 2);
  218. atomic_set(&cur_trans->pending_ordered, 0);
  219. cur_trans->flags = 0;
  220. cur_trans->start_time = get_seconds();
  221. memset(&cur_trans->delayed_refs, 0, sizeof(cur_trans->delayed_refs));
  222. cur_trans->delayed_refs.href_root = RB_ROOT;
  223. cur_trans->delayed_refs.dirty_extent_root = RB_ROOT;
  224. atomic_set(&cur_trans->delayed_refs.num_entries, 0);
  225. /*
  226. * although the tree mod log is per file system and not per transaction,
  227. * the log must never go across transaction boundaries.
  228. */
  229. smp_mb();
  230. if (!list_empty(&fs_info->tree_mod_seq_list))
  231. WARN(1, KERN_ERR "BTRFS: tree_mod_seq_list not empty when creating a fresh transaction\n");
  232. if (!RB_EMPTY_ROOT(&fs_info->tree_mod_log))
  233. WARN(1, KERN_ERR "BTRFS: tree_mod_log rb tree not empty when creating a fresh transaction\n");
  234. atomic64_set(&fs_info->tree_mod_seq, 0);
  235. spin_lock_init(&cur_trans->delayed_refs.lock);
  236. INIT_LIST_HEAD(&cur_trans->pending_snapshots);
  237. INIT_LIST_HEAD(&cur_trans->pending_chunks);
  238. INIT_LIST_HEAD(&cur_trans->switch_commits);
  239. INIT_LIST_HEAD(&cur_trans->dirty_bgs);
  240. INIT_LIST_HEAD(&cur_trans->io_bgs);
  241. INIT_LIST_HEAD(&cur_trans->dropped_roots);
  242. mutex_init(&cur_trans->cache_write_mutex);
  243. cur_trans->num_dirty_bgs = 0;
  244. spin_lock_init(&cur_trans->dirty_bgs_lock);
  245. INIT_LIST_HEAD(&cur_trans->deleted_bgs);
  246. spin_lock_init(&cur_trans->dropped_roots_lock);
  247. list_add_tail(&cur_trans->list, &fs_info->trans_list);
  248. extent_io_tree_init(&cur_trans->dirty_pages,
  249. fs_info->btree_inode);
  250. fs_info->generation++;
  251. cur_trans->transid = fs_info->generation;
  252. fs_info->running_transaction = cur_trans;
  253. cur_trans->aborted = 0;
  254. spin_unlock(&fs_info->trans_lock);
  255. return 0;
  256. }
  257. /*
  258. * this does all the record keeping required to make sure that a reference
  259. * counted root is properly recorded in a given transaction. This is required
  260. * to make sure the old root from before we joined the transaction is deleted
  261. * when the transaction commits
  262. */
  263. static int record_root_in_trans(struct btrfs_trans_handle *trans,
  264. struct btrfs_root *root,
  265. int force)
  266. {
  267. struct btrfs_fs_info *fs_info = root->fs_info;
  268. if ((test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
  269. root->last_trans < trans->transid) || force) {
  270. WARN_ON(root == fs_info->extent_root);
  271. WARN_ON(!force && root->commit_root != root->node);
  272. /*
  273. * see below for IN_TRANS_SETUP usage rules
  274. * we have the reloc mutex held now, so there
  275. * is only one writer in this function
  276. */
  277. set_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state);
  278. /* make sure readers find IN_TRANS_SETUP before
  279. * they find our root->last_trans update
  280. */
  281. smp_wmb();
  282. spin_lock(&fs_info->fs_roots_radix_lock);
  283. if (root->last_trans == trans->transid && !force) {
  284. spin_unlock(&fs_info->fs_roots_radix_lock);
  285. return 0;
  286. }
  287. radix_tree_tag_set(&fs_info->fs_roots_radix,
  288. (unsigned long)root->root_key.objectid,
  289. BTRFS_ROOT_TRANS_TAG);
  290. spin_unlock(&fs_info->fs_roots_radix_lock);
  291. root->last_trans = trans->transid;
  292. /* this is pretty tricky. We don't want to
  293. * take the relocation lock in btrfs_record_root_in_trans
  294. * unless we're really doing the first setup for this root in
  295. * this transaction.
  296. *
  297. * Normally we'd use root->last_trans as a flag to decide
  298. * if we want to take the expensive mutex.
  299. *
  300. * But, we have to set root->last_trans before we
  301. * init the relocation root, otherwise, we trip over warnings
  302. * in ctree.c. The solution used here is to flag ourselves
  303. * with root IN_TRANS_SETUP. When this is 1, we're still
  304. * fixing up the reloc trees and everyone must wait.
  305. *
  306. * When this is zero, they can trust root->last_trans and fly
  307. * through btrfs_record_root_in_trans without having to take the
  308. * lock. smp_wmb() makes sure that all the writes above are
  309. * done before we pop in the zero below
  310. */
  311. btrfs_init_reloc_root(trans, root);
  312. smp_mb__before_atomic();
  313. clear_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state);
  314. }
  315. return 0;
  316. }
  317. void btrfs_add_dropped_root(struct btrfs_trans_handle *trans,
  318. struct btrfs_root *root)
  319. {
  320. struct btrfs_fs_info *fs_info = root->fs_info;
  321. struct btrfs_transaction *cur_trans = trans->transaction;
  322. /* Add ourselves to the transaction dropped list */
  323. spin_lock(&cur_trans->dropped_roots_lock);
  324. list_add_tail(&root->root_list, &cur_trans->dropped_roots);
  325. spin_unlock(&cur_trans->dropped_roots_lock);
  326. /* Make sure we don't try to update the root at commit time */
  327. spin_lock(&fs_info->fs_roots_radix_lock);
  328. radix_tree_tag_clear(&fs_info->fs_roots_radix,
  329. (unsigned long)root->root_key.objectid,
  330. BTRFS_ROOT_TRANS_TAG);
  331. spin_unlock(&fs_info->fs_roots_radix_lock);
  332. }
  333. int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
  334. struct btrfs_root *root)
  335. {
  336. struct btrfs_fs_info *fs_info = root->fs_info;
  337. if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
  338. return 0;
  339. /*
  340. * see record_root_in_trans for comments about IN_TRANS_SETUP usage
  341. * and barriers
  342. */
  343. smp_rmb();
  344. if (root->last_trans == trans->transid &&
  345. !test_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state))
  346. return 0;
  347. mutex_lock(&fs_info->reloc_mutex);
  348. record_root_in_trans(trans, root, 0);
  349. mutex_unlock(&fs_info->reloc_mutex);
  350. return 0;
  351. }
  352. static inline int is_transaction_blocked(struct btrfs_transaction *trans)
  353. {
  354. return (trans->state >= TRANS_STATE_BLOCKED &&
  355. trans->state < TRANS_STATE_UNBLOCKED &&
  356. !trans->aborted);
  357. }
  358. /* wait for commit against the current transaction to become unblocked
  359. * when this is done, it is safe to start a new transaction, but the current
  360. * transaction might not be fully on disk.
  361. */
  362. static void wait_current_trans(struct btrfs_fs_info *fs_info)
  363. {
  364. struct btrfs_transaction *cur_trans;
  365. spin_lock(&fs_info->trans_lock);
  366. cur_trans = fs_info->running_transaction;
  367. if (cur_trans && is_transaction_blocked(cur_trans)) {
  368. refcount_inc(&cur_trans->use_count);
  369. spin_unlock(&fs_info->trans_lock);
  370. wait_event(fs_info->transaction_wait,
  371. cur_trans->state >= TRANS_STATE_UNBLOCKED ||
  372. cur_trans->aborted);
  373. btrfs_put_transaction(cur_trans);
  374. } else {
  375. spin_unlock(&fs_info->trans_lock);
  376. }
  377. }
  378. static int may_wait_transaction(struct btrfs_fs_info *fs_info, int type)
  379. {
  380. if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
  381. return 0;
  382. if (type == TRANS_START)
  383. return 1;
  384. return 0;
  385. }
  386. static inline bool need_reserve_reloc_root(struct btrfs_root *root)
  387. {
  388. struct btrfs_fs_info *fs_info = root->fs_info;
  389. if (!fs_info->reloc_ctl ||
  390. !test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
  391. root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
  392. root->reloc_root)
  393. return false;
  394. return true;
  395. }
  396. static struct btrfs_trans_handle *
  397. start_transaction(struct btrfs_root *root, unsigned int num_items,
  398. unsigned int type, enum btrfs_reserve_flush_enum flush,
  399. bool enforce_qgroups)
  400. {
  401. struct btrfs_fs_info *fs_info = root->fs_info;
  402. struct btrfs_trans_handle *h;
  403. struct btrfs_transaction *cur_trans;
  404. u64 num_bytes = 0;
  405. u64 qgroup_reserved = 0;
  406. bool reloc_reserved = false;
  407. int ret;
  408. /* Send isn't supposed to start transactions. */
  409. ASSERT(current->journal_info != BTRFS_SEND_TRANS_STUB);
  410. if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
  411. return ERR_PTR(-EROFS);
  412. if (current->journal_info) {
  413. WARN_ON(type & TRANS_EXTWRITERS);
  414. h = current->journal_info;
  415. refcount_inc(&h->use_count);
  416. WARN_ON(refcount_read(&h->use_count) > 2);
  417. h->orig_rsv = h->block_rsv;
  418. h->block_rsv = NULL;
  419. goto got_it;
  420. }
  421. /*
  422. * Do the reservation before we join the transaction so we can do all
  423. * the appropriate flushing if need be.
  424. */
  425. if (num_items && root != fs_info->chunk_root) {
  426. qgroup_reserved = num_items * fs_info->nodesize;
  427. ret = btrfs_qgroup_reserve_meta_pertrans(root, qgroup_reserved,
  428. enforce_qgroups);
  429. if (ret)
  430. return ERR_PTR(ret);
  431. num_bytes = btrfs_calc_trans_metadata_size(fs_info, num_items);
  432. /*
  433. * Do the reservation for the relocation root creation
  434. */
  435. if (need_reserve_reloc_root(root)) {
  436. num_bytes += fs_info->nodesize;
  437. reloc_reserved = true;
  438. }
  439. ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
  440. num_bytes, flush);
  441. if (ret)
  442. goto reserve_fail;
  443. }
  444. again:
  445. h = kmem_cache_zalloc(btrfs_trans_handle_cachep, GFP_NOFS);
  446. if (!h) {
  447. ret = -ENOMEM;
  448. goto alloc_fail;
  449. }
  450. /*
  451. * If we are JOIN_NOLOCK we're already committing a transaction and
  452. * waiting on this guy, so we don't need to do the sb_start_intwrite
  453. * because we're already holding a ref. We need this because we could
  454. * have raced in and did an fsync() on a file which can kick a commit
  455. * and then we deadlock with somebody doing a freeze.
  456. *
  457. * If we are ATTACH, it means we just want to catch the current
  458. * transaction and commit it, so we needn't do sb_start_intwrite().
  459. */
  460. if (type & __TRANS_FREEZABLE)
  461. sb_start_intwrite(fs_info->sb);
  462. if (may_wait_transaction(fs_info, type))
  463. wait_current_trans(fs_info);
  464. do {
  465. ret = join_transaction(fs_info, type);
  466. if (ret == -EBUSY) {
  467. wait_current_trans(fs_info);
  468. if (unlikely(type == TRANS_ATTACH))
  469. ret = -ENOENT;
  470. }
  471. } while (ret == -EBUSY);
  472. if (ret < 0)
  473. goto join_fail;
  474. cur_trans = fs_info->running_transaction;
  475. h->transid = cur_trans->transid;
  476. h->transaction = cur_trans;
  477. h->root = root;
  478. refcount_set(&h->use_count, 1);
  479. h->fs_info = root->fs_info;
  480. h->type = type;
  481. h->can_flush_pending_bgs = true;
  482. INIT_LIST_HEAD(&h->new_bgs);
  483. smp_mb();
  484. if (cur_trans->state >= TRANS_STATE_BLOCKED &&
  485. may_wait_transaction(fs_info, type)) {
  486. current->journal_info = h;
  487. btrfs_commit_transaction(h);
  488. goto again;
  489. }
  490. if (num_bytes) {
  491. trace_btrfs_space_reservation(fs_info, "transaction",
  492. h->transid, num_bytes, 1);
  493. h->block_rsv = &fs_info->trans_block_rsv;
  494. h->bytes_reserved = num_bytes;
  495. h->reloc_reserved = reloc_reserved;
  496. }
  497. got_it:
  498. btrfs_record_root_in_trans(h, root);
  499. if (!current->journal_info)
  500. current->journal_info = h;
  501. return h;
  502. join_fail:
  503. if (type & __TRANS_FREEZABLE)
  504. sb_end_intwrite(fs_info->sb);
  505. kmem_cache_free(btrfs_trans_handle_cachep, h);
  506. alloc_fail:
  507. if (num_bytes)
  508. btrfs_block_rsv_release(fs_info, &fs_info->trans_block_rsv,
  509. num_bytes);
  510. reserve_fail:
  511. btrfs_qgroup_free_meta_pertrans(root, qgroup_reserved);
  512. return ERR_PTR(ret);
  513. }
  514. struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
  515. unsigned int num_items)
  516. {
  517. return start_transaction(root, num_items, TRANS_START,
  518. BTRFS_RESERVE_FLUSH_ALL, true);
  519. }
  520. struct btrfs_trans_handle *btrfs_start_transaction_fallback_global_rsv(
  521. struct btrfs_root *root,
  522. unsigned int num_items,
  523. int min_factor)
  524. {
  525. struct btrfs_fs_info *fs_info = root->fs_info;
  526. struct btrfs_trans_handle *trans;
  527. u64 num_bytes;
  528. int ret;
  529. /*
  530. * We have two callers: unlink and block group removal. The
  531. * former should succeed even if we will temporarily exceed
  532. * quota and the latter operates on the extent root so
  533. * qgroup enforcement is ignored anyway.
  534. */
  535. trans = start_transaction(root, num_items, TRANS_START,
  536. BTRFS_RESERVE_FLUSH_ALL, false);
  537. if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
  538. return trans;
  539. trans = btrfs_start_transaction(root, 0);
  540. if (IS_ERR(trans))
  541. return trans;
  542. num_bytes = btrfs_calc_trans_metadata_size(fs_info, num_items);
  543. ret = btrfs_cond_migrate_bytes(fs_info, &fs_info->trans_block_rsv,
  544. num_bytes, min_factor);
  545. if (ret) {
  546. btrfs_end_transaction(trans);
  547. return ERR_PTR(ret);
  548. }
  549. trans->block_rsv = &fs_info->trans_block_rsv;
  550. trans->bytes_reserved = num_bytes;
  551. trace_btrfs_space_reservation(fs_info, "transaction",
  552. trans->transid, num_bytes, 1);
  553. return trans;
  554. }
  555. struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root)
  556. {
  557. return start_transaction(root, 0, TRANS_JOIN, BTRFS_RESERVE_NO_FLUSH,
  558. true);
  559. }
  560. struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root)
  561. {
  562. return start_transaction(root, 0, TRANS_JOIN_NOLOCK,
  563. BTRFS_RESERVE_NO_FLUSH, true);
  564. }
  565. /*
  566. * btrfs_attach_transaction() - catch the running transaction
  567. *
  568. * It is used when we want to commit the current the transaction, but
  569. * don't want to start a new one.
  570. *
  571. * Note: If this function return -ENOENT, it just means there is no
  572. * running transaction. But it is possible that the inactive transaction
  573. * is still in the memory, not fully on disk. If you hope there is no
  574. * inactive transaction in the fs when -ENOENT is returned, you should
  575. * invoke
  576. * btrfs_attach_transaction_barrier()
  577. */
  578. struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root)
  579. {
  580. return start_transaction(root, 0, TRANS_ATTACH,
  581. BTRFS_RESERVE_NO_FLUSH, true);
  582. }
  583. /*
  584. * btrfs_attach_transaction_barrier() - catch the running transaction
  585. *
  586. * It is similar to the above function, the differentia is this one
  587. * will wait for all the inactive transactions until they fully
  588. * complete.
  589. */
  590. struct btrfs_trans_handle *
  591. btrfs_attach_transaction_barrier(struct btrfs_root *root)
  592. {
  593. struct btrfs_trans_handle *trans;
  594. trans = start_transaction(root, 0, TRANS_ATTACH,
  595. BTRFS_RESERVE_NO_FLUSH, true);
  596. if (IS_ERR(trans) && PTR_ERR(trans) == -ENOENT)
  597. btrfs_wait_for_commit(root->fs_info, 0);
  598. return trans;
  599. }
  600. /* wait for a transaction commit to be fully complete */
  601. static noinline void wait_for_commit(struct btrfs_transaction *commit)
  602. {
  603. wait_event(commit->commit_wait, commit->state == TRANS_STATE_COMPLETED);
  604. }
  605. int btrfs_wait_for_commit(struct btrfs_fs_info *fs_info, u64 transid)
  606. {
  607. struct btrfs_transaction *cur_trans = NULL, *t;
  608. int ret = 0;
  609. if (transid) {
  610. if (transid <= fs_info->last_trans_committed)
  611. goto out;
  612. /* find specified transaction */
  613. spin_lock(&fs_info->trans_lock);
  614. list_for_each_entry(t, &fs_info->trans_list, list) {
  615. if (t->transid == transid) {
  616. cur_trans = t;
  617. refcount_inc(&cur_trans->use_count);
  618. ret = 0;
  619. break;
  620. }
  621. if (t->transid > transid) {
  622. ret = 0;
  623. break;
  624. }
  625. }
  626. spin_unlock(&fs_info->trans_lock);
  627. /*
  628. * The specified transaction doesn't exist, or we
  629. * raced with btrfs_commit_transaction
  630. */
  631. if (!cur_trans) {
  632. if (transid > fs_info->last_trans_committed)
  633. ret = -EINVAL;
  634. goto out;
  635. }
  636. } else {
  637. /* find newest transaction that is committing | committed */
  638. spin_lock(&fs_info->trans_lock);
  639. list_for_each_entry_reverse(t, &fs_info->trans_list,
  640. list) {
  641. if (t->state >= TRANS_STATE_COMMIT_START) {
  642. if (t->state == TRANS_STATE_COMPLETED)
  643. break;
  644. cur_trans = t;
  645. refcount_inc(&cur_trans->use_count);
  646. break;
  647. }
  648. }
  649. spin_unlock(&fs_info->trans_lock);
  650. if (!cur_trans)
  651. goto out; /* nothing committing|committed */
  652. }
  653. wait_for_commit(cur_trans);
  654. btrfs_put_transaction(cur_trans);
  655. out:
  656. return ret;
  657. }
  658. void btrfs_throttle(struct btrfs_fs_info *fs_info)
  659. {
  660. wait_current_trans(fs_info);
  661. }
  662. static int should_end_transaction(struct btrfs_trans_handle *trans)
  663. {
  664. struct btrfs_fs_info *fs_info = trans->fs_info;
  665. if (btrfs_check_space_for_delayed_refs(trans, fs_info))
  666. return 1;
  667. return !!btrfs_block_rsv_check(&fs_info->global_block_rsv, 5);
  668. }
  669. int btrfs_should_end_transaction(struct btrfs_trans_handle *trans)
  670. {
  671. struct btrfs_transaction *cur_trans = trans->transaction;
  672. int updates;
  673. int err;
  674. smp_mb();
  675. if (cur_trans->state >= TRANS_STATE_BLOCKED ||
  676. cur_trans->delayed_refs.flushing)
  677. return 1;
  678. updates = trans->delayed_ref_updates;
  679. trans->delayed_ref_updates = 0;
  680. if (updates) {
  681. err = btrfs_run_delayed_refs(trans, updates * 2);
  682. if (err) /* Error code will also eval true */
  683. return err;
  684. }
  685. return should_end_transaction(trans);
  686. }
  687. static void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans)
  688. {
  689. struct btrfs_fs_info *fs_info = trans->fs_info;
  690. if (!trans->block_rsv) {
  691. ASSERT(!trans->bytes_reserved);
  692. return;
  693. }
  694. if (!trans->bytes_reserved)
  695. return;
  696. ASSERT(trans->block_rsv == &fs_info->trans_block_rsv);
  697. trace_btrfs_space_reservation(fs_info, "transaction",
  698. trans->transid, trans->bytes_reserved, 0);
  699. btrfs_block_rsv_release(fs_info, trans->block_rsv,
  700. trans->bytes_reserved);
  701. trans->bytes_reserved = 0;
  702. }
  703. static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
  704. int throttle)
  705. {
  706. struct btrfs_fs_info *info = trans->fs_info;
  707. struct btrfs_transaction *cur_trans = trans->transaction;
  708. u64 transid = trans->transid;
  709. unsigned long cur = trans->delayed_ref_updates;
  710. int lock = (trans->type != TRANS_JOIN_NOLOCK);
  711. int err = 0;
  712. int must_run_delayed_refs = 0;
  713. if (refcount_read(&trans->use_count) > 1) {
  714. refcount_dec(&trans->use_count);
  715. trans->block_rsv = trans->orig_rsv;
  716. return 0;
  717. }
  718. btrfs_trans_release_metadata(trans);
  719. trans->block_rsv = NULL;
  720. if (!list_empty(&trans->new_bgs))
  721. btrfs_create_pending_block_groups(trans);
  722. trans->delayed_ref_updates = 0;
  723. if (!trans->sync) {
  724. must_run_delayed_refs =
  725. btrfs_should_throttle_delayed_refs(trans, info);
  726. cur = max_t(unsigned long, cur, 32);
  727. /*
  728. * don't make the caller wait if they are from a NOLOCK
  729. * or ATTACH transaction, it will deadlock with commit
  730. */
  731. if (must_run_delayed_refs == 1 &&
  732. (trans->type & (__TRANS_JOIN_NOLOCK | __TRANS_ATTACH)))
  733. must_run_delayed_refs = 2;
  734. }
  735. btrfs_trans_release_metadata(trans);
  736. trans->block_rsv = NULL;
  737. if (!list_empty(&trans->new_bgs))
  738. btrfs_create_pending_block_groups(trans);
  739. btrfs_trans_release_chunk_metadata(trans);
  740. if (lock && should_end_transaction(trans) &&
  741. READ_ONCE(cur_trans->state) == TRANS_STATE_RUNNING) {
  742. spin_lock(&info->trans_lock);
  743. if (cur_trans->state == TRANS_STATE_RUNNING)
  744. cur_trans->state = TRANS_STATE_BLOCKED;
  745. spin_unlock(&info->trans_lock);
  746. }
  747. if (lock && READ_ONCE(cur_trans->state) == TRANS_STATE_BLOCKED) {
  748. if (throttle)
  749. return btrfs_commit_transaction(trans);
  750. else
  751. wake_up_process(info->transaction_kthread);
  752. }
  753. if (trans->type & __TRANS_FREEZABLE)
  754. sb_end_intwrite(info->sb);
  755. WARN_ON(cur_trans != info->running_transaction);
  756. WARN_ON(atomic_read(&cur_trans->num_writers) < 1);
  757. atomic_dec(&cur_trans->num_writers);
  758. extwriter_counter_dec(cur_trans, trans->type);
  759. cond_wake_up(&cur_trans->writer_wait);
  760. btrfs_put_transaction(cur_trans);
  761. if (current->journal_info == trans)
  762. current->journal_info = NULL;
  763. if (throttle)
  764. btrfs_run_delayed_iputs(info);
  765. if (trans->aborted ||
  766. test_bit(BTRFS_FS_STATE_ERROR, &info->fs_state)) {
  767. wake_up_process(info->transaction_kthread);
  768. err = -EIO;
  769. }
  770. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  771. if (must_run_delayed_refs) {
  772. btrfs_async_run_delayed_refs(info, cur, transid,
  773. must_run_delayed_refs == 1);
  774. }
  775. return err;
  776. }
  777. int btrfs_end_transaction(struct btrfs_trans_handle *trans)
  778. {
  779. return __btrfs_end_transaction(trans, 0);
  780. }
  781. int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans)
  782. {
  783. return __btrfs_end_transaction(trans, 1);
  784. }
  785. /*
  786. * when btree blocks are allocated, they have some corresponding bits set for
  787. * them in one of two extent_io trees. This is used to make sure all of
  788. * those extents are sent to disk but does not wait on them
  789. */
  790. int btrfs_write_marked_extents(struct btrfs_fs_info *fs_info,
  791. struct extent_io_tree *dirty_pages, int mark)
  792. {
  793. int err = 0;
  794. int werr = 0;
  795. struct address_space *mapping = fs_info->btree_inode->i_mapping;
  796. struct extent_state *cached_state = NULL;
  797. u64 start = 0;
  798. u64 end;
  799. atomic_inc(&BTRFS_I(fs_info->btree_inode)->sync_writers);
  800. while (!find_first_extent_bit(dirty_pages, start, &start, &end,
  801. mark, &cached_state)) {
  802. bool wait_writeback = false;
  803. err = convert_extent_bit(dirty_pages, start, end,
  804. EXTENT_NEED_WAIT,
  805. mark, &cached_state);
  806. /*
  807. * convert_extent_bit can return -ENOMEM, which is most of the
  808. * time a temporary error. So when it happens, ignore the error
  809. * and wait for writeback of this range to finish - because we
  810. * failed to set the bit EXTENT_NEED_WAIT for the range, a call
  811. * to __btrfs_wait_marked_extents() would not know that
  812. * writeback for this range started and therefore wouldn't
  813. * wait for it to finish - we don't want to commit a
  814. * superblock that points to btree nodes/leafs for which
  815. * writeback hasn't finished yet (and without errors).
  816. * We cleanup any entries left in the io tree when committing
  817. * the transaction (through clear_btree_io_tree()).
  818. */
  819. if (err == -ENOMEM) {
  820. err = 0;
  821. wait_writeback = true;
  822. }
  823. if (!err)
  824. err = filemap_fdatawrite_range(mapping, start, end);
  825. if (err)
  826. werr = err;
  827. else if (wait_writeback)
  828. werr = filemap_fdatawait_range(mapping, start, end);
  829. free_extent_state(cached_state);
  830. cached_state = NULL;
  831. cond_resched();
  832. start = end + 1;
  833. }
  834. atomic_dec(&BTRFS_I(fs_info->btree_inode)->sync_writers);
  835. return werr;
  836. }
  837. /*
  838. * when btree blocks are allocated, they have some corresponding bits set for
  839. * them in one of two extent_io trees. This is used to make sure all of
  840. * those extents are on disk for transaction or log commit. We wait
  841. * on all the pages and clear them from the dirty pages state tree
  842. */
  843. static int __btrfs_wait_marked_extents(struct btrfs_fs_info *fs_info,
  844. struct extent_io_tree *dirty_pages)
  845. {
  846. int err = 0;
  847. int werr = 0;
  848. struct address_space *mapping = fs_info->btree_inode->i_mapping;
  849. struct extent_state *cached_state = NULL;
  850. u64 start = 0;
  851. u64 end;
  852. while (!find_first_extent_bit(dirty_pages, start, &start, &end,
  853. EXTENT_NEED_WAIT, &cached_state)) {
  854. /*
  855. * Ignore -ENOMEM errors returned by clear_extent_bit().
  856. * When committing the transaction, we'll remove any entries
  857. * left in the io tree. For a log commit, we don't remove them
  858. * after committing the log because the tree can be accessed
  859. * concurrently - we do it only at transaction commit time when
  860. * it's safe to do it (through clear_btree_io_tree()).
  861. */
  862. err = clear_extent_bit(dirty_pages, start, end,
  863. EXTENT_NEED_WAIT, 0, 0, &cached_state);
  864. if (err == -ENOMEM)
  865. err = 0;
  866. if (!err)
  867. err = filemap_fdatawait_range(mapping, start, end);
  868. if (err)
  869. werr = err;
  870. free_extent_state(cached_state);
  871. cached_state = NULL;
  872. cond_resched();
  873. start = end + 1;
  874. }
  875. if (err)
  876. werr = err;
  877. return werr;
  878. }
  879. int btrfs_wait_extents(struct btrfs_fs_info *fs_info,
  880. struct extent_io_tree *dirty_pages)
  881. {
  882. bool errors = false;
  883. int err;
  884. err = __btrfs_wait_marked_extents(fs_info, dirty_pages);
  885. if (test_and_clear_bit(BTRFS_FS_BTREE_ERR, &fs_info->flags))
  886. errors = true;
  887. if (errors && !err)
  888. err = -EIO;
  889. return err;
  890. }
  891. int btrfs_wait_tree_log_extents(struct btrfs_root *log_root, int mark)
  892. {
  893. struct btrfs_fs_info *fs_info = log_root->fs_info;
  894. struct extent_io_tree *dirty_pages = &log_root->dirty_log_pages;
  895. bool errors = false;
  896. int err;
  897. ASSERT(log_root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID);
  898. err = __btrfs_wait_marked_extents(fs_info, dirty_pages);
  899. if ((mark & EXTENT_DIRTY) &&
  900. test_and_clear_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags))
  901. errors = true;
  902. if ((mark & EXTENT_NEW) &&
  903. test_and_clear_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags))
  904. errors = true;
  905. if (errors && !err)
  906. err = -EIO;
  907. return err;
  908. }
  909. /*
  910. * When btree blocks are allocated the corresponding extents are marked dirty.
  911. * This function ensures such extents are persisted on disk for transaction or
  912. * log commit.
  913. *
  914. * @trans: transaction whose dirty pages we'd like to write
  915. */
  916. static int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans)
  917. {
  918. int ret;
  919. int ret2;
  920. struct extent_io_tree *dirty_pages = &trans->transaction->dirty_pages;
  921. struct btrfs_fs_info *fs_info = trans->fs_info;
  922. struct blk_plug plug;
  923. blk_start_plug(&plug);
  924. ret = btrfs_write_marked_extents(fs_info, dirty_pages, EXTENT_DIRTY);
  925. blk_finish_plug(&plug);
  926. ret2 = btrfs_wait_extents(fs_info, dirty_pages);
  927. clear_btree_io_tree(&trans->transaction->dirty_pages);
  928. if (ret)
  929. return ret;
  930. else if (ret2)
  931. return ret2;
  932. else
  933. return 0;
  934. }
  935. /*
  936. * this is used to update the root pointer in the tree of tree roots.
  937. *
  938. * But, in the case of the extent allocation tree, updating the root
  939. * pointer may allocate blocks which may change the root of the extent
  940. * allocation tree.
  941. *
  942. * So, this loops and repeats and makes sure the cowonly root didn't
  943. * change while the root pointer was being updated in the metadata.
  944. */
  945. static int update_cowonly_root(struct btrfs_trans_handle *trans,
  946. struct btrfs_root *root)
  947. {
  948. int ret;
  949. u64 old_root_bytenr;
  950. u64 old_root_used;
  951. struct btrfs_fs_info *fs_info = root->fs_info;
  952. struct btrfs_root *tree_root = fs_info->tree_root;
  953. old_root_used = btrfs_root_used(&root->root_item);
  954. while (1) {
  955. old_root_bytenr = btrfs_root_bytenr(&root->root_item);
  956. if (old_root_bytenr == root->node->start &&
  957. old_root_used == btrfs_root_used(&root->root_item))
  958. break;
  959. btrfs_set_root_node(&root->root_item, root->node);
  960. ret = btrfs_update_root(trans, tree_root,
  961. &root->root_key,
  962. &root->root_item);
  963. if (ret)
  964. return ret;
  965. old_root_used = btrfs_root_used(&root->root_item);
  966. }
  967. return 0;
  968. }
  969. /*
  970. * update all the cowonly tree roots on disk
  971. *
  972. * The error handling in this function may not be obvious. Any of the
  973. * failures will cause the file system to go offline. We still need
  974. * to clean up the delayed refs.
  975. */
  976. static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans)
  977. {
  978. struct btrfs_fs_info *fs_info = trans->fs_info;
  979. struct list_head *dirty_bgs = &trans->transaction->dirty_bgs;
  980. struct list_head *io_bgs = &trans->transaction->io_bgs;
  981. struct list_head *next;
  982. struct extent_buffer *eb;
  983. int ret;
  984. eb = btrfs_lock_root_node(fs_info->tree_root);
  985. ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL,
  986. 0, &eb);
  987. btrfs_tree_unlock(eb);
  988. free_extent_buffer(eb);
  989. if (ret)
  990. return ret;
  991. ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
  992. if (ret)
  993. return ret;
  994. ret = btrfs_run_dev_stats(trans, fs_info);
  995. if (ret)
  996. return ret;
  997. ret = btrfs_run_dev_replace(trans, fs_info);
  998. if (ret)
  999. return ret;
  1000. ret = btrfs_run_qgroups(trans, fs_info);
  1001. if (ret)
  1002. return ret;
  1003. ret = btrfs_setup_space_cache(trans, fs_info);
  1004. if (ret)
  1005. return ret;
  1006. /* run_qgroups might have added some more refs */
  1007. ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
  1008. if (ret)
  1009. return ret;
  1010. again:
  1011. while (!list_empty(&fs_info->dirty_cowonly_roots)) {
  1012. struct btrfs_root *root;
  1013. next = fs_info->dirty_cowonly_roots.next;
  1014. list_del_init(next);
  1015. root = list_entry(next, struct btrfs_root, dirty_list);
  1016. clear_bit(BTRFS_ROOT_DIRTY, &root->state);
  1017. if (root != fs_info->extent_root)
  1018. list_add_tail(&root->dirty_list,
  1019. &trans->transaction->switch_commits);
  1020. ret = update_cowonly_root(trans, root);
  1021. if (ret)
  1022. return ret;
  1023. ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
  1024. if (ret)
  1025. return ret;
  1026. }
  1027. while (!list_empty(dirty_bgs) || !list_empty(io_bgs)) {
  1028. ret = btrfs_write_dirty_block_groups(trans, fs_info);
  1029. if (ret)
  1030. return ret;
  1031. ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
  1032. if (ret)
  1033. return ret;
  1034. }
  1035. if (!list_empty(&fs_info->dirty_cowonly_roots))
  1036. goto again;
  1037. list_add_tail(&fs_info->extent_root->dirty_list,
  1038. &trans->transaction->switch_commits);
  1039. btrfs_after_dev_replace_commit(fs_info);
  1040. return 0;
  1041. }
  1042. /*
  1043. * dead roots are old snapshots that need to be deleted. This allocates
  1044. * a dirty root struct and adds it into the list of dead roots that need to
  1045. * be deleted
  1046. */
  1047. void btrfs_add_dead_root(struct btrfs_root *root)
  1048. {
  1049. struct btrfs_fs_info *fs_info = root->fs_info;
  1050. spin_lock(&fs_info->trans_lock);
  1051. if (list_empty(&root->root_list))
  1052. list_add_tail(&root->root_list, &fs_info->dead_roots);
  1053. spin_unlock(&fs_info->trans_lock);
  1054. }
  1055. /*
  1056. * update all the cowonly tree roots on disk
  1057. */
  1058. static noinline int commit_fs_roots(struct btrfs_trans_handle *trans)
  1059. {
  1060. struct btrfs_fs_info *fs_info = trans->fs_info;
  1061. struct btrfs_root *gang[8];
  1062. int i;
  1063. int ret;
  1064. int err = 0;
  1065. spin_lock(&fs_info->fs_roots_radix_lock);
  1066. while (1) {
  1067. ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
  1068. (void **)gang, 0,
  1069. ARRAY_SIZE(gang),
  1070. BTRFS_ROOT_TRANS_TAG);
  1071. if (ret == 0)
  1072. break;
  1073. for (i = 0; i < ret; i++) {
  1074. struct btrfs_root *root = gang[i];
  1075. radix_tree_tag_clear(&fs_info->fs_roots_radix,
  1076. (unsigned long)root->root_key.objectid,
  1077. BTRFS_ROOT_TRANS_TAG);
  1078. spin_unlock(&fs_info->fs_roots_radix_lock);
  1079. btrfs_free_log(trans, root);
  1080. btrfs_update_reloc_root(trans, root);
  1081. btrfs_save_ino_cache(root, trans);
  1082. /* see comments in should_cow_block() */
  1083. clear_bit(BTRFS_ROOT_FORCE_COW, &root->state);
  1084. smp_mb__after_atomic();
  1085. if (root->commit_root != root->node) {
  1086. list_add_tail(&root->dirty_list,
  1087. &trans->transaction->switch_commits);
  1088. btrfs_set_root_node(&root->root_item,
  1089. root->node);
  1090. }
  1091. err = btrfs_update_root(trans, fs_info->tree_root,
  1092. &root->root_key,
  1093. &root->root_item);
  1094. spin_lock(&fs_info->fs_roots_radix_lock);
  1095. if (err)
  1096. break;
  1097. btrfs_qgroup_free_meta_all_pertrans(root);
  1098. }
  1099. }
  1100. spin_unlock(&fs_info->fs_roots_radix_lock);
  1101. return err;
  1102. }
  1103. /*
  1104. * defrag a given btree.
  1105. * Every leaf in the btree is read and defragged.
  1106. */
  1107. int btrfs_defrag_root(struct btrfs_root *root)
  1108. {
  1109. struct btrfs_fs_info *info = root->fs_info;
  1110. struct btrfs_trans_handle *trans;
  1111. int ret;
  1112. if (test_and_set_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state))
  1113. return 0;
  1114. while (1) {
  1115. trans = btrfs_start_transaction(root, 0);
  1116. if (IS_ERR(trans))
  1117. return PTR_ERR(trans);
  1118. ret = btrfs_defrag_leaves(trans, root);
  1119. btrfs_end_transaction(trans);
  1120. btrfs_btree_balance_dirty(info);
  1121. cond_resched();
  1122. if (btrfs_fs_closing(info) || ret != -EAGAIN)
  1123. break;
  1124. if (btrfs_defrag_cancelled(info)) {
  1125. btrfs_debug(info, "defrag_root cancelled");
  1126. ret = -EAGAIN;
  1127. break;
  1128. }
  1129. }
  1130. clear_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state);
  1131. return ret;
  1132. }
  1133. /*
  1134. * Do all special snapshot related qgroup dirty hack.
  1135. *
  1136. * Will do all needed qgroup inherit and dirty hack like switch commit
  1137. * roots inside one transaction and write all btree into disk, to make
  1138. * qgroup works.
  1139. */
  1140. static int qgroup_account_snapshot(struct btrfs_trans_handle *trans,
  1141. struct btrfs_root *src,
  1142. struct btrfs_root *parent,
  1143. struct btrfs_qgroup_inherit *inherit,
  1144. u64 dst_objectid)
  1145. {
  1146. struct btrfs_fs_info *fs_info = src->fs_info;
  1147. int ret;
  1148. /*
  1149. * Save some performance in the case that qgroups are not
  1150. * enabled. If this check races with the ioctl, rescan will
  1151. * kick in anyway.
  1152. */
  1153. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
  1154. return 0;
  1155. /*
  1156. * Ensure dirty @src will be commited. Or, after comming
  1157. * commit_fs_roots() and switch_commit_roots(), any dirty but not
  1158. * recorded root will never be updated again, causing an outdated root
  1159. * item.
  1160. */
  1161. record_root_in_trans(trans, src, 1);
  1162. /*
  1163. * We are going to commit transaction, see btrfs_commit_transaction()
  1164. * comment for reason locking tree_log_mutex
  1165. */
  1166. mutex_lock(&fs_info->tree_log_mutex);
  1167. ret = commit_fs_roots(trans);
  1168. if (ret)
  1169. goto out;
  1170. ret = btrfs_qgroup_account_extents(trans);
  1171. if (ret < 0)
  1172. goto out;
  1173. /* Now qgroup are all updated, we can inherit it to new qgroups */
  1174. ret = btrfs_qgroup_inherit(trans, fs_info,
  1175. src->root_key.objectid, dst_objectid,
  1176. inherit);
  1177. if (ret < 0)
  1178. goto out;
  1179. /*
  1180. * Now we do a simplified commit transaction, which will:
  1181. * 1) commit all subvolume and extent tree
  1182. * To ensure all subvolume and extent tree have a valid
  1183. * commit_root to accounting later insert_dir_item()
  1184. * 2) write all btree blocks onto disk
  1185. * This is to make sure later btree modification will be cowed
  1186. * Or commit_root can be populated and cause wrong qgroup numbers
  1187. * In this simplified commit, we don't really care about other trees
  1188. * like chunk and root tree, as they won't affect qgroup.
  1189. * And we don't write super to avoid half committed status.
  1190. */
  1191. ret = commit_cowonly_roots(trans);
  1192. if (ret)
  1193. goto out;
  1194. switch_commit_roots(trans->transaction);
  1195. ret = btrfs_write_and_wait_transaction(trans);
  1196. if (ret)
  1197. btrfs_handle_fs_error(fs_info, ret,
  1198. "Error while writing out transaction for qgroup");
  1199. out:
  1200. mutex_unlock(&fs_info->tree_log_mutex);
  1201. /*
  1202. * Force parent root to be updated, as we recorded it before so its
  1203. * last_trans == cur_transid.
  1204. * Or it won't be committed again onto disk after later
  1205. * insert_dir_item()
  1206. */
  1207. if (!ret)
  1208. record_root_in_trans(trans, parent, 1);
  1209. return ret;
  1210. }
  1211. /*
  1212. * new snapshots need to be created at a very specific time in the
  1213. * transaction commit. This does the actual creation.
  1214. *
  1215. * Note:
  1216. * If the error which may affect the commitment of the current transaction
  1217. * happens, we should return the error number. If the error which just affect
  1218. * the creation of the pending snapshots, just return 0.
  1219. */
  1220. static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
  1221. struct btrfs_pending_snapshot *pending)
  1222. {
  1223. struct btrfs_fs_info *fs_info = trans->fs_info;
  1224. struct btrfs_key key;
  1225. struct btrfs_root_item *new_root_item;
  1226. struct btrfs_root *tree_root = fs_info->tree_root;
  1227. struct btrfs_root *root = pending->root;
  1228. struct btrfs_root *parent_root;
  1229. struct btrfs_block_rsv *rsv;
  1230. struct inode *parent_inode;
  1231. struct btrfs_path *path;
  1232. struct btrfs_dir_item *dir_item;
  1233. struct dentry *dentry;
  1234. struct extent_buffer *tmp;
  1235. struct extent_buffer *old;
  1236. struct timespec64 cur_time;
  1237. int ret = 0;
  1238. u64 to_reserve = 0;
  1239. u64 index = 0;
  1240. u64 objectid;
  1241. u64 root_flags;
  1242. uuid_le new_uuid;
  1243. ASSERT(pending->path);
  1244. path = pending->path;
  1245. ASSERT(pending->root_item);
  1246. new_root_item = pending->root_item;
  1247. pending->error = btrfs_find_free_objectid(tree_root, &objectid);
  1248. if (pending->error)
  1249. goto no_free_objectid;
  1250. /*
  1251. * Make qgroup to skip current new snapshot's qgroupid, as it is
  1252. * accounted by later btrfs_qgroup_inherit().
  1253. */
  1254. btrfs_set_skip_qgroup(trans, objectid);
  1255. btrfs_reloc_pre_snapshot(pending, &to_reserve);
  1256. if (to_reserve > 0) {
  1257. pending->error = btrfs_block_rsv_add(root,
  1258. &pending->block_rsv,
  1259. to_reserve,
  1260. BTRFS_RESERVE_NO_FLUSH);
  1261. if (pending->error)
  1262. goto clear_skip_qgroup;
  1263. }
  1264. key.objectid = objectid;
  1265. key.offset = (u64)-1;
  1266. key.type = BTRFS_ROOT_ITEM_KEY;
  1267. rsv = trans->block_rsv;
  1268. trans->block_rsv = &pending->block_rsv;
  1269. trans->bytes_reserved = trans->block_rsv->reserved;
  1270. trace_btrfs_space_reservation(fs_info, "transaction",
  1271. trans->transid,
  1272. trans->bytes_reserved, 1);
  1273. dentry = pending->dentry;
  1274. parent_inode = pending->dir;
  1275. parent_root = BTRFS_I(parent_inode)->root;
  1276. record_root_in_trans(trans, parent_root, 0);
  1277. cur_time = current_time(parent_inode);
  1278. /*
  1279. * insert the directory item
  1280. */
  1281. ret = btrfs_set_inode_index(BTRFS_I(parent_inode), &index);
  1282. BUG_ON(ret); /* -ENOMEM */
  1283. /* check if there is a file/dir which has the same name. */
  1284. dir_item = btrfs_lookup_dir_item(NULL, parent_root, path,
  1285. btrfs_ino(BTRFS_I(parent_inode)),
  1286. dentry->d_name.name,
  1287. dentry->d_name.len, 0);
  1288. if (dir_item != NULL && !IS_ERR(dir_item)) {
  1289. pending->error = -EEXIST;
  1290. goto dir_item_existed;
  1291. } else if (IS_ERR(dir_item)) {
  1292. ret = PTR_ERR(dir_item);
  1293. btrfs_abort_transaction(trans, ret);
  1294. goto fail;
  1295. }
  1296. btrfs_release_path(path);
  1297. /*
  1298. * pull in the delayed directory update
  1299. * and the delayed inode item
  1300. * otherwise we corrupt the FS during
  1301. * snapshot
  1302. */
  1303. ret = btrfs_run_delayed_items(trans);
  1304. if (ret) { /* Transaction aborted */
  1305. btrfs_abort_transaction(trans, ret);
  1306. goto fail;
  1307. }
  1308. record_root_in_trans(trans, root, 0);
  1309. btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
  1310. memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
  1311. btrfs_check_and_init_root_item(new_root_item);
  1312. root_flags = btrfs_root_flags(new_root_item);
  1313. if (pending->readonly)
  1314. root_flags |= BTRFS_ROOT_SUBVOL_RDONLY;
  1315. else
  1316. root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY;
  1317. btrfs_set_root_flags(new_root_item, root_flags);
  1318. btrfs_set_root_generation_v2(new_root_item,
  1319. trans->transid);
  1320. uuid_le_gen(&new_uuid);
  1321. memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
  1322. memcpy(new_root_item->parent_uuid, root->root_item.uuid,
  1323. BTRFS_UUID_SIZE);
  1324. if (!(root_flags & BTRFS_ROOT_SUBVOL_RDONLY)) {
  1325. memset(new_root_item->received_uuid, 0,
  1326. sizeof(new_root_item->received_uuid));
  1327. memset(&new_root_item->stime, 0, sizeof(new_root_item->stime));
  1328. memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime));
  1329. btrfs_set_root_stransid(new_root_item, 0);
  1330. btrfs_set_root_rtransid(new_root_item, 0);
  1331. }
  1332. btrfs_set_stack_timespec_sec(&new_root_item->otime, cur_time.tv_sec);
  1333. btrfs_set_stack_timespec_nsec(&new_root_item->otime, cur_time.tv_nsec);
  1334. btrfs_set_root_otransid(new_root_item, trans->transid);
  1335. old = btrfs_lock_root_node(root);
  1336. ret = btrfs_cow_block(trans, root, old, NULL, 0, &old);
  1337. if (ret) {
  1338. btrfs_tree_unlock(old);
  1339. free_extent_buffer(old);
  1340. btrfs_abort_transaction(trans, ret);
  1341. goto fail;
  1342. }
  1343. btrfs_set_lock_blocking(old);
  1344. ret = btrfs_copy_root(trans, root, old, &tmp, objectid);
  1345. /* clean up in any case */
  1346. btrfs_tree_unlock(old);
  1347. free_extent_buffer(old);
  1348. if (ret) {
  1349. btrfs_abort_transaction(trans, ret);
  1350. goto fail;
  1351. }
  1352. /* see comments in should_cow_block() */
  1353. set_bit(BTRFS_ROOT_FORCE_COW, &root->state);
  1354. smp_wmb();
  1355. btrfs_set_root_node(new_root_item, tmp);
  1356. /* record when the snapshot was created in key.offset */
  1357. key.offset = trans->transid;
  1358. ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
  1359. btrfs_tree_unlock(tmp);
  1360. free_extent_buffer(tmp);
  1361. if (ret) {
  1362. btrfs_abort_transaction(trans, ret);
  1363. goto fail;
  1364. }
  1365. /*
  1366. * insert root back/forward references
  1367. */
  1368. ret = btrfs_add_root_ref(trans, fs_info, objectid,
  1369. parent_root->root_key.objectid,
  1370. btrfs_ino(BTRFS_I(parent_inode)), index,
  1371. dentry->d_name.name, dentry->d_name.len);
  1372. if (ret) {
  1373. btrfs_abort_transaction(trans, ret);
  1374. goto fail;
  1375. }
  1376. key.offset = (u64)-1;
  1377. pending->snap = btrfs_read_fs_root_no_name(fs_info, &key);
  1378. if (IS_ERR(pending->snap)) {
  1379. ret = PTR_ERR(pending->snap);
  1380. btrfs_abort_transaction(trans, ret);
  1381. goto fail;
  1382. }
  1383. ret = btrfs_reloc_post_snapshot(trans, pending);
  1384. if (ret) {
  1385. btrfs_abort_transaction(trans, ret);
  1386. goto fail;
  1387. }
  1388. ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
  1389. if (ret) {
  1390. btrfs_abort_transaction(trans, ret);
  1391. goto fail;
  1392. }
  1393. /*
  1394. * Do special qgroup accounting for snapshot, as we do some qgroup
  1395. * snapshot hack to do fast snapshot.
  1396. * To co-operate with that hack, we do hack again.
  1397. * Or snapshot will be greatly slowed down by a subtree qgroup rescan
  1398. */
  1399. ret = qgroup_account_snapshot(trans, root, parent_root,
  1400. pending->inherit, objectid);
  1401. if (ret < 0)
  1402. goto fail;
  1403. ret = btrfs_insert_dir_item(trans, parent_root,
  1404. dentry->d_name.name, dentry->d_name.len,
  1405. BTRFS_I(parent_inode), &key,
  1406. BTRFS_FT_DIR, index);
  1407. /* We have check then name at the beginning, so it is impossible. */
  1408. BUG_ON(ret == -EEXIST || ret == -EOVERFLOW);
  1409. if (ret) {
  1410. btrfs_abort_transaction(trans, ret);
  1411. goto fail;
  1412. }
  1413. btrfs_i_size_write(BTRFS_I(parent_inode), parent_inode->i_size +
  1414. dentry->d_name.len * 2);
  1415. parent_inode->i_mtime = parent_inode->i_ctime =
  1416. current_time(parent_inode);
  1417. ret = btrfs_update_inode_fallback(trans, parent_root, parent_inode);
  1418. if (ret) {
  1419. btrfs_abort_transaction(trans, ret);
  1420. goto fail;
  1421. }
  1422. ret = btrfs_uuid_tree_add(trans, new_uuid.b, BTRFS_UUID_KEY_SUBVOL,
  1423. objectid);
  1424. if (ret) {
  1425. btrfs_abort_transaction(trans, ret);
  1426. goto fail;
  1427. }
  1428. if (!btrfs_is_empty_uuid(new_root_item->received_uuid)) {
  1429. ret = btrfs_uuid_tree_add(trans, new_root_item->received_uuid,
  1430. BTRFS_UUID_KEY_RECEIVED_SUBVOL,
  1431. objectid);
  1432. if (ret && ret != -EEXIST) {
  1433. btrfs_abort_transaction(trans, ret);
  1434. goto fail;
  1435. }
  1436. }
  1437. ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
  1438. if (ret) {
  1439. btrfs_abort_transaction(trans, ret);
  1440. goto fail;
  1441. }
  1442. fail:
  1443. pending->error = ret;
  1444. dir_item_existed:
  1445. trans->block_rsv = rsv;
  1446. trans->bytes_reserved = 0;
  1447. clear_skip_qgroup:
  1448. btrfs_clear_skip_qgroup(trans);
  1449. no_free_objectid:
  1450. kfree(new_root_item);
  1451. pending->root_item = NULL;
  1452. btrfs_free_path(path);
  1453. pending->path = NULL;
  1454. return ret;
  1455. }
  1456. /*
  1457. * create all the snapshots we've scheduled for creation
  1458. */
  1459. static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans)
  1460. {
  1461. struct btrfs_pending_snapshot *pending, *next;
  1462. struct list_head *head = &trans->transaction->pending_snapshots;
  1463. int ret = 0;
  1464. list_for_each_entry_safe(pending, next, head, list) {
  1465. list_del(&pending->list);
  1466. ret = create_pending_snapshot(trans, pending);
  1467. if (ret)
  1468. break;
  1469. }
  1470. return ret;
  1471. }
  1472. static void update_super_roots(struct btrfs_fs_info *fs_info)
  1473. {
  1474. struct btrfs_root_item *root_item;
  1475. struct btrfs_super_block *super;
  1476. super = fs_info->super_copy;
  1477. root_item = &fs_info->chunk_root->root_item;
  1478. super->chunk_root = root_item->bytenr;
  1479. super->chunk_root_generation = root_item->generation;
  1480. super->chunk_root_level = root_item->level;
  1481. root_item = &fs_info->tree_root->root_item;
  1482. super->root = root_item->bytenr;
  1483. super->generation = root_item->generation;
  1484. super->root_level = root_item->level;
  1485. if (btrfs_test_opt(fs_info, SPACE_CACHE))
  1486. super->cache_generation = root_item->generation;
  1487. if (test_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags))
  1488. super->uuid_tree_generation = root_item->generation;
  1489. }
  1490. int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
  1491. {
  1492. struct btrfs_transaction *trans;
  1493. int ret = 0;
  1494. spin_lock(&info->trans_lock);
  1495. trans = info->running_transaction;
  1496. if (trans)
  1497. ret = (trans->state >= TRANS_STATE_COMMIT_START);
  1498. spin_unlock(&info->trans_lock);
  1499. return ret;
  1500. }
  1501. int btrfs_transaction_blocked(struct btrfs_fs_info *info)
  1502. {
  1503. struct btrfs_transaction *trans;
  1504. int ret = 0;
  1505. spin_lock(&info->trans_lock);
  1506. trans = info->running_transaction;
  1507. if (trans)
  1508. ret = is_transaction_blocked(trans);
  1509. spin_unlock(&info->trans_lock);
  1510. return ret;
  1511. }
  1512. /*
  1513. * wait for the current transaction commit to start and block subsequent
  1514. * transaction joins
  1515. */
  1516. static void wait_current_trans_commit_start(struct btrfs_fs_info *fs_info,
  1517. struct btrfs_transaction *trans)
  1518. {
  1519. wait_event(fs_info->transaction_blocked_wait,
  1520. trans->state >= TRANS_STATE_COMMIT_START || trans->aborted);
  1521. }
  1522. /*
  1523. * wait for the current transaction to start and then become unblocked.
  1524. * caller holds ref.
  1525. */
  1526. static void wait_current_trans_commit_start_and_unblock(
  1527. struct btrfs_fs_info *fs_info,
  1528. struct btrfs_transaction *trans)
  1529. {
  1530. wait_event(fs_info->transaction_wait,
  1531. trans->state >= TRANS_STATE_UNBLOCKED || trans->aborted);
  1532. }
  1533. /*
  1534. * commit transactions asynchronously. once btrfs_commit_transaction_async
  1535. * returns, any subsequent transaction will not be allowed to join.
  1536. */
  1537. struct btrfs_async_commit {
  1538. struct btrfs_trans_handle *newtrans;
  1539. struct work_struct work;
  1540. };
  1541. static void do_async_commit(struct work_struct *work)
  1542. {
  1543. struct btrfs_async_commit *ac =
  1544. container_of(work, struct btrfs_async_commit, work);
  1545. /*
  1546. * We've got freeze protection passed with the transaction.
  1547. * Tell lockdep about it.
  1548. */
  1549. if (ac->newtrans->type & __TRANS_FREEZABLE)
  1550. __sb_writers_acquired(ac->newtrans->fs_info->sb, SB_FREEZE_FS);
  1551. current->journal_info = ac->newtrans;
  1552. btrfs_commit_transaction(ac->newtrans);
  1553. kfree(ac);
  1554. }
  1555. int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
  1556. int wait_for_unblock)
  1557. {
  1558. struct btrfs_fs_info *fs_info = trans->fs_info;
  1559. struct btrfs_async_commit *ac;
  1560. struct btrfs_transaction *cur_trans;
  1561. ac = kmalloc(sizeof(*ac), GFP_NOFS);
  1562. if (!ac)
  1563. return -ENOMEM;
  1564. INIT_WORK(&ac->work, do_async_commit);
  1565. ac->newtrans = btrfs_join_transaction(trans->root);
  1566. if (IS_ERR(ac->newtrans)) {
  1567. int err = PTR_ERR(ac->newtrans);
  1568. kfree(ac);
  1569. return err;
  1570. }
  1571. /* take transaction reference */
  1572. cur_trans = trans->transaction;
  1573. refcount_inc(&cur_trans->use_count);
  1574. btrfs_end_transaction(trans);
  1575. /*
  1576. * Tell lockdep we've released the freeze rwsem, since the
  1577. * async commit thread will be the one to unlock it.
  1578. */
  1579. if (ac->newtrans->type & __TRANS_FREEZABLE)
  1580. __sb_writers_release(fs_info->sb, SB_FREEZE_FS);
  1581. schedule_work(&ac->work);
  1582. /* wait for transaction to start and unblock */
  1583. if (wait_for_unblock)
  1584. wait_current_trans_commit_start_and_unblock(fs_info, cur_trans);
  1585. else
  1586. wait_current_trans_commit_start(fs_info, cur_trans);
  1587. if (current->journal_info == trans)
  1588. current->journal_info = NULL;
  1589. btrfs_put_transaction(cur_trans);
  1590. return 0;
  1591. }
  1592. static void cleanup_transaction(struct btrfs_trans_handle *trans, int err)
  1593. {
  1594. struct btrfs_fs_info *fs_info = trans->fs_info;
  1595. struct btrfs_transaction *cur_trans = trans->transaction;
  1596. DEFINE_WAIT(wait);
  1597. WARN_ON(refcount_read(&trans->use_count) > 1);
  1598. btrfs_abort_transaction(trans, err);
  1599. spin_lock(&fs_info->trans_lock);
  1600. /*
  1601. * If the transaction is removed from the list, it means this
  1602. * transaction has been committed successfully, so it is impossible
  1603. * to call the cleanup function.
  1604. */
  1605. BUG_ON(list_empty(&cur_trans->list));
  1606. list_del_init(&cur_trans->list);
  1607. if (cur_trans == fs_info->running_transaction) {
  1608. cur_trans->state = TRANS_STATE_COMMIT_DOING;
  1609. spin_unlock(&fs_info->trans_lock);
  1610. wait_event(cur_trans->writer_wait,
  1611. atomic_read(&cur_trans->num_writers) == 1);
  1612. spin_lock(&fs_info->trans_lock);
  1613. }
  1614. spin_unlock(&fs_info->trans_lock);
  1615. btrfs_cleanup_one_transaction(trans->transaction, fs_info);
  1616. spin_lock(&fs_info->trans_lock);
  1617. if (cur_trans == fs_info->running_transaction)
  1618. fs_info->running_transaction = NULL;
  1619. spin_unlock(&fs_info->trans_lock);
  1620. if (trans->type & __TRANS_FREEZABLE)
  1621. sb_end_intwrite(fs_info->sb);
  1622. btrfs_put_transaction(cur_trans);
  1623. btrfs_put_transaction(cur_trans);
  1624. trace_btrfs_transaction_commit(trans->root);
  1625. if (current->journal_info == trans)
  1626. current->journal_info = NULL;
  1627. btrfs_scrub_cancel(fs_info);
  1628. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  1629. }
  1630. static inline int btrfs_start_delalloc_flush(struct btrfs_fs_info *fs_info)
  1631. {
  1632. /*
  1633. * We use writeback_inodes_sb here because if we used
  1634. * btrfs_start_delalloc_roots we would deadlock with fs freeze.
  1635. * Currently are holding the fs freeze lock, if we do an async flush
  1636. * we'll do btrfs_join_transaction() and deadlock because we need to
  1637. * wait for the fs freeze lock. Using the direct flushing we benefit
  1638. * from already being in a transaction and our join_transaction doesn't
  1639. * have to re-take the fs freeze lock.
  1640. */
  1641. if (btrfs_test_opt(fs_info, FLUSHONCOMMIT))
  1642. writeback_inodes_sb(fs_info->sb, WB_REASON_SYNC);
  1643. return 0;
  1644. }
  1645. static inline void btrfs_wait_delalloc_flush(struct btrfs_fs_info *fs_info)
  1646. {
  1647. if (btrfs_test_opt(fs_info, FLUSHONCOMMIT))
  1648. btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
  1649. }
  1650. static inline void
  1651. btrfs_wait_pending_ordered(struct btrfs_transaction *cur_trans)
  1652. {
  1653. wait_event(cur_trans->pending_wait,
  1654. atomic_read(&cur_trans->pending_ordered) == 0);
  1655. }
  1656. int btrfs_commit_transaction(struct btrfs_trans_handle *trans)
  1657. {
  1658. struct btrfs_fs_info *fs_info = trans->fs_info;
  1659. struct btrfs_transaction *cur_trans = trans->transaction;
  1660. struct btrfs_transaction *prev_trans = NULL;
  1661. int ret;
  1662. /* Stop the commit early if ->aborted is set */
  1663. if (unlikely(READ_ONCE(cur_trans->aborted))) {
  1664. ret = cur_trans->aborted;
  1665. btrfs_end_transaction(trans);
  1666. return ret;
  1667. }
  1668. /* make a pass through all the delayed refs we have so far
  1669. * any runnings procs may add more while we are here
  1670. */
  1671. ret = btrfs_run_delayed_refs(trans, 0);
  1672. if (ret) {
  1673. btrfs_end_transaction(trans);
  1674. return ret;
  1675. }
  1676. btrfs_trans_release_metadata(trans);
  1677. trans->block_rsv = NULL;
  1678. cur_trans = trans->transaction;
  1679. /*
  1680. * set the flushing flag so procs in this transaction have to
  1681. * start sending their work down.
  1682. */
  1683. cur_trans->delayed_refs.flushing = 1;
  1684. smp_wmb();
  1685. if (!list_empty(&trans->new_bgs))
  1686. btrfs_create_pending_block_groups(trans);
  1687. ret = btrfs_run_delayed_refs(trans, 0);
  1688. if (ret) {
  1689. btrfs_end_transaction(trans);
  1690. return ret;
  1691. }
  1692. if (!test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &cur_trans->flags)) {
  1693. int run_it = 0;
  1694. /* this mutex is also taken before trying to set
  1695. * block groups readonly. We need to make sure
  1696. * that nobody has set a block group readonly
  1697. * after a extents from that block group have been
  1698. * allocated for cache files. btrfs_set_block_group_ro
  1699. * will wait for the transaction to commit if it
  1700. * finds BTRFS_TRANS_DIRTY_BG_RUN set.
  1701. *
  1702. * The BTRFS_TRANS_DIRTY_BG_RUN flag is also used to make sure
  1703. * only one process starts all the block group IO. It wouldn't
  1704. * hurt to have more than one go through, but there's no
  1705. * real advantage to it either.
  1706. */
  1707. mutex_lock(&fs_info->ro_block_group_mutex);
  1708. if (!test_and_set_bit(BTRFS_TRANS_DIRTY_BG_RUN,
  1709. &cur_trans->flags))
  1710. run_it = 1;
  1711. mutex_unlock(&fs_info->ro_block_group_mutex);
  1712. if (run_it) {
  1713. ret = btrfs_start_dirty_block_groups(trans);
  1714. if (ret) {
  1715. btrfs_end_transaction(trans);
  1716. return ret;
  1717. }
  1718. }
  1719. }
  1720. spin_lock(&fs_info->trans_lock);
  1721. if (cur_trans->state >= TRANS_STATE_COMMIT_START) {
  1722. spin_unlock(&fs_info->trans_lock);
  1723. refcount_inc(&cur_trans->use_count);
  1724. ret = btrfs_end_transaction(trans);
  1725. wait_for_commit(cur_trans);
  1726. if (unlikely(cur_trans->aborted))
  1727. ret = cur_trans->aborted;
  1728. btrfs_put_transaction(cur_trans);
  1729. return ret;
  1730. }
  1731. cur_trans->state = TRANS_STATE_COMMIT_START;
  1732. wake_up(&fs_info->transaction_blocked_wait);
  1733. if (cur_trans->list.prev != &fs_info->trans_list) {
  1734. prev_trans = list_entry(cur_trans->list.prev,
  1735. struct btrfs_transaction, list);
  1736. if (prev_trans->state != TRANS_STATE_COMPLETED) {
  1737. refcount_inc(&prev_trans->use_count);
  1738. spin_unlock(&fs_info->trans_lock);
  1739. wait_for_commit(prev_trans);
  1740. ret = prev_trans->aborted;
  1741. btrfs_put_transaction(prev_trans);
  1742. if (ret)
  1743. goto cleanup_transaction;
  1744. } else {
  1745. spin_unlock(&fs_info->trans_lock);
  1746. }
  1747. } else {
  1748. spin_unlock(&fs_info->trans_lock);
  1749. }
  1750. extwriter_counter_dec(cur_trans, trans->type);
  1751. ret = btrfs_start_delalloc_flush(fs_info);
  1752. if (ret)
  1753. goto cleanup_transaction;
  1754. ret = btrfs_run_delayed_items(trans);
  1755. if (ret)
  1756. goto cleanup_transaction;
  1757. wait_event(cur_trans->writer_wait,
  1758. extwriter_counter_read(cur_trans) == 0);
  1759. /* some pending stuffs might be added after the previous flush. */
  1760. ret = btrfs_run_delayed_items(trans);
  1761. if (ret)
  1762. goto cleanup_transaction;
  1763. btrfs_wait_delalloc_flush(fs_info);
  1764. btrfs_wait_pending_ordered(cur_trans);
  1765. btrfs_scrub_pause(fs_info);
  1766. /*
  1767. * Ok now we need to make sure to block out any other joins while we
  1768. * commit the transaction. We could have started a join before setting
  1769. * COMMIT_DOING so make sure to wait for num_writers to == 1 again.
  1770. */
  1771. spin_lock(&fs_info->trans_lock);
  1772. cur_trans->state = TRANS_STATE_COMMIT_DOING;
  1773. spin_unlock(&fs_info->trans_lock);
  1774. wait_event(cur_trans->writer_wait,
  1775. atomic_read(&cur_trans->num_writers) == 1);
  1776. /* ->aborted might be set after the previous check, so check it */
  1777. if (unlikely(READ_ONCE(cur_trans->aborted))) {
  1778. ret = cur_trans->aborted;
  1779. goto scrub_continue;
  1780. }
  1781. /*
  1782. * the reloc mutex makes sure that we stop
  1783. * the balancing code from coming in and moving
  1784. * extents around in the middle of the commit
  1785. */
  1786. mutex_lock(&fs_info->reloc_mutex);
  1787. /*
  1788. * We needn't worry about the delayed items because we will
  1789. * deal with them in create_pending_snapshot(), which is the
  1790. * core function of the snapshot creation.
  1791. */
  1792. ret = create_pending_snapshots(trans);
  1793. if (ret) {
  1794. mutex_unlock(&fs_info->reloc_mutex);
  1795. goto scrub_continue;
  1796. }
  1797. /*
  1798. * We insert the dir indexes of the snapshots and update the inode
  1799. * of the snapshots' parents after the snapshot creation, so there
  1800. * are some delayed items which are not dealt with. Now deal with
  1801. * them.
  1802. *
  1803. * We needn't worry that this operation will corrupt the snapshots,
  1804. * because all the tree which are snapshoted will be forced to COW
  1805. * the nodes and leaves.
  1806. */
  1807. ret = btrfs_run_delayed_items(trans);
  1808. if (ret) {
  1809. mutex_unlock(&fs_info->reloc_mutex);
  1810. goto scrub_continue;
  1811. }
  1812. ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
  1813. if (ret) {
  1814. mutex_unlock(&fs_info->reloc_mutex);
  1815. goto scrub_continue;
  1816. }
  1817. /*
  1818. * make sure none of the code above managed to slip in a
  1819. * delayed item
  1820. */
  1821. btrfs_assert_delayed_root_empty(fs_info);
  1822. WARN_ON(cur_trans != trans->transaction);
  1823. /* btrfs_commit_tree_roots is responsible for getting the
  1824. * various roots consistent with each other. Every pointer
  1825. * in the tree of tree roots has to point to the most up to date
  1826. * root for every subvolume and other tree. So, we have to keep
  1827. * the tree logging code from jumping in and changing any
  1828. * of the trees.
  1829. *
  1830. * At this point in the commit, there can't be any tree-log
  1831. * writers, but a little lower down we drop the trans mutex
  1832. * and let new people in. By holding the tree_log_mutex
  1833. * from now until after the super is written, we avoid races
  1834. * with the tree-log code.
  1835. */
  1836. mutex_lock(&fs_info->tree_log_mutex);
  1837. ret = commit_fs_roots(trans);
  1838. if (ret) {
  1839. mutex_unlock(&fs_info->tree_log_mutex);
  1840. mutex_unlock(&fs_info->reloc_mutex);
  1841. goto scrub_continue;
  1842. }
  1843. /*
  1844. * Since the transaction is done, we can apply the pending changes
  1845. * before the next transaction.
  1846. */
  1847. btrfs_apply_pending_changes(fs_info);
  1848. /* commit_fs_roots gets rid of all the tree log roots, it is now
  1849. * safe to free the root of tree log roots
  1850. */
  1851. btrfs_free_log_root_tree(trans, fs_info);
  1852. /*
  1853. * commit_fs_roots() can call btrfs_save_ino_cache(), which generates
  1854. * new delayed refs. Must handle them or qgroup can be wrong.
  1855. */
  1856. ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
  1857. if (ret) {
  1858. mutex_unlock(&fs_info->tree_log_mutex);
  1859. mutex_unlock(&fs_info->reloc_mutex);
  1860. goto scrub_continue;
  1861. }
  1862. /*
  1863. * Since fs roots are all committed, we can get a quite accurate
  1864. * new_roots. So let's do quota accounting.
  1865. */
  1866. ret = btrfs_qgroup_account_extents(trans);
  1867. if (ret < 0) {
  1868. mutex_unlock(&fs_info->tree_log_mutex);
  1869. mutex_unlock(&fs_info->reloc_mutex);
  1870. goto scrub_continue;
  1871. }
  1872. ret = commit_cowonly_roots(trans);
  1873. if (ret) {
  1874. mutex_unlock(&fs_info->tree_log_mutex);
  1875. mutex_unlock(&fs_info->reloc_mutex);
  1876. goto scrub_continue;
  1877. }
  1878. /*
  1879. * The tasks which save the space cache and inode cache may also
  1880. * update ->aborted, check it.
  1881. */
  1882. if (unlikely(READ_ONCE(cur_trans->aborted))) {
  1883. ret = cur_trans->aborted;
  1884. mutex_unlock(&fs_info->tree_log_mutex);
  1885. mutex_unlock(&fs_info->reloc_mutex);
  1886. goto scrub_continue;
  1887. }
  1888. btrfs_prepare_extent_commit(fs_info);
  1889. cur_trans = fs_info->running_transaction;
  1890. btrfs_set_root_node(&fs_info->tree_root->root_item,
  1891. fs_info->tree_root->node);
  1892. list_add_tail(&fs_info->tree_root->dirty_list,
  1893. &cur_trans->switch_commits);
  1894. btrfs_set_root_node(&fs_info->chunk_root->root_item,
  1895. fs_info->chunk_root->node);
  1896. list_add_tail(&fs_info->chunk_root->dirty_list,
  1897. &cur_trans->switch_commits);
  1898. switch_commit_roots(cur_trans);
  1899. ASSERT(list_empty(&cur_trans->dirty_bgs));
  1900. ASSERT(list_empty(&cur_trans->io_bgs));
  1901. update_super_roots(fs_info);
  1902. btrfs_set_super_log_root(fs_info->super_copy, 0);
  1903. btrfs_set_super_log_root_level(fs_info->super_copy, 0);
  1904. memcpy(fs_info->super_for_commit, fs_info->super_copy,
  1905. sizeof(*fs_info->super_copy));
  1906. btrfs_update_commit_device_size(fs_info);
  1907. btrfs_update_commit_device_bytes_used(cur_trans);
  1908. clear_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags);
  1909. clear_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags);
  1910. btrfs_trans_release_chunk_metadata(trans);
  1911. spin_lock(&fs_info->trans_lock);
  1912. cur_trans->state = TRANS_STATE_UNBLOCKED;
  1913. fs_info->running_transaction = NULL;
  1914. spin_unlock(&fs_info->trans_lock);
  1915. mutex_unlock(&fs_info->reloc_mutex);
  1916. wake_up(&fs_info->transaction_wait);
  1917. ret = btrfs_write_and_wait_transaction(trans);
  1918. if (ret) {
  1919. btrfs_handle_fs_error(fs_info, ret,
  1920. "Error while writing out transaction");
  1921. mutex_unlock(&fs_info->tree_log_mutex);
  1922. goto scrub_continue;
  1923. }
  1924. ret = write_all_supers(fs_info, 0);
  1925. /*
  1926. * the super is written, we can safely allow the tree-loggers
  1927. * to go about their business
  1928. */
  1929. mutex_unlock(&fs_info->tree_log_mutex);
  1930. if (ret)
  1931. goto scrub_continue;
  1932. btrfs_finish_extent_commit(trans);
  1933. if (test_bit(BTRFS_TRANS_HAVE_FREE_BGS, &cur_trans->flags))
  1934. btrfs_clear_space_info_full(fs_info);
  1935. fs_info->last_trans_committed = cur_trans->transid;
  1936. /*
  1937. * We needn't acquire the lock here because there is no other task
  1938. * which can change it.
  1939. */
  1940. cur_trans->state = TRANS_STATE_COMPLETED;
  1941. wake_up(&cur_trans->commit_wait);
  1942. clear_bit(BTRFS_FS_NEED_ASYNC_COMMIT, &fs_info->flags);
  1943. spin_lock(&fs_info->trans_lock);
  1944. list_del_init(&cur_trans->list);
  1945. spin_unlock(&fs_info->trans_lock);
  1946. btrfs_put_transaction(cur_trans);
  1947. btrfs_put_transaction(cur_trans);
  1948. if (trans->type & __TRANS_FREEZABLE)
  1949. sb_end_intwrite(fs_info->sb);
  1950. trace_btrfs_transaction_commit(trans->root);
  1951. btrfs_scrub_continue(fs_info);
  1952. if (current->journal_info == trans)
  1953. current->journal_info = NULL;
  1954. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  1955. /*
  1956. * If fs has been frozen, we can not handle delayed iputs, otherwise
  1957. * it'll result in deadlock about SB_FREEZE_FS.
  1958. */
  1959. if (current != fs_info->transaction_kthread &&
  1960. current != fs_info->cleaner_kthread &&
  1961. !test_bit(BTRFS_FS_FROZEN, &fs_info->flags))
  1962. btrfs_run_delayed_iputs(fs_info);
  1963. return ret;
  1964. scrub_continue:
  1965. btrfs_scrub_continue(fs_info);
  1966. cleanup_transaction:
  1967. btrfs_trans_release_metadata(trans);
  1968. btrfs_trans_release_chunk_metadata(trans);
  1969. trans->block_rsv = NULL;
  1970. btrfs_warn(fs_info, "Skipping commit of aborted transaction.");
  1971. if (current->journal_info == trans)
  1972. current->journal_info = NULL;
  1973. cleanup_transaction(trans, ret);
  1974. return ret;
  1975. }
  1976. /*
  1977. * return < 0 if error
  1978. * 0 if there are no more dead_roots at the time of call
  1979. * 1 there are more to be processed, call me again
  1980. *
  1981. * The return value indicates there are certainly more snapshots to delete, but
  1982. * if there comes a new one during processing, it may return 0. We don't mind,
  1983. * because btrfs_commit_super will poke cleaner thread and it will process it a
  1984. * few seconds later.
  1985. */
  1986. int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root)
  1987. {
  1988. int ret;
  1989. struct btrfs_fs_info *fs_info = root->fs_info;
  1990. spin_lock(&fs_info->trans_lock);
  1991. if (list_empty(&fs_info->dead_roots)) {
  1992. spin_unlock(&fs_info->trans_lock);
  1993. return 0;
  1994. }
  1995. root = list_first_entry(&fs_info->dead_roots,
  1996. struct btrfs_root, root_list);
  1997. list_del_init(&root->root_list);
  1998. spin_unlock(&fs_info->trans_lock);
  1999. btrfs_debug(fs_info, "cleaner removing %llu", root->objectid);
  2000. btrfs_kill_all_delayed_nodes(root);
  2001. if (btrfs_header_backref_rev(root->node) <
  2002. BTRFS_MIXED_BACKREF_REV)
  2003. ret = btrfs_drop_snapshot(root, NULL, 0, 0);
  2004. else
  2005. ret = btrfs_drop_snapshot(root, NULL, 1, 0);
  2006. return (ret < 0) ? 0 : 1;
  2007. }
  2008. void btrfs_apply_pending_changes(struct btrfs_fs_info *fs_info)
  2009. {
  2010. unsigned long prev;
  2011. unsigned long bit;
  2012. prev = xchg(&fs_info->pending_changes, 0);
  2013. if (!prev)
  2014. return;
  2015. bit = 1 << BTRFS_PENDING_SET_INODE_MAP_CACHE;
  2016. if (prev & bit)
  2017. btrfs_set_opt(fs_info->mount_opt, INODE_MAP_CACHE);
  2018. prev &= ~bit;
  2019. bit = 1 << BTRFS_PENDING_CLEAR_INODE_MAP_CACHE;
  2020. if (prev & bit)
  2021. btrfs_clear_opt(fs_info->mount_opt, INODE_MAP_CACHE);
  2022. prev &= ~bit;
  2023. bit = 1 << BTRFS_PENDING_COMMIT;
  2024. if (prev & bit)
  2025. btrfs_debug(fs_info, "pending commit done");
  2026. prev &= ~bit;
  2027. if (prev)
  2028. btrfs_warn(fs_info,
  2029. "unknown pending changes left 0x%lx, ignoring", prev);
  2030. }