volumes.c 154 KB

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