transaction.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381
  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. /*
  760. * Make sure counter is updated before we wake up waiters.
  761. */
  762. smp_mb();
  763. if (waitqueue_active(&cur_trans->writer_wait))
  764. wake_up(&cur_trans->writer_wait);
  765. btrfs_put_transaction(cur_trans);
  766. if (current->journal_info == trans)
  767. current->journal_info = NULL;
  768. if (throttle)
  769. btrfs_run_delayed_iputs(info);
  770. if (trans->aborted ||
  771. test_bit(BTRFS_FS_STATE_ERROR, &info->fs_state)) {
  772. wake_up_process(info->transaction_kthread);
  773. err = -EIO;
  774. }
  775. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  776. if (must_run_delayed_refs) {
  777. btrfs_async_run_delayed_refs(info, cur, transid,
  778. must_run_delayed_refs == 1);
  779. }
  780. return err;
  781. }
  782. int btrfs_end_transaction(struct btrfs_trans_handle *trans)
  783. {
  784. return __btrfs_end_transaction(trans, 0);
  785. }
  786. int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans)
  787. {
  788. return __btrfs_end_transaction(trans, 1);
  789. }
  790. /*
  791. * when btree blocks are allocated, they have some corresponding bits set for
  792. * them in one of two extent_io trees. This is used to make sure all of
  793. * those extents are sent to disk but does not wait on them
  794. */
  795. int btrfs_write_marked_extents(struct btrfs_fs_info *fs_info,
  796. struct extent_io_tree *dirty_pages, int mark)
  797. {
  798. int err = 0;
  799. int werr = 0;
  800. struct address_space *mapping = fs_info->btree_inode->i_mapping;
  801. struct extent_state *cached_state = NULL;
  802. u64 start = 0;
  803. u64 end;
  804. atomic_inc(&BTRFS_I(fs_info->btree_inode)->sync_writers);
  805. while (!find_first_extent_bit(dirty_pages, start, &start, &end,
  806. mark, &cached_state)) {
  807. bool wait_writeback = false;
  808. err = convert_extent_bit(dirty_pages, start, end,
  809. EXTENT_NEED_WAIT,
  810. mark, &cached_state);
  811. /*
  812. * convert_extent_bit can return -ENOMEM, which is most of the
  813. * time a temporary error. So when it happens, ignore the error
  814. * and wait for writeback of this range to finish - because we
  815. * failed to set the bit EXTENT_NEED_WAIT for the range, a call
  816. * to __btrfs_wait_marked_extents() would not know that
  817. * writeback for this range started and therefore wouldn't
  818. * wait for it to finish - we don't want to commit a
  819. * superblock that points to btree nodes/leafs for which
  820. * writeback hasn't finished yet (and without errors).
  821. * We cleanup any entries left in the io tree when committing
  822. * the transaction (through clear_btree_io_tree()).
  823. */
  824. if (err == -ENOMEM) {
  825. err = 0;
  826. wait_writeback = true;
  827. }
  828. if (!err)
  829. err = filemap_fdatawrite_range(mapping, start, end);
  830. if (err)
  831. werr = err;
  832. else if (wait_writeback)
  833. werr = filemap_fdatawait_range(mapping, start, end);
  834. free_extent_state(cached_state);
  835. cached_state = NULL;
  836. cond_resched();
  837. start = end + 1;
  838. }
  839. atomic_dec(&BTRFS_I(fs_info->btree_inode)->sync_writers);
  840. return werr;
  841. }
  842. /*
  843. * when btree blocks are allocated, they have some corresponding bits set for
  844. * them in one of two extent_io trees. This is used to make sure all of
  845. * those extents are on disk for transaction or log commit. We wait
  846. * on all the pages and clear them from the dirty pages state tree
  847. */
  848. static int __btrfs_wait_marked_extents(struct btrfs_fs_info *fs_info,
  849. struct extent_io_tree *dirty_pages)
  850. {
  851. int err = 0;
  852. int werr = 0;
  853. struct address_space *mapping = fs_info->btree_inode->i_mapping;
  854. struct extent_state *cached_state = NULL;
  855. u64 start = 0;
  856. u64 end;
  857. while (!find_first_extent_bit(dirty_pages, start, &start, &end,
  858. EXTENT_NEED_WAIT, &cached_state)) {
  859. /*
  860. * Ignore -ENOMEM errors returned by clear_extent_bit().
  861. * When committing the transaction, we'll remove any entries
  862. * left in the io tree. For a log commit, we don't remove them
  863. * after committing the log because the tree can be accessed
  864. * concurrently - we do it only at transaction commit time when
  865. * it's safe to do it (through clear_btree_io_tree()).
  866. */
  867. err = clear_extent_bit(dirty_pages, start, end,
  868. EXTENT_NEED_WAIT, 0, 0, &cached_state);
  869. if (err == -ENOMEM)
  870. err = 0;
  871. if (!err)
  872. err = filemap_fdatawait_range(mapping, start, end);
  873. if (err)
  874. werr = err;
  875. free_extent_state(cached_state);
  876. cached_state = NULL;
  877. cond_resched();
  878. start = end + 1;
  879. }
  880. if (err)
  881. werr = err;
  882. return werr;
  883. }
  884. int btrfs_wait_extents(struct btrfs_fs_info *fs_info,
  885. struct extent_io_tree *dirty_pages)
  886. {
  887. bool errors = false;
  888. int err;
  889. err = __btrfs_wait_marked_extents(fs_info, dirty_pages);
  890. if (test_and_clear_bit(BTRFS_FS_BTREE_ERR, &fs_info->flags))
  891. errors = true;
  892. if (errors && !err)
  893. err = -EIO;
  894. return err;
  895. }
  896. int btrfs_wait_tree_log_extents(struct btrfs_root *log_root, int mark)
  897. {
  898. struct btrfs_fs_info *fs_info = log_root->fs_info;
  899. struct extent_io_tree *dirty_pages = &log_root->dirty_log_pages;
  900. bool errors = false;
  901. int err;
  902. ASSERT(log_root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID);
  903. err = __btrfs_wait_marked_extents(fs_info, dirty_pages);
  904. if ((mark & EXTENT_DIRTY) &&
  905. test_and_clear_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags))
  906. errors = true;
  907. if ((mark & EXTENT_NEW) &&
  908. test_and_clear_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags))
  909. errors = true;
  910. if (errors && !err)
  911. err = -EIO;
  912. return err;
  913. }
  914. /*
  915. * When btree blocks are allocated the corresponding extents are marked dirty.
  916. * This function ensures such extents are persisted on disk for transaction or
  917. * log commit.
  918. *
  919. * @trans: transaction whose dirty pages we'd like to write
  920. */
  921. static int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans)
  922. {
  923. int ret;
  924. int ret2;
  925. struct extent_io_tree *dirty_pages = &trans->transaction->dirty_pages;
  926. struct btrfs_fs_info *fs_info = trans->fs_info;
  927. struct blk_plug plug;
  928. blk_start_plug(&plug);
  929. ret = btrfs_write_marked_extents(fs_info, dirty_pages, EXTENT_DIRTY);
  930. blk_finish_plug(&plug);
  931. ret2 = btrfs_wait_extents(fs_info, dirty_pages);
  932. clear_btree_io_tree(&trans->transaction->dirty_pages);
  933. if (ret)
  934. return ret;
  935. else if (ret2)
  936. return ret2;
  937. else
  938. return 0;
  939. }
  940. /*
  941. * this is used to update the root pointer in the tree of tree roots.
  942. *
  943. * But, in the case of the extent allocation tree, updating the root
  944. * pointer may allocate blocks which may change the root of the extent
  945. * allocation tree.
  946. *
  947. * So, this loops and repeats and makes sure the cowonly root didn't
  948. * change while the root pointer was being updated in the metadata.
  949. */
  950. static int update_cowonly_root(struct btrfs_trans_handle *trans,
  951. struct btrfs_root *root)
  952. {
  953. int ret;
  954. u64 old_root_bytenr;
  955. u64 old_root_used;
  956. struct btrfs_fs_info *fs_info = root->fs_info;
  957. struct btrfs_root *tree_root = fs_info->tree_root;
  958. old_root_used = btrfs_root_used(&root->root_item);
  959. while (1) {
  960. old_root_bytenr = btrfs_root_bytenr(&root->root_item);
  961. if (old_root_bytenr == root->node->start &&
  962. old_root_used == btrfs_root_used(&root->root_item))
  963. break;
  964. btrfs_set_root_node(&root->root_item, root->node);
  965. ret = btrfs_update_root(trans, tree_root,
  966. &root->root_key,
  967. &root->root_item);
  968. if (ret)
  969. return ret;
  970. old_root_used = btrfs_root_used(&root->root_item);
  971. }
  972. return 0;
  973. }
  974. /*
  975. * update all the cowonly tree roots on disk
  976. *
  977. * The error handling in this function may not be obvious. Any of the
  978. * failures will cause the file system to go offline. We still need
  979. * to clean up the delayed refs.
  980. */
  981. static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans)
  982. {
  983. struct btrfs_fs_info *fs_info = trans->fs_info;
  984. struct list_head *dirty_bgs = &trans->transaction->dirty_bgs;
  985. struct list_head *io_bgs = &trans->transaction->io_bgs;
  986. struct list_head *next;
  987. struct extent_buffer *eb;
  988. int ret;
  989. eb = btrfs_lock_root_node(fs_info->tree_root);
  990. ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL,
  991. 0, &eb);
  992. btrfs_tree_unlock(eb);
  993. free_extent_buffer(eb);
  994. if (ret)
  995. return ret;
  996. ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
  997. if (ret)
  998. return ret;
  999. ret = btrfs_run_dev_stats(trans, fs_info);
  1000. if (ret)
  1001. return ret;
  1002. ret = btrfs_run_dev_replace(trans, fs_info);
  1003. if (ret)
  1004. return ret;
  1005. ret = btrfs_run_qgroups(trans, fs_info);
  1006. if (ret)
  1007. return ret;
  1008. ret = btrfs_setup_space_cache(trans, fs_info);
  1009. if (ret)
  1010. return ret;
  1011. /* run_qgroups might have added some more refs */
  1012. ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
  1013. if (ret)
  1014. return ret;
  1015. again:
  1016. while (!list_empty(&fs_info->dirty_cowonly_roots)) {
  1017. struct btrfs_root *root;
  1018. next = fs_info->dirty_cowonly_roots.next;
  1019. list_del_init(next);
  1020. root = list_entry(next, struct btrfs_root, dirty_list);
  1021. clear_bit(BTRFS_ROOT_DIRTY, &root->state);
  1022. if (root != fs_info->extent_root)
  1023. list_add_tail(&root->dirty_list,
  1024. &trans->transaction->switch_commits);
  1025. ret = update_cowonly_root(trans, root);
  1026. if (ret)
  1027. return ret;
  1028. ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
  1029. if (ret)
  1030. return ret;
  1031. }
  1032. while (!list_empty(dirty_bgs) || !list_empty(io_bgs)) {
  1033. ret = btrfs_write_dirty_block_groups(trans, fs_info);
  1034. if (ret)
  1035. return ret;
  1036. ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
  1037. if (ret)
  1038. return ret;
  1039. }
  1040. if (!list_empty(&fs_info->dirty_cowonly_roots))
  1041. goto again;
  1042. list_add_tail(&fs_info->extent_root->dirty_list,
  1043. &trans->transaction->switch_commits);
  1044. btrfs_after_dev_replace_commit(fs_info);
  1045. return 0;
  1046. }
  1047. /*
  1048. * dead roots are old snapshots that need to be deleted. This allocates
  1049. * a dirty root struct and adds it into the list of dead roots that need to
  1050. * be deleted
  1051. */
  1052. void btrfs_add_dead_root(struct btrfs_root *root)
  1053. {
  1054. struct btrfs_fs_info *fs_info = root->fs_info;
  1055. spin_lock(&fs_info->trans_lock);
  1056. if (list_empty(&root->root_list))
  1057. list_add_tail(&root->root_list, &fs_info->dead_roots);
  1058. spin_unlock(&fs_info->trans_lock);
  1059. }
  1060. /*
  1061. * update all the cowonly tree roots on disk
  1062. */
  1063. static noinline int commit_fs_roots(struct btrfs_trans_handle *trans)
  1064. {
  1065. struct btrfs_fs_info *fs_info = trans->fs_info;
  1066. struct btrfs_root *gang[8];
  1067. int i;
  1068. int ret;
  1069. int err = 0;
  1070. spin_lock(&fs_info->fs_roots_radix_lock);
  1071. while (1) {
  1072. ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
  1073. (void **)gang, 0,
  1074. ARRAY_SIZE(gang),
  1075. BTRFS_ROOT_TRANS_TAG);
  1076. if (ret == 0)
  1077. break;
  1078. for (i = 0; i < ret; i++) {
  1079. struct btrfs_root *root = gang[i];
  1080. radix_tree_tag_clear(&fs_info->fs_roots_radix,
  1081. (unsigned long)root->root_key.objectid,
  1082. BTRFS_ROOT_TRANS_TAG);
  1083. spin_unlock(&fs_info->fs_roots_radix_lock);
  1084. btrfs_free_log(trans, root);
  1085. btrfs_update_reloc_root(trans, root);
  1086. btrfs_orphan_commit_root(trans, root);
  1087. btrfs_save_ino_cache(root, trans);
  1088. /* see comments in should_cow_block() */
  1089. clear_bit(BTRFS_ROOT_FORCE_COW, &root->state);
  1090. smp_mb__after_atomic();
  1091. if (root->commit_root != root->node) {
  1092. list_add_tail(&root->dirty_list,
  1093. &trans->transaction->switch_commits);
  1094. btrfs_set_root_node(&root->root_item,
  1095. root->node);
  1096. }
  1097. err = btrfs_update_root(trans, fs_info->tree_root,
  1098. &root->root_key,
  1099. &root->root_item);
  1100. spin_lock(&fs_info->fs_roots_radix_lock);
  1101. if (err)
  1102. break;
  1103. btrfs_qgroup_free_meta_all_pertrans(root);
  1104. }
  1105. }
  1106. spin_unlock(&fs_info->fs_roots_radix_lock);
  1107. return err;
  1108. }
  1109. /*
  1110. * defrag a given btree.
  1111. * Every leaf in the btree is read and defragged.
  1112. */
  1113. int btrfs_defrag_root(struct btrfs_root *root)
  1114. {
  1115. struct btrfs_fs_info *info = root->fs_info;
  1116. struct btrfs_trans_handle *trans;
  1117. int ret;
  1118. if (test_and_set_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state))
  1119. return 0;
  1120. while (1) {
  1121. trans = btrfs_start_transaction(root, 0);
  1122. if (IS_ERR(trans))
  1123. return PTR_ERR(trans);
  1124. ret = btrfs_defrag_leaves(trans, root);
  1125. btrfs_end_transaction(trans);
  1126. btrfs_btree_balance_dirty(info);
  1127. cond_resched();
  1128. if (btrfs_fs_closing(info) || ret != -EAGAIN)
  1129. break;
  1130. if (btrfs_defrag_cancelled(info)) {
  1131. btrfs_debug(info, "defrag_root cancelled");
  1132. ret = -EAGAIN;
  1133. break;
  1134. }
  1135. }
  1136. clear_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state);
  1137. return ret;
  1138. }
  1139. /*
  1140. * Do all special snapshot related qgroup dirty hack.
  1141. *
  1142. * Will do all needed qgroup inherit and dirty hack like switch commit
  1143. * roots inside one transaction and write all btree into disk, to make
  1144. * qgroup works.
  1145. */
  1146. static int qgroup_account_snapshot(struct btrfs_trans_handle *trans,
  1147. struct btrfs_root *src,
  1148. struct btrfs_root *parent,
  1149. struct btrfs_qgroup_inherit *inherit,
  1150. u64 dst_objectid)
  1151. {
  1152. struct btrfs_fs_info *fs_info = src->fs_info;
  1153. int ret;
  1154. /*
  1155. * Save some performance in the case that qgroups are not
  1156. * enabled. If this check races with the ioctl, rescan will
  1157. * kick in anyway.
  1158. */
  1159. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
  1160. return 0;
  1161. /*
  1162. * Ensure dirty @src will be commited. Or, after comming
  1163. * commit_fs_roots() and switch_commit_roots(), any dirty but not
  1164. * recorded root will never be updated again, causing an outdated root
  1165. * item.
  1166. */
  1167. record_root_in_trans(trans, src, 1);
  1168. /*
  1169. * We are going to commit transaction, see btrfs_commit_transaction()
  1170. * comment for reason locking tree_log_mutex
  1171. */
  1172. mutex_lock(&fs_info->tree_log_mutex);
  1173. ret = commit_fs_roots(trans);
  1174. if (ret)
  1175. goto out;
  1176. ret = btrfs_qgroup_account_extents(trans);
  1177. if (ret < 0)
  1178. goto out;
  1179. /* Now qgroup are all updated, we can inherit it to new qgroups */
  1180. ret = btrfs_qgroup_inherit(trans, fs_info,
  1181. src->root_key.objectid, dst_objectid,
  1182. inherit);
  1183. if (ret < 0)
  1184. goto out;
  1185. /*
  1186. * Now we do a simplified commit transaction, which will:
  1187. * 1) commit all subvolume and extent tree
  1188. * To ensure all subvolume and extent tree have a valid
  1189. * commit_root to accounting later insert_dir_item()
  1190. * 2) write all btree blocks onto disk
  1191. * This is to make sure later btree modification will be cowed
  1192. * Or commit_root can be populated and cause wrong qgroup numbers
  1193. * In this simplified commit, we don't really care about other trees
  1194. * like chunk and root tree, as they won't affect qgroup.
  1195. * And we don't write super to avoid half committed status.
  1196. */
  1197. ret = commit_cowonly_roots(trans);
  1198. if (ret)
  1199. goto out;
  1200. switch_commit_roots(trans->transaction);
  1201. ret = btrfs_write_and_wait_transaction(trans);
  1202. if (ret)
  1203. btrfs_handle_fs_error(fs_info, ret,
  1204. "Error while writing out transaction for qgroup");
  1205. out:
  1206. mutex_unlock(&fs_info->tree_log_mutex);
  1207. /*
  1208. * Force parent root to be updated, as we recorded it before so its
  1209. * last_trans == cur_transid.
  1210. * Or it won't be committed again onto disk after later
  1211. * insert_dir_item()
  1212. */
  1213. if (!ret)
  1214. record_root_in_trans(trans, parent, 1);
  1215. return ret;
  1216. }
  1217. /*
  1218. * new snapshots need to be created at a very specific time in the
  1219. * transaction commit. This does the actual creation.
  1220. *
  1221. * Note:
  1222. * If the error which may affect the commitment of the current transaction
  1223. * happens, we should return the error number. If the error which just affect
  1224. * the creation of the pending snapshots, just return 0.
  1225. */
  1226. static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
  1227. struct btrfs_pending_snapshot *pending)
  1228. {
  1229. struct btrfs_fs_info *fs_info = trans->fs_info;
  1230. struct btrfs_key key;
  1231. struct btrfs_root_item *new_root_item;
  1232. struct btrfs_root *tree_root = fs_info->tree_root;
  1233. struct btrfs_root *root = pending->root;
  1234. struct btrfs_root *parent_root;
  1235. struct btrfs_block_rsv *rsv;
  1236. struct inode *parent_inode;
  1237. struct btrfs_path *path;
  1238. struct btrfs_dir_item *dir_item;
  1239. struct dentry *dentry;
  1240. struct extent_buffer *tmp;
  1241. struct extent_buffer *old;
  1242. struct timespec64 cur_time;
  1243. int ret = 0;
  1244. u64 to_reserve = 0;
  1245. u64 index = 0;
  1246. u64 objectid;
  1247. u64 root_flags;
  1248. uuid_le new_uuid;
  1249. ASSERT(pending->path);
  1250. path = pending->path;
  1251. ASSERT(pending->root_item);
  1252. new_root_item = pending->root_item;
  1253. pending->error = btrfs_find_free_objectid(tree_root, &objectid);
  1254. if (pending->error)
  1255. goto no_free_objectid;
  1256. /*
  1257. * Make qgroup to skip current new snapshot's qgroupid, as it is
  1258. * accounted by later btrfs_qgroup_inherit().
  1259. */
  1260. btrfs_set_skip_qgroup(trans, objectid);
  1261. btrfs_reloc_pre_snapshot(pending, &to_reserve);
  1262. if (to_reserve > 0) {
  1263. pending->error = btrfs_block_rsv_add(root,
  1264. &pending->block_rsv,
  1265. to_reserve,
  1266. BTRFS_RESERVE_NO_FLUSH);
  1267. if (pending->error)
  1268. goto clear_skip_qgroup;
  1269. }
  1270. key.objectid = objectid;
  1271. key.offset = (u64)-1;
  1272. key.type = BTRFS_ROOT_ITEM_KEY;
  1273. rsv = trans->block_rsv;
  1274. trans->block_rsv = &pending->block_rsv;
  1275. trans->bytes_reserved = trans->block_rsv->reserved;
  1276. trace_btrfs_space_reservation(fs_info, "transaction",
  1277. trans->transid,
  1278. trans->bytes_reserved, 1);
  1279. dentry = pending->dentry;
  1280. parent_inode = pending->dir;
  1281. parent_root = BTRFS_I(parent_inode)->root;
  1282. record_root_in_trans(trans, parent_root, 0);
  1283. cur_time = current_time(parent_inode);
  1284. /*
  1285. * insert the directory item
  1286. */
  1287. ret = btrfs_set_inode_index(BTRFS_I(parent_inode), &index);
  1288. BUG_ON(ret); /* -ENOMEM */
  1289. /* check if there is a file/dir which has the same name. */
  1290. dir_item = btrfs_lookup_dir_item(NULL, parent_root, path,
  1291. btrfs_ino(BTRFS_I(parent_inode)),
  1292. dentry->d_name.name,
  1293. dentry->d_name.len, 0);
  1294. if (dir_item != NULL && !IS_ERR(dir_item)) {
  1295. pending->error = -EEXIST;
  1296. goto dir_item_existed;
  1297. } else if (IS_ERR(dir_item)) {
  1298. ret = PTR_ERR(dir_item);
  1299. btrfs_abort_transaction(trans, ret);
  1300. goto fail;
  1301. }
  1302. btrfs_release_path(path);
  1303. /*
  1304. * pull in the delayed directory update
  1305. * and the delayed inode item
  1306. * otherwise we corrupt the FS during
  1307. * snapshot
  1308. */
  1309. ret = btrfs_run_delayed_items(trans);
  1310. if (ret) { /* Transaction aborted */
  1311. btrfs_abort_transaction(trans, ret);
  1312. goto fail;
  1313. }
  1314. record_root_in_trans(trans, root, 0);
  1315. btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
  1316. memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
  1317. btrfs_check_and_init_root_item(new_root_item);
  1318. root_flags = btrfs_root_flags(new_root_item);
  1319. if (pending->readonly)
  1320. root_flags |= BTRFS_ROOT_SUBVOL_RDONLY;
  1321. else
  1322. root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY;
  1323. btrfs_set_root_flags(new_root_item, root_flags);
  1324. btrfs_set_root_generation_v2(new_root_item,
  1325. trans->transid);
  1326. uuid_le_gen(&new_uuid);
  1327. memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
  1328. memcpy(new_root_item->parent_uuid, root->root_item.uuid,
  1329. BTRFS_UUID_SIZE);
  1330. if (!(root_flags & BTRFS_ROOT_SUBVOL_RDONLY)) {
  1331. memset(new_root_item->received_uuid, 0,
  1332. sizeof(new_root_item->received_uuid));
  1333. memset(&new_root_item->stime, 0, sizeof(new_root_item->stime));
  1334. memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime));
  1335. btrfs_set_root_stransid(new_root_item, 0);
  1336. btrfs_set_root_rtransid(new_root_item, 0);
  1337. }
  1338. btrfs_set_stack_timespec_sec(&new_root_item->otime, cur_time.tv_sec);
  1339. btrfs_set_stack_timespec_nsec(&new_root_item->otime, cur_time.tv_nsec);
  1340. btrfs_set_root_otransid(new_root_item, trans->transid);
  1341. old = btrfs_lock_root_node(root);
  1342. ret = btrfs_cow_block(trans, root, old, NULL, 0, &old);
  1343. if (ret) {
  1344. btrfs_tree_unlock(old);
  1345. free_extent_buffer(old);
  1346. btrfs_abort_transaction(trans, ret);
  1347. goto fail;
  1348. }
  1349. btrfs_set_lock_blocking(old);
  1350. ret = btrfs_copy_root(trans, root, old, &tmp, objectid);
  1351. /* clean up in any case */
  1352. btrfs_tree_unlock(old);
  1353. free_extent_buffer(old);
  1354. if (ret) {
  1355. btrfs_abort_transaction(trans, ret);
  1356. goto fail;
  1357. }
  1358. /* see comments in should_cow_block() */
  1359. set_bit(BTRFS_ROOT_FORCE_COW, &root->state);
  1360. smp_wmb();
  1361. btrfs_set_root_node(new_root_item, tmp);
  1362. /* record when the snapshot was created in key.offset */
  1363. key.offset = trans->transid;
  1364. ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
  1365. btrfs_tree_unlock(tmp);
  1366. free_extent_buffer(tmp);
  1367. if (ret) {
  1368. btrfs_abort_transaction(trans, ret);
  1369. goto fail;
  1370. }
  1371. /*
  1372. * insert root back/forward references
  1373. */
  1374. ret = btrfs_add_root_ref(trans, fs_info, objectid,
  1375. parent_root->root_key.objectid,
  1376. btrfs_ino(BTRFS_I(parent_inode)), index,
  1377. dentry->d_name.name, dentry->d_name.len);
  1378. if (ret) {
  1379. btrfs_abort_transaction(trans, ret);
  1380. goto fail;
  1381. }
  1382. key.offset = (u64)-1;
  1383. pending->snap = btrfs_read_fs_root_no_name(fs_info, &key);
  1384. if (IS_ERR(pending->snap)) {
  1385. ret = PTR_ERR(pending->snap);
  1386. btrfs_abort_transaction(trans, ret);
  1387. goto fail;
  1388. }
  1389. ret = btrfs_reloc_post_snapshot(trans, pending);
  1390. if (ret) {
  1391. btrfs_abort_transaction(trans, ret);
  1392. goto fail;
  1393. }
  1394. ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
  1395. if (ret) {
  1396. btrfs_abort_transaction(trans, ret);
  1397. goto fail;
  1398. }
  1399. /*
  1400. * Do special qgroup accounting for snapshot, as we do some qgroup
  1401. * snapshot hack to do fast snapshot.
  1402. * To co-operate with that hack, we do hack again.
  1403. * Or snapshot will be greatly slowed down by a subtree qgroup rescan
  1404. */
  1405. ret = qgroup_account_snapshot(trans, root, parent_root,
  1406. pending->inherit, objectid);
  1407. if (ret < 0)
  1408. goto fail;
  1409. ret = btrfs_insert_dir_item(trans, parent_root,
  1410. dentry->d_name.name, dentry->d_name.len,
  1411. BTRFS_I(parent_inode), &key,
  1412. BTRFS_FT_DIR, index);
  1413. /* We have check then name at the beginning, so it is impossible. */
  1414. BUG_ON(ret == -EEXIST || ret == -EOVERFLOW);
  1415. if (ret) {
  1416. btrfs_abort_transaction(trans, ret);
  1417. goto fail;
  1418. }
  1419. btrfs_i_size_write(BTRFS_I(parent_inode), parent_inode->i_size +
  1420. dentry->d_name.len * 2);
  1421. parent_inode->i_mtime = parent_inode->i_ctime =
  1422. current_time(parent_inode);
  1423. ret = btrfs_update_inode_fallback(trans, parent_root, parent_inode);
  1424. if (ret) {
  1425. btrfs_abort_transaction(trans, ret);
  1426. goto fail;
  1427. }
  1428. ret = btrfs_uuid_tree_add(trans, fs_info, new_uuid.b,
  1429. BTRFS_UUID_KEY_SUBVOL, objectid);
  1430. if (ret) {
  1431. btrfs_abort_transaction(trans, ret);
  1432. goto fail;
  1433. }
  1434. if (!btrfs_is_empty_uuid(new_root_item->received_uuid)) {
  1435. ret = btrfs_uuid_tree_add(trans, fs_info,
  1436. new_root_item->received_uuid,
  1437. BTRFS_UUID_KEY_RECEIVED_SUBVOL,
  1438. objectid);
  1439. if (ret && ret != -EEXIST) {
  1440. btrfs_abort_transaction(trans, ret);
  1441. goto fail;
  1442. }
  1443. }
  1444. ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
  1445. if (ret) {
  1446. btrfs_abort_transaction(trans, ret);
  1447. goto fail;
  1448. }
  1449. fail:
  1450. pending->error = ret;
  1451. dir_item_existed:
  1452. trans->block_rsv = rsv;
  1453. trans->bytes_reserved = 0;
  1454. clear_skip_qgroup:
  1455. btrfs_clear_skip_qgroup(trans);
  1456. no_free_objectid:
  1457. kfree(new_root_item);
  1458. pending->root_item = NULL;
  1459. btrfs_free_path(path);
  1460. pending->path = NULL;
  1461. return ret;
  1462. }
  1463. /*
  1464. * create all the snapshots we've scheduled for creation
  1465. */
  1466. static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans)
  1467. {
  1468. struct btrfs_pending_snapshot *pending, *next;
  1469. struct list_head *head = &trans->transaction->pending_snapshots;
  1470. int ret = 0;
  1471. list_for_each_entry_safe(pending, next, head, list) {
  1472. list_del(&pending->list);
  1473. ret = create_pending_snapshot(trans, pending);
  1474. if (ret)
  1475. break;
  1476. }
  1477. return ret;
  1478. }
  1479. static void update_super_roots(struct btrfs_fs_info *fs_info)
  1480. {
  1481. struct btrfs_root_item *root_item;
  1482. struct btrfs_super_block *super;
  1483. super = fs_info->super_copy;
  1484. root_item = &fs_info->chunk_root->root_item;
  1485. super->chunk_root = root_item->bytenr;
  1486. super->chunk_root_generation = root_item->generation;
  1487. super->chunk_root_level = root_item->level;
  1488. root_item = &fs_info->tree_root->root_item;
  1489. super->root = root_item->bytenr;
  1490. super->generation = root_item->generation;
  1491. super->root_level = root_item->level;
  1492. if (btrfs_test_opt(fs_info, SPACE_CACHE))
  1493. super->cache_generation = root_item->generation;
  1494. if (test_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags))
  1495. super->uuid_tree_generation = root_item->generation;
  1496. }
  1497. int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
  1498. {
  1499. struct btrfs_transaction *trans;
  1500. int ret = 0;
  1501. spin_lock(&info->trans_lock);
  1502. trans = info->running_transaction;
  1503. if (trans)
  1504. ret = (trans->state >= TRANS_STATE_COMMIT_START);
  1505. spin_unlock(&info->trans_lock);
  1506. return ret;
  1507. }
  1508. int btrfs_transaction_blocked(struct btrfs_fs_info *info)
  1509. {
  1510. struct btrfs_transaction *trans;
  1511. int ret = 0;
  1512. spin_lock(&info->trans_lock);
  1513. trans = info->running_transaction;
  1514. if (trans)
  1515. ret = is_transaction_blocked(trans);
  1516. spin_unlock(&info->trans_lock);
  1517. return ret;
  1518. }
  1519. /*
  1520. * wait for the current transaction commit to start and block subsequent
  1521. * transaction joins
  1522. */
  1523. static void wait_current_trans_commit_start(struct btrfs_fs_info *fs_info,
  1524. struct btrfs_transaction *trans)
  1525. {
  1526. wait_event(fs_info->transaction_blocked_wait,
  1527. trans->state >= TRANS_STATE_COMMIT_START || trans->aborted);
  1528. }
  1529. /*
  1530. * wait for the current transaction to start and then become unblocked.
  1531. * caller holds ref.
  1532. */
  1533. static void wait_current_trans_commit_start_and_unblock(
  1534. struct btrfs_fs_info *fs_info,
  1535. struct btrfs_transaction *trans)
  1536. {
  1537. wait_event(fs_info->transaction_wait,
  1538. trans->state >= TRANS_STATE_UNBLOCKED || trans->aborted);
  1539. }
  1540. /*
  1541. * commit transactions asynchronously. once btrfs_commit_transaction_async
  1542. * returns, any subsequent transaction will not be allowed to join.
  1543. */
  1544. struct btrfs_async_commit {
  1545. struct btrfs_trans_handle *newtrans;
  1546. struct work_struct work;
  1547. };
  1548. static void do_async_commit(struct work_struct *work)
  1549. {
  1550. struct btrfs_async_commit *ac =
  1551. container_of(work, struct btrfs_async_commit, work);
  1552. /*
  1553. * We've got freeze protection passed with the transaction.
  1554. * Tell lockdep about it.
  1555. */
  1556. if (ac->newtrans->type & __TRANS_FREEZABLE)
  1557. __sb_writers_acquired(ac->newtrans->fs_info->sb, SB_FREEZE_FS);
  1558. current->journal_info = ac->newtrans;
  1559. btrfs_commit_transaction(ac->newtrans);
  1560. kfree(ac);
  1561. }
  1562. int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
  1563. int wait_for_unblock)
  1564. {
  1565. struct btrfs_fs_info *fs_info = trans->fs_info;
  1566. struct btrfs_async_commit *ac;
  1567. struct btrfs_transaction *cur_trans;
  1568. ac = kmalloc(sizeof(*ac), GFP_NOFS);
  1569. if (!ac)
  1570. return -ENOMEM;
  1571. INIT_WORK(&ac->work, do_async_commit);
  1572. ac->newtrans = btrfs_join_transaction(trans->root);
  1573. if (IS_ERR(ac->newtrans)) {
  1574. int err = PTR_ERR(ac->newtrans);
  1575. kfree(ac);
  1576. return err;
  1577. }
  1578. /* take transaction reference */
  1579. cur_trans = trans->transaction;
  1580. refcount_inc(&cur_trans->use_count);
  1581. btrfs_end_transaction(trans);
  1582. /*
  1583. * Tell lockdep we've released the freeze rwsem, since the
  1584. * async commit thread will be the one to unlock it.
  1585. */
  1586. if (ac->newtrans->type & __TRANS_FREEZABLE)
  1587. __sb_writers_release(fs_info->sb, SB_FREEZE_FS);
  1588. schedule_work(&ac->work);
  1589. /* wait for transaction to start and unblock */
  1590. if (wait_for_unblock)
  1591. wait_current_trans_commit_start_and_unblock(fs_info, cur_trans);
  1592. else
  1593. wait_current_trans_commit_start(fs_info, cur_trans);
  1594. if (current->journal_info == trans)
  1595. current->journal_info = NULL;
  1596. btrfs_put_transaction(cur_trans);
  1597. return 0;
  1598. }
  1599. static void cleanup_transaction(struct btrfs_trans_handle *trans, int err)
  1600. {
  1601. struct btrfs_fs_info *fs_info = trans->fs_info;
  1602. struct btrfs_transaction *cur_trans = trans->transaction;
  1603. DEFINE_WAIT(wait);
  1604. WARN_ON(refcount_read(&trans->use_count) > 1);
  1605. btrfs_abort_transaction(trans, err);
  1606. spin_lock(&fs_info->trans_lock);
  1607. /*
  1608. * If the transaction is removed from the list, it means this
  1609. * transaction has been committed successfully, so it is impossible
  1610. * to call the cleanup function.
  1611. */
  1612. BUG_ON(list_empty(&cur_trans->list));
  1613. list_del_init(&cur_trans->list);
  1614. if (cur_trans == fs_info->running_transaction) {
  1615. cur_trans->state = TRANS_STATE_COMMIT_DOING;
  1616. spin_unlock(&fs_info->trans_lock);
  1617. wait_event(cur_trans->writer_wait,
  1618. atomic_read(&cur_trans->num_writers) == 1);
  1619. spin_lock(&fs_info->trans_lock);
  1620. }
  1621. spin_unlock(&fs_info->trans_lock);
  1622. btrfs_cleanup_one_transaction(trans->transaction, fs_info);
  1623. spin_lock(&fs_info->trans_lock);
  1624. if (cur_trans == fs_info->running_transaction)
  1625. fs_info->running_transaction = NULL;
  1626. spin_unlock(&fs_info->trans_lock);
  1627. if (trans->type & __TRANS_FREEZABLE)
  1628. sb_end_intwrite(fs_info->sb);
  1629. btrfs_put_transaction(cur_trans);
  1630. btrfs_put_transaction(cur_trans);
  1631. trace_btrfs_transaction_commit(trans->root);
  1632. if (current->journal_info == trans)
  1633. current->journal_info = NULL;
  1634. btrfs_scrub_cancel(fs_info);
  1635. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  1636. }
  1637. static inline int btrfs_start_delalloc_flush(struct btrfs_fs_info *fs_info)
  1638. {
  1639. /*
  1640. * We use writeback_inodes_sb here because if we used
  1641. * btrfs_start_delalloc_roots we would deadlock with fs freeze.
  1642. * Currently are holding the fs freeze lock, if we do an async flush
  1643. * we'll do btrfs_join_transaction() and deadlock because we need to
  1644. * wait for the fs freeze lock. Using the direct flushing we benefit
  1645. * from already being in a transaction and our join_transaction doesn't
  1646. * have to re-take the fs freeze lock.
  1647. */
  1648. if (btrfs_test_opt(fs_info, FLUSHONCOMMIT))
  1649. writeback_inodes_sb(fs_info->sb, WB_REASON_SYNC);
  1650. return 0;
  1651. }
  1652. static inline void btrfs_wait_delalloc_flush(struct btrfs_fs_info *fs_info)
  1653. {
  1654. if (btrfs_test_opt(fs_info, FLUSHONCOMMIT))
  1655. btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
  1656. }
  1657. static inline void
  1658. btrfs_wait_pending_ordered(struct btrfs_transaction *cur_trans)
  1659. {
  1660. wait_event(cur_trans->pending_wait,
  1661. atomic_read(&cur_trans->pending_ordered) == 0);
  1662. }
  1663. int btrfs_commit_transaction(struct btrfs_trans_handle *trans)
  1664. {
  1665. struct btrfs_fs_info *fs_info = trans->fs_info;
  1666. struct btrfs_transaction *cur_trans = trans->transaction;
  1667. struct btrfs_transaction *prev_trans = NULL;
  1668. int ret;
  1669. /* Stop the commit early if ->aborted is set */
  1670. if (unlikely(READ_ONCE(cur_trans->aborted))) {
  1671. ret = cur_trans->aborted;
  1672. btrfs_end_transaction(trans);
  1673. return ret;
  1674. }
  1675. /* make a pass through all the delayed refs we have so far
  1676. * any runnings procs may add more while we are here
  1677. */
  1678. ret = btrfs_run_delayed_refs(trans, 0);
  1679. if (ret) {
  1680. btrfs_end_transaction(trans);
  1681. return ret;
  1682. }
  1683. btrfs_trans_release_metadata(trans);
  1684. trans->block_rsv = NULL;
  1685. cur_trans = trans->transaction;
  1686. /*
  1687. * set the flushing flag so procs in this transaction have to
  1688. * start sending their work down.
  1689. */
  1690. cur_trans->delayed_refs.flushing = 1;
  1691. smp_wmb();
  1692. if (!list_empty(&trans->new_bgs))
  1693. btrfs_create_pending_block_groups(trans);
  1694. ret = btrfs_run_delayed_refs(trans, 0);
  1695. if (ret) {
  1696. btrfs_end_transaction(trans);
  1697. return ret;
  1698. }
  1699. if (!test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &cur_trans->flags)) {
  1700. int run_it = 0;
  1701. /* this mutex is also taken before trying to set
  1702. * block groups readonly. We need to make sure
  1703. * that nobody has set a block group readonly
  1704. * after a extents from that block group have been
  1705. * allocated for cache files. btrfs_set_block_group_ro
  1706. * will wait for the transaction to commit if it
  1707. * finds BTRFS_TRANS_DIRTY_BG_RUN set.
  1708. *
  1709. * The BTRFS_TRANS_DIRTY_BG_RUN flag is also used to make sure
  1710. * only one process starts all the block group IO. It wouldn't
  1711. * hurt to have more than one go through, but there's no
  1712. * real advantage to it either.
  1713. */
  1714. mutex_lock(&fs_info->ro_block_group_mutex);
  1715. if (!test_and_set_bit(BTRFS_TRANS_DIRTY_BG_RUN,
  1716. &cur_trans->flags))
  1717. run_it = 1;
  1718. mutex_unlock(&fs_info->ro_block_group_mutex);
  1719. if (run_it) {
  1720. ret = btrfs_start_dirty_block_groups(trans);
  1721. if (ret) {
  1722. btrfs_end_transaction(trans);
  1723. return ret;
  1724. }
  1725. }
  1726. }
  1727. spin_lock(&fs_info->trans_lock);
  1728. if (cur_trans->state >= TRANS_STATE_COMMIT_START) {
  1729. spin_unlock(&fs_info->trans_lock);
  1730. refcount_inc(&cur_trans->use_count);
  1731. ret = btrfs_end_transaction(trans);
  1732. wait_for_commit(cur_trans);
  1733. if (unlikely(cur_trans->aborted))
  1734. ret = cur_trans->aborted;
  1735. btrfs_put_transaction(cur_trans);
  1736. return ret;
  1737. }
  1738. cur_trans->state = TRANS_STATE_COMMIT_START;
  1739. wake_up(&fs_info->transaction_blocked_wait);
  1740. if (cur_trans->list.prev != &fs_info->trans_list) {
  1741. prev_trans = list_entry(cur_trans->list.prev,
  1742. struct btrfs_transaction, list);
  1743. if (prev_trans->state != TRANS_STATE_COMPLETED) {
  1744. refcount_inc(&prev_trans->use_count);
  1745. spin_unlock(&fs_info->trans_lock);
  1746. wait_for_commit(prev_trans);
  1747. ret = prev_trans->aborted;
  1748. btrfs_put_transaction(prev_trans);
  1749. if (ret)
  1750. goto cleanup_transaction;
  1751. } else {
  1752. spin_unlock(&fs_info->trans_lock);
  1753. }
  1754. } else {
  1755. spin_unlock(&fs_info->trans_lock);
  1756. }
  1757. extwriter_counter_dec(cur_trans, trans->type);
  1758. ret = btrfs_start_delalloc_flush(fs_info);
  1759. if (ret)
  1760. goto cleanup_transaction;
  1761. ret = btrfs_run_delayed_items(trans);
  1762. if (ret)
  1763. goto cleanup_transaction;
  1764. wait_event(cur_trans->writer_wait,
  1765. extwriter_counter_read(cur_trans) == 0);
  1766. /* some pending stuffs might be added after the previous flush. */
  1767. ret = btrfs_run_delayed_items(trans);
  1768. if (ret)
  1769. goto cleanup_transaction;
  1770. btrfs_wait_delalloc_flush(fs_info);
  1771. btrfs_wait_pending_ordered(cur_trans);
  1772. btrfs_scrub_pause(fs_info);
  1773. /*
  1774. * Ok now we need to make sure to block out any other joins while we
  1775. * commit the transaction. We could have started a join before setting
  1776. * COMMIT_DOING so make sure to wait for num_writers to == 1 again.
  1777. */
  1778. spin_lock(&fs_info->trans_lock);
  1779. cur_trans->state = TRANS_STATE_COMMIT_DOING;
  1780. spin_unlock(&fs_info->trans_lock);
  1781. wait_event(cur_trans->writer_wait,
  1782. atomic_read(&cur_trans->num_writers) == 1);
  1783. /* ->aborted might be set after the previous check, so check it */
  1784. if (unlikely(READ_ONCE(cur_trans->aborted))) {
  1785. ret = cur_trans->aborted;
  1786. goto scrub_continue;
  1787. }
  1788. /*
  1789. * the reloc mutex makes sure that we stop
  1790. * the balancing code from coming in and moving
  1791. * extents around in the middle of the commit
  1792. */
  1793. mutex_lock(&fs_info->reloc_mutex);
  1794. /*
  1795. * We needn't worry about the delayed items because we will
  1796. * deal with them in create_pending_snapshot(), which is the
  1797. * core function of the snapshot creation.
  1798. */
  1799. ret = create_pending_snapshots(trans);
  1800. if (ret) {
  1801. mutex_unlock(&fs_info->reloc_mutex);
  1802. goto scrub_continue;
  1803. }
  1804. /*
  1805. * We insert the dir indexes of the snapshots and update the inode
  1806. * of the snapshots' parents after the snapshot creation, so there
  1807. * are some delayed items which are not dealt with. Now deal with
  1808. * them.
  1809. *
  1810. * We needn't worry that this operation will corrupt the snapshots,
  1811. * because all the tree which are snapshoted will be forced to COW
  1812. * the nodes and leaves.
  1813. */
  1814. ret = btrfs_run_delayed_items(trans);
  1815. if (ret) {
  1816. mutex_unlock(&fs_info->reloc_mutex);
  1817. goto scrub_continue;
  1818. }
  1819. ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
  1820. if (ret) {
  1821. mutex_unlock(&fs_info->reloc_mutex);
  1822. goto scrub_continue;
  1823. }
  1824. /*
  1825. * make sure none of the code above managed to slip in a
  1826. * delayed item
  1827. */
  1828. btrfs_assert_delayed_root_empty(fs_info);
  1829. WARN_ON(cur_trans != trans->transaction);
  1830. /* btrfs_commit_tree_roots is responsible for getting the
  1831. * various roots consistent with each other. Every pointer
  1832. * in the tree of tree roots has to point to the most up to date
  1833. * root for every subvolume and other tree. So, we have to keep
  1834. * the tree logging code from jumping in and changing any
  1835. * of the trees.
  1836. *
  1837. * At this point in the commit, there can't be any tree-log
  1838. * writers, but a little lower down we drop the trans mutex
  1839. * and let new people in. By holding the tree_log_mutex
  1840. * from now until after the super is written, we avoid races
  1841. * with the tree-log code.
  1842. */
  1843. mutex_lock(&fs_info->tree_log_mutex);
  1844. ret = commit_fs_roots(trans);
  1845. if (ret) {
  1846. mutex_unlock(&fs_info->tree_log_mutex);
  1847. mutex_unlock(&fs_info->reloc_mutex);
  1848. goto scrub_continue;
  1849. }
  1850. /*
  1851. * Since the transaction is done, we can apply the pending changes
  1852. * before the next transaction.
  1853. */
  1854. btrfs_apply_pending_changes(fs_info);
  1855. /* commit_fs_roots gets rid of all the tree log roots, it is now
  1856. * safe to free the root of tree log roots
  1857. */
  1858. btrfs_free_log_root_tree(trans, fs_info);
  1859. /*
  1860. * commit_fs_roots() can call btrfs_save_ino_cache(), which generates
  1861. * new delayed refs. Must handle them or qgroup can be wrong.
  1862. */
  1863. ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
  1864. if (ret) {
  1865. mutex_unlock(&fs_info->tree_log_mutex);
  1866. mutex_unlock(&fs_info->reloc_mutex);
  1867. goto scrub_continue;
  1868. }
  1869. /*
  1870. * Since fs roots are all committed, we can get a quite accurate
  1871. * new_roots. So let's do quota accounting.
  1872. */
  1873. ret = btrfs_qgroup_account_extents(trans);
  1874. if (ret < 0) {
  1875. mutex_unlock(&fs_info->tree_log_mutex);
  1876. mutex_unlock(&fs_info->reloc_mutex);
  1877. goto scrub_continue;
  1878. }
  1879. ret = commit_cowonly_roots(trans);
  1880. if (ret) {
  1881. mutex_unlock(&fs_info->tree_log_mutex);
  1882. mutex_unlock(&fs_info->reloc_mutex);
  1883. goto scrub_continue;
  1884. }
  1885. /*
  1886. * The tasks which save the space cache and inode cache may also
  1887. * update ->aborted, check it.
  1888. */
  1889. if (unlikely(READ_ONCE(cur_trans->aborted))) {
  1890. ret = cur_trans->aborted;
  1891. mutex_unlock(&fs_info->tree_log_mutex);
  1892. mutex_unlock(&fs_info->reloc_mutex);
  1893. goto scrub_continue;
  1894. }
  1895. btrfs_prepare_extent_commit(fs_info);
  1896. cur_trans = fs_info->running_transaction;
  1897. btrfs_set_root_node(&fs_info->tree_root->root_item,
  1898. fs_info->tree_root->node);
  1899. list_add_tail(&fs_info->tree_root->dirty_list,
  1900. &cur_trans->switch_commits);
  1901. btrfs_set_root_node(&fs_info->chunk_root->root_item,
  1902. fs_info->chunk_root->node);
  1903. list_add_tail(&fs_info->chunk_root->dirty_list,
  1904. &cur_trans->switch_commits);
  1905. switch_commit_roots(cur_trans);
  1906. ASSERT(list_empty(&cur_trans->dirty_bgs));
  1907. ASSERT(list_empty(&cur_trans->io_bgs));
  1908. update_super_roots(fs_info);
  1909. btrfs_set_super_log_root(fs_info->super_copy, 0);
  1910. btrfs_set_super_log_root_level(fs_info->super_copy, 0);
  1911. memcpy(fs_info->super_for_commit, fs_info->super_copy,
  1912. sizeof(*fs_info->super_copy));
  1913. btrfs_update_commit_device_size(fs_info);
  1914. btrfs_update_commit_device_bytes_used(cur_trans);
  1915. clear_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags);
  1916. clear_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags);
  1917. btrfs_trans_release_chunk_metadata(trans);
  1918. spin_lock(&fs_info->trans_lock);
  1919. cur_trans->state = TRANS_STATE_UNBLOCKED;
  1920. fs_info->running_transaction = NULL;
  1921. spin_unlock(&fs_info->trans_lock);
  1922. mutex_unlock(&fs_info->reloc_mutex);
  1923. wake_up(&fs_info->transaction_wait);
  1924. ret = btrfs_write_and_wait_transaction(trans);
  1925. if (ret) {
  1926. btrfs_handle_fs_error(fs_info, ret,
  1927. "Error while writing out transaction");
  1928. mutex_unlock(&fs_info->tree_log_mutex);
  1929. goto scrub_continue;
  1930. }
  1931. ret = write_all_supers(fs_info, 0);
  1932. /*
  1933. * the super is written, we can safely allow the tree-loggers
  1934. * to go about their business
  1935. */
  1936. mutex_unlock(&fs_info->tree_log_mutex);
  1937. if (ret)
  1938. goto scrub_continue;
  1939. btrfs_finish_extent_commit(trans);
  1940. if (test_bit(BTRFS_TRANS_HAVE_FREE_BGS, &cur_trans->flags))
  1941. btrfs_clear_space_info_full(fs_info);
  1942. fs_info->last_trans_committed = cur_trans->transid;
  1943. /*
  1944. * We needn't acquire the lock here because there is no other task
  1945. * which can change it.
  1946. */
  1947. cur_trans->state = TRANS_STATE_COMPLETED;
  1948. wake_up(&cur_trans->commit_wait);
  1949. clear_bit(BTRFS_FS_NEED_ASYNC_COMMIT, &fs_info->flags);
  1950. spin_lock(&fs_info->trans_lock);
  1951. list_del_init(&cur_trans->list);
  1952. spin_unlock(&fs_info->trans_lock);
  1953. btrfs_put_transaction(cur_trans);
  1954. btrfs_put_transaction(cur_trans);
  1955. if (trans->type & __TRANS_FREEZABLE)
  1956. sb_end_intwrite(fs_info->sb);
  1957. trace_btrfs_transaction_commit(trans->root);
  1958. btrfs_scrub_continue(fs_info);
  1959. if (current->journal_info == trans)
  1960. current->journal_info = NULL;
  1961. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  1962. /*
  1963. * If fs has been frozen, we can not handle delayed iputs, otherwise
  1964. * it'll result in deadlock about SB_FREEZE_FS.
  1965. */
  1966. if (current != fs_info->transaction_kthread &&
  1967. current != fs_info->cleaner_kthread &&
  1968. !test_bit(BTRFS_FS_FROZEN, &fs_info->flags))
  1969. btrfs_run_delayed_iputs(fs_info);
  1970. return ret;
  1971. scrub_continue:
  1972. btrfs_scrub_continue(fs_info);
  1973. cleanup_transaction:
  1974. btrfs_trans_release_metadata(trans);
  1975. btrfs_trans_release_chunk_metadata(trans);
  1976. trans->block_rsv = NULL;
  1977. btrfs_warn(fs_info, "Skipping commit of aborted transaction.");
  1978. if (current->journal_info == trans)
  1979. current->journal_info = NULL;
  1980. cleanup_transaction(trans, ret);
  1981. return ret;
  1982. }
  1983. /*
  1984. * return < 0 if error
  1985. * 0 if there are no more dead_roots at the time of call
  1986. * 1 there are more to be processed, call me again
  1987. *
  1988. * The return value indicates there are certainly more snapshots to delete, but
  1989. * if there comes a new one during processing, it may return 0. We don't mind,
  1990. * because btrfs_commit_super will poke cleaner thread and it will process it a
  1991. * few seconds later.
  1992. */
  1993. int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root)
  1994. {
  1995. int ret;
  1996. struct btrfs_fs_info *fs_info = root->fs_info;
  1997. spin_lock(&fs_info->trans_lock);
  1998. if (list_empty(&fs_info->dead_roots)) {
  1999. spin_unlock(&fs_info->trans_lock);
  2000. return 0;
  2001. }
  2002. root = list_first_entry(&fs_info->dead_roots,
  2003. struct btrfs_root, root_list);
  2004. list_del_init(&root->root_list);
  2005. spin_unlock(&fs_info->trans_lock);
  2006. btrfs_debug(fs_info, "cleaner removing %llu", root->objectid);
  2007. btrfs_kill_all_delayed_nodes(root);
  2008. if (btrfs_header_backref_rev(root->node) <
  2009. BTRFS_MIXED_BACKREF_REV)
  2010. ret = btrfs_drop_snapshot(root, NULL, 0, 0);
  2011. else
  2012. ret = btrfs_drop_snapshot(root, NULL, 1, 0);
  2013. return (ret < 0) ? 0 : 1;
  2014. }
  2015. void btrfs_apply_pending_changes(struct btrfs_fs_info *fs_info)
  2016. {
  2017. unsigned long prev;
  2018. unsigned long bit;
  2019. prev = xchg(&fs_info->pending_changes, 0);
  2020. if (!prev)
  2021. return;
  2022. bit = 1 << BTRFS_PENDING_SET_INODE_MAP_CACHE;
  2023. if (prev & bit)
  2024. btrfs_set_opt(fs_info->mount_opt, INODE_MAP_CACHE);
  2025. prev &= ~bit;
  2026. bit = 1 << BTRFS_PENDING_CLEAR_INODE_MAP_CACHE;
  2027. if (prev & bit)
  2028. btrfs_clear_opt(fs_info->mount_opt, INODE_MAP_CACHE);
  2029. prev &= ~bit;
  2030. bit = 1 << BTRFS_PENDING_COMMIT;
  2031. if (prev & bit)
  2032. btrfs_debug(fs_info, "pending commit done");
  2033. prev &= ~bit;
  2034. if (prev)
  2035. btrfs_warn(fs_info,
  2036. "unknown pending changes left 0x%lx, ignoring", prev);
  2037. }