volumes.c 154 KB

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