volumes.c 161 KB

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