volumes.c 160 KB

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