disk-io.c 125 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583
  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/blkdev.h>
  7. #include <linux/radix-tree.h>
  8. #include <linux/writeback.h>
  9. #include <linux/buffer_head.h>
  10. #include <linux/workqueue.h>
  11. #include <linux/kthread.h>
  12. #include <linux/slab.h>
  13. #include <linux/migrate.h>
  14. #include <linux/ratelimit.h>
  15. #include <linux/uuid.h>
  16. #include <linux/semaphore.h>
  17. #include <linux/error-injection.h>
  18. #include <linux/crc32c.h>
  19. #include <linux/sched/mm.h>
  20. #include <asm/unaligned.h>
  21. #include "ctree.h"
  22. #include "disk-io.h"
  23. #include "transaction.h"
  24. #include "btrfs_inode.h"
  25. #include "volumes.h"
  26. #include "print-tree.h"
  27. #include "locking.h"
  28. #include "tree-log.h"
  29. #include "free-space-cache.h"
  30. #include "free-space-tree.h"
  31. #include "inode-map.h"
  32. #include "check-integrity.h"
  33. #include "rcu-string.h"
  34. #include "dev-replace.h"
  35. #include "raid56.h"
  36. #include "sysfs.h"
  37. #include "qgroup.h"
  38. #include "compression.h"
  39. #include "tree-checker.h"
  40. #include "ref-verify.h"
  41. #ifdef CONFIG_X86
  42. #include <asm/cpufeature.h>
  43. #endif
  44. #define BTRFS_SUPER_FLAG_SUPP (BTRFS_HEADER_FLAG_WRITTEN |\
  45. BTRFS_HEADER_FLAG_RELOC |\
  46. BTRFS_SUPER_FLAG_ERROR |\
  47. BTRFS_SUPER_FLAG_SEEDING |\
  48. BTRFS_SUPER_FLAG_METADUMP |\
  49. BTRFS_SUPER_FLAG_METADUMP_V2)
  50. static const struct extent_io_ops btree_extent_io_ops;
  51. static void end_workqueue_fn(struct btrfs_work *work);
  52. static void btrfs_destroy_ordered_extents(struct btrfs_root *root);
  53. static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans,
  54. struct btrfs_fs_info *fs_info);
  55. static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root);
  56. static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info,
  57. struct extent_io_tree *dirty_pages,
  58. int mark);
  59. static int btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info,
  60. struct extent_io_tree *pinned_extents);
  61. static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info);
  62. static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info);
  63. /*
  64. * btrfs_end_io_wq structs are used to do processing in task context when an IO
  65. * is complete. This is used during reads to verify checksums, and it is used
  66. * by writes to insert metadata for new file extents after IO is complete.
  67. */
  68. struct btrfs_end_io_wq {
  69. struct bio *bio;
  70. bio_end_io_t *end_io;
  71. void *private;
  72. struct btrfs_fs_info *info;
  73. blk_status_t status;
  74. enum btrfs_wq_endio_type metadata;
  75. struct btrfs_work work;
  76. };
  77. static struct kmem_cache *btrfs_end_io_wq_cache;
  78. int __init btrfs_end_io_wq_init(void)
  79. {
  80. btrfs_end_io_wq_cache = kmem_cache_create("btrfs_end_io_wq",
  81. sizeof(struct btrfs_end_io_wq),
  82. 0,
  83. SLAB_MEM_SPREAD,
  84. NULL);
  85. if (!btrfs_end_io_wq_cache)
  86. return -ENOMEM;
  87. return 0;
  88. }
  89. void __cold btrfs_end_io_wq_exit(void)
  90. {
  91. kmem_cache_destroy(btrfs_end_io_wq_cache);
  92. }
  93. /*
  94. * async submit bios are used to offload expensive checksumming
  95. * onto the worker threads. They checksum file and metadata bios
  96. * just before they are sent down the IO stack.
  97. */
  98. struct async_submit_bio {
  99. void *private_data;
  100. struct bio *bio;
  101. extent_submit_bio_start_t *submit_bio_start;
  102. int mirror_num;
  103. /*
  104. * bio_offset is optional, can be used if the pages in the bio
  105. * can't tell us where in the file the bio should go
  106. */
  107. u64 bio_offset;
  108. struct btrfs_work work;
  109. blk_status_t status;
  110. };
  111. /*
  112. * Lockdep class keys for extent_buffer->lock's in this root. For a given
  113. * eb, the lockdep key is determined by the btrfs_root it belongs to and
  114. * the level the eb occupies in the tree.
  115. *
  116. * Different roots are used for different purposes and may nest inside each
  117. * other and they require separate keysets. As lockdep keys should be
  118. * static, assign keysets according to the purpose of the root as indicated
  119. * by btrfs_root->objectid. This ensures that all special purpose roots
  120. * have separate keysets.
  121. *
  122. * Lock-nesting across peer nodes is always done with the immediate parent
  123. * node locked thus preventing deadlock. As lockdep doesn't know this, use
  124. * subclass to avoid triggering lockdep warning in such cases.
  125. *
  126. * The key is set by the readpage_end_io_hook after the buffer has passed
  127. * csum validation but before the pages are unlocked. It is also set by
  128. * btrfs_init_new_buffer on freshly allocated blocks.
  129. *
  130. * We also add a check to make sure the highest level of the tree is the
  131. * same as our lockdep setup here. If BTRFS_MAX_LEVEL changes, this code
  132. * needs update as well.
  133. */
  134. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  135. # if BTRFS_MAX_LEVEL != 8
  136. # error
  137. # endif
  138. static struct btrfs_lockdep_keyset {
  139. u64 id; /* root objectid */
  140. const char *name_stem; /* lock name stem */
  141. char names[BTRFS_MAX_LEVEL + 1][20];
  142. struct lock_class_key keys[BTRFS_MAX_LEVEL + 1];
  143. } btrfs_lockdep_keysets[] = {
  144. { .id = BTRFS_ROOT_TREE_OBJECTID, .name_stem = "root" },
  145. { .id = BTRFS_EXTENT_TREE_OBJECTID, .name_stem = "extent" },
  146. { .id = BTRFS_CHUNK_TREE_OBJECTID, .name_stem = "chunk" },
  147. { .id = BTRFS_DEV_TREE_OBJECTID, .name_stem = "dev" },
  148. { .id = BTRFS_FS_TREE_OBJECTID, .name_stem = "fs" },
  149. { .id = BTRFS_CSUM_TREE_OBJECTID, .name_stem = "csum" },
  150. { .id = BTRFS_QUOTA_TREE_OBJECTID, .name_stem = "quota" },
  151. { .id = BTRFS_TREE_LOG_OBJECTID, .name_stem = "log" },
  152. { .id = BTRFS_TREE_RELOC_OBJECTID, .name_stem = "treloc" },
  153. { .id = BTRFS_DATA_RELOC_TREE_OBJECTID, .name_stem = "dreloc" },
  154. { .id = BTRFS_UUID_TREE_OBJECTID, .name_stem = "uuid" },
  155. { .id = BTRFS_FREE_SPACE_TREE_OBJECTID, .name_stem = "free-space" },
  156. { .id = 0, .name_stem = "tree" },
  157. };
  158. void __init btrfs_init_lockdep(void)
  159. {
  160. int i, j;
  161. /* initialize lockdep class names */
  162. for (i = 0; i < ARRAY_SIZE(btrfs_lockdep_keysets); i++) {
  163. struct btrfs_lockdep_keyset *ks = &btrfs_lockdep_keysets[i];
  164. for (j = 0; j < ARRAY_SIZE(ks->names); j++)
  165. snprintf(ks->names[j], sizeof(ks->names[j]),
  166. "btrfs-%s-%02d", ks->name_stem, j);
  167. }
  168. }
  169. void btrfs_set_buffer_lockdep_class(u64 objectid, struct extent_buffer *eb,
  170. int level)
  171. {
  172. struct btrfs_lockdep_keyset *ks;
  173. BUG_ON(level >= ARRAY_SIZE(ks->keys));
  174. /* find the matching keyset, id 0 is the default entry */
  175. for (ks = btrfs_lockdep_keysets; ks->id; ks++)
  176. if (ks->id == objectid)
  177. break;
  178. lockdep_set_class_and_name(&eb->lock,
  179. &ks->keys[level], ks->names[level]);
  180. }
  181. #endif
  182. /*
  183. * extents on the btree inode are pretty simple, there's one extent
  184. * that covers the entire device
  185. */
  186. struct extent_map *btree_get_extent(struct btrfs_inode *inode,
  187. struct page *page, size_t pg_offset, u64 start, u64 len,
  188. int create)
  189. {
  190. struct btrfs_fs_info *fs_info = inode->root->fs_info;
  191. struct extent_map_tree *em_tree = &inode->extent_tree;
  192. struct extent_map *em;
  193. int ret;
  194. read_lock(&em_tree->lock);
  195. em = lookup_extent_mapping(em_tree, start, len);
  196. if (em) {
  197. em->bdev = fs_info->fs_devices->latest_bdev;
  198. read_unlock(&em_tree->lock);
  199. goto out;
  200. }
  201. read_unlock(&em_tree->lock);
  202. em = alloc_extent_map();
  203. if (!em) {
  204. em = ERR_PTR(-ENOMEM);
  205. goto out;
  206. }
  207. em->start = 0;
  208. em->len = (u64)-1;
  209. em->block_len = (u64)-1;
  210. em->block_start = 0;
  211. em->bdev = fs_info->fs_devices->latest_bdev;
  212. write_lock(&em_tree->lock);
  213. ret = add_extent_mapping(em_tree, em, 0);
  214. if (ret == -EEXIST) {
  215. free_extent_map(em);
  216. em = lookup_extent_mapping(em_tree, start, len);
  217. if (!em)
  218. em = ERR_PTR(-EIO);
  219. } else if (ret) {
  220. free_extent_map(em);
  221. em = ERR_PTR(ret);
  222. }
  223. write_unlock(&em_tree->lock);
  224. out:
  225. return em;
  226. }
  227. u32 btrfs_csum_data(const char *data, u32 seed, size_t len)
  228. {
  229. return crc32c(seed, data, len);
  230. }
  231. void btrfs_csum_final(u32 crc, u8 *result)
  232. {
  233. put_unaligned_le32(~crc, result);
  234. }
  235. /*
  236. * compute the csum for a btree block, and either verify it or write it
  237. * into the csum field of the block.
  238. */
  239. static int csum_tree_block(struct btrfs_fs_info *fs_info,
  240. struct extent_buffer *buf,
  241. int verify)
  242. {
  243. u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
  244. char result[BTRFS_CSUM_SIZE];
  245. unsigned long len;
  246. unsigned long cur_len;
  247. unsigned long offset = BTRFS_CSUM_SIZE;
  248. char *kaddr;
  249. unsigned long map_start;
  250. unsigned long map_len;
  251. int err;
  252. u32 crc = ~(u32)0;
  253. len = buf->len - offset;
  254. while (len > 0) {
  255. err = map_private_extent_buffer(buf, offset, 32,
  256. &kaddr, &map_start, &map_len);
  257. if (err)
  258. return err;
  259. cur_len = min(len, map_len - (offset - map_start));
  260. crc = btrfs_csum_data(kaddr + offset - map_start,
  261. crc, cur_len);
  262. len -= cur_len;
  263. offset += cur_len;
  264. }
  265. memset(result, 0, BTRFS_CSUM_SIZE);
  266. btrfs_csum_final(crc, result);
  267. if (verify) {
  268. if (memcmp_extent_buffer(buf, result, 0, csum_size)) {
  269. u32 val;
  270. u32 found = 0;
  271. memcpy(&found, result, csum_size);
  272. read_extent_buffer(buf, &val, 0, csum_size);
  273. btrfs_warn_rl(fs_info,
  274. "%s checksum verify failed on %llu wanted %X found %X level %d",
  275. fs_info->sb->s_id, buf->start,
  276. val, found, btrfs_header_level(buf));
  277. return -EUCLEAN;
  278. }
  279. } else {
  280. write_extent_buffer(buf, result, 0, csum_size);
  281. }
  282. return 0;
  283. }
  284. /*
  285. * we can't consider a given block up to date unless the transid of the
  286. * block matches the transid in the parent node's pointer. This is how we
  287. * detect blocks that either didn't get written at all or got written
  288. * in the wrong place.
  289. */
  290. static int verify_parent_transid(struct extent_io_tree *io_tree,
  291. struct extent_buffer *eb, u64 parent_transid,
  292. int atomic)
  293. {
  294. struct extent_state *cached_state = NULL;
  295. int ret;
  296. bool need_lock = (current->journal_info == BTRFS_SEND_TRANS_STUB);
  297. if (!parent_transid || btrfs_header_generation(eb) == parent_transid)
  298. return 0;
  299. if (atomic)
  300. return -EAGAIN;
  301. if (need_lock) {
  302. btrfs_tree_read_lock(eb);
  303. btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
  304. }
  305. lock_extent_bits(io_tree, eb->start, eb->start + eb->len - 1,
  306. &cached_state);
  307. if (extent_buffer_uptodate(eb) &&
  308. btrfs_header_generation(eb) == parent_transid) {
  309. ret = 0;
  310. goto out;
  311. }
  312. btrfs_err_rl(eb->fs_info,
  313. "parent transid verify failed on %llu wanted %llu found %llu",
  314. eb->start,
  315. parent_transid, btrfs_header_generation(eb));
  316. ret = 1;
  317. /*
  318. * Things reading via commit roots that don't have normal protection,
  319. * like send, can have a really old block in cache that may point at a
  320. * block that has been freed and re-allocated. So don't clear uptodate
  321. * if we find an eb that is under IO (dirty/writeback) because we could
  322. * end up reading in the stale data and then writing it back out and
  323. * making everybody very sad.
  324. */
  325. if (!extent_buffer_under_io(eb))
  326. clear_extent_buffer_uptodate(eb);
  327. out:
  328. unlock_extent_cached(io_tree, eb->start, eb->start + eb->len - 1,
  329. &cached_state);
  330. if (need_lock)
  331. btrfs_tree_read_unlock_blocking(eb);
  332. return ret;
  333. }
  334. /*
  335. * Return 0 if the superblock checksum type matches the checksum value of that
  336. * algorithm. Pass the raw disk superblock data.
  337. */
  338. static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info,
  339. char *raw_disk_sb)
  340. {
  341. struct btrfs_super_block *disk_sb =
  342. (struct btrfs_super_block *)raw_disk_sb;
  343. u16 csum_type = btrfs_super_csum_type(disk_sb);
  344. int ret = 0;
  345. if (csum_type == BTRFS_CSUM_TYPE_CRC32) {
  346. u32 crc = ~(u32)0;
  347. char result[sizeof(crc)];
  348. /*
  349. * The super_block structure does not span the whole
  350. * BTRFS_SUPER_INFO_SIZE range, we expect that the unused space
  351. * is filled with zeros and is included in the checksum.
  352. */
  353. crc = btrfs_csum_data(raw_disk_sb + BTRFS_CSUM_SIZE,
  354. crc, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
  355. btrfs_csum_final(crc, result);
  356. if (memcmp(raw_disk_sb, result, sizeof(result)))
  357. ret = 1;
  358. }
  359. if (csum_type >= ARRAY_SIZE(btrfs_csum_sizes)) {
  360. btrfs_err(fs_info, "unsupported checksum algorithm %u",
  361. csum_type);
  362. ret = 1;
  363. }
  364. return ret;
  365. }
  366. int btrfs_verify_level_key(struct btrfs_fs_info *fs_info,
  367. struct extent_buffer *eb, int level,
  368. struct btrfs_key *first_key, u64 parent_transid)
  369. {
  370. int found_level;
  371. struct btrfs_key found_key;
  372. int ret;
  373. found_level = btrfs_header_level(eb);
  374. if (found_level != level) {
  375. #ifdef CONFIG_BTRFS_DEBUG
  376. WARN_ON(1);
  377. btrfs_err(fs_info,
  378. "tree level mismatch detected, bytenr=%llu level expected=%u has=%u",
  379. eb->start, level, found_level);
  380. #endif
  381. return -EIO;
  382. }
  383. if (!first_key)
  384. return 0;
  385. /*
  386. * For live tree block (new tree blocks in current transaction),
  387. * we need proper lock context to avoid race, which is impossible here.
  388. * So we only checks tree blocks which is read from disk, whose
  389. * generation <= fs_info->last_trans_committed.
  390. */
  391. if (btrfs_header_generation(eb) > fs_info->last_trans_committed)
  392. return 0;
  393. /* We have @first_key, so this @eb must have at least one item */
  394. if (btrfs_header_nritems(eb) == 0) {
  395. btrfs_err(fs_info,
  396. "invalid tree nritems, bytenr=%llu nritems=0 expect >0",
  397. eb->start);
  398. WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG));
  399. return -EUCLEAN;
  400. }
  401. if (found_level)
  402. btrfs_node_key_to_cpu(eb, &found_key, 0);
  403. else
  404. btrfs_item_key_to_cpu(eb, &found_key, 0);
  405. ret = btrfs_comp_cpu_keys(first_key, &found_key);
  406. #ifdef CONFIG_BTRFS_DEBUG
  407. if (ret) {
  408. WARN_ON(1);
  409. btrfs_err(fs_info,
  410. "tree first key mismatch detected, bytenr=%llu parent_transid=%llu key expected=(%llu,%u,%llu) has=(%llu,%u,%llu)",
  411. eb->start, parent_transid, first_key->objectid,
  412. first_key->type, first_key->offset,
  413. found_key.objectid, found_key.type,
  414. found_key.offset);
  415. }
  416. #endif
  417. return ret;
  418. }
  419. /*
  420. * helper to read a given tree block, doing retries as required when
  421. * the checksums don't match and we have alternate mirrors to try.
  422. *
  423. * @parent_transid: expected transid, skip check if 0
  424. * @level: expected level, mandatory check
  425. * @first_key: expected key of first slot, skip check if NULL
  426. */
  427. static int btree_read_extent_buffer_pages(struct btrfs_fs_info *fs_info,
  428. struct extent_buffer *eb,
  429. u64 parent_transid, int level,
  430. struct btrfs_key *first_key)
  431. {
  432. struct extent_io_tree *io_tree;
  433. int failed = 0;
  434. int ret;
  435. int num_copies = 0;
  436. int mirror_num = 0;
  437. int failed_mirror = 0;
  438. io_tree = &BTRFS_I(fs_info->btree_inode)->io_tree;
  439. while (1) {
  440. clear_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags);
  441. ret = read_extent_buffer_pages(io_tree, eb, WAIT_COMPLETE,
  442. mirror_num);
  443. if (!ret) {
  444. if (verify_parent_transid(io_tree, eb,
  445. parent_transid, 0))
  446. ret = -EIO;
  447. else if (btrfs_verify_level_key(fs_info, eb, level,
  448. first_key, parent_transid))
  449. ret = -EUCLEAN;
  450. else
  451. break;
  452. }
  453. num_copies = btrfs_num_copies(fs_info,
  454. eb->start, eb->len);
  455. if (num_copies == 1)
  456. break;
  457. if (!failed_mirror) {
  458. failed = 1;
  459. failed_mirror = eb->read_mirror;
  460. }
  461. mirror_num++;
  462. if (mirror_num == failed_mirror)
  463. mirror_num++;
  464. if (mirror_num > num_copies)
  465. break;
  466. }
  467. if (failed && !ret && failed_mirror)
  468. repair_eb_io_failure(fs_info, eb, failed_mirror);
  469. return ret;
  470. }
  471. /*
  472. * checksum a dirty tree block before IO. This has extra checks to make sure
  473. * we only fill in the checksum field in the first page of a multi-page block
  474. */
  475. static int csum_dirty_buffer(struct btrfs_fs_info *fs_info, struct page *page)
  476. {
  477. u64 start = page_offset(page);
  478. u64 found_start;
  479. struct extent_buffer *eb;
  480. eb = (struct extent_buffer *)page->private;
  481. if (page != eb->pages[0])
  482. return 0;
  483. found_start = btrfs_header_bytenr(eb);
  484. /*
  485. * Please do not consolidate these warnings into a single if.
  486. * It is useful to know what went wrong.
  487. */
  488. if (WARN_ON(found_start != start))
  489. return -EUCLEAN;
  490. if (WARN_ON(!PageUptodate(page)))
  491. return -EUCLEAN;
  492. ASSERT(memcmp_extent_buffer(eb, fs_info->fsid,
  493. btrfs_header_fsid(), BTRFS_FSID_SIZE) == 0);
  494. return csum_tree_block(fs_info, eb, 0);
  495. }
  496. static int check_tree_block_fsid(struct btrfs_fs_info *fs_info,
  497. struct extent_buffer *eb)
  498. {
  499. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  500. u8 fsid[BTRFS_FSID_SIZE];
  501. int ret = 1;
  502. read_extent_buffer(eb, fsid, btrfs_header_fsid(), BTRFS_FSID_SIZE);
  503. while (fs_devices) {
  504. if (!memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE)) {
  505. ret = 0;
  506. break;
  507. }
  508. fs_devices = fs_devices->seed;
  509. }
  510. return ret;
  511. }
  512. static int btree_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
  513. u64 phy_offset, struct page *page,
  514. u64 start, u64 end, int mirror)
  515. {
  516. u64 found_start;
  517. int found_level;
  518. struct extent_buffer *eb;
  519. struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
  520. struct btrfs_fs_info *fs_info = root->fs_info;
  521. int ret = 0;
  522. int reads_done;
  523. if (!page->private)
  524. goto out;
  525. eb = (struct extent_buffer *)page->private;
  526. /* the pending IO might have been the only thing that kept this buffer
  527. * in memory. Make sure we have a ref for all this other checks
  528. */
  529. extent_buffer_get(eb);
  530. reads_done = atomic_dec_and_test(&eb->io_pages);
  531. if (!reads_done)
  532. goto err;
  533. eb->read_mirror = mirror;
  534. if (test_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags)) {
  535. ret = -EIO;
  536. goto err;
  537. }
  538. found_start = btrfs_header_bytenr(eb);
  539. if (found_start != eb->start) {
  540. btrfs_err_rl(fs_info, "bad tree block start, want %llu have %llu",
  541. eb->start, found_start);
  542. ret = -EIO;
  543. goto err;
  544. }
  545. if (check_tree_block_fsid(fs_info, eb)) {
  546. btrfs_err_rl(fs_info, "bad fsid on block %llu",
  547. eb->start);
  548. ret = -EIO;
  549. goto err;
  550. }
  551. found_level = btrfs_header_level(eb);
  552. if (found_level >= BTRFS_MAX_LEVEL) {
  553. btrfs_err(fs_info, "bad tree block level %d on %llu",
  554. (int)btrfs_header_level(eb), eb->start);
  555. ret = -EIO;
  556. goto err;
  557. }
  558. btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb),
  559. eb, found_level);
  560. ret = csum_tree_block(fs_info, eb, 1);
  561. if (ret)
  562. goto err;
  563. /*
  564. * If this is a leaf block and it is corrupt, set the corrupt bit so
  565. * that we don't try and read the other copies of this block, just
  566. * return -EIO.
  567. */
  568. if (found_level == 0 && btrfs_check_leaf_full(fs_info, eb)) {
  569. set_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags);
  570. ret = -EIO;
  571. }
  572. if (found_level > 0 && btrfs_check_node(fs_info, eb))
  573. ret = -EIO;
  574. if (!ret)
  575. set_extent_buffer_uptodate(eb);
  576. err:
  577. if (reads_done &&
  578. test_and_clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags))
  579. btree_readahead_hook(eb, ret);
  580. if (ret) {
  581. /*
  582. * our io error hook is going to dec the io pages
  583. * again, we have to make sure it has something
  584. * to decrement
  585. */
  586. atomic_inc(&eb->io_pages);
  587. clear_extent_buffer_uptodate(eb);
  588. }
  589. free_extent_buffer(eb);
  590. out:
  591. return ret;
  592. }
  593. static int btree_io_failed_hook(struct page *page, int failed_mirror)
  594. {
  595. struct extent_buffer *eb;
  596. eb = (struct extent_buffer *)page->private;
  597. set_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
  598. eb->read_mirror = failed_mirror;
  599. atomic_dec(&eb->io_pages);
  600. if (test_and_clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags))
  601. btree_readahead_hook(eb, -EIO);
  602. return -EIO; /* we fixed nothing */
  603. }
  604. static void end_workqueue_bio(struct bio *bio)
  605. {
  606. struct btrfs_end_io_wq *end_io_wq = bio->bi_private;
  607. struct btrfs_fs_info *fs_info;
  608. struct btrfs_workqueue *wq;
  609. btrfs_work_func_t func;
  610. fs_info = end_io_wq->info;
  611. end_io_wq->status = bio->bi_status;
  612. if (bio_op(bio) == REQ_OP_WRITE) {
  613. if (end_io_wq->metadata == BTRFS_WQ_ENDIO_METADATA) {
  614. wq = fs_info->endio_meta_write_workers;
  615. func = btrfs_endio_meta_write_helper;
  616. } else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_FREE_SPACE) {
  617. wq = fs_info->endio_freespace_worker;
  618. func = btrfs_freespace_write_helper;
  619. } else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) {
  620. wq = fs_info->endio_raid56_workers;
  621. func = btrfs_endio_raid56_helper;
  622. } else {
  623. wq = fs_info->endio_write_workers;
  624. func = btrfs_endio_write_helper;
  625. }
  626. } else {
  627. if (unlikely(end_io_wq->metadata ==
  628. BTRFS_WQ_ENDIO_DIO_REPAIR)) {
  629. wq = fs_info->endio_repair_workers;
  630. func = btrfs_endio_repair_helper;
  631. } else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) {
  632. wq = fs_info->endio_raid56_workers;
  633. func = btrfs_endio_raid56_helper;
  634. } else if (end_io_wq->metadata) {
  635. wq = fs_info->endio_meta_workers;
  636. func = btrfs_endio_meta_helper;
  637. } else {
  638. wq = fs_info->endio_workers;
  639. func = btrfs_endio_helper;
  640. }
  641. }
  642. btrfs_init_work(&end_io_wq->work, func, end_workqueue_fn, NULL, NULL);
  643. btrfs_queue_work(wq, &end_io_wq->work);
  644. }
  645. blk_status_t btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio,
  646. enum btrfs_wq_endio_type metadata)
  647. {
  648. struct btrfs_end_io_wq *end_io_wq;
  649. end_io_wq = kmem_cache_alloc(btrfs_end_io_wq_cache, GFP_NOFS);
  650. if (!end_io_wq)
  651. return BLK_STS_RESOURCE;
  652. end_io_wq->private = bio->bi_private;
  653. end_io_wq->end_io = bio->bi_end_io;
  654. end_io_wq->info = info;
  655. end_io_wq->status = 0;
  656. end_io_wq->bio = bio;
  657. end_io_wq->metadata = metadata;
  658. bio->bi_private = end_io_wq;
  659. bio->bi_end_io = end_workqueue_bio;
  660. return 0;
  661. }
  662. static void run_one_async_start(struct btrfs_work *work)
  663. {
  664. struct async_submit_bio *async;
  665. blk_status_t ret;
  666. async = container_of(work, struct async_submit_bio, work);
  667. ret = async->submit_bio_start(async->private_data, async->bio,
  668. async->bio_offset);
  669. if (ret)
  670. async->status = ret;
  671. }
  672. static void run_one_async_done(struct btrfs_work *work)
  673. {
  674. struct async_submit_bio *async;
  675. async = container_of(work, struct async_submit_bio, work);
  676. /* If an error occurred we just want to clean up the bio and move on */
  677. if (async->status) {
  678. async->bio->bi_status = async->status;
  679. bio_endio(async->bio);
  680. return;
  681. }
  682. btrfs_submit_bio_done(async->private_data, async->bio, async->mirror_num);
  683. }
  684. static void run_one_async_free(struct btrfs_work *work)
  685. {
  686. struct async_submit_bio *async;
  687. async = container_of(work, struct async_submit_bio, work);
  688. kfree(async);
  689. }
  690. blk_status_t btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct bio *bio,
  691. int mirror_num, unsigned long bio_flags,
  692. u64 bio_offset, void *private_data,
  693. extent_submit_bio_start_t *submit_bio_start)
  694. {
  695. struct async_submit_bio *async;
  696. async = kmalloc(sizeof(*async), GFP_NOFS);
  697. if (!async)
  698. return BLK_STS_RESOURCE;
  699. async->private_data = private_data;
  700. async->bio = bio;
  701. async->mirror_num = mirror_num;
  702. async->submit_bio_start = submit_bio_start;
  703. btrfs_init_work(&async->work, btrfs_worker_helper, run_one_async_start,
  704. run_one_async_done, run_one_async_free);
  705. async->bio_offset = bio_offset;
  706. async->status = 0;
  707. if (op_is_sync(bio->bi_opf))
  708. btrfs_set_work_high_priority(&async->work);
  709. btrfs_queue_work(fs_info->workers, &async->work);
  710. return 0;
  711. }
  712. static blk_status_t btree_csum_one_bio(struct bio *bio)
  713. {
  714. struct bio_vec *bvec;
  715. struct btrfs_root *root;
  716. int i, ret = 0;
  717. ASSERT(!bio_flagged(bio, BIO_CLONED));
  718. bio_for_each_segment_all(bvec, bio, i) {
  719. root = BTRFS_I(bvec->bv_page->mapping->host)->root;
  720. ret = csum_dirty_buffer(root->fs_info, bvec->bv_page);
  721. if (ret)
  722. break;
  723. }
  724. return errno_to_blk_status(ret);
  725. }
  726. static blk_status_t btree_submit_bio_start(void *private_data, struct bio *bio,
  727. u64 bio_offset)
  728. {
  729. /*
  730. * when we're called for a write, we're already in the async
  731. * submission context. Just jump into btrfs_map_bio
  732. */
  733. return btree_csum_one_bio(bio);
  734. }
  735. static int check_async_write(struct btrfs_inode *bi)
  736. {
  737. if (atomic_read(&bi->sync_writers))
  738. return 0;
  739. #ifdef CONFIG_X86
  740. if (static_cpu_has(X86_FEATURE_XMM4_2))
  741. return 0;
  742. #endif
  743. return 1;
  744. }
  745. static blk_status_t btree_submit_bio_hook(void *private_data, struct bio *bio,
  746. int mirror_num, unsigned long bio_flags,
  747. u64 bio_offset)
  748. {
  749. struct inode *inode = private_data;
  750. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  751. int async = check_async_write(BTRFS_I(inode));
  752. blk_status_t ret;
  753. if (bio_op(bio) != REQ_OP_WRITE) {
  754. /*
  755. * called for a read, do the setup so that checksum validation
  756. * can happen in the async kernel threads
  757. */
  758. ret = btrfs_bio_wq_end_io(fs_info, bio,
  759. BTRFS_WQ_ENDIO_METADATA);
  760. if (ret)
  761. goto out_w_error;
  762. ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
  763. } else if (!async) {
  764. ret = btree_csum_one_bio(bio);
  765. if (ret)
  766. goto out_w_error;
  767. ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
  768. } else {
  769. /*
  770. * kthread helpers are used to submit writes so that
  771. * checksumming can happen in parallel across all CPUs
  772. */
  773. ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, 0,
  774. bio_offset, private_data,
  775. btree_submit_bio_start);
  776. }
  777. if (ret)
  778. goto out_w_error;
  779. return 0;
  780. out_w_error:
  781. bio->bi_status = ret;
  782. bio_endio(bio);
  783. return ret;
  784. }
  785. #ifdef CONFIG_MIGRATION
  786. static int btree_migratepage(struct address_space *mapping,
  787. struct page *newpage, struct page *page,
  788. enum migrate_mode mode)
  789. {
  790. /*
  791. * we can't safely write a btree page from here,
  792. * we haven't done the locking hook
  793. */
  794. if (PageDirty(page))
  795. return -EAGAIN;
  796. /*
  797. * Buffers may be managed in a filesystem specific way.
  798. * We must have no buffers or drop them.
  799. */
  800. if (page_has_private(page) &&
  801. !try_to_release_page(page, GFP_KERNEL))
  802. return -EAGAIN;
  803. return migrate_page(mapping, newpage, page, mode);
  804. }
  805. #endif
  806. static int btree_writepages(struct address_space *mapping,
  807. struct writeback_control *wbc)
  808. {
  809. struct btrfs_fs_info *fs_info;
  810. int ret;
  811. if (wbc->sync_mode == WB_SYNC_NONE) {
  812. if (wbc->for_kupdate)
  813. return 0;
  814. fs_info = BTRFS_I(mapping->host)->root->fs_info;
  815. /* this is a bit racy, but that's ok */
  816. ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes,
  817. BTRFS_DIRTY_METADATA_THRESH,
  818. fs_info->dirty_metadata_batch);
  819. if (ret < 0)
  820. return 0;
  821. }
  822. return btree_write_cache_pages(mapping, wbc);
  823. }
  824. static int btree_readpage(struct file *file, struct page *page)
  825. {
  826. struct extent_io_tree *tree;
  827. tree = &BTRFS_I(page->mapping->host)->io_tree;
  828. return extent_read_full_page(tree, page, btree_get_extent, 0);
  829. }
  830. static int btree_releasepage(struct page *page, gfp_t gfp_flags)
  831. {
  832. if (PageWriteback(page) || PageDirty(page))
  833. return 0;
  834. return try_release_extent_buffer(page);
  835. }
  836. static void btree_invalidatepage(struct page *page, unsigned int offset,
  837. unsigned int length)
  838. {
  839. struct extent_io_tree *tree;
  840. tree = &BTRFS_I(page->mapping->host)->io_tree;
  841. extent_invalidatepage(tree, page, offset);
  842. btree_releasepage(page, GFP_NOFS);
  843. if (PagePrivate(page)) {
  844. btrfs_warn(BTRFS_I(page->mapping->host)->root->fs_info,
  845. "page private not zero on page %llu",
  846. (unsigned long long)page_offset(page));
  847. ClearPagePrivate(page);
  848. set_page_private(page, 0);
  849. put_page(page);
  850. }
  851. }
  852. static int btree_set_page_dirty(struct page *page)
  853. {
  854. #ifdef DEBUG
  855. struct extent_buffer *eb;
  856. BUG_ON(!PagePrivate(page));
  857. eb = (struct extent_buffer *)page->private;
  858. BUG_ON(!eb);
  859. BUG_ON(!test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
  860. BUG_ON(!atomic_read(&eb->refs));
  861. btrfs_assert_tree_locked(eb);
  862. #endif
  863. return __set_page_dirty_nobuffers(page);
  864. }
  865. static const struct address_space_operations btree_aops = {
  866. .readpage = btree_readpage,
  867. .writepages = btree_writepages,
  868. .releasepage = btree_releasepage,
  869. .invalidatepage = btree_invalidatepage,
  870. #ifdef CONFIG_MIGRATION
  871. .migratepage = btree_migratepage,
  872. #endif
  873. .set_page_dirty = btree_set_page_dirty,
  874. };
  875. void readahead_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr)
  876. {
  877. struct extent_buffer *buf = NULL;
  878. struct inode *btree_inode = fs_info->btree_inode;
  879. int ret;
  880. buf = btrfs_find_create_tree_block(fs_info, bytenr);
  881. if (IS_ERR(buf))
  882. return;
  883. ret = read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree, buf,
  884. WAIT_NONE, 0);
  885. if (ret < 0)
  886. free_extent_buffer_stale(buf);
  887. else
  888. free_extent_buffer(buf);
  889. }
  890. int reada_tree_block_flagged(struct btrfs_fs_info *fs_info, u64 bytenr,
  891. int mirror_num, struct extent_buffer **eb)
  892. {
  893. struct extent_buffer *buf = NULL;
  894. struct inode *btree_inode = fs_info->btree_inode;
  895. struct extent_io_tree *io_tree = &BTRFS_I(btree_inode)->io_tree;
  896. int ret;
  897. buf = btrfs_find_create_tree_block(fs_info, bytenr);
  898. if (IS_ERR(buf))
  899. return 0;
  900. set_bit(EXTENT_BUFFER_READAHEAD, &buf->bflags);
  901. ret = read_extent_buffer_pages(io_tree, buf, WAIT_PAGE_LOCK,
  902. mirror_num);
  903. if (ret) {
  904. free_extent_buffer_stale(buf);
  905. return ret;
  906. }
  907. if (test_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags)) {
  908. free_extent_buffer_stale(buf);
  909. return -EIO;
  910. } else if (extent_buffer_uptodate(buf)) {
  911. *eb = buf;
  912. } else {
  913. free_extent_buffer(buf);
  914. }
  915. return 0;
  916. }
  917. struct extent_buffer *btrfs_find_create_tree_block(
  918. struct btrfs_fs_info *fs_info,
  919. u64 bytenr)
  920. {
  921. if (btrfs_is_testing(fs_info))
  922. return alloc_test_extent_buffer(fs_info, bytenr);
  923. return alloc_extent_buffer(fs_info, bytenr);
  924. }
  925. int btrfs_write_tree_block(struct extent_buffer *buf)
  926. {
  927. return filemap_fdatawrite_range(buf->pages[0]->mapping, buf->start,
  928. buf->start + buf->len - 1);
  929. }
  930. void btrfs_wait_tree_block_writeback(struct extent_buffer *buf)
  931. {
  932. filemap_fdatawait_range(buf->pages[0]->mapping,
  933. buf->start, buf->start + buf->len - 1);
  934. }
  935. /*
  936. * Read tree block at logical address @bytenr and do variant basic but critical
  937. * verification.
  938. *
  939. * @parent_transid: expected transid of this tree block, skip check if 0
  940. * @level: expected level, mandatory check
  941. * @first_key: expected key in slot 0, skip check if NULL
  942. */
  943. struct extent_buffer *read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr,
  944. u64 parent_transid, int level,
  945. struct btrfs_key *first_key)
  946. {
  947. struct extent_buffer *buf = NULL;
  948. int ret;
  949. buf = btrfs_find_create_tree_block(fs_info, bytenr);
  950. if (IS_ERR(buf))
  951. return buf;
  952. ret = btree_read_extent_buffer_pages(fs_info, buf, parent_transid,
  953. level, first_key);
  954. if (ret) {
  955. free_extent_buffer_stale(buf);
  956. return ERR_PTR(ret);
  957. }
  958. return buf;
  959. }
  960. void clean_tree_block(struct btrfs_fs_info *fs_info,
  961. struct extent_buffer *buf)
  962. {
  963. if (btrfs_header_generation(buf) ==
  964. fs_info->running_transaction->transid) {
  965. btrfs_assert_tree_locked(buf);
  966. if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)) {
  967. percpu_counter_add_batch(&fs_info->dirty_metadata_bytes,
  968. -buf->len,
  969. fs_info->dirty_metadata_batch);
  970. /* ugh, clear_extent_buffer_dirty needs to lock the page */
  971. btrfs_set_lock_blocking(buf);
  972. clear_extent_buffer_dirty(buf);
  973. }
  974. }
  975. }
  976. static struct btrfs_subvolume_writers *btrfs_alloc_subvolume_writers(void)
  977. {
  978. struct btrfs_subvolume_writers *writers;
  979. int ret;
  980. writers = kmalloc(sizeof(*writers), GFP_NOFS);
  981. if (!writers)
  982. return ERR_PTR(-ENOMEM);
  983. ret = percpu_counter_init(&writers->counter, 0, GFP_NOFS);
  984. if (ret < 0) {
  985. kfree(writers);
  986. return ERR_PTR(ret);
  987. }
  988. init_waitqueue_head(&writers->wait);
  989. return writers;
  990. }
  991. static void
  992. btrfs_free_subvolume_writers(struct btrfs_subvolume_writers *writers)
  993. {
  994. percpu_counter_destroy(&writers->counter);
  995. kfree(writers);
  996. }
  997. static void __setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info,
  998. u64 objectid)
  999. {
  1000. bool dummy = test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state);
  1001. root->node = NULL;
  1002. root->commit_root = NULL;
  1003. root->state = 0;
  1004. root->orphan_cleanup_state = 0;
  1005. root->objectid = objectid;
  1006. root->last_trans = 0;
  1007. root->highest_objectid = 0;
  1008. root->nr_delalloc_inodes = 0;
  1009. root->nr_ordered_extents = 0;
  1010. root->inode_tree = RB_ROOT;
  1011. INIT_RADIX_TREE(&root->delayed_nodes_tree, GFP_ATOMIC);
  1012. root->block_rsv = NULL;
  1013. INIT_LIST_HEAD(&root->dirty_list);
  1014. INIT_LIST_HEAD(&root->root_list);
  1015. INIT_LIST_HEAD(&root->delalloc_inodes);
  1016. INIT_LIST_HEAD(&root->delalloc_root);
  1017. INIT_LIST_HEAD(&root->ordered_extents);
  1018. INIT_LIST_HEAD(&root->ordered_root);
  1019. INIT_LIST_HEAD(&root->logged_list[0]);
  1020. INIT_LIST_HEAD(&root->logged_list[1]);
  1021. spin_lock_init(&root->inode_lock);
  1022. spin_lock_init(&root->delalloc_lock);
  1023. spin_lock_init(&root->ordered_extent_lock);
  1024. spin_lock_init(&root->accounting_lock);
  1025. spin_lock_init(&root->log_extents_lock[0]);
  1026. spin_lock_init(&root->log_extents_lock[1]);
  1027. spin_lock_init(&root->qgroup_meta_rsv_lock);
  1028. mutex_init(&root->objectid_mutex);
  1029. mutex_init(&root->log_mutex);
  1030. mutex_init(&root->ordered_extent_mutex);
  1031. mutex_init(&root->delalloc_mutex);
  1032. init_waitqueue_head(&root->log_writer_wait);
  1033. init_waitqueue_head(&root->log_commit_wait[0]);
  1034. init_waitqueue_head(&root->log_commit_wait[1]);
  1035. INIT_LIST_HEAD(&root->log_ctxs[0]);
  1036. INIT_LIST_HEAD(&root->log_ctxs[1]);
  1037. atomic_set(&root->log_commit[0], 0);
  1038. atomic_set(&root->log_commit[1], 0);
  1039. atomic_set(&root->log_writers, 0);
  1040. atomic_set(&root->log_batch, 0);
  1041. refcount_set(&root->refs, 1);
  1042. atomic_set(&root->will_be_snapshotted, 0);
  1043. atomic_set(&root->snapshot_force_cow, 0);
  1044. root->log_transid = 0;
  1045. root->log_transid_committed = -1;
  1046. root->last_log_commit = 0;
  1047. if (!dummy)
  1048. extent_io_tree_init(&root->dirty_log_pages, NULL);
  1049. memset(&root->root_key, 0, sizeof(root->root_key));
  1050. memset(&root->root_item, 0, sizeof(root->root_item));
  1051. memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
  1052. if (!dummy)
  1053. root->defrag_trans_start = fs_info->generation;
  1054. else
  1055. root->defrag_trans_start = 0;
  1056. root->root_key.objectid = objectid;
  1057. root->anon_dev = 0;
  1058. spin_lock_init(&root->root_item_lock);
  1059. }
  1060. static struct btrfs_root *btrfs_alloc_root(struct btrfs_fs_info *fs_info,
  1061. gfp_t flags)
  1062. {
  1063. struct btrfs_root *root = kzalloc(sizeof(*root), flags);
  1064. if (root)
  1065. root->fs_info = fs_info;
  1066. return root;
  1067. }
  1068. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  1069. /* Should only be used by the testing infrastructure */
  1070. struct btrfs_root *btrfs_alloc_dummy_root(struct btrfs_fs_info *fs_info)
  1071. {
  1072. struct btrfs_root *root;
  1073. if (!fs_info)
  1074. return ERR_PTR(-EINVAL);
  1075. root = btrfs_alloc_root(fs_info, GFP_KERNEL);
  1076. if (!root)
  1077. return ERR_PTR(-ENOMEM);
  1078. /* We don't use the stripesize in selftest, set it as sectorsize */
  1079. __setup_root(root, fs_info, BTRFS_ROOT_TREE_OBJECTID);
  1080. root->alloc_bytenr = 0;
  1081. return root;
  1082. }
  1083. #endif
  1084. struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans,
  1085. struct btrfs_fs_info *fs_info,
  1086. u64 objectid)
  1087. {
  1088. struct extent_buffer *leaf;
  1089. struct btrfs_root *tree_root = fs_info->tree_root;
  1090. struct btrfs_root *root;
  1091. struct btrfs_key key;
  1092. unsigned int nofs_flag;
  1093. int ret = 0;
  1094. uuid_le uuid = NULL_UUID_LE;
  1095. /*
  1096. * We're holding a transaction handle, so use a NOFS memory allocation
  1097. * context to avoid deadlock if reclaim happens.
  1098. */
  1099. nofs_flag = memalloc_nofs_save();
  1100. root = btrfs_alloc_root(fs_info, GFP_KERNEL);
  1101. memalloc_nofs_restore(nofs_flag);
  1102. if (!root)
  1103. return ERR_PTR(-ENOMEM);
  1104. __setup_root(root, fs_info, objectid);
  1105. root->root_key.objectid = objectid;
  1106. root->root_key.type = BTRFS_ROOT_ITEM_KEY;
  1107. root->root_key.offset = 0;
  1108. leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0);
  1109. if (IS_ERR(leaf)) {
  1110. ret = PTR_ERR(leaf);
  1111. leaf = NULL;
  1112. goto fail;
  1113. }
  1114. root->node = leaf;
  1115. btrfs_mark_buffer_dirty(leaf);
  1116. root->commit_root = btrfs_root_node(root);
  1117. set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
  1118. root->root_item.flags = 0;
  1119. root->root_item.byte_limit = 0;
  1120. btrfs_set_root_bytenr(&root->root_item, leaf->start);
  1121. btrfs_set_root_generation(&root->root_item, trans->transid);
  1122. btrfs_set_root_level(&root->root_item, 0);
  1123. btrfs_set_root_refs(&root->root_item, 1);
  1124. btrfs_set_root_used(&root->root_item, leaf->len);
  1125. btrfs_set_root_last_snapshot(&root->root_item, 0);
  1126. btrfs_set_root_dirid(&root->root_item, 0);
  1127. if (is_fstree(objectid))
  1128. uuid_le_gen(&uuid);
  1129. memcpy(root->root_item.uuid, uuid.b, BTRFS_UUID_SIZE);
  1130. root->root_item.drop_level = 0;
  1131. key.objectid = objectid;
  1132. key.type = BTRFS_ROOT_ITEM_KEY;
  1133. key.offset = 0;
  1134. ret = btrfs_insert_root(trans, tree_root, &key, &root->root_item);
  1135. if (ret)
  1136. goto fail;
  1137. btrfs_tree_unlock(leaf);
  1138. return root;
  1139. fail:
  1140. if (leaf) {
  1141. btrfs_tree_unlock(leaf);
  1142. free_extent_buffer(root->commit_root);
  1143. free_extent_buffer(leaf);
  1144. }
  1145. kfree(root);
  1146. return ERR_PTR(ret);
  1147. }
  1148. static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans,
  1149. struct btrfs_fs_info *fs_info)
  1150. {
  1151. struct btrfs_root *root;
  1152. struct extent_buffer *leaf;
  1153. root = btrfs_alloc_root(fs_info, GFP_NOFS);
  1154. if (!root)
  1155. return ERR_PTR(-ENOMEM);
  1156. __setup_root(root, fs_info, BTRFS_TREE_LOG_OBJECTID);
  1157. root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID;
  1158. root->root_key.type = BTRFS_ROOT_ITEM_KEY;
  1159. root->root_key.offset = BTRFS_TREE_LOG_OBJECTID;
  1160. /*
  1161. * DON'T set REF_COWS for log trees
  1162. *
  1163. * log trees do not get reference counted because they go away
  1164. * before a real commit is actually done. They do store pointers
  1165. * to file data extents, and those reference counts still get
  1166. * updated (along with back refs to the log tree).
  1167. */
  1168. leaf = btrfs_alloc_tree_block(trans, root, 0, BTRFS_TREE_LOG_OBJECTID,
  1169. NULL, 0, 0, 0);
  1170. if (IS_ERR(leaf)) {
  1171. kfree(root);
  1172. return ERR_CAST(leaf);
  1173. }
  1174. root->node = leaf;
  1175. btrfs_mark_buffer_dirty(root->node);
  1176. btrfs_tree_unlock(root->node);
  1177. return root;
  1178. }
  1179. int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans,
  1180. struct btrfs_fs_info *fs_info)
  1181. {
  1182. struct btrfs_root *log_root;
  1183. log_root = alloc_log_tree(trans, fs_info);
  1184. if (IS_ERR(log_root))
  1185. return PTR_ERR(log_root);
  1186. WARN_ON(fs_info->log_root_tree);
  1187. fs_info->log_root_tree = log_root;
  1188. return 0;
  1189. }
  1190. int btrfs_add_log_tree(struct btrfs_trans_handle *trans,
  1191. struct btrfs_root *root)
  1192. {
  1193. struct btrfs_fs_info *fs_info = root->fs_info;
  1194. struct btrfs_root *log_root;
  1195. struct btrfs_inode_item *inode_item;
  1196. log_root = alloc_log_tree(trans, fs_info);
  1197. if (IS_ERR(log_root))
  1198. return PTR_ERR(log_root);
  1199. log_root->last_trans = trans->transid;
  1200. log_root->root_key.offset = root->root_key.objectid;
  1201. inode_item = &log_root->root_item.inode;
  1202. btrfs_set_stack_inode_generation(inode_item, 1);
  1203. btrfs_set_stack_inode_size(inode_item, 3);
  1204. btrfs_set_stack_inode_nlink(inode_item, 1);
  1205. btrfs_set_stack_inode_nbytes(inode_item,
  1206. fs_info->nodesize);
  1207. btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
  1208. btrfs_set_root_node(&log_root->root_item, log_root->node);
  1209. WARN_ON(root->log_root);
  1210. root->log_root = log_root;
  1211. root->log_transid = 0;
  1212. root->log_transid_committed = -1;
  1213. root->last_log_commit = 0;
  1214. return 0;
  1215. }
  1216. static struct btrfs_root *btrfs_read_tree_root(struct btrfs_root *tree_root,
  1217. struct btrfs_key *key)
  1218. {
  1219. struct btrfs_root *root;
  1220. struct btrfs_fs_info *fs_info = tree_root->fs_info;
  1221. struct btrfs_path *path;
  1222. u64 generation;
  1223. int ret;
  1224. int level;
  1225. path = btrfs_alloc_path();
  1226. if (!path)
  1227. return ERR_PTR(-ENOMEM);
  1228. root = btrfs_alloc_root(fs_info, GFP_NOFS);
  1229. if (!root) {
  1230. ret = -ENOMEM;
  1231. goto alloc_fail;
  1232. }
  1233. __setup_root(root, fs_info, key->objectid);
  1234. ret = btrfs_find_root(tree_root, key, path,
  1235. &root->root_item, &root->root_key);
  1236. if (ret) {
  1237. if (ret > 0)
  1238. ret = -ENOENT;
  1239. goto find_fail;
  1240. }
  1241. generation = btrfs_root_generation(&root->root_item);
  1242. level = btrfs_root_level(&root->root_item);
  1243. root->node = read_tree_block(fs_info,
  1244. btrfs_root_bytenr(&root->root_item),
  1245. generation, level, NULL);
  1246. if (IS_ERR(root->node)) {
  1247. ret = PTR_ERR(root->node);
  1248. goto find_fail;
  1249. } else if (!btrfs_buffer_uptodate(root->node, generation, 0)) {
  1250. ret = -EIO;
  1251. free_extent_buffer(root->node);
  1252. goto find_fail;
  1253. }
  1254. root->commit_root = btrfs_root_node(root);
  1255. out:
  1256. btrfs_free_path(path);
  1257. return root;
  1258. find_fail:
  1259. kfree(root);
  1260. alloc_fail:
  1261. root = ERR_PTR(ret);
  1262. goto out;
  1263. }
  1264. struct btrfs_root *btrfs_read_fs_root(struct btrfs_root *tree_root,
  1265. struct btrfs_key *location)
  1266. {
  1267. struct btrfs_root *root;
  1268. root = btrfs_read_tree_root(tree_root, location);
  1269. if (IS_ERR(root))
  1270. return root;
  1271. if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
  1272. set_bit(BTRFS_ROOT_REF_COWS, &root->state);
  1273. btrfs_check_and_init_root_item(&root->root_item);
  1274. }
  1275. return root;
  1276. }
  1277. int btrfs_init_fs_root(struct btrfs_root *root)
  1278. {
  1279. int ret;
  1280. struct btrfs_subvolume_writers *writers;
  1281. root->free_ino_ctl = kzalloc(sizeof(*root->free_ino_ctl), GFP_NOFS);
  1282. root->free_ino_pinned = kzalloc(sizeof(*root->free_ino_pinned),
  1283. GFP_NOFS);
  1284. if (!root->free_ino_pinned || !root->free_ino_ctl) {
  1285. ret = -ENOMEM;
  1286. goto fail;
  1287. }
  1288. writers = btrfs_alloc_subvolume_writers();
  1289. if (IS_ERR(writers)) {
  1290. ret = PTR_ERR(writers);
  1291. goto fail;
  1292. }
  1293. root->subv_writers = writers;
  1294. btrfs_init_free_ino_ctl(root);
  1295. spin_lock_init(&root->ino_cache_lock);
  1296. init_waitqueue_head(&root->ino_cache_wait);
  1297. /*
  1298. * Don't assign anonymous block device to roots that are not exposed to
  1299. * userspace, the id pool is limited to 1M
  1300. */
  1301. if (is_fstree(root->root_key.objectid) &&
  1302. btrfs_root_refs(&root->root_item) > 0) {
  1303. ret = get_anon_bdev(&root->anon_dev);
  1304. if (ret)
  1305. goto fail;
  1306. }
  1307. mutex_lock(&root->objectid_mutex);
  1308. ret = btrfs_find_highest_objectid(root,
  1309. &root->highest_objectid);
  1310. if (ret) {
  1311. mutex_unlock(&root->objectid_mutex);
  1312. goto fail;
  1313. }
  1314. ASSERT(root->highest_objectid <= BTRFS_LAST_FREE_OBJECTID);
  1315. mutex_unlock(&root->objectid_mutex);
  1316. return 0;
  1317. fail:
  1318. /* The caller is responsible to call btrfs_free_fs_root */
  1319. return ret;
  1320. }
  1321. struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info,
  1322. u64 root_id)
  1323. {
  1324. struct btrfs_root *root;
  1325. spin_lock(&fs_info->fs_roots_radix_lock);
  1326. root = radix_tree_lookup(&fs_info->fs_roots_radix,
  1327. (unsigned long)root_id);
  1328. spin_unlock(&fs_info->fs_roots_radix_lock);
  1329. return root;
  1330. }
  1331. int btrfs_insert_fs_root(struct btrfs_fs_info *fs_info,
  1332. struct btrfs_root *root)
  1333. {
  1334. int ret;
  1335. ret = radix_tree_preload(GFP_NOFS);
  1336. if (ret)
  1337. return ret;
  1338. spin_lock(&fs_info->fs_roots_radix_lock);
  1339. ret = radix_tree_insert(&fs_info->fs_roots_radix,
  1340. (unsigned long)root->root_key.objectid,
  1341. root);
  1342. if (ret == 0)
  1343. set_bit(BTRFS_ROOT_IN_RADIX, &root->state);
  1344. spin_unlock(&fs_info->fs_roots_radix_lock);
  1345. radix_tree_preload_end();
  1346. return ret;
  1347. }
  1348. struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info,
  1349. struct btrfs_key *location,
  1350. bool check_ref)
  1351. {
  1352. struct btrfs_root *root;
  1353. struct btrfs_path *path;
  1354. struct btrfs_key key;
  1355. int ret;
  1356. if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
  1357. return fs_info->tree_root;
  1358. if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
  1359. return fs_info->extent_root;
  1360. if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
  1361. return fs_info->chunk_root;
  1362. if (location->objectid == BTRFS_DEV_TREE_OBJECTID)
  1363. return fs_info->dev_root;
  1364. if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
  1365. return fs_info->csum_root;
  1366. if (location->objectid == BTRFS_QUOTA_TREE_OBJECTID)
  1367. return fs_info->quota_root ? fs_info->quota_root :
  1368. ERR_PTR(-ENOENT);
  1369. if (location->objectid == BTRFS_UUID_TREE_OBJECTID)
  1370. return fs_info->uuid_root ? fs_info->uuid_root :
  1371. ERR_PTR(-ENOENT);
  1372. if (location->objectid == BTRFS_FREE_SPACE_TREE_OBJECTID)
  1373. return fs_info->free_space_root ? fs_info->free_space_root :
  1374. ERR_PTR(-ENOENT);
  1375. again:
  1376. root = btrfs_lookup_fs_root(fs_info, location->objectid);
  1377. if (root) {
  1378. if (check_ref && btrfs_root_refs(&root->root_item) == 0)
  1379. return ERR_PTR(-ENOENT);
  1380. return root;
  1381. }
  1382. root = btrfs_read_fs_root(fs_info->tree_root, location);
  1383. if (IS_ERR(root))
  1384. return root;
  1385. if (check_ref && btrfs_root_refs(&root->root_item) == 0) {
  1386. ret = -ENOENT;
  1387. goto fail;
  1388. }
  1389. ret = btrfs_init_fs_root(root);
  1390. if (ret)
  1391. goto fail;
  1392. path = btrfs_alloc_path();
  1393. if (!path) {
  1394. ret = -ENOMEM;
  1395. goto fail;
  1396. }
  1397. key.objectid = BTRFS_ORPHAN_OBJECTID;
  1398. key.type = BTRFS_ORPHAN_ITEM_KEY;
  1399. key.offset = location->objectid;
  1400. ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
  1401. btrfs_free_path(path);
  1402. if (ret < 0)
  1403. goto fail;
  1404. if (ret == 0)
  1405. set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state);
  1406. ret = btrfs_insert_fs_root(fs_info, root);
  1407. if (ret) {
  1408. if (ret == -EEXIST) {
  1409. btrfs_free_fs_root(root);
  1410. goto again;
  1411. }
  1412. goto fail;
  1413. }
  1414. return root;
  1415. fail:
  1416. btrfs_free_fs_root(root);
  1417. return ERR_PTR(ret);
  1418. }
  1419. static int btrfs_congested_fn(void *congested_data, int bdi_bits)
  1420. {
  1421. struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data;
  1422. int ret = 0;
  1423. struct btrfs_device *device;
  1424. struct backing_dev_info *bdi;
  1425. rcu_read_lock();
  1426. list_for_each_entry_rcu(device, &info->fs_devices->devices, dev_list) {
  1427. if (!device->bdev)
  1428. continue;
  1429. bdi = device->bdev->bd_bdi;
  1430. if (bdi_congested(bdi, bdi_bits)) {
  1431. ret = 1;
  1432. break;
  1433. }
  1434. }
  1435. rcu_read_unlock();
  1436. return ret;
  1437. }
  1438. /*
  1439. * called by the kthread helper functions to finally call the bio end_io
  1440. * functions. This is where read checksum verification actually happens
  1441. */
  1442. static void end_workqueue_fn(struct btrfs_work *work)
  1443. {
  1444. struct bio *bio;
  1445. struct btrfs_end_io_wq *end_io_wq;
  1446. end_io_wq = container_of(work, struct btrfs_end_io_wq, work);
  1447. bio = end_io_wq->bio;
  1448. bio->bi_status = end_io_wq->status;
  1449. bio->bi_private = end_io_wq->private;
  1450. bio->bi_end_io = end_io_wq->end_io;
  1451. bio_endio(bio);
  1452. kmem_cache_free(btrfs_end_io_wq_cache, end_io_wq);
  1453. }
  1454. static int cleaner_kthread(void *arg)
  1455. {
  1456. struct btrfs_root *root = arg;
  1457. struct btrfs_fs_info *fs_info = root->fs_info;
  1458. int again;
  1459. while (1) {
  1460. again = 0;
  1461. /* Make the cleaner go to sleep early. */
  1462. if (btrfs_need_cleaner_sleep(fs_info))
  1463. goto sleep;
  1464. /*
  1465. * Do not do anything if we might cause open_ctree() to block
  1466. * before we have finished mounting the filesystem.
  1467. */
  1468. if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags))
  1469. goto sleep;
  1470. if (!mutex_trylock(&fs_info->cleaner_mutex))
  1471. goto sleep;
  1472. /*
  1473. * Avoid the problem that we change the status of the fs
  1474. * during the above check and trylock.
  1475. */
  1476. if (btrfs_need_cleaner_sleep(fs_info)) {
  1477. mutex_unlock(&fs_info->cleaner_mutex);
  1478. goto sleep;
  1479. }
  1480. mutex_lock(&fs_info->cleaner_delayed_iput_mutex);
  1481. btrfs_run_delayed_iputs(fs_info);
  1482. mutex_unlock(&fs_info->cleaner_delayed_iput_mutex);
  1483. again = btrfs_clean_one_deleted_snapshot(root);
  1484. mutex_unlock(&fs_info->cleaner_mutex);
  1485. /*
  1486. * The defragger has dealt with the R/O remount and umount,
  1487. * needn't do anything special here.
  1488. */
  1489. btrfs_run_defrag_inodes(fs_info);
  1490. /*
  1491. * Acquires fs_info->delete_unused_bgs_mutex to avoid racing
  1492. * with relocation (btrfs_relocate_chunk) and relocation
  1493. * acquires fs_info->cleaner_mutex (btrfs_relocate_block_group)
  1494. * after acquiring fs_info->delete_unused_bgs_mutex. So we
  1495. * can't hold, nor need to, fs_info->cleaner_mutex when deleting
  1496. * unused block groups.
  1497. */
  1498. btrfs_delete_unused_bgs(fs_info);
  1499. sleep:
  1500. if (kthread_should_park())
  1501. kthread_parkme();
  1502. if (kthread_should_stop())
  1503. return 0;
  1504. if (!again) {
  1505. set_current_state(TASK_INTERRUPTIBLE);
  1506. schedule();
  1507. __set_current_state(TASK_RUNNING);
  1508. }
  1509. }
  1510. }
  1511. static int transaction_kthread(void *arg)
  1512. {
  1513. struct btrfs_root *root = arg;
  1514. struct btrfs_fs_info *fs_info = root->fs_info;
  1515. struct btrfs_trans_handle *trans;
  1516. struct btrfs_transaction *cur;
  1517. u64 transid;
  1518. time64_t now;
  1519. unsigned long delay;
  1520. bool cannot_commit;
  1521. do {
  1522. cannot_commit = false;
  1523. delay = HZ * fs_info->commit_interval;
  1524. mutex_lock(&fs_info->transaction_kthread_mutex);
  1525. spin_lock(&fs_info->trans_lock);
  1526. cur = fs_info->running_transaction;
  1527. if (!cur) {
  1528. spin_unlock(&fs_info->trans_lock);
  1529. goto sleep;
  1530. }
  1531. now = ktime_get_seconds();
  1532. if (cur->state < TRANS_STATE_BLOCKED &&
  1533. !test_bit(BTRFS_FS_NEED_ASYNC_COMMIT, &fs_info->flags) &&
  1534. (now < cur->start_time ||
  1535. now - cur->start_time < fs_info->commit_interval)) {
  1536. spin_unlock(&fs_info->trans_lock);
  1537. delay = HZ * 5;
  1538. goto sleep;
  1539. }
  1540. transid = cur->transid;
  1541. spin_unlock(&fs_info->trans_lock);
  1542. /* If the file system is aborted, this will always fail. */
  1543. trans = btrfs_attach_transaction(root);
  1544. if (IS_ERR(trans)) {
  1545. if (PTR_ERR(trans) != -ENOENT)
  1546. cannot_commit = true;
  1547. goto sleep;
  1548. }
  1549. if (transid == trans->transid) {
  1550. btrfs_commit_transaction(trans);
  1551. } else {
  1552. btrfs_end_transaction(trans);
  1553. }
  1554. sleep:
  1555. wake_up_process(fs_info->cleaner_kthread);
  1556. mutex_unlock(&fs_info->transaction_kthread_mutex);
  1557. if (unlikely(test_bit(BTRFS_FS_STATE_ERROR,
  1558. &fs_info->fs_state)))
  1559. btrfs_cleanup_transaction(fs_info);
  1560. if (!kthread_should_stop() &&
  1561. (!btrfs_transaction_blocked(fs_info) ||
  1562. cannot_commit))
  1563. schedule_timeout_interruptible(delay);
  1564. } while (!kthread_should_stop());
  1565. return 0;
  1566. }
  1567. /*
  1568. * this will find the highest generation in the array of
  1569. * root backups. The index of the highest array is returned,
  1570. * or -1 if we can't find anything.
  1571. *
  1572. * We check to make sure the array is valid by comparing the
  1573. * generation of the latest root in the array with the generation
  1574. * in the super block. If they don't match we pitch it.
  1575. */
  1576. static int find_newest_super_backup(struct btrfs_fs_info *info, u64 newest_gen)
  1577. {
  1578. u64 cur;
  1579. int newest_index = -1;
  1580. struct btrfs_root_backup *root_backup;
  1581. int i;
  1582. for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) {
  1583. root_backup = info->super_copy->super_roots + i;
  1584. cur = btrfs_backup_tree_root_gen(root_backup);
  1585. if (cur == newest_gen)
  1586. newest_index = i;
  1587. }
  1588. /* check to see if we actually wrapped around */
  1589. if (newest_index == BTRFS_NUM_BACKUP_ROOTS - 1) {
  1590. root_backup = info->super_copy->super_roots;
  1591. cur = btrfs_backup_tree_root_gen(root_backup);
  1592. if (cur == newest_gen)
  1593. newest_index = 0;
  1594. }
  1595. return newest_index;
  1596. }
  1597. /*
  1598. * find the oldest backup so we know where to store new entries
  1599. * in the backup array. This will set the backup_root_index
  1600. * field in the fs_info struct
  1601. */
  1602. static void find_oldest_super_backup(struct btrfs_fs_info *info,
  1603. u64 newest_gen)
  1604. {
  1605. int newest_index = -1;
  1606. newest_index = find_newest_super_backup(info, newest_gen);
  1607. /* if there was garbage in there, just move along */
  1608. if (newest_index == -1) {
  1609. info->backup_root_index = 0;
  1610. } else {
  1611. info->backup_root_index = (newest_index + 1) % BTRFS_NUM_BACKUP_ROOTS;
  1612. }
  1613. }
  1614. /*
  1615. * copy all the root pointers into the super backup array.
  1616. * this will bump the backup pointer by one when it is
  1617. * done
  1618. */
  1619. static void backup_super_roots(struct btrfs_fs_info *info)
  1620. {
  1621. int next_backup;
  1622. struct btrfs_root_backup *root_backup;
  1623. int last_backup;
  1624. next_backup = info->backup_root_index;
  1625. last_backup = (next_backup + BTRFS_NUM_BACKUP_ROOTS - 1) %
  1626. BTRFS_NUM_BACKUP_ROOTS;
  1627. /*
  1628. * just overwrite the last backup if we're at the same generation
  1629. * this happens only at umount
  1630. */
  1631. root_backup = info->super_for_commit->super_roots + last_backup;
  1632. if (btrfs_backup_tree_root_gen(root_backup) ==
  1633. btrfs_header_generation(info->tree_root->node))
  1634. next_backup = last_backup;
  1635. root_backup = info->super_for_commit->super_roots + next_backup;
  1636. /*
  1637. * make sure all of our padding and empty slots get zero filled
  1638. * regardless of which ones we use today
  1639. */
  1640. memset(root_backup, 0, sizeof(*root_backup));
  1641. info->backup_root_index = (next_backup + 1) % BTRFS_NUM_BACKUP_ROOTS;
  1642. btrfs_set_backup_tree_root(root_backup, info->tree_root->node->start);
  1643. btrfs_set_backup_tree_root_gen(root_backup,
  1644. btrfs_header_generation(info->tree_root->node));
  1645. btrfs_set_backup_tree_root_level(root_backup,
  1646. btrfs_header_level(info->tree_root->node));
  1647. btrfs_set_backup_chunk_root(root_backup, info->chunk_root->node->start);
  1648. btrfs_set_backup_chunk_root_gen(root_backup,
  1649. btrfs_header_generation(info->chunk_root->node));
  1650. btrfs_set_backup_chunk_root_level(root_backup,
  1651. btrfs_header_level(info->chunk_root->node));
  1652. btrfs_set_backup_extent_root(root_backup, info->extent_root->node->start);
  1653. btrfs_set_backup_extent_root_gen(root_backup,
  1654. btrfs_header_generation(info->extent_root->node));
  1655. btrfs_set_backup_extent_root_level(root_backup,
  1656. btrfs_header_level(info->extent_root->node));
  1657. /*
  1658. * we might commit during log recovery, which happens before we set
  1659. * the fs_root. Make sure it is valid before we fill it in.
  1660. */
  1661. if (info->fs_root && info->fs_root->node) {
  1662. btrfs_set_backup_fs_root(root_backup,
  1663. info->fs_root->node->start);
  1664. btrfs_set_backup_fs_root_gen(root_backup,
  1665. btrfs_header_generation(info->fs_root->node));
  1666. btrfs_set_backup_fs_root_level(root_backup,
  1667. btrfs_header_level(info->fs_root->node));
  1668. }
  1669. btrfs_set_backup_dev_root(root_backup, info->dev_root->node->start);
  1670. btrfs_set_backup_dev_root_gen(root_backup,
  1671. btrfs_header_generation(info->dev_root->node));
  1672. btrfs_set_backup_dev_root_level(root_backup,
  1673. btrfs_header_level(info->dev_root->node));
  1674. btrfs_set_backup_csum_root(root_backup, info->csum_root->node->start);
  1675. btrfs_set_backup_csum_root_gen(root_backup,
  1676. btrfs_header_generation(info->csum_root->node));
  1677. btrfs_set_backup_csum_root_level(root_backup,
  1678. btrfs_header_level(info->csum_root->node));
  1679. btrfs_set_backup_total_bytes(root_backup,
  1680. btrfs_super_total_bytes(info->super_copy));
  1681. btrfs_set_backup_bytes_used(root_backup,
  1682. btrfs_super_bytes_used(info->super_copy));
  1683. btrfs_set_backup_num_devices(root_backup,
  1684. btrfs_super_num_devices(info->super_copy));
  1685. /*
  1686. * if we don't copy this out to the super_copy, it won't get remembered
  1687. * for the next commit
  1688. */
  1689. memcpy(&info->super_copy->super_roots,
  1690. &info->super_for_commit->super_roots,
  1691. sizeof(*root_backup) * BTRFS_NUM_BACKUP_ROOTS);
  1692. }
  1693. /*
  1694. * this copies info out of the root backup array and back into
  1695. * the in-memory super block. It is meant to help iterate through
  1696. * the array, so you send it the number of backups you've already
  1697. * tried and the last backup index you used.
  1698. *
  1699. * this returns -1 when it has tried all the backups
  1700. */
  1701. static noinline int next_root_backup(struct btrfs_fs_info *info,
  1702. struct btrfs_super_block *super,
  1703. int *num_backups_tried, int *backup_index)
  1704. {
  1705. struct btrfs_root_backup *root_backup;
  1706. int newest = *backup_index;
  1707. if (*num_backups_tried == 0) {
  1708. u64 gen = btrfs_super_generation(super);
  1709. newest = find_newest_super_backup(info, gen);
  1710. if (newest == -1)
  1711. return -1;
  1712. *backup_index = newest;
  1713. *num_backups_tried = 1;
  1714. } else if (*num_backups_tried == BTRFS_NUM_BACKUP_ROOTS) {
  1715. /* we've tried all the backups, all done */
  1716. return -1;
  1717. } else {
  1718. /* jump to the next oldest backup */
  1719. newest = (*backup_index + BTRFS_NUM_BACKUP_ROOTS - 1) %
  1720. BTRFS_NUM_BACKUP_ROOTS;
  1721. *backup_index = newest;
  1722. *num_backups_tried += 1;
  1723. }
  1724. root_backup = super->super_roots + newest;
  1725. btrfs_set_super_generation(super,
  1726. btrfs_backup_tree_root_gen(root_backup));
  1727. btrfs_set_super_root(super, btrfs_backup_tree_root(root_backup));
  1728. btrfs_set_super_root_level(super,
  1729. btrfs_backup_tree_root_level(root_backup));
  1730. btrfs_set_super_bytes_used(super, btrfs_backup_bytes_used(root_backup));
  1731. /*
  1732. * fixme: the total bytes and num_devices need to match or we should
  1733. * need a fsck
  1734. */
  1735. btrfs_set_super_total_bytes(super, btrfs_backup_total_bytes(root_backup));
  1736. btrfs_set_super_num_devices(super, btrfs_backup_num_devices(root_backup));
  1737. return 0;
  1738. }
  1739. /* helper to cleanup workers */
  1740. static void btrfs_stop_all_workers(struct btrfs_fs_info *fs_info)
  1741. {
  1742. btrfs_destroy_workqueue(fs_info->fixup_workers);
  1743. btrfs_destroy_workqueue(fs_info->delalloc_workers);
  1744. btrfs_destroy_workqueue(fs_info->workers);
  1745. btrfs_destroy_workqueue(fs_info->endio_workers);
  1746. btrfs_destroy_workqueue(fs_info->endio_raid56_workers);
  1747. btrfs_destroy_workqueue(fs_info->endio_repair_workers);
  1748. btrfs_destroy_workqueue(fs_info->rmw_workers);
  1749. btrfs_destroy_workqueue(fs_info->endio_write_workers);
  1750. btrfs_destroy_workqueue(fs_info->endio_freespace_worker);
  1751. btrfs_destroy_workqueue(fs_info->submit_workers);
  1752. btrfs_destroy_workqueue(fs_info->delayed_workers);
  1753. btrfs_destroy_workqueue(fs_info->caching_workers);
  1754. btrfs_destroy_workqueue(fs_info->readahead_workers);
  1755. btrfs_destroy_workqueue(fs_info->flush_workers);
  1756. btrfs_destroy_workqueue(fs_info->qgroup_rescan_workers);
  1757. btrfs_destroy_workqueue(fs_info->extent_workers);
  1758. /*
  1759. * Now that all other work queues are destroyed, we can safely destroy
  1760. * the queues used for metadata I/O, since tasks from those other work
  1761. * queues can do metadata I/O operations.
  1762. */
  1763. btrfs_destroy_workqueue(fs_info->endio_meta_workers);
  1764. btrfs_destroy_workqueue(fs_info->endio_meta_write_workers);
  1765. }
  1766. static void free_root_extent_buffers(struct btrfs_root *root)
  1767. {
  1768. if (root) {
  1769. free_extent_buffer(root->node);
  1770. free_extent_buffer(root->commit_root);
  1771. root->node = NULL;
  1772. root->commit_root = NULL;
  1773. }
  1774. }
  1775. /* helper to cleanup tree roots */
  1776. static void free_root_pointers(struct btrfs_fs_info *info, bool free_chunk_root)
  1777. {
  1778. free_root_extent_buffers(info->tree_root);
  1779. free_root_extent_buffers(info->dev_root);
  1780. free_root_extent_buffers(info->extent_root);
  1781. free_root_extent_buffers(info->csum_root);
  1782. free_root_extent_buffers(info->quota_root);
  1783. free_root_extent_buffers(info->uuid_root);
  1784. if (free_chunk_root)
  1785. free_root_extent_buffers(info->chunk_root);
  1786. free_root_extent_buffers(info->free_space_root);
  1787. }
  1788. void btrfs_free_fs_roots(struct btrfs_fs_info *fs_info)
  1789. {
  1790. int ret;
  1791. struct btrfs_root *gang[8];
  1792. int i;
  1793. while (!list_empty(&fs_info->dead_roots)) {
  1794. gang[0] = list_entry(fs_info->dead_roots.next,
  1795. struct btrfs_root, root_list);
  1796. list_del(&gang[0]->root_list);
  1797. if (test_bit(BTRFS_ROOT_IN_RADIX, &gang[0]->state)) {
  1798. btrfs_drop_and_free_fs_root(fs_info, gang[0]);
  1799. } else {
  1800. free_extent_buffer(gang[0]->node);
  1801. free_extent_buffer(gang[0]->commit_root);
  1802. btrfs_put_fs_root(gang[0]);
  1803. }
  1804. }
  1805. while (1) {
  1806. ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
  1807. (void **)gang, 0,
  1808. ARRAY_SIZE(gang));
  1809. if (!ret)
  1810. break;
  1811. for (i = 0; i < ret; i++)
  1812. btrfs_drop_and_free_fs_root(fs_info, gang[i]);
  1813. }
  1814. if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
  1815. btrfs_free_log_root_tree(NULL, fs_info);
  1816. btrfs_destroy_pinned_extent(fs_info, fs_info->pinned_extents);
  1817. }
  1818. }
  1819. static void btrfs_init_scrub(struct btrfs_fs_info *fs_info)
  1820. {
  1821. mutex_init(&fs_info->scrub_lock);
  1822. atomic_set(&fs_info->scrubs_running, 0);
  1823. atomic_set(&fs_info->scrub_pause_req, 0);
  1824. atomic_set(&fs_info->scrubs_paused, 0);
  1825. atomic_set(&fs_info->scrub_cancel_req, 0);
  1826. init_waitqueue_head(&fs_info->scrub_pause_wait);
  1827. fs_info->scrub_workers_refcnt = 0;
  1828. }
  1829. static void btrfs_init_balance(struct btrfs_fs_info *fs_info)
  1830. {
  1831. spin_lock_init(&fs_info->balance_lock);
  1832. mutex_init(&fs_info->balance_mutex);
  1833. atomic_set(&fs_info->balance_pause_req, 0);
  1834. atomic_set(&fs_info->balance_cancel_req, 0);
  1835. fs_info->balance_ctl = NULL;
  1836. init_waitqueue_head(&fs_info->balance_wait_q);
  1837. }
  1838. static void btrfs_init_btree_inode(struct btrfs_fs_info *fs_info)
  1839. {
  1840. struct inode *inode = fs_info->btree_inode;
  1841. inode->i_ino = BTRFS_BTREE_INODE_OBJECTID;
  1842. set_nlink(inode, 1);
  1843. /*
  1844. * we set the i_size on the btree inode to the max possible int.
  1845. * the real end of the address space is determined by all of
  1846. * the devices in the system
  1847. */
  1848. inode->i_size = OFFSET_MAX;
  1849. inode->i_mapping->a_ops = &btree_aops;
  1850. RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
  1851. extent_io_tree_init(&BTRFS_I(inode)->io_tree, inode);
  1852. BTRFS_I(inode)->io_tree.track_uptodate = 0;
  1853. extent_map_tree_init(&BTRFS_I(inode)->extent_tree);
  1854. BTRFS_I(inode)->io_tree.ops = &btree_extent_io_ops;
  1855. BTRFS_I(inode)->root = fs_info->tree_root;
  1856. memset(&BTRFS_I(inode)->location, 0, sizeof(struct btrfs_key));
  1857. set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
  1858. btrfs_insert_inode_hash(inode);
  1859. }
  1860. static void btrfs_init_dev_replace_locks(struct btrfs_fs_info *fs_info)
  1861. {
  1862. mutex_init(&fs_info->dev_replace.lock_finishing_cancel_unmount);
  1863. rwlock_init(&fs_info->dev_replace.lock);
  1864. atomic_set(&fs_info->dev_replace.read_locks, 0);
  1865. atomic_set(&fs_info->dev_replace.blocking_readers, 0);
  1866. init_waitqueue_head(&fs_info->replace_wait);
  1867. init_waitqueue_head(&fs_info->dev_replace.read_lock_wq);
  1868. }
  1869. static void btrfs_init_qgroup(struct btrfs_fs_info *fs_info)
  1870. {
  1871. spin_lock_init(&fs_info->qgroup_lock);
  1872. mutex_init(&fs_info->qgroup_ioctl_lock);
  1873. fs_info->qgroup_tree = RB_ROOT;
  1874. fs_info->qgroup_op_tree = RB_ROOT;
  1875. INIT_LIST_HEAD(&fs_info->dirty_qgroups);
  1876. fs_info->qgroup_seq = 1;
  1877. fs_info->qgroup_ulist = NULL;
  1878. fs_info->qgroup_rescan_running = false;
  1879. mutex_init(&fs_info->qgroup_rescan_lock);
  1880. }
  1881. static int btrfs_init_workqueues(struct btrfs_fs_info *fs_info,
  1882. struct btrfs_fs_devices *fs_devices)
  1883. {
  1884. u32 max_active = fs_info->thread_pool_size;
  1885. unsigned int flags = WQ_MEM_RECLAIM | WQ_FREEZABLE | WQ_UNBOUND;
  1886. fs_info->workers =
  1887. btrfs_alloc_workqueue(fs_info, "worker",
  1888. flags | WQ_HIGHPRI, max_active, 16);
  1889. fs_info->delalloc_workers =
  1890. btrfs_alloc_workqueue(fs_info, "delalloc",
  1891. flags, max_active, 2);
  1892. fs_info->flush_workers =
  1893. btrfs_alloc_workqueue(fs_info, "flush_delalloc",
  1894. flags, max_active, 0);
  1895. fs_info->caching_workers =
  1896. btrfs_alloc_workqueue(fs_info, "cache", flags, max_active, 0);
  1897. /*
  1898. * a higher idle thresh on the submit workers makes it much more
  1899. * likely that bios will be send down in a sane order to the
  1900. * devices
  1901. */
  1902. fs_info->submit_workers =
  1903. btrfs_alloc_workqueue(fs_info, "submit", flags,
  1904. min_t(u64, fs_devices->num_devices,
  1905. max_active), 64);
  1906. fs_info->fixup_workers =
  1907. btrfs_alloc_workqueue(fs_info, "fixup", flags, 1, 0);
  1908. /*
  1909. * endios are largely parallel and should have a very
  1910. * low idle thresh
  1911. */
  1912. fs_info->endio_workers =
  1913. btrfs_alloc_workqueue(fs_info, "endio", flags, max_active, 4);
  1914. fs_info->endio_meta_workers =
  1915. btrfs_alloc_workqueue(fs_info, "endio-meta", flags,
  1916. max_active, 4);
  1917. fs_info->endio_meta_write_workers =
  1918. btrfs_alloc_workqueue(fs_info, "endio-meta-write", flags,
  1919. max_active, 2);
  1920. fs_info->endio_raid56_workers =
  1921. btrfs_alloc_workqueue(fs_info, "endio-raid56", flags,
  1922. max_active, 4);
  1923. fs_info->endio_repair_workers =
  1924. btrfs_alloc_workqueue(fs_info, "endio-repair", flags, 1, 0);
  1925. fs_info->rmw_workers =
  1926. btrfs_alloc_workqueue(fs_info, "rmw", flags, max_active, 2);
  1927. fs_info->endio_write_workers =
  1928. btrfs_alloc_workqueue(fs_info, "endio-write", flags,
  1929. max_active, 2);
  1930. fs_info->endio_freespace_worker =
  1931. btrfs_alloc_workqueue(fs_info, "freespace-write", flags,
  1932. max_active, 0);
  1933. fs_info->delayed_workers =
  1934. btrfs_alloc_workqueue(fs_info, "delayed-meta", flags,
  1935. max_active, 0);
  1936. fs_info->readahead_workers =
  1937. btrfs_alloc_workqueue(fs_info, "readahead", flags,
  1938. max_active, 2);
  1939. fs_info->qgroup_rescan_workers =
  1940. btrfs_alloc_workqueue(fs_info, "qgroup-rescan", flags, 1, 0);
  1941. fs_info->extent_workers =
  1942. btrfs_alloc_workqueue(fs_info, "extent-refs", flags,
  1943. min_t(u64, fs_devices->num_devices,
  1944. max_active), 8);
  1945. if (!(fs_info->workers && fs_info->delalloc_workers &&
  1946. fs_info->submit_workers && fs_info->flush_workers &&
  1947. fs_info->endio_workers && fs_info->endio_meta_workers &&
  1948. fs_info->endio_meta_write_workers &&
  1949. fs_info->endio_repair_workers &&
  1950. fs_info->endio_write_workers && fs_info->endio_raid56_workers &&
  1951. fs_info->endio_freespace_worker && fs_info->rmw_workers &&
  1952. fs_info->caching_workers && fs_info->readahead_workers &&
  1953. fs_info->fixup_workers && fs_info->delayed_workers &&
  1954. fs_info->extent_workers &&
  1955. fs_info->qgroup_rescan_workers)) {
  1956. return -ENOMEM;
  1957. }
  1958. return 0;
  1959. }
  1960. static int btrfs_replay_log(struct btrfs_fs_info *fs_info,
  1961. struct btrfs_fs_devices *fs_devices)
  1962. {
  1963. int ret;
  1964. struct btrfs_root *log_tree_root;
  1965. struct btrfs_super_block *disk_super = fs_info->super_copy;
  1966. u64 bytenr = btrfs_super_log_root(disk_super);
  1967. int level = btrfs_super_log_root_level(disk_super);
  1968. if (fs_devices->rw_devices == 0) {
  1969. btrfs_warn(fs_info, "log replay required on RO media");
  1970. return -EIO;
  1971. }
  1972. log_tree_root = btrfs_alloc_root(fs_info, GFP_KERNEL);
  1973. if (!log_tree_root)
  1974. return -ENOMEM;
  1975. __setup_root(log_tree_root, fs_info, BTRFS_TREE_LOG_OBJECTID);
  1976. log_tree_root->node = read_tree_block(fs_info, bytenr,
  1977. fs_info->generation + 1,
  1978. level, NULL);
  1979. if (IS_ERR(log_tree_root->node)) {
  1980. btrfs_warn(fs_info, "failed to read log tree");
  1981. ret = PTR_ERR(log_tree_root->node);
  1982. kfree(log_tree_root);
  1983. return ret;
  1984. } else if (!extent_buffer_uptodate(log_tree_root->node)) {
  1985. btrfs_err(fs_info, "failed to read log tree");
  1986. free_extent_buffer(log_tree_root->node);
  1987. kfree(log_tree_root);
  1988. return -EIO;
  1989. }
  1990. /* returns with log_tree_root freed on success */
  1991. ret = btrfs_recover_log_trees(log_tree_root);
  1992. if (ret) {
  1993. btrfs_handle_fs_error(fs_info, ret,
  1994. "Failed to recover log tree");
  1995. free_extent_buffer(log_tree_root->node);
  1996. kfree(log_tree_root);
  1997. return ret;
  1998. }
  1999. if (sb_rdonly(fs_info->sb)) {
  2000. ret = btrfs_commit_super(fs_info);
  2001. if (ret)
  2002. return ret;
  2003. }
  2004. return 0;
  2005. }
  2006. static int btrfs_read_roots(struct btrfs_fs_info *fs_info)
  2007. {
  2008. struct btrfs_root *tree_root = fs_info->tree_root;
  2009. struct btrfs_root *root;
  2010. struct btrfs_key location;
  2011. int ret;
  2012. BUG_ON(!fs_info->tree_root);
  2013. location.objectid = BTRFS_EXTENT_TREE_OBJECTID;
  2014. location.type = BTRFS_ROOT_ITEM_KEY;
  2015. location.offset = 0;
  2016. root = btrfs_read_tree_root(tree_root, &location);
  2017. if (IS_ERR(root)) {
  2018. ret = PTR_ERR(root);
  2019. goto out;
  2020. }
  2021. set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
  2022. fs_info->extent_root = root;
  2023. location.objectid = BTRFS_DEV_TREE_OBJECTID;
  2024. root = btrfs_read_tree_root(tree_root, &location);
  2025. if (IS_ERR(root)) {
  2026. ret = PTR_ERR(root);
  2027. goto out;
  2028. }
  2029. set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
  2030. fs_info->dev_root = root;
  2031. btrfs_init_devices_late(fs_info);
  2032. location.objectid = BTRFS_CSUM_TREE_OBJECTID;
  2033. root = btrfs_read_tree_root(tree_root, &location);
  2034. if (IS_ERR(root)) {
  2035. ret = PTR_ERR(root);
  2036. goto out;
  2037. }
  2038. set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
  2039. fs_info->csum_root = root;
  2040. location.objectid = BTRFS_QUOTA_TREE_OBJECTID;
  2041. root = btrfs_read_tree_root(tree_root, &location);
  2042. if (!IS_ERR(root)) {
  2043. set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
  2044. set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
  2045. fs_info->quota_root = root;
  2046. }
  2047. location.objectid = BTRFS_UUID_TREE_OBJECTID;
  2048. root = btrfs_read_tree_root(tree_root, &location);
  2049. if (IS_ERR(root)) {
  2050. ret = PTR_ERR(root);
  2051. if (ret != -ENOENT)
  2052. goto out;
  2053. } else {
  2054. set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
  2055. fs_info->uuid_root = root;
  2056. }
  2057. if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
  2058. location.objectid = BTRFS_FREE_SPACE_TREE_OBJECTID;
  2059. root = btrfs_read_tree_root(tree_root, &location);
  2060. if (IS_ERR(root)) {
  2061. ret = PTR_ERR(root);
  2062. goto out;
  2063. }
  2064. set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
  2065. fs_info->free_space_root = root;
  2066. }
  2067. return 0;
  2068. out:
  2069. btrfs_warn(fs_info, "failed to read root (objectid=%llu): %d",
  2070. location.objectid, ret);
  2071. return ret;
  2072. }
  2073. /*
  2074. * Real super block validation
  2075. * NOTE: super csum type and incompat features will not be checked here.
  2076. *
  2077. * @sb: super block to check
  2078. * @mirror_num: the super block number to check its bytenr:
  2079. * 0 the primary (1st) sb
  2080. * 1, 2 2nd and 3rd backup copy
  2081. * -1 skip bytenr check
  2082. */
  2083. static int validate_super(struct btrfs_fs_info *fs_info,
  2084. struct btrfs_super_block *sb, int mirror_num)
  2085. {
  2086. u64 nodesize = btrfs_super_nodesize(sb);
  2087. u64 sectorsize = btrfs_super_sectorsize(sb);
  2088. int ret = 0;
  2089. if (btrfs_super_magic(sb) != BTRFS_MAGIC) {
  2090. btrfs_err(fs_info, "no valid FS found");
  2091. ret = -EINVAL;
  2092. }
  2093. if (btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP) {
  2094. btrfs_err(fs_info, "unrecognized or unsupported super flag: %llu",
  2095. btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP);
  2096. ret = -EINVAL;
  2097. }
  2098. if (btrfs_super_root_level(sb) >= BTRFS_MAX_LEVEL) {
  2099. btrfs_err(fs_info, "tree_root level too big: %d >= %d",
  2100. btrfs_super_root_level(sb), BTRFS_MAX_LEVEL);
  2101. ret = -EINVAL;
  2102. }
  2103. if (btrfs_super_chunk_root_level(sb) >= BTRFS_MAX_LEVEL) {
  2104. btrfs_err(fs_info, "chunk_root level too big: %d >= %d",
  2105. btrfs_super_chunk_root_level(sb), BTRFS_MAX_LEVEL);
  2106. ret = -EINVAL;
  2107. }
  2108. if (btrfs_super_log_root_level(sb) >= BTRFS_MAX_LEVEL) {
  2109. btrfs_err(fs_info, "log_root level too big: %d >= %d",
  2110. btrfs_super_log_root_level(sb), BTRFS_MAX_LEVEL);
  2111. ret = -EINVAL;
  2112. }
  2113. /*
  2114. * Check sectorsize and nodesize first, other check will need it.
  2115. * Check all possible sectorsize(4K, 8K, 16K, 32K, 64K) here.
  2116. */
  2117. if (!is_power_of_2(sectorsize) || sectorsize < 4096 ||
  2118. sectorsize > BTRFS_MAX_METADATA_BLOCKSIZE) {
  2119. btrfs_err(fs_info, "invalid sectorsize %llu", sectorsize);
  2120. ret = -EINVAL;
  2121. }
  2122. /* Only PAGE SIZE is supported yet */
  2123. if (sectorsize != PAGE_SIZE) {
  2124. btrfs_err(fs_info,
  2125. "sectorsize %llu not supported yet, only support %lu",
  2126. sectorsize, PAGE_SIZE);
  2127. ret = -EINVAL;
  2128. }
  2129. if (!is_power_of_2(nodesize) || nodesize < sectorsize ||
  2130. nodesize > BTRFS_MAX_METADATA_BLOCKSIZE) {
  2131. btrfs_err(fs_info, "invalid nodesize %llu", nodesize);
  2132. ret = -EINVAL;
  2133. }
  2134. if (nodesize != le32_to_cpu(sb->__unused_leafsize)) {
  2135. btrfs_err(fs_info, "invalid leafsize %u, should be %llu",
  2136. le32_to_cpu(sb->__unused_leafsize), nodesize);
  2137. ret = -EINVAL;
  2138. }
  2139. /* Root alignment check */
  2140. if (!IS_ALIGNED(btrfs_super_root(sb), sectorsize)) {
  2141. btrfs_warn(fs_info, "tree_root block unaligned: %llu",
  2142. btrfs_super_root(sb));
  2143. ret = -EINVAL;
  2144. }
  2145. if (!IS_ALIGNED(btrfs_super_chunk_root(sb), sectorsize)) {
  2146. btrfs_warn(fs_info, "chunk_root block unaligned: %llu",
  2147. btrfs_super_chunk_root(sb));
  2148. ret = -EINVAL;
  2149. }
  2150. if (!IS_ALIGNED(btrfs_super_log_root(sb), sectorsize)) {
  2151. btrfs_warn(fs_info, "log_root block unaligned: %llu",
  2152. btrfs_super_log_root(sb));
  2153. ret = -EINVAL;
  2154. }
  2155. if (memcmp(fs_info->fsid, sb->dev_item.fsid, BTRFS_FSID_SIZE) != 0) {
  2156. btrfs_err(fs_info,
  2157. "dev_item UUID does not match fsid: %pU != %pU",
  2158. fs_info->fsid, sb->dev_item.fsid);
  2159. ret = -EINVAL;
  2160. }
  2161. /*
  2162. * Hint to catch really bogus numbers, bitflips or so, more exact checks are
  2163. * done later
  2164. */
  2165. if (btrfs_super_bytes_used(sb) < 6 * btrfs_super_nodesize(sb)) {
  2166. btrfs_err(fs_info, "bytes_used is too small %llu",
  2167. btrfs_super_bytes_used(sb));
  2168. ret = -EINVAL;
  2169. }
  2170. if (!is_power_of_2(btrfs_super_stripesize(sb))) {
  2171. btrfs_err(fs_info, "invalid stripesize %u",
  2172. btrfs_super_stripesize(sb));
  2173. ret = -EINVAL;
  2174. }
  2175. if (btrfs_super_num_devices(sb) > (1UL << 31))
  2176. btrfs_warn(fs_info, "suspicious number of devices: %llu",
  2177. btrfs_super_num_devices(sb));
  2178. if (btrfs_super_num_devices(sb) == 0) {
  2179. btrfs_err(fs_info, "number of devices is 0");
  2180. ret = -EINVAL;
  2181. }
  2182. if (mirror_num >= 0 &&
  2183. btrfs_super_bytenr(sb) != btrfs_sb_offset(mirror_num)) {
  2184. btrfs_err(fs_info, "super offset mismatch %llu != %u",
  2185. btrfs_super_bytenr(sb), BTRFS_SUPER_INFO_OFFSET);
  2186. ret = -EINVAL;
  2187. }
  2188. /*
  2189. * Obvious sys_chunk_array corruptions, it must hold at least one key
  2190. * and one chunk
  2191. */
  2192. if (btrfs_super_sys_array_size(sb) > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
  2193. btrfs_err(fs_info, "system chunk array too big %u > %u",
  2194. btrfs_super_sys_array_size(sb),
  2195. BTRFS_SYSTEM_CHUNK_ARRAY_SIZE);
  2196. ret = -EINVAL;
  2197. }
  2198. if (btrfs_super_sys_array_size(sb) < sizeof(struct btrfs_disk_key)
  2199. + sizeof(struct btrfs_chunk)) {
  2200. btrfs_err(fs_info, "system chunk array too small %u < %zu",
  2201. btrfs_super_sys_array_size(sb),
  2202. sizeof(struct btrfs_disk_key)
  2203. + sizeof(struct btrfs_chunk));
  2204. ret = -EINVAL;
  2205. }
  2206. /*
  2207. * The generation is a global counter, we'll trust it more than the others
  2208. * but it's still possible that it's the one that's wrong.
  2209. */
  2210. if (btrfs_super_generation(sb) < btrfs_super_chunk_root_generation(sb))
  2211. btrfs_warn(fs_info,
  2212. "suspicious: generation < chunk_root_generation: %llu < %llu",
  2213. btrfs_super_generation(sb),
  2214. btrfs_super_chunk_root_generation(sb));
  2215. if (btrfs_super_generation(sb) < btrfs_super_cache_generation(sb)
  2216. && btrfs_super_cache_generation(sb) != (u64)-1)
  2217. btrfs_warn(fs_info,
  2218. "suspicious: generation < cache_generation: %llu < %llu",
  2219. btrfs_super_generation(sb),
  2220. btrfs_super_cache_generation(sb));
  2221. return ret;
  2222. }
  2223. /*
  2224. * Validation of super block at mount time.
  2225. * Some checks already done early at mount time, like csum type and incompat
  2226. * flags will be skipped.
  2227. */
  2228. static int btrfs_validate_mount_super(struct btrfs_fs_info *fs_info)
  2229. {
  2230. return validate_super(fs_info, fs_info->super_copy, 0);
  2231. }
  2232. /*
  2233. * Validation of super block at write time.
  2234. * Some checks like bytenr check will be skipped as their values will be
  2235. * overwritten soon.
  2236. * Extra checks like csum type and incompat flags will be done here.
  2237. */
  2238. static int btrfs_validate_write_super(struct btrfs_fs_info *fs_info,
  2239. struct btrfs_super_block *sb)
  2240. {
  2241. int ret;
  2242. ret = validate_super(fs_info, sb, -1);
  2243. if (ret < 0)
  2244. goto out;
  2245. if (btrfs_super_csum_type(sb) != BTRFS_CSUM_TYPE_CRC32) {
  2246. ret = -EUCLEAN;
  2247. btrfs_err(fs_info, "invalid csum type, has %u want %u",
  2248. btrfs_super_csum_type(sb), BTRFS_CSUM_TYPE_CRC32);
  2249. goto out;
  2250. }
  2251. if (btrfs_super_incompat_flags(sb) & ~BTRFS_FEATURE_INCOMPAT_SUPP) {
  2252. ret = -EUCLEAN;
  2253. btrfs_err(fs_info,
  2254. "invalid incompat flags, has 0x%llx valid mask 0x%llx",
  2255. btrfs_super_incompat_flags(sb),
  2256. (unsigned long long)BTRFS_FEATURE_INCOMPAT_SUPP);
  2257. goto out;
  2258. }
  2259. out:
  2260. if (ret < 0)
  2261. btrfs_err(fs_info,
  2262. "super block corruption detected before writing it to disk");
  2263. return ret;
  2264. }
  2265. int open_ctree(struct super_block *sb,
  2266. struct btrfs_fs_devices *fs_devices,
  2267. char *options)
  2268. {
  2269. u32 sectorsize;
  2270. u32 nodesize;
  2271. u32 stripesize;
  2272. u64 generation;
  2273. u64 features;
  2274. struct btrfs_key location;
  2275. struct buffer_head *bh;
  2276. struct btrfs_super_block *disk_super;
  2277. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  2278. struct btrfs_root *tree_root;
  2279. struct btrfs_root *chunk_root;
  2280. int ret;
  2281. int err = -EINVAL;
  2282. int num_backups_tried = 0;
  2283. int backup_index = 0;
  2284. int clear_free_space_tree = 0;
  2285. int level;
  2286. tree_root = fs_info->tree_root = btrfs_alloc_root(fs_info, GFP_KERNEL);
  2287. chunk_root = fs_info->chunk_root = btrfs_alloc_root(fs_info, GFP_KERNEL);
  2288. if (!tree_root || !chunk_root) {
  2289. err = -ENOMEM;
  2290. goto fail;
  2291. }
  2292. ret = init_srcu_struct(&fs_info->subvol_srcu);
  2293. if (ret) {
  2294. err = ret;
  2295. goto fail;
  2296. }
  2297. ret = percpu_counter_init(&fs_info->dirty_metadata_bytes, 0, GFP_KERNEL);
  2298. if (ret) {
  2299. err = ret;
  2300. goto fail_srcu;
  2301. }
  2302. fs_info->dirty_metadata_batch = PAGE_SIZE *
  2303. (1 + ilog2(nr_cpu_ids));
  2304. ret = percpu_counter_init(&fs_info->delalloc_bytes, 0, GFP_KERNEL);
  2305. if (ret) {
  2306. err = ret;
  2307. goto fail_dirty_metadata_bytes;
  2308. }
  2309. ret = percpu_counter_init(&fs_info->bio_counter, 0, GFP_KERNEL);
  2310. if (ret) {
  2311. err = ret;
  2312. goto fail_delalloc_bytes;
  2313. }
  2314. INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC);
  2315. INIT_RADIX_TREE(&fs_info->buffer_radix, GFP_ATOMIC);
  2316. INIT_LIST_HEAD(&fs_info->trans_list);
  2317. INIT_LIST_HEAD(&fs_info->dead_roots);
  2318. INIT_LIST_HEAD(&fs_info->delayed_iputs);
  2319. INIT_LIST_HEAD(&fs_info->delalloc_roots);
  2320. INIT_LIST_HEAD(&fs_info->caching_block_groups);
  2321. INIT_LIST_HEAD(&fs_info->pending_raid_kobjs);
  2322. spin_lock_init(&fs_info->pending_raid_kobjs_lock);
  2323. spin_lock_init(&fs_info->delalloc_root_lock);
  2324. spin_lock_init(&fs_info->trans_lock);
  2325. spin_lock_init(&fs_info->fs_roots_radix_lock);
  2326. spin_lock_init(&fs_info->delayed_iput_lock);
  2327. spin_lock_init(&fs_info->defrag_inodes_lock);
  2328. spin_lock_init(&fs_info->super_lock);
  2329. spin_lock_init(&fs_info->qgroup_op_lock);
  2330. spin_lock_init(&fs_info->buffer_lock);
  2331. spin_lock_init(&fs_info->unused_bgs_lock);
  2332. rwlock_init(&fs_info->tree_mod_log_lock);
  2333. mutex_init(&fs_info->unused_bg_unpin_mutex);
  2334. mutex_init(&fs_info->delete_unused_bgs_mutex);
  2335. mutex_init(&fs_info->reloc_mutex);
  2336. mutex_init(&fs_info->delalloc_root_mutex);
  2337. mutex_init(&fs_info->cleaner_delayed_iput_mutex);
  2338. seqlock_init(&fs_info->profiles_lock);
  2339. INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
  2340. INIT_LIST_HEAD(&fs_info->space_info);
  2341. INIT_LIST_HEAD(&fs_info->tree_mod_seq_list);
  2342. INIT_LIST_HEAD(&fs_info->unused_bgs);
  2343. btrfs_mapping_init(&fs_info->mapping_tree);
  2344. btrfs_init_block_rsv(&fs_info->global_block_rsv,
  2345. BTRFS_BLOCK_RSV_GLOBAL);
  2346. btrfs_init_block_rsv(&fs_info->trans_block_rsv, BTRFS_BLOCK_RSV_TRANS);
  2347. btrfs_init_block_rsv(&fs_info->chunk_block_rsv, BTRFS_BLOCK_RSV_CHUNK);
  2348. btrfs_init_block_rsv(&fs_info->empty_block_rsv, BTRFS_BLOCK_RSV_EMPTY);
  2349. btrfs_init_block_rsv(&fs_info->delayed_block_rsv,
  2350. BTRFS_BLOCK_RSV_DELOPS);
  2351. atomic_set(&fs_info->async_delalloc_pages, 0);
  2352. atomic_set(&fs_info->defrag_running, 0);
  2353. atomic_set(&fs_info->qgroup_op_seq, 0);
  2354. atomic_set(&fs_info->reada_works_cnt, 0);
  2355. atomic64_set(&fs_info->tree_mod_seq, 0);
  2356. fs_info->sb = sb;
  2357. fs_info->max_inline = BTRFS_DEFAULT_MAX_INLINE;
  2358. fs_info->metadata_ratio = 0;
  2359. fs_info->defrag_inodes = RB_ROOT;
  2360. atomic64_set(&fs_info->free_chunk_space, 0);
  2361. fs_info->tree_mod_log = RB_ROOT;
  2362. fs_info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL;
  2363. fs_info->avg_delayed_ref_runtime = NSEC_PER_SEC >> 6; /* div by 64 */
  2364. /* readahead state */
  2365. INIT_RADIX_TREE(&fs_info->reada_tree, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
  2366. spin_lock_init(&fs_info->reada_lock);
  2367. btrfs_init_ref_verify(fs_info);
  2368. fs_info->thread_pool_size = min_t(unsigned long,
  2369. num_online_cpus() + 2, 8);
  2370. INIT_LIST_HEAD(&fs_info->ordered_roots);
  2371. spin_lock_init(&fs_info->ordered_root_lock);
  2372. fs_info->btree_inode = new_inode(sb);
  2373. if (!fs_info->btree_inode) {
  2374. err = -ENOMEM;
  2375. goto fail_bio_counter;
  2376. }
  2377. mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
  2378. fs_info->delayed_root = kmalloc(sizeof(struct btrfs_delayed_root),
  2379. GFP_KERNEL);
  2380. if (!fs_info->delayed_root) {
  2381. err = -ENOMEM;
  2382. goto fail_iput;
  2383. }
  2384. btrfs_init_delayed_root(fs_info->delayed_root);
  2385. btrfs_init_scrub(fs_info);
  2386. #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
  2387. fs_info->check_integrity_print_mask = 0;
  2388. #endif
  2389. btrfs_init_balance(fs_info);
  2390. btrfs_init_async_reclaim_work(&fs_info->async_reclaim_work);
  2391. sb->s_blocksize = BTRFS_BDEV_BLOCKSIZE;
  2392. sb->s_blocksize_bits = blksize_bits(BTRFS_BDEV_BLOCKSIZE);
  2393. btrfs_init_btree_inode(fs_info);
  2394. spin_lock_init(&fs_info->block_group_cache_lock);
  2395. fs_info->block_group_cache_tree = RB_ROOT;
  2396. fs_info->first_logical_byte = (u64)-1;
  2397. extent_io_tree_init(&fs_info->freed_extents[0], NULL);
  2398. extent_io_tree_init(&fs_info->freed_extents[1], NULL);
  2399. fs_info->pinned_extents = &fs_info->freed_extents[0];
  2400. set_bit(BTRFS_FS_BARRIER, &fs_info->flags);
  2401. mutex_init(&fs_info->ordered_operations_mutex);
  2402. mutex_init(&fs_info->tree_log_mutex);
  2403. mutex_init(&fs_info->chunk_mutex);
  2404. mutex_init(&fs_info->transaction_kthread_mutex);
  2405. mutex_init(&fs_info->cleaner_mutex);
  2406. mutex_init(&fs_info->ro_block_group_mutex);
  2407. init_rwsem(&fs_info->commit_root_sem);
  2408. init_rwsem(&fs_info->cleanup_work_sem);
  2409. init_rwsem(&fs_info->subvol_sem);
  2410. sema_init(&fs_info->uuid_tree_rescan_sem, 1);
  2411. btrfs_init_dev_replace_locks(fs_info);
  2412. btrfs_init_qgroup(fs_info);
  2413. btrfs_init_free_cluster(&fs_info->meta_alloc_cluster);
  2414. btrfs_init_free_cluster(&fs_info->data_alloc_cluster);
  2415. init_waitqueue_head(&fs_info->transaction_throttle);
  2416. init_waitqueue_head(&fs_info->transaction_wait);
  2417. init_waitqueue_head(&fs_info->transaction_blocked_wait);
  2418. init_waitqueue_head(&fs_info->async_submit_wait);
  2419. INIT_LIST_HEAD(&fs_info->pinned_chunks);
  2420. /* Usable values until the real ones are cached from the superblock */
  2421. fs_info->nodesize = 4096;
  2422. fs_info->sectorsize = 4096;
  2423. fs_info->stripesize = 4096;
  2424. ret = btrfs_alloc_stripe_hash_table(fs_info);
  2425. if (ret) {
  2426. err = ret;
  2427. goto fail_alloc;
  2428. }
  2429. __setup_root(tree_root, fs_info, BTRFS_ROOT_TREE_OBJECTID);
  2430. invalidate_bdev(fs_devices->latest_bdev);
  2431. /*
  2432. * Read super block and check the signature bytes only
  2433. */
  2434. bh = btrfs_read_dev_super(fs_devices->latest_bdev);
  2435. if (IS_ERR(bh)) {
  2436. err = PTR_ERR(bh);
  2437. goto fail_alloc;
  2438. }
  2439. /*
  2440. * We want to check superblock checksum, the type is stored inside.
  2441. * Pass the whole disk block of size BTRFS_SUPER_INFO_SIZE (4k).
  2442. */
  2443. if (btrfs_check_super_csum(fs_info, bh->b_data)) {
  2444. btrfs_err(fs_info, "superblock checksum mismatch");
  2445. err = -EINVAL;
  2446. brelse(bh);
  2447. goto fail_alloc;
  2448. }
  2449. /*
  2450. * super_copy is zeroed at allocation time and we never touch the
  2451. * following bytes up to INFO_SIZE, the checksum is calculated from
  2452. * the whole block of INFO_SIZE
  2453. */
  2454. memcpy(fs_info->super_copy, bh->b_data, sizeof(*fs_info->super_copy));
  2455. memcpy(fs_info->super_for_commit, fs_info->super_copy,
  2456. sizeof(*fs_info->super_for_commit));
  2457. brelse(bh);
  2458. memcpy(fs_info->fsid, fs_info->super_copy->fsid, BTRFS_FSID_SIZE);
  2459. ret = btrfs_validate_mount_super(fs_info);
  2460. if (ret) {
  2461. btrfs_err(fs_info, "superblock contains fatal errors");
  2462. err = -EINVAL;
  2463. goto fail_alloc;
  2464. }
  2465. disk_super = fs_info->super_copy;
  2466. if (!btrfs_super_root(disk_super))
  2467. goto fail_alloc;
  2468. /* check FS state, whether FS is broken. */
  2469. if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_ERROR)
  2470. set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state);
  2471. /*
  2472. * run through our array of backup supers and setup
  2473. * our ring pointer to the oldest one
  2474. */
  2475. generation = btrfs_super_generation(disk_super);
  2476. find_oldest_super_backup(fs_info, generation);
  2477. /*
  2478. * In the long term, we'll store the compression type in the super
  2479. * block, and it'll be used for per file compression control.
  2480. */
  2481. fs_info->compress_type = BTRFS_COMPRESS_ZLIB;
  2482. ret = btrfs_parse_options(fs_info, options, sb->s_flags);
  2483. if (ret) {
  2484. err = ret;
  2485. goto fail_alloc;
  2486. }
  2487. features = btrfs_super_incompat_flags(disk_super) &
  2488. ~BTRFS_FEATURE_INCOMPAT_SUPP;
  2489. if (features) {
  2490. btrfs_err(fs_info,
  2491. "cannot mount because of unsupported optional features (%llx)",
  2492. features);
  2493. err = -EINVAL;
  2494. goto fail_alloc;
  2495. }
  2496. features = btrfs_super_incompat_flags(disk_super);
  2497. features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF;
  2498. if (fs_info->compress_type == BTRFS_COMPRESS_LZO)
  2499. features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
  2500. else if (fs_info->compress_type == BTRFS_COMPRESS_ZSTD)
  2501. features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_ZSTD;
  2502. if (features & BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA)
  2503. btrfs_info(fs_info, "has skinny extents");
  2504. /*
  2505. * flag our filesystem as having big metadata blocks if
  2506. * they are bigger than the page size
  2507. */
  2508. if (btrfs_super_nodesize(disk_super) > PAGE_SIZE) {
  2509. if (!(features & BTRFS_FEATURE_INCOMPAT_BIG_METADATA))
  2510. btrfs_info(fs_info,
  2511. "flagging fs with big metadata feature");
  2512. features |= BTRFS_FEATURE_INCOMPAT_BIG_METADATA;
  2513. }
  2514. nodesize = btrfs_super_nodesize(disk_super);
  2515. sectorsize = btrfs_super_sectorsize(disk_super);
  2516. stripesize = sectorsize;
  2517. fs_info->dirty_metadata_batch = nodesize * (1 + ilog2(nr_cpu_ids));
  2518. fs_info->delalloc_batch = sectorsize * 512 * (1 + ilog2(nr_cpu_ids));
  2519. /* Cache block sizes */
  2520. fs_info->nodesize = nodesize;
  2521. fs_info->sectorsize = sectorsize;
  2522. fs_info->stripesize = stripesize;
  2523. /*
  2524. * mixed block groups end up with duplicate but slightly offset
  2525. * extent buffers for the same range. It leads to corruptions
  2526. */
  2527. if ((features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) &&
  2528. (sectorsize != nodesize)) {
  2529. btrfs_err(fs_info,
  2530. "unequal nodesize/sectorsize (%u != %u) are not allowed for mixed block groups",
  2531. nodesize, sectorsize);
  2532. goto fail_alloc;
  2533. }
  2534. /*
  2535. * Needn't use the lock because there is no other task which will
  2536. * update the flag.
  2537. */
  2538. btrfs_set_super_incompat_flags(disk_super, features);
  2539. features = btrfs_super_compat_ro_flags(disk_super) &
  2540. ~BTRFS_FEATURE_COMPAT_RO_SUPP;
  2541. if (!sb_rdonly(sb) && features) {
  2542. btrfs_err(fs_info,
  2543. "cannot mount read-write because of unsupported optional features (%llx)",
  2544. features);
  2545. err = -EINVAL;
  2546. goto fail_alloc;
  2547. }
  2548. ret = btrfs_init_workqueues(fs_info, fs_devices);
  2549. if (ret) {
  2550. err = ret;
  2551. goto fail_sb_buffer;
  2552. }
  2553. sb->s_bdi->congested_fn = btrfs_congested_fn;
  2554. sb->s_bdi->congested_data = fs_info;
  2555. sb->s_bdi->capabilities |= BDI_CAP_CGROUP_WRITEBACK;
  2556. sb->s_bdi->ra_pages = VM_MAX_READAHEAD * SZ_1K / PAGE_SIZE;
  2557. sb->s_bdi->ra_pages *= btrfs_super_num_devices(disk_super);
  2558. sb->s_bdi->ra_pages = max(sb->s_bdi->ra_pages, SZ_4M / PAGE_SIZE);
  2559. sb->s_blocksize = sectorsize;
  2560. sb->s_blocksize_bits = blksize_bits(sectorsize);
  2561. memcpy(&sb->s_uuid, fs_info->fsid, BTRFS_FSID_SIZE);
  2562. mutex_lock(&fs_info->chunk_mutex);
  2563. ret = btrfs_read_sys_array(fs_info);
  2564. mutex_unlock(&fs_info->chunk_mutex);
  2565. if (ret) {
  2566. btrfs_err(fs_info, "failed to read the system array: %d", ret);
  2567. goto fail_sb_buffer;
  2568. }
  2569. generation = btrfs_super_chunk_root_generation(disk_super);
  2570. level = btrfs_super_chunk_root_level(disk_super);
  2571. __setup_root(chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
  2572. chunk_root->node = read_tree_block(fs_info,
  2573. btrfs_super_chunk_root(disk_super),
  2574. generation, level, NULL);
  2575. if (IS_ERR(chunk_root->node) ||
  2576. !extent_buffer_uptodate(chunk_root->node)) {
  2577. btrfs_err(fs_info, "failed to read chunk root");
  2578. if (!IS_ERR(chunk_root->node))
  2579. free_extent_buffer(chunk_root->node);
  2580. chunk_root->node = NULL;
  2581. goto fail_tree_roots;
  2582. }
  2583. btrfs_set_root_node(&chunk_root->root_item, chunk_root->node);
  2584. chunk_root->commit_root = btrfs_root_node(chunk_root);
  2585. read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid,
  2586. btrfs_header_chunk_tree_uuid(chunk_root->node), BTRFS_UUID_SIZE);
  2587. ret = btrfs_read_chunk_tree(fs_info);
  2588. if (ret) {
  2589. btrfs_err(fs_info, "failed to read chunk tree: %d", ret);
  2590. goto fail_tree_roots;
  2591. }
  2592. /*
  2593. * Keep the devid that is marked to be the target device for the
  2594. * device replace procedure
  2595. */
  2596. btrfs_free_extra_devids(fs_devices, 0);
  2597. if (!fs_devices->latest_bdev) {
  2598. btrfs_err(fs_info, "failed to read devices");
  2599. goto fail_tree_roots;
  2600. }
  2601. retry_root_backup:
  2602. generation = btrfs_super_generation(disk_super);
  2603. level = btrfs_super_root_level(disk_super);
  2604. tree_root->node = read_tree_block(fs_info,
  2605. btrfs_super_root(disk_super),
  2606. generation, level, NULL);
  2607. if (IS_ERR(tree_root->node) ||
  2608. !extent_buffer_uptodate(tree_root->node)) {
  2609. btrfs_warn(fs_info, "failed to read tree root");
  2610. if (!IS_ERR(tree_root->node))
  2611. free_extent_buffer(tree_root->node);
  2612. tree_root->node = NULL;
  2613. goto recovery_tree_root;
  2614. }
  2615. btrfs_set_root_node(&tree_root->root_item, tree_root->node);
  2616. tree_root->commit_root = btrfs_root_node(tree_root);
  2617. btrfs_set_root_refs(&tree_root->root_item, 1);
  2618. mutex_lock(&tree_root->objectid_mutex);
  2619. ret = btrfs_find_highest_objectid(tree_root,
  2620. &tree_root->highest_objectid);
  2621. if (ret) {
  2622. mutex_unlock(&tree_root->objectid_mutex);
  2623. goto recovery_tree_root;
  2624. }
  2625. ASSERT(tree_root->highest_objectid <= BTRFS_LAST_FREE_OBJECTID);
  2626. mutex_unlock(&tree_root->objectid_mutex);
  2627. ret = btrfs_read_roots(fs_info);
  2628. if (ret)
  2629. goto recovery_tree_root;
  2630. fs_info->generation = generation;
  2631. fs_info->last_trans_committed = generation;
  2632. /*
  2633. * If we have a uuid root and we're not being told to rescan we need to
  2634. * check the generation here so we can set the
  2635. * BTRFS_FS_UPDATE_UUID_TREE_GEN bit. Otherwise we could commit the
  2636. * transaction during a balance or the log replay without updating the
  2637. * uuid generation, and then if we crash we would rescan the uuid tree,
  2638. * even though it was perfectly fine.
  2639. */
  2640. if (fs_info->uuid_root && !btrfs_test_opt(fs_info, RESCAN_UUID_TREE) &&
  2641. fs_info->generation == btrfs_super_uuid_tree_generation(disk_super))
  2642. set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags);
  2643. ret = btrfs_verify_dev_extents(fs_info);
  2644. if (ret) {
  2645. btrfs_err(fs_info,
  2646. "failed to verify dev extents against chunks: %d",
  2647. ret);
  2648. goto fail_block_groups;
  2649. }
  2650. ret = btrfs_recover_balance(fs_info);
  2651. if (ret) {
  2652. btrfs_err(fs_info, "failed to recover balance: %d", ret);
  2653. goto fail_block_groups;
  2654. }
  2655. ret = btrfs_init_dev_stats(fs_info);
  2656. if (ret) {
  2657. btrfs_err(fs_info, "failed to init dev_stats: %d", ret);
  2658. goto fail_block_groups;
  2659. }
  2660. ret = btrfs_init_dev_replace(fs_info);
  2661. if (ret) {
  2662. btrfs_err(fs_info, "failed to init dev_replace: %d", ret);
  2663. goto fail_block_groups;
  2664. }
  2665. btrfs_free_extra_devids(fs_devices, 1);
  2666. ret = btrfs_sysfs_add_fsid(fs_devices, NULL);
  2667. if (ret) {
  2668. btrfs_err(fs_info, "failed to init sysfs fsid interface: %d",
  2669. ret);
  2670. goto fail_block_groups;
  2671. }
  2672. ret = btrfs_sysfs_add_device(fs_devices);
  2673. if (ret) {
  2674. btrfs_err(fs_info, "failed to init sysfs device interface: %d",
  2675. ret);
  2676. goto fail_fsdev_sysfs;
  2677. }
  2678. ret = btrfs_sysfs_add_mounted(fs_info);
  2679. if (ret) {
  2680. btrfs_err(fs_info, "failed to init sysfs interface: %d", ret);
  2681. goto fail_fsdev_sysfs;
  2682. }
  2683. ret = btrfs_init_space_info(fs_info);
  2684. if (ret) {
  2685. btrfs_err(fs_info, "failed to initialize space info: %d", ret);
  2686. goto fail_sysfs;
  2687. }
  2688. ret = btrfs_read_block_groups(fs_info);
  2689. if (ret) {
  2690. btrfs_err(fs_info, "failed to read block groups: %d", ret);
  2691. goto fail_sysfs;
  2692. }
  2693. if (!sb_rdonly(sb) && !btrfs_check_rw_degradable(fs_info, NULL)) {
  2694. btrfs_warn(fs_info,
  2695. "writeable mount is not allowed due to too many missing devices");
  2696. goto fail_sysfs;
  2697. }
  2698. fs_info->cleaner_kthread = kthread_run(cleaner_kthread, tree_root,
  2699. "btrfs-cleaner");
  2700. if (IS_ERR(fs_info->cleaner_kthread))
  2701. goto fail_sysfs;
  2702. fs_info->transaction_kthread = kthread_run(transaction_kthread,
  2703. tree_root,
  2704. "btrfs-transaction");
  2705. if (IS_ERR(fs_info->transaction_kthread))
  2706. goto fail_cleaner;
  2707. if (!btrfs_test_opt(fs_info, NOSSD) &&
  2708. !fs_info->fs_devices->rotating) {
  2709. btrfs_set_and_info(fs_info, SSD, "enabling ssd optimizations");
  2710. }
  2711. /*
  2712. * Mount does not set all options immediately, we can do it now and do
  2713. * not have to wait for transaction commit
  2714. */
  2715. btrfs_apply_pending_changes(fs_info);
  2716. #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
  2717. if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) {
  2718. ret = btrfsic_mount(fs_info, fs_devices,
  2719. btrfs_test_opt(fs_info,
  2720. CHECK_INTEGRITY_INCLUDING_EXTENT_DATA) ?
  2721. 1 : 0,
  2722. fs_info->check_integrity_print_mask);
  2723. if (ret)
  2724. btrfs_warn(fs_info,
  2725. "failed to initialize integrity check module: %d",
  2726. ret);
  2727. }
  2728. #endif
  2729. ret = btrfs_read_qgroup_config(fs_info);
  2730. if (ret)
  2731. goto fail_trans_kthread;
  2732. if (btrfs_build_ref_tree(fs_info))
  2733. btrfs_err(fs_info, "couldn't build ref tree");
  2734. /* do not make disk changes in broken FS or nologreplay is given */
  2735. if (btrfs_super_log_root(disk_super) != 0 &&
  2736. !btrfs_test_opt(fs_info, NOLOGREPLAY)) {
  2737. btrfs_info(fs_info, "start tree-log replay");
  2738. ret = btrfs_replay_log(fs_info, fs_devices);
  2739. if (ret) {
  2740. err = ret;
  2741. goto fail_qgroup;
  2742. }
  2743. }
  2744. ret = btrfs_find_orphan_roots(fs_info);
  2745. if (ret)
  2746. goto fail_qgroup;
  2747. if (!sb_rdonly(sb)) {
  2748. ret = btrfs_cleanup_fs_roots(fs_info);
  2749. if (ret)
  2750. goto fail_qgroup;
  2751. mutex_lock(&fs_info->cleaner_mutex);
  2752. ret = btrfs_recover_relocation(tree_root);
  2753. mutex_unlock(&fs_info->cleaner_mutex);
  2754. if (ret < 0) {
  2755. btrfs_warn(fs_info, "failed to recover relocation: %d",
  2756. ret);
  2757. err = -EINVAL;
  2758. goto fail_qgroup;
  2759. }
  2760. }
  2761. location.objectid = BTRFS_FS_TREE_OBJECTID;
  2762. location.type = BTRFS_ROOT_ITEM_KEY;
  2763. location.offset = 0;
  2764. fs_info->fs_root = btrfs_read_fs_root_no_name(fs_info, &location);
  2765. if (IS_ERR(fs_info->fs_root)) {
  2766. err = PTR_ERR(fs_info->fs_root);
  2767. btrfs_warn(fs_info, "failed to read fs tree: %d", err);
  2768. fs_info->fs_root = NULL;
  2769. goto fail_qgroup;
  2770. }
  2771. if (sb_rdonly(sb))
  2772. return 0;
  2773. if (btrfs_test_opt(fs_info, CLEAR_CACHE) &&
  2774. btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
  2775. clear_free_space_tree = 1;
  2776. } else if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) &&
  2777. !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID)) {
  2778. btrfs_warn(fs_info, "free space tree is invalid");
  2779. clear_free_space_tree = 1;
  2780. }
  2781. if (clear_free_space_tree) {
  2782. btrfs_info(fs_info, "clearing free space tree");
  2783. ret = btrfs_clear_free_space_tree(fs_info);
  2784. if (ret) {
  2785. btrfs_warn(fs_info,
  2786. "failed to clear free space tree: %d", ret);
  2787. close_ctree(fs_info);
  2788. return ret;
  2789. }
  2790. }
  2791. if (btrfs_test_opt(fs_info, FREE_SPACE_TREE) &&
  2792. !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
  2793. btrfs_info(fs_info, "creating free space tree");
  2794. ret = btrfs_create_free_space_tree(fs_info);
  2795. if (ret) {
  2796. btrfs_warn(fs_info,
  2797. "failed to create free space tree: %d", ret);
  2798. close_ctree(fs_info);
  2799. return ret;
  2800. }
  2801. }
  2802. down_read(&fs_info->cleanup_work_sem);
  2803. if ((ret = btrfs_orphan_cleanup(fs_info->fs_root)) ||
  2804. (ret = btrfs_orphan_cleanup(fs_info->tree_root))) {
  2805. up_read(&fs_info->cleanup_work_sem);
  2806. close_ctree(fs_info);
  2807. return ret;
  2808. }
  2809. up_read(&fs_info->cleanup_work_sem);
  2810. ret = btrfs_resume_balance_async(fs_info);
  2811. if (ret) {
  2812. btrfs_warn(fs_info, "failed to resume balance: %d", ret);
  2813. close_ctree(fs_info);
  2814. return ret;
  2815. }
  2816. ret = btrfs_resume_dev_replace_async(fs_info);
  2817. if (ret) {
  2818. btrfs_warn(fs_info, "failed to resume device replace: %d", ret);
  2819. close_ctree(fs_info);
  2820. return ret;
  2821. }
  2822. btrfs_qgroup_rescan_resume(fs_info);
  2823. if (!fs_info->uuid_root) {
  2824. btrfs_info(fs_info, "creating UUID tree");
  2825. ret = btrfs_create_uuid_tree(fs_info);
  2826. if (ret) {
  2827. btrfs_warn(fs_info,
  2828. "failed to create the UUID tree: %d", ret);
  2829. close_ctree(fs_info);
  2830. return ret;
  2831. }
  2832. } else if (btrfs_test_opt(fs_info, RESCAN_UUID_TREE) ||
  2833. fs_info->generation !=
  2834. btrfs_super_uuid_tree_generation(disk_super)) {
  2835. btrfs_info(fs_info, "checking UUID tree");
  2836. ret = btrfs_check_uuid_tree(fs_info);
  2837. if (ret) {
  2838. btrfs_warn(fs_info,
  2839. "failed to check the UUID tree: %d", ret);
  2840. close_ctree(fs_info);
  2841. return ret;
  2842. }
  2843. }
  2844. set_bit(BTRFS_FS_OPEN, &fs_info->flags);
  2845. /*
  2846. * backuproot only affect mount behavior, and if open_ctree succeeded,
  2847. * no need to keep the flag
  2848. */
  2849. btrfs_clear_opt(fs_info->mount_opt, USEBACKUPROOT);
  2850. return 0;
  2851. fail_qgroup:
  2852. btrfs_free_qgroup_config(fs_info);
  2853. fail_trans_kthread:
  2854. kthread_stop(fs_info->transaction_kthread);
  2855. btrfs_cleanup_transaction(fs_info);
  2856. btrfs_free_fs_roots(fs_info);
  2857. fail_cleaner:
  2858. kthread_stop(fs_info->cleaner_kthread);
  2859. /*
  2860. * make sure we're done with the btree inode before we stop our
  2861. * kthreads
  2862. */
  2863. filemap_write_and_wait(fs_info->btree_inode->i_mapping);
  2864. fail_sysfs:
  2865. btrfs_sysfs_remove_mounted(fs_info);
  2866. fail_fsdev_sysfs:
  2867. btrfs_sysfs_remove_fsid(fs_info->fs_devices);
  2868. fail_block_groups:
  2869. btrfs_put_block_group_cache(fs_info);
  2870. fail_tree_roots:
  2871. free_root_pointers(fs_info, true);
  2872. invalidate_inode_pages2(fs_info->btree_inode->i_mapping);
  2873. fail_sb_buffer:
  2874. btrfs_stop_all_workers(fs_info);
  2875. btrfs_free_block_groups(fs_info);
  2876. fail_alloc:
  2877. fail_iput:
  2878. btrfs_mapping_tree_free(&fs_info->mapping_tree);
  2879. iput(fs_info->btree_inode);
  2880. fail_bio_counter:
  2881. percpu_counter_destroy(&fs_info->bio_counter);
  2882. fail_delalloc_bytes:
  2883. percpu_counter_destroy(&fs_info->delalloc_bytes);
  2884. fail_dirty_metadata_bytes:
  2885. percpu_counter_destroy(&fs_info->dirty_metadata_bytes);
  2886. fail_srcu:
  2887. cleanup_srcu_struct(&fs_info->subvol_srcu);
  2888. fail:
  2889. btrfs_free_stripe_hash_table(fs_info);
  2890. btrfs_close_devices(fs_info->fs_devices);
  2891. return err;
  2892. recovery_tree_root:
  2893. if (!btrfs_test_opt(fs_info, USEBACKUPROOT))
  2894. goto fail_tree_roots;
  2895. free_root_pointers(fs_info, false);
  2896. /* don't use the log in recovery mode, it won't be valid */
  2897. btrfs_set_super_log_root(disk_super, 0);
  2898. /* we can't trust the free space cache either */
  2899. btrfs_set_opt(fs_info->mount_opt, CLEAR_CACHE);
  2900. ret = next_root_backup(fs_info, fs_info->super_copy,
  2901. &num_backups_tried, &backup_index);
  2902. if (ret == -1)
  2903. goto fail_block_groups;
  2904. goto retry_root_backup;
  2905. }
  2906. ALLOW_ERROR_INJECTION(open_ctree, ERRNO);
  2907. static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate)
  2908. {
  2909. if (uptodate) {
  2910. set_buffer_uptodate(bh);
  2911. } else {
  2912. struct btrfs_device *device = (struct btrfs_device *)
  2913. bh->b_private;
  2914. btrfs_warn_rl_in_rcu(device->fs_info,
  2915. "lost page write due to IO error on %s",
  2916. rcu_str_deref(device->name));
  2917. /* note, we don't set_buffer_write_io_error because we have
  2918. * our own ways of dealing with the IO errors
  2919. */
  2920. clear_buffer_uptodate(bh);
  2921. btrfs_dev_stat_inc_and_print(device, BTRFS_DEV_STAT_WRITE_ERRS);
  2922. }
  2923. unlock_buffer(bh);
  2924. put_bh(bh);
  2925. }
  2926. int btrfs_read_dev_one_super(struct block_device *bdev, int copy_num,
  2927. struct buffer_head **bh_ret)
  2928. {
  2929. struct buffer_head *bh;
  2930. struct btrfs_super_block *super;
  2931. u64 bytenr;
  2932. bytenr = btrfs_sb_offset(copy_num);
  2933. if (bytenr + BTRFS_SUPER_INFO_SIZE >= i_size_read(bdev->bd_inode))
  2934. return -EINVAL;
  2935. bh = __bread(bdev, bytenr / BTRFS_BDEV_BLOCKSIZE, BTRFS_SUPER_INFO_SIZE);
  2936. /*
  2937. * If we fail to read from the underlying devices, as of now
  2938. * the best option we have is to mark it EIO.
  2939. */
  2940. if (!bh)
  2941. return -EIO;
  2942. super = (struct btrfs_super_block *)bh->b_data;
  2943. if (btrfs_super_bytenr(super) != bytenr ||
  2944. btrfs_super_magic(super) != BTRFS_MAGIC) {
  2945. brelse(bh);
  2946. return -EINVAL;
  2947. }
  2948. *bh_ret = bh;
  2949. return 0;
  2950. }
  2951. struct buffer_head *btrfs_read_dev_super(struct block_device *bdev)
  2952. {
  2953. struct buffer_head *bh;
  2954. struct buffer_head *latest = NULL;
  2955. struct btrfs_super_block *super;
  2956. int i;
  2957. u64 transid = 0;
  2958. int ret = -EINVAL;
  2959. /* we would like to check all the supers, but that would make
  2960. * a btrfs mount succeed after a mkfs from a different FS.
  2961. * So, we need to add a special mount option to scan for
  2962. * later supers, using BTRFS_SUPER_MIRROR_MAX instead
  2963. */
  2964. for (i = 0; i < 1; i++) {
  2965. ret = btrfs_read_dev_one_super(bdev, i, &bh);
  2966. if (ret)
  2967. continue;
  2968. super = (struct btrfs_super_block *)bh->b_data;
  2969. if (!latest || btrfs_super_generation(super) > transid) {
  2970. brelse(latest);
  2971. latest = bh;
  2972. transid = btrfs_super_generation(super);
  2973. } else {
  2974. brelse(bh);
  2975. }
  2976. }
  2977. if (!latest)
  2978. return ERR_PTR(ret);
  2979. return latest;
  2980. }
  2981. /*
  2982. * Write superblock @sb to the @device. Do not wait for completion, all the
  2983. * buffer heads we write are pinned.
  2984. *
  2985. * Write @max_mirrors copies of the superblock, where 0 means default that fit
  2986. * the expected device size at commit time. Note that max_mirrors must be
  2987. * same for write and wait phases.
  2988. *
  2989. * Return number of errors when buffer head is not found or submission fails.
  2990. */
  2991. static int write_dev_supers(struct btrfs_device *device,
  2992. struct btrfs_super_block *sb, int max_mirrors)
  2993. {
  2994. struct buffer_head *bh;
  2995. int i;
  2996. int ret;
  2997. int errors = 0;
  2998. u32 crc;
  2999. u64 bytenr;
  3000. int op_flags;
  3001. if (max_mirrors == 0)
  3002. max_mirrors = BTRFS_SUPER_MIRROR_MAX;
  3003. for (i = 0; i < max_mirrors; i++) {
  3004. bytenr = btrfs_sb_offset(i);
  3005. if (bytenr + BTRFS_SUPER_INFO_SIZE >=
  3006. device->commit_total_bytes)
  3007. break;
  3008. btrfs_set_super_bytenr(sb, bytenr);
  3009. crc = ~(u32)0;
  3010. crc = btrfs_csum_data((const char *)sb + BTRFS_CSUM_SIZE, crc,
  3011. BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
  3012. btrfs_csum_final(crc, sb->csum);
  3013. /* One reference for us, and we leave it for the caller */
  3014. bh = __getblk(device->bdev, bytenr / BTRFS_BDEV_BLOCKSIZE,
  3015. BTRFS_SUPER_INFO_SIZE);
  3016. if (!bh) {
  3017. btrfs_err(device->fs_info,
  3018. "couldn't get super buffer head for bytenr %llu",
  3019. bytenr);
  3020. errors++;
  3021. continue;
  3022. }
  3023. memcpy(bh->b_data, sb, BTRFS_SUPER_INFO_SIZE);
  3024. /* one reference for submit_bh */
  3025. get_bh(bh);
  3026. set_buffer_uptodate(bh);
  3027. lock_buffer(bh);
  3028. bh->b_end_io = btrfs_end_buffer_write_sync;
  3029. bh->b_private = device;
  3030. /*
  3031. * we fua the first super. The others we allow
  3032. * to go down lazy.
  3033. */
  3034. op_flags = REQ_SYNC | REQ_META | REQ_PRIO;
  3035. if (i == 0 && !btrfs_test_opt(device->fs_info, NOBARRIER))
  3036. op_flags |= REQ_FUA;
  3037. ret = btrfsic_submit_bh(REQ_OP_WRITE, op_flags, bh);
  3038. if (ret)
  3039. errors++;
  3040. }
  3041. return errors < i ? 0 : -1;
  3042. }
  3043. /*
  3044. * Wait for write completion of superblocks done by write_dev_supers,
  3045. * @max_mirrors same for write and wait phases.
  3046. *
  3047. * Return number of errors when buffer head is not found or not marked up to
  3048. * date.
  3049. */
  3050. static int wait_dev_supers(struct btrfs_device *device, int max_mirrors)
  3051. {
  3052. struct buffer_head *bh;
  3053. int i;
  3054. int errors = 0;
  3055. bool primary_failed = false;
  3056. u64 bytenr;
  3057. if (max_mirrors == 0)
  3058. max_mirrors = BTRFS_SUPER_MIRROR_MAX;
  3059. for (i = 0; i < max_mirrors; i++) {
  3060. bytenr = btrfs_sb_offset(i);
  3061. if (bytenr + BTRFS_SUPER_INFO_SIZE >=
  3062. device->commit_total_bytes)
  3063. break;
  3064. bh = __find_get_block(device->bdev,
  3065. bytenr / BTRFS_BDEV_BLOCKSIZE,
  3066. BTRFS_SUPER_INFO_SIZE);
  3067. if (!bh) {
  3068. errors++;
  3069. if (i == 0)
  3070. primary_failed = true;
  3071. continue;
  3072. }
  3073. wait_on_buffer(bh);
  3074. if (!buffer_uptodate(bh)) {
  3075. errors++;
  3076. if (i == 0)
  3077. primary_failed = true;
  3078. }
  3079. /* drop our reference */
  3080. brelse(bh);
  3081. /* drop the reference from the writing run */
  3082. brelse(bh);
  3083. }
  3084. /* log error, force error return */
  3085. if (primary_failed) {
  3086. btrfs_err(device->fs_info, "error writing primary super block to device %llu",
  3087. device->devid);
  3088. return -1;
  3089. }
  3090. return errors < i ? 0 : -1;
  3091. }
  3092. /*
  3093. * endio for the write_dev_flush, this will wake anyone waiting
  3094. * for the barrier when it is done
  3095. */
  3096. static void btrfs_end_empty_barrier(struct bio *bio)
  3097. {
  3098. complete(bio->bi_private);
  3099. }
  3100. /*
  3101. * Submit a flush request to the device if it supports it. Error handling is
  3102. * done in the waiting counterpart.
  3103. */
  3104. static void write_dev_flush(struct btrfs_device *device)
  3105. {
  3106. struct request_queue *q = bdev_get_queue(device->bdev);
  3107. struct bio *bio = device->flush_bio;
  3108. if (!test_bit(QUEUE_FLAG_WC, &q->queue_flags))
  3109. return;
  3110. bio_reset(bio);
  3111. bio->bi_end_io = btrfs_end_empty_barrier;
  3112. bio_set_dev(bio, device->bdev);
  3113. bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH;
  3114. init_completion(&device->flush_wait);
  3115. bio->bi_private = &device->flush_wait;
  3116. btrfsic_submit_bio(bio);
  3117. set_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state);
  3118. }
  3119. /*
  3120. * If the flush bio has been submitted by write_dev_flush, wait for it.
  3121. */
  3122. static blk_status_t wait_dev_flush(struct btrfs_device *device)
  3123. {
  3124. struct bio *bio = device->flush_bio;
  3125. if (!test_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state))
  3126. return BLK_STS_OK;
  3127. clear_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state);
  3128. wait_for_completion_io(&device->flush_wait);
  3129. return bio->bi_status;
  3130. }
  3131. static int check_barrier_error(struct btrfs_fs_info *fs_info)
  3132. {
  3133. if (!btrfs_check_rw_degradable(fs_info, NULL))
  3134. return -EIO;
  3135. return 0;
  3136. }
  3137. /*
  3138. * send an empty flush down to each device in parallel,
  3139. * then wait for them
  3140. */
  3141. static int barrier_all_devices(struct btrfs_fs_info *info)
  3142. {
  3143. struct list_head *head;
  3144. struct btrfs_device *dev;
  3145. int errors_wait = 0;
  3146. blk_status_t ret;
  3147. lockdep_assert_held(&info->fs_devices->device_list_mutex);
  3148. /* send down all the barriers */
  3149. head = &info->fs_devices->devices;
  3150. list_for_each_entry(dev, head, dev_list) {
  3151. if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state))
  3152. continue;
  3153. if (!dev->bdev)
  3154. continue;
  3155. if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
  3156. !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
  3157. continue;
  3158. write_dev_flush(dev);
  3159. dev->last_flush_error = BLK_STS_OK;
  3160. }
  3161. /* wait for all the barriers */
  3162. list_for_each_entry(dev, head, dev_list) {
  3163. if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state))
  3164. continue;
  3165. if (!dev->bdev) {
  3166. errors_wait++;
  3167. continue;
  3168. }
  3169. if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
  3170. !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
  3171. continue;
  3172. ret = wait_dev_flush(dev);
  3173. if (ret) {
  3174. dev->last_flush_error = ret;
  3175. btrfs_dev_stat_inc_and_print(dev,
  3176. BTRFS_DEV_STAT_FLUSH_ERRS);
  3177. errors_wait++;
  3178. }
  3179. }
  3180. if (errors_wait) {
  3181. /*
  3182. * At some point we need the status of all disks
  3183. * to arrive at the volume status. So error checking
  3184. * is being pushed to a separate loop.
  3185. */
  3186. return check_barrier_error(info);
  3187. }
  3188. return 0;
  3189. }
  3190. int btrfs_get_num_tolerated_disk_barrier_failures(u64 flags)
  3191. {
  3192. int raid_type;
  3193. int min_tolerated = INT_MAX;
  3194. if ((flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 ||
  3195. (flags & BTRFS_AVAIL_ALLOC_BIT_SINGLE))
  3196. min_tolerated = min(min_tolerated,
  3197. btrfs_raid_array[BTRFS_RAID_SINGLE].
  3198. tolerated_failures);
  3199. for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) {
  3200. if (raid_type == BTRFS_RAID_SINGLE)
  3201. continue;
  3202. if (!(flags & btrfs_raid_array[raid_type].bg_flag))
  3203. continue;
  3204. min_tolerated = min(min_tolerated,
  3205. btrfs_raid_array[raid_type].
  3206. tolerated_failures);
  3207. }
  3208. if (min_tolerated == INT_MAX) {
  3209. pr_warn("BTRFS: unknown raid flag: %llu", flags);
  3210. min_tolerated = 0;
  3211. }
  3212. return min_tolerated;
  3213. }
  3214. int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors)
  3215. {
  3216. struct list_head *head;
  3217. struct btrfs_device *dev;
  3218. struct btrfs_super_block *sb;
  3219. struct btrfs_dev_item *dev_item;
  3220. int ret;
  3221. int do_barriers;
  3222. int max_errors;
  3223. int total_errors = 0;
  3224. u64 flags;
  3225. do_barriers = !btrfs_test_opt(fs_info, NOBARRIER);
  3226. /*
  3227. * max_mirrors == 0 indicates we're from commit_transaction,
  3228. * not from fsync where the tree roots in fs_info have not
  3229. * been consistent on disk.
  3230. */
  3231. if (max_mirrors == 0)
  3232. backup_super_roots(fs_info);
  3233. sb = fs_info->super_for_commit;
  3234. dev_item = &sb->dev_item;
  3235. mutex_lock(&fs_info->fs_devices->device_list_mutex);
  3236. head = &fs_info->fs_devices->devices;
  3237. max_errors = btrfs_super_num_devices(fs_info->super_copy) - 1;
  3238. if (do_barriers) {
  3239. ret = barrier_all_devices(fs_info);
  3240. if (ret) {
  3241. mutex_unlock(
  3242. &fs_info->fs_devices->device_list_mutex);
  3243. btrfs_handle_fs_error(fs_info, ret,
  3244. "errors while submitting device barriers.");
  3245. return ret;
  3246. }
  3247. }
  3248. list_for_each_entry(dev, head, dev_list) {
  3249. if (!dev->bdev) {
  3250. total_errors++;
  3251. continue;
  3252. }
  3253. if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
  3254. !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
  3255. continue;
  3256. btrfs_set_stack_device_generation(dev_item, 0);
  3257. btrfs_set_stack_device_type(dev_item, dev->type);
  3258. btrfs_set_stack_device_id(dev_item, dev->devid);
  3259. btrfs_set_stack_device_total_bytes(dev_item,
  3260. dev->commit_total_bytes);
  3261. btrfs_set_stack_device_bytes_used(dev_item,
  3262. dev->commit_bytes_used);
  3263. btrfs_set_stack_device_io_align(dev_item, dev->io_align);
  3264. btrfs_set_stack_device_io_width(dev_item, dev->io_width);
  3265. btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
  3266. memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
  3267. memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_FSID_SIZE);
  3268. flags = btrfs_super_flags(sb);
  3269. btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
  3270. ret = btrfs_validate_write_super(fs_info, sb);
  3271. if (ret < 0) {
  3272. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  3273. btrfs_handle_fs_error(fs_info, -EUCLEAN,
  3274. "unexpected superblock corruption detected");
  3275. return -EUCLEAN;
  3276. }
  3277. ret = write_dev_supers(dev, sb, max_mirrors);
  3278. if (ret)
  3279. total_errors++;
  3280. }
  3281. if (total_errors > max_errors) {
  3282. btrfs_err(fs_info, "%d errors while writing supers",
  3283. total_errors);
  3284. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  3285. /* FUA is masked off if unsupported and can't be the reason */
  3286. btrfs_handle_fs_error(fs_info, -EIO,
  3287. "%d errors while writing supers",
  3288. total_errors);
  3289. return -EIO;
  3290. }
  3291. total_errors = 0;
  3292. list_for_each_entry(dev, head, dev_list) {
  3293. if (!dev->bdev)
  3294. continue;
  3295. if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
  3296. !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
  3297. continue;
  3298. ret = wait_dev_supers(dev, max_mirrors);
  3299. if (ret)
  3300. total_errors++;
  3301. }
  3302. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  3303. if (total_errors > max_errors) {
  3304. btrfs_handle_fs_error(fs_info, -EIO,
  3305. "%d errors while writing supers",
  3306. total_errors);
  3307. return -EIO;
  3308. }
  3309. return 0;
  3310. }
  3311. /* Drop a fs root from the radix tree and free it. */
  3312. void btrfs_drop_and_free_fs_root(struct btrfs_fs_info *fs_info,
  3313. struct btrfs_root *root)
  3314. {
  3315. spin_lock(&fs_info->fs_roots_radix_lock);
  3316. radix_tree_delete(&fs_info->fs_roots_radix,
  3317. (unsigned long)root->root_key.objectid);
  3318. spin_unlock(&fs_info->fs_roots_radix_lock);
  3319. if (btrfs_root_refs(&root->root_item) == 0)
  3320. synchronize_srcu(&fs_info->subvol_srcu);
  3321. if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
  3322. btrfs_free_log(NULL, root);
  3323. if (root->reloc_root) {
  3324. free_extent_buffer(root->reloc_root->node);
  3325. free_extent_buffer(root->reloc_root->commit_root);
  3326. btrfs_put_fs_root(root->reloc_root);
  3327. root->reloc_root = NULL;
  3328. }
  3329. }
  3330. if (root->free_ino_pinned)
  3331. __btrfs_remove_free_space_cache(root->free_ino_pinned);
  3332. if (root->free_ino_ctl)
  3333. __btrfs_remove_free_space_cache(root->free_ino_ctl);
  3334. btrfs_free_fs_root(root);
  3335. }
  3336. void btrfs_free_fs_root(struct btrfs_root *root)
  3337. {
  3338. iput(root->ino_cache_inode);
  3339. WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree));
  3340. if (root->anon_dev)
  3341. free_anon_bdev(root->anon_dev);
  3342. if (root->subv_writers)
  3343. btrfs_free_subvolume_writers(root->subv_writers);
  3344. free_extent_buffer(root->node);
  3345. free_extent_buffer(root->commit_root);
  3346. kfree(root->free_ino_ctl);
  3347. kfree(root->free_ino_pinned);
  3348. btrfs_put_fs_root(root);
  3349. }
  3350. int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info)
  3351. {
  3352. u64 root_objectid = 0;
  3353. struct btrfs_root *gang[8];
  3354. int i = 0;
  3355. int err = 0;
  3356. unsigned int ret = 0;
  3357. int index;
  3358. while (1) {
  3359. index = srcu_read_lock(&fs_info->subvol_srcu);
  3360. ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
  3361. (void **)gang, root_objectid,
  3362. ARRAY_SIZE(gang));
  3363. if (!ret) {
  3364. srcu_read_unlock(&fs_info->subvol_srcu, index);
  3365. break;
  3366. }
  3367. root_objectid = gang[ret - 1]->root_key.objectid + 1;
  3368. for (i = 0; i < ret; i++) {
  3369. /* Avoid to grab roots in dead_roots */
  3370. if (btrfs_root_refs(&gang[i]->root_item) == 0) {
  3371. gang[i] = NULL;
  3372. continue;
  3373. }
  3374. /* grab all the search result for later use */
  3375. gang[i] = btrfs_grab_fs_root(gang[i]);
  3376. }
  3377. srcu_read_unlock(&fs_info->subvol_srcu, index);
  3378. for (i = 0; i < ret; i++) {
  3379. if (!gang[i])
  3380. continue;
  3381. root_objectid = gang[i]->root_key.objectid;
  3382. err = btrfs_orphan_cleanup(gang[i]);
  3383. if (err)
  3384. break;
  3385. btrfs_put_fs_root(gang[i]);
  3386. }
  3387. root_objectid++;
  3388. }
  3389. /* release the uncleaned roots due to error */
  3390. for (; i < ret; i++) {
  3391. if (gang[i])
  3392. btrfs_put_fs_root(gang[i]);
  3393. }
  3394. return err;
  3395. }
  3396. int btrfs_commit_super(struct btrfs_fs_info *fs_info)
  3397. {
  3398. struct btrfs_root *root = fs_info->tree_root;
  3399. struct btrfs_trans_handle *trans;
  3400. mutex_lock(&fs_info->cleaner_mutex);
  3401. btrfs_run_delayed_iputs(fs_info);
  3402. mutex_unlock(&fs_info->cleaner_mutex);
  3403. wake_up_process(fs_info->cleaner_kthread);
  3404. /* wait until ongoing cleanup work done */
  3405. down_write(&fs_info->cleanup_work_sem);
  3406. up_write(&fs_info->cleanup_work_sem);
  3407. trans = btrfs_join_transaction(root);
  3408. if (IS_ERR(trans))
  3409. return PTR_ERR(trans);
  3410. return btrfs_commit_transaction(trans);
  3411. }
  3412. void close_ctree(struct btrfs_fs_info *fs_info)
  3413. {
  3414. int ret;
  3415. set_bit(BTRFS_FS_CLOSING_START, &fs_info->flags);
  3416. /*
  3417. * We don't want the cleaner to start new transactions, add more delayed
  3418. * iputs, etc. while we're closing. We can't use kthread_stop() yet
  3419. * because that frees the task_struct, and the transaction kthread might
  3420. * still try to wake up the cleaner.
  3421. */
  3422. kthread_park(fs_info->cleaner_kthread);
  3423. /* wait for the qgroup rescan worker to stop */
  3424. btrfs_qgroup_wait_for_completion(fs_info, false);
  3425. /* wait for the uuid_scan task to finish */
  3426. down(&fs_info->uuid_tree_rescan_sem);
  3427. /* avoid complains from lockdep et al., set sem back to initial state */
  3428. up(&fs_info->uuid_tree_rescan_sem);
  3429. /* pause restriper - we want to resume on mount */
  3430. btrfs_pause_balance(fs_info);
  3431. btrfs_dev_replace_suspend_for_unmount(fs_info);
  3432. btrfs_scrub_cancel(fs_info);
  3433. /* wait for any defraggers to finish */
  3434. wait_event(fs_info->transaction_wait,
  3435. (atomic_read(&fs_info->defrag_running) == 0));
  3436. /* clear out the rbtree of defraggable inodes */
  3437. btrfs_cleanup_defrag_inodes(fs_info);
  3438. cancel_work_sync(&fs_info->async_reclaim_work);
  3439. if (!sb_rdonly(fs_info->sb)) {
  3440. /*
  3441. * The cleaner kthread is stopped, so do one final pass over
  3442. * unused block groups.
  3443. */
  3444. btrfs_delete_unused_bgs(fs_info);
  3445. /*
  3446. * There might be existing delayed inode workers still running
  3447. * and holding an empty delayed inode item. We must wait for
  3448. * them to complete first because they can create a transaction.
  3449. * This happens when someone calls btrfs_balance_delayed_items()
  3450. * and then a transaction commit runs the same delayed nodes
  3451. * before any delayed worker has done something with the nodes.
  3452. * We must wait for any worker here and not at transaction
  3453. * commit time since that could cause a deadlock.
  3454. * This is a very rare case.
  3455. */
  3456. btrfs_flush_workqueue(fs_info->delayed_workers);
  3457. ret = btrfs_commit_super(fs_info);
  3458. if (ret)
  3459. btrfs_err(fs_info, "commit super ret %d", ret);
  3460. }
  3461. if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state) ||
  3462. test_bit(BTRFS_FS_STATE_TRANS_ABORTED, &fs_info->fs_state))
  3463. btrfs_error_commit_super(fs_info);
  3464. kthread_stop(fs_info->transaction_kthread);
  3465. kthread_stop(fs_info->cleaner_kthread);
  3466. set_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags);
  3467. btrfs_free_qgroup_config(fs_info);
  3468. ASSERT(list_empty(&fs_info->delalloc_roots));
  3469. if (percpu_counter_sum(&fs_info->delalloc_bytes)) {
  3470. btrfs_info(fs_info, "at unmount delalloc count %lld",
  3471. percpu_counter_sum(&fs_info->delalloc_bytes));
  3472. }
  3473. btrfs_sysfs_remove_mounted(fs_info);
  3474. btrfs_sysfs_remove_fsid(fs_info->fs_devices);
  3475. btrfs_free_fs_roots(fs_info);
  3476. btrfs_put_block_group_cache(fs_info);
  3477. /*
  3478. * we must make sure there is not any read request to
  3479. * submit after we stopping all workers.
  3480. */
  3481. invalidate_inode_pages2(fs_info->btree_inode->i_mapping);
  3482. btrfs_stop_all_workers(fs_info);
  3483. clear_bit(BTRFS_FS_OPEN, &fs_info->flags);
  3484. free_root_pointers(fs_info, true);
  3485. /*
  3486. * We must free the block groups after dropping the fs_roots as we could
  3487. * have had an IO error and have left over tree log blocks that aren't
  3488. * cleaned up until the fs roots are freed. This makes the block group
  3489. * accounting appear to be wrong because there's pending reserved bytes,
  3490. * so make sure we do the block group cleanup afterwards.
  3491. */
  3492. btrfs_free_block_groups(fs_info);
  3493. iput(fs_info->btree_inode);
  3494. #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
  3495. if (btrfs_test_opt(fs_info, CHECK_INTEGRITY))
  3496. btrfsic_unmount(fs_info->fs_devices);
  3497. #endif
  3498. btrfs_close_devices(fs_info->fs_devices);
  3499. btrfs_mapping_tree_free(&fs_info->mapping_tree);
  3500. percpu_counter_destroy(&fs_info->dirty_metadata_bytes);
  3501. percpu_counter_destroy(&fs_info->delalloc_bytes);
  3502. percpu_counter_destroy(&fs_info->bio_counter);
  3503. cleanup_srcu_struct(&fs_info->subvol_srcu);
  3504. btrfs_free_stripe_hash_table(fs_info);
  3505. btrfs_free_ref_cache(fs_info);
  3506. while (!list_empty(&fs_info->pinned_chunks)) {
  3507. struct extent_map *em;
  3508. em = list_first_entry(&fs_info->pinned_chunks,
  3509. struct extent_map, list);
  3510. list_del_init(&em->list);
  3511. free_extent_map(em);
  3512. }
  3513. }
  3514. int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid,
  3515. int atomic)
  3516. {
  3517. int ret;
  3518. struct inode *btree_inode = buf->pages[0]->mapping->host;
  3519. ret = extent_buffer_uptodate(buf);
  3520. if (!ret)
  3521. return ret;
  3522. ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf,
  3523. parent_transid, atomic);
  3524. if (ret == -EAGAIN)
  3525. return ret;
  3526. return !ret;
  3527. }
  3528. void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
  3529. {
  3530. struct btrfs_fs_info *fs_info;
  3531. struct btrfs_root *root;
  3532. u64 transid = btrfs_header_generation(buf);
  3533. int was_dirty;
  3534. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  3535. /*
  3536. * This is a fast path so only do this check if we have sanity tests
  3537. * enabled. Normal people shouldn't be using umapped buffers as dirty
  3538. * outside of the sanity tests.
  3539. */
  3540. if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &buf->bflags)))
  3541. return;
  3542. #endif
  3543. root = BTRFS_I(buf->pages[0]->mapping->host)->root;
  3544. fs_info = root->fs_info;
  3545. btrfs_assert_tree_locked(buf);
  3546. if (transid != fs_info->generation)
  3547. WARN(1, KERN_CRIT "btrfs transid mismatch buffer %llu, found %llu running %llu\n",
  3548. buf->start, transid, fs_info->generation);
  3549. was_dirty = set_extent_buffer_dirty(buf);
  3550. if (!was_dirty)
  3551. percpu_counter_add_batch(&fs_info->dirty_metadata_bytes,
  3552. buf->len,
  3553. fs_info->dirty_metadata_batch);
  3554. #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
  3555. /*
  3556. * Since btrfs_mark_buffer_dirty() can be called with item pointer set
  3557. * but item data not updated.
  3558. * So here we should only check item pointers, not item data.
  3559. */
  3560. if (btrfs_header_level(buf) == 0 &&
  3561. btrfs_check_leaf_relaxed(fs_info, buf)) {
  3562. btrfs_print_leaf(buf);
  3563. ASSERT(0);
  3564. }
  3565. #endif
  3566. }
  3567. static void __btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info,
  3568. int flush_delayed)
  3569. {
  3570. /*
  3571. * looks as though older kernels can get into trouble with
  3572. * this code, they end up stuck in balance_dirty_pages forever
  3573. */
  3574. int ret;
  3575. if (current->flags & PF_MEMALLOC)
  3576. return;
  3577. if (flush_delayed)
  3578. btrfs_balance_delayed_items(fs_info);
  3579. ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes,
  3580. BTRFS_DIRTY_METADATA_THRESH,
  3581. fs_info->dirty_metadata_batch);
  3582. if (ret > 0) {
  3583. balance_dirty_pages_ratelimited(fs_info->btree_inode->i_mapping);
  3584. }
  3585. }
  3586. void btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info)
  3587. {
  3588. __btrfs_btree_balance_dirty(fs_info, 1);
  3589. }
  3590. void btrfs_btree_balance_dirty_nodelay(struct btrfs_fs_info *fs_info)
  3591. {
  3592. __btrfs_btree_balance_dirty(fs_info, 0);
  3593. }
  3594. int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid, int level,
  3595. struct btrfs_key *first_key)
  3596. {
  3597. struct btrfs_root *root = BTRFS_I(buf->pages[0]->mapping->host)->root;
  3598. struct btrfs_fs_info *fs_info = root->fs_info;
  3599. return btree_read_extent_buffer_pages(fs_info, buf, parent_transid,
  3600. level, first_key);
  3601. }
  3602. static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info)
  3603. {
  3604. /* cleanup FS via transaction */
  3605. btrfs_cleanup_transaction(fs_info);
  3606. mutex_lock(&fs_info->cleaner_mutex);
  3607. btrfs_run_delayed_iputs(fs_info);
  3608. mutex_unlock(&fs_info->cleaner_mutex);
  3609. down_write(&fs_info->cleanup_work_sem);
  3610. up_write(&fs_info->cleanup_work_sem);
  3611. }
  3612. static void btrfs_destroy_ordered_extents(struct btrfs_root *root)
  3613. {
  3614. struct btrfs_ordered_extent *ordered;
  3615. spin_lock(&root->ordered_extent_lock);
  3616. /*
  3617. * This will just short circuit the ordered completion stuff which will
  3618. * make sure the ordered extent gets properly cleaned up.
  3619. */
  3620. list_for_each_entry(ordered, &root->ordered_extents,
  3621. root_extent_list)
  3622. set_bit(BTRFS_ORDERED_IOERR, &ordered->flags);
  3623. spin_unlock(&root->ordered_extent_lock);
  3624. }
  3625. static void btrfs_destroy_all_ordered_extents(struct btrfs_fs_info *fs_info)
  3626. {
  3627. struct btrfs_root *root;
  3628. struct list_head splice;
  3629. INIT_LIST_HEAD(&splice);
  3630. spin_lock(&fs_info->ordered_root_lock);
  3631. list_splice_init(&fs_info->ordered_roots, &splice);
  3632. while (!list_empty(&splice)) {
  3633. root = list_first_entry(&splice, struct btrfs_root,
  3634. ordered_root);
  3635. list_move_tail(&root->ordered_root,
  3636. &fs_info->ordered_roots);
  3637. spin_unlock(&fs_info->ordered_root_lock);
  3638. btrfs_destroy_ordered_extents(root);
  3639. cond_resched();
  3640. spin_lock(&fs_info->ordered_root_lock);
  3641. }
  3642. spin_unlock(&fs_info->ordered_root_lock);
  3643. /*
  3644. * We need this here because if we've been flipped read-only we won't
  3645. * get sync() from the umount, so we need to make sure any ordered
  3646. * extents that haven't had their dirty pages IO start writeout yet
  3647. * actually get run and error out properly.
  3648. */
  3649. btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
  3650. }
  3651. static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans,
  3652. struct btrfs_fs_info *fs_info)
  3653. {
  3654. struct rb_node *node;
  3655. struct btrfs_delayed_ref_root *delayed_refs;
  3656. struct btrfs_delayed_ref_node *ref;
  3657. int ret = 0;
  3658. delayed_refs = &trans->delayed_refs;
  3659. spin_lock(&delayed_refs->lock);
  3660. if (atomic_read(&delayed_refs->num_entries) == 0) {
  3661. spin_unlock(&delayed_refs->lock);
  3662. btrfs_info(fs_info, "delayed_refs has NO entry");
  3663. return ret;
  3664. }
  3665. while ((node = rb_first(&delayed_refs->href_root)) != NULL) {
  3666. struct btrfs_delayed_ref_head *head;
  3667. struct rb_node *n;
  3668. bool pin_bytes = false;
  3669. head = rb_entry(node, struct btrfs_delayed_ref_head,
  3670. href_node);
  3671. if (!mutex_trylock(&head->mutex)) {
  3672. refcount_inc(&head->refs);
  3673. spin_unlock(&delayed_refs->lock);
  3674. mutex_lock(&head->mutex);
  3675. mutex_unlock(&head->mutex);
  3676. btrfs_put_delayed_ref_head(head);
  3677. spin_lock(&delayed_refs->lock);
  3678. continue;
  3679. }
  3680. spin_lock(&head->lock);
  3681. while ((n = rb_first(&head->ref_tree)) != NULL) {
  3682. ref = rb_entry(n, struct btrfs_delayed_ref_node,
  3683. ref_node);
  3684. ref->in_tree = 0;
  3685. rb_erase(&ref->ref_node, &head->ref_tree);
  3686. RB_CLEAR_NODE(&ref->ref_node);
  3687. if (!list_empty(&ref->add_list))
  3688. list_del(&ref->add_list);
  3689. atomic_dec(&delayed_refs->num_entries);
  3690. btrfs_put_delayed_ref(ref);
  3691. }
  3692. if (head->must_insert_reserved)
  3693. pin_bytes = true;
  3694. btrfs_free_delayed_extent_op(head->extent_op);
  3695. delayed_refs->num_heads--;
  3696. if (head->processing == 0)
  3697. delayed_refs->num_heads_ready--;
  3698. atomic_dec(&delayed_refs->num_entries);
  3699. rb_erase(&head->href_node, &delayed_refs->href_root);
  3700. RB_CLEAR_NODE(&head->href_node);
  3701. spin_unlock(&head->lock);
  3702. spin_unlock(&delayed_refs->lock);
  3703. mutex_unlock(&head->mutex);
  3704. if (pin_bytes)
  3705. btrfs_pin_extent(fs_info, head->bytenr,
  3706. head->num_bytes, 1);
  3707. btrfs_put_delayed_ref_head(head);
  3708. cond_resched();
  3709. spin_lock(&delayed_refs->lock);
  3710. }
  3711. spin_unlock(&delayed_refs->lock);
  3712. return ret;
  3713. }
  3714. static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root)
  3715. {
  3716. struct btrfs_inode *btrfs_inode;
  3717. struct list_head splice;
  3718. INIT_LIST_HEAD(&splice);
  3719. spin_lock(&root->delalloc_lock);
  3720. list_splice_init(&root->delalloc_inodes, &splice);
  3721. while (!list_empty(&splice)) {
  3722. struct inode *inode = NULL;
  3723. btrfs_inode = list_first_entry(&splice, struct btrfs_inode,
  3724. delalloc_inodes);
  3725. __btrfs_del_delalloc_inode(root, btrfs_inode);
  3726. spin_unlock(&root->delalloc_lock);
  3727. /*
  3728. * Make sure we get a live inode and that it'll not disappear
  3729. * meanwhile.
  3730. */
  3731. inode = igrab(&btrfs_inode->vfs_inode);
  3732. if (inode) {
  3733. invalidate_inode_pages2(inode->i_mapping);
  3734. iput(inode);
  3735. }
  3736. spin_lock(&root->delalloc_lock);
  3737. }
  3738. spin_unlock(&root->delalloc_lock);
  3739. }
  3740. static void btrfs_destroy_all_delalloc_inodes(struct btrfs_fs_info *fs_info)
  3741. {
  3742. struct btrfs_root *root;
  3743. struct list_head splice;
  3744. INIT_LIST_HEAD(&splice);
  3745. spin_lock(&fs_info->delalloc_root_lock);
  3746. list_splice_init(&fs_info->delalloc_roots, &splice);
  3747. while (!list_empty(&splice)) {
  3748. root = list_first_entry(&splice, struct btrfs_root,
  3749. delalloc_root);
  3750. root = btrfs_grab_fs_root(root);
  3751. BUG_ON(!root);
  3752. spin_unlock(&fs_info->delalloc_root_lock);
  3753. btrfs_destroy_delalloc_inodes(root);
  3754. btrfs_put_fs_root(root);
  3755. spin_lock(&fs_info->delalloc_root_lock);
  3756. }
  3757. spin_unlock(&fs_info->delalloc_root_lock);
  3758. }
  3759. static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info,
  3760. struct extent_io_tree *dirty_pages,
  3761. int mark)
  3762. {
  3763. int ret;
  3764. struct extent_buffer *eb;
  3765. u64 start = 0;
  3766. u64 end;
  3767. while (1) {
  3768. ret = find_first_extent_bit(dirty_pages, start, &start, &end,
  3769. mark, NULL);
  3770. if (ret)
  3771. break;
  3772. clear_extent_bits(dirty_pages, start, end, mark);
  3773. while (start <= end) {
  3774. eb = find_extent_buffer(fs_info, start);
  3775. start += fs_info->nodesize;
  3776. if (!eb)
  3777. continue;
  3778. wait_on_extent_buffer_writeback(eb);
  3779. if (test_and_clear_bit(EXTENT_BUFFER_DIRTY,
  3780. &eb->bflags))
  3781. clear_extent_buffer_dirty(eb);
  3782. free_extent_buffer_stale(eb);
  3783. }
  3784. }
  3785. return ret;
  3786. }
  3787. static int btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info,
  3788. struct extent_io_tree *pinned_extents)
  3789. {
  3790. struct extent_io_tree *unpin;
  3791. u64 start;
  3792. u64 end;
  3793. int ret;
  3794. bool loop = true;
  3795. unpin = pinned_extents;
  3796. again:
  3797. while (1) {
  3798. struct extent_state *cached_state = NULL;
  3799. /*
  3800. * The btrfs_finish_extent_commit() may get the same range as
  3801. * ours between find_first_extent_bit and clear_extent_dirty.
  3802. * Hence, hold the unused_bg_unpin_mutex to avoid double unpin
  3803. * the same extent range.
  3804. */
  3805. mutex_lock(&fs_info->unused_bg_unpin_mutex);
  3806. ret = find_first_extent_bit(unpin, 0, &start, &end,
  3807. EXTENT_DIRTY, &cached_state);
  3808. if (ret) {
  3809. mutex_unlock(&fs_info->unused_bg_unpin_mutex);
  3810. break;
  3811. }
  3812. clear_extent_dirty(unpin, start, end, &cached_state);
  3813. free_extent_state(cached_state);
  3814. btrfs_error_unpin_extent_range(fs_info, start, end);
  3815. mutex_unlock(&fs_info->unused_bg_unpin_mutex);
  3816. cond_resched();
  3817. }
  3818. if (loop) {
  3819. if (unpin == &fs_info->freed_extents[0])
  3820. unpin = &fs_info->freed_extents[1];
  3821. else
  3822. unpin = &fs_info->freed_extents[0];
  3823. loop = false;
  3824. goto again;
  3825. }
  3826. return 0;
  3827. }
  3828. static void btrfs_cleanup_bg_io(struct btrfs_block_group_cache *cache)
  3829. {
  3830. struct inode *inode;
  3831. inode = cache->io_ctl.inode;
  3832. if (inode) {
  3833. invalidate_inode_pages2(inode->i_mapping);
  3834. BTRFS_I(inode)->generation = 0;
  3835. cache->io_ctl.inode = NULL;
  3836. iput(inode);
  3837. }
  3838. ASSERT(cache->io_ctl.pages == NULL);
  3839. btrfs_put_block_group(cache);
  3840. }
  3841. void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans,
  3842. struct btrfs_fs_info *fs_info)
  3843. {
  3844. struct btrfs_block_group_cache *cache;
  3845. spin_lock(&cur_trans->dirty_bgs_lock);
  3846. while (!list_empty(&cur_trans->dirty_bgs)) {
  3847. cache = list_first_entry(&cur_trans->dirty_bgs,
  3848. struct btrfs_block_group_cache,
  3849. dirty_list);
  3850. if (!list_empty(&cache->io_list)) {
  3851. spin_unlock(&cur_trans->dirty_bgs_lock);
  3852. list_del_init(&cache->io_list);
  3853. btrfs_cleanup_bg_io(cache);
  3854. spin_lock(&cur_trans->dirty_bgs_lock);
  3855. }
  3856. list_del_init(&cache->dirty_list);
  3857. spin_lock(&cache->lock);
  3858. cache->disk_cache_state = BTRFS_DC_ERROR;
  3859. spin_unlock(&cache->lock);
  3860. spin_unlock(&cur_trans->dirty_bgs_lock);
  3861. btrfs_put_block_group(cache);
  3862. spin_lock(&cur_trans->dirty_bgs_lock);
  3863. }
  3864. spin_unlock(&cur_trans->dirty_bgs_lock);
  3865. /*
  3866. * Refer to the definition of io_bgs member for details why it's safe
  3867. * to use it without any locking
  3868. */
  3869. while (!list_empty(&cur_trans->io_bgs)) {
  3870. cache = list_first_entry(&cur_trans->io_bgs,
  3871. struct btrfs_block_group_cache,
  3872. io_list);
  3873. list_del_init(&cache->io_list);
  3874. spin_lock(&cache->lock);
  3875. cache->disk_cache_state = BTRFS_DC_ERROR;
  3876. spin_unlock(&cache->lock);
  3877. btrfs_cleanup_bg_io(cache);
  3878. }
  3879. }
  3880. void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans,
  3881. struct btrfs_fs_info *fs_info)
  3882. {
  3883. btrfs_cleanup_dirty_bgs(cur_trans, fs_info);
  3884. ASSERT(list_empty(&cur_trans->dirty_bgs));
  3885. ASSERT(list_empty(&cur_trans->io_bgs));
  3886. btrfs_destroy_delayed_refs(cur_trans, fs_info);
  3887. cur_trans->state = TRANS_STATE_COMMIT_START;
  3888. wake_up(&fs_info->transaction_blocked_wait);
  3889. cur_trans->state = TRANS_STATE_UNBLOCKED;
  3890. wake_up(&fs_info->transaction_wait);
  3891. btrfs_destroy_delayed_inodes(fs_info);
  3892. btrfs_destroy_marked_extents(fs_info, &cur_trans->dirty_pages,
  3893. EXTENT_DIRTY);
  3894. btrfs_destroy_pinned_extent(fs_info,
  3895. fs_info->pinned_extents);
  3896. cur_trans->state =TRANS_STATE_COMPLETED;
  3897. wake_up(&cur_trans->commit_wait);
  3898. }
  3899. static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info)
  3900. {
  3901. struct btrfs_transaction *t;
  3902. mutex_lock(&fs_info->transaction_kthread_mutex);
  3903. spin_lock(&fs_info->trans_lock);
  3904. while (!list_empty(&fs_info->trans_list)) {
  3905. t = list_first_entry(&fs_info->trans_list,
  3906. struct btrfs_transaction, list);
  3907. if (t->state >= TRANS_STATE_COMMIT_START) {
  3908. refcount_inc(&t->use_count);
  3909. spin_unlock(&fs_info->trans_lock);
  3910. btrfs_wait_for_commit(fs_info, t->transid);
  3911. btrfs_put_transaction(t);
  3912. spin_lock(&fs_info->trans_lock);
  3913. continue;
  3914. }
  3915. if (t == fs_info->running_transaction) {
  3916. t->state = TRANS_STATE_COMMIT_DOING;
  3917. spin_unlock(&fs_info->trans_lock);
  3918. /*
  3919. * We wait for 0 num_writers since we don't hold a trans
  3920. * handle open currently for this transaction.
  3921. */
  3922. wait_event(t->writer_wait,
  3923. atomic_read(&t->num_writers) == 0);
  3924. } else {
  3925. spin_unlock(&fs_info->trans_lock);
  3926. }
  3927. btrfs_cleanup_one_transaction(t, fs_info);
  3928. spin_lock(&fs_info->trans_lock);
  3929. if (t == fs_info->running_transaction)
  3930. fs_info->running_transaction = NULL;
  3931. list_del_init(&t->list);
  3932. spin_unlock(&fs_info->trans_lock);
  3933. btrfs_put_transaction(t);
  3934. trace_btrfs_transaction_commit(fs_info->tree_root);
  3935. spin_lock(&fs_info->trans_lock);
  3936. }
  3937. spin_unlock(&fs_info->trans_lock);
  3938. btrfs_destroy_all_ordered_extents(fs_info);
  3939. btrfs_destroy_delayed_inodes(fs_info);
  3940. btrfs_assert_delayed_root_empty(fs_info);
  3941. btrfs_destroy_pinned_extent(fs_info, fs_info->pinned_extents);
  3942. btrfs_destroy_all_delalloc_inodes(fs_info);
  3943. mutex_unlock(&fs_info->transaction_kthread_mutex);
  3944. return 0;
  3945. }
  3946. static const struct extent_io_ops btree_extent_io_ops = {
  3947. /* mandatory callbacks */
  3948. .submit_bio_hook = btree_submit_bio_hook,
  3949. .readpage_end_io_hook = btree_readpage_end_io_hook,
  3950. .readpage_io_failed_hook = btree_io_failed_hook,
  3951. /* optional callbacks */
  3952. };