volumes.c 155 KB

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