volumes.c 160 KB

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