transaction.c 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711
  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/sched/mm.h>
  9. #include <linux/writeback.h>
  10. #include <linux/pagemap.h>
  11. #include <linux/blkdev.h>
  12. #include <linux/uuid.h>
  13. #include <linux/timekeeping.h>
  14. #include "misc.h"
  15. #include "ctree.h"
  16. #include "disk-io.h"
  17. #include "transaction.h"
  18. #include "locking.h"
  19. #include "tree-log.h"
  20. #include "volumes.h"
  21. #include "dev-replace.h"
  22. #include "qgroup.h"
  23. #include "block-group.h"
  24. #include "space-info.h"
  25. #include "fs.h"
  26. #include "accessors.h"
  27. #include "extent-tree.h"
  28. #include "root-tree.h"
  29. #include "dir-item.h"
  30. #include "uuid-tree.h"
  31. #include "ioctl.h"
  32. #include "relocation.h"
  33. #include "scrub.h"
  34. static struct kmem_cache *btrfs_trans_handle_cachep;
  35. /*
  36. * Transaction states and transitions
  37. *
  38. * No running transaction (fs tree blocks are not modified)
  39. * |
  40. * | To next stage:
  41. * | Call start_transaction() variants. Except btrfs_join_transaction_nostart().
  42. * V
  43. * Transaction N [[TRANS_STATE_RUNNING]]
  44. * |
  45. * | New trans handles can be attached to transaction N by calling all
  46. * | start_transaction() variants.
  47. * |
  48. * | To next stage:
  49. * | Call btrfs_commit_transaction() on any trans handle attached to
  50. * | transaction N
  51. * V
  52. * Transaction N [[TRANS_STATE_COMMIT_PREP]]
  53. * |
  54. * | If there are simultaneous calls to btrfs_commit_transaction() one will win
  55. * | the race and the rest will wait for the winner to commit the transaction.
  56. * |
  57. * | The winner will wait for previous running transaction to completely finish
  58. * | if there is one.
  59. * |
  60. * Transaction N [[TRANS_STATE_COMMIT_START]]
  61. * |
  62. * | Then one of the following happens:
  63. * | - Wait for all other trans handle holders to release.
  64. * | The btrfs_commit_transaction() caller will do the commit work.
  65. * | - Wait for current transaction to be committed by others.
  66. * | Other btrfs_commit_transaction() caller will do the commit work.
  67. * |
  68. * | At this stage, only btrfs_join_transaction*() variants can attach
  69. * | to this running transaction.
  70. * | All other variants will wait for current one to finish and attach to
  71. * | transaction N+1.
  72. * |
  73. * | To next stage:
  74. * | Caller is chosen to commit transaction N, and all other trans handle
  75. * | haven been released.
  76. * V
  77. * Transaction N [[TRANS_STATE_COMMIT_DOING]]
  78. * |
  79. * | The heavy lifting transaction work is started.
  80. * | From running delayed refs (modifying extent tree) to creating pending
  81. * | snapshots, running qgroups.
  82. * | In short, modify supporting trees to reflect modifications of subvolume
  83. * | trees.
  84. * |
  85. * | At this stage, all start_transaction() calls will wait for this
  86. * | transaction to finish and attach to transaction N+1.
  87. * |
  88. * | To next stage:
  89. * | Until all supporting trees are updated.
  90. * V
  91. * Transaction N [[TRANS_STATE_UNBLOCKED]]
  92. * | Transaction N+1
  93. * | All needed trees are modified, thus we only [[TRANS_STATE_RUNNING]]
  94. * | need to write them back to disk and update |
  95. * | super blocks. |
  96. * | |
  97. * | At this stage, new transaction is allowed to |
  98. * | start. |
  99. * | All new start_transaction() calls will be |
  100. * | attached to transid N+1. |
  101. * | |
  102. * | To next stage: |
  103. * | Until all tree blocks are super blocks are |
  104. * | written to block devices |
  105. * V |
  106. * Transaction N [[TRANS_STATE_COMPLETED]] V
  107. * All tree blocks and super blocks are written. Transaction N+1
  108. * This transaction is finished and all its [[TRANS_STATE_COMMIT_START]]
  109. * data structures will be cleaned up. | Life goes on
  110. */
  111. static const unsigned int btrfs_blocked_trans_types[TRANS_STATE_MAX] = {
  112. [TRANS_STATE_RUNNING] = 0U,
  113. [TRANS_STATE_COMMIT_PREP] = 0U,
  114. [TRANS_STATE_COMMIT_START] = (__TRANS_START | __TRANS_ATTACH),
  115. [TRANS_STATE_COMMIT_DOING] = (__TRANS_START |
  116. __TRANS_ATTACH |
  117. __TRANS_JOIN |
  118. __TRANS_JOIN_NOSTART),
  119. [TRANS_STATE_UNBLOCKED] = (__TRANS_START |
  120. __TRANS_ATTACH |
  121. __TRANS_JOIN |
  122. __TRANS_JOIN_NOLOCK |
  123. __TRANS_JOIN_NOSTART),
  124. [TRANS_STATE_SUPER_COMMITTED] = (__TRANS_START |
  125. __TRANS_ATTACH |
  126. __TRANS_JOIN |
  127. __TRANS_JOIN_NOLOCK |
  128. __TRANS_JOIN_NOSTART),
  129. [TRANS_STATE_COMPLETED] = (__TRANS_START |
  130. __TRANS_ATTACH |
  131. __TRANS_JOIN |
  132. __TRANS_JOIN_NOLOCK |
  133. __TRANS_JOIN_NOSTART),
  134. };
  135. void btrfs_put_transaction(struct btrfs_transaction *transaction)
  136. {
  137. WARN_ON(refcount_read(&transaction->use_count) == 0);
  138. if (refcount_dec_and_test(&transaction->use_count)) {
  139. BUG_ON(!list_empty(&transaction->list));
  140. WARN_ON(!RB_EMPTY_ROOT(
  141. &transaction->delayed_refs.href_root.rb_root));
  142. WARN_ON(!xa_empty(&transaction->delayed_refs.dirty_extents));
  143. if (transaction->delayed_refs.pending_csums)
  144. btrfs_err(transaction->fs_info,
  145. "pending csums is %llu",
  146. transaction->delayed_refs.pending_csums);
  147. /*
  148. * If any block groups are found in ->deleted_bgs then it's
  149. * because the transaction was aborted and a commit did not
  150. * happen (things failed before writing the new superblock
  151. * and calling btrfs_finish_extent_commit()), so we can not
  152. * discard the physical locations of the block groups.
  153. */
  154. while (!list_empty(&transaction->deleted_bgs)) {
  155. struct btrfs_block_group *cache;
  156. cache = list_first_entry(&transaction->deleted_bgs,
  157. struct btrfs_block_group,
  158. bg_list);
  159. /*
  160. * Not strictly necessary to lock, as no other task will be using a
  161. * block_group on the deleted_bgs list during a transaction abort.
  162. */
  163. spin_lock(&transaction->fs_info->unused_bgs_lock);
  164. list_del_init(&cache->bg_list);
  165. spin_unlock(&transaction->fs_info->unused_bgs_lock);
  166. btrfs_unfreeze_block_group(cache);
  167. btrfs_put_block_group(cache);
  168. }
  169. WARN_ON(!list_empty(&transaction->dev_update_list));
  170. kfree(transaction);
  171. }
  172. }
  173. static noinline void switch_commit_roots(struct btrfs_trans_handle *trans)
  174. {
  175. struct btrfs_transaction *cur_trans = trans->transaction;
  176. struct btrfs_fs_info *fs_info = trans->fs_info;
  177. struct btrfs_root *root, *tmp;
  178. /*
  179. * At this point no one can be using this transaction to modify any tree
  180. * and no one can start another transaction to modify any tree either.
  181. */
  182. ASSERT(cur_trans->state == TRANS_STATE_COMMIT_DOING);
  183. down_write(&fs_info->commit_root_sem);
  184. if (test_bit(BTRFS_FS_RELOC_RUNNING, &fs_info->flags))
  185. fs_info->last_reloc_trans = trans->transid;
  186. list_for_each_entry_safe(root, tmp, &cur_trans->switch_commits,
  187. dirty_list) {
  188. list_del_init(&root->dirty_list);
  189. free_extent_buffer(root->commit_root);
  190. root->commit_root = btrfs_root_node(root);
  191. extent_io_tree_release(&root->dirty_log_pages);
  192. btrfs_qgroup_clean_swapped_blocks(root);
  193. }
  194. /* We can free old roots now. */
  195. spin_lock(&cur_trans->dropped_roots_lock);
  196. while (!list_empty(&cur_trans->dropped_roots)) {
  197. root = list_first_entry(&cur_trans->dropped_roots,
  198. struct btrfs_root, root_list);
  199. list_del_init(&root->root_list);
  200. spin_unlock(&cur_trans->dropped_roots_lock);
  201. btrfs_free_log(trans, root);
  202. btrfs_drop_and_free_fs_root(fs_info, root);
  203. spin_lock(&cur_trans->dropped_roots_lock);
  204. }
  205. spin_unlock(&cur_trans->dropped_roots_lock);
  206. up_write(&fs_info->commit_root_sem);
  207. }
  208. static inline void extwriter_counter_inc(struct btrfs_transaction *trans,
  209. unsigned int type)
  210. {
  211. if (type & TRANS_EXTWRITERS)
  212. atomic_inc(&trans->num_extwriters);
  213. }
  214. static inline void extwriter_counter_dec(struct btrfs_transaction *trans,
  215. unsigned int type)
  216. {
  217. if (type & TRANS_EXTWRITERS)
  218. atomic_dec(&trans->num_extwriters);
  219. }
  220. static inline void extwriter_counter_init(struct btrfs_transaction *trans,
  221. unsigned int type)
  222. {
  223. atomic_set(&trans->num_extwriters, ((type & TRANS_EXTWRITERS) ? 1 : 0));
  224. }
  225. static inline int extwriter_counter_read(struct btrfs_transaction *trans)
  226. {
  227. return atomic_read(&trans->num_extwriters);
  228. }
  229. /*
  230. * To be called after doing the chunk btree updates right after allocating a new
  231. * chunk (after btrfs_chunk_alloc_add_chunk_item() is called), when removing a
  232. * chunk after all chunk btree updates and after finishing the second phase of
  233. * chunk allocation (btrfs_create_pending_block_groups()) in case some block
  234. * group had its chunk item insertion delayed to the second phase.
  235. */
  236. void btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle *trans)
  237. {
  238. struct btrfs_fs_info *fs_info = trans->fs_info;
  239. if (!trans->chunk_bytes_reserved)
  240. return;
  241. btrfs_block_rsv_release(fs_info, &fs_info->chunk_block_rsv,
  242. trans->chunk_bytes_reserved, NULL);
  243. trans->chunk_bytes_reserved = 0;
  244. }
  245. /*
  246. * either allocate a new transaction or hop into the existing one
  247. */
  248. static noinline int join_transaction(struct btrfs_fs_info *fs_info,
  249. unsigned int type)
  250. {
  251. struct btrfs_transaction *cur_trans;
  252. spin_lock(&fs_info->trans_lock);
  253. loop:
  254. /* The file system has been taken offline. No new transactions. */
  255. if (BTRFS_FS_ERROR(fs_info)) {
  256. spin_unlock(&fs_info->trans_lock);
  257. return -EROFS;
  258. }
  259. cur_trans = fs_info->running_transaction;
  260. if (cur_trans) {
  261. if (TRANS_ABORTED(cur_trans)) {
  262. const int abort_error = cur_trans->aborted;
  263. spin_unlock(&fs_info->trans_lock);
  264. return abort_error;
  265. }
  266. if (btrfs_blocked_trans_types[cur_trans->state] & type) {
  267. spin_unlock(&fs_info->trans_lock);
  268. return -EBUSY;
  269. }
  270. refcount_inc(&cur_trans->use_count);
  271. atomic_inc(&cur_trans->num_writers);
  272. extwriter_counter_inc(cur_trans, type);
  273. spin_unlock(&fs_info->trans_lock);
  274. btrfs_lockdep_acquire(fs_info, btrfs_trans_num_writers);
  275. btrfs_lockdep_acquire(fs_info, btrfs_trans_num_extwriters);
  276. return 0;
  277. }
  278. spin_unlock(&fs_info->trans_lock);
  279. /*
  280. * If we are ATTACH or TRANS_JOIN_NOSTART, we just want to catch the
  281. * current transaction, and commit it. If there is no transaction, just
  282. * return ENOENT.
  283. */
  284. if (type == TRANS_ATTACH || type == TRANS_JOIN_NOSTART)
  285. return -ENOENT;
  286. /*
  287. * JOIN_NOLOCK only happens during the transaction commit, so
  288. * it is impossible that ->running_transaction is NULL
  289. */
  290. BUG_ON(type == TRANS_JOIN_NOLOCK);
  291. cur_trans = kmalloc(sizeof(*cur_trans), GFP_NOFS);
  292. if (!cur_trans)
  293. return -ENOMEM;
  294. btrfs_lockdep_acquire(fs_info, btrfs_trans_num_writers);
  295. btrfs_lockdep_acquire(fs_info, btrfs_trans_num_extwriters);
  296. spin_lock(&fs_info->trans_lock);
  297. if (fs_info->running_transaction) {
  298. /*
  299. * someone started a transaction after we unlocked. Make sure
  300. * to redo the checks above
  301. */
  302. btrfs_lockdep_release(fs_info, btrfs_trans_num_extwriters);
  303. btrfs_lockdep_release(fs_info, btrfs_trans_num_writers);
  304. kfree(cur_trans);
  305. goto loop;
  306. } else if (BTRFS_FS_ERROR(fs_info)) {
  307. spin_unlock(&fs_info->trans_lock);
  308. btrfs_lockdep_release(fs_info, btrfs_trans_num_extwriters);
  309. btrfs_lockdep_release(fs_info, btrfs_trans_num_writers);
  310. kfree(cur_trans);
  311. return -EROFS;
  312. }
  313. cur_trans->fs_info = fs_info;
  314. atomic_set(&cur_trans->pending_ordered, 0);
  315. init_waitqueue_head(&cur_trans->pending_wait);
  316. atomic_set(&cur_trans->num_writers, 1);
  317. extwriter_counter_init(cur_trans, type);
  318. init_waitqueue_head(&cur_trans->writer_wait);
  319. init_waitqueue_head(&cur_trans->commit_wait);
  320. cur_trans->state = TRANS_STATE_RUNNING;
  321. /*
  322. * One for this trans handle, one so it will live on until we
  323. * commit the transaction.
  324. */
  325. refcount_set(&cur_trans->use_count, 2);
  326. cur_trans->flags = 0;
  327. cur_trans->start_time = ktime_get_seconds();
  328. memset(&cur_trans->delayed_refs, 0, sizeof(cur_trans->delayed_refs));
  329. cur_trans->delayed_refs.href_root = RB_ROOT_CACHED;
  330. xa_init(&cur_trans->delayed_refs.dirty_extents);
  331. atomic_set(&cur_trans->delayed_refs.num_entries, 0);
  332. /*
  333. * although the tree mod log is per file system and not per transaction,
  334. * the log must never go across transaction boundaries.
  335. */
  336. smp_mb();
  337. if (!list_empty(&fs_info->tree_mod_seq_list))
  338. WARN(1, KERN_ERR "BTRFS: tree_mod_seq_list not empty when creating a fresh transaction\n");
  339. if (!RB_EMPTY_ROOT(&fs_info->tree_mod_log))
  340. WARN(1, KERN_ERR "BTRFS: tree_mod_log rb tree not empty when creating a fresh transaction\n");
  341. atomic64_set(&fs_info->tree_mod_seq, 0);
  342. spin_lock_init(&cur_trans->delayed_refs.lock);
  343. INIT_LIST_HEAD(&cur_trans->pending_snapshots);
  344. INIT_LIST_HEAD(&cur_trans->dev_update_list);
  345. INIT_LIST_HEAD(&cur_trans->switch_commits);
  346. INIT_LIST_HEAD(&cur_trans->dirty_bgs);
  347. INIT_LIST_HEAD(&cur_trans->io_bgs);
  348. INIT_LIST_HEAD(&cur_trans->dropped_roots);
  349. mutex_init(&cur_trans->cache_write_mutex);
  350. spin_lock_init(&cur_trans->dirty_bgs_lock);
  351. INIT_LIST_HEAD(&cur_trans->deleted_bgs);
  352. spin_lock_init(&cur_trans->dropped_roots_lock);
  353. list_add_tail(&cur_trans->list, &fs_info->trans_list);
  354. extent_io_tree_init(fs_info, &cur_trans->dirty_pages,
  355. IO_TREE_TRANS_DIRTY_PAGES);
  356. extent_io_tree_init(fs_info, &cur_trans->pinned_extents,
  357. IO_TREE_FS_PINNED_EXTENTS);
  358. btrfs_set_fs_generation(fs_info, fs_info->generation + 1);
  359. cur_trans->transid = fs_info->generation;
  360. fs_info->running_transaction = cur_trans;
  361. cur_trans->aborted = 0;
  362. spin_unlock(&fs_info->trans_lock);
  363. return 0;
  364. }
  365. /*
  366. * This does all the record keeping required to make sure that a shareable root
  367. * is properly recorded in a given transaction. This is required to make sure
  368. * the old root from before we joined the transaction is deleted when the
  369. * transaction commits.
  370. */
  371. static int record_root_in_trans(struct btrfs_trans_handle *trans,
  372. struct btrfs_root *root,
  373. int force)
  374. {
  375. struct btrfs_fs_info *fs_info = root->fs_info;
  376. int ret = 0;
  377. if ((test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
  378. btrfs_get_root_last_trans(root) < trans->transid) || force) {
  379. WARN_ON(!force && root->commit_root != root->node);
  380. /*
  381. * see below for IN_TRANS_SETUP usage rules
  382. * we have the reloc mutex held now, so there
  383. * is only one writer in this function
  384. */
  385. set_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state);
  386. /* make sure readers find IN_TRANS_SETUP before
  387. * they find our root->last_trans update
  388. */
  389. smp_wmb();
  390. spin_lock(&fs_info->fs_roots_radix_lock);
  391. if (btrfs_get_root_last_trans(root) == trans->transid && !force) {
  392. spin_unlock(&fs_info->fs_roots_radix_lock);
  393. return 0;
  394. }
  395. radix_tree_tag_set(&fs_info->fs_roots_radix,
  396. (unsigned long)btrfs_root_id(root),
  397. BTRFS_ROOT_TRANS_TAG);
  398. spin_unlock(&fs_info->fs_roots_radix_lock);
  399. btrfs_set_root_last_trans(root, trans->transid);
  400. /* this is pretty tricky. We don't want to
  401. * take the relocation lock in btrfs_record_root_in_trans
  402. * unless we're really doing the first setup for this root in
  403. * this transaction.
  404. *
  405. * Normally we'd use root->last_trans as a flag to decide
  406. * if we want to take the expensive mutex.
  407. *
  408. * But, we have to set root->last_trans before we
  409. * init the relocation root, otherwise, we trip over warnings
  410. * in ctree.c. The solution used here is to flag ourselves
  411. * with root IN_TRANS_SETUP. When this is 1, we're still
  412. * fixing up the reloc trees and everyone must wait.
  413. *
  414. * When this is zero, they can trust root->last_trans and fly
  415. * through btrfs_record_root_in_trans without having to take the
  416. * lock. smp_wmb() makes sure that all the writes above are
  417. * done before we pop in the zero below
  418. */
  419. ret = btrfs_init_reloc_root(trans, root);
  420. smp_mb__before_atomic();
  421. clear_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state);
  422. }
  423. return ret;
  424. }
  425. void btrfs_add_dropped_root(struct btrfs_trans_handle *trans,
  426. struct btrfs_root *root)
  427. {
  428. struct btrfs_fs_info *fs_info = root->fs_info;
  429. struct btrfs_transaction *cur_trans = trans->transaction;
  430. /* Add ourselves to the transaction dropped list */
  431. spin_lock(&cur_trans->dropped_roots_lock);
  432. list_add_tail(&root->root_list, &cur_trans->dropped_roots);
  433. spin_unlock(&cur_trans->dropped_roots_lock);
  434. /* Make sure we don't try to update the root at commit time */
  435. spin_lock(&fs_info->fs_roots_radix_lock);
  436. radix_tree_tag_clear(&fs_info->fs_roots_radix,
  437. (unsigned long)btrfs_root_id(root),
  438. BTRFS_ROOT_TRANS_TAG);
  439. spin_unlock(&fs_info->fs_roots_radix_lock);
  440. }
  441. int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
  442. struct btrfs_root *root)
  443. {
  444. struct btrfs_fs_info *fs_info = root->fs_info;
  445. int ret;
  446. if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
  447. return 0;
  448. /*
  449. * see record_root_in_trans for comments about IN_TRANS_SETUP usage
  450. * and barriers
  451. */
  452. smp_rmb();
  453. if (btrfs_get_root_last_trans(root) == trans->transid &&
  454. !test_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state))
  455. return 0;
  456. mutex_lock(&fs_info->reloc_mutex);
  457. ret = record_root_in_trans(trans, root, 0);
  458. mutex_unlock(&fs_info->reloc_mutex);
  459. return ret;
  460. }
  461. static inline int is_transaction_blocked(struct btrfs_transaction *trans)
  462. {
  463. return (trans->state >= TRANS_STATE_COMMIT_START &&
  464. trans->state < TRANS_STATE_UNBLOCKED &&
  465. !TRANS_ABORTED(trans));
  466. }
  467. /* wait for commit against the current transaction to become unblocked
  468. * when this is done, it is safe to start a new transaction, but the current
  469. * transaction might not be fully on disk.
  470. */
  471. static void wait_current_trans(struct btrfs_fs_info *fs_info)
  472. {
  473. struct btrfs_transaction *cur_trans;
  474. spin_lock(&fs_info->trans_lock);
  475. cur_trans = fs_info->running_transaction;
  476. if (cur_trans && is_transaction_blocked(cur_trans)) {
  477. refcount_inc(&cur_trans->use_count);
  478. spin_unlock(&fs_info->trans_lock);
  479. btrfs_might_wait_for_state(fs_info, BTRFS_LOCKDEP_TRANS_UNBLOCKED);
  480. wait_event(fs_info->transaction_wait,
  481. cur_trans->state >= TRANS_STATE_UNBLOCKED ||
  482. TRANS_ABORTED(cur_trans));
  483. btrfs_put_transaction(cur_trans);
  484. } else {
  485. spin_unlock(&fs_info->trans_lock);
  486. }
  487. }
  488. static int may_wait_transaction(struct btrfs_fs_info *fs_info, int type)
  489. {
  490. if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
  491. return 0;
  492. if (type == TRANS_START)
  493. return 1;
  494. return 0;
  495. }
  496. static inline bool need_reserve_reloc_root(struct btrfs_root *root)
  497. {
  498. struct btrfs_fs_info *fs_info = root->fs_info;
  499. if (!fs_info->reloc_ctl ||
  500. !test_bit(BTRFS_ROOT_SHAREABLE, &root->state) ||
  501. btrfs_root_id(root) == BTRFS_TREE_RELOC_OBJECTID ||
  502. root->reloc_root)
  503. return false;
  504. return true;
  505. }
  506. static int btrfs_reserve_trans_metadata(struct btrfs_fs_info *fs_info,
  507. enum btrfs_reserve_flush_enum flush,
  508. u64 num_bytes,
  509. u64 *delayed_refs_bytes)
  510. {
  511. struct btrfs_space_info *si = fs_info->trans_block_rsv.space_info;
  512. u64 bytes = num_bytes + *delayed_refs_bytes;
  513. int ret;
  514. /*
  515. * We want to reserve all the bytes we may need all at once, so we only
  516. * do 1 enospc flushing cycle per transaction start.
  517. */
  518. ret = btrfs_reserve_metadata_bytes(fs_info, si, bytes, flush);
  519. /*
  520. * If we are an emergency flush, which can steal from the global block
  521. * reserve, then attempt to not reserve space for the delayed refs, as
  522. * we will consume space for them from the global block reserve.
  523. */
  524. if (ret && flush == BTRFS_RESERVE_FLUSH_ALL_STEAL) {
  525. bytes -= *delayed_refs_bytes;
  526. *delayed_refs_bytes = 0;
  527. ret = btrfs_reserve_metadata_bytes(fs_info, si, bytes, flush);
  528. }
  529. return ret;
  530. }
  531. static struct btrfs_trans_handle *
  532. start_transaction(struct btrfs_root *root, unsigned int num_items,
  533. unsigned int type, enum btrfs_reserve_flush_enum flush,
  534. bool enforce_qgroups)
  535. {
  536. struct btrfs_fs_info *fs_info = root->fs_info;
  537. struct btrfs_block_rsv *delayed_refs_rsv = &fs_info->delayed_refs_rsv;
  538. struct btrfs_block_rsv *trans_rsv = &fs_info->trans_block_rsv;
  539. struct btrfs_trans_handle *h;
  540. struct btrfs_transaction *cur_trans;
  541. u64 num_bytes = 0;
  542. u64 qgroup_reserved = 0;
  543. u64 delayed_refs_bytes = 0;
  544. bool reloc_reserved = false;
  545. bool do_chunk_alloc = false;
  546. int ret;
  547. if (BTRFS_FS_ERROR(fs_info))
  548. return ERR_PTR(-EROFS);
  549. if (current->journal_info) {
  550. WARN_ON(type & TRANS_EXTWRITERS);
  551. h = current->journal_info;
  552. refcount_inc(&h->use_count);
  553. WARN_ON(refcount_read(&h->use_count) > 2);
  554. h->orig_rsv = h->block_rsv;
  555. h->block_rsv = NULL;
  556. goto got_it;
  557. }
  558. /*
  559. * Do the reservation before we join the transaction so we can do all
  560. * the appropriate flushing if need be.
  561. */
  562. if (num_items && root != fs_info->chunk_root) {
  563. qgroup_reserved = num_items * fs_info->nodesize;
  564. /*
  565. * Use prealloc for now, as there might be a currently running
  566. * transaction that could free this reserved space prematurely
  567. * by committing.
  568. */
  569. ret = btrfs_qgroup_reserve_meta_prealloc(root, qgroup_reserved,
  570. enforce_qgroups, false);
  571. if (ret)
  572. return ERR_PTR(ret);
  573. num_bytes = btrfs_calc_insert_metadata_size(fs_info, num_items);
  574. /*
  575. * If we plan to insert/update/delete "num_items" from a btree,
  576. * we will also generate delayed refs for extent buffers in the
  577. * respective btree paths, so reserve space for the delayed refs
  578. * that will be generated by the caller as it modifies btrees.
  579. * Try to reserve them to avoid excessive use of the global
  580. * block reserve.
  581. */
  582. delayed_refs_bytes = btrfs_calc_delayed_ref_bytes(fs_info, num_items);
  583. /*
  584. * Do the reservation for the relocation root creation
  585. */
  586. if (need_reserve_reloc_root(root)) {
  587. num_bytes += fs_info->nodesize;
  588. reloc_reserved = true;
  589. }
  590. ret = btrfs_reserve_trans_metadata(fs_info, flush, num_bytes,
  591. &delayed_refs_bytes);
  592. if (ret)
  593. goto reserve_fail;
  594. btrfs_block_rsv_add_bytes(trans_rsv, num_bytes, true);
  595. if (trans_rsv->space_info->force_alloc)
  596. do_chunk_alloc = true;
  597. } else if (num_items == 0 && flush == BTRFS_RESERVE_FLUSH_ALL &&
  598. !btrfs_block_rsv_full(delayed_refs_rsv)) {
  599. /*
  600. * Some people call with btrfs_start_transaction(root, 0)
  601. * because they can be throttled, but have some other mechanism
  602. * for reserving space. We still want these guys to refill the
  603. * delayed block_rsv so just add 1 items worth of reservation
  604. * here.
  605. */
  606. ret = btrfs_delayed_refs_rsv_refill(fs_info, flush);
  607. if (ret)
  608. goto reserve_fail;
  609. }
  610. again:
  611. h = kmem_cache_zalloc(btrfs_trans_handle_cachep, GFP_NOFS);
  612. if (!h) {
  613. ret = -ENOMEM;
  614. goto alloc_fail;
  615. }
  616. /*
  617. * If we are JOIN_NOLOCK we're already committing a transaction and
  618. * waiting on this guy, so we don't need to do the sb_start_intwrite
  619. * because we're already holding a ref. We need this because we could
  620. * have raced in and did an fsync() on a file which can kick a commit
  621. * and then we deadlock with somebody doing a freeze.
  622. *
  623. * If we are ATTACH, it means we just want to catch the current
  624. * transaction and commit it, so we needn't do sb_start_intwrite().
  625. */
  626. if (type & __TRANS_FREEZABLE)
  627. sb_start_intwrite(fs_info->sb);
  628. if (may_wait_transaction(fs_info, type))
  629. wait_current_trans(fs_info);
  630. do {
  631. ret = join_transaction(fs_info, type);
  632. if (ret == -EBUSY) {
  633. wait_current_trans(fs_info);
  634. if (unlikely(type == TRANS_ATTACH ||
  635. type == TRANS_JOIN_NOSTART))
  636. ret = -ENOENT;
  637. }
  638. } while (ret == -EBUSY);
  639. if (ret < 0)
  640. goto join_fail;
  641. cur_trans = fs_info->running_transaction;
  642. h->transid = cur_trans->transid;
  643. h->transaction = cur_trans;
  644. refcount_set(&h->use_count, 1);
  645. h->fs_info = root->fs_info;
  646. h->type = type;
  647. INIT_LIST_HEAD(&h->new_bgs);
  648. btrfs_init_metadata_block_rsv(fs_info, &h->delayed_rsv, BTRFS_BLOCK_RSV_DELOPS);
  649. smp_mb();
  650. if (cur_trans->state >= TRANS_STATE_COMMIT_START &&
  651. may_wait_transaction(fs_info, type)) {
  652. current->journal_info = h;
  653. btrfs_commit_transaction(h);
  654. goto again;
  655. }
  656. if (num_bytes) {
  657. trace_btrfs_space_reservation(fs_info, "transaction",
  658. h->transid, num_bytes, 1);
  659. h->block_rsv = trans_rsv;
  660. h->bytes_reserved = num_bytes;
  661. if (delayed_refs_bytes > 0) {
  662. trace_btrfs_space_reservation(fs_info,
  663. "local_delayed_refs_rsv",
  664. h->transid,
  665. delayed_refs_bytes, 1);
  666. h->delayed_refs_bytes_reserved = delayed_refs_bytes;
  667. btrfs_block_rsv_add_bytes(&h->delayed_rsv, delayed_refs_bytes, true);
  668. delayed_refs_bytes = 0;
  669. }
  670. h->reloc_reserved = reloc_reserved;
  671. }
  672. got_it:
  673. if (!current->journal_info)
  674. current->journal_info = h;
  675. /*
  676. * If the space_info is marked ALLOC_FORCE then we'll get upgraded to
  677. * ALLOC_FORCE the first run through, and then we won't allocate for
  678. * anybody else who races in later. We don't care about the return
  679. * value here.
  680. */
  681. if (do_chunk_alloc && num_bytes) {
  682. u64 flags = h->block_rsv->space_info->flags;
  683. btrfs_chunk_alloc(h, btrfs_get_alloc_profile(fs_info, flags),
  684. CHUNK_ALLOC_NO_FORCE);
  685. }
  686. /*
  687. * btrfs_record_root_in_trans() needs to alloc new extents, and may
  688. * call btrfs_join_transaction() while we're also starting a
  689. * transaction.
  690. *
  691. * Thus it need to be called after current->journal_info initialized,
  692. * or we can deadlock.
  693. */
  694. ret = btrfs_record_root_in_trans(h, root);
  695. if (ret) {
  696. /*
  697. * The transaction handle is fully initialized and linked with
  698. * other structures so it needs to be ended in case of errors,
  699. * not just freed.
  700. */
  701. btrfs_end_transaction(h);
  702. goto reserve_fail;
  703. }
  704. /*
  705. * Now that we have found a transaction to be a part of, convert the
  706. * qgroup reservation from prealloc to pertrans. A different transaction
  707. * can't race in and free our pertrans out from under us.
  708. */
  709. if (qgroup_reserved)
  710. btrfs_qgroup_convert_reserved_meta(root, qgroup_reserved);
  711. return h;
  712. join_fail:
  713. if (type & __TRANS_FREEZABLE)
  714. sb_end_intwrite(fs_info->sb);
  715. kmem_cache_free(btrfs_trans_handle_cachep, h);
  716. alloc_fail:
  717. if (num_bytes)
  718. btrfs_block_rsv_release(fs_info, trans_rsv, num_bytes, NULL);
  719. if (delayed_refs_bytes)
  720. btrfs_space_info_free_bytes_may_use(fs_info, trans_rsv->space_info,
  721. delayed_refs_bytes);
  722. reserve_fail:
  723. btrfs_qgroup_free_meta_prealloc(root, qgroup_reserved);
  724. return ERR_PTR(ret);
  725. }
  726. struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
  727. unsigned int num_items)
  728. {
  729. return start_transaction(root, num_items, TRANS_START,
  730. BTRFS_RESERVE_FLUSH_ALL, true);
  731. }
  732. struct btrfs_trans_handle *btrfs_start_transaction_fallback_global_rsv(
  733. struct btrfs_root *root,
  734. unsigned int num_items)
  735. {
  736. return start_transaction(root, num_items, TRANS_START,
  737. BTRFS_RESERVE_FLUSH_ALL_STEAL, false);
  738. }
  739. struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root)
  740. {
  741. return start_transaction(root, 0, TRANS_JOIN, BTRFS_RESERVE_NO_FLUSH,
  742. true);
  743. }
  744. struct btrfs_trans_handle *btrfs_join_transaction_spacecache(struct btrfs_root *root)
  745. {
  746. return start_transaction(root, 0, TRANS_JOIN_NOLOCK,
  747. BTRFS_RESERVE_NO_FLUSH, true);
  748. }
  749. /*
  750. * Similar to regular join but it never starts a transaction when none is
  751. * running or when there's a running one at a state >= TRANS_STATE_UNBLOCKED.
  752. * This is similar to btrfs_attach_transaction() but it allows the join to
  753. * happen if the transaction commit already started but it's not yet in the
  754. * "doing" phase (the state is < TRANS_STATE_COMMIT_DOING).
  755. */
  756. struct btrfs_trans_handle *btrfs_join_transaction_nostart(struct btrfs_root *root)
  757. {
  758. return start_transaction(root, 0, TRANS_JOIN_NOSTART,
  759. BTRFS_RESERVE_NO_FLUSH, true);
  760. }
  761. /*
  762. * Catch the running transaction.
  763. *
  764. * It is used when we want to commit the current the transaction, but
  765. * don't want to start a new one.
  766. *
  767. * Note: If this function return -ENOENT, it just means there is no
  768. * running transaction. But it is possible that the inactive transaction
  769. * is still in the memory, not fully on disk. If you hope there is no
  770. * inactive transaction in the fs when -ENOENT is returned, you should
  771. * invoke
  772. * btrfs_attach_transaction_barrier()
  773. */
  774. struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root)
  775. {
  776. return start_transaction(root, 0, TRANS_ATTACH,
  777. BTRFS_RESERVE_NO_FLUSH, true);
  778. }
  779. /*
  780. * Catch the running transaction.
  781. *
  782. * It is similar to the above function, the difference is this one
  783. * will wait for all the inactive transactions until they fully
  784. * complete.
  785. */
  786. struct btrfs_trans_handle *
  787. btrfs_attach_transaction_barrier(struct btrfs_root *root)
  788. {
  789. struct btrfs_trans_handle *trans;
  790. trans = start_transaction(root, 0, TRANS_ATTACH,
  791. BTRFS_RESERVE_NO_FLUSH, true);
  792. if (trans == ERR_PTR(-ENOENT)) {
  793. int ret;
  794. ret = btrfs_wait_for_commit(root->fs_info, 0);
  795. if (ret)
  796. return ERR_PTR(ret);
  797. }
  798. return trans;
  799. }
  800. /* Wait for a transaction commit to reach at least the given state. */
  801. static noinline void wait_for_commit(struct btrfs_transaction *commit,
  802. const enum btrfs_trans_state min_state)
  803. {
  804. struct btrfs_fs_info *fs_info = commit->fs_info;
  805. u64 transid = commit->transid;
  806. bool put = false;
  807. /*
  808. * At the moment this function is called with min_state either being
  809. * TRANS_STATE_COMPLETED or TRANS_STATE_SUPER_COMMITTED.
  810. */
  811. if (min_state == TRANS_STATE_COMPLETED)
  812. btrfs_might_wait_for_state(fs_info, BTRFS_LOCKDEP_TRANS_COMPLETED);
  813. else
  814. btrfs_might_wait_for_state(fs_info, BTRFS_LOCKDEP_TRANS_SUPER_COMMITTED);
  815. while (1) {
  816. wait_event(commit->commit_wait, commit->state >= min_state);
  817. if (put)
  818. btrfs_put_transaction(commit);
  819. if (min_state < TRANS_STATE_COMPLETED)
  820. break;
  821. /*
  822. * A transaction isn't really completed until all of the
  823. * previous transactions are completed, but with fsync we can
  824. * end up with SUPER_COMMITTED transactions before a COMPLETED
  825. * transaction. Wait for those.
  826. */
  827. spin_lock(&fs_info->trans_lock);
  828. commit = list_first_entry_or_null(&fs_info->trans_list,
  829. struct btrfs_transaction,
  830. list);
  831. if (!commit || commit->transid > transid) {
  832. spin_unlock(&fs_info->trans_lock);
  833. break;
  834. }
  835. refcount_inc(&commit->use_count);
  836. put = true;
  837. spin_unlock(&fs_info->trans_lock);
  838. }
  839. }
  840. int btrfs_wait_for_commit(struct btrfs_fs_info *fs_info, u64 transid)
  841. {
  842. struct btrfs_transaction *cur_trans = NULL, *t;
  843. int ret = 0;
  844. if (transid) {
  845. if (transid <= btrfs_get_last_trans_committed(fs_info))
  846. goto out;
  847. /* find specified transaction */
  848. spin_lock(&fs_info->trans_lock);
  849. list_for_each_entry(t, &fs_info->trans_list, list) {
  850. if (t->transid == transid) {
  851. cur_trans = t;
  852. refcount_inc(&cur_trans->use_count);
  853. ret = 0;
  854. break;
  855. }
  856. if (t->transid > transid) {
  857. ret = 0;
  858. break;
  859. }
  860. }
  861. spin_unlock(&fs_info->trans_lock);
  862. /*
  863. * The specified transaction doesn't exist, or we
  864. * raced with btrfs_commit_transaction
  865. */
  866. if (!cur_trans) {
  867. if (transid > btrfs_get_last_trans_committed(fs_info))
  868. ret = -EINVAL;
  869. goto out;
  870. }
  871. } else {
  872. /* find newest transaction that is committing | committed */
  873. spin_lock(&fs_info->trans_lock);
  874. list_for_each_entry_reverse(t, &fs_info->trans_list,
  875. list) {
  876. if (t->state >= TRANS_STATE_COMMIT_START) {
  877. if (t->state == TRANS_STATE_COMPLETED)
  878. break;
  879. cur_trans = t;
  880. refcount_inc(&cur_trans->use_count);
  881. break;
  882. }
  883. }
  884. spin_unlock(&fs_info->trans_lock);
  885. if (!cur_trans)
  886. goto out; /* nothing committing|committed */
  887. }
  888. wait_for_commit(cur_trans, TRANS_STATE_COMPLETED);
  889. ret = cur_trans->aborted;
  890. btrfs_put_transaction(cur_trans);
  891. out:
  892. return ret;
  893. }
  894. void btrfs_throttle(struct btrfs_fs_info *fs_info)
  895. {
  896. wait_current_trans(fs_info);
  897. }
  898. bool btrfs_should_end_transaction(struct btrfs_trans_handle *trans)
  899. {
  900. struct btrfs_transaction *cur_trans = trans->transaction;
  901. if (cur_trans->state >= TRANS_STATE_COMMIT_START ||
  902. test_bit(BTRFS_DELAYED_REFS_FLUSHING, &cur_trans->delayed_refs.flags))
  903. return true;
  904. if (btrfs_check_space_for_delayed_refs(trans->fs_info))
  905. return true;
  906. return !!btrfs_block_rsv_check(&trans->fs_info->global_block_rsv, 50);
  907. }
  908. static void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans)
  909. {
  910. struct btrfs_fs_info *fs_info = trans->fs_info;
  911. if (!trans->block_rsv) {
  912. ASSERT(!trans->bytes_reserved);
  913. ASSERT(!trans->delayed_refs_bytes_reserved);
  914. return;
  915. }
  916. if (!trans->bytes_reserved) {
  917. ASSERT(!trans->delayed_refs_bytes_reserved);
  918. return;
  919. }
  920. ASSERT(trans->block_rsv == &fs_info->trans_block_rsv);
  921. trace_btrfs_space_reservation(fs_info, "transaction",
  922. trans->transid, trans->bytes_reserved, 0);
  923. btrfs_block_rsv_release(fs_info, trans->block_rsv,
  924. trans->bytes_reserved, NULL);
  925. trans->bytes_reserved = 0;
  926. if (!trans->delayed_refs_bytes_reserved)
  927. return;
  928. trace_btrfs_space_reservation(fs_info, "local_delayed_refs_rsv",
  929. trans->transid,
  930. trans->delayed_refs_bytes_reserved, 0);
  931. btrfs_block_rsv_release(fs_info, &trans->delayed_rsv,
  932. trans->delayed_refs_bytes_reserved, NULL);
  933. trans->delayed_refs_bytes_reserved = 0;
  934. }
  935. static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
  936. int throttle)
  937. {
  938. struct btrfs_fs_info *info = trans->fs_info;
  939. struct btrfs_transaction *cur_trans = trans->transaction;
  940. int ret = 0;
  941. if (refcount_read(&trans->use_count) > 1) {
  942. refcount_dec(&trans->use_count);
  943. trans->block_rsv = trans->orig_rsv;
  944. return 0;
  945. }
  946. btrfs_trans_release_metadata(trans);
  947. trans->block_rsv = NULL;
  948. btrfs_create_pending_block_groups(trans);
  949. btrfs_trans_release_chunk_metadata(trans);
  950. if (trans->type & __TRANS_FREEZABLE)
  951. sb_end_intwrite(info->sb);
  952. WARN_ON(cur_trans != info->running_transaction);
  953. WARN_ON(atomic_read(&cur_trans->num_writers) < 1);
  954. atomic_dec(&cur_trans->num_writers);
  955. extwriter_counter_dec(cur_trans, trans->type);
  956. cond_wake_up(&cur_trans->writer_wait);
  957. btrfs_lockdep_release(info, btrfs_trans_num_extwriters);
  958. btrfs_lockdep_release(info, btrfs_trans_num_writers);
  959. btrfs_put_transaction(cur_trans);
  960. if (current->journal_info == trans)
  961. current->journal_info = NULL;
  962. if (throttle)
  963. btrfs_run_delayed_iputs(info);
  964. if (TRANS_ABORTED(trans) || BTRFS_FS_ERROR(info)) {
  965. wake_up_process(info->transaction_kthread);
  966. if (TRANS_ABORTED(trans))
  967. ret = trans->aborted;
  968. else
  969. ret = -EROFS;
  970. }
  971. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  972. return ret;
  973. }
  974. int btrfs_end_transaction(struct btrfs_trans_handle *trans)
  975. {
  976. return __btrfs_end_transaction(trans, 0);
  977. }
  978. int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans)
  979. {
  980. return __btrfs_end_transaction(trans, 1);
  981. }
  982. /*
  983. * when btree blocks are allocated, they have some corresponding bits set for
  984. * them in one of two extent_io trees. This is used to make sure all of
  985. * those extents are sent to disk but does not wait on them
  986. */
  987. int btrfs_write_marked_extents(struct btrfs_fs_info *fs_info,
  988. struct extent_io_tree *dirty_pages, int mark)
  989. {
  990. int ret = 0;
  991. struct address_space *mapping = fs_info->btree_inode->i_mapping;
  992. struct extent_state *cached_state = NULL;
  993. u64 start = 0;
  994. u64 end;
  995. while (find_first_extent_bit(dirty_pages, start, &start, &end,
  996. mark, &cached_state)) {
  997. bool wait_writeback = false;
  998. ret = convert_extent_bit(dirty_pages, start, end,
  999. EXTENT_NEED_WAIT,
  1000. mark, &cached_state);
  1001. /*
  1002. * convert_extent_bit can return -ENOMEM, which is most of the
  1003. * time a temporary error. So when it happens, ignore the error
  1004. * and wait for writeback of this range to finish - because we
  1005. * failed to set the bit EXTENT_NEED_WAIT for the range, a call
  1006. * to __btrfs_wait_marked_extents() would not know that
  1007. * writeback for this range started and therefore wouldn't
  1008. * wait for it to finish - we don't want to commit a
  1009. * superblock that points to btree nodes/leafs for which
  1010. * writeback hasn't finished yet (and without errors).
  1011. * We cleanup any entries left in the io tree when committing
  1012. * the transaction (through extent_io_tree_release()).
  1013. */
  1014. if (ret == -ENOMEM) {
  1015. ret = 0;
  1016. wait_writeback = true;
  1017. }
  1018. if (!ret)
  1019. ret = filemap_fdatawrite_range(mapping, start, end);
  1020. if (!ret && wait_writeback)
  1021. ret = filemap_fdatawait_range(mapping, start, end);
  1022. free_extent_state(cached_state);
  1023. if (ret)
  1024. break;
  1025. cached_state = NULL;
  1026. cond_resched();
  1027. start = end + 1;
  1028. }
  1029. return ret;
  1030. }
  1031. /*
  1032. * when btree blocks are allocated, they have some corresponding bits set for
  1033. * them in one of two extent_io trees. This is used to make sure all of
  1034. * those extents are on disk for transaction or log commit. We wait
  1035. * on all the pages and clear them from the dirty pages state tree
  1036. */
  1037. static int __btrfs_wait_marked_extents(struct btrfs_fs_info *fs_info,
  1038. struct extent_io_tree *dirty_pages)
  1039. {
  1040. struct address_space *mapping = fs_info->btree_inode->i_mapping;
  1041. struct extent_state *cached_state = NULL;
  1042. u64 start = 0;
  1043. u64 end;
  1044. int ret = 0;
  1045. while (find_first_extent_bit(dirty_pages, start, &start, &end,
  1046. EXTENT_NEED_WAIT, &cached_state)) {
  1047. /*
  1048. * Ignore -ENOMEM errors returned by clear_extent_bit().
  1049. * When committing the transaction, we'll remove any entries
  1050. * left in the io tree. For a log commit, we don't remove them
  1051. * after committing the log because the tree can be accessed
  1052. * concurrently - we do it only at transaction commit time when
  1053. * it's safe to do it (through extent_io_tree_release()).
  1054. */
  1055. ret = clear_extent_bit(dirty_pages, start, end,
  1056. EXTENT_NEED_WAIT, &cached_state);
  1057. if (ret == -ENOMEM)
  1058. ret = 0;
  1059. if (!ret)
  1060. ret = filemap_fdatawait_range(mapping, start, end);
  1061. free_extent_state(cached_state);
  1062. if (ret)
  1063. break;
  1064. cached_state = NULL;
  1065. cond_resched();
  1066. start = end + 1;
  1067. }
  1068. return ret;
  1069. }
  1070. static int btrfs_wait_extents(struct btrfs_fs_info *fs_info,
  1071. struct extent_io_tree *dirty_pages)
  1072. {
  1073. bool errors = false;
  1074. int err;
  1075. err = __btrfs_wait_marked_extents(fs_info, dirty_pages);
  1076. if (test_and_clear_bit(BTRFS_FS_BTREE_ERR, &fs_info->flags))
  1077. errors = true;
  1078. if (errors && !err)
  1079. err = -EIO;
  1080. return err;
  1081. }
  1082. int btrfs_wait_tree_log_extents(struct btrfs_root *log_root, int mark)
  1083. {
  1084. struct btrfs_fs_info *fs_info = log_root->fs_info;
  1085. struct extent_io_tree *dirty_pages = &log_root->dirty_log_pages;
  1086. bool errors = false;
  1087. int err;
  1088. ASSERT(btrfs_root_id(log_root) == BTRFS_TREE_LOG_OBJECTID);
  1089. err = __btrfs_wait_marked_extents(fs_info, dirty_pages);
  1090. if ((mark & EXTENT_DIRTY) &&
  1091. test_and_clear_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags))
  1092. errors = true;
  1093. if ((mark & EXTENT_NEW) &&
  1094. test_and_clear_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags))
  1095. errors = true;
  1096. if (errors && !err)
  1097. err = -EIO;
  1098. return err;
  1099. }
  1100. /*
  1101. * When btree blocks are allocated the corresponding extents are marked dirty.
  1102. * This function ensures such extents are persisted on disk for transaction or
  1103. * log commit.
  1104. *
  1105. * @trans: transaction whose dirty pages we'd like to write
  1106. */
  1107. static int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans)
  1108. {
  1109. int ret;
  1110. int ret2;
  1111. struct extent_io_tree *dirty_pages = &trans->transaction->dirty_pages;
  1112. struct btrfs_fs_info *fs_info = trans->fs_info;
  1113. struct blk_plug plug;
  1114. blk_start_plug(&plug);
  1115. ret = btrfs_write_marked_extents(fs_info, dirty_pages, EXTENT_DIRTY);
  1116. blk_finish_plug(&plug);
  1117. ret2 = btrfs_wait_extents(fs_info, dirty_pages);
  1118. extent_io_tree_release(&trans->transaction->dirty_pages);
  1119. if (ret)
  1120. return ret;
  1121. else if (ret2)
  1122. return ret2;
  1123. else
  1124. return 0;
  1125. }
  1126. /*
  1127. * this is used to update the root pointer in the tree of tree roots.
  1128. *
  1129. * But, in the case of the extent allocation tree, updating the root
  1130. * pointer may allocate blocks which may change the root of the extent
  1131. * allocation tree.
  1132. *
  1133. * So, this loops and repeats and makes sure the cowonly root didn't
  1134. * change while the root pointer was being updated in the metadata.
  1135. */
  1136. static int update_cowonly_root(struct btrfs_trans_handle *trans,
  1137. struct btrfs_root *root)
  1138. {
  1139. int ret;
  1140. u64 old_root_bytenr;
  1141. u64 old_root_used;
  1142. struct btrfs_fs_info *fs_info = root->fs_info;
  1143. struct btrfs_root *tree_root = fs_info->tree_root;
  1144. old_root_used = btrfs_root_used(&root->root_item);
  1145. while (1) {
  1146. old_root_bytenr = btrfs_root_bytenr(&root->root_item);
  1147. if (old_root_bytenr == root->node->start &&
  1148. old_root_used == btrfs_root_used(&root->root_item))
  1149. break;
  1150. btrfs_set_root_node(&root->root_item, root->node);
  1151. ret = btrfs_update_root(trans, tree_root,
  1152. &root->root_key,
  1153. &root->root_item);
  1154. if (ret)
  1155. return ret;
  1156. old_root_used = btrfs_root_used(&root->root_item);
  1157. }
  1158. return 0;
  1159. }
  1160. /*
  1161. * update all the cowonly tree roots on disk
  1162. *
  1163. * The error handling in this function may not be obvious. Any of the
  1164. * failures will cause the file system to go offline. We still need
  1165. * to clean up the delayed refs.
  1166. */
  1167. static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans)
  1168. {
  1169. struct btrfs_fs_info *fs_info = trans->fs_info;
  1170. struct list_head *dirty_bgs = &trans->transaction->dirty_bgs;
  1171. struct list_head *io_bgs = &trans->transaction->io_bgs;
  1172. struct list_head *next;
  1173. struct extent_buffer *eb;
  1174. int ret;
  1175. /*
  1176. * At this point no one can be using this transaction to modify any tree
  1177. * and no one can start another transaction to modify any tree either.
  1178. */
  1179. ASSERT(trans->transaction->state == TRANS_STATE_COMMIT_DOING);
  1180. eb = btrfs_lock_root_node(fs_info->tree_root);
  1181. ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL,
  1182. 0, &eb, BTRFS_NESTING_COW);
  1183. btrfs_tree_unlock(eb);
  1184. free_extent_buffer(eb);
  1185. if (ret)
  1186. return ret;
  1187. ret = btrfs_run_dev_stats(trans);
  1188. if (ret)
  1189. return ret;
  1190. ret = btrfs_run_dev_replace(trans);
  1191. if (ret)
  1192. return ret;
  1193. ret = btrfs_run_qgroups(trans);
  1194. if (ret)
  1195. return ret;
  1196. ret = btrfs_setup_space_cache(trans);
  1197. if (ret)
  1198. return ret;
  1199. again:
  1200. while (!list_empty(&fs_info->dirty_cowonly_roots)) {
  1201. struct btrfs_root *root;
  1202. next = fs_info->dirty_cowonly_roots.next;
  1203. list_del_init(next);
  1204. root = list_entry(next, struct btrfs_root, dirty_list);
  1205. clear_bit(BTRFS_ROOT_DIRTY, &root->state);
  1206. list_add_tail(&root->dirty_list,
  1207. &trans->transaction->switch_commits);
  1208. ret = update_cowonly_root(trans, root);
  1209. if (ret)
  1210. return ret;
  1211. }
  1212. /* Now flush any delayed refs generated by updating all of the roots */
  1213. ret = btrfs_run_delayed_refs(trans, U64_MAX);
  1214. if (ret)
  1215. return ret;
  1216. while (!list_empty(dirty_bgs) || !list_empty(io_bgs)) {
  1217. ret = btrfs_write_dirty_block_groups(trans);
  1218. if (ret)
  1219. return ret;
  1220. /*
  1221. * We're writing the dirty block groups, which could generate
  1222. * delayed refs, which could generate more dirty block groups,
  1223. * so we want to keep this flushing in this loop to make sure
  1224. * everything gets run.
  1225. */
  1226. ret = btrfs_run_delayed_refs(trans, U64_MAX);
  1227. if (ret)
  1228. return ret;
  1229. }
  1230. if (!list_empty(&fs_info->dirty_cowonly_roots))
  1231. goto again;
  1232. /* Update dev-replace pointer once everything is committed */
  1233. fs_info->dev_replace.committed_cursor_left =
  1234. fs_info->dev_replace.cursor_left_last_write_of_item;
  1235. return 0;
  1236. }
  1237. /*
  1238. * If we had a pending drop we need to see if there are any others left in our
  1239. * dead roots list, and if not clear our bit and wake any waiters.
  1240. */
  1241. void btrfs_maybe_wake_unfinished_drop(struct btrfs_fs_info *fs_info)
  1242. {
  1243. /*
  1244. * We put the drop in progress roots at the front of the list, so if the
  1245. * first entry doesn't have UNFINISHED_DROP set we can wake everybody
  1246. * up.
  1247. */
  1248. spin_lock(&fs_info->trans_lock);
  1249. if (!list_empty(&fs_info->dead_roots)) {
  1250. struct btrfs_root *root = list_first_entry(&fs_info->dead_roots,
  1251. struct btrfs_root,
  1252. root_list);
  1253. if (test_bit(BTRFS_ROOT_UNFINISHED_DROP, &root->state)) {
  1254. spin_unlock(&fs_info->trans_lock);
  1255. return;
  1256. }
  1257. }
  1258. spin_unlock(&fs_info->trans_lock);
  1259. btrfs_wake_unfinished_drop(fs_info);
  1260. }
  1261. /*
  1262. * dead roots are old snapshots that need to be deleted. This allocates
  1263. * a dirty root struct and adds it into the list of dead roots that need to
  1264. * be deleted
  1265. */
  1266. void btrfs_add_dead_root(struct btrfs_root *root)
  1267. {
  1268. struct btrfs_fs_info *fs_info = root->fs_info;
  1269. spin_lock(&fs_info->trans_lock);
  1270. if (list_empty(&root->root_list)) {
  1271. btrfs_grab_root(root);
  1272. /* We want to process the partially complete drops first. */
  1273. if (test_bit(BTRFS_ROOT_UNFINISHED_DROP, &root->state))
  1274. list_add(&root->root_list, &fs_info->dead_roots);
  1275. else
  1276. list_add_tail(&root->root_list, &fs_info->dead_roots);
  1277. }
  1278. spin_unlock(&fs_info->trans_lock);
  1279. }
  1280. /*
  1281. * Update each subvolume root and its relocation root, if it exists, in the tree
  1282. * of tree roots. Also free log roots if they exist.
  1283. */
  1284. static noinline int commit_fs_roots(struct btrfs_trans_handle *trans)
  1285. {
  1286. struct btrfs_fs_info *fs_info = trans->fs_info;
  1287. struct btrfs_root *gang[8];
  1288. int i;
  1289. int ret;
  1290. /*
  1291. * At this point no one can be using this transaction to modify any tree
  1292. * and no one can start another transaction to modify any tree either.
  1293. */
  1294. ASSERT(trans->transaction->state == TRANS_STATE_COMMIT_DOING);
  1295. spin_lock(&fs_info->fs_roots_radix_lock);
  1296. while (1) {
  1297. ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
  1298. (void **)gang, 0,
  1299. ARRAY_SIZE(gang),
  1300. BTRFS_ROOT_TRANS_TAG);
  1301. if (ret == 0)
  1302. break;
  1303. for (i = 0; i < ret; i++) {
  1304. struct btrfs_root *root = gang[i];
  1305. int ret2;
  1306. /*
  1307. * At this point we can neither have tasks logging inodes
  1308. * from a root nor trying to commit a log tree.
  1309. */
  1310. ASSERT(atomic_read(&root->log_writers) == 0);
  1311. ASSERT(atomic_read(&root->log_commit[0]) == 0);
  1312. ASSERT(atomic_read(&root->log_commit[1]) == 0);
  1313. radix_tree_tag_clear(&fs_info->fs_roots_radix,
  1314. (unsigned long)btrfs_root_id(root),
  1315. BTRFS_ROOT_TRANS_TAG);
  1316. btrfs_qgroup_free_meta_all_pertrans(root);
  1317. spin_unlock(&fs_info->fs_roots_radix_lock);
  1318. btrfs_free_log(trans, root);
  1319. ret2 = btrfs_update_reloc_root(trans, root);
  1320. if (ret2)
  1321. return ret2;
  1322. /* see comments in should_cow_block() */
  1323. clear_bit(BTRFS_ROOT_FORCE_COW, &root->state);
  1324. smp_mb__after_atomic();
  1325. if (root->commit_root != root->node) {
  1326. list_add_tail(&root->dirty_list,
  1327. &trans->transaction->switch_commits);
  1328. btrfs_set_root_node(&root->root_item,
  1329. root->node);
  1330. }
  1331. ret2 = btrfs_update_root(trans, fs_info->tree_root,
  1332. &root->root_key,
  1333. &root->root_item);
  1334. if (ret2)
  1335. return ret2;
  1336. spin_lock(&fs_info->fs_roots_radix_lock);
  1337. }
  1338. }
  1339. spin_unlock(&fs_info->fs_roots_radix_lock);
  1340. return 0;
  1341. }
  1342. /*
  1343. * Do all special snapshot related qgroup dirty hack.
  1344. *
  1345. * Will do all needed qgroup inherit and dirty hack like switch commit
  1346. * roots inside one transaction and write all btree into disk, to make
  1347. * qgroup works.
  1348. */
  1349. static int qgroup_account_snapshot(struct btrfs_trans_handle *trans,
  1350. struct btrfs_root *src,
  1351. struct btrfs_root *parent,
  1352. struct btrfs_qgroup_inherit *inherit,
  1353. u64 dst_objectid)
  1354. {
  1355. struct btrfs_fs_info *fs_info = src->fs_info;
  1356. int ret;
  1357. /*
  1358. * Save some performance in the case that qgroups are not enabled. If
  1359. * this check races with the ioctl, rescan will kick in anyway.
  1360. */
  1361. if (!btrfs_qgroup_full_accounting(fs_info))
  1362. return 0;
  1363. /*
  1364. * Ensure dirty @src will be committed. Or, after coming
  1365. * commit_fs_roots() and switch_commit_roots(), any dirty but not
  1366. * recorded root will never be updated again, causing an outdated root
  1367. * item.
  1368. */
  1369. ret = record_root_in_trans(trans, src, 1);
  1370. if (ret)
  1371. return ret;
  1372. /*
  1373. * btrfs_qgroup_inherit relies on a consistent view of the usage for the
  1374. * src root, so we must run the delayed refs here.
  1375. *
  1376. * However this isn't particularly fool proof, because there's no
  1377. * synchronization keeping us from changing the tree after this point
  1378. * before we do the qgroup_inherit, or even from making changes while
  1379. * we're doing the qgroup_inherit. But that's a problem for the future,
  1380. * for now flush the delayed refs to narrow the race window where the
  1381. * qgroup counters could end up wrong.
  1382. */
  1383. ret = btrfs_run_delayed_refs(trans, U64_MAX);
  1384. if (ret) {
  1385. btrfs_abort_transaction(trans, ret);
  1386. return ret;
  1387. }
  1388. ret = commit_fs_roots(trans);
  1389. if (ret)
  1390. goto out;
  1391. ret = btrfs_qgroup_account_extents(trans);
  1392. if (ret < 0)
  1393. goto out;
  1394. /* Now qgroup are all updated, we can inherit it to new qgroups */
  1395. ret = btrfs_qgroup_inherit(trans, btrfs_root_id(src), dst_objectid,
  1396. btrfs_root_id(parent), inherit);
  1397. if (ret < 0)
  1398. goto out;
  1399. /*
  1400. * Now we do a simplified commit transaction, which will:
  1401. * 1) commit all subvolume and extent tree
  1402. * To ensure all subvolume and extent tree have a valid
  1403. * commit_root to accounting later insert_dir_item()
  1404. * 2) write all btree blocks onto disk
  1405. * This is to make sure later btree modification will be cowed
  1406. * Or commit_root can be populated and cause wrong qgroup numbers
  1407. * In this simplified commit, we don't really care about other trees
  1408. * like chunk and root tree, as they won't affect qgroup.
  1409. * And we don't write super to avoid half committed status.
  1410. */
  1411. ret = commit_cowonly_roots(trans);
  1412. if (ret)
  1413. goto out;
  1414. switch_commit_roots(trans);
  1415. ret = btrfs_write_and_wait_transaction(trans);
  1416. if (ret)
  1417. btrfs_handle_fs_error(fs_info, ret,
  1418. "Error while writing out transaction for qgroup");
  1419. out:
  1420. /*
  1421. * Force parent root to be updated, as we recorded it before so its
  1422. * last_trans == cur_transid.
  1423. * Or it won't be committed again onto disk after later
  1424. * insert_dir_item()
  1425. */
  1426. if (!ret)
  1427. ret = record_root_in_trans(trans, parent, 1);
  1428. return ret;
  1429. }
  1430. /*
  1431. * new snapshots need to be created at a very specific time in the
  1432. * transaction commit. This does the actual creation.
  1433. *
  1434. * Note:
  1435. * If the error which may affect the commitment of the current transaction
  1436. * happens, we should return the error number. If the error which just affect
  1437. * the creation of the pending snapshots, just return 0.
  1438. */
  1439. static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
  1440. struct btrfs_pending_snapshot *pending)
  1441. {
  1442. struct btrfs_fs_info *fs_info = trans->fs_info;
  1443. struct btrfs_key key;
  1444. struct btrfs_root_item *new_root_item;
  1445. struct btrfs_root *tree_root = fs_info->tree_root;
  1446. struct btrfs_root *root = pending->root;
  1447. struct btrfs_root *parent_root;
  1448. struct btrfs_block_rsv *rsv;
  1449. struct inode *parent_inode = &pending->dir->vfs_inode;
  1450. struct btrfs_path *path;
  1451. struct btrfs_dir_item *dir_item;
  1452. struct extent_buffer *tmp;
  1453. struct extent_buffer *old;
  1454. struct timespec64 cur_time;
  1455. int ret = 0;
  1456. u64 to_reserve = 0;
  1457. u64 index = 0;
  1458. u64 objectid;
  1459. u64 root_flags;
  1460. unsigned int nofs_flags;
  1461. struct fscrypt_name fname;
  1462. ASSERT(pending->path);
  1463. path = pending->path;
  1464. ASSERT(pending->root_item);
  1465. new_root_item = pending->root_item;
  1466. /*
  1467. * We're inside a transaction and must make sure that any potential
  1468. * allocations with GFP_KERNEL in fscrypt won't recurse back to
  1469. * filesystem.
  1470. */
  1471. nofs_flags = memalloc_nofs_save();
  1472. pending->error = fscrypt_setup_filename(parent_inode,
  1473. &pending->dentry->d_name, 0,
  1474. &fname);
  1475. memalloc_nofs_restore(nofs_flags);
  1476. if (pending->error)
  1477. goto free_pending;
  1478. pending->error = btrfs_get_free_objectid(tree_root, &objectid);
  1479. if (pending->error)
  1480. goto free_fname;
  1481. /*
  1482. * Make qgroup to skip current new snapshot's qgroupid, as it is
  1483. * accounted by later btrfs_qgroup_inherit().
  1484. */
  1485. btrfs_set_skip_qgroup(trans, objectid);
  1486. btrfs_reloc_pre_snapshot(pending, &to_reserve);
  1487. if (to_reserve > 0) {
  1488. pending->error = btrfs_block_rsv_add(fs_info,
  1489. &pending->block_rsv,
  1490. to_reserve,
  1491. BTRFS_RESERVE_NO_FLUSH);
  1492. if (pending->error)
  1493. goto clear_skip_qgroup;
  1494. }
  1495. key.objectid = objectid;
  1496. key.offset = (u64)-1;
  1497. key.type = BTRFS_ROOT_ITEM_KEY;
  1498. rsv = trans->block_rsv;
  1499. trans->block_rsv = &pending->block_rsv;
  1500. trans->bytes_reserved = trans->block_rsv->reserved;
  1501. trace_btrfs_space_reservation(fs_info, "transaction",
  1502. trans->transid,
  1503. trans->bytes_reserved, 1);
  1504. parent_root = BTRFS_I(parent_inode)->root;
  1505. ret = record_root_in_trans(trans, parent_root, 0);
  1506. if (ret)
  1507. goto fail;
  1508. cur_time = current_time(parent_inode);
  1509. /*
  1510. * insert the directory item
  1511. */
  1512. ret = btrfs_set_inode_index(BTRFS_I(parent_inode), &index);
  1513. if (ret) {
  1514. btrfs_abort_transaction(trans, ret);
  1515. goto fail;
  1516. }
  1517. /* check if there is a file/dir which has the same name. */
  1518. dir_item = btrfs_lookup_dir_item(NULL, parent_root, path,
  1519. btrfs_ino(BTRFS_I(parent_inode)),
  1520. &fname.disk_name, 0);
  1521. if (dir_item != NULL && !IS_ERR(dir_item)) {
  1522. pending->error = -EEXIST;
  1523. goto dir_item_existed;
  1524. } else if (IS_ERR(dir_item)) {
  1525. ret = PTR_ERR(dir_item);
  1526. btrfs_abort_transaction(trans, ret);
  1527. goto fail;
  1528. }
  1529. btrfs_release_path(path);
  1530. ret = btrfs_create_qgroup(trans, objectid);
  1531. if (ret && ret != -EEXIST) {
  1532. if (ret != -ENOTCONN || btrfs_qgroup_enabled(fs_info)) {
  1533. btrfs_abort_transaction(trans, ret);
  1534. goto fail;
  1535. }
  1536. }
  1537. /*
  1538. * pull in the delayed directory update
  1539. * and the delayed inode item
  1540. * otherwise we corrupt the FS during
  1541. * snapshot
  1542. */
  1543. ret = btrfs_run_delayed_items(trans);
  1544. if (ret) { /* Transaction aborted */
  1545. btrfs_abort_transaction(trans, ret);
  1546. goto fail;
  1547. }
  1548. ret = record_root_in_trans(trans, root, 0);
  1549. if (ret) {
  1550. btrfs_abort_transaction(trans, ret);
  1551. goto fail;
  1552. }
  1553. btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
  1554. memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
  1555. btrfs_check_and_init_root_item(new_root_item);
  1556. root_flags = btrfs_root_flags(new_root_item);
  1557. if (pending->readonly)
  1558. root_flags |= BTRFS_ROOT_SUBVOL_RDONLY;
  1559. else
  1560. root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY;
  1561. btrfs_set_root_flags(new_root_item, root_flags);
  1562. btrfs_set_root_generation_v2(new_root_item,
  1563. trans->transid);
  1564. generate_random_guid(new_root_item->uuid);
  1565. memcpy(new_root_item->parent_uuid, root->root_item.uuid,
  1566. BTRFS_UUID_SIZE);
  1567. if (!(root_flags & BTRFS_ROOT_SUBVOL_RDONLY)) {
  1568. memset(new_root_item->received_uuid, 0,
  1569. sizeof(new_root_item->received_uuid));
  1570. memset(&new_root_item->stime, 0, sizeof(new_root_item->stime));
  1571. memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime));
  1572. btrfs_set_root_stransid(new_root_item, 0);
  1573. btrfs_set_root_rtransid(new_root_item, 0);
  1574. }
  1575. btrfs_set_stack_timespec_sec(&new_root_item->otime, cur_time.tv_sec);
  1576. btrfs_set_stack_timespec_nsec(&new_root_item->otime, cur_time.tv_nsec);
  1577. btrfs_set_root_otransid(new_root_item, trans->transid);
  1578. old = btrfs_lock_root_node(root);
  1579. ret = btrfs_cow_block(trans, root, old, NULL, 0, &old,
  1580. BTRFS_NESTING_COW);
  1581. if (ret) {
  1582. btrfs_tree_unlock(old);
  1583. free_extent_buffer(old);
  1584. btrfs_abort_transaction(trans, ret);
  1585. goto fail;
  1586. }
  1587. ret = btrfs_copy_root(trans, root, old, &tmp, objectid);
  1588. /* clean up in any case */
  1589. btrfs_tree_unlock(old);
  1590. free_extent_buffer(old);
  1591. if (ret) {
  1592. btrfs_abort_transaction(trans, ret);
  1593. goto fail;
  1594. }
  1595. /* see comments in should_cow_block() */
  1596. set_bit(BTRFS_ROOT_FORCE_COW, &root->state);
  1597. smp_wmb();
  1598. btrfs_set_root_node(new_root_item, tmp);
  1599. /* record when the snapshot was created in key.offset */
  1600. key.offset = trans->transid;
  1601. ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
  1602. btrfs_tree_unlock(tmp);
  1603. free_extent_buffer(tmp);
  1604. if (ret) {
  1605. btrfs_abort_transaction(trans, ret);
  1606. goto fail;
  1607. }
  1608. /*
  1609. * insert root back/forward references
  1610. */
  1611. ret = btrfs_add_root_ref(trans, objectid,
  1612. btrfs_root_id(parent_root),
  1613. btrfs_ino(BTRFS_I(parent_inode)), index,
  1614. &fname.disk_name);
  1615. if (ret) {
  1616. btrfs_abort_transaction(trans, ret);
  1617. goto fail;
  1618. }
  1619. key.offset = (u64)-1;
  1620. pending->snap = btrfs_get_new_fs_root(fs_info, objectid, &pending->anon_dev);
  1621. if (IS_ERR(pending->snap)) {
  1622. ret = PTR_ERR(pending->snap);
  1623. pending->snap = NULL;
  1624. btrfs_abort_transaction(trans, ret);
  1625. goto fail;
  1626. }
  1627. ret = btrfs_reloc_post_snapshot(trans, pending);
  1628. if (ret) {
  1629. btrfs_abort_transaction(trans, ret);
  1630. goto fail;
  1631. }
  1632. /*
  1633. * Do special qgroup accounting for snapshot, as we do some qgroup
  1634. * snapshot hack to do fast snapshot.
  1635. * To co-operate with that hack, we do hack again.
  1636. * Or snapshot will be greatly slowed down by a subtree qgroup rescan
  1637. */
  1638. if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_FULL)
  1639. ret = qgroup_account_snapshot(trans, root, parent_root,
  1640. pending->inherit, objectid);
  1641. else if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_SIMPLE)
  1642. ret = btrfs_qgroup_inherit(trans, btrfs_root_id(root), objectid,
  1643. btrfs_root_id(parent_root), pending->inherit);
  1644. if (ret < 0)
  1645. goto fail;
  1646. ret = btrfs_insert_dir_item(trans, &fname.disk_name,
  1647. BTRFS_I(parent_inode), &key, BTRFS_FT_DIR,
  1648. index);
  1649. if (ret) {
  1650. btrfs_abort_transaction(trans, ret);
  1651. goto fail;
  1652. }
  1653. btrfs_i_size_write(BTRFS_I(parent_inode), parent_inode->i_size +
  1654. fname.disk_name.len * 2);
  1655. inode_set_mtime_to_ts(parent_inode,
  1656. inode_set_ctime_current(parent_inode));
  1657. ret = btrfs_update_inode_fallback(trans, BTRFS_I(parent_inode));
  1658. if (ret) {
  1659. btrfs_abort_transaction(trans, ret);
  1660. goto fail;
  1661. }
  1662. ret = btrfs_uuid_tree_add(trans, new_root_item->uuid,
  1663. BTRFS_UUID_KEY_SUBVOL,
  1664. objectid);
  1665. if (ret) {
  1666. btrfs_abort_transaction(trans, ret);
  1667. goto fail;
  1668. }
  1669. if (!btrfs_is_empty_uuid(new_root_item->received_uuid)) {
  1670. ret = btrfs_uuid_tree_add(trans, new_root_item->received_uuid,
  1671. BTRFS_UUID_KEY_RECEIVED_SUBVOL,
  1672. objectid);
  1673. if (ret && ret != -EEXIST) {
  1674. btrfs_abort_transaction(trans, ret);
  1675. goto fail;
  1676. }
  1677. }
  1678. fail:
  1679. pending->error = ret;
  1680. dir_item_existed:
  1681. trans->block_rsv = rsv;
  1682. trans->bytes_reserved = 0;
  1683. clear_skip_qgroup:
  1684. btrfs_clear_skip_qgroup(trans);
  1685. free_fname:
  1686. fscrypt_free_filename(&fname);
  1687. free_pending:
  1688. kfree(new_root_item);
  1689. pending->root_item = NULL;
  1690. btrfs_free_path(path);
  1691. pending->path = NULL;
  1692. return ret;
  1693. }
  1694. /*
  1695. * create all the snapshots we've scheduled for creation
  1696. */
  1697. static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans)
  1698. {
  1699. struct btrfs_pending_snapshot *pending, *next;
  1700. struct list_head *head = &trans->transaction->pending_snapshots;
  1701. int ret = 0;
  1702. list_for_each_entry_safe(pending, next, head, list) {
  1703. list_del(&pending->list);
  1704. ret = create_pending_snapshot(trans, pending);
  1705. if (ret)
  1706. break;
  1707. }
  1708. return ret;
  1709. }
  1710. static void update_super_roots(struct btrfs_fs_info *fs_info)
  1711. {
  1712. struct btrfs_root_item *root_item;
  1713. struct btrfs_super_block *super;
  1714. super = fs_info->super_copy;
  1715. root_item = &fs_info->chunk_root->root_item;
  1716. super->chunk_root = root_item->bytenr;
  1717. super->chunk_root_generation = root_item->generation;
  1718. super->chunk_root_level = root_item->level;
  1719. root_item = &fs_info->tree_root->root_item;
  1720. super->root = root_item->bytenr;
  1721. super->generation = root_item->generation;
  1722. super->root_level = root_item->level;
  1723. if (btrfs_test_opt(fs_info, SPACE_CACHE))
  1724. super->cache_generation = root_item->generation;
  1725. else if (test_bit(BTRFS_FS_CLEANUP_SPACE_CACHE_V1, &fs_info->flags))
  1726. super->cache_generation = 0;
  1727. if (test_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags))
  1728. super->uuid_tree_generation = root_item->generation;
  1729. }
  1730. int btrfs_transaction_blocked(struct btrfs_fs_info *info)
  1731. {
  1732. struct btrfs_transaction *trans;
  1733. int ret = 0;
  1734. spin_lock(&info->trans_lock);
  1735. trans = info->running_transaction;
  1736. if (trans)
  1737. ret = is_transaction_blocked(trans);
  1738. spin_unlock(&info->trans_lock);
  1739. return ret;
  1740. }
  1741. void btrfs_commit_transaction_async(struct btrfs_trans_handle *trans)
  1742. {
  1743. struct btrfs_fs_info *fs_info = trans->fs_info;
  1744. struct btrfs_transaction *cur_trans;
  1745. /* Kick the transaction kthread. */
  1746. set_bit(BTRFS_FS_COMMIT_TRANS, &fs_info->flags);
  1747. wake_up_process(fs_info->transaction_kthread);
  1748. /* take transaction reference */
  1749. cur_trans = trans->transaction;
  1750. refcount_inc(&cur_trans->use_count);
  1751. btrfs_end_transaction(trans);
  1752. /*
  1753. * Wait for the current transaction commit to start and block
  1754. * subsequent transaction joins
  1755. */
  1756. btrfs_might_wait_for_state(fs_info, BTRFS_LOCKDEP_TRANS_COMMIT_PREP);
  1757. wait_event(fs_info->transaction_blocked_wait,
  1758. cur_trans->state >= TRANS_STATE_COMMIT_START ||
  1759. TRANS_ABORTED(cur_trans));
  1760. btrfs_put_transaction(cur_trans);
  1761. }
  1762. /*
  1763. * If there is a running transaction commit it or if it's already committing,
  1764. * wait for its commit to complete. Does not start and commit a new transaction
  1765. * if there isn't any running.
  1766. */
  1767. int btrfs_commit_current_transaction(struct btrfs_root *root)
  1768. {
  1769. struct btrfs_trans_handle *trans;
  1770. trans = btrfs_attach_transaction_barrier(root);
  1771. if (IS_ERR(trans)) {
  1772. int ret = PTR_ERR(trans);
  1773. return (ret == -ENOENT) ? 0 : ret;
  1774. }
  1775. return btrfs_commit_transaction(trans);
  1776. }
  1777. static void cleanup_transaction(struct btrfs_trans_handle *trans, int err)
  1778. {
  1779. struct btrfs_fs_info *fs_info = trans->fs_info;
  1780. struct btrfs_transaction *cur_trans = trans->transaction;
  1781. WARN_ON(refcount_read(&trans->use_count) > 1);
  1782. btrfs_abort_transaction(trans, err);
  1783. spin_lock(&fs_info->trans_lock);
  1784. /*
  1785. * If the transaction is removed from the list, it means this
  1786. * transaction has been committed successfully, so it is impossible
  1787. * to call the cleanup function.
  1788. */
  1789. BUG_ON(list_empty(&cur_trans->list));
  1790. if (cur_trans == fs_info->running_transaction) {
  1791. cur_trans->state = TRANS_STATE_COMMIT_DOING;
  1792. spin_unlock(&fs_info->trans_lock);
  1793. /*
  1794. * The thread has already released the lockdep map as reader
  1795. * already in btrfs_commit_transaction().
  1796. */
  1797. btrfs_might_wait_for_event(fs_info, btrfs_trans_num_writers);
  1798. wait_event(cur_trans->writer_wait,
  1799. atomic_read(&cur_trans->num_writers) == 1);
  1800. spin_lock(&fs_info->trans_lock);
  1801. }
  1802. /*
  1803. * Now that we know no one else is still using the transaction we can
  1804. * remove the transaction from the list of transactions. This avoids
  1805. * the transaction kthread from cleaning up the transaction while some
  1806. * other task is still using it, which could result in a use-after-free
  1807. * on things like log trees, as it forces the transaction kthread to
  1808. * wait for this transaction to be cleaned up by us.
  1809. */
  1810. list_del_init(&cur_trans->list);
  1811. spin_unlock(&fs_info->trans_lock);
  1812. btrfs_cleanup_one_transaction(trans->transaction, fs_info);
  1813. spin_lock(&fs_info->trans_lock);
  1814. if (cur_trans == fs_info->running_transaction)
  1815. fs_info->running_transaction = NULL;
  1816. spin_unlock(&fs_info->trans_lock);
  1817. if (trans->type & __TRANS_FREEZABLE)
  1818. sb_end_intwrite(fs_info->sb);
  1819. btrfs_put_transaction(cur_trans);
  1820. btrfs_put_transaction(cur_trans);
  1821. trace_btrfs_transaction_commit(fs_info);
  1822. if (current->journal_info == trans)
  1823. current->journal_info = NULL;
  1824. /*
  1825. * If relocation is running, we can't cancel scrub because that will
  1826. * result in a deadlock. Before relocating a block group, relocation
  1827. * pauses scrub, then starts and commits a transaction before unpausing
  1828. * scrub. If the transaction commit is being done by the relocation
  1829. * task or triggered by another task and the relocation task is waiting
  1830. * for the commit, and we end up here due to an error in the commit
  1831. * path, then calling btrfs_scrub_cancel() will deadlock, as we are
  1832. * asking for scrub to stop while having it asked to be paused higher
  1833. * above in relocation code.
  1834. */
  1835. if (!test_bit(BTRFS_FS_RELOC_RUNNING, &fs_info->flags))
  1836. btrfs_scrub_cancel(fs_info);
  1837. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  1838. }
  1839. /*
  1840. * Release reserved delayed ref space of all pending block groups of the
  1841. * transaction and remove them from the list
  1842. */
  1843. static void btrfs_cleanup_pending_block_groups(struct btrfs_trans_handle *trans)
  1844. {
  1845. struct btrfs_fs_info *fs_info = trans->fs_info;
  1846. struct btrfs_block_group *block_group, *tmp;
  1847. list_for_each_entry_safe(block_group, tmp, &trans->new_bgs, bg_list) {
  1848. btrfs_dec_delayed_refs_rsv_bg_inserts(fs_info);
  1849. /*
  1850. * Not strictly necessary to lock, as no other task will be using a
  1851. * block_group on the new_bgs list during a transaction abort.
  1852. */
  1853. spin_lock(&fs_info->unused_bgs_lock);
  1854. list_del_init(&block_group->bg_list);
  1855. btrfs_put_block_group(block_group);
  1856. spin_unlock(&fs_info->unused_bgs_lock);
  1857. }
  1858. }
  1859. static inline int btrfs_start_delalloc_flush(struct btrfs_fs_info *fs_info)
  1860. {
  1861. /*
  1862. * We use try_to_writeback_inodes_sb() here because if we used
  1863. * btrfs_start_delalloc_roots we would deadlock with fs freeze.
  1864. * Currently are holding the fs freeze lock, if we do an async flush
  1865. * we'll do btrfs_join_transaction() and deadlock because we need to
  1866. * wait for the fs freeze lock. Using the direct flushing we benefit
  1867. * from already being in a transaction and our join_transaction doesn't
  1868. * have to re-take the fs freeze lock.
  1869. *
  1870. * Note that try_to_writeback_inodes_sb() will only trigger writeback
  1871. * if it can read lock sb->s_umount. It will always be able to lock it,
  1872. * except when the filesystem is being unmounted or being frozen, but in
  1873. * those cases sync_filesystem() is called, which results in calling
  1874. * writeback_inodes_sb() while holding a write lock on sb->s_umount.
  1875. * Note that we don't call writeback_inodes_sb() directly, because it
  1876. * will emit a warning if sb->s_umount is not locked.
  1877. */
  1878. if (btrfs_test_opt(fs_info, FLUSHONCOMMIT))
  1879. try_to_writeback_inodes_sb(fs_info->sb, WB_REASON_SYNC);
  1880. return 0;
  1881. }
  1882. static inline void btrfs_wait_delalloc_flush(struct btrfs_fs_info *fs_info)
  1883. {
  1884. if (btrfs_test_opt(fs_info, FLUSHONCOMMIT))
  1885. btrfs_wait_ordered_roots(fs_info, U64_MAX, NULL);
  1886. }
  1887. /*
  1888. * Add a pending snapshot associated with the given transaction handle to the
  1889. * respective handle. This must be called after the transaction commit started
  1890. * and while holding fs_info->trans_lock.
  1891. * This serves to guarantee a caller of btrfs_commit_transaction() that it can
  1892. * safely free the pending snapshot pointer in case btrfs_commit_transaction()
  1893. * returns an error.
  1894. */
  1895. static void add_pending_snapshot(struct btrfs_trans_handle *trans)
  1896. {
  1897. struct btrfs_transaction *cur_trans = trans->transaction;
  1898. if (!trans->pending_snapshot)
  1899. return;
  1900. lockdep_assert_held(&trans->fs_info->trans_lock);
  1901. ASSERT(cur_trans->state >= TRANS_STATE_COMMIT_PREP);
  1902. list_add(&trans->pending_snapshot->list, &cur_trans->pending_snapshots);
  1903. }
  1904. static void update_commit_stats(struct btrfs_fs_info *fs_info, ktime_t interval)
  1905. {
  1906. fs_info->commit_stats.commit_count++;
  1907. fs_info->commit_stats.last_commit_dur = interval;
  1908. fs_info->commit_stats.max_commit_dur =
  1909. max_t(u64, fs_info->commit_stats.max_commit_dur, interval);
  1910. fs_info->commit_stats.total_commit_dur += interval;
  1911. }
  1912. int btrfs_commit_transaction(struct btrfs_trans_handle *trans)
  1913. {
  1914. struct btrfs_fs_info *fs_info = trans->fs_info;
  1915. struct btrfs_transaction *cur_trans = trans->transaction;
  1916. struct btrfs_transaction *prev_trans = NULL;
  1917. int ret;
  1918. ktime_t start_time;
  1919. ktime_t interval;
  1920. ASSERT(refcount_read(&trans->use_count) == 1);
  1921. btrfs_trans_state_lockdep_acquire(fs_info, BTRFS_LOCKDEP_TRANS_COMMIT_PREP);
  1922. clear_bit(BTRFS_FS_NEED_TRANS_COMMIT, &fs_info->flags);
  1923. /* Stop the commit early if ->aborted is set */
  1924. if (TRANS_ABORTED(cur_trans)) {
  1925. ret = cur_trans->aborted;
  1926. goto lockdep_trans_commit_start_release;
  1927. }
  1928. btrfs_trans_release_metadata(trans);
  1929. trans->block_rsv = NULL;
  1930. /*
  1931. * We only want one transaction commit doing the flushing so we do not
  1932. * waste a bunch of time on lock contention on the extent root node.
  1933. */
  1934. if (!test_and_set_bit(BTRFS_DELAYED_REFS_FLUSHING,
  1935. &cur_trans->delayed_refs.flags)) {
  1936. /*
  1937. * Make a pass through all the delayed refs we have so far.
  1938. * Any running threads may add more while we are here.
  1939. */
  1940. ret = btrfs_run_delayed_refs(trans, 0);
  1941. if (ret)
  1942. goto lockdep_trans_commit_start_release;
  1943. }
  1944. btrfs_create_pending_block_groups(trans);
  1945. if (!test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &cur_trans->flags)) {
  1946. int run_it = 0;
  1947. /* this mutex is also taken before trying to set
  1948. * block groups readonly. We need to make sure
  1949. * that nobody has set a block group readonly
  1950. * after a extents from that block group have been
  1951. * allocated for cache files. btrfs_set_block_group_ro
  1952. * will wait for the transaction to commit if it
  1953. * finds BTRFS_TRANS_DIRTY_BG_RUN set.
  1954. *
  1955. * The BTRFS_TRANS_DIRTY_BG_RUN flag is also used to make sure
  1956. * only one process starts all the block group IO. It wouldn't
  1957. * hurt to have more than one go through, but there's no
  1958. * real advantage to it either.
  1959. */
  1960. mutex_lock(&fs_info->ro_block_group_mutex);
  1961. if (!test_and_set_bit(BTRFS_TRANS_DIRTY_BG_RUN,
  1962. &cur_trans->flags))
  1963. run_it = 1;
  1964. mutex_unlock(&fs_info->ro_block_group_mutex);
  1965. if (run_it) {
  1966. ret = btrfs_start_dirty_block_groups(trans);
  1967. if (ret)
  1968. goto lockdep_trans_commit_start_release;
  1969. }
  1970. }
  1971. spin_lock(&fs_info->trans_lock);
  1972. if (cur_trans->state >= TRANS_STATE_COMMIT_PREP) {
  1973. enum btrfs_trans_state want_state = TRANS_STATE_COMPLETED;
  1974. add_pending_snapshot(trans);
  1975. spin_unlock(&fs_info->trans_lock);
  1976. refcount_inc(&cur_trans->use_count);
  1977. if (trans->in_fsync)
  1978. want_state = TRANS_STATE_SUPER_COMMITTED;
  1979. btrfs_trans_state_lockdep_release(fs_info,
  1980. BTRFS_LOCKDEP_TRANS_COMMIT_PREP);
  1981. ret = btrfs_end_transaction(trans);
  1982. wait_for_commit(cur_trans, want_state);
  1983. if (TRANS_ABORTED(cur_trans))
  1984. ret = cur_trans->aborted;
  1985. btrfs_put_transaction(cur_trans);
  1986. return ret;
  1987. }
  1988. cur_trans->state = TRANS_STATE_COMMIT_PREP;
  1989. wake_up(&fs_info->transaction_blocked_wait);
  1990. btrfs_trans_state_lockdep_release(fs_info, BTRFS_LOCKDEP_TRANS_COMMIT_PREP);
  1991. if (cur_trans->list.prev != &fs_info->trans_list) {
  1992. enum btrfs_trans_state want_state = TRANS_STATE_COMPLETED;
  1993. if (trans->in_fsync)
  1994. want_state = TRANS_STATE_SUPER_COMMITTED;
  1995. prev_trans = list_entry(cur_trans->list.prev,
  1996. struct btrfs_transaction, list);
  1997. if (prev_trans->state < want_state) {
  1998. refcount_inc(&prev_trans->use_count);
  1999. spin_unlock(&fs_info->trans_lock);
  2000. wait_for_commit(prev_trans, want_state);
  2001. ret = READ_ONCE(prev_trans->aborted);
  2002. btrfs_put_transaction(prev_trans);
  2003. if (ret)
  2004. goto lockdep_release;
  2005. spin_lock(&fs_info->trans_lock);
  2006. }
  2007. } else {
  2008. /*
  2009. * The previous transaction was aborted and was already removed
  2010. * from the list of transactions at fs_info->trans_list. So we
  2011. * abort to prevent writing a new superblock that reflects a
  2012. * corrupt state (pointing to trees with unwritten nodes/leafs).
  2013. */
  2014. if (BTRFS_FS_ERROR(fs_info)) {
  2015. spin_unlock(&fs_info->trans_lock);
  2016. ret = -EROFS;
  2017. goto lockdep_release;
  2018. }
  2019. }
  2020. cur_trans->state = TRANS_STATE_COMMIT_START;
  2021. wake_up(&fs_info->transaction_blocked_wait);
  2022. spin_unlock(&fs_info->trans_lock);
  2023. /*
  2024. * Get the time spent on the work done by the commit thread and not
  2025. * the time spent waiting on a previous commit
  2026. */
  2027. start_time = ktime_get_ns();
  2028. extwriter_counter_dec(cur_trans, trans->type);
  2029. ret = btrfs_start_delalloc_flush(fs_info);
  2030. if (ret)
  2031. goto lockdep_release;
  2032. ret = btrfs_run_delayed_items(trans);
  2033. if (ret)
  2034. goto lockdep_release;
  2035. /*
  2036. * The thread has started/joined the transaction thus it holds the
  2037. * lockdep map as a reader. It has to release it before acquiring the
  2038. * lockdep map as a writer.
  2039. */
  2040. btrfs_lockdep_release(fs_info, btrfs_trans_num_extwriters);
  2041. btrfs_might_wait_for_event(fs_info, btrfs_trans_num_extwriters);
  2042. wait_event(cur_trans->writer_wait,
  2043. extwriter_counter_read(cur_trans) == 0);
  2044. /* some pending stuffs might be added after the previous flush. */
  2045. ret = btrfs_run_delayed_items(trans);
  2046. if (ret) {
  2047. btrfs_lockdep_release(fs_info, btrfs_trans_num_writers);
  2048. goto cleanup_transaction;
  2049. }
  2050. btrfs_wait_delalloc_flush(fs_info);
  2051. /*
  2052. * Wait for all ordered extents started by a fast fsync that joined this
  2053. * transaction. Otherwise if this transaction commits before the ordered
  2054. * extents complete we lose logged data after a power failure.
  2055. */
  2056. btrfs_might_wait_for_event(fs_info, btrfs_trans_pending_ordered);
  2057. wait_event(cur_trans->pending_wait,
  2058. atomic_read(&cur_trans->pending_ordered) == 0);
  2059. btrfs_scrub_pause(fs_info);
  2060. /*
  2061. * Ok now we need to make sure to block out any other joins while we
  2062. * commit the transaction. We could have started a join before setting
  2063. * COMMIT_DOING so make sure to wait for num_writers to == 1 again.
  2064. */
  2065. spin_lock(&fs_info->trans_lock);
  2066. add_pending_snapshot(trans);
  2067. cur_trans->state = TRANS_STATE_COMMIT_DOING;
  2068. spin_unlock(&fs_info->trans_lock);
  2069. /*
  2070. * The thread has started/joined the transaction thus it holds the
  2071. * lockdep map as a reader. It has to release it before acquiring the
  2072. * lockdep map as a writer.
  2073. */
  2074. btrfs_lockdep_release(fs_info, btrfs_trans_num_writers);
  2075. btrfs_might_wait_for_event(fs_info, btrfs_trans_num_writers);
  2076. wait_event(cur_trans->writer_wait,
  2077. atomic_read(&cur_trans->num_writers) == 1);
  2078. /*
  2079. * Make lockdep happy by acquiring the state locks after
  2080. * btrfs_trans_num_writers is released. If we acquired the state locks
  2081. * before releasing the btrfs_trans_num_writers lock then lockdep would
  2082. * complain because we did not follow the reverse order unlocking rule.
  2083. */
  2084. btrfs_trans_state_lockdep_acquire(fs_info, BTRFS_LOCKDEP_TRANS_COMPLETED);
  2085. btrfs_trans_state_lockdep_acquire(fs_info, BTRFS_LOCKDEP_TRANS_SUPER_COMMITTED);
  2086. btrfs_trans_state_lockdep_acquire(fs_info, BTRFS_LOCKDEP_TRANS_UNBLOCKED);
  2087. /*
  2088. * We've started the commit, clear the flag in case we were triggered to
  2089. * do an async commit but somebody else started before the transaction
  2090. * kthread could do the work.
  2091. */
  2092. clear_bit(BTRFS_FS_COMMIT_TRANS, &fs_info->flags);
  2093. if (TRANS_ABORTED(cur_trans)) {
  2094. ret = cur_trans->aborted;
  2095. btrfs_trans_state_lockdep_release(fs_info, BTRFS_LOCKDEP_TRANS_UNBLOCKED);
  2096. goto scrub_continue;
  2097. }
  2098. /*
  2099. * the reloc mutex makes sure that we stop
  2100. * the balancing code from coming in and moving
  2101. * extents around in the middle of the commit
  2102. */
  2103. mutex_lock(&fs_info->reloc_mutex);
  2104. /*
  2105. * We needn't worry about the delayed items because we will
  2106. * deal with them in create_pending_snapshot(), which is the
  2107. * core function of the snapshot creation.
  2108. */
  2109. ret = create_pending_snapshots(trans);
  2110. if (ret)
  2111. goto unlock_reloc;
  2112. /*
  2113. * We insert the dir indexes of the snapshots and update the inode
  2114. * of the snapshots' parents after the snapshot creation, so there
  2115. * are some delayed items which are not dealt with. Now deal with
  2116. * them.
  2117. *
  2118. * We needn't worry that this operation will corrupt the snapshots,
  2119. * because all the tree which are snapshoted will be forced to COW
  2120. * the nodes and leaves.
  2121. */
  2122. ret = btrfs_run_delayed_items(trans);
  2123. if (ret)
  2124. goto unlock_reloc;
  2125. ret = btrfs_run_delayed_refs(trans, U64_MAX);
  2126. if (ret)
  2127. goto unlock_reloc;
  2128. /*
  2129. * make sure none of the code above managed to slip in a
  2130. * delayed item
  2131. */
  2132. btrfs_assert_delayed_root_empty(fs_info);
  2133. WARN_ON(cur_trans != trans->transaction);
  2134. ret = commit_fs_roots(trans);
  2135. if (ret)
  2136. goto unlock_reloc;
  2137. /* commit_fs_roots gets rid of all the tree log roots, it is now
  2138. * safe to free the root of tree log roots
  2139. */
  2140. btrfs_free_log_root_tree(trans, fs_info);
  2141. /*
  2142. * Since fs roots are all committed, we can get a quite accurate
  2143. * new_roots. So let's do quota accounting.
  2144. */
  2145. ret = btrfs_qgroup_account_extents(trans);
  2146. if (ret < 0)
  2147. goto unlock_reloc;
  2148. ret = commit_cowonly_roots(trans);
  2149. if (ret)
  2150. goto unlock_reloc;
  2151. /*
  2152. * The tasks which save the space cache and inode cache may also
  2153. * update ->aborted, check it.
  2154. */
  2155. if (TRANS_ABORTED(cur_trans)) {
  2156. ret = cur_trans->aborted;
  2157. goto unlock_reloc;
  2158. }
  2159. cur_trans = fs_info->running_transaction;
  2160. btrfs_set_root_node(&fs_info->tree_root->root_item,
  2161. fs_info->tree_root->node);
  2162. list_add_tail(&fs_info->tree_root->dirty_list,
  2163. &cur_trans->switch_commits);
  2164. btrfs_set_root_node(&fs_info->chunk_root->root_item,
  2165. fs_info->chunk_root->node);
  2166. list_add_tail(&fs_info->chunk_root->dirty_list,
  2167. &cur_trans->switch_commits);
  2168. if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) {
  2169. btrfs_set_root_node(&fs_info->block_group_root->root_item,
  2170. fs_info->block_group_root->node);
  2171. list_add_tail(&fs_info->block_group_root->dirty_list,
  2172. &cur_trans->switch_commits);
  2173. }
  2174. switch_commit_roots(trans);
  2175. ASSERT(list_empty(&cur_trans->dirty_bgs));
  2176. ASSERT(list_empty(&cur_trans->io_bgs));
  2177. update_super_roots(fs_info);
  2178. btrfs_set_super_log_root(fs_info->super_copy, 0);
  2179. btrfs_set_super_log_root_level(fs_info->super_copy, 0);
  2180. memcpy(fs_info->super_for_commit, fs_info->super_copy,
  2181. sizeof(*fs_info->super_copy));
  2182. btrfs_commit_device_sizes(cur_trans);
  2183. clear_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags);
  2184. clear_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags);
  2185. btrfs_trans_release_chunk_metadata(trans);
  2186. /*
  2187. * Before changing the transaction state to TRANS_STATE_UNBLOCKED and
  2188. * setting fs_info->running_transaction to NULL, lock tree_log_mutex to
  2189. * make sure that before we commit our superblock, no other task can
  2190. * start a new transaction and commit a log tree before we commit our
  2191. * superblock. Anyone trying to commit a log tree locks this mutex before
  2192. * writing its superblock.
  2193. */
  2194. mutex_lock(&fs_info->tree_log_mutex);
  2195. spin_lock(&fs_info->trans_lock);
  2196. cur_trans->state = TRANS_STATE_UNBLOCKED;
  2197. fs_info->running_transaction = NULL;
  2198. spin_unlock(&fs_info->trans_lock);
  2199. mutex_unlock(&fs_info->reloc_mutex);
  2200. wake_up(&fs_info->transaction_wait);
  2201. btrfs_trans_state_lockdep_release(fs_info, BTRFS_LOCKDEP_TRANS_UNBLOCKED);
  2202. /* If we have features changed, wake up the cleaner to update sysfs. */
  2203. if (test_bit(BTRFS_FS_FEATURE_CHANGED, &fs_info->flags) &&
  2204. fs_info->cleaner_kthread)
  2205. wake_up_process(fs_info->cleaner_kthread);
  2206. ret = btrfs_write_and_wait_transaction(trans);
  2207. if (ret) {
  2208. btrfs_handle_fs_error(fs_info, ret,
  2209. "Error while writing out transaction");
  2210. mutex_unlock(&fs_info->tree_log_mutex);
  2211. goto scrub_continue;
  2212. }
  2213. ret = write_all_supers(fs_info, 0);
  2214. /*
  2215. * the super is written, we can safely allow the tree-loggers
  2216. * to go about their business
  2217. */
  2218. mutex_unlock(&fs_info->tree_log_mutex);
  2219. if (ret)
  2220. goto scrub_continue;
  2221. /*
  2222. * We needn't acquire the lock here because there is no other task
  2223. * which can change it.
  2224. */
  2225. cur_trans->state = TRANS_STATE_SUPER_COMMITTED;
  2226. wake_up(&cur_trans->commit_wait);
  2227. btrfs_trans_state_lockdep_release(fs_info, BTRFS_LOCKDEP_TRANS_SUPER_COMMITTED);
  2228. btrfs_finish_extent_commit(trans);
  2229. if (test_bit(BTRFS_TRANS_HAVE_FREE_BGS, &cur_trans->flags))
  2230. btrfs_clear_space_info_full(fs_info);
  2231. btrfs_set_last_trans_committed(fs_info, cur_trans->transid);
  2232. /*
  2233. * We needn't acquire the lock here because there is no other task
  2234. * which can change it.
  2235. */
  2236. cur_trans->state = TRANS_STATE_COMPLETED;
  2237. wake_up(&cur_trans->commit_wait);
  2238. btrfs_trans_state_lockdep_release(fs_info, BTRFS_LOCKDEP_TRANS_COMPLETED);
  2239. spin_lock(&fs_info->trans_lock);
  2240. list_del_init(&cur_trans->list);
  2241. spin_unlock(&fs_info->trans_lock);
  2242. btrfs_put_transaction(cur_trans);
  2243. btrfs_put_transaction(cur_trans);
  2244. if (trans->type & __TRANS_FREEZABLE)
  2245. sb_end_intwrite(fs_info->sb);
  2246. trace_btrfs_transaction_commit(fs_info);
  2247. interval = ktime_get_ns() - start_time;
  2248. btrfs_scrub_continue(fs_info);
  2249. if (current->journal_info == trans)
  2250. current->journal_info = NULL;
  2251. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  2252. update_commit_stats(fs_info, interval);
  2253. return ret;
  2254. unlock_reloc:
  2255. mutex_unlock(&fs_info->reloc_mutex);
  2256. btrfs_trans_state_lockdep_release(fs_info, BTRFS_LOCKDEP_TRANS_UNBLOCKED);
  2257. scrub_continue:
  2258. btrfs_trans_state_lockdep_release(fs_info, BTRFS_LOCKDEP_TRANS_SUPER_COMMITTED);
  2259. btrfs_trans_state_lockdep_release(fs_info, BTRFS_LOCKDEP_TRANS_COMPLETED);
  2260. btrfs_scrub_continue(fs_info);
  2261. cleanup_transaction:
  2262. btrfs_trans_release_metadata(trans);
  2263. btrfs_cleanup_pending_block_groups(trans);
  2264. btrfs_trans_release_chunk_metadata(trans);
  2265. trans->block_rsv = NULL;
  2266. btrfs_warn(fs_info, "Skipping commit of aborted transaction.");
  2267. if (current->journal_info == trans)
  2268. current->journal_info = NULL;
  2269. cleanup_transaction(trans, ret);
  2270. return ret;
  2271. lockdep_release:
  2272. btrfs_lockdep_release(fs_info, btrfs_trans_num_extwriters);
  2273. btrfs_lockdep_release(fs_info, btrfs_trans_num_writers);
  2274. goto cleanup_transaction;
  2275. lockdep_trans_commit_start_release:
  2276. btrfs_trans_state_lockdep_release(fs_info, BTRFS_LOCKDEP_TRANS_COMMIT_PREP);
  2277. btrfs_end_transaction(trans);
  2278. return ret;
  2279. }
  2280. /*
  2281. * return < 0 if error
  2282. * 0 if there are no more dead_roots at the time of call
  2283. * 1 there are more to be processed, call me again
  2284. *
  2285. * The return value indicates there are certainly more snapshots to delete, but
  2286. * if there comes a new one during processing, it may return 0. We don't mind,
  2287. * because btrfs_commit_super will poke cleaner thread and it will process it a
  2288. * few seconds later.
  2289. */
  2290. int btrfs_clean_one_deleted_snapshot(struct btrfs_fs_info *fs_info)
  2291. {
  2292. struct btrfs_root *root;
  2293. int ret;
  2294. spin_lock(&fs_info->trans_lock);
  2295. if (list_empty(&fs_info->dead_roots)) {
  2296. spin_unlock(&fs_info->trans_lock);
  2297. return 0;
  2298. }
  2299. root = list_first_entry(&fs_info->dead_roots,
  2300. struct btrfs_root, root_list);
  2301. list_del_init(&root->root_list);
  2302. spin_unlock(&fs_info->trans_lock);
  2303. btrfs_debug(fs_info, "cleaner removing %llu", btrfs_root_id(root));
  2304. btrfs_kill_all_delayed_nodes(root);
  2305. if (btrfs_header_backref_rev(root->node) <
  2306. BTRFS_MIXED_BACKREF_REV)
  2307. ret = btrfs_drop_snapshot(root, 0, 0);
  2308. else
  2309. ret = btrfs_drop_snapshot(root, 1, 0);
  2310. btrfs_put_root(root);
  2311. return (ret < 0) ? 0 : 1;
  2312. }
  2313. /*
  2314. * We only mark the transaction aborted and then set the file system read-only.
  2315. * This will prevent new transactions from starting or trying to join this
  2316. * one.
  2317. *
  2318. * This means that error recovery at the call site is limited to freeing
  2319. * any local memory allocations and passing the error code up without
  2320. * further cleanup. The transaction should complete as it normally would
  2321. * in the call path but will return -EIO.
  2322. *
  2323. * We'll complete the cleanup in btrfs_end_transaction and
  2324. * btrfs_commit_transaction.
  2325. */
  2326. void __cold __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
  2327. const char *function,
  2328. unsigned int line, int error, bool first_hit)
  2329. {
  2330. struct btrfs_fs_info *fs_info = trans->fs_info;
  2331. WRITE_ONCE(trans->aborted, error);
  2332. WRITE_ONCE(trans->transaction->aborted, error);
  2333. if (first_hit && error == -ENOSPC)
  2334. btrfs_dump_space_info_for_trans_abort(fs_info);
  2335. /* Wake up anybody who may be waiting on this transaction */
  2336. wake_up(&fs_info->transaction_wait);
  2337. wake_up(&fs_info->transaction_blocked_wait);
  2338. __btrfs_handle_fs_error(fs_info, function, line, error, NULL);
  2339. }
  2340. int __init btrfs_transaction_init(void)
  2341. {
  2342. btrfs_trans_handle_cachep = KMEM_CACHE(btrfs_trans_handle, SLAB_TEMPORARY);
  2343. if (!btrfs_trans_handle_cachep)
  2344. return -ENOMEM;
  2345. return 0;
  2346. }
  2347. void __cold btrfs_transaction_exit(void)
  2348. {
  2349. kmem_cache_destroy(btrfs_trans_handle_cachep);
  2350. }