volumes.c 189 KB

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