volumes.c 155 KB

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