disk-io.c 122 KB

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