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