volumes.c 154 KB

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