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