volumes.c 159 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213
  1. /*
  2. * Copyright (C) 2007 Oracle. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #include <linux/sched.h>
  19. #include <linux/bio.h>
  20. #include <linux/slab.h>
  21. #include <linux/buffer_head.h>
  22. #include <linux/blkdev.h>
  23. #include <linux/random.h>
  24. #include <linux/iocontext.h>
  25. #include <linux/capability.h>
  26. #include <linux/ratelimit.h>
  27. #include <linux/kthread.h>
  28. #include <linux/raid/pq.h>
  29. #include <linux/semaphore.h>
  30. #include <asm/div64.h>
  31. #include "compat.h"
  32. #include "ctree.h"
  33. #include "extent_map.h"
  34. #include "disk-io.h"
  35. #include "transaction.h"
  36. #include "print-tree.h"
  37. #include "volumes.h"
  38. #include "raid56.h"
  39. #include "async-thread.h"
  40. #include "check-integrity.h"
  41. #include "rcu-string.h"
  42. #include "math.h"
  43. #include "dev-replace.h"
  44. static int init_first_rw_device(struct btrfs_trans_handle *trans,
  45. struct btrfs_root *root,
  46. struct btrfs_device *device);
  47. static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
  48. static void __btrfs_reset_dev_stats(struct btrfs_device *dev);
  49. static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev);
  50. static void btrfs_dev_stat_print_on_load(struct btrfs_device *device);
  51. static DEFINE_MUTEX(uuid_mutex);
  52. static LIST_HEAD(fs_uuids);
  53. static void lock_chunks(struct btrfs_root *root)
  54. {
  55. mutex_lock(&root->fs_info->chunk_mutex);
  56. }
  57. static void unlock_chunks(struct btrfs_root *root)
  58. {
  59. mutex_unlock(&root->fs_info->chunk_mutex);
  60. }
  61. static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
  62. {
  63. struct btrfs_device *device;
  64. WARN_ON(fs_devices->opened);
  65. while (!list_empty(&fs_devices->devices)) {
  66. device = list_entry(fs_devices->devices.next,
  67. struct btrfs_device, dev_list);
  68. list_del(&device->dev_list);
  69. rcu_string_free(device->name);
  70. kfree(device);
  71. }
  72. kfree(fs_devices);
  73. }
  74. static void btrfs_kobject_uevent(struct block_device *bdev,
  75. enum kobject_action action)
  76. {
  77. int ret;
  78. ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
  79. if (ret)
  80. pr_warn("Sending event '%d' to kobject: '%s' (%p): failed\n",
  81. action,
  82. kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
  83. &disk_to_dev(bdev->bd_disk)->kobj);
  84. }
  85. void btrfs_cleanup_fs_uuids(void)
  86. {
  87. struct btrfs_fs_devices *fs_devices;
  88. while (!list_empty(&fs_uuids)) {
  89. fs_devices = list_entry(fs_uuids.next,
  90. struct btrfs_fs_devices, list);
  91. list_del(&fs_devices->list);
  92. free_fs_devices(fs_devices);
  93. }
  94. }
  95. static noinline struct btrfs_device *__find_device(struct list_head *head,
  96. u64 devid, u8 *uuid)
  97. {
  98. struct btrfs_device *dev;
  99. list_for_each_entry(dev, head, dev_list) {
  100. if (dev->devid == devid &&
  101. (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
  102. return dev;
  103. }
  104. }
  105. return NULL;
  106. }
  107. static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
  108. {
  109. struct btrfs_fs_devices *fs_devices;
  110. list_for_each_entry(fs_devices, &fs_uuids, list) {
  111. if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
  112. return fs_devices;
  113. }
  114. return NULL;
  115. }
  116. static int
  117. btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
  118. int flush, struct block_device **bdev,
  119. struct buffer_head **bh)
  120. {
  121. int ret;
  122. *bdev = blkdev_get_by_path(device_path, flags, holder);
  123. if (IS_ERR(*bdev)) {
  124. ret = PTR_ERR(*bdev);
  125. printk(KERN_INFO "btrfs: open %s failed\n", device_path);
  126. goto error;
  127. }
  128. if (flush)
  129. filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
  130. ret = set_blocksize(*bdev, 4096);
  131. if (ret) {
  132. blkdev_put(*bdev, flags);
  133. goto error;
  134. }
  135. invalidate_bdev(*bdev);
  136. *bh = btrfs_read_dev_super(*bdev);
  137. if (!*bh) {
  138. ret = -EINVAL;
  139. blkdev_put(*bdev, flags);
  140. goto error;
  141. }
  142. return 0;
  143. error:
  144. *bdev = NULL;
  145. *bh = NULL;
  146. return ret;
  147. }
  148. static void requeue_list(struct btrfs_pending_bios *pending_bios,
  149. struct bio *head, struct bio *tail)
  150. {
  151. struct bio *old_head;
  152. old_head = pending_bios->head;
  153. pending_bios->head = head;
  154. if (pending_bios->tail)
  155. tail->bi_next = old_head;
  156. else
  157. pending_bios->tail = tail;
  158. }
  159. /*
  160. * we try to collect pending bios for a device so we don't get a large
  161. * number of procs sending bios down to the same device. This greatly
  162. * improves the schedulers ability to collect and merge the bios.
  163. *
  164. * But, it also turns into a long list of bios to process and that is sure
  165. * to eventually make the worker thread block. The solution here is to
  166. * make some progress and then put this work struct back at the end of
  167. * the list if the block device is congested. This way, multiple devices
  168. * can make progress from a single worker thread.
  169. */
  170. static noinline void run_scheduled_bios(struct btrfs_device *device)
  171. {
  172. struct bio *pending;
  173. struct backing_dev_info *bdi;
  174. struct btrfs_fs_info *fs_info;
  175. struct btrfs_pending_bios *pending_bios;
  176. struct bio *tail;
  177. struct bio *cur;
  178. int again = 0;
  179. unsigned long num_run;
  180. unsigned long batch_run = 0;
  181. unsigned long limit;
  182. unsigned long last_waited = 0;
  183. int force_reg = 0;
  184. int sync_pending = 0;
  185. struct blk_plug plug;
  186. /*
  187. * this function runs all the bios we've collected for
  188. * a particular device. We don't want to wander off to
  189. * another device without first sending all of these down.
  190. * So, setup a plug here and finish it off before we return
  191. */
  192. blk_start_plug(&plug);
  193. bdi = blk_get_backing_dev_info(device->bdev);
  194. fs_info = device->dev_root->fs_info;
  195. limit = btrfs_async_submit_limit(fs_info);
  196. limit = limit * 2 / 3;
  197. loop:
  198. spin_lock(&device->io_lock);
  199. loop_lock:
  200. num_run = 0;
  201. /* take all the bios off the list at once and process them
  202. * later on (without the lock held). But, remember the
  203. * tail and other pointers so the bios can be properly reinserted
  204. * into the list if we hit congestion
  205. */
  206. if (!force_reg && device->pending_sync_bios.head) {
  207. pending_bios = &device->pending_sync_bios;
  208. force_reg = 1;
  209. } else {
  210. pending_bios = &device->pending_bios;
  211. force_reg = 0;
  212. }
  213. pending = pending_bios->head;
  214. tail = pending_bios->tail;
  215. WARN_ON(pending && !tail);
  216. /*
  217. * if pending was null this time around, no bios need processing
  218. * at all and we can stop. Otherwise it'll loop back up again
  219. * and do an additional check so no bios are missed.
  220. *
  221. * device->running_pending is used to synchronize with the
  222. * schedule_bio code.
  223. */
  224. if (device->pending_sync_bios.head == NULL &&
  225. device->pending_bios.head == NULL) {
  226. again = 0;
  227. device->running_pending = 0;
  228. } else {
  229. again = 1;
  230. device->running_pending = 1;
  231. }
  232. pending_bios->head = NULL;
  233. pending_bios->tail = NULL;
  234. spin_unlock(&device->io_lock);
  235. while (pending) {
  236. rmb();
  237. /* we want to work on both lists, but do more bios on the
  238. * sync list than the regular list
  239. */
  240. if ((num_run > 32 &&
  241. pending_bios != &device->pending_sync_bios &&
  242. device->pending_sync_bios.head) ||
  243. (num_run > 64 && pending_bios == &device->pending_sync_bios &&
  244. device->pending_bios.head)) {
  245. spin_lock(&device->io_lock);
  246. requeue_list(pending_bios, pending, tail);
  247. goto loop_lock;
  248. }
  249. cur = pending;
  250. pending = pending->bi_next;
  251. cur->bi_next = NULL;
  252. if (atomic_dec_return(&fs_info->nr_async_bios) < limit &&
  253. waitqueue_active(&fs_info->async_submit_wait))
  254. wake_up(&fs_info->async_submit_wait);
  255. BUG_ON(atomic_read(&cur->bi_cnt) == 0);
  256. /*
  257. * if we're doing the sync list, record that our
  258. * plug has some sync requests on it
  259. *
  260. * If we're doing the regular list and there are
  261. * sync requests sitting around, unplug before
  262. * we add more
  263. */
  264. if (pending_bios == &device->pending_sync_bios) {
  265. sync_pending = 1;
  266. } else if (sync_pending) {
  267. blk_finish_plug(&plug);
  268. blk_start_plug(&plug);
  269. sync_pending = 0;
  270. }
  271. btrfsic_submit_bio(cur->bi_rw, cur);
  272. num_run++;
  273. batch_run++;
  274. if (need_resched())
  275. cond_resched();
  276. /*
  277. * we made progress, there is more work to do and the bdi
  278. * is now congested. Back off and let other work structs
  279. * run instead
  280. */
  281. if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
  282. fs_info->fs_devices->open_devices > 1) {
  283. struct io_context *ioc;
  284. ioc = current->io_context;
  285. /*
  286. * the main goal here is that we don't want to
  287. * block if we're going to be able to submit
  288. * more requests without blocking.
  289. *
  290. * This code does two great things, it pokes into
  291. * the elevator code from a filesystem _and_
  292. * it makes assumptions about how batching works.
  293. */
  294. if (ioc && ioc->nr_batch_requests > 0 &&
  295. time_before(jiffies, ioc->last_waited + HZ/50UL) &&
  296. (last_waited == 0 ||
  297. ioc->last_waited == last_waited)) {
  298. /*
  299. * we want to go through our batch of
  300. * requests and stop. So, we copy out
  301. * the ioc->last_waited time and test
  302. * against it before looping
  303. */
  304. last_waited = ioc->last_waited;
  305. if (need_resched())
  306. cond_resched();
  307. continue;
  308. }
  309. spin_lock(&device->io_lock);
  310. requeue_list(pending_bios, pending, tail);
  311. device->running_pending = 1;
  312. spin_unlock(&device->io_lock);
  313. btrfs_requeue_work(&device->work);
  314. goto done;
  315. }
  316. /* unplug every 64 requests just for good measure */
  317. if (batch_run % 64 == 0) {
  318. blk_finish_plug(&plug);
  319. blk_start_plug(&plug);
  320. sync_pending = 0;
  321. }
  322. }
  323. cond_resched();
  324. if (again)
  325. goto loop;
  326. spin_lock(&device->io_lock);
  327. if (device->pending_bios.head || device->pending_sync_bios.head)
  328. goto loop_lock;
  329. spin_unlock(&device->io_lock);
  330. done:
  331. blk_finish_plug(&plug);
  332. }
  333. static void pending_bios_fn(struct btrfs_work *work)
  334. {
  335. struct btrfs_device *device;
  336. device = container_of(work, struct btrfs_device, work);
  337. run_scheduled_bios(device);
  338. }
  339. static noinline int device_list_add(const char *path,
  340. struct btrfs_super_block *disk_super,
  341. u64 devid, struct btrfs_fs_devices **fs_devices_ret)
  342. {
  343. struct btrfs_device *device;
  344. struct btrfs_fs_devices *fs_devices;
  345. struct rcu_string *name;
  346. u64 found_transid = btrfs_super_generation(disk_super);
  347. fs_devices = find_fsid(disk_super->fsid);
  348. if (!fs_devices) {
  349. fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
  350. if (!fs_devices)
  351. return -ENOMEM;
  352. INIT_LIST_HEAD(&fs_devices->devices);
  353. INIT_LIST_HEAD(&fs_devices->alloc_list);
  354. list_add(&fs_devices->list, &fs_uuids);
  355. memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
  356. fs_devices->latest_devid = devid;
  357. fs_devices->latest_trans = found_transid;
  358. mutex_init(&fs_devices->device_list_mutex);
  359. device = NULL;
  360. } else {
  361. device = __find_device(&fs_devices->devices, devid,
  362. disk_super->dev_item.uuid);
  363. }
  364. if (!device) {
  365. if (fs_devices->opened)
  366. return -EBUSY;
  367. device = kzalloc(sizeof(*device), GFP_NOFS);
  368. if (!device) {
  369. /* we can safely leave the fs_devices entry around */
  370. return -ENOMEM;
  371. }
  372. device->devid = devid;
  373. device->dev_stats_valid = 0;
  374. device->work.func = pending_bios_fn;
  375. memcpy(device->uuid, disk_super->dev_item.uuid,
  376. BTRFS_UUID_SIZE);
  377. spin_lock_init(&device->io_lock);
  378. name = rcu_string_strdup(path, GFP_NOFS);
  379. if (!name) {
  380. kfree(device);
  381. return -ENOMEM;
  382. }
  383. rcu_assign_pointer(device->name, name);
  384. INIT_LIST_HEAD(&device->dev_alloc_list);
  385. /* init readahead state */
  386. spin_lock_init(&device->reada_lock);
  387. device->reada_curr_zone = NULL;
  388. atomic_set(&device->reada_in_flight, 0);
  389. device->reada_next = 0;
  390. INIT_RADIX_TREE(&device->reada_zones, GFP_NOFS & ~__GFP_WAIT);
  391. INIT_RADIX_TREE(&device->reada_extents, GFP_NOFS & ~__GFP_WAIT);
  392. mutex_lock(&fs_devices->device_list_mutex);
  393. list_add_rcu(&device->dev_list, &fs_devices->devices);
  394. mutex_unlock(&fs_devices->device_list_mutex);
  395. device->fs_devices = fs_devices;
  396. fs_devices->num_devices++;
  397. } else if (!device->name || strcmp(device->name->str, path)) {
  398. name = rcu_string_strdup(path, GFP_NOFS);
  399. if (!name)
  400. return -ENOMEM;
  401. rcu_string_free(device->name);
  402. rcu_assign_pointer(device->name, name);
  403. if (device->missing) {
  404. fs_devices->missing_devices--;
  405. device->missing = 0;
  406. }
  407. }
  408. if (found_transid > fs_devices->latest_trans) {
  409. fs_devices->latest_devid = devid;
  410. fs_devices->latest_trans = found_transid;
  411. }
  412. *fs_devices_ret = fs_devices;
  413. return 0;
  414. }
  415. static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
  416. {
  417. struct btrfs_fs_devices *fs_devices;
  418. struct btrfs_device *device;
  419. struct btrfs_device *orig_dev;
  420. fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
  421. if (!fs_devices)
  422. return ERR_PTR(-ENOMEM);
  423. INIT_LIST_HEAD(&fs_devices->devices);
  424. INIT_LIST_HEAD(&fs_devices->alloc_list);
  425. INIT_LIST_HEAD(&fs_devices->list);
  426. mutex_init(&fs_devices->device_list_mutex);
  427. fs_devices->latest_devid = orig->latest_devid;
  428. fs_devices->latest_trans = orig->latest_trans;
  429. fs_devices->total_devices = orig->total_devices;
  430. memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
  431. /* We have held the volume lock, it is safe to get the devices. */
  432. list_for_each_entry(orig_dev, &orig->devices, dev_list) {
  433. struct rcu_string *name;
  434. device = kzalloc(sizeof(*device), GFP_NOFS);
  435. if (!device)
  436. goto error;
  437. /*
  438. * This is ok to do without rcu read locked because we hold the
  439. * uuid mutex so nothing we touch in here is going to disappear.
  440. */
  441. name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS);
  442. if (!name) {
  443. kfree(device);
  444. goto error;
  445. }
  446. rcu_assign_pointer(device->name, name);
  447. device->devid = orig_dev->devid;
  448. device->work.func = pending_bios_fn;
  449. memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
  450. spin_lock_init(&device->io_lock);
  451. INIT_LIST_HEAD(&device->dev_list);
  452. INIT_LIST_HEAD(&device->dev_alloc_list);
  453. list_add(&device->dev_list, &fs_devices->devices);
  454. device->fs_devices = fs_devices;
  455. fs_devices->num_devices++;
  456. }
  457. return fs_devices;
  458. error:
  459. free_fs_devices(fs_devices);
  460. return ERR_PTR(-ENOMEM);
  461. }
  462. void btrfs_close_extra_devices(struct btrfs_fs_info *fs_info,
  463. struct btrfs_fs_devices *fs_devices, int step)
  464. {
  465. struct btrfs_device *device, *next;
  466. struct block_device *latest_bdev = NULL;
  467. u64 latest_devid = 0;
  468. u64 latest_transid = 0;
  469. mutex_lock(&uuid_mutex);
  470. again:
  471. /* This is the initialized path, it is safe to release the devices. */
  472. list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
  473. if (device->in_fs_metadata) {
  474. if (!device->is_tgtdev_for_dev_replace &&
  475. (!latest_transid ||
  476. device->generation > latest_transid)) {
  477. latest_devid = device->devid;
  478. latest_transid = device->generation;
  479. latest_bdev = device->bdev;
  480. }
  481. continue;
  482. }
  483. if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
  484. /*
  485. * In the first step, keep the device which has
  486. * the correct fsid and the devid that is used
  487. * for the dev_replace procedure.
  488. * In the second step, the dev_replace state is
  489. * read from the device tree and it is known
  490. * whether the procedure is really active or
  491. * not, which means whether this device is
  492. * used or whether it should be removed.
  493. */
  494. if (step == 0 || device->is_tgtdev_for_dev_replace) {
  495. continue;
  496. }
  497. }
  498. if (device->bdev) {
  499. blkdev_put(device->bdev, device->mode);
  500. device->bdev = NULL;
  501. fs_devices->open_devices--;
  502. }
  503. if (device->writeable) {
  504. list_del_init(&device->dev_alloc_list);
  505. device->writeable = 0;
  506. if (!device->is_tgtdev_for_dev_replace)
  507. fs_devices->rw_devices--;
  508. }
  509. list_del_init(&device->dev_list);
  510. fs_devices->num_devices--;
  511. rcu_string_free(device->name);
  512. kfree(device);
  513. }
  514. if (fs_devices->seed) {
  515. fs_devices = fs_devices->seed;
  516. goto again;
  517. }
  518. fs_devices->latest_bdev = latest_bdev;
  519. fs_devices->latest_devid = latest_devid;
  520. fs_devices->latest_trans = latest_transid;
  521. mutex_unlock(&uuid_mutex);
  522. }
  523. static void __free_device(struct work_struct *work)
  524. {
  525. struct btrfs_device *device;
  526. device = container_of(work, struct btrfs_device, rcu_work);
  527. if (device->bdev)
  528. blkdev_put(device->bdev, device->mode);
  529. rcu_string_free(device->name);
  530. kfree(device);
  531. }
  532. static void free_device(struct rcu_head *head)
  533. {
  534. struct btrfs_device *device;
  535. device = container_of(head, struct btrfs_device, rcu);
  536. INIT_WORK(&device->rcu_work, __free_device);
  537. schedule_work(&device->rcu_work);
  538. }
  539. static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
  540. {
  541. struct btrfs_device *device;
  542. if (--fs_devices->opened > 0)
  543. return 0;
  544. mutex_lock(&fs_devices->device_list_mutex);
  545. list_for_each_entry(device, &fs_devices->devices, dev_list) {
  546. struct btrfs_device *new_device;
  547. struct rcu_string *name;
  548. if (device->bdev)
  549. fs_devices->open_devices--;
  550. if (device->writeable && !device->is_tgtdev_for_dev_replace) {
  551. list_del_init(&device->dev_alloc_list);
  552. fs_devices->rw_devices--;
  553. }
  554. if (device->can_discard)
  555. fs_devices->num_can_discard--;
  556. new_device = kmalloc(sizeof(*new_device), GFP_NOFS);
  557. BUG_ON(!new_device); /* -ENOMEM */
  558. memcpy(new_device, device, sizeof(*new_device));
  559. /* Safe because we are under uuid_mutex */
  560. if (device->name) {
  561. name = rcu_string_strdup(device->name->str, GFP_NOFS);
  562. BUG_ON(device->name && !name); /* -ENOMEM */
  563. rcu_assign_pointer(new_device->name, name);
  564. }
  565. new_device->bdev = NULL;
  566. new_device->writeable = 0;
  567. new_device->in_fs_metadata = 0;
  568. new_device->can_discard = 0;
  569. spin_lock_init(&new_device->io_lock);
  570. list_replace_rcu(&device->dev_list, &new_device->dev_list);
  571. call_rcu(&device->rcu, free_device);
  572. }
  573. mutex_unlock(&fs_devices->device_list_mutex);
  574. WARN_ON(fs_devices->open_devices);
  575. WARN_ON(fs_devices->rw_devices);
  576. fs_devices->opened = 0;
  577. fs_devices->seeding = 0;
  578. return 0;
  579. }
  580. int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
  581. {
  582. struct btrfs_fs_devices *seed_devices = NULL;
  583. int ret;
  584. mutex_lock(&uuid_mutex);
  585. ret = __btrfs_close_devices(fs_devices);
  586. if (!fs_devices->opened) {
  587. seed_devices = fs_devices->seed;
  588. fs_devices->seed = NULL;
  589. }
  590. mutex_unlock(&uuid_mutex);
  591. while (seed_devices) {
  592. fs_devices = seed_devices;
  593. seed_devices = fs_devices->seed;
  594. __btrfs_close_devices(fs_devices);
  595. free_fs_devices(fs_devices);
  596. }
  597. /*
  598. * Wait for rcu kworkers under __btrfs_close_devices
  599. * to finish all blkdev_puts so device is really
  600. * free when umount is done.
  601. */
  602. rcu_barrier();
  603. return ret;
  604. }
  605. static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
  606. fmode_t flags, void *holder)
  607. {
  608. struct request_queue *q;
  609. struct block_device *bdev;
  610. struct list_head *head = &fs_devices->devices;
  611. struct btrfs_device *device;
  612. struct block_device *latest_bdev = NULL;
  613. struct buffer_head *bh;
  614. struct btrfs_super_block *disk_super;
  615. u64 latest_devid = 0;
  616. u64 latest_transid = 0;
  617. u64 devid;
  618. int seeding = 1;
  619. int ret = 0;
  620. flags |= FMODE_EXCL;
  621. list_for_each_entry(device, head, dev_list) {
  622. if (device->bdev)
  623. continue;
  624. if (!device->name)
  625. continue;
  626. /* Just open everything we can; ignore failures here */
  627. if (btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
  628. &bdev, &bh))
  629. continue;
  630. disk_super = (struct btrfs_super_block *)bh->b_data;
  631. devid = btrfs_stack_device_id(&disk_super->dev_item);
  632. if (devid != device->devid)
  633. goto error_brelse;
  634. if (memcmp(device->uuid, disk_super->dev_item.uuid,
  635. BTRFS_UUID_SIZE))
  636. goto error_brelse;
  637. device->generation = btrfs_super_generation(disk_super);
  638. if (!latest_transid || device->generation > latest_transid) {
  639. latest_devid = devid;
  640. latest_transid = device->generation;
  641. latest_bdev = bdev;
  642. }
  643. if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
  644. device->writeable = 0;
  645. } else {
  646. device->writeable = !bdev_read_only(bdev);
  647. seeding = 0;
  648. }
  649. q = bdev_get_queue(bdev);
  650. if (blk_queue_discard(q)) {
  651. device->can_discard = 1;
  652. fs_devices->num_can_discard++;
  653. }
  654. device->bdev = bdev;
  655. device->in_fs_metadata = 0;
  656. device->mode = flags;
  657. if (!blk_queue_nonrot(bdev_get_queue(bdev)))
  658. fs_devices->rotating = 1;
  659. fs_devices->open_devices++;
  660. if (device->writeable && !device->is_tgtdev_for_dev_replace) {
  661. fs_devices->rw_devices++;
  662. list_add(&device->dev_alloc_list,
  663. &fs_devices->alloc_list);
  664. }
  665. brelse(bh);
  666. continue;
  667. error_brelse:
  668. brelse(bh);
  669. blkdev_put(bdev, flags);
  670. continue;
  671. }
  672. if (fs_devices->open_devices == 0) {
  673. ret = -EINVAL;
  674. goto out;
  675. }
  676. fs_devices->seeding = seeding;
  677. fs_devices->opened = 1;
  678. fs_devices->latest_bdev = latest_bdev;
  679. fs_devices->latest_devid = latest_devid;
  680. fs_devices->latest_trans = latest_transid;
  681. fs_devices->total_rw_bytes = 0;
  682. out:
  683. return ret;
  684. }
  685. int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
  686. fmode_t flags, void *holder)
  687. {
  688. int ret;
  689. mutex_lock(&uuid_mutex);
  690. if (fs_devices->opened) {
  691. fs_devices->opened++;
  692. ret = 0;
  693. } else {
  694. ret = __btrfs_open_devices(fs_devices, flags, holder);
  695. }
  696. mutex_unlock(&uuid_mutex);
  697. return ret;
  698. }
  699. /*
  700. * Look for a btrfs signature on a device. This may be called out of the mount path
  701. * and we are not allowed to call set_blocksize during the scan. The superblock
  702. * is read via pagecache
  703. */
  704. int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
  705. struct btrfs_fs_devices **fs_devices_ret)
  706. {
  707. struct btrfs_super_block *disk_super;
  708. struct block_device *bdev;
  709. struct page *page;
  710. void *p;
  711. int ret = -EINVAL;
  712. u64 devid;
  713. u64 transid;
  714. u64 total_devices;
  715. u64 bytenr;
  716. pgoff_t index;
  717. /*
  718. * we would like to check all the supers, but that would make
  719. * a btrfs mount succeed after a mkfs from a different FS.
  720. * So, we need to add a special mount option to scan for
  721. * later supers, using BTRFS_SUPER_MIRROR_MAX instead
  722. */
  723. bytenr = btrfs_sb_offset(0);
  724. flags |= FMODE_EXCL;
  725. mutex_lock(&uuid_mutex);
  726. bdev = blkdev_get_by_path(path, flags, holder);
  727. if (IS_ERR(bdev)) {
  728. ret = PTR_ERR(bdev);
  729. goto error;
  730. }
  731. /* make sure our super fits in the device */
  732. if (bytenr + PAGE_CACHE_SIZE >= i_size_read(bdev->bd_inode))
  733. goto error_bdev_put;
  734. /* make sure our super fits in the page */
  735. if (sizeof(*disk_super) > PAGE_CACHE_SIZE)
  736. goto error_bdev_put;
  737. /* make sure our super doesn't straddle pages on disk */
  738. index = bytenr >> PAGE_CACHE_SHIFT;
  739. if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_CACHE_SHIFT != index)
  740. goto error_bdev_put;
  741. /* pull in the page with our super */
  742. page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
  743. index, GFP_NOFS);
  744. if (IS_ERR_OR_NULL(page))
  745. goto error_bdev_put;
  746. p = kmap(page);
  747. /* align our pointer to the offset of the super block */
  748. disk_super = p + (bytenr & ~PAGE_CACHE_MASK);
  749. if (btrfs_super_bytenr(disk_super) != bytenr ||
  750. btrfs_super_magic(disk_super) != BTRFS_MAGIC)
  751. goto error_unmap;
  752. devid = btrfs_stack_device_id(&disk_super->dev_item);
  753. transid = btrfs_super_generation(disk_super);
  754. total_devices = btrfs_super_num_devices(disk_super);
  755. if (disk_super->label[0]) {
  756. if (disk_super->label[BTRFS_LABEL_SIZE - 1])
  757. disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0';
  758. printk(KERN_INFO "device label %s ", disk_super->label);
  759. } else {
  760. printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
  761. }
  762. printk(KERN_CONT "devid %llu transid %llu %s\n",
  763. (unsigned long long)devid, (unsigned long long)transid, path);
  764. ret = device_list_add(path, disk_super, devid, fs_devices_ret);
  765. if (!ret && fs_devices_ret)
  766. (*fs_devices_ret)->total_devices = total_devices;
  767. error_unmap:
  768. kunmap(page);
  769. page_cache_release(page);
  770. error_bdev_put:
  771. blkdev_put(bdev, flags);
  772. error:
  773. mutex_unlock(&uuid_mutex);
  774. return ret;
  775. }
  776. /* helper to account the used device space in the range */
  777. int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
  778. u64 end, u64 *length)
  779. {
  780. struct btrfs_key key;
  781. struct btrfs_root *root = device->dev_root;
  782. struct btrfs_dev_extent *dev_extent;
  783. struct btrfs_path *path;
  784. u64 extent_end;
  785. int ret;
  786. int slot;
  787. struct extent_buffer *l;
  788. *length = 0;
  789. if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace)
  790. return 0;
  791. path = btrfs_alloc_path();
  792. if (!path)
  793. return -ENOMEM;
  794. path->reada = 2;
  795. key.objectid = device->devid;
  796. key.offset = start;
  797. key.type = BTRFS_DEV_EXTENT_KEY;
  798. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  799. if (ret < 0)
  800. goto out;
  801. if (ret > 0) {
  802. ret = btrfs_previous_item(root, path, key.objectid, key.type);
  803. if (ret < 0)
  804. goto out;
  805. }
  806. while (1) {
  807. l = path->nodes[0];
  808. slot = path->slots[0];
  809. if (slot >= btrfs_header_nritems(l)) {
  810. ret = btrfs_next_leaf(root, path);
  811. if (ret == 0)
  812. continue;
  813. if (ret < 0)
  814. goto out;
  815. break;
  816. }
  817. btrfs_item_key_to_cpu(l, &key, slot);
  818. if (key.objectid < device->devid)
  819. goto next;
  820. if (key.objectid > device->devid)
  821. break;
  822. if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
  823. goto next;
  824. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  825. extent_end = key.offset + btrfs_dev_extent_length(l,
  826. dev_extent);
  827. if (key.offset <= start && extent_end > end) {
  828. *length = end - start + 1;
  829. break;
  830. } else if (key.offset <= start && extent_end > start)
  831. *length += extent_end - start;
  832. else if (key.offset > start && extent_end <= end)
  833. *length += extent_end - key.offset;
  834. else if (key.offset > start && key.offset <= end) {
  835. *length += end - key.offset + 1;
  836. break;
  837. } else if (key.offset > end)
  838. break;
  839. next:
  840. path->slots[0]++;
  841. }
  842. ret = 0;
  843. out:
  844. btrfs_free_path(path);
  845. return ret;
  846. }
  847. static int contains_pending_extent(struct btrfs_trans_handle *trans,
  848. struct btrfs_device *device,
  849. u64 *start, u64 len)
  850. {
  851. struct extent_map *em;
  852. int ret = 0;
  853. list_for_each_entry(em, &trans->transaction->pending_chunks, list) {
  854. struct map_lookup *map;
  855. int i;
  856. map = (struct map_lookup *)em->bdev;
  857. for (i = 0; i < map->num_stripes; i++) {
  858. if (map->stripes[i].dev != device)
  859. continue;
  860. if (map->stripes[i].physical >= *start + len ||
  861. map->stripes[i].physical + em->orig_block_len <=
  862. *start)
  863. continue;
  864. *start = map->stripes[i].physical +
  865. em->orig_block_len;
  866. ret = 1;
  867. }
  868. }
  869. return ret;
  870. }
  871. /*
  872. * find_free_dev_extent - find free space in the specified device
  873. * @device: the device which we search the free space in
  874. * @num_bytes: the size of the free space that we need
  875. * @start: store the start of the free space.
  876. * @len: the size of the free space. that we find, or the size of the max
  877. * free space if we don't find suitable free space
  878. *
  879. * this uses a pretty simple search, the expectation is that it is
  880. * called very infrequently and that a given device has a small number
  881. * of extents
  882. *
  883. * @start is used to store the start of the free space if we find. But if we
  884. * don't find suitable free space, it will be used to store the start position
  885. * of the max free space.
  886. *
  887. * @len is used to store the size of the free space that we find.
  888. * But if we don't find suitable free space, it is used to store the size of
  889. * the max free space.
  890. */
  891. int find_free_dev_extent(struct btrfs_trans_handle *trans,
  892. struct btrfs_device *device, u64 num_bytes,
  893. u64 *start, u64 *len)
  894. {
  895. struct btrfs_key key;
  896. struct btrfs_root *root = device->dev_root;
  897. struct btrfs_dev_extent *dev_extent;
  898. struct btrfs_path *path;
  899. u64 hole_size;
  900. u64 max_hole_start;
  901. u64 max_hole_size;
  902. u64 extent_end;
  903. u64 search_start;
  904. u64 search_end = device->total_bytes;
  905. int ret;
  906. int slot;
  907. struct extent_buffer *l;
  908. /* FIXME use last free of some kind */
  909. /* we don't want to overwrite the superblock on the drive,
  910. * so we make sure to start at an offset of at least 1MB
  911. */
  912. search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
  913. path = btrfs_alloc_path();
  914. if (!path)
  915. return -ENOMEM;
  916. again:
  917. max_hole_start = search_start;
  918. max_hole_size = 0;
  919. hole_size = 0;
  920. if (search_start >= search_end || device->is_tgtdev_for_dev_replace) {
  921. ret = -ENOSPC;
  922. goto out;
  923. }
  924. path->reada = 2;
  925. path->search_commit_root = 1;
  926. path->skip_locking = 1;
  927. key.objectid = device->devid;
  928. key.offset = search_start;
  929. key.type = BTRFS_DEV_EXTENT_KEY;
  930. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  931. if (ret < 0)
  932. goto out;
  933. if (ret > 0) {
  934. ret = btrfs_previous_item(root, path, key.objectid, key.type);
  935. if (ret < 0)
  936. goto out;
  937. }
  938. while (1) {
  939. l = path->nodes[0];
  940. slot = path->slots[0];
  941. if (slot >= btrfs_header_nritems(l)) {
  942. ret = btrfs_next_leaf(root, path);
  943. if (ret == 0)
  944. continue;
  945. if (ret < 0)
  946. goto out;
  947. break;
  948. }
  949. btrfs_item_key_to_cpu(l, &key, slot);
  950. if (key.objectid < device->devid)
  951. goto next;
  952. if (key.objectid > device->devid)
  953. break;
  954. if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
  955. goto next;
  956. if (key.offset > search_start) {
  957. hole_size = key.offset - search_start;
  958. /*
  959. * Have to check before we set max_hole_start, otherwise
  960. * we could end up sending back this offset anyway.
  961. */
  962. if (contains_pending_extent(trans, device,
  963. &search_start,
  964. hole_size))
  965. hole_size = 0;
  966. if (hole_size > max_hole_size) {
  967. max_hole_start = search_start;
  968. max_hole_size = hole_size;
  969. }
  970. /*
  971. * If this free space is greater than which we need,
  972. * it must be the max free space that we have found
  973. * until now, so max_hole_start must point to the start
  974. * of this free space and the length of this free space
  975. * is stored in max_hole_size. Thus, we return
  976. * max_hole_start and max_hole_size and go back to the
  977. * caller.
  978. */
  979. if (hole_size >= num_bytes) {
  980. ret = 0;
  981. goto out;
  982. }
  983. }
  984. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  985. extent_end = key.offset + btrfs_dev_extent_length(l,
  986. dev_extent);
  987. if (extent_end > search_start)
  988. search_start = extent_end;
  989. next:
  990. path->slots[0]++;
  991. cond_resched();
  992. }
  993. /*
  994. * At this point, search_start should be the end of
  995. * allocated dev extents, and when shrinking the device,
  996. * search_end may be smaller than search_start.
  997. */
  998. if (search_end > search_start)
  999. hole_size = search_end - search_start;
  1000. if (hole_size > max_hole_size) {
  1001. max_hole_start = search_start;
  1002. max_hole_size = hole_size;
  1003. }
  1004. if (contains_pending_extent(trans, device, &search_start, hole_size)) {
  1005. btrfs_release_path(path);
  1006. goto again;
  1007. }
  1008. /* See above. */
  1009. if (hole_size < num_bytes)
  1010. ret = -ENOSPC;
  1011. else
  1012. ret = 0;
  1013. out:
  1014. btrfs_free_path(path);
  1015. *start = max_hole_start;
  1016. if (len)
  1017. *len = max_hole_size;
  1018. return ret;
  1019. }
  1020. static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
  1021. struct btrfs_device *device,
  1022. u64 start)
  1023. {
  1024. int ret;
  1025. struct btrfs_path *path;
  1026. struct btrfs_root *root = device->dev_root;
  1027. struct btrfs_key key;
  1028. struct btrfs_key found_key;
  1029. struct extent_buffer *leaf = NULL;
  1030. struct btrfs_dev_extent *extent = NULL;
  1031. path = btrfs_alloc_path();
  1032. if (!path)
  1033. return -ENOMEM;
  1034. key.objectid = device->devid;
  1035. key.offset = start;
  1036. key.type = BTRFS_DEV_EXTENT_KEY;
  1037. again:
  1038. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  1039. if (ret > 0) {
  1040. ret = btrfs_previous_item(root, path, key.objectid,
  1041. BTRFS_DEV_EXTENT_KEY);
  1042. if (ret)
  1043. goto out;
  1044. leaf = path->nodes[0];
  1045. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  1046. extent = btrfs_item_ptr(leaf, path->slots[0],
  1047. struct btrfs_dev_extent);
  1048. BUG_ON(found_key.offset > start || found_key.offset +
  1049. btrfs_dev_extent_length(leaf, extent) < start);
  1050. key = found_key;
  1051. btrfs_release_path(path);
  1052. goto again;
  1053. } else if (ret == 0) {
  1054. leaf = path->nodes[0];
  1055. extent = btrfs_item_ptr(leaf, path->slots[0],
  1056. struct btrfs_dev_extent);
  1057. } else {
  1058. btrfs_error(root->fs_info, ret, "Slot search failed");
  1059. goto out;
  1060. }
  1061. if (device->bytes_used > 0) {
  1062. u64 len = btrfs_dev_extent_length(leaf, extent);
  1063. device->bytes_used -= len;
  1064. spin_lock(&root->fs_info->free_chunk_lock);
  1065. root->fs_info->free_chunk_space += len;
  1066. spin_unlock(&root->fs_info->free_chunk_lock);
  1067. }
  1068. ret = btrfs_del_item(trans, root, path);
  1069. if (ret) {
  1070. btrfs_error(root->fs_info, ret,
  1071. "Failed to remove dev extent item");
  1072. }
  1073. out:
  1074. btrfs_free_path(path);
  1075. return ret;
  1076. }
  1077. static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
  1078. struct btrfs_device *device,
  1079. u64 chunk_tree, u64 chunk_objectid,
  1080. u64 chunk_offset, u64 start, u64 num_bytes)
  1081. {
  1082. int ret;
  1083. struct btrfs_path *path;
  1084. struct btrfs_root *root = device->dev_root;
  1085. struct btrfs_dev_extent *extent;
  1086. struct extent_buffer *leaf;
  1087. struct btrfs_key key;
  1088. WARN_ON(!device->in_fs_metadata);
  1089. WARN_ON(device->is_tgtdev_for_dev_replace);
  1090. path = btrfs_alloc_path();
  1091. if (!path)
  1092. return -ENOMEM;
  1093. key.objectid = device->devid;
  1094. key.offset = start;
  1095. key.type = BTRFS_DEV_EXTENT_KEY;
  1096. ret = btrfs_insert_empty_item(trans, root, path, &key,
  1097. sizeof(*extent));
  1098. if (ret)
  1099. goto out;
  1100. leaf = path->nodes[0];
  1101. extent = btrfs_item_ptr(leaf, path->slots[0],
  1102. struct btrfs_dev_extent);
  1103. btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
  1104. btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
  1105. btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
  1106. write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
  1107. (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
  1108. BTRFS_UUID_SIZE);
  1109. btrfs_set_dev_extent_length(leaf, extent, num_bytes);
  1110. btrfs_mark_buffer_dirty(leaf);
  1111. out:
  1112. btrfs_free_path(path);
  1113. return ret;
  1114. }
  1115. static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
  1116. {
  1117. struct extent_map_tree *em_tree;
  1118. struct extent_map *em;
  1119. struct rb_node *n;
  1120. u64 ret = 0;
  1121. em_tree = &fs_info->mapping_tree.map_tree;
  1122. read_lock(&em_tree->lock);
  1123. n = rb_last(&em_tree->map);
  1124. if (n) {
  1125. em = rb_entry(n, struct extent_map, rb_node);
  1126. ret = em->start + em->len;
  1127. }
  1128. read_unlock(&em_tree->lock);
  1129. return ret;
  1130. }
  1131. static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
  1132. {
  1133. int ret;
  1134. struct btrfs_key key;
  1135. struct btrfs_key found_key;
  1136. struct btrfs_path *path;
  1137. root = root->fs_info->chunk_root;
  1138. path = btrfs_alloc_path();
  1139. if (!path)
  1140. return -ENOMEM;
  1141. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  1142. key.type = BTRFS_DEV_ITEM_KEY;
  1143. key.offset = (u64)-1;
  1144. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1145. if (ret < 0)
  1146. goto error;
  1147. BUG_ON(ret == 0); /* Corruption */
  1148. ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
  1149. BTRFS_DEV_ITEM_KEY);
  1150. if (ret) {
  1151. *objectid = 1;
  1152. } else {
  1153. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  1154. path->slots[0]);
  1155. *objectid = found_key.offset + 1;
  1156. }
  1157. ret = 0;
  1158. error:
  1159. btrfs_free_path(path);
  1160. return ret;
  1161. }
  1162. /*
  1163. * the device information is stored in the chunk root
  1164. * the btrfs_device struct should be fully filled in
  1165. */
  1166. static int btrfs_add_device(struct btrfs_trans_handle *trans,
  1167. struct btrfs_root *root,
  1168. struct btrfs_device *device)
  1169. {
  1170. int ret;
  1171. struct btrfs_path *path;
  1172. struct btrfs_dev_item *dev_item;
  1173. struct extent_buffer *leaf;
  1174. struct btrfs_key key;
  1175. unsigned long ptr;
  1176. root = root->fs_info->chunk_root;
  1177. path = btrfs_alloc_path();
  1178. if (!path)
  1179. return -ENOMEM;
  1180. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  1181. key.type = BTRFS_DEV_ITEM_KEY;
  1182. key.offset = device->devid;
  1183. ret = btrfs_insert_empty_item(trans, root, path, &key,
  1184. sizeof(*dev_item));
  1185. if (ret)
  1186. goto out;
  1187. leaf = path->nodes[0];
  1188. dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
  1189. btrfs_set_device_id(leaf, dev_item, device->devid);
  1190. btrfs_set_device_generation(leaf, dev_item, 0);
  1191. btrfs_set_device_type(leaf, dev_item, device->type);
  1192. btrfs_set_device_io_align(leaf, dev_item, device->io_align);
  1193. btrfs_set_device_io_width(leaf, dev_item, device->io_width);
  1194. btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
  1195. btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
  1196. btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
  1197. btrfs_set_device_group(leaf, dev_item, 0);
  1198. btrfs_set_device_seek_speed(leaf, dev_item, 0);
  1199. btrfs_set_device_bandwidth(leaf, dev_item, 0);
  1200. btrfs_set_device_start_offset(leaf, dev_item, 0);
  1201. ptr = (unsigned long)btrfs_device_uuid(dev_item);
  1202. write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
  1203. ptr = (unsigned long)btrfs_device_fsid(dev_item);
  1204. write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
  1205. btrfs_mark_buffer_dirty(leaf);
  1206. ret = 0;
  1207. out:
  1208. btrfs_free_path(path);
  1209. return ret;
  1210. }
  1211. static int btrfs_rm_dev_item(struct btrfs_root *root,
  1212. struct btrfs_device *device)
  1213. {
  1214. int ret;
  1215. struct btrfs_path *path;
  1216. struct btrfs_key key;
  1217. struct btrfs_trans_handle *trans;
  1218. root = root->fs_info->chunk_root;
  1219. path = btrfs_alloc_path();
  1220. if (!path)
  1221. return -ENOMEM;
  1222. trans = btrfs_start_transaction(root, 0);
  1223. if (IS_ERR(trans)) {
  1224. btrfs_free_path(path);
  1225. return PTR_ERR(trans);
  1226. }
  1227. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  1228. key.type = BTRFS_DEV_ITEM_KEY;
  1229. key.offset = device->devid;
  1230. lock_chunks(root);
  1231. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  1232. if (ret < 0)
  1233. goto out;
  1234. if (ret > 0) {
  1235. ret = -ENOENT;
  1236. goto out;
  1237. }
  1238. ret = btrfs_del_item(trans, root, path);
  1239. if (ret)
  1240. goto out;
  1241. out:
  1242. btrfs_free_path(path);
  1243. unlock_chunks(root);
  1244. btrfs_commit_transaction(trans, root);
  1245. return ret;
  1246. }
  1247. int btrfs_rm_device(struct btrfs_root *root, char *device_path)
  1248. {
  1249. struct btrfs_device *device;
  1250. struct btrfs_device *next_device;
  1251. struct block_device *bdev;
  1252. struct buffer_head *bh = NULL;
  1253. struct btrfs_super_block *disk_super;
  1254. struct btrfs_fs_devices *cur_devices;
  1255. u64 all_avail;
  1256. u64 devid;
  1257. u64 num_devices;
  1258. u8 *dev_uuid;
  1259. unsigned seq;
  1260. int ret = 0;
  1261. bool clear_super = false;
  1262. mutex_lock(&uuid_mutex);
  1263. do {
  1264. seq = read_seqbegin(&root->fs_info->profiles_lock);
  1265. all_avail = root->fs_info->avail_data_alloc_bits |
  1266. root->fs_info->avail_system_alloc_bits |
  1267. root->fs_info->avail_metadata_alloc_bits;
  1268. } while (read_seqretry(&root->fs_info->profiles_lock, seq));
  1269. num_devices = root->fs_info->fs_devices->num_devices;
  1270. btrfs_dev_replace_lock(&root->fs_info->dev_replace);
  1271. if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) {
  1272. WARN_ON(num_devices < 1);
  1273. num_devices--;
  1274. }
  1275. btrfs_dev_replace_unlock(&root->fs_info->dev_replace);
  1276. if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) {
  1277. ret = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET;
  1278. goto out;
  1279. }
  1280. if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) {
  1281. ret = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET;
  1282. goto out;
  1283. }
  1284. if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) &&
  1285. root->fs_info->fs_devices->rw_devices <= 2) {
  1286. ret = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET;
  1287. goto out;
  1288. }
  1289. if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) &&
  1290. root->fs_info->fs_devices->rw_devices <= 3) {
  1291. ret = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET;
  1292. goto out;
  1293. }
  1294. if (strcmp(device_path, "missing") == 0) {
  1295. struct list_head *devices;
  1296. struct btrfs_device *tmp;
  1297. device = NULL;
  1298. devices = &root->fs_info->fs_devices->devices;
  1299. /*
  1300. * It is safe to read the devices since the volume_mutex
  1301. * is held.
  1302. */
  1303. list_for_each_entry(tmp, devices, dev_list) {
  1304. if (tmp->in_fs_metadata &&
  1305. !tmp->is_tgtdev_for_dev_replace &&
  1306. !tmp->bdev) {
  1307. device = tmp;
  1308. break;
  1309. }
  1310. }
  1311. bdev = NULL;
  1312. bh = NULL;
  1313. disk_super = NULL;
  1314. if (!device) {
  1315. ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
  1316. goto out;
  1317. }
  1318. } else {
  1319. ret = btrfs_get_bdev_and_sb(device_path,
  1320. FMODE_WRITE | FMODE_EXCL,
  1321. root->fs_info->bdev_holder, 0,
  1322. &bdev, &bh);
  1323. if (ret)
  1324. goto out;
  1325. disk_super = (struct btrfs_super_block *)bh->b_data;
  1326. devid = btrfs_stack_device_id(&disk_super->dev_item);
  1327. dev_uuid = disk_super->dev_item.uuid;
  1328. device = btrfs_find_device(root->fs_info, devid, dev_uuid,
  1329. disk_super->fsid);
  1330. if (!device) {
  1331. ret = -ENOENT;
  1332. goto error_brelse;
  1333. }
  1334. }
  1335. if (device->is_tgtdev_for_dev_replace) {
  1336. ret = BTRFS_ERROR_DEV_TGT_REPLACE;
  1337. goto error_brelse;
  1338. }
  1339. if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
  1340. ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
  1341. goto error_brelse;
  1342. }
  1343. if (device->writeable) {
  1344. lock_chunks(root);
  1345. list_del_init(&device->dev_alloc_list);
  1346. unlock_chunks(root);
  1347. root->fs_info->fs_devices->rw_devices--;
  1348. clear_super = true;
  1349. }
  1350. mutex_unlock(&uuid_mutex);
  1351. ret = btrfs_shrink_device(device, 0);
  1352. mutex_lock(&uuid_mutex);
  1353. if (ret)
  1354. goto error_undo;
  1355. /*
  1356. * TODO: the superblock still includes this device in its num_devices
  1357. * counter although write_all_supers() is not locked out. This
  1358. * could give a filesystem state which requires a degraded mount.
  1359. */
  1360. ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
  1361. if (ret)
  1362. goto error_undo;
  1363. spin_lock(&root->fs_info->free_chunk_lock);
  1364. root->fs_info->free_chunk_space = device->total_bytes -
  1365. device->bytes_used;
  1366. spin_unlock(&root->fs_info->free_chunk_lock);
  1367. device->in_fs_metadata = 0;
  1368. btrfs_scrub_cancel_dev(root->fs_info, device);
  1369. /*
  1370. * the device list mutex makes sure that we don't change
  1371. * the device list while someone else is writing out all
  1372. * the device supers.
  1373. */
  1374. cur_devices = device->fs_devices;
  1375. mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
  1376. list_del_rcu(&device->dev_list);
  1377. device->fs_devices->num_devices--;
  1378. device->fs_devices->total_devices--;
  1379. if (device->missing)
  1380. root->fs_info->fs_devices->missing_devices--;
  1381. next_device = list_entry(root->fs_info->fs_devices->devices.next,
  1382. struct btrfs_device, dev_list);
  1383. if (device->bdev == root->fs_info->sb->s_bdev)
  1384. root->fs_info->sb->s_bdev = next_device->bdev;
  1385. if (device->bdev == root->fs_info->fs_devices->latest_bdev)
  1386. root->fs_info->fs_devices->latest_bdev = next_device->bdev;
  1387. if (device->bdev)
  1388. device->fs_devices->open_devices--;
  1389. call_rcu(&device->rcu, free_device);
  1390. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1391. num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
  1392. btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
  1393. if (cur_devices->open_devices == 0) {
  1394. struct btrfs_fs_devices *fs_devices;
  1395. fs_devices = root->fs_info->fs_devices;
  1396. while (fs_devices) {
  1397. if (fs_devices->seed == cur_devices)
  1398. break;
  1399. fs_devices = fs_devices->seed;
  1400. }
  1401. fs_devices->seed = cur_devices->seed;
  1402. cur_devices->seed = NULL;
  1403. lock_chunks(root);
  1404. __btrfs_close_devices(cur_devices);
  1405. unlock_chunks(root);
  1406. free_fs_devices(cur_devices);
  1407. }
  1408. root->fs_info->num_tolerated_disk_barrier_failures =
  1409. btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
  1410. /*
  1411. * at this point, the device is zero sized. We want to
  1412. * remove it from the devices list and zero out the old super
  1413. */
  1414. if (clear_super && disk_super) {
  1415. /* make sure this device isn't detected as part of
  1416. * the FS anymore
  1417. */
  1418. memset(&disk_super->magic, 0, sizeof(disk_super->magic));
  1419. set_buffer_dirty(bh);
  1420. sync_dirty_buffer(bh);
  1421. }
  1422. ret = 0;
  1423. /* Notify udev that device has changed */
  1424. if (bdev)
  1425. btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
  1426. error_brelse:
  1427. brelse(bh);
  1428. if (bdev)
  1429. blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
  1430. out:
  1431. mutex_unlock(&uuid_mutex);
  1432. return ret;
  1433. error_undo:
  1434. if (device->writeable) {
  1435. lock_chunks(root);
  1436. list_add(&device->dev_alloc_list,
  1437. &root->fs_info->fs_devices->alloc_list);
  1438. unlock_chunks(root);
  1439. root->fs_info->fs_devices->rw_devices++;
  1440. }
  1441. goto error_brelse;
  1442. }
  1443. void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info *fs_info,
  1444. struct btrfs_device *srcdev)
  1445. {
  1446. WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
  1447. list_del_rcu(&srcdev->dev_list);
  1448. list_del_rcu(&srcdev->dev_alloc_list);
  1449. fs_info->fs_devices->num_devices--;
  1450. if (srcdev->missing) {
  1451. fs_info->fs_devices->missing_devices--;
  1452. fs_info->fs_devices->rw_devices++;
  1453. }
  1454. if (srcdev->can_discard)
  1455. fs_info->fs_devices->num_can_discard--;
  1456. if (srcdev->bdev)
  1457. fs_info->fs_devices->open_devices--;
  1458. call_rcu(&srcdev->rcu, free_device);
  1459. }
  1460. void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
  1461. struct btrfs_device *tgtdev)
  1462. {
  1463. struct btrfs_device *next_device;
  1464. WARN_ON(!tgtdev);
  1465. mutex_lock(&fs_info->fs_devices->device_list_mutex);
  1466. if (tgtdev->bdev) {
  1467. btrfs_scratch_superblock(tgtdev);
  1468. fs_info->fs_devices->open_devices--;
  1469. }
  1470. fs_info->fs_devices->num_devices--;
  1471. if (tgtdev->can_discard)
  1472. fs_info->fs_devices->num_can_discard++;
  1473. next_device = list_entry(fs_info->fs_devices->devices.next,
  1474. struct btrfs_device, dev_list);
  1475. if (tgtdev->bdev == fs_info->sb->s_bdev)
  1476. fs_info->sb->s_bdev = next_device->bdev;
  1477. if (tgtdev->bdev == fs_info->fs_devices->latest_bdev)
  1478. fs_info->fs_devices->latest_bdev = next_device->bdev;
  1479. list_del_rcu(&tgtdev->dev_list);
  1480. call_rcu(&tgtdev->rcu, free_device);
  1481. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  1482. }
  1483. static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
  1484. struct btrfs_device **device)
  1485. {
  1486. int ret = 0;
  1487. struct btrfs_super_block *disk_super;
  1488. u64 devid;
  1489. u8 *dev_uuid;
  1490. struct block_device *bdev;
  1491. struct buffer_head *bh;
  1492. *device = NULL;
  1493. ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
  1494. root->fs_info->bdev_holder, 0, &bdev, &bh);
  1495. if (ret)
  1496. return ret;
  1497. disk_super = (struct btrfs_super_block *)bh->b_data;
  1498. devid = btrfs_stack_device_id(&disk_super->dev_item);
  1499. dev_uuid = disk_super->dev_item.uuid;
  1500. *device = btrfs_find_device(root->fs_info, devid, dev_uuid,
  1501. disk_super->fsid);
  1502. brelse(bh);
  1503. if (!*device)
  1504. ret = -ENOENT;
  1505. blkdev_put(bdev, FMODE_READ);
  1506. return ret;
  1507. }
  1508. int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
  1509. char *device_path,
  1510. struct btrfs_device **device)
  1511. {
  1512. *device = NULL;
  1513. if (strcmp(device_path, "missing") == 0) {
  1514. struct list_head *devices;
  1515. struct btrfs_device *tmp;
  1516. devices = &root->fs_info->fs_devices->devices;
  1517. /*
  1518. * It is safe to read the devices since the volume_mutex
  1519. * is held by the caller.
  1520. */
  1521. list_for_each_entry(tmp, devices, dev_list) {
  1522. if (tmp->in_fs_metadata && !tmp->bdev) {
  1523. *device = tmp;
  1524. break;
  1525. }
  1526. }
  1527. if (!*device) {
  1528. pr_err("btrfs: no missing device found\n");
  1529. return -ENOENT;
  1530. }
  1531. return 0;
  1532. } else {
  1533. return btrfs_find_device_by_path(root, device_path, device);
  1534. }
  1535. }
  1536. /*
  1537. * does all the dirty work required for changing file system's UUID.
  1538. */
  1539. static int btrfs_prepare_sprout(struct btrfs_root *root)
  1540. {
  1541. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  1542. struct btrfs_fs_devices *old_devices;
  1543. struct btrfs_fs_devices *seed_devices;
  1544. struct btrfs_super_block *disk_super = root->fs_info->super_copy;
  1545. struct btrfs_device *device;
  1546. u64 super_flags;
  1547. BUG_ON(!mutex_is_locked(&uuid_mutex));
  1548. if (!fs_devices->seeding)
  1549. return -EINVAL;
  1550. seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
  1551. if (!seed_devices)
  1552. return -ENOMEM;
  1553. old_devices = clone_fs_devices(fs_devices);
  1554. if (IS_ERR(old_devices)) {
  1555. kfree(seed_devices);
  1556. return PTR_ERR(old_devices);
  1557. }
  1558. list_add(&old_devices->list, &fs_uuids);
  1559. memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
  1560. seed_devices->opened = 1;
  1561. INIT_LIST_HEAD(&seed_devices->devices);
  1562. INIT_LIST_HEAD(&seed_devices->alloc_list);
  1563. mutex_init(&seed_devices->device_list_mutex);
  1564. mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
  1565. list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
  1566. synchronize_rcu);
  1567. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1568. list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
  1569. list_for_each_entry(device, &seed_devices->devices, dev_list) {
  1570. device->fs_devices = seed_devices;
  1571. }
  1572. fs_devices->seeding = 0;
  1573. fs_devices->num_devices = 0;
  1574. fs_devices->open_devices = 0;
  1575. fs_devices->total_devices = 0;
  1576. fs_devices->seed = seed_devices;
  1577. generate_random_uuid(fs_devices->fsid);
  1578. memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
  1579. memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
  1580. super_flags = btrfs_super_flags(disk_super) &
  1581. ~BTRFS_SUPER_FLAG_SEEDING;
  1582. btrfs_set_super_flags(disk_super, super_flags);
  1583. return 0;
  1584. }
  1585. /*
  1586. * strore the expected generation for seed devices in device items.
  1587. */
  1588. static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
  1589. struct btrfs_root *root)
  1590. {
  1591. struct btrfs_path *path;
  1592. struct extent_buffer *leaf;
  1593. struct btrfs_dev_item *dev_item;
  1594. struct btrfs_device *device;
  1595. struct btrfs_key key;
  1596. u8 fs_uuid[BTRFS_UUID_SIZE];
  1597. u8 dev_uuid[BTRFS_UUID_SIZE];
  1598. u64 devid;
  1599. int ret;
  1600. path = btrfs_alloc_path();
  1601. if (!path)
  1602. return -ENOMEM;
  1603. root = root->fs_info->chunk_root;
  1604. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  1605. key.offset = 0;
  1606. key.type = BTRFS_DEV_ITEM_KEY;
  1607. while (1) {
  1608. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  1609. if (ret < 0)
  1610. goto error;
  1611. leaf = path->nodes[0];
  1612. next_slot:
  1613. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  1614. ret = btrfs_next_leaf(root, path);
  1615. if (ret > 0)
  1616. break;
  1617. if (ret < 0)
  1618. goto error;
  1619. leaf = path->nodes[0];
  1620. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  1621. btrfs_release_path(path);
  1622. continue;
  1623. }
  1624. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  1625. if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
  1626. key.type != BTRFS_DEV_ITEM_KEY)
  1627. break;
  1628. dev_item = btrfs_item_ptr(leaf, path->slots[0],
  1629. struct btrfs_dev_item);
  1630. devid = btrfs_device_id(leaf, dev_item);
  1631. read_extent_buffer(leaf, dev_uuid,
  1632. (unsigned long)btrfs_device_uuid(dev_item),
  1633. BTRFS_UUID_SIZE);
  1634. read_extent_buffer(leaf, fs_uuid,
  1635. (unsigned long)btrfs_device_fsid(dev_item),
  1636. BTRFS_UUID_SIZE);
  1637. device = btrfs_find_device(root->fs_info, devid, dev_uuid,
  1638. fs_uuid);
  1639. BUG_ON(!device); /* Logic error */
  1640. if (device->fs_devices->seeding) {
  1641. btrfs_set_device_generation(leaf, dev_item,
  1642. device->generation);
  1643. btrfs_mark_buffer_dirty(leaf);
  1644. }
  1645. path->slots[0]++;
  1646. goto next_slot;
  1647. }
  1648. ret = 0;
  1649. error:
  1650. btrfs_free_path(path);
  1651. return ret;
  1652. }
  1653. int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
  1654. {
  1655. struct request_queue *q;
  1656. struct btrfs_trans_handle *trans;
  1657. struct btrfs_device *device;
  1658. struct block_device *bdev;
  1659. struct list_head *devices;
  1660. struct super_block *sb = root->fs_info->sb;
  1661. struct rcu_string *name;
  1662. u64 total_bytes;
  1663. int seeding_dev = 0;
  1664. int ret = 0;
  1665. if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
  1666. return -EROFS;
  1667. bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
  1668. root->fs_info->bdev_holder);
  1669. if (IS_ERR(bdev))
  1670. return PTR_ERR(bdev);
  1671. if (root->fs_info->fs_devices->seeding) {
  1672. seeding_dev = 1;
  1673. down_write(&sb->s_umount);
  1674. mutex_lock(&uuid_mutex);
  1675. }
  1676. filemap_write_and_wait(bdev->bd_inode->i_mapping);
  1677. devices = &root->fs_info->fs_devices->devices;
  1678. mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
  1679. list_for_each_entry(device, devices, dev_list) {
  1680. if (device->bdev == bdev) {
  1681. ret = -EEXIST;
  1682. mutex_unlock(
  1683. &root->fs_info->fs_devices->device_list_mutex);
  1684. goto error;
  1685. }
  1686. }
  1687. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1688. device = kzalloc(sizeof(*device), GFP_NOFS);
  1689. if (!device) {
  1690. /* we can safely leave the fs_devices entry around */
  1691. ret = -ENOMEM;
  1692. goto error;
  1693. }
  1694. name = rcu_string_strdup(device_path, GFP_NOFS);
  1695. if (!name) {
  1696. kfree(device);
  1697. ret = -ENOMEM;
  1698. goto error;
  1699. }
  1700. rcu_assign_pointer(device->name, name);
  1701. ret = find_next_devid(root, &device->devid);
  1702. if (ret) {
  1703. rcu_string_free(device->name);
  1704. kfree(device);
  1705. goto error;
  1706. }
  1707. trans = btrfs_start_transaction(root, 0);
  1708. if (IS_ERR(trans)) {
  1709. rcu_string_free(device->name);
  1710. kfree(device);
  1711. ret = PTR_ERR(trans);
  1712. goto error;
  1713. }
  1714. lock_chunks(root);
  1715. q = bdev_get_queue(bdev);
  1716. if (blk_queue_discard(q))
  1717. device->can_discard = 1;
  1718. device->writeable = 1;
  1719. device->work.func = pending_bios_fn;
  1720. generate_random_uuid(device->uuid);
  1721. spin_lock_init(&device->io_lock);
  1722. device->generation = trans->transid;
  1723. device->io_width = root->sectorsize;
  1724. device->io_align = root->sectorsize;
  1725. device->sector_size = root->sectorsize;
  1726. device->total_bytes = i_size_read(bdev->bd_inode);
  1727. device->disk_total_bytes = device->total_bytes;
  1728. device->dev_root = root->fs_info->dev_root;
  1729. device->bdev = bdev;
  1730. device->in_fs_metadata = 1;
  1731. device->is_tgtdev_for_dev_replace = 0;
  1732. device->mode = FMODE_EXCL;
  1733. set_blocksize(device->bdev, 4096);
  1734. if (seeding_dev) {
  1735. sb->s_flags &= ~MS_RDONLY;
  1736. ret = btrfs_prepare_sprout(root);
  1737. BUG_ON(ret); /* -ENOMEM */
  1738. }
  1739. device->fs_devices = root->fs_info->fs_devices;
  1740. mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
  1741. list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
  1742. list_add(&device->dev_alloc_list,
  1743. &root->fs_info->fs_devices->alloc_list);
  1744. root->fs_info->fs_devices->num_devices++;
  1745. root->fs_info->fs_devices->open_devices++;
  1746. root->fs_info->fs_devices->rw_devices++;
  1747. root->fs_info->fs_devices->total_devices++;
  1748. if (device->can_discard)
  1749. root->fs_info->fs_devices->num_can_discard++;
  1750. root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
  1751. spin_lock(&root->fs_info->free_chunk_lock);
  1752. root->fs_info->free_chunk_space += device->total_bytes;
  1753. spin_unlock(&root->fs_info->free_chunk_lock);
  1754. if (!blk_queue_nonrot(bdev_get_queue(bdev)))
  1755. root->fs_info->fs_devices->rotating = 1;
  1756. total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
  1757. btrfs_set_super_total_bytes(root->fs_info->super_copy,
  1758. total_bytes + device->total_bytes);
  1759. total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
  1760. btrfs_set_super_num_devices(root->fs_info->super_copy,
  1761. total_bytes + 1);
  1762. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1763. if (seeding_dev) {
  1764. ret = init_first_rw_device(trans, root, device);
  1765. if (ret) {
  1766. btrfs_abort_transaction(trans, root, ret);
  1767. goto error_trans;
  1768. }
  1769. ret = btrfs_finish_sprout(trans, root);
  1770. if (ret) {
  1771. btrfs_abort_transaction(trans, root, ret);
  1772. goto error_trans;
  1773. }
  1774. } else {
  1775. ret = btrfs_add_device(trans, root, device);
  1776. if (ret) {
  1777. btrfs_abort_transaction(trans, root, ret);
  1778. goto error_trans;
  1779. }
  1780. }
  1781. /*
  1782. * we've got more storage, clear any full flags on the space
  1783. * infos
  1784. */
  1785. btrfs_clear_space_info_full(root->fs_info);
  1786. unlock_chunks(root);
  1787. root->fs_info->num_tolerated_disk_barrier_failures =
  1788. btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
  1789. ret = btrfs_commit_transaction(trans, root);
  1790. if (seeding_dev) {
  1791. mutex_unlock(&uuid_mutex);
  1792. up_write(&sb->s_umount);
  1793. if (ret) /* transaction commit */
  1794. return ret;
  1795. ret = btrfs_relocate_sys_chunks(root);
  1796. if (ret < 0)
  1797. btrfs_error(root->fs_info, ret,
  1798. "Failed to relocate sys chunks after "
  1799. "device initialization. This can be fixed "
  1800. "using the \"btrfs balance\" command.");
  1801. trans = btrfs_attach_transaction(root);
  1802. if (IS_ERR(trans)) {
  1803. if (PTR_ERR(trans) == -ENOENT)
  1804. return 0;
  1805. return PTR_ERR(trans);
  1806. }
  1807. ret = btrfs_commit_transaction(trans, root);
  1808. }
  1809. return ret;
  1810. error_trans:
  1811. unlock_chunks(root);
  1812. btrfs_end_transaction(trans, root);
  1813. rcu_string_free(device->name);
  1814. kfree(device);
  1815. error:
  1816. blkdev_put(bdev, FMODE_EXCL);
  1817. if (seeding_dev) {
  1818. mutex_unlock(&uuid_mutex);
  1819. up_write(&sb->s_umount);
  1820. }
  1821. return ret;
  1822. }
  1823. int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
  1824. struct btrfs_device **device_out)
  1825. {
  1826. struct request_queue *q;
  1827. struct btrfs_device *device;
  1828. struct block_device *bdev;
  1829. struct btrfs_fs_info *fs_info = root->fs_info;
  1830. struct list_head *devices;
  1831. struct rcu_string *name;
  1832. int ret = 0;
  1833. *device_out = NULL;
  1834. if (fs_info->fs_devices->seeding)
  1835. return -EINVAL;
  1836. bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
  1837. fs_info->bdev_holder);
  1838. if (IS_ERR(bdev))
  1839. return PTR_ERR(bdev);
  1840. filemap_write_and_wait(bdev->bd_inode->i_mapping);
  1841. devices = &fs_info->fs_devices->devices;
  1842. list_for_each_entry(device, devices, dev_list) {
  1843. if (device->bdev == bdev) {
  1844. ret = -EEXIST;
  1845. goto error;
  1846. }
  1847. }
  1848. device = kzalloc(sizeof(*device), GFP_NOFS);
  1849. if (!device) {
  1850. ret = -ENOMEM;
  1851. goto error;
  1852. }
  1853. name = rcu_string_strdup(device_path, GFP_NOFS);
  1854. if (!name) {
  1855. kfree(device);
  1856. ret = -ENOMEM;
  1857. goto error;
  1858. }
  1859. rcu_assign_pointer(device->name, name);
  1860. q = bdev_get_queue(bdev);
  1861. if (blk_queue_discard(q))
  1862. device->can_discard = 1;
  1863. mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
  1864. device->writeable = 1;
  1865. device->work.func = pending_bios_fn;
  1866. generate_random_uuid(device->uuid);
  1867. device->devid = BTRFS_DEV_REPLACE_DEVID;
  1868. spin_lock_init(&device->io_lock);
  1869. device->generation = 0;
  1870. device->io_width = root->sectorsize;
  1871. device->io_align = root->sectorsize;
  1872. device->sector_size = root->sectorsize;
  1873. device->total_bytes = i_size_read(bdev->bd_inode);
  1874. device->disk_total_bytes = device->total_bytes;
  1875. device->dev_root = fs_info->dev_root;
  1876. device->bdev = bdev;
  1877. device->in_fs_metadata = 1;
  1878. device->is_tgtdev_for_dev_replace = 1;
  1879. device->mode = FMODE_EXCL;
  1880. set_blocksize(device->bdev, 4096);
  1881. device->fs_devices = fs_info->fs_devices;
  1882. list_add(&device->dev_list, &fs_info->fs_devices->devices);
  1883. fs_info->fs_devices->num_devices++;
  1884. fs_info->fs_devices->open_devices++;
  1885. if (device->can_discard)
  1886. fs_info->fs_devices->num_can_discard++;
  1887. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1888. *device_out = device;
  1889. return ret;
  1890. error:
  1891. blkdev_put(bdev, FMODE_EXCL);
  1892. return ret;
  1893. }
  1894. void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
  1895. struct btrfs_device *tgtdev)
  1896. {
  1897. WARN_ON(fs_info->fs_devices->rw_devices == 0);
  1898. tgtdev->io_width = fs_info->dev_root->sectorsize;
  1899. tgtdev->io_align = fs_info->dev_root->sectorsize;
  1900. tgtdev->sector_size = fs_info->dev_root->sectorsize;
  1901. tgtdev->dev_root = fs_info->dev_root;
  1902. tgtdev->in_fs_metadata = 1;
  1903. }
  1904. static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
  1905. struct btrfs_device *device)
  1906. {
  1907. int ret;
  1908. struct btrfs_path *path;
  1909. struct btrfs_root *root;
  1910. struct btrfs_dev_item *dev_item;
  1911. struct extent_buffer *leaf;
  1912. struct btrfs_key key;
  1913. root = device->dev_root->fs_info->chunk_root;
  1914. path = btrfs_alloc_path();
  1915. if (!path)
  1916. return -ENOMEM;
  1917. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  1918. key.type = BTRFS_DEV_ITEM_KEY;
  1919. key.offset = device->devid;
  1920. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  1921. if (ret < 0)
  1922. goto out;
  1923. if (ret > 0) {
  1924. ret = -ENOENT;
  1925. goto out;
  1926. }
  1927. leaf = path->nodes[0];
  1928. dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
  1929. btrfs_set_device_id(leaf, dev_item, device->devid);
  1930. btrfs_set_device_type(leaf, dev_item, device->type);
  1931. btrfs_set_device_io_align(leaf, dev_item, device->io_align);
  1932. btrfs_set_device_io_width(leaf, dev_item, device->io_width);
  1933. btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
  1934. btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
  1935. btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
  1936. btrfs_mark_buffer_dirty(leaf);
  1937. out:
  1938. btrfs_free_path(path);
  1939. return ret;
  1940. }
  1941. static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
  1942. struct btrfs_device *device, u64 new_size)
  1943. {
  1944. struct btrfs_super_block *super_copy =
  1945. device->dev_root->fs_info->super_copy;
  1946. u64 old_total = btrfs_super_total_bytes(super_copy);
  1947. u64 diff = new_size - device->total_bytes;
  1948. if (!device->writeable)
  1949. return -EACCES;
  1950. if (new_size <= device->total_bytes ||
  1951. device->is_tgtdev_for_dev_replace)
  1952. return -EINVAL;
  1953. btrfs_set_super_total_bytes(super_copy, old_total + diff);
  1954. device->fs_devices->total_rw_bytes += diff;
  1955. device->total_bytes = new_size;
  1956. device->disk_total_bytes = new_size;
  1957. btrfs_clear_space_info_full(device->dev_root->fs_info);
  1958. return btrfs_update_device(trans, device);
  1959. }
  1960. int btrfs_grow_device(struct btrfs_trans_handle *trans,
  1961. struct btrfs_device *device, u64 new_size)
  1962. {
  1963. int ret;
  1964. lock_chunks(device->dev_root);
  1965. ret = __btrfs_grow_device(trans, device, new_size);
  1966. unlock_chunks(device->dev_root);
  1967. return ret;
  1968. }
  1969. static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
  1970. struct btrfs_root *root,
  1971. u64 chunk_tree, u64 chunk_objectid,
  1972. u64 chunk_offset)
  1973. {
  1974. int ret;
  1975. struct btrfs_path *path;
  1976. struct btrfs_key key;
  1977. root = root->fs_info->chunk_root;
  1978. path = btrfs_alloc_path();
  1979. if (!path)
  1980. return -ENOMEM;
  1981. key.objectid = chunk_objectid;
  1982. key.offset = chunk_offset;
  1983. key.type = BTRFS_CHUNK_ITEM_KEY;
  1984. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  1985. if (ret < 0)
  1986. goto out;
  1987. else if (ret > 0) { /* Logic error or corruption */
  1988. btrfs_error(root->fs_info, -ENOENT,
  1989. "Failed lookup while freeing chunk.");
  1990. ret = -ENOENT;
  1991. goto out;
  1992. }
  1993. ret = btrfs_del_item(trans, root, path);
  1994. if (ret < 0)
  1995. btrfs_error(root->fs_info, ret,
  1996. "Failed to delete chunk item.");
  1997. out:
  1998. btrfs_free_path(path);
  1999. return ret;
  2000. }
  2001. static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
  2002. chunk_offset)
  2003. {
  2004. struct btrfs_super_block *super_copy = root->fs_info->super_copy;
  2005. struct btrfs_disk_key *disk_key;
  2006. struct btrfs_chunk *chunk;
  2007. u8 *ptr;
  2008. int ret = 0;
  2009. u32 num_stripes;
  2010. u32 array_size;
  2011. u32 len = 0;
  2012. u32 cur;
  2013. struct btrfs_key key;
  2014. array_size = btrfs_super_sys_array_size(super_copy);
  2015. ptr = super_copy->sys_chunk_array;
  2016. cur = 0;
  2017. while (cur < array_size) {
  2018. disk_key = (struct btrfs_disk_key *)ptr;
  2019. btrfs_disk_key_to_cpu(&key, disk_key);
  2020. len = sizeof(*disk_key);
  2021. if (key.type == BTRFS_CHUNK_ITEM_KEY) {
  2022. chunk = (struct btrfs_chunk *)(ptr + len);
  2023. num_stripes = btrfs_stack_chunk_num_stripes(chunk);
  2024. len += btrfs_chunk_item_size(num_stripes);
  2025. } else {
  2026. ret = -EIO;
  2027. break;
  2028. }
  2029. if (key.objectid == chunk_objectid &&
  2030. key.offset == chunk_offset) {
  2031. memmove(ptr, ptr + len, array_size - (cur + len));
  2032. array_size -= len;
  2033. btrfs_set_super_sys_array_size(super_copy, array_size);
  2034. } else {
  2035. ptr += len;
  2036. cur += len;
  2037. }
  2038. }
  2039. return ret;
  2040. }
  2041. static int btrfs_relocate_chunk(struct btrfs_root *root,
  2042. u64 chunk_tree, u64 chunk_objectid,
  2043. u64 chunk_offset)
  2044. {
  2045. struct extent_map_tree *em_tree;
  2046. struct btrfs_root *extent_root;
  2047. struct btrfs_trans_handle *trans;
  2048. struct extent_map *em;
  2049. struct map_lookup *map;
  2050. int ret;
  2051. int i;
  2052. root = root->fs_info->chunk_root;
  2053. extent_root = root->fs_info->extent_root;
  2054. em_tree = &root->fs_info->mapping_tree.map_tree;
  2055. ret = btrfs_can_relocate(extent_root, chunk_offset);
  2056. if (ret)
  2057. return -ENOSPC;
  2058. /* step one, relocate all the extents inside this chunk */
  2059. ret = btrfs_relocate_block_group(extent_root, chunk_offset);
  2060. if (ret)
  2061. return ret;
  2062. trans = btrfs_start_transaction(root, 0);
  2063. if (IS_ERR(trans)) {
  2064. ret = PTR_ERR(trans);
  2065. btrfs_std_error(root->fs_info, ret);
  2066. return ret;
  2067. }
  2068. lock_chunks(root);
  2069. /*
  2070. * step two, delete the device extents and the
  2071. * chunk tree entries
  2072. */
  2073. read_lock(&em_tree->lock);
  2074. em = lookup_extent_mapping(em_tree, chunk_offset, 1);
  2075. read_unlock(&em_tree->lock);
  2076. BUG_ON(!em || em->start > chunk_offset ||
  2077. em->start + em->len < chunk_offset);
  2078. map = (struct map_lookup *)em->bdev;
  2079. for (i = 0; i < map->num_stripes; i++) {
  2080. ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
  2081. map->stripes[i].physical);
  2082. BUG_ON(ret);
  2083. if (map->stripes[i].dev) {
  2084. ret = btrfs_update_device(trans, map->stripes[i].dev);
  2085. BUG_ON(ret);
  2086. }
  2087. }
  2088. ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
  2089. chunk_offset);
  2090. BUG_ON(ret);
  2091. trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
  2092. if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
  2093. ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
  2094. BUG_ON(ret);
  2095. }
  2096. ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
  2097. BUG_ON(ret);
  2098. write_lock(&em_tree->lock);
  2099. remove_extent_mapping(em_tree, em);
  2100. write_unlock(&em_tree->lock);
  2101. kfree(map);
  2102. em->bdev = NULL;
  2103. /* once for the tree */
  2104. free_extent_map(em);
  2105. /* once for us */
  2106. free_extent_map(em);
  2107. unlock_chunks(root);
  2108. btrfs_end_transaction(trans, root);
  2109. return 0;
  2110. }
  2111. static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
  2112. {
  2113. struct btrfs_root *chunk_root = root->fs_info->chunk_root;
  2114. struct btrfs_path *path;
  2115. struct extent_buffer *leaf;
  2116. struct btrfs_chunk *chunk;
  2117. struct btrfs_key key;
  2118. struct btrfs_key found_key;
  2119. u64 chunk_tree = chunk_root->root_key.objectid;
  2120. u64 chunk_type;
  2121. bool retried = false;
  2122. int failed = 0;
  2123. int ret;
  2124. path = btrfs_alloc_path();
  2125. if (!path)
  2126. return -ENOMEM;
  2127. again:
  2128. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  2129. key.offset = (u64)-1;
  2130. key.type = BTRFS_CHUNK_ITEM_KEY;
  2131. while (1) {
  2132. ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
  2133. if (ret < 0)
  2134. goto error;
  2135. BUG_ON(ret == 0); /* Corruption */
  2136. ret = btrfs_previous_item(chunk_root, path, key.objectid,
  2137. key.type);
  2138. if (ret < 0)
  2139. goto error;
  2140. if (ret > 0)
  2141. break;
  2142. leaf = path->nodes[0];
  2143. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  2144. chunk = btrfs_item_ptr(leaf, path->slots[0],
  2145. struct btrfs_chunk);
  2146. chunk_type = btrfs_chunk_type(leaf, chunk);
  2147. btrfs_release_path(path);
  2148. if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
  2149. ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
  2150. found_key.objectid,
  2151. found_key.offset);
  2152. if (ret == -ENOSPC)
  2153. failed++;
  2154. else if (ret)
  2155. BUG();
  2156. }
  2157. if (found_key.offset == 0)
  2158. break;
  2159. key.offset = found_key.offset - 1;
  2160. }
  2161. ret = 0;
  2162. if (failed && !retried) {
  2163. failed = 0;
  2164. retried = true;
  2165. goto again;
  2166. } else if (failed && retried) {
  2167. WARN_ON(1);
  2168. ret = -ENOSPC;
  2169. }
  2170. error:
  2171. btrfs_free_path(path);
  2172. return ret;
  2173. }
  2174. static int insert_balance_item(struct btrfs_root *root,
  2175. struct btrfs_balance_control *bctl)
  2176. {
  2177. struct btrfs_trans_handle *trans;
  2178. struct btrfs_balance_item *item;
  2179. struct btrfs_disk_balance_args disk_bargs;
  2180. struct btrfs_path *path;
  2181. struct extent_buffer *leaf;
  2182. struct btrfs_key key;
  2183. int ret, err;
  2184. path = btrfs_alloc_path();
  2185. if (!path)
  2186. return -ENOMEM;
  2187. trans = btrfs_start_transaction(root, 0);
  2188. if (IS_ERR(trans)) {
  2189. btrfs_free_path(path);
  2190. return PTR_ERR(trans);
  2191. }
  2192. key.objectid = BTRFS_BALANCE_OBJECTID;
  2193. key.type = BTRFS_BALANCE_ITEM_KEY;
  2194. key.offset = 0;
  2195. ret = btrfs_insert_empty_item(trans, root, path, &key,
  2196. sizeof(*item));
  2197. if (ret)
  2198. goto out;
  2199. leaf = path->nodes[0];
  2200. item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
  2201. memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
  2202. btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
  2203. btrfs_set_balance_data(leaf, item, &disk_bargs);
  2204. btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
  2205. btrfs_set_balance_meta(leaf, item, &disk_bargs);
  2206. btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
  2207. btrfs_set_balance_sys(leaf, item, &disk_bargs);
  2208. btrfs_set_balance_flags(leaf, item, bctl->flags);
  2209. btrfs_mark_buffer_dirty(leaf);
  2210. out:
  2211. btrfs_free_path(path);
  2212. err = btrfs_commit_transaction(trans, root);
  2213. if (err && !ret)
  2214. ret = err;
  2215. return ret;
  2216. }
  2217. static int del_balance_item(struct btrfs_root *root)
  2218. {
  2219. struct btrfs_trans_handle *trans;
  2220. struct btrfs_path *path;
  2221. struct btrfs_key key;
  2222. int ret, err;
  2223. path = btrfs_alloc_path();
  2224. if (!path)
  2225. return -ENOMEM;
  2226. trans = btrfs_start_transaction(root, 0);
  2227. if (IS_ERR(trans)) {
  2228. btrfs_free_path(path);
  2229. return PTR_ERR(trans);
  2230. }
  2231. key.objectid = BTRFS_BALANCE_OBJECTID;
  2232. key.type = BTRFS_BALANCE_ITEM_KEY;
  2233. key.offset = 0;
  2234. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  2235. if (ret < 0)
  2236. goto out;
  2237. if (ret > 0) {
  2238. ret = -ENOENT;
  2239. goto out;
  2240. }
  2241. ret = btrfs_del_item(trans, root, path);
  2242. out:
  2243. btrfs_free_path(path);
  2244. err = btrfs_commit_transaction(trans, root);
  2245. if (err && !ret)
  2246. ret = err;
  2247. return ret;
  2248. }
  2249. /*
  2250. * This is a heuristic used to reduce the number of chunks balanced on
  2251. * resume after balance was interrupted.
  2252. */
  2253. static void update_balance_args(struct btrfs_balance_control *bctl)
  2254. {
  2255. /*
  2256. * Turn on soft mode for chunk types that were being converted.
  2257. */
  2258. if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
  2259. bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
  2260. if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
  2261. bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
  2262. if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
  2263. bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
  2264. /*
  2265. * Turn on usage filter if is not already used. The idea is
  2266. * that chunks that we have already balanced should be
  2267. * reasonably full. Don't do it for chunks that are being
  2268. * converted - that will keep us from relocating unconverted
  2269. * (albeit full) chunks.
  2270. */
  2271. if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
  2272. !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
  2273. bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
  2274. bctl->data.usage = 90;
  2275. }
  2276. if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
  2277. !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
  2278. bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
  2279. bctl->sys.usage = 90;
  2280. }
  2281. if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
  2282. !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
  2283. bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
  2284. bctl->meta.usage = 90;
  2285. }
  2286. }
  2287. /*
  2288. * Should be called with both balance and volume mutexes held to
  2289. * serialize other volume operations (add_dev/rm_dev/resize) with
  2290. * restriper. Same goes for unset_balance_control.
  2291. */
  2292. static void set_balance_control(struct btrfs_balance_control *bctl)
  2293. {
  2294. struct btrfs_fs_info *fs_info = bctl->fs_info;
  2295. BUG_ON(fs_info->balance_ctl);
  2296. spin_lock(&fs_info->balance_lock);
  2297. fs_info->balance_ctl = bctl;
  2298. spin_unlock(&fs_info->balance_lock);
  2299. }
  2300. static void unset_balance_control(struct btrfs_fs_info *fs_info)
  2301. {
  2302. struct btrfs_balance_control *bctl = fs_info->balance_ctl;
  2303. BUG_ON(!fs_info->balance_ctl);
  2304. spin_lock(&fs_info->balance_lock);
  2305. fs_info->balance_ctl = NULL;
  2306. spin_unlock(&fs_info->balance_lock);
  2307. kfree(bctl);
  2308. }
  2309. /*
  2310. * Balance filters. Return 1 if chunk should be filtered out
  2311. * (should not be balanced).
  2312. */
  2313. static int chunk_profiles_filter(u64 chunk_type,
  2314. struct btrfs_balance_args *bargs)
  2315. {
  2316. chunk_type = chunk_to_extended(chunk_type) &
  2317. BTRFS_EXTENDED_PROFILE_MASK;
  2318. if (bargs->profiles & chunk_type)
  2319. return 0;
  2320. return 1;
  2321. }
  2322. static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
  2323. struct btrfs_balance_args *bargs)
  2324. {
  2325. struct btrfs_block_group_cache *cache;
  2326. u64 chunk_used, user_thresh;
  2327. int ret = 1;
  2328. cache = btrfs_lookup_block_group(fs_info, chunk_offset);
  2329. chunk_used = btrfs_block_group_used(&cache->item);
  2330. if (bargs->usage == 0)
  2331. user_thresh = 1;
  2332. else if (bargs->usage > 100)
  2333. user_thresh = cache->key.offset;
  2334. else
  2335. user_thresh = div_factor_fine(cache->key.offset,
  2336. bargs->usage);
  2337. if (chunk_used < user_thresh)
  2338. ret = 0;
  2339. btrfs_put_block_group(cache);
  2340. return ret;
  2341. }
  2342. static int chunk_devid_filter(struct extent_buffer *leaf,
  2343. struct btrfs_chunk *chunk,
  2344. struct btrfs_balance_args *bargs)
  2345. {
  2346. struct btrfs_stripe *stripe;
  2347. int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
  2348. int i;
  2349. for (i = 0; i < num_stripes; i++) {
  2350. stripe = btrfs_stripe_nr(chunk, i);
  2351. if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
  2352. return 0;
  2353. }
  2354. return 1;
  2355. }
  2356. /* [pstart, pend) */
  2357. static int chunk_drange_filter(struct extent_buffer *leaf,
  2358. struct btrfs_chunk *chunk,
  2359. u64 chunk_offset,
  2360. struct btrfs_balance_args *bargs)
  2361. {
  2362. struct btrfs_stripe *stripe;
  2363. int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
  2364. u64 stripe_offset;
  2365. u64 stripe_length;
  2366. int factor;
  2367. int i;
  2368. if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
  2369. return 0;
  2370. if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
  2371. BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
  2372. factor = num_stripes / 2;
  2373. } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
  2374. factor = num_stripes - 1;
  2375. } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
  2376. factor = num_stripes - 2;
  2377. } else {
  2378. factor = num_stripes;
  2379. }
  2380. for (i = 0; i < num_stripes; i++) {
  2381. stripe = btrfs_stripe_nr(chunk, i);
  2382. if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
  2383. continue;
  2384. stripe_offset = btrfs_stripe_offset(leaf, stripe);
  2385. stripe_length = btrfs_chunk_length(leaf, chunk);
  2386. do_div(stripe_length, factor);
  2387. if (stripe_offset < bargs->pend &&
  2388. stripe_offset + stripe_length > bargs->pstart)
  2389. return 0;
  2390. }
  2391. return 1;
  2392. }
  2393. /* [vstart, vend) */
  2394. static int chunk_vrange_filter(struct extent_buffer *leaf,
  2395. struct btrfs_chunk *chunk,
  2396. u64 chunk_offset,
  2397. struct btrfs_balance_args *bargs)
  2398. {
  2399. if (chunk_offset < bargs->vend &&
  2400. chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
  2401. /* at least part of the chunk is inside this vrange */
  2402. return 0;
  2403. return 1;
  2404. }
  2405. static int chunk_soft_convert_filter(u64 chunk_type,
  2406. struct btrfs_balance_args *bargs)
  2407. {
  2408. if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
  2409. return 0;
  2410. chunk_type = chunk_to_extended(chunk_type) &
  2411. BTRFS_EXTENDED_PROFILE_MASK;
  2412. if (bargs->target == chunk_type)
  2413. return 1;
  2414. return 0;
  2415. }
  2416. static int should_balance_chunk(struct btrfs_root *root,
  2417. struct extent_buffer *leaf,
  2418. struct btrfs_chunk *chunk, u64 chunk_offset)
  2419. {
  2420. struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
  2421. struct btrfs_balance_args *bargs = NULL;
  2422. u64 chunk_type = btrfs_chunk_type(leaf, chunk);
  2423. /* type filter */
  2424. if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
  2425. (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
  2426. return 0;
  2427. }
  2428. if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
  2429. bargs = &bctl->data;
  2430. else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
  2431. bargs = &bctl->sys;
  2432. else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
  2433. bargs = &bctl->meta;
  2434. /* profiles filter */
  2435. if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
  2436. chunk_profiles_filter(chunk_type, bargs)) {
  2437. return 0;
  2438. }
  2439. /* usage filter */
  2440. if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
  2441. chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
  2442. return 0;
  2443. }
  2444. /* devid filter */
  2445. if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
  2446. chunk_devid_filter(leaf, chunk, bargs)) {
  2447. return 0;
  2448. }
  2449. /* drange filter, makes sense only with devid filter */
  2450. if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
  2451. chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
  2452. return 0;
  2453. }
  2454. /* vrange filter */
  2455. if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
  2456. chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
  2457. return 0;
  2458. }
  2459. /* soft profile changing mode */
  2460. if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
  2461. chunk_soft_convert_filter(chunk_type, bargs)) {
  2462. return 0;
  2463. }
  2464. return 1;
  2465. }
  2466. static int __btrfs_balance(struct btrfs_fs_info *fs_info)
  2467. {
  2468. struct btrfs_balance_control *bctl = fs_info->balance_ctl;
  2469. struct btrfs_root *chunk_root = fs_info->chunk_root;
  2470. struct btrfs_root *dev_root = fs_info->dev_root;
  2471. struct list_head *devices;
  2472. struct btrfs_device *device;
  2473. u64 old_size;
  2474. u64 size_to_free;
  2475. struct btrfs_chunk *chunk;
  2476. struct btrfs_path *path;
  2477. struct btrfs_key key;
  2478. struct btrfs_key found_key;
  2479. struct btrfs_trans_handle *trans;
  2480. struct extent_buffer *leaf;
  2481. int slot;
  2482. int ret;
  2483. int enospc_errors = 0;
  2484. bool counting = true;
  2485. /* step one make some room on all the devices */
  2486. devices = &fs_info->fs_devices->devices;
  2487. list_for_each_entry(device, devices, dev_list) {
  2488. old_size = device->total_bytes;
  2489. size_to_free = div_factor(old_size, 1);
  2490. size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
  2491. if (!device->writeable ||
  2492. device->total_bytes - device->bytes_used > size_to_free ||
  2493. device->is_tgtdev_for_dev_replace)
  2494. continue;
  2495. ret = btrfs_shrink_device(device, old_size - size_to_free);
  2496. if (ret == -ENOSPC)
  2497. break;
  2498. BUG_ON(ret);
  2499. trans = btrfs_start_transaction(dev_root, 0);
  2500. BUG_ON(IS_ERR(trans));
  2501. ret = btrfs_grow_device(trans, device, old_size);
  2502. BUG_ON(ret);
  2503. btrfs_end_transaction(trans, dev_root);
  2504. }
  2505. /* step two, relocate all the chunks */
  2506. path = btrfs_alloc_path();
  2507. if (!path) {
  2508. ret = -ENOMEM;
  2509. goto error;
  2510. }
  2511. /* zero out stat counters */
  2512. spin_lock(&fs_info->balance_lock);
  2513. memset(&bctl->stat, 0, sizeof(bctl->stat));
  2514. spin_unlock(&fs_info->balance_lock);
  2515. again:
  2516. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  2517. key.offset = (u64)-1;
  2518. key.type = BTRFS_CHUNK_ITEM_KEY;
  2519. while (1) {
  2520. if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
  2521. atomic_read(&fs_info->balance_cancel_req)) {
  2522. ret = -ECANCELED;
  2523. goto error;
  2524. }
  2525. ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
  2526. if (ret < 0)
  2527. goto error;
  2528. /*
  2529. * this shouldn't happen, it means the last relocate
  2530. * failed
  2531. */
  2532. if (ret == 0)
  2533. BUG(); /* FIXME break ? */
  2534. ret = btrfs_previous_item(chunk_root, path, 0,
  2535. BTRFS_CHUNK_ITEM_KEY);
  2536. if (ret) {
  2537. ret = 0;
  2538. break;
  2539. }
  2540. leaf = path->nodes[0];
  2541. slot = path->slots[0];
  2542. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  2543. if (found_key.objectid != key.objectid)
  2544. break;
  2545. /* chunk zero is special */
  2546. if (found_key.offset == 0)
  2547. break;
  2548. chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
  2549. if (!counting) {
  2550. spin_lock(&fs_info->balance_lock);
  2551. bctl->stat.considered++;
  2552. spin_unlock(&fs_info->balance_lock);
  2553. }
  2554. ret = should_balance_chunk(chunk_root, leaf, chunk,
  2555. found_key.offset);
  2556. btrfs_release_path(path);
  2557. if (!ret)
  2558. goto loop;
  2559. if (counting) {
  2560. spin_lock(&fs_info->balance_lock);
  2561. bctl->stat.expected++;
  2562. spin_unlock(&fs_info->balance_lock);
  2563. goto loop;
  2564. }
  2565. ret = btrfs_relocate_chunk(chunk_root,
  2566. chunk_root->root_key.objectid,
  2567. found_key.objectid,
  2568. found_key.offset);
  2569. if (ret && ret != -ENOSPC)
  2570. goto error;
  2571. if (ret == -ENOSPC) {
  2572. enospc_errors++;
  2573. } else {
  2574. spin_lock(&fs_info->balance_lock);
  2575. bctl->stat.completed++;
  2576. spin_unlock(&fs_info->balance_lock);
  2577. }
  2578. loop:
  2579. key.offset = found_key.offset - 1;
  2580. }
  2581. if (counting) {
  2582. btrfs_release_path(path);
  2583. counting = false;
  2584. goto again;
  2585. }
  2586. error:
  2587. btrfs_free_path(path);
  2588. if (enospc_errors) {
  2589. printk(KERN_INFO "btrfs: %d enospc errors during balance\n",
  2590. enospc_errors);
  2591. if (!ret)
  2592. ret = -ENOSPC;
  2593. }
  2594. return ret;
  2595. }
  2596. /**
  2597. * alloc_profile_is_valid - see if a given profile is valid and reduced
  2598. * @flags: profile to validate
  2599. * @extended: if true @flags is treated as an extended profile
  2600. */
  2601. static int alloc_profile_is_valid(u64 flags, int extended)
  2602. {
  2603. u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
  2604. BTRFS_BLOCK_GROUP_PROFILE_MASK);
  2605. flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
  2606. /* 1) check that all other bits are zeroed */
  2607. if (flags & ~mask)
  2608. return 0;
  2609. /* 2) see if profile is reduced */
  2610. if (flags == 0)
  2611. return !extended; /* "0" is valid for usual profiles */
  2612. /* true if exactly one bit set */
  2613. return (flags & (flags - 1)) == 0;
  2614. }
  2615. static inline int balance_need_close(struct btrfs_fs_info *fs_info)
  2616. {
  2617. /* cancel requested || normal exit path */
  2618. return atomic_read(&fs_info->balance_cancel_req) ||
  2619. (atomic_read(&fs_info->balance_pause_req) == 0 &&
  2620. atomic_read(&fs_info->balance_cancel_req) == 0);
  2621. }
  2622. static void __cancel_balance(struct btrfs_fs_info *fs_info)
  2623. {
  2624. int ret;
  2625. unset_balance_control(fs_info);
  2626. ret = del_balance_item(fs_info->tree_root);
  2627. if (ret)
  2628. btrfs_std_error(fs_info, ret);
  2629. atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
  2630. }
  2631. /*
  2632. * Should be called with both balance and volume mutexes held
  2633. */
  2634. int btrfs_balance(struct btrfs_balance_control *bctl,
  2635. struct btrfs_ioctl_balance_args *bargs)
  2636. {
  2637. struct btrfs_fs_info *fs_info = bctl->fs_info;
  2638. u64 allowed;
  2639. int mixed = 0;
  2640. int ret;
  2641. u64 num_devices;
  2642. unsigned seq;
  2643. if (btrfs_fs_closing(fs_info) ||
  2644. atomic_read(&fs_info->balance_pause_req) ||
  2645. atomic_read(&fs_info->balance_cancel_req)) {
  2646. ret = -EINVAL;
  2647. goto out;
  2648. }
  2649. allowed = btrfs_super_incompat_flags(fs_info->super_copy);
  2650. if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
  2651. mixed = 1;
  2652. /*
  2653. * In case of mixed groups both data and meta should be picked,
  2654. * and identical options should be given for both of them.
  2655. */
  2656. allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
  2657. if (mixed && (bctl->flags & allowed)) {
  2658. if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
  2659. !(bctl->flags & BTRFS_BALANCE_METADATA) ||
  2660. memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
  2661. printk(KERN_ERR "btrfs: with mixed groups data and "
  2662. "metadata balance options must be the same\n");
  2663. ret = -EINVAL;
  2664. goto out;
  2665. }
  2666. }
  2667. num_devices = fs_info->fs_devices->num_devices;
  2668. btrfs_dev_replace_lock(&fs_info->dev_replace);
  2669. if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
  2670. BUG_ON(num_devices < 1);
  2671. num_devices--;
  2672. }
  2673. btrfs_dev_replace_unlock(&fs_info->dev_replace);
  2674. allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
  2675. if (num_devices == 1)
  2676. allowed |= BTRFS_BLOCK_GROUP_DUP;
  2677. else if (num_devices > 1)
  2678. allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
  2679. if (num_devices > 2)
  2680. allowed |= BTRFS_BLOCK_GROUP_RAID5;
  2681. if (num_devices > 3)
  2682. allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
  2683. BTRFS_BLOCK_GROUP_RAID6);
  2684. if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
  2685. (!alloc_profile_is_valid(bctl->data.target, 1) ||
  2686. (bctl->data.target & ~allowed))) {
  2687. printk(KERN_ERR "btrfs: unable to start balance with target "
  2688. "data profile %llu\n",
  2689. (unsigned long long)bctl->data.target);
  2690. ret = -EINVAL;
  2691. goto out;
  2692. }
  2693. if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
  2694. (!alloc_profile_is_valid(bctl->meta.target, 1) ||
  2695. (bctl->meta.target & ~allowed))) {
  2696. printk(KERN_ERR "btrfs: unable to start balance with target "
  2697. "metadata profile %llu\n",
  2698. (unsigned long long)bctl->meta.target);
  2699. ret = -EINVAL;
  2700. goto out;
  2701. }
  2702. if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
  2703. (!alloc_profile_is_valid(bctl->sys.target, 1) ||
  2704. (bctl->sys.target & ~allowed))) {
  2705. printk(KERN_ERR "btrfs: unable to start balance with target "
  2706. "system profile %llu\n",
  2707. (unsigned long long)bctl->sys.target);
  2708. ret = -EINVAL;
  2709. goto out;
  2710. }
  2711. /* allow dup'ed data chunks only in mixed mode */
  2712. if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
  2713. (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
  2714. printk(KERN_ERR "btrfs: dup for data is not allowed\n");
  2715. ret = -EINVAL;
  2716. goto out;
  2717. }
  2718. /* allow to reduce meta or sys integrity only if force set */
  2719. allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
  2720. BTRFS_BLOCK_GROUP_RAID10 |
  2721. BTRFS_BLOCK_GROUP_RAID5 |
  2722. BTRFS_BLOCK_GROUP_RAID6;
  2723. do {
  2724. seq = read_seqbegin(&fs_info->profiles_lock);
  2725. if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
  2726. (fs_info->avail_system_alloc_bits & allowed) &&
  2727. !(bctl->sys.target & allowed)) ||
  2728. ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
  2729. (fs_info->avail_metadata_alloc_bits & allowed) &&
  2730. !(bctl->meta.target & allowed))) {
  2731. if (bctl->flags & BTRFS_BALANCE_FORCE) {
  2732. printk(KERN_INFO "btrfs: force reducing metadata "
  2733. "integrity\n");
  2734. } else {
  2735. printk(KERN_ERR "btrfs: balance will reduce metadata "
  2736. "integrity, use force if you want this\n");
  2737. ret = -EINVAL;
  2738. goto out;
  2739. }
  2740. }
  2741. } while (read_seqretry(&fs_info->profiles_lock, seq));
  2742. if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
  2743. int num_tolerated_disk_barrier_failures;
  2744. u64 target = bctl->sys.target;
  2745. num_tolerated_disk_barrier_failures =
  2746. btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
  2747. if (num_tolerated_disk_barrier_failures > 0 &&
  2748. (target &
  2749. (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
  2750. BTRFS_AVAIL_ALLOC_BIT_SINGLE)))
  2751. num_tolerated_disk_barrier_failures = 0;
  2752. else if (num_tolerated_disk_barrier_failures > 1 &&
  2753. (target &
  2754. (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)))
  2755. num_tolerated_disk_barrier_failures = 1;
  2756. fs_info->num_tolerated_disk_barrier_failures =
  2757. num_tolerated_disk_barrier_failures;
  2758. }
  2759. ret = insert_balance_item(fs_info->tree_root, bctl);
  2760. if (ret && ret != -EEXIST)
  2761. goto out;
  2762. if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
  2763. BUG_ON(ret == -EEXIST);
  2764. set_balance_control(bctl);
  2765. } else {
  2766. BUG_ON(ret != -EEXIST);
  2767. spin_lock(&fs_info->balance_lock);
  2768. update_balance_args(bctl);
  2769. spin_unlock(&fs_info->balance_lock);
  2770. }
  2771. atomic_inc(&fs_info->balance_running);
  2772. mutex_unlock(&fs_info->balance_mutex);
  2773. ret = __btrfs_balance(fs_info);
  2774. mutex_lock(&fs_info->balance_mutex);
  2775. atomic_dec(&fs_info->balance_running);
  2776. if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
  2777. fs_info->num_tolerated_disk_barrier_failures =
  2778. btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
  2779. }
  2780. if (bargs) {
  2781. memset(bargs, 0, sizeof(*bargs));
  2782. update_ioctl_balance_args(fs_info, 0, bargs);
  2783. }
  2784. if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
  2785. balance_need_close(fs_info)) {
  2786. __cancel_balance(fs_info);
  2787. }
  2788. wake_up(&fs_info->balance_wait_q);
  2789. return ret;
  2790. out:
  2791. if (bctl->flags & BTRFS_BALANCE_RESUME)
  2792. __cancel_balance(fs_info);
  2793. else {
  2794. kfree(bctl);
  2795. atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
  2796. }
  2797. return ret;
  2798. }
  2799. static int balance_kthread(void *data)
  2800. {
  2801. struct btrfs_fs_info *fs_info = data;
  2802. int ret = 0;
  2803. mutex_lock(&fs_info->volume_mutex);
  2804. mutex_lock(&fs_info->balance_mutex);
  2805. if (fs_info->balance_ctl) {
  2806. printk(KERN_INFO "btrfs: continuing balance\n");
  2807. ret = btrfs_balance(fs_info->balance_ctl, NULL);
  2808. }
  2809. mutex_unlock(&fs_info->balance_mutex);
  2810. mutex_unlock(&fs_info->volume_mutex);
  2811. return ret;
  2812. }
  2813. int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
  2814. {
  2815. struct task_struct *tsk;
  2816. spin_lock(&fs_info->balance_lock);
  2817. if (!fs_info->balance_ctl) {
  2818. spin_unlock(&fs_info->balance_lock);
  2819. return 0;
  2820. }
  2821. spin_unlock(&fs_info->balance_lock);
  2822. if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
  2823. printk(KERN_INFO "btrfs: force skipping balance\n");
  2824. return 0;
  2825. }
  2826. tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
  2827. return PTR_RET(tsk);
  2828. }
  2829. int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
  2830. {
  2831. struct btrfs_balance_control *bctl;
  2832. struct btrfs_balance_item *item;
  2833. struct btrfs_disk_balance_args disk_bargs;
  2834. struct btrfs_path *path;
  2835. struct extent_buffer *leaf;
  2836. struct btrfs_key key;
  2837. int ret;
  2838. path = btrfs_alloc_path();
  2839. if (!path)
  2840. return -ENOMEM;
  2841. key.objectid = BTRFS_BALANCE_OBJECTID;
  2842. key.type = BTRFS_BALANCE_ITEM_KEY;
  2843. key.offset = 0;
  2844. ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
  2845. if (ret < 0)
  2846. goto out;
  2847. if (ret > 0) { /* ret = -ENOENT; */
  2848. ret = 0;
  2849. goto out;
  2850. }
  2851. bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
  2852. if (!bctl) {
  2853. ret = -ENOMEM;
  2854. goto out;
  2855. }
  2856. leaf = path->nodes[0];
  2857. item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
  2858. bctl->fs_info = fs_info;
  2859. bctl->flags = btrfs_balance_flags(leaf, item);
  2860. bctl->flags |= BTRFS_BALANCE_RESUME;
  2861. btrfs_balance_data(leaf, item, &disk_bargs);
  2862. btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
  2863. btrfs_balance_meta(leaf, item, &disk_bargs);
  2864. btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
  2865. btrfs_balance_sys(leaf, item, &disk_bargs);
  2866. btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
  2867. WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
  2868. mutex_lock(&fs_info->volume_mutex);
  2869. mutex_lock(&fs_info->balance_mutex);
  2870. set_balance_control(bctl);
  2871. mutex_unlock(&fs_info->balance_mutex);
  2872. mutex_unlock(&fs_info->volume_mutex);
  2873. out:
  2874. btrfs_free_path(path);
  2875. return ret;
  2876. }
  2877. int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
  2878. {
  2879. int ret = 0;
  2880. mutex_lock(&fs_info->balance_mutex);
  2881. if (!fs_info->balance_ctl) {
  2882. mutex_unlock(&fs_info->balance_mutex);
  2883. return -ENOTCONN;
  2884. }
  2885. if (atomic_read(&fs_info->balance_running)) {
  2886. atomic_inc(&fs_info->balance_pause_req);
  2887. mutex_unlock(&fs_info->balance_mutex);
  2888. wait_event(fs_info->balance_wait_q,
  2889. atomic_read(&fs_info->balance_running) == 0);
  2890. mutex_lock(&fs_info->balance_mutex);
  2891. /* we are good with balance_ctl ripped off from under us */
  2892. BUG_ON(atomic_read(&fs_info->balance_running));
  2893. atomic_dec(&fs_info->balance_pause_req);
  2894. } else {
  2895. ret = -ENOTCONN;
  2896. }
  2897. mutex_unlock(&fs_info->balance_mutex);
  2898. return ret;
  2899. }
  2900. int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
  2901. {
  2902. mutex_lock(&fs_info->balance_mutex);
  2903. if (!fs_info->balance_ctl) {
  2904. mutex_unlock(&fs_info->balance_mutex);
  2905. return -ENOTCONN;
  2906. }
  2907. atomic_inc(&fs_info->balance_cancel_req);
  2908. /*
  2909. * if we are running just wait and return, balance item is
  2910. * deleted in btrfs_balance in this case
  2911. */
  2912. if (atomic_read(&fs_info->balance_running)) {
  2913. mutex_unlock(&fs_info->balance_mutex);
  2914. wait_event(fs_info->balance_wait_q,
  2915. atomic_read(&fs_info->balance_running) == 0);
  2916. mutex_lock(&fs_info->balance_mutex);
  2917. } else {
  2918. /* __cancel_balance needs volume_mutex */
  2919. mutex_unlock(&fs_info->balance_mutex);
  2920. mutex_lock(&fs_info->volume_mutex);
  2921. mutex_lock(&fs_info->balance_mutex);
  2922. if (fs_info->balance_ctl)
  2923. __cancel_balance(fs_info);
  2924. mutex_unlock(&fs_info->volume_mutex);
  2925. }
  2926. BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
  2927. atomic_dec(&fs_info->balance_cancel_req);
  2928. mutex_unlock(&fs_info->balance_mutex);
  2929. return 0;
  2930. }
  2931. static int btrfs_uuid_scan_kthread(void *data)
  2932. {
  2933. struct btrfs_fs_info *fs_info = data;
  2934. struct btrfs_root *root = fs_info->tree_root;
  2935. struct btrfs_key key;
  2936. struct btrfs_key max_key;
  2937. struct btrfs_path *path = NULL;
  2938. int ret = 0;
  2939. struct extent_buffer *eb;
  2940. int slot;
  2941. struct btrfs_root_item root_item;
  2942. u32 item_size;
  2943. struct btrfs_trans_handle *trans;
  2944. path = btrfs_alloc_path();
  2945. if (!path) {
  2946. ret = -ENOMEM;
  2947. goto out;
  2948. }
  2949. key.objectid = 0;
  2950. key.type = BTRFS_ROOT_ITEM_KEY;
  2951. key.offset = 0;
  2952. max_key.objectid = (u64)-1;
  2953. max_key.type = BTRFS_ROOT_ITEM_KEY;
  2954. max_key.offset = (u64)-1;
  2955. path->keep_locks = 1;
  2956. while (1) {
  2957. ret = btrfs_search_forward(root, &key, &max_key, path, 0);
  2958. if (ret) {
  2959. if (ret > 0)
  2960. ret = 0;
  2961. break;
  2962. }
  2963. if (key.type != BTRFS_ROOT_ITEM_KEY ||
  2964. (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
  2965. key.objectid != BTRFS_FS_TREE_OBJECTID) ||
  2966. key.objectid > BTRFS_LAST_FREE_OBJECTID)
  2967. goto skip;
  2968. eb = path->nodes[0];
  2969. slot = path->slots[0];
  2970. item_size = btrfs_item_size_nr(eb, slot);
  2971. if (item_size < sizeof(root_item))
  2972. goto skip;
  2973. trans = NULL;
  2974. read_extent_buffer(eb, &root_item,
  2975. btrfs_item_ptr_offset(eb, slot),
  2976. (int)sizeof(root_item));
  2977. if (btrfs_root_refs(&root_item) == 0)
  2978. goto skip;
  2979. if (!btrfs_is_empty_uuid(root_item.uuid)) {
  2980. /*
  2981. * 1 - subvol uuid item
  2982. * 1 - received_subvol uuid item
  2983. */
  2984. trans = btrfs_start_transaction(fs_info->uuid_root, 2);
  2985. if (IS_ERR(trans)) {
  2986. ret = PTR_ERR(trans);
  2987. break;
  2988. }
  2989. ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
  2990. root_item.uuid,
  2991. BTRFS_UUID_KEY_SUBVOL,
  2992. key.objectid);
  2993. if (ret < 0) {
  2994. pr_warn("btrfs: uuid_tree_add failed %d\n",
  2995. ret);
  2996. btrfs_end_transaction(trans,
  2997. fs_info->uuid_root);
  2998. break;
  2999. }
  3000. }
  3001. if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
  3002. if (!trans) {
  3003. /* 1 - received_subvol uuid item */
  3004. trans = btrfs_start_transaction(
  3005. fs_info->uuid_root, 1);
  3006. if (IS_ERR(trans)) {
  3007. ret = PTR_ERR(trans);
  3008. break;
  3009. }
  3010. }
  3011. ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
  3012. root_item.received_uuid,
  3013. BTRFS_UUID_KEY_RECEIVED_SUBVOL,
  3014. key.objectid);
  3015. if (ret < 0) {
  3016. pr_warn("btrfs: uuid_tree_add failed %d\n",
  3017. ret);
  3018. btrfs_end_transaction(trans,
  3019. fs_info->uuid_root);
  3020. break;
  3021. }
  3022. }
  3023. if (trans) {
  3024. ret = btrfs_end_transaction(trans, fs_info->uuid_root);
  3025. if (ret)
  3026. break;
  3027. }
  3028. skip:
  3029. btrfs_release_path(path);
  3030. if (key.offset < (u64)-1) {
  3031. key.offset++;
  3032. } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
  3033. key.offset = 0;
  3034. key.type = BTRFS_ROOT_ITEM_KEY;
  3035. } else if (key.objectid < (u64)-1) {
  3036. key.offset = 0;
  3037. key.type = BTRFS_ROOT_ITEM_KEY;
  3038. key.objectid++;
  3039. } else {
  3040. break;
  3041. }
  3042. cond_resched();
  3043. }
  3044. out:
  3045. btrfs_free_path(path);
  3046. if (ret)
  3047. pr_warn("btrfs: btrfs_uuid_scan_kthread failed %d\n", ret);
  3048. else
  3049. fs_info->update_uuid_tree_gen = 1;
  3050. up(&fs_info->uuid_tree_rescan_sem);
  3051. return 0;
  3052. }
  3053. /*
  3054. * Callback for btrfs_uuid_tree_iterate().
  3055. * returns:
  3056. * 0 check succeeded, the entry is not outdated.
  3057. * < 0 if an error occured.
  3058. * > 0 if the check failed, which means the caller shall remove the entry.
  3059. */
  3060. static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
  3061. u8 *uuid, u8 type, u64 subid)
  3062. {
  3063. struct btrfs_key key;
  3064. int ret = 0;
  3065. struct btrfs_root *subvol_root;
  3066. if (type != BTRFS_UUID_KEY_SUBVOL &&
  3067. type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
  3068. goto out;
  3069. key.objectid = subid;
  3070. key.type = BTRFS_ROOT_ITEM_KEY;
  3071. key.offset = (u64)-1;
  3072. subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
  3073. if (IS_ERR(subvol_root)) {
  3074. ret = PTR_ERR(subvol_root);
  3075. if (ret == -ENOENT)
  3076. ret = 1;
  3077. goto out;
  3078. }
  3079. switch (type) {
  3080. case BTRFS_UUID_KEY_SUBVOL:
  3081. if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
  3082. ret = 1;
  3083. break;
  3084. case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
  3085. if (memcmp(uuid, subvol_root->root_item.received_uuid,
  3086. BTRFS_UUID_SIZE))
  3087. ret = 1;
  3088. break;
  3089. }
  3090. out:
  3091. return ret;
  3092. }
  3093. static int btrfs_uuid_rescan_kthread(void *data)
  3094. {
  3095. struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
  3096. int ret;
  3097. /*
  3098. * 1st step is to iterate through the existing UUID tree and
  3099. * to delete all entries that contain outdated data.
  3100. * 2nd step is to add all missing entries to the UUID tree.
  3101. */
  3102. ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
  3103. if (ret < 0) {
  3104. pr_warn("btrfs: iterating uuid_tree failed %d\n", ret);
  3105. up(&fs_info->uuid_tree_rescan_sem);
  3106. return ret;
  3107. }
  3108. return btrfs_uuid_scan_kthread(data);
  3109. }
  3110. int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
  3111. {
  3112. struct btrfs_trans_handle *trans;
  3113. struct btrfs_root *tree_root = fs_info->tree_root;
  3114. struct btrfs_root *uuid_root;
  3115. struct task_struct *task;
  3116. int ret;
  3117. /*
  3118. * 1 - root node
  3119. * 1 - root item
  3120. */
  3121. trans = btrfs_start_transaction(tree_root, 2);
  3122. if (IS_ERR(trans))
  3123. return PTR_ERR(trans);
  3124. uuid_root = btrfs_create_tree(trans, fs_info,
  3125. BTRFS_UUID_TREE_OBJECTID);
  3126. if (IS_ERR(uuid_root)) {
  3127. btrfs_abort_transaction(trans, tree_root,
  3128. PTR_ERR(uuid_root));
  3129. return PTR_ERR(uuid_root);
  3130. }
  3131. fs_info->uuid_root = uuid_root;
  3132. ret = btrfs_commit_transaction(trans, tree_root);
  3133. if (ret)
  3134. return ret;
  3135. down(&fs_info->uuid_tree_rescan_sem);
  3136. task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
  3137. if (IS_ERR(task)) {
  3138. /* fs_info->update_uuid_tree_gen remains 0 in all error case */
  3139. pr_warn("btrfs: failed to start uuid_scan task\n");
  3140. up(&fs_info->uuid_tree_rescan_sem);
  3141. return PTR_ERR(task);
  3142. }
  3143. return 0;
  3144. }
  3145. int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
  3146. {
  3147. struct task_struct *task;
  3148. down(&fs_info->uuid_tree_rescan_sem);
  3149. task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
  3150. if (IS_ERR(task)) {
  3151. /* fs_info->update_uuid_tree_gen remains 0 in all error case */
  3152. pr_warn("btrfs: failed to start uuid_rescan task\n");
  3153. up(&fs_info->uuid_tree_rescan_sem);
  3154. return PTR_ERR(task);
  3155. }
  3156. return 0;
  3157. }
  3158. /*
  3159. * shrinking a device means finding all of the device extents past
  3160. * the new size, and then following the back refs to the chunks.
  3161. * The chunk relocation code actually frees the device extent
  3162. */
  3163. int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
  3164. {
  3165. struct btrfs_trans_handle *trans;
  3166. struct btrfs_root *root = device->dev_root;
  3167. struct btrfs_dev_extent *dev_extent = NULL;
  3168. struct btrfs_path *path;
  3169. u64 length;
  3170. u64 chunk_tree;
  3171. u64 chunk_objectid;
  3172. u64 chunk_offset;
  3173. int ret;
  3174. int slot;
  3175. int failed = 0;
  3176. bool retried = false;
  3177. struct extent_buffer *l;
  3178. struct btrfs_key key;
  3179. struct btrfs_super_block *super_copy = root->fs_info->super_copy;
  3180. u64 old_total = btrfs_super_total_bytes(super_copy);
  3181. u64 old_size = device->total_bytes;
  3182. u64 diff = device->total_bytes - new_size;
  3183. if (device->is_tgtdev_for_dev_replace)
  3184. return -EINVAL;
  3185. path = btrfs_alloc_path();
  3186. if (!path)
  3187. return -ENOMEM;
  3188. path->reada = 2;
  3189. lock_chunks(root);
  3190. device->total_bytes = new_size;
  3191. if (device->writeable) {
  3192. device->fs_devices->total_rw_bytes -= diff;
  3193. spin_lock(&root->fs_info->free_chunk_lock);
  3194. root->fs_info->free_chunk_space -= diff;
  3195. spin_unlock(&root->fs_info->free_chunk_lock);
  3196. }
  3197. unlock_chunks(root);
  3198. again:
  3199. key.objectid = device->devid;
  3200. key.offset = (u64)-1;
  3201. key.type = BTRFS_DEV_EXTENT_KEY;
  3202. do {
  3203. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3204. if (ret < 0)
  3205. goto done;
  3206. ret = btrfs_previous_item(root, path, 0, key.type);
  3207. if (ret < 0)
  3208. goto done;
  3209. if (ret) {
  3210. ret = 0;
  3211. btrfs_release_path(path);
  3212. break;
  3213. }
  3214. l = path->nodes[0];
  3215. slot = path->slots[0];
  3216. btrfs_item_key_to_cpu(l, &key, path->slots[0]);
  3217. if (key.objectid != device->devid) {
  3218. btrfs_release_path(path);
  3219. break;
  3220. }
  3221. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  3222. length = btrfs_dev_extent_length(l, dev_extent);
  3223. if (key.offset + length <= new_size) {
  3224. btrfs_release_path(path);
  3225. break;
  3226. }
  3227. chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
  3228. chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
  3229. chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
  3230. btrfs_release_path(path);
  3231. ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
  3232. chunk_offset);
  3233. if (ret && ret != -ENOSPC)
  3234. goto done;
  3235. if (ret == -ENOSPC)
  3236. failed++;
  3237. } while (key.offset-- > 0);
  3238. if (failed && !retried) {
  3239. failed = 0;
  3240. retried = true;
  3241. goto again;
  3242. } else if (failed && retried) {
  3243. ret = -ENOSPC;
  3244. lock_chunks(root);
  3245. device->total_bytes = old_size;
  3246. if (device->writeable)
  3247. device->fs_devices->total_rw_bytes += diff;
  3248. spin_lock(&root->fs_info->free_chunk_lock);
  3249. root->fs_info->free_chunk_space += diff;
  3250. spin_unlock(&root->fs_info->free_chunk_lock);
  3251. unlock_chunks(root);
  3252. goto done;
  3253. }
  3254. /* Shrinking succeeded, else we would be at "done". */
  3255. trans = btrfs_start_transaction(root, 0);
  3256. if (IS_ERR(trans)) {
  3257. ret = PTR_ERR(trans);
  3258. goto done;
  3259. }
  3260. lock_chunks(root);
  3261. device->disk_total_bytes = new_size;
  3262. /* Now btrfs_update_device() will change the on-disk size. */
  3263. ret = btrfs_update_device(trans, device);
  3264. if (ret) {
  3265. unlock_chunks(root);
  3266. btrfs_end_transaction(trans, root);
  3267. goto done;
  3268. }
  3269. WARN_ON(diff > old_total);
  3270. btrfs_set_super_total_bytes(super_copy, old_total - diff);
  3271. unlock_chunks(root);
  3272. btrfs_end_transaction(trans, root);
  3273. done:
  3274. btrfs_free_path(path);
  3275. return ret;
  3276. }
  3277. static int btrfs_add_system_chunk(struct btrfs_root *root,
  3278. struct btrfs_key *key,
  3279. struct btrfs_chunk *chunk, int item_size)
  3280. {
  3281. struct btrfs_super_block *super_copy = root->fs_info->super_copy;
  3282. struct btrfs_disk_key disk_key;
  3283. u32 array_size;
  3284. u8 *ptr;
  3285. array_size = btrfs_super_sys_array_size(super_copy);
  3286. if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
  3287. return -EFBIG;
  3288. ptr = super_copy->sys_chunk_array + array_size;
  3289. btrfs_cpu_key_to_disk(&disk_key, key);
  3290. memcpy(ptr, &disk_key, sizeof(disk_key));
  3291. ptr += sizeof(disk_key);
  3292. memcpy(ptr, chunk, item_size);
  3293. item_size += sizeof(disk_key);
  3294. btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
  3295. return 0;
  3296. }
  3297. /*
  3298. * sort the devices in descending order by max_avail, total_avail
  3299. */
  3300. static int btrfs_cmp_device_info(const void *a, const void *b)
  3301. {
  3302. const struct btrfs_device_info *di_a = a;
  3303. const struct btrfs_device_info *di_b = b;
  3304. if (di_a->max_avail > di_b->max_avail)
  3305. return -1;
  3306. if (di_a->max_avail < di_b->max_avail)
  3307. return 1;
  3308. if (di_a->total_avail > di_b->total_avail)
  3309. return -1;
  3310. if (di_a->total_avail < di_b->total_avail)
  3311. return 1;
  3312. return 0;
  3313. }
  3314. static struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
  3315. [BTRFS_RAID_RAID10] = {
  3316. .sub_stripes = 2,
  3317. .dev_stripes = 1,
  3318. .devs_max = 0, /* 0 == as many as possible */
  3319. .devs_min = 4,
  3320. .devs_increment = 2,
  3321. .ncopies = 2,
  3322. },
  3323. [BTRFS_RAID_RAID1] = {
  3324. .sub_stripes = 1,
  3325. .dev_stripes = 1,
  3326. .devs_max = 2,
  3327. .devs_min = 2,
  3328. .devs_increment = 2,
  3329. .ncopies = 2,
  3330. },
  3331. [BTRFS_RAID_DUP] = {
  3332. .sub_stripes = 1,
  3333. .dev_stripes = 2,
  3334. .devs_max = 1,
  3335. .devs_min = 1,
  3336. .devs_increment = 1,
  3337. .ncopies = 2,
  3338. },
  3339. [BTRFS_RAID_RAID0] = {
  3340. .sub_stripes = 1,
  3341. .dev_stripes = 1,
  3342. .devs_max = 0,
  3343. .devs_min = 2,
  3344. .devs_increment = 1,
  3345. .ncopies = 1,
  3346. },
  3347. [BTRFS_RAID_SINGLE] = {
  3348. .sub_stripes = 1,
  3349. .dev_stripes = 1,
  3350. .devs_max = 1,
  3351. .devs_min = 1,
  3352. .devs_increment = 1,
  3353. .ncopies = 1,
  3354. },
  3355. [BTRFS_RAID_RAID5] = {
  3356. .sub_stripes = 1,
  3357. .dev_stripes = 1,
  3358. .devs_max = 0,
  3359. .devs_min = 2,
  3360. .devs_increment = 1,
  3361. .ncopies = 2,
  3362. },
  3363. [BTRFS_RAID_RAID6] = {
  3364. .sub_stripes = 1,
  3365. .dev_stripes = 1,
  3366. .devs_max = 0,
  3367. .devs_min = 3,
  3368. .devs_increment = 1,
  3369. .ncopies = 3,
  3370. },
  3371. };
  3372. static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
  3373. {
  3374. /* TODO allow them to set a preferred stripe size */
  3375. return 64 * 1024;
  3376. }
  3377. static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
  3378. {
  3379. if (!(type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)))
  3380. return;
  3381. btrfs_set_fs_incompat(info, RAID56);
  3382. }
  3383. static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
  3384. struct btrfs_root *extent_root, u64 start,
  3385. u64 type)
  3386. {
  3387. struct btrfs_fs_info *info = extent_root->fs_info;
  3388. struct btrfs_fs_devices *fs_devices = info->fs_devices;
  3389. struct list_head *cur;
  3390. struct map_lookup *map = NULL;
  3391. struct extent_map_tree *em_tree;
  3392. struct extent_map *em;
  3393. struct btrfs_device_info *devices_info = NULL;
  3394. u64 total_avail;
  3395. int num_stripes; /* total number of stripes to allocate */
  3396. int data_stripes; /* number of stripes that count for
  3397. block group size */
  3398. int sub_stripes; /* sub_stripes info for map */
  3399. int dev_stripes; /* stripes per dev */
  3400. int devs_max; /* max devs to use */
  3401. int devs_min; /* min devs needed */
  3402. int devs_increment; /* ndevs has to be a multiple of this */
  3403. int ncopies; /* how many copies to data has */
  3404. int ret;
  3405. u64 max_stripe_size;
  3406. u64 max_chunk_size;
  3407. u64 stripe_size;
  3408. u64 num_bytes;
  3409. u64 raid_stripe_len = BTRFS_STRIPE_LEN;
  3410. int ndevs;
  3411. int i;
  3412. int j;
  3413. int index;
  3414. BUG_ON(!alloc_profile_is_valid(type, 0));
  3415. if (list_empty(&fs_devices->alloc_list))
  3416. return -ENOSPC;
  3417. index = __get_raid_index(type);
  3418. sub_stripes = btrfs_raid_array[index].sub_stripes;
  3419. dev_stripes = btrfs_raid_array[index].dev_stripes;
  3420. devs_max = btrfs_raid_array[index].devs_max;
  3421. devs_min = btrfs_raid_array[index].devs_min;
  3422. devs_increment = btrfs_raid_array[index].devs_increment;
  3423. ncopies = btrfs_raid_array[index].ncopies;
  3424. if (type & BTRFS_BLOCK_GROUP_DATA) {
  3425. max_stripe_size = 1024 * 1024 * 1024;
  3426. max_chunk_size = 10 * max_stripe_size;
  3427. } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
  3428. /* for larger filesystems, use larger metadata chunks */
  3429. if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
  3430. max_stripe_size = 1024 * 1024 * 1024;
  3431. else
  3432. max_stripe_size = 256 * 1024 * 1024;
  3433. max_chunk_size = max_stripe_size;
  3434. } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
  3435. max_stripe_size = 32 * 1024 * 1024;
  3436. max_chunk_size = 2 * max_stripe_size;
  3437. } else {
  3438. printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
  3439. type);
  3440. BUG_ON(1);
  3441. }
  3442. /* we don't want a chunk larger than 10% of writeable space */
  3443. max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
  3444. max_chunk_size);
  3445. devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
  3446. GFP_NOFS);
  3447. if (!devices_info)
  3448. return -ENOMEM;
  3449. cur = fs_devices->alloc_list.next;
  3450. /*
  3451. * in the first pass through the devices list, we gather information
  3452. * about the available holes on each device.
  3453. */
  3454. ndevs = 0;
  3455. while (cur != &fs_devices->alloc_list) {
  3456. struct btrfs_device *device;
  3457. u64 max_avail;
  3458. u64 dev_offset;
  3459. device = list_entry(cur, struct btrfs_device, dev_alloc_list);
  3460. cur = cur->next;
  3461. if (!device->writeable) {
  3462. WARN(1, KERN_ERR
  3463. "btrfs: read-only device in alloc_list\n");
  3464. continue;
  3465. }
  3466. if (!device->in_fs_metadata ||
  3467. device->is_tgtdev_for_dev_replace)
  3468. continue;
  3469. if (device->total_bytes > device->bytes_used)
  3470. total_avail = device->total_bytes - device->bytes_used;
  3471. else
  3472. total_avail = 0;
  3473. /* If there is no space on this device, skip it. */
  3474. if (total_avail == 0)
  3475. continue;
  3476. ret = find_free_dev_extent(trans, device,
  3477. max_stripe_size * dev_stripes,
  3478. &dev_offset, &max_avail);
  3479. if (ret && ret != -ENOSPC)
  3480. goto error;
  3481. if (ret == 0)
  3482. max_avail = max_stripe_size * dev_stripes;
  3483. if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
  3484. continue;
  3485. if (ndevs == fs_devices->rw_devices) {
  3486. WARN(1, "%s: found more than %llu devices\n",
  3487. __func__, fs_devices->rw_devices);
  3488. break;
  3489. }
  3490. devices_info[ndevs].dev_offset = dev_offset;
  3491. devices_info[ndevs].max_avail = max_avail;
  3492. devices_info[ndevs].total_avail = total_avail;
  3493. devices_info[ndevs].dev = device;
  3494. ++ndevs;
  3495. }
  3496. /*
  3497. * now sort the devices by hole size / available space
  3498. */
  3499. sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
  3500. btrfs_cmp_device_info, NULL);
  3501. /* round down to number of usable stripes */
  3502. ndevs -= ndevs % devs_increment;
  3503. if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
  3504. ret = -ENOSPC;
  3505. goto error;
  3506. }
  3507. if (devs_max && ndevs > devs_max)
  3508. ndevs = devs_max;
  3509. /*
  3510. * the primary goal is to maximize the number of stripes, so use as many
  3511. * devices as possible, even if the stripes are not maximum sized.
  3512. */
  3513. stripe_size = devices_info[ndevs-1].max_avail;
  3514. num_stripes = ndevs * dev_stripes;
  3515. /*
  3516. * this will have to be fixed for RAID1 and RAID10 over
  3517. * more drives
  3518. */
  3519. data_stripes = num_stripes / ncopies;
  3520. if (type & BTRFS_BLOCK_GROUP_RAID5) {
  3521. raid_stripe_len = find_raid56_stripe_len(ndevs - 1,
  3522. btrfs_super_stripesize(info->super_copy));
  3523. data_stripes = num_stripes - 1;
  3524. }
  3525. if (type & BTRFS_BLOCK_GROUP_RAID6) {
  3526. raid_stripe_len = find_raid56_stripe_len(ndevs - 2,
  3527. btrfs_super_stripesize(info->super_copy));
  3528. data_stripes = num_stripes - 2;
  3529. }
  3530. /*
  3531. * Use the number of data stripes to figure out how big this chunk
  3532. * is really going to be in terms of logical address space,
  3533. * and compare that answer with the max chunk size
  3534. */
  3535. if (stripe_size * data_stripes > max_chunk_size) {
  3536. u64 mask = (1ULL << 24) - 1;
  3537. stripe_size = max_chunk_size;
  3538. do_div(stripe_size, data_stripes);
  3539. /* bump the answer up to a 16MB boundary */
  3540. stripe_size = (stripe_size + mask) & ~mask;
  3541. /* but don't go higher than the limits we found
  3542. * while searching for free extents
  3543. */
  3544. if (stripe_size > devices_info[ndevs-1].max_avail)
  3545. stripe_size = devices_info[ndevs-1].max_avail;
  3546. }
  3547. do_div(stripe_size, dev_stripes);
  3548. /* align to BTRFS_STRIPE_LEN */
  3549. do_div(stripe_size, raid_stripe_len);
  3550. stripe_size *= raid_stripe_len;
  3551. map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
  3552. if (!map) {
  3553. ret = -ENOMEM;
  3554. goto error;
  3555. }
  3556. map->num_stripes = num_stripes;
  3557. for (i = 0; i < ndevs; ++i) {
  3558. for (j = 0; j < dev_stripes; ++j) {
  3559. int s = i * dev_stripes + j;
  3560. map->stripes[s].dev = devices_info[i].dev;
  3561. map->stripes[s].physical = devices_info[i].dev_offset +
  3562. j * stripe_size;
  3563. }
  3564. }
  3565. map->sector_size = extent_root->sectorsize;
  3566. map->stripe_len = raid_stripe_len;
  3567. map->io_align = raid_stripe_len;
  3568. map->io_width = raid_stripe_len;
  3569. map->type = type;
  3570. map->sub_stripes = sub_stripes;
  3571. num_bytes = stripe_size * data_stripes;
  3572. trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
  3573. em = alloc_extent_map();
  3574. if (!em) {
  3575. ret = -ENOMEM;
  3576. goto error;
  3577. }
  3578. em->bdev = (struct block_device *)map;
  3579. em->start = start;
  3580. em->len = num_bytes;
  3581. em->block_start = 0;
  3582. em->block_len = em->len;
  3583. em->orig_block_len = stripe_size;
  3584. em_tree = &extent_root->fs_info->mapping_tree.map_tree;
  3585. write_lock(&em_tree->lock);
  3586. ret = add_extent_mapping(em_tree, em, 0);
  3587. if (!ret) {
  3588. list_add_tail(&em->list, &trans->transaction->pending_chunks);
  3589. atomic_inc(&em->refs);
  3590. }
  3591. write_unlock(&em_tree->lock);
  3592. if (ret) {
  3593. free_extent_map(em);
  3594. goto error;
  3595. }
  3596. ret = btrfs_make_block_group(trans, extent_root, 0, type,
  3597. BTRFS_FIRST_CHUNK_TREE_OBJECTID,
  3598. start, num_bytes);
  3599. if (ret)
  3600. goto error_del_extent;
  3601. free_extent_map(em);
  3602. check_raid56_incompat_flag(extent_root->fs_info, type);
  3603. kfree(devices_info);
  3604. return 0;
  3605. error_del_extent:
  3606. write_lock(&em_tree->lock);
  3607. remove_extent_mapping(em_tree, em);
  3608. write_unlock(&em_tree->lock);
  3609. /* One for our allocation */
  3610. free_extent_map(em);
  3611. /* One for the tree reference */
  3612. free_extent_map(em);
  3613. error:
  3614. kfree(map);
  3615. kfree(devices_info);
  3616. return ret;
  3617. }
  3618. int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
  3619. struct btrfs_root *extent_root,
  3620. u64 chunk_offset, u64 chunk_size)
  3621. {
  3622. struct btrfs_key key;
  3623. struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
  3624. struct btrfs_device *device;
  3625. struct btrfs_chunk *chunk;
  3626. struct btrfs_stripe *stripe;
  3627. struct extent_map_tree *em_tree;
  3628. struct extent_map *em;
  3629. struct map_lookup *map;
  3630. size_t item_size;
  3631. u64 dev_offset;
  3632. u64 stripe_size;
  3633. int i = 0;
  3634. int ret;
  3635. em_tree = &extent_root->fs_info->mapping_tree.map_tree;
  3636. read_lock(&em_tree->lock);
  3637. em = lookup_extent_mapping(em_tree, chunk_offset, chunk_size);
  3638. read_unlock(&em_tree->lock);
  3639. if (!em) {
  3640. btrfs_crit(extent_root->fs_info, "unable to find logical "
  3641. "%Lu len %Lu", chunk_offset, chunk_size);
  3642. return -EINVAL;
  3643. }
  3644. if (em->start != chunk_offset || em->len != chunk_size) {
  3645. btrfs_crit(extent_root->fs_info, "found a bad mapping, wanted"
  3646. " %Lu-%Lu, found %Lu-%Lu\n", chunk_offset,
  3647. chunk_size, em->start, em->len);
  3648. free_extent_map(em);
  3649. return -EINVAL;
  3650. }
  3651. map = (struct map_lookup *)em->bdev;
  3652. item_size = btrfs_chunk_item_size(map->num_stripes);
  3653. stripe_size = em->orig_block_len;
  3654. chunk = kzalloc(item_size, GFP_NOFS);
  3655. if (!chunk) {
  3656. ret = -ENOMEM;
  3657. goto out;
  3658. }
  3659. for (i = 0; i < map->num_stripes; i++) {
  3660. device = map->stripes[i].dev;
  3661. dev_offset = map->stripes[i].physical;
  3662. device->bytes_used += stripe_size;
  3663. ret = btrfs_update_device(trans, device);
  3664. if (ret)
  3665. goto out;
  3666. ret = btrfs_alloc_dev_extent(trans, device,
  3667. chunk_root->root_key.objectid,
  3668. BTRFS_FIRST_CHUNK_TREE_OBJECTID,
  3669. chunk_offset, dev_offset,
  3670. stripe_size);
  3671. if (ret)
  3672. goto out;
  3673. }
  3674. spin_lock(&extent_root->fs_info->free_chunk_lock);
  3675. extent_root->fs_info->free_chunk_space -= (stripe_size *
  3676. map->num_stripes);
  3677. spin_unlock(&extent_root->fs_info->free_chunk_lock);
  3678. stripe = &chunk->stripe;
  3679. for (i = 0; i < map->num_stripes; i++) {
  3680. device = map->stripes[i].dev;
  3681. dev_offset = map->stripes[i].physical;
  3682. btrfs_set_stack_stripe_devid(stripe, device->devid);
  3683. btrfs_set_stack_stripe_offset(stripe, dev_offset);
  3684. memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
  3685. stripe++;
  3686. }
  3687. btrfs_set_stack_chunk_length(chunk, chunk_size);
  3688. btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
  3689. btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
  3690. btrfs_set_stack_chunk_type(chunk, map->type);
  3691. btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
  3692. btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
  3693. btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
  3694. btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
  3695. btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
  3696. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  3697. key.type = BTRFS_CHUNK_ITEM_KEY;
  3698. key.offset = chunk_offset;
  3699. ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
  3700. if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
  3701. /*
  3702. * TODO: Cleanup of inserted chunk root in case of
  3703. * failure.
  3704. */
  3705. ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
  3706. item_size);
  3707. }
  3708. out:
  3709. kfree(chunk);
  3710. free_extent_map(em);
  3711. return ret;
  3712. }
  3713. /*
  3714. * Chunk allocation falls into two parts. The first part does works
  3715. * that make the new allocated chunk useable, but not do any operation
  3716. * that modifies the chunk tree. The second part does the works that
  3717. * require modifying the chunk tree. This division is important for the
  3718. * bootstrap process of adding storage to a seed btrfs.
  3719. */
  3720. int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
  3721. struct btrfs_root *extent_root, u64 type)
  3722. {
  3723. u64 chunk_offset;
  3724. chunk_offset = find_next_chunk(extent_root->fs_info);
  3725. return __btrfs_alloc_chunk(trans, extent_root, chunk_offset, type);
  3726. }
  3727. static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
  3728. struct btrfs_root *root,
  3729. struct btrfs_device *device)
  3730. {
  3731. u64 chunk_offset;
  3732. u64 sys_chunk_offset;
  3733. u64 alloc_profile;
  3734. struct btrfs_fs_info *fs_info = root->fs_info;
  3735. struct btrfs_root *extent_root = fs_info->extent_root;
  3736. int ret;
  3737. chunk_offset = find_next_chunk(fs_info);
  3738. alloc_profile = btrfs_get_alloc_profile(extent_root, 0);
  3739. ret = __btrfs_alloc_chunk(trans, extent_root, chunk_offset,
  3740. alloc_profile);
  3741. if (ret)
  3742. return ret;
  3743. sys_chunk_offset = find_next_chunk(root->fs_info);
  3744. alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0);
  3745. ret = __btrfs_alloc_chunk(trans, extent_root, sys_chunk_offset,
  3746. alloc_profile);
  3747. if (ret) {
  3748. btrfs_abort_transaction(trans, root, ret);
  3749. goto out;
  3750. }
  3751. ret = btrfs_add_device(trans, fs_info->chunk_root, device);
  3752. if (ret)
  3753. btrfs_abort_transaction(trans, root, ret);
  3754. out:
  3755. return ret;
  3756. }
  3757. int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
  3758. {
  3759. struct extent_map *em;
  3760. struct map_lookup *map;
  3761. struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
  3762. int readonly = 0;
  3763. int i;
  3764. read_lock(&map_tree->map_tree.lock);
  3765. em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
  3766. read_unlock(&map_tree->map_tree.lock);
  3767. if (!em)
  3768. return 1;
  3769. if (btrfs_test_opt(root, DEGRADED)) {
  3770. free_extent_map(em);
  3771. return 0;
  3772. }
  3773. map = (struct map_lookup *)em->bdev;
  3774. for (i = 0; i < map->num_stripes; i++) {
  3775. if (!map->stripes[i].dev->writeable) {
  3776. readonly = 1;
  3777. break;
  3778. }
  3779. }
  3780. free_extent_map(em);
  3781. return readonly;
  3782. }
  3783. void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
  3784. {
  3785. extent_map_tree_init(&tree->map_tree);
  3786. }
  3787. void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
  3788. {
  3789. struct extent_map *em;
  3790. while (1) {
  3791. write_lock(&tree->map_tree.lock);
  3792. em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
  3793. if (em)
  3794. remove_extent_mapping(&tree->map_tree, em);
  3795. write_unlock(&tree->map_tree.lock);
  3796. if (!em)
  3797. break;
  3798. kfree(em->bdev);
  3799. /* once for us */
  3800. free_extent_map(em);
  3801. /* once for the tree */
  3802. free_extent_map(em);
  3803. }
  3804. }
  3805. int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
  3806. {
  3807. struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
  3808. struct extent_map *em;
  3809. struct map_lookup *map;
  3810. struct extent_map_tree *em_tree = &map_tree->map_tree;
  3811. int ret;
  3812. read_lock(&em_tree->lock);
  3813. em = lookup_extent_mapping(em_tree, logical, len);
  3814. read_unlock(&em_tree->lock);
  3815. /*
  3816. * We could return errors for these cases, but that could get ugly and
  3817. * we'd probably do the same thing which is just not do anything else
  3818. * and exit, so return 1 so the callers don't try to use other copies.
  3819. */
  3820. if (!em) {
  3821. btrfs_crit(fs_info, "No mapping for %Lu-%Lu\n", logical,
  3822. logical+len);
  3823. return 1;
  3824. }
  3825. if (em->start > logical || em->start + em->len < logical) {
  3826. btrfs_crit(fs_info, "Invalid mapping for %Lu-%Lu, got "
  3827. "%Lu-%Lu\n", logical, logical+len, em->start,
  3828. em->start + em->len);
  3829. return 1;
  3830. }
  3831. map = (struct map_lookup *)em->bdev;
  3832. if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
  3833. ret = map->num_stripes;
  3834. else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
  3835. ret = map->sub_stripes;
  3836. else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
  3837. ret = 2;
  3838. else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
  3839. ret = 3;
  3840. else
  3841. ret = 1;
  3842. free_extent_map(em);
  3843. btrfs_dev_replace_lock(&fs_info->dev_replace);
  3844. if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))
  3845. ret++;
  3846. btrfs_dev_replace_unlock(&fs_info->dev_replace);
  3847. return ret;
  3848. }
  3849. unsigned long btrfs_full_stripe_len(struct btrfs_root *root,
  3850. struct btrfs_mapping_tree *map_tree,
  3851. u64 logical)
  3852. {
  3853. struct extent_map *em;
  3854. struct map_lookup *map;
  3855. struct extent_map_tree *em_tree = &map_tree->map_tree;
  3856. unsigned long len = root->sectorsize;
  3857. read_lock(&em_tree->lock);
  3858. em = lookup_extent_mapping(em_tree, logical, len);
  3859. read_unlock(&em_tree->lock);
  3860. BUG_ON(!em);
  3861. BUG_ON(em->start > logical || em->start + em->len < logical);
  3862. map = (struct map_lookup *)em->bdev;
  3863. if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
  3864. BTRFS_BLOCK_GROUP_RAID6)) {
  3865. len = map->stripe_len * nr_data_stripes(map);
  3866. }
  3867. free_extent_map(em);
  3868. return len;
  3869. }
  3870. int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree,
  3871. u64 logical, u64 len, int mirror_num)
  3872. {
  3873. struct extent_map *em;
  3874. struct map_lookup *map;
  3875. struct extent_map_tree *em_tree = &map_tree->map_tree;
  3876. int ret = 0;
  3877. read_lock(&em_tree->lock);
  3878. em = lookup_extent_mapping(em_tree, logical, len);
  3879. read_unlock(&em_tree->lock);
  3880. BUG_ON(!em);
  3881. BUG_ON(em->start > logical || em->start + em->len < logical);
  3882. map = (struct map_lookup *)em->bdev;
  3883. if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
  3884. BTRFS_BLOCK_GROUP_RAID6))
  3885. ret = 1;
  3886. free_extent_map(em);
  3887. return ret;
  3888. }
  3889. static int find_live_mirror(struct btrfs_fs_info *fs_info,
  3890. struct map_lookup *map, int first, int num,
  3891. int optimal, int dev_replace_is_ongoing)
  3892. {
  3893. int i;
  3894. int tolerance;
  3895. struct btrfs_device *srcdev;
  3896. if (dev_replace_is_ongoing &&
  3897. fs_info->dev_replace.cont_reading_from_srcdev_mode ==
  3898. BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
  3899. srcdev = fs_info->dev_replace.srcdev;
  3900. else
  3901. srcdev = NULL;
  3902. /*
  3903. * try to avoid the drive that is the source drive for a
  3904. * dev-replace procedure, only choose it if no other non-missing
  3905. * mirror is available
  3906. */
  3907. for (tolerance = 0; tolerance < 2; tolerance++) {
  3908. if (map->stripes[optimal].dev->bdev &&
  3909. (tolerance || map->stripes[optimal].dev != srcdev))
  3910. return optimal;
  3911. for (i = first; i < first + num; i++) {
  3912. if (map->stripes[i].dev->bdev &&
  3913. (tolerance || map->stripes[i].dev != srcdev))
  3914. return i;
  3915. }
  3916. }
  3917. /* we couldn't find one that doesn't fail. Just return something
  3918. * and the io error handling code will clean up eventually
  3919. */
  3920. return optimal;
  3921. }
  3922. static inline int parity_smaller(u64 a, u64 b)
  3923. {
  3924. return a > b;
  3925. }
  3926. /* Bubble-sort the stripe set to put the parity/syndrome stripes last */
  3927. static void sort_parity_stripes(struct btrfs_bio *bbio, u64 *raid_map)
  3928. {
  3929. struct btrfs_bio_stripe s;
  3930. int i;
  3931. u64 l;
  3932. int again = 1;
  3933. while (again) {
  3934. again = 0;
  3935. for (i = 0; i < bbio->num_stripes - 1; i++) {
  3936. if (parity_smaller(raid_map[i], raid_map[i+1])) {
  3937. s = bbio->stripes[i];
  3938. l = raid_map[i];
  3939. bbio->stripes[i] = bbio->stripes[i+1];
  3940. raid_map[i] = raid_map[i+1];
  3941. bbio->stripes[i+1] = s;
  3942. raid_map[i+1] = l;
  3943. again = 1;
  3944. }
  3945. }
  3946. }
  3947. }
  3948. static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
  3949. u64 logical, u64 *length,
  3950. struct btrfs_bio **bbio_ret,
  3951. int mirror_num, u64 **raid_map_ret)
  3952. {
  3953. struct extent_map *em;
  3954. struct map_lookup *map;
  3955. struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
  3956. struct extent_map_tree *em_tree = &map_tree->map_tree;
  3957. u64 offset;
  3958. u64 stripe_offset;
  3959. u64 stripe_end_offset;
  3960. u64 stripe_nr;
  3961. u64 stripe_nr_orig;
  3962. u64 stripe_nr_end;
  3963. u64 stripe_len;
  3964. u64 *raid_map = NULL;
  3965. int stripe_index;
  3966. int i;
  3967. int ret = 0;
  3968. int num_stripes;
  3969. int max_errors = 0;
  3970. struct btrfs_bio *bbio = NULL;
  3971. struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
  3972. int dev_replace_is_ongoing = 0;
  3973. int num_alloc_stripes;
  3974. int patch_the_first_stripe_for_dev_replace = 0;
  3975. u64 physical_to_patch_in_first_stripe = 0;
  3976. u64 raid56_full_stripe_start = (u64)-1;
  3977. read_lock(&em_tree->lock);
  3978. em = lookup_extent_mapping(em_tree, logical, *length);
  3979. read_unlock(&em_tree->lock);
  3980. if (!em) {
  3981. btrfs_crit(fs_info, "unable to find logical %llu len %llu",
  3982. (unsigned long long)logical,
  3983. (unsigned long long)*length);
  3984. return -EINVAL;
  3985. }
  3986. if (em->start > logical || em->start + em->len < logical) {
  3987. btrfs_crit(fs_info, "found a bad mapping, wanted %Lu, "
  3988. "found %Lu-%Lu\n", logical, em->start,
  3989. em->start + em->len);
  3990. return -EINVAL;
  3991. }
  3992. map = (struct map_lookup *)em->bdev;
  3993. offset = logical - em->start;
  3994. stripe_len = map->stripe_len;
  3995. stripe_nr = offset;
  3996. /*
  3997. * stripe_nr counts the total number of stripes we have to stride
  3998. * to get to this block
  3999. */
  4000. do_div(stripe_nr, stripe_len);
  4001. stripe_offset = stripe_nr * stripe_len;
  4002. BUG_ON(offset < stripe_offset);
  4003. /* stripe_offset is the offset of this block in its stripe*/
  4004. stripe_offset = offset - stripe_offset;
  4005. /* if we're here for raid56, we need to know the stripe aligned start */
  4006. if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
  4007. unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
  4008. raid56_full_stripe_start = offset;
  4009. /* allow a write of a full stripe, but make sure we don't
  4010. * allow straddling of stripes
  4011. */
  4012. do_div(raid56_full_stripe_start, full_stripe_len);
  4013. raid56_full_stripe_start *= full_stripe_len;
  4014. }
  4015. if (rw & REQ_DISCARD) {
  4016. /* we don't discard raid56 yet */
  4017. if (map->type &
  4018. (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
  4019. ret = -EOPNOTSUPP;
  4020. goto out;
  4021. }
  4022. *length = min_t(u64, em->len - offset, *length);
  4023. } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
  4024. u64 max_len;
  4025. /* For writes to RAID[56], allow a full stripeset across all disks.
  4026. For other RAID types and for RAID[56] reads, just allow a single
  4027. stripe (on a single disk). */
  4028. if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6) &&
  4029. (rw & REQ_WRITE)) {
  4030. max_len = stripe_len * nr_data_stripes(map) -
  4031. (offset - raid56_full_stripe_start);
  4032. } else {
  4033. /* we limit the length of each bio to what fits in a stripe */
  4034. max_len = stripe_len - stripe_offset;
  4035. }
  4036. *length = min_t(u64, em->len - offset, max_len);
  4037. } else {
  4038. *length = em->len - offset;
  4039. }
  4040. /* This is for when we're called from btrfs_merge_bio_hook() and all
  4041. it cares about is the length */
  4042. if (!bbio_ret)
  4043. goto out;
  4044. btrfs_dev_replace_lock(dev_replace);
  4045. dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
  4046. if (!dev_replace_is_ongoing)
  4047. btrfs_dev_replace_unlock(dev_replace);
  4048. if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
  4049. !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) &&
  4050. dev_replace->tgtdev != NULL) {
  4051. /*
  4052. * in dev-replace case, for repair case (that's the only
  4053. * case where the mirror is selected explicitly when
  4054. * calling btrfs_map_block), blocks left of the left cursor
  4055. * can also be read from the target drive.
  4056. * For REQ_GET_READ_MIRRORS, the target drive is added as
  4057. * the last one to the array of stripes. For READ, it also
  4058. * needs to be supported using the same mirror number.
  4059. * If the requested block is not left of the left cursor,
  4060. * EIO is returned. This can happen because btrfs_num_copies()
  4061. * returns one more in the dev-replace case.
  4062. */
  4063. u64 tmp_length = *length;
  4064. struct btrfs_bio *tmp_bbio = NULL;
  4065. int tmp_num_stripes;
  4066. u64 srcdev_devid = dev_replace->srcdev->devid;
  4067. int index_srcdev = 0;
  4068. int found = 0;
  4069. u64 physical_of_found = 0;
  4070. ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS,
  4071. logical, &tmp_length, &tmp_bbio, 0, NULL);
  4072. if (ret) {
  4073. WARN_ON(tmp_bbio != NULL);
  4074. goto out;
  4075. }
  4076. tmp_num_stripes = tmp_bbio->num_stripes;
  4077. if (mirror_num > tmp_num_stripes) {
  4078. /*
  4079. * REQ_GET_READ_MIRRORS does not contain this
  4080. * mirror, that means that the requested area
  4081. * is not left of the left cursor
  4082. */
  4083. ret = -EIO;
  4084. kfree(tmp_bbio);
  4085. goto out;
  4086. }
  4087. /*
  4088. * process the rest of the function using the mirror_num
  4089. * of the source drive. Therefore look it up first.
  4090. * At the end, patch the device pointer to the one of the
  4091. * target drive.
  4092. */
  4093. for (i = 0; i < tmp_num_stripes; i++) {
  4094. if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) {
  4095. /*
  4096. * In case of DUP, in order to keep it
  4097. * simple, only add the mirror with the
  4098. * lowest physical address
  4099. */
  4100. if (found &&
  4101. physical_of_found <=
  4102. tmp_bbio->stripes[i].physical)
  4103. continue;
  4104. index_srcdev = i;
  4105. found = 1;
  4106. physical_of_found =
  4107. tmp_bbio->stripes[i].physical;
  4108. }
  4109. }
  4110. if (found) {
  4111. mirror_num = index_srcdev + 1;
  4112. patch_the_first_stripe_for_dev_replace = 1;
  4113. physical_to_patch_in_first_stripe = physical_of_found;
  4114. } else {
  4115. WARN_ON(1);
  4116. ret = -EIO;
  4117. kfree(tmp_bbio);
  4118. goto out;
  4119. }
  4120. kfree(tmp_bbio);
  4121. } else if (mirror_num > map->num_stripes) {
  4122. mirror_num = 0;
  4123. }
  4124. num_stripes = 1;
  4125. stripe_index = 0;
  4126. stripe_nr_orig = stripe_nr;
  4127. stripe_nr_end = ALIGN(offset + *length, map->stripe_len);
  4128. do_div(stripe_nr_end, map->stripe_len);
  4129. stripe_end_offset = stripe_nr_end * map->stripe_len -
  4130. (offset + *length);
  4131. if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
  4132. if (rw & REQ_DISCARD)
  4133. num_stripes = min_t(u64, map->num_stripes,
  4134. stripe_nr_end - stripe_nr_orig);
  4135. stripe_index = do_div(stripe_nr, map->num_stripes);
  4136. } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
  4137. if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))
  4138. num_stripes = map->num_stripes;
  4139. else if (mirror_num)
  4140. stripe_index = mirror_num - 1;
  4141. else {
  4142. stripe_index = find_live_mirror(fs_info, map, 0,
  4143. map->num_stripes,
  4144. current->pid % map->num_stripes,
  4145. dev_replace_is_ongoing);
  4146. mirror_num = stripe_index + 1;
  4147. }
  4148. } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
  4149. if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) {
  4150. num_stripes = map->num_stripes;
  4151. } else if (mirror_num) {
  4152. stripe_index = mirror_num - 1;
  4153. } else {
  4154. mirror_num = 1;
  4155. }
  4156. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  4157. int factor = map->num_stripes / map->sub_stripes;
  4158. stripe_index = do_div(stripe_nr, factor);
  4159. stripe_index *= map->sub_stripes;
  4160. if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
  4161. num_stripes = map->sub_stripes;
  4162. else if (rw & REQ_DISCARD)
  4163. num_stripes = min_t(u64, map->sub_stripes *
  4164. (stripe_nr_end - stripe_nr_orig),
  4165. map->num_stripes);
  4166. else if (mirror_num)
  4167. stripe_index += mirror_num - 1;
  4168. else {
  4169. int old_stripe_index = stripe_index;
  4170. stripe_index = find_live_mirror(fs_info, map,
  4171. stripe_index,
  4172. map->sub_stripes, stripe_index +
  4173. current->pid % map->sub_stripes,
  4174. dev_replace_is_ongoing);
  4175. mirror_num = stripe_index - old_stripe_index + 1;
  4176. }
  4177. } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
  4178. BTRFS_BLOCK_GROUP_RAID6)) {
  4179. u64 tmp;
  4180. if (bbio_ret && ((rw & REQ_WRITE) || mirror_num > 1)
  4181. && raid_map_ret) {
  4182. int i, rot;
  4183. /* push stripe_nr back to the start of the full stripe */
  4184. stripe_nr = raid56_full_stripe_start;
  4185. do_div(stripe_nr, stripe_len);
  4186. stripe_index = do_div(stripe_nr, nr_data_stripes(map));
  4187. /* RAID[56] write or recovery. Return all stripes */
  4188. num_stripes = map->num_stripes;
  4189. max_errors = nr_parity_stripes(map);
  4190. raid_map = kmalloc(sizeof(u64) * num_stripes,
  4191. GFP_NOFS);
  4192. if (!raid_map) {
  4193. ret = -ENOMEM;
  4194. goto out;
  4195. }
  4196. /* Work out the disk rotation on this stripe-set */
  4197. tmp = stripe_nr;
  4198. rot = do_div(tmp, num_stripes);
  4199. /* Fill in the logical address of each stripe */
  4200. tmp = stripe_nr * nr_data_stripes(map);
  4201. for (i = 0; i < nr_data_stripes(map); i++)
  4202. raid_map[(i+rot) % num_stripes] =
  4203. em->start + (tmp + i) * map->stripe_len;
  4204. raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
  4205. if (map->type & BTRFS_BLOCK_GROUP_RAID6)
  4206. raid_map[(i+rot+1) % num_stripes] =
  4207. RAID6_Q_STRIPE;
  4208. *length = map->stripe_len;
  4209. stripe_index = 0;
  4210. stripe_offset = 0;
  4211. } else {
  4212. /*
  4213. * Mirror #0 or #1 means the original data block.
  4214. * Mirror #2 is RAID5 parity block.
  4215. * Mirror #3 is RAID6 Q block.
  4216. */
  4217. stripe_index = do_div(stripe_nr, nr_data_stripes(map));
  4218. if (mirror_num > 1)
  4219. stripe_index = nr_data_stripes(map) +
  4220. mirror_num - 2;
  4221. /* We distribute the parity blocks across stripes */
  4222. tmp = stripe_nr + stripe_index;
  4223. stripe_index = do_div(tmp, map->num_stripes);
  4224. }
  4225. } else {
  4226. /*
  4227. * after this do_div call, stripe_nr is the number of stripes
  4228. * on this device we have to walk to find the data, and
  4229. * stripe_index is the number of our device in the stripe array
  4230. */
  4231. stripe_index = do_div(stripe_nr, map->num_stripes);
  4232. mirror_num = stripe_index + 1;
  4233. }
  4234. BUG_ON(stripe_index >= map->num_stripes);
  4235. num_alloc_stripes = num_stripes;
  4236. if (dev_replace_is_ongoing) {
  4237. if (rw & (REQ_WRITE | REQ_DISCARD))
  4238. num_alloc_stripes <<= 1;
  4239. if (rw & REQ_GET_READ_MIRRORS)
  4240. num_alloc_stripes++;
  4241. }
  4242. bbio = kzalloc(btrfs_bio_size(num_alloc_stripes), GFP_NOFS);
  4243. if (!bbio) {
  4244. kfree(raid_map);
  4245. ret = -ENOMEM;
  4246. goto out;
  4247. }
  4248. atomic_set(&bbio->error, 0);
  4249. if (rw & REQ_DISCARD) {
  4250. int factor = 0;
  4251. int sub_stripes = 0;
  4252. u64 stripes_per_dev = 0;
  4253. u32 remaining_stripes = 0;
  4254. u32 last_stripe = 0;
  4255. if (map->type &
  4256. (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
  4257. if (map->type & BTRFS_BLOCK_GROUP_RAID0)
  4258. sub_stripes = 1;
  4259. else
  4260. sub_stripes = map->sub_stripes;
  4261. factor = map->num_stripes / sub_stripes;
  4262. stripes_per_dev = div_u64_rem(stripe_nr_end -
  4263. stripe_nr_orig,
  4264. factor,
  4265. &remaining_stripes);
  4266. div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
  4267. last_stripe *= sub_stripes;
  4268. }
  4269. for (i = 0; i < num_stripes; i++) {
  4270. bbio->stripes[i].physical =
  4271. map->stripes[stripe_index].physical +
  4272. stripe_offset + stripe_nr * map->stripe_len;
  4273. bbio->stripes[i].dev = map->stripes[stripe_index].dev;
  4274. if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
  4275. BTRFS_BLOCK_GROUP_RAID10)) {
  4276. bbio->stripes[i].length = stripes_per_dev *
  4277. map->stripe_len;
  4278. if (i / sub_stripes < remaining_stripes)
  4279. bbio->stripes[i].length +=
  4280. map->stripe_len;
  4281. /*
  4282. * Special for the first stripe and
  4283. * the last stripe:
  4284. *
  4285. * |-------|...|-------|
  4286. * |----------|
  4287. * off end_off
  4288. */
  4289. if (i < sub_stripes)
  4290. bbio->stripes[i].length -=
  4291. stripe_offset;
  4292. if (stripe_index >= last_stripe &&
  4293. stripe_index <= (last_stripe +
  4294. sub_stripes - 1))
  4295. bbio->stripes[i].length -=
  4296. stripe_end_offset;
  4297. if (i == sub_stripes - 1)
  4298. stripe_offset = 0;
  4299. } else
  4300. bbio->stripes[i].length = *length;
  4301. stripe_index++;
  4302. if (stripe_index == map->num_stripes) {
  4303. /* This could only happen for RAID0/10 */
  4304. stripe_index = 0;
  4305. stripe_nr++;
  4306. }
  4307. }
  4308. } else {
  4309. for (i = 0; i < num_stripes; i++) {
  4310. bbio->stripes[i].physical =
  4311. map->stripes[stripe_index].physical +
  4312. stripe_offset +
  4313. stripe_nr * map->stripe_len;
  4314. bbio->stripes[i].dev =
  4315. map->stripes[stripe_index].dev;
  4316. stripe_index++;
  4317. }
  4318. }
  4319. if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) {
  4320. if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
  4321. BTRFS_BLOCK_GROUP_RAID10 |
  4322. BTRFS_BLOCK_GROUP_RAID5 |
  4323. BTRFS_BLOCK_GROUP_DUP)) {
  4324. max_errors = 1;
  4325. } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
  4326. max_errors = 2;
  4327. }
  4328. }
  4329. if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) &&
  4330. dev_replace->tgtdev != NULL) {
  4331. int index_where_to_add;
  4332. u64 srcdev_devid = dev_replace->srcdev->devid;
  4333. /*
  4334. * duplicate the write operations while the dev replace
  4335. * procedure is running. Since the copying of the old disk
  4336. * to the new disk takes place at run time while the
  4337. * filesystem is mounted writable, the regular write
  4338. * operations to the old disk have to be duplicated to go
  4339. * to the new disk as well.
  4340. * Note that device->missing is handled by the caller, and
  4341. * that the write to the old disk is already set up in the
  4342. * stripes array.
  4343. */
  4344. index_where_to_add = num_stripes;
  4345. for (i = 0; i < num_stripes; i++) {
  4346. if (bbio->stripes[i].dev->devid == srcdev_devid) {
  4347. /* write to new disk, too */
  4348. struct btrfs_bio_stripe *new =
  4349. bbio->stripes + index_where_to_add;
  4350. struct btrfs_bio_stripe *old =
  4351. bbio->stripes + i;
  4352. new->physical = old->physical;
  4353. new->length = old->length;
  4354. new->dev = dev_replace->tgtdev;
  4355. index_where_to_add++;
  4356. max_errors++;
  4357. }
  4358. }
  4359. num_stripes = index_where_to_add;
  4360. } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) &&
  4361. dev_replace->tgtdev != NULL) {
  4362. u64 srcdev_devid = dev_replace->srcdev->devid;
  4363. int index_srcdev = 0;
  4364. int found = 0;
  4365. u64 physical_of_found = 0;
  4366. /*
  4367. * During the dev-replace procedure, the target drive can
  4368. * also be used to read data in case it is needed to repair
  4369. * a corrupt block elsewhere. This is possible if the
  4370. * requested area is left of the left cursor. In this area,
  4371. * the target drive is a full copy of the source drive.
  4372. */
  4373. for (i = 0; i < num_stripes; i++) {
  4374. if (bbio->stripes[i].dev->devid == srcdev_devid) {
  4375. /*
  4376. * In case of DUP, in order to keep it
  4377. * simple, only add the mirror with the
  4378. * lowest physical address
  4379. */
  4380. if (found &&
  4381. physical_of_found <=
  4382. bbio->stripes[i].physical)
  4383. continue;
  4384. index_srcdev = i;
  4385. found = 1;
  4386. physical_of_found = bbio->stripes[i].physical;
  4387. }
  4388. }
  4389. if (found) {
  4390. u64 length = map->stripe_len;
  4391. if (physical_of_found + length <=
  4392. dev_replace->cursor_left) {
  4393. struct btrfs_bio_stripe *tgtdev_stripe =
  4394. bbio->stripes + num_stripes;
  4395. tgtdev_stripe->physical = physical_of_found;
  4396. tgtdev_stripe->length =
  4397. bbio->stripes[index_srcdev].length;
  4398. tgtdev_stripe->dev = dev_replace->tgtdev;
  4399. num_stripes++;
  4400. }
  4401. }
  4402. }
  4403. *bbio_ret = bbio;
  4404. bbio->num_stripes = num_stripes;
  4405. bbio->max_errors = max_errors;
  4406. bbio->mirror_num = mirror_num;
  4407. /*
  4408. * this is the case that REQ_READ && dev_replace_is_ongoing &&
  4409. * mirror_num == num_stripes + 1 && dev_replace target drive is
  4410. * available as a mirror
  4411. */
  4412. if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
  4413. WARN_ON(num_stripes > 1);
  4414. bbio->stripes[0].dev = dev_replace->tgtdev;
  4415. bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
  4416. bbio->mirror_num = map->num_stripes + 1;
  4417. }
  4418. if (raid_map) {
  4419. sort_parity_stripes(bbio, raid_map);
  4420. *raid_map_ret = raid_map;
  4421. }
  4422. out:
  4423. if (dev_replace_is_ongoing)
  4424. btrfs_dev_replace_unlock(dev_replace);
  4425. free_extent_map(em);
  4426. return ret;
  4427. }
  4428. int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
  4429. u64 logical, u64 *length,
  4430. struct btrfs_bio **bbio_ret, int mirror_num)
  4431. {
  4432. return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
  4433. mirror_num, NULL);
  4434. }
  4435. int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
  4436. u64 chunk_start, u64 physical, u64 devid,
  4437. u64 **logical, int *naddrs, int *stripe_len)
  4438. {
  4439. struct extent_map_tree *em_tree = &map_tree->map_tree;
  4440. struct extent_map *em;
  4441. struct map_lookup *map;
  4442. u64 *buf;
  4443. u64 bytenr;
  4444. u64 length;
  4445. u64 stripe_nr;
  4446. u64 rmap_len;
  4447. int i, j, nr = 0;
  4448. read_lock(&em_tree->lock);
  4449. em = lookup_extent_mapping(em_tree, chunk_start, 1);
  4450. read_unlock(&em_tree->lock);
  4451. if (!em) {
  4452. printk(KERN_ERR "btrfs: couldn't find em for chunk %Lu\n",
  4453. chunk_start);
  4454. return -EIO;
  4455. }
  4456. if (em->start != chunk_start) {
  4457. printk(KERN_ERR "btrfs: bad chunk start, em=%Lu, wanted=%Lu\n",
  4458. em->start, chunk_start);
  4459. free_extent_map(em);
  4460. return -EIO;
  4461. }
  4462. map = (struct map_lookup *)em->bdev;
  4463. length = em->len;
  4464. rmap_len = map->stripe_len;
  4465. if (map->type & BTRFS_BLOCK_GROUP_RAID10)
  4466. do_div(length, map->num_stripes / map->sub_stripes);
  4467. else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
  4468. do_div(length, map->num_stripes);
  4469. else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
  4470. BTRFS_BLOCK_GROUP_RAID6)) {
  4471. do_div(length, nr_data_stripes(map));
  4472. rmap_len = map->stripe_len * nr_data_stripes(map);
  4473. }
  4474. buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
  4475. BUG_ON(!buf); /* -ENOMEM */
  4476. for (i = 0; i < map->num_stripes; i++) {
  4477. if (devid && map->stripes[i].dev->devid != devid)
  4478. continue;
  4479. if (map->stripes[i].physical > physical ||
  4480. map->stripes[i].physical + length <= physical)
  4481. continue;
  4482. stripe_nr = physical - map->stripes[i].physical;
  4483. do_div(stripe_nr, map->stripe_len);
  4484. if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  4485. stripe_nr = stripe_nr * map->num_stripes + i;
  4486. do_div(stripe_nr, map->sub_stripes);
  4487. } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
  4488. stripe_nr = stripe_nr * map->num_stripes + i;
  4489. } /* else if RAID[56], multiply by nr_data_stripes().
  4490. * Alternatively, just use rmap_len below instead of
  4491. * map->stripe_len */
  4492. bytenr = chunk_start + stripe_nr * rmap_len;
  4493. WARN_ON(nr >= map->num_stripes);
  4494. for (j = 0; j < nr; j++) {
  4495. if (buf[j] == bytenr)
  4496. break;
  4497. }
  4498. if (j == nr) {
  4499. WARN_ON(nr >= map->num_stripes);
  4500. buf[nr++] = bytenr;
  4501. }
  4502. }
  4503. *logical = buf;
  4504. *naddrs = nr;
  4505. *stripe_len = rmap_len;
  4506. free_extent_map(em);
  4507. return 0;
  4508. }
  4509. static void btrfs_end_bio(struct bio *bio, int err)
  4510. {
  4511. struct btrfs_bio *bbio = bio->bi_private;
  4512. int is_orig_bio = 0;
  4513. if (err) {
  4514. atomic_inc(&bbio->error);
  4515. if (err == -EIO || err == -EREMOTEIO) {
  4516. unsigned int stripe_index =
  4517. btrfs_io_bio(bio)->stripe_index;
  4518. struct btrfs_device *dev;
  4519. BUG_ON(stripe_index >= bbio->num_stripes);
  4520. dev = bbio->stripes[stripe_index].dev;
  4521. if (dev->bdev) {
  4522. if (bio->bi_rw & WRITE)
  4523. btrfs_dev_stat_inc(dev,
  4524. BTRFS_DEV_STAT_WRITE_ERRS);
  4525. else
  4526. btrfs_dev_stat_inc(dev,
  4527. BTRFS_DEV_STAT_READ_ERRS);
  4528. if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
  4529. btrfs_dev_stat_inc(dev,
  4530. BTRFS_DEV_STAT_FLUSH_ERRS);
  4531. btrfs_dev_stat_print_on_error(dev);
  4532. }
  4533. }
  4534. }
  4535. if (bio == bbio->orig_bio)
  4536. is_orig_bio = 1;
  4537. if (atomic_dec_and_test(&bbio->stripes_pending)) {
  4538. if (!is_orig_bio) {
  4539. bio_put(bio);
  4540. bio = bbio->orig_bio;
  4541. }
  4542. bio->bi_private = bbio->private;
  4543. bio->bi_end_io = bbio->end_io;
  4544. btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
  4545. /* only send an error to the higher layers if it is
  4546. * beyond the tolerance of the btrfs bio
  4547. */
  4548. if (atomic_read(&bbio->error) > bbio->max_errors) {
  4549. err = -EIO;
  4550. } else {
  4551. /*
  4552. * this bio is actually up to date, we didn't
  4553. * go over the max number of errors
  4554. */
  4555. set_bit(BIO_UPTODATE, &bio->bi_flags);
  4556. err = 0;
  4557. }
  4558. kfree(bbio);
  4559. bio_endio(bio, err);
  4560. } else if (!is_orig_bio) {
  4561. bio_put(bio);
  4562. }
  4563. }
  4564. struct async_sched {
  4565. struct bio *bio;
  4566. int rw;
  4567. struct btrfs_fs_info *info;
  4568. struct btrfs_work work;
  4569. };
  4570. /*
  4571. * see run_scheduled_bios for a description of why bios are collected for
  4572. * async submit.
  4573. *
  4574. * This will add one bio to the pending list for a device and make sure
  4575. * the work struct is scheduled.
  4576. */
  4577. static noinline void btrfs_schedule_bio(struct btrfs_root *root,
  4578. struct btrfs_device *device,
  4579. int rw, struct bio *bio)
  4580. {
  4581. int should_queue = 1;
  4582. struct btrfs_pending_bios *pending_bios;
  4583. if (device->missing || !device->bdev) {
  4584. bio_endio(bio, -EIO);
  4585. return;
  4586. }
  4587. /* don't bother with additional async steps for reads, right now */
  4588. if (!(rw & REQ_WRITE)) {
  4589. bio_get(bio);
  4590. btrfsic_submit_bio(rw, bio);
  4591. bio_put(bio);
  4592. return;
  4593. }
  4594. /*
  4595. * nr_async_bios allows us to reliably return congestion to the
  4596. * higher layers. Otherwise, the async bio makes it appear we have
  4597. * made progress against dirty pages when we've really just put it
  4598. * on a queue for later
  4599. */
  4600. atomic_inc(&root->fs_info->nr_async_bios);
  4601. WARN_ON(bio->bi_next);
  4602. bio->bi_next = NULL;
  4603. bio->bi_rw |= rw;
  4604. spin_lock(&device->io_lock);
  4605. if (bio->bi_rw & REQ_SYNC)
  4606. pending_bios = &device->pending_sync_bios;
  4607. else
  4608. pending_bios = &device->pending_bios;
  4609. if (pending_bios->tail)
  4610. pending_bios->tail->bi_next = bio;
  4611. pending_bios->tail = bio;
  4612. if (!pending_bios->head)
  4613. pending_bios->head = bio;
  4614. if (device->running_pending)
  4615. should_queue = 0;
  4616. spin_unlock(&device->io_lock);
  4617. if (should_queue)
  4618. btrfs_queue_worker(&root->fs_info->submit_workers,
  4619. &device->work);
  4620. }
  4621. static int bio_size_ok(struct block_device *bdev, struct bio *bio,
  4622. sector_t sector)
  4623. {
  4624. struct bio_vec *prev;
  4625. struct request_queue *q = bdev_get_queue(bdev);
  4626. unsigned short max_sectors = queue_max_sectors(q);
  4627. struct bvec_merge_data bvm = {
  4628. .bi_bdev = bdev,
  4629. .bi_sector = sector,
  4630. .bi_rw = bio->bi_rw,
  4631. };
  4632. if (bio->bi_vcnt == 0) {
  4633. WARN_ON(1);
  4634. return 1;
  4635. }
  4636. prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
  4637. if (bio_sectors(bio) > max_sectors)
  4638. return 0;
  4639. if (!q->merge_bvec_fn)
  4640. return 1;
  4641. bvm.bi_size = bio->bi_size - prev->bv_len;
  4642. if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len)
  4643. return 0;
  4644. return 1;
  4645. }
  4646. static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
  4647. struct bio *bio, u64 physical, int dev_nr,
  4648. int rw, int async)
  4649. {
  4650. struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
  4651. bio->bi_private = bbio;
  4652. btrfs_io_bio(bio)->stripe_index = dev_nr;
  4653. bio->bi_end_io = btrfs_end_bio;
  4654. bio->bi_sector = physical >> 9;
  4655. #ifdef DEBUG
  4656. {
  4657. struct rcu_string *name;
  4658. rcu_read_lock();
  4659. name = rcu_dereference(dev->name);
  4660. pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
  4661. "(%s id %llu), size=%u\n", rw,
  4662. (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
  4663. name->str, dev->devid, bio->bi_size);
  4664. rcu_read_unlock();
  4665. }
  4666. #endif
  4667. bio->bi_bdev = dev->bdev;
  4668. if (async)
  4669. btrfs_schedule_bio(root, dev, rw, bio);
  4670. else
  4671. btrfsic_submit_bio(rw, bio);
  4672. }
  4673. static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
  4674. struct bio *first_bio, struct btrfs_device *dev,
  4675. int dev_nr, int rw, int async)
  4676. {
  4677. struct bio_vec *bvec = first_bio->bi_io_vec;
  4678. struct bio *bio;
  4679. int nr_vecs = bio_get_nr_vecs(dev->bdev);
  4680. u64 physical = bbio->stripes[dev_nr].physical;
  4681. again:
  4682. bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS);
  4683. if (!bio)
  4684. return -ENOMEM;
  4685. while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) {
  4686. if (bio_add_page(bio, bvec->bv_page, bvec->bv_len,
  4687. bvec->bv_offset) < bvec->bv_len) {
  4688. u64 len = bio->bi_size;
  4689. atomic_inc(&bbio->stripes_pending);
  4690. submit_stripe_bio(root, bbio, bio, physical, dev_nr,
  4691. rw, async);
  4692. physical += len;
  4693. goto again;
  4694. }
  4695. bvec++;
  4696. }
  4697. submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async);
  4698. return 0;
  4699. }
  4700. static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
  4701. {
  4702. atomic_inc(&bbio->error);
  4703. if (atomic_dec_and_test(&bbio->stripes_pending)) {
  4704. bio->bi_private = bbio->private;
  4705. bio->bi_end_io = bbio->end_io;
  4706. btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
  4707. bio->bi_sector = logical >> 9;
  4708. kfree(bbio);
  4709. bio_endio(bio, -EIO);
  4710. }
  4711. }
  4712. int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
  4713. int mirror_num, int async_submit)
  4714. {
  4715. struct btrfs_device *dev;
  4716. struct bio *first_bio = bio;
  4717. u64 logical = (u64)bio->bi_sector << 9;
  4718. u64 length = 0;
  4719. u64 map_length;
  4720. u64 *raid_map = NULL;
  4721. int ret;
  4722. int dev_nr = 0;
  4723. int total_devs = 1;
  4724. struct btrfs_bio *bbio = NULL;
  4725. length = bio->bi_size;
  4726. map_length = length;
  4727. ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio,
  4728. mirror_num, &raid_map);
  4729. if (ret) /* -ENOMEM */
  4730. return ret;
  4731. total_devs = bbio->num_stripes;
  4732. bbio->orig_bio = first_bio;
  4733. bbio->private = first_bio->bi_private;
  4734. bbio->end_io = first_bio->bi_end_io;
  4735. atomic_set(&bbio->stripes_pending, bbio->num_stripes);
  4736. if (raid_map) {
  4737. /* In this case, map_length has been set to the length of
  4738. a single stripe; not the whole write */
  4739. if (rw & WRITE) {
  4740. return raid56_parity_write(root, bio, bbio,
  4741. raid_map, map_length);
  4742. } else {
  4743. return raid56_parity_recover(root, bio, bbio,
  4744. raid_map, map_length,
  4745. mirror_num);
  4746. }
  4747. }
  4748. if (map_length < length) {
  4749. btrfs_crit(root->fs_info, "mapping failed logical %llu bio len %llu len %llu",
  4750. (unsigned long long)logical,
  4751. (unsigned long long)length,
  4752. (unsigned long long)map_length);
  4753. BUG();
  4754. }
  4755. while (dev_nr < total_devs) {
  4756. dev = bbio->stripes[dev_nr].dev;
  4757. if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
  4758. bbio_error(bbio, first_bio, logical);
  4759. dev_nr++;
  4760. continue;
  4761. }
  4762. /*
  4763. * Check and see if we're ok with this bio based on it's size
  4764. * and offset with the given device.
  4765. */
  4766. if (!bio_size_ok(dev->bdev, first_bio,
  4767. bbio->stripes[dev_nr].physical >> 9)) {
  4768. ret = breakup_stripe_bio(root, bbio, first_bio, dev,
  4769. dev_nr, rw, async_submit);
  4770. BUG_ON(ret);
  4771. dev_nr++;
  4772. continue;
  4773. }
  4774. if (dev_nr < total_devs - 1) {
  4775. bio = btrfs_bio_clone(first_bio, GFP_NOFS);
  4776. BUG_ON(!bio); /* -ENOMEM */
  4777. } else {
  4778. bio = first_bio;
  4779. }
  4780. submit_stripe_bio(root, bbio, bio,
  4781. bbio->stripes[dev_nr].physical, dev_nr, rw,
  4782. async_submit);
  4783. dev_nr++;
  4784. }
  4785. return 0;
  4786. }
  4787. struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
  4788. u8 *uuid, u8 *fsid)
  4789. {
  4790. struct btrfs_device *device;
  4791. struct btrfs_fs_devices *cur_devices;
  4792. cur_devices = fs_info->fs_devices;
  4793. while (cur_devices) {
  4794. if (!fsid ||
  4795. !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
  4796. device = __find_device(&cur_devices->devices,
  4797. devid, uuid);
  4798. if (device)
  4799. return device;
  4800. }
  4801. cur_devices = cur_devices->seed;
  4802. }
  4803. return NULL;
  4804. }
  4805. static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
  4806. u64 devid, u8 *dev_uuid)
  4807. {
  4808. struct btrfs_device *device;
  4809. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  4810. device = kzalloc(sizeof(*device), GFP_NOFS);
  4811. if (!device)
  4812. return NULL;
  4813. list_add(&device->dev_list,
  4814. &fs_devices->devices);
  4815. device->devid = devid;
  4816. device->work.func = pending_bios_fn;
  4817. device->fs_devices = fs_devices;
  4818. device->missing = 1;
  4819. fs_devices->num_devices++;
  4820. fs_devices->missing_devices++;
  4821. spin_lock_init(&device->io_lock);
  4822. INIT_LIST_HEAD(&device->dev_alloc_list);
  4823. memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
  4824. return device;
  4825. }
  4826. static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
  4827. struct extent_buffer *leaf,
  4828. struct btrfs_chunk *chunk)
  4829. {
  4830. struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
  4831. struct map_lookup *map;
  4832. struct extent_map *em;
  4833. u64 logical;
  4834. u64 length;
  4835. u64 devid;
  4836. u8 uuid[BTRFS_UUID_SIZE];
  4837. int num_stripes;
  4838. int ret;
  4839. int i;
  4840. logical = key->offset;
  4841. length = btrfs_chunk_length(leaf, chunk);
  4842. read_lock(&map_tree->map_tree.lock);
  4843. em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
  4844. read_unlock(&map_tree->map_tree.lock);
  4845. /* already mapped? */
  4846. if (em && em->start <= logical && em->start + em->len > logical) {
  4847. free_extent_map(em);
  4848. return 0;
  4849. } else if (em) {
  4850. free_extent_map(em);
  4851. }
  4852. em = alloc_extent_map();
  4853. if (!em)
  4854. return -ENOMEM;
  4855. num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
  4856. map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
  4857. if (!map) {
  4858. free_extent_map(em);
  4859. return -ENOMEM;
  4860. }
  4861. em->bdev = (struct block_device *)map;
  4862. em->start = logical;
  4863. em->len = length;
  4864. em->orig_start = 0;
  4865. em->block_start = 0;
  4866. em->block_len = em->len;
  4867. map->num_stripes = num_stripes;
  4868. map->io_width = btrfs_chunk_io_width(leaf, chunk);
  4869. map->io_align = btrfs_chunk_io_align(leaf, chunk);
  4870. map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
  4871. map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
  4872. map->type = btrfs_chunk_type(leaf, chunk);
  4873. map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
  4874. for (i = 0; i < num_stripes; i++) {
  4875. map->stripes[i].physical =
  4876. btrfs_stripe_offset_nr(leaf, chunk, i);
  4877. devid = btrfs_stripe_devid_nr(leaf, chunk, i);
  4878. read_extent_buffer(leaf, uuid, (unsigned long)
  4879. btrfs_stripe_dev_uuid_nr(chunk, i),
  4880. BTRFS_UUID_SIZE);
  4881. map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
  4882. uuid, NULL);
  4883. if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
  4884. kfree(map);
  4885. free_extent_map(em);
  4886. return -EIO;
  4887. }
  4888. if (!map->stripes[i].dev) {
  4889. map->stripes[i].dev =
  4890. add_missing_dev(root, devid, uuid);
  4891. if (!map->stripes[i].dev) {
  4892. kfree(map);
  4893. free_extent_map(em);
  4894. return -EIO;
  4895. }
  4896. }
  4897. map->stripes[i].dev->in_fs_metadata = 1;
  4898. }
  4899. write_lock(&map_tree->map_tree.lock);
  4900. ret = add_extent_mapping(&map_tree->map_tree, em, 0);
  4901. write_unlock(&map_tree->map_tree.lock);
  4902. BUG_ON(ret); /* Tree corruption */
  4903. free_extent_map(em);
  4904. return 0;
  4905. }
  4906. static void fill_device_from_item(struct extent_buffer *leaf,
  4907. struct btrfs_dev_item *dev_item,
  4908. struct btrfs_device *device)
  4909. {
  4910. unsigned long ptr;
  4911. device->devid = btrfs_device_id(leaf, dev_item);
  4912. device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
  4913. device->total_bytes = device->disk_total_bytes;
  4914. device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
  4915. device->type = btrfs_device_type(leaf, dev_item);
  4916. device->io_align = btrfs_device_io_align(leaf, dev_item);
  4917. device->io_width = btrfs_device_io_width(leaf, dev_item);
  4918. device->sector_size = btrfs_device_sector_size(leaf, dev_item);
  4919. WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
  4920. device->is_tgtdev_for_dev_replace = 0;
  4921. ptr = (unsigned long)btrfs_device_uuid(dev_item);
  4922. read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
  4923. }
  4924. static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
  4925. {
  4926. struct btrfs_fs_devices *fs_devices;
  4927. int ret;
  4928. BUG_ON(!mutex_is_locked(&uuid_mutex));
  4929. fs_devices = root->fs_info->fs_devices->seed;
  4930. while (fs_devices) {
  4931. if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
  4932. ret = 0;
  4933. goto out;
  4934. }
  4935. fs_devices = fs_devices->seed;
  4936. }
  4937. fs_devices = find_fsid(fsid);
  4938. if (!fs_devices) {
  4939. ret = -ENOENT;
  4940. goto out;
  4941. }
  4942. fs_devices = clone_fs_devices(fs_devices);
  4943. if (IS_ERR(fs_devices)) {
  4944. ret = PTR_ERR(fs_devices);
  4945. goto out;
  4946. }
  4947. ret = __btrfs_open_devices(fs_devices, FMODE_READ,
  4948. root->fs_info->bdev_holder);
  4949. if (ret) {
  4950. free_fs_devices(fs_devices);
  4951. goto out;
  4952. }
  4953. if (!fs_devices->seeding) {
  4954. __btrfs_close_devices(fs_devices);
  4955. free_fs_devices(fs_devices);
  4956. ret = -EINVAL;
  4957. goto out;
  4958. }
  4959. fs_devices->seed = root->fs_info->fs_devices->seed;
  4960. root->fs_info->fs_devices->seed = fs_devices;
  4961. out:
  4962. return ret;
  4963. }
  4964. static int read_one_dev(struct btrfs_root *root,
  4965. struct extent_buffer *leaf,
  4966. struct btrfs_dev_item *dev_item)
  4967. {
  4968. struct btrfs_device *device;
  4969. u64 devid;
  4970. int ret;
  4971. u8 fs_uuid[BTRFS_UUID_SIZE];
  4972. u8 dev_uuid[BTRFS_UUID_SIZE];
  4973. devid = btrfs_device_id(leaf, dev_item);
  4974. read_extent_buffer(leaf, dev_uuid,
  4975. (unsigned long)btrfs_device_uuid(dev_item),
  4976. BTRFS_UUID_SIZE);
  4977. read_extent_buffer(leaf, fs_uuid,
  4978. (unsigned long)btrfs_device_fsid(dev_item),
  4979. BTRFS_UUID_SIZE);
  4980. if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
  4981. ret = open_seed_devices(root, fs_uuid);
  4982. if (ret && !btrfs_test_opt(root, DEGRADED))
  4983. return ret;
  4984. }
  4985. device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid);
  4986. if (!device || !device->bdev) {
  4987. if (!btrfs_test_opt(root, DEGRADED))
  4988. return -EIO;
  4989. if (!device) {
  4990. btrfs_warn(root->fs_info, "devid %llu missing",
  4991. (unsigned long long)devid);
  4992. device = add_missing_dev(root, devid, dev_uuid);
  4993. if (!device)
  4994. return -ENOMEM;
  4995. } else if (!device->missing) {
  4996. /*
  4997. * this happens when a device that was properly setup
  4998. * in the device info lists suddenly goes bad.
  4999. * device->bdev is NULL, and so we have to set
  5000. * device->missing to one here
  5001. */
  5002. root->fs_info->fs_devices->missing_devices++;
  5003. device->missing = 1;
  5004. }
  5005. }
  5006. if (device->fs_devices != root->fs_info->fs_devices) {
  5007. BUG_ON(device->writeable);
  5008. if (device->generation !=
  5009. btrfs_device_generation(leaf, dev_item))
  5010. return -EINVAL;
  5011. }
  5012. fill_device_from_item(leaf, dev_item, device);
  5013. device->in_fs_metadata = 1;
  5014. if (device->writeable && !device->is_tgtdev_for_dev_replace) {
  5015. device->fs_devices->total_rw_bytes += device->total_bytes;
  5016. spin_lock(&root->fs_info->free_chunk_lock);
  5017. root->fs_info->free_chunk_space += device->total_bytes -
  5018. device->bytes_used;
  5019. spin_unlock(&root->fs_info->free_chunk_lock);
  5020. }
  5021. ret = 0;
  5022. return ret;
  5023. }
  5024. int btrfs_read_sys_array(struct btrfs_root *root)
  5025. {
  5026. struct btrfs_super_block *super_copy = root->fs_info->super_copy;
  5027. struct extent_buffer *sb;
  5028. struct btrfs_disk_key *disk_key;
  5029. struct btrfs_chunk *chunk;
  5030. u8 *ptr;
  5031. unsigned long sb_ptr;
  5032. int ret = 0;
  5033. u32 num_stripes;
  5034. u32 array_size;
  5035. u32 len = 0;
  5036. u32 cur;
  5037. struct btrfs_key key;
  5038. sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
  5039. BTRFS_SUPER_INFO_SIZE);
  5040. if (!sb)
  5041. return -ENOMEM;
  5042. btrfs_set_buffer_uptodate(sb);
  5043. btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
  5044. /*
  5045. * The sb extent buffer is artifical and just used to read the system array.
  5046. * btrfs_set_buffer_uptodate() call does not properly mark all it's
  5047. * pages up-to-date when the page is larger: extent does not cover the
  5048. * whole page and consequently check_page_uptodate does not find all
  5049. * the page's extents up-to-date (the hole beyond sb),
  5050. * write_extent_buffer then triggers a WARN_ON.
  5051. *
  5052. * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
  5053. * but sb spans only this function. Add an explicit SetPageUptodate call
  5054. * to silence the warning eg. on PowerPC 64.
  5055. */
  5056. if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
  5057. SetPageUptodate(sb->pages[0]);
  5058. write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
  5059. array_size = btrfs_super_sys_array_size(super_copy);
  5060. ptr = super_copy->sys_chunk_array;
  5061. sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
  5062. cur = 0;
  5063. while (cur < array_size) {
  5064. disk_key = (struct btrfs_disk_key *)ptr;
  5065. btrfs_disk_key_to_cpu(&key, disk_key);
  5066. len = sizeof(*disk_key); ptr += len;
  5067. sb_ptr += len;
  5068. cur += len;
  5069. if (key.type == BTRFS_CHUNK_ITEM_KEY) {
  5070. chunk = (struct btrfs_chunk *)sb_ptr;
  5071. ret = read_one_chunk(root, &key, sb, chunk);
  5072. if (ret)
  5073. break;
  5074. num_stripes = btrfs_chunk_num_stripes(sb, chunk);
  5075. len = btrfs_chunk_item_size(num_stripes);
  5076. } else {
  5077. ret = -EIO;
  5078. break;
  5079. }
  5080. ptr += len;
  5081. sb_ptr += len;
  5082. cur += len;
  5083. }
  5084. free_extent_buffer(sb);
  5085. return ret;
  5086. }
  5087. int btrfs_read_chunk_tree(struct btrfs_root *root)
  5088. {
  5089. struct btrfs_path *path;
  5090. struct extent_buffer *leaf;
  5091. struct btrfs_key key;
  5092. struct btrfs_key found_key;
  5093. int ret;
  5094. int slot;
  5095. root = root->fs_info->chunk_root;
  5096. path = btrfs_alloc_path();
  5097. if (!path)
  5098. return -ENOMEM;
  5099. mutex_lock(&uuid_mutex);
  5100. lock_chunks(root);
  5101. /*
  5102. * Read all device items, and then all the chunk items. All
  5103. * device items are found before any chunk item (their object id
  5104. * is smaller than the lowest possible object id for a chunk
  5105. * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
  5106. */
  5107. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  5108. key.offset = 0;
  5109. key.type = 0;
  5110. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  5111. if (ret < 0)
  5112. goto error;
  5113. while (1) {
  5114. leaf = path->nodes[0];
  5115. slot = path->slots[0];
  5116. if (slot >= btrfs_header_nritems(leaf)) {
  5117. ret = btrfs_next_leaf(root, path);
  5118. if (ret == 0)
  5119. continue;
  5120. if (ret < 0)
  5121. goto error;
  5122. break;
  5123. }
  5124. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  5125. if (found_key.type == BTRFS_DEV_ITEM_KEY) {
  5126. struct btrfs_dev_item *dev_item;
  5127. dev_item = btrfs_item_ptr(leaf, slot,
  5128. struct btrfs_dev_item);
  5129. ret = read_one_dev(root, leaf, dev_item);
  5130. if (ret)
  5131. goto error;
  5132. } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
  5133. struct btrfs_chunk *chunk;
  5134. chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
  5135. ret = read_one_chunk(root, &found_key, leaf, chunk);
  5136. if (ret)
  5137. goto error;
  5138. }
  5139. path->slots[0]++;
  5140. }
  5141. ret = 0;
  5142. error:
  5143. unlock_chunks(root);
  5144. mutex_unlock(&uuid_mutex);
  5145. btrfs_free_path(path);
  5146. return ret;
  5147. }
  5148. void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
  5149. {
  5150. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  5151. struct btrfs_device *device;
  5152. mutex_lock(&fs_devices->device_list_mutex);
  5153. list_for_each_entry(device, &fs_devices->devices, dev_list)
  5154. device->dev_root = fs_info->dev_root;
  5155. mutex_unlock(&fs_devices->device_list_mutex);
  5156. }
  5157. static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
  5158. {
  5159. int i;
  5160. for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
  5161. btrfs_dev_stat_reset(dev, i);
  5162. }
  5163. int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
  5164. {
  5165. struct btrfs_key key;
  5166. struct btrfs_key found_key;
  5167. struct btrfs_root *dev_root = fs_info->dev_root;
  5168. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  5169. struct extent_buffer *eb;
  5170. int slot;
  5171. int ret = 0;
  5172. struct btrfs_device *device;
  5173. struct btrfs_path *path = NULL;
  5174. int i;
  5175. path = btrfs_alloc_path();
  5176. if (!path) {
  5177. ret = -ENOMEM;
  5178. goto out;
  5179. }
  5180. mutex_lock(&fs_devices->device_list_mutex);
  5181. list_for_each_entry(device, &fs_devices->devices, dev_list) {
  5182. int item_size;
  5183. struct btrfs_dev_stats_item *ptr;
  5184. key.objectid = 0;
  5185. key.type = BTRFS_DEV_STATS_KEY;
  5186. key.offset = device->devid;
  5187. ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
  5188. if (ret) {
  5189. __btrfs_reset_dev_stats(device);
  5190. device->dev_stats_valid = 1;
  5191. btrfs_release_path(path);
  5192. continue;
  5193. }
  5194. slot = path->slots[0];
  5195. eb = path->nodes[0];
  5196. btrfs_item_key_to_cpu(eb, &found_key, slot);
  5197. item_size = btrfs_item_size_nr(eb, slot);
  5198. ptr = btrfs_item_ptr(eb, slot,
  5199. struct btrfs_dev_stats_item);
  5200. for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
  5201. if (item_size >= (1 + i) * sizeof(__le64))
  5202. btrfs_dev_stat_set(device, i,
  5203. btrfs_dev_stats_value(eb, ptr, i));
  5204. else
  5205. btrfs_dev_stat_reset(device, i);
  5206. }
  5207. device->dev_stats_valid = 1;
  5208. btrfs_dev_stat_print_on_load(device);
  5209. btrfs_release_path(path);
  5210. }
  5211. mutex_unlock(&fs_devices->device_list_mutex);
  5212. out:
  5213. btrfs_free_path(path);
  5214. return ret < 0 ? ret : 0;
  5215. }
  5216. static int update_dev_stat_item(struct btrfs_trans_handle *trans,
  5217. struct btrfs_root *dev_root,
  5218. struct btrfs_device *device)
  5219. {
  5220. struct btrfs_path *path;
  5221. struct btrfs_key key;
  5222. struct extent_buffer *eb;
  5223. struct btrfs_dev_stats_item *ptr;
  5224. int ret;
  5225. int i;
  5226. key.objectid = 0;
  5227. key.type = BTRFS_DEV_STATS_KEY;
  5228. key.offset = device->devid;
  5229. path = btrfs_alloc_path();
  5230. BUG_ON(!path);
  5231. ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
  5232. if (ret < 0) {
  5233. printk_in_rcu(KERN_WARNING "btrfs: error %d while searching for dev_stats item for device %s!\n",
  5234. ret, rcu_str_deref(device->name));
  5235. goto out;
  5236. }
  5237. if (ret == 0 &&
  5238. btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
  5239. /* need to delete old one and insert a new one */
  5240. ret = btrfs_del_item(trans, dev_root, path);
  5241. if (ret != 0) {
  5242. printk_in_rcu(KERN_WARNING "btrfs: delete too small dev_stats item for device %s failed %d!\n",
  5243. rcu_str_deref(device->name), ret);
  5244. goto out;
  5245. }
  5246. ret = 1;
  5247. }
  5248. if (ret == 1) {
  5249. /* need to insert a new item */
  5250. btrfs_release_path(path);
  5251. ret = btrfs_insert_empty_item(trans, dev_root, path,
  5252. &key, sizeof(*ptr));
  5253. if (ret < 0) {
  5254. printk_in_rcu(KERN_WARNING "btrfs: insert dev_stats item for device %s failed %d!\n",
  5255. rcu_str_deref(device->name), ret);
  5256. goto out;
  5257. }
  5258. }
  5259. eb = path->nodes[0];
  5260. ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
  5261. for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
  5262. btrfs_set_dev_stats_value(eb, ptr, i,
  5263. btrfs_dev_stat_read(device, i));
  5264. btrfs_mark_buffer_dirty(eb);
  5265. out:
  5266. btrfs_free_path(path);
  5267. return ret;
  5268. }
  5269. /*
  5270. * called from commit_transaction. Writes all changed device stats to disk.
  5271. */
  5272. int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
  5273. struct btrfs_fs_info *fs_info)
  5274. {
  5275. struct btrfs_root *dev_root = fs_info->dev_root;
  5276. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  5277. struct btrfs_device *device;
  5278. int ret = 0;
  5279. mutex_lock(&fs_devices->device_list_mutex);
  5280. list_for_each_entry(device, &fs_devices->devices, dev_list) {
  5281. if (!device->dev_stats_valid || !device->dev_stats_dirty)
  5282. continue;
  5283. ret = update_dev_stat_item(trans, dev_root, device);
  5284. if (!ret)
  5285. device->dev_stats_dirty = 0;
  5286. }
  5287. mutex_unlock(&fs_devices->device_list_mutex);
  5288. return ret;
  5289. }
  5290. void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
  5291. {
  5292. btrfs_dev_stat_inc(dev, index);
  5293. btrfs_dev_stat_print_on_error(dev);
  5294. }
  5295. static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
  5296. {
  5297. if (!dev->dev_stats_valid)
  5298. return;
  5299. printk_ratelimited_in_rcu(KERN_ERR
  5300. "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
  5301. rcu_str_deref(dev->name),
  5302. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
  5303. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
  5304. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
  5305. btrfs_dev_stat_read(dev,
  5306. BTRFS_DEV_STAT_CORRUPTION_ERRS),
  5307. btrfs_dev_stat_read(dev,
  5308. BTRFS_DEV_STAT_GENERATION_ERRS));
  5309. }
  5310. static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
  5311. {
  5312. int i;
  5313. for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
  5314. if (btrfs_dev_stat_read(dev, i) != 0)
  5315. break;
  5316. if (i == BTRFS_DEV_STAT_VALUES_MAX)
  5317. return; /* all values == 0, suppress message */
  5318. printk_in_rcu(KERN_INFO "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
  5319. rcu_str_deref(dev->name),
  5320. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
  5321. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
  5322. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
  5323. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
  5324. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
  5325. }
  5326. int btrfs_get_dev_stats(struct btrfs_root *root,
  5327. struct btrfs_ioctl_get_dev_stats *stats)
  5328. {
  5329. struct btrfs_device *dev;
  5330. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  5331. int i;
  5332. mutex_lock(&fs_devices->device_list_mutex);
  5333. dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL);
  5334. mutex_unlock(&fs_devices->device_list_mutex);
  5335. if (!dev) {
  5336. printk(KERN_WARNING
  5337. "btrfs: get dev_stats failed, device not found\n");
  5338. return -ENODEV;
  5339. } else if (!dev->dev_stats_valid) {
  5340. printk(KERN_WARNING
  5341. "btrfs: get dev_stats failed, not yet valid\n");
  5342. return -ENODEV;
  5343. } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
  5344. for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
  5345. if (stats->nr_items > i)
  5346. stats->values[i] =
  5347. btrfs_dev_stat_read_and_reset(dev, i);
  5348. else
  5349. btrfs_dev_stat_reset(dev, i);
  5350. }
  5351. } else {
  5352. for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
  5353. if (stats->nr_items > i)
  5354. stats->values[i] = btrfs_dev_stat_read(dev, i);
  5355. }
  5356. if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
  5357. stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
  5358. return 0;
  5359. }
  5360. int btrfs_scratch_superblock(struct btrfs_device *device)
  5361. {
  5362. struct buffer_head *bh;
  5363. struct btrfs_super_block *disk_super;
  5364. bh = btrfs_read_dev_super(device->bdev);
  5365. if (!bh)
  5366. return -EINVAL;
  5367. disk_super = (struct btrfs_super_block *)bh->b_data;
  5368. memset(&disk_super->magic, 0, sizeof(disk_super->magic));
  5369. set_buffer_dirty(bh);
  5370. sync_dirty_buffer(bh);
  5371. brelse(bh);
  5372. return 0;
  5373. }