volumes.c 188 KB

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