volumes.c 173 KB

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