volumes.c 187 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257
  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_offset, u64 start, u64 num_bytes)
  1368. {
  1369. int ret;
  1370. struct btrfs_path *path;
  1371. struct btrfs_fs_info *fs_info = device->fs_info;
  1372. struct btrfs_root *root = fs_info->dev_root;
  1373. struct btrfs_dev_extent *extent;
  1374. struct extent_buffer *leaf;
  1375. struct btrfs_key key;
  1376. WARN_ON(!device->in_fs_metadata);
  1377. WARN_ON(device->is_tgtdev_for_dev_replace);
  1378. path = btrfs_alloc_path();
  1379. if (!path)
  1380. return -ENOMEM;
  1381. key.objectid = device->devid;
  1382. key.offset = start;
  1383. key.type = BTRFS_DEV_EXTENT_KEY;
  1384. ret = btrfs_insert_empty_item(trans, root, path, &key,
  1385. sizeof(*extent));
  1386. if (ret)
  1387. goto out;
  1388. leaf = path->nodes[0];
  1389. extent = btrfs_item_ptr(leaf, path->slots[0],
  1390. struct btrfs_dev_extent);
  1391. btrfs_set_dev_extent_chunk_tree(leaf, extent,
  1392. BTRFS_CHUNK_TREE_OBJECTID);
  1393. btrfs_set_dev_extent_chunk_objectid(leaf, extent,
  1394. BTRFS_FIRST_CHUNK_TREE_OBJECTID);
  1395. btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
  1396. btrfs_set_dev_extent_length(leaf, extent, num_bytes);
  1397. btrfs_mark_buffer_dirty(leaf);
  1398. out:
  1399. btrfs_free_path(path);
  1400. return ret;
  1401. }
  1402. static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
  1403. {
  1404. struct extent_map_tree *em_tree;
  1405. struct extent_map *em;
  1406. struct rb_node *n;
  1407. u64 ret = 0;
  1408. em_tree = &fs_info->mapping_tree.map_tree;
  1409. read_lock(&em_tree->lock);
  1410. n = rb_last(&em_tree->map);
  1411. if (n) {
  1412. em = rb_entry(n, struct extent_map, rb_node);
  1413. ret = em->start + em->len;
  1414. }
  1415. read_unlock(&em_tree->lock);
  1416. return ret;
  1417. }
  1418. static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
  1419. u64 *devid_ret)
  1420. {
  1421. int ret;
  1422. struct btrfs_key key;
  1423. struct btrfs_key found_key;
  1424. struct btrfs_path *path;
  1425. path = btrfs_alloc_path();
  1426. if (!path)
  1427. return -ENOMEM;
  1428. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  1429. key.type = BTRFS_DEV_ITEM_KEY;
  1430. key.offset = (u64)-1;
  1431. ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
  1432. if (ret < 0)
  1433. goto error;
  1434. BUG_ON(ret == 0); /* Corruption */
  1435. ret = btrfs_previous_item(fs_info->chunk_root, path,
  1436. BTRFS_DEV_ITEMS_OBJECTID,
  1437. BTRFS_DEV_ITEM_KEY);
  1438. if (ret) {
  1439. *devid_ret = 1;
  1440. } else {
  1441. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  1442. path->slots[0]);
  1443. *devid_ret = found_key.offset + 1;
  1444. }
  1445. ret = 0;
  1446. error:
  1447. btrfs_free_path(path);
  1448. return ret;
  1449. }
  1450. /*
  1451. * the device information is stored in the chunk root
  1452. * the btrfs_device struct should be fully filled in
  1453. */
  1454. static int btrfs_add_device(struct btrfs_trans_handle *trans,
  1455. struct btrfs_fs_info *fs_info,
  1456. struct btrfs_device *device)
  1457. {
  1458. struct btrfs_root *root = fs_info->chunk_root;
  1459. int ret;
  1460. struct btrfs_path *path;
  1461. struct btrfs_dev_item *dev_item;
  1462. struct extent_buffer *leaf;
  1463. struct btrfs_key key;
  1464. unsigned long ptr;
  1465. path = btrfs_alloc_path();
  1466. if (!path)
  1467. return -ENOMEM;
  1468. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  1469. key.type = BTRFS_DEV_ITEM_KEY;
  1470. key.offset = device->devid;
  1471. ret = btrfs_insert_empty_item(trans, root, path, &key,
  1472. sizeof(*dev_item));
  1473. if (ret)
  1474. goto out;
  1475. leaf = path->nodes[0];
  1476. dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
  1477. btrfs_set_device_id(leaf, dev_item, device->devid);
  1478. btrfs_set_device_generation(leaf, dev_item, 0);
  1479. btrfs_set_device_type(leaf, dev_item, device->type);
  1480. btrfs_set_device_io_align(leaf, dev_item, device->io_align);
  1481. btrfs_set_device_io_width(leaf, dev_item, device->io_width);
  1482. btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
  1483. btrfs_set_device_total_bytes(leaf, dev_item,
  1484. btrfs_device_get_disk_total_bytes(device));
  1485. btrfs_set_device_bytes_used(leaf, dev_item,
  1486. btrfs_device_get_bytes_used(device));
  1487. btrfs_set_device_group(leaf, dev_item, 0);
  1488. btrfs_set_device_seek_speed(leaf, dev_item, 0);
  1489. btrfs_set_device_bandwidth(leaf, dev_item, 0);
  1490. btrfs_set_device_start_offset(leaf, dev_item, 0);
  1491. ptr = btrfs_device_uuid(dev_item);
  1492. write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
  1493. ptr = btrfs_device_fsid(dev_item);
  1494. write_extent_buffer(leaf, fs_info->fsid, ptr, BTRFS_FSID_SIZE);
  1495. btrfs_mark_buffer_dirty(leaf);
  1496. ret = 0;
  1497. out:
  1498. btrfs_free_path(path);
  1499. return ret;
  1500. }
  1501. /*
  1502. * Function to update ctime/mtime for a given device path.
  1503. * Mainly used for ctime/mtime based probe like libblkid.
  1504. */
  1505. static void update_dev_time(const char *path_name)
  1506. {
  1507. struct file *filp;
  1508. filp = filp_open(path_name, O_RDWR, 0);
  1509. if (IS_ERR(filp))
  1510. return;
  1511. file_update_time(filp);
  1512. filp_close(filp, NULL);
  1513. }
  1514. static int btrfs_rm_dev_item(struct btrfs_fs_info *fs_info,
  1515. struct btrfs_device *device)
  1516. {
  1517. struct btrfs_root *root = fs_info->chunk_root;
  1518. int ret;
  1519. struct btrfs_path *path;
  1520. struct btrfs_key key;
  1521. struct btrfs_trans_handle *trans;
  1522. path = btrfs_alloc_path();
  1523. if (!path)
  1524. return -ENOMEM;
  1525. trans = btrfs_start_transaction(root, 0);
  1526. if (IS_ERR(trans)) {
  1527. btrfs_free_path(path);
  1528. return PTR_ERR(trans);
  1529. }
  1530. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  1531. key.type = BTRFS_DEV_ITEM_KEY;
  1532. key.offset = device->devid;
  1533. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  1534. if (ret < 0)
  1535. goto out;
  1536. if (ret > 0) {
  1537. ret = -ENOENT;
  1538. goto out;
  1539. }
  1540. ret = btrfs_del_item(trans, root, path);
  1541. if (ret)
  1542. goto out;
  1543. out:
  1544. btrfs_free_path(path);
  1545. btrfs_commit_transaction(trans);
  1546. return ret;
  1547. }
  1548. /*
  1549. * Verify that @num_devices satisfies the RAID profile constraints in the whole
  1550. * filesystem. It's up to the caller to adjust that number regarding eg. device
  1551. * replace.
  1552. */
  1553. static int btrfs_check_raid_min_devices(struct btrfs_fs_info *fs_info,
  1554. u64 num_devices)
  1555. {
  1556. u64 all_avail;
  1557. unsigned seq;
  1558. int i;
  1559. do {
  1560. seq = read_seqbegin(&fs_info->profiles_lock);
  1561. all_avail = fs_info->avail_data_alloc_bits |
  1562. fs_info->avail_system_alloc_bits |
  1563. fs_info->avail_metadata_alloc_bits;
  1564. } while (read_seqretry(&fs_info->profiles_lock, seq));
  1565. for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
  1566. if (!(all_avail & btrfs_raid_group[i]))
  1567. continue;
  1568. if (num_devices < btrfs_raid_array[i].devs_min) {
  1569. int ret = btrfs_raid_mindev_error[i];
  1570. if (ret)
  1571. return ret;
  1572. }
  1573. }
  1574. return 0;
  1575. }
  1576. struct btrfs_device *btrfs_find_next_active_device(struct btrfs_fs_devices *fs_devs,
  1577. struct btrfs_device *device)
  1578. {
  1579. struct btrfs_device *next_device;
  1580. list_for_each_entry(next_device, &fs_devs->devices, dev_list) {
  1581. if (next_device != device &&
  1582. !next_device->missing && next_device->bdev)
  1583. return next_device;
  1584. }
  1585. return NULL;
  1586. }
  1587. /*
  1588. * Helper function to check if the given device is part of s_bdev / latest_bdev
  1589. * and replace it with the provided or the next active device, in the context
  1590. * where this function called, there should be always be another device (or
  1591. * this_dev) which is active.
  1592. */
  1593. void btrfs_assign_next_active_device(struct btrfs_fs_info *fs_info,
  1594. struct btrfs_device *device, struct btrfs_device *this_dev)
  1595. {
  1596. struct btrfs_device *next_device;
  1597. if (this_dev)
  1598. next_device = this_dev;
  1599. else
  1600. next_device = btrfs_find_next_active_device(fs_info->fs_devices,
  1601. device);
  1602. ASSERT(next_device);
  1603. if (fs_info->sb->s_bdev &&
  1604. (fs_info->sb->s_bdev == device->bdev))
  1605. fs_info->sb->s_bdev = next_device->bdev;
  1606. if (fs_info->fs_devices->latest_bdev == device->bdev)
  1607. fs_info->fs_devices->latest_bdev = next_device->bdev;
  1608. }
  1609. int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path,
  1610. u64 devid)
  1611. {
  1612. struct btrfs_device *device;
  1613. struct btrfs_fs_devices *cur_devices;
  1614. u64 num_devices;
  1615. int ret = 0;
  1616. mutex_lock(&uuid_mutex);
  1617. num_devices = fs_info->fs_devices->num_devices;
  1618. btrfs_dev_replace_lock(&fs_info->dev_replace, 0);
  1619. if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
  1620. WARN_ON(num_devices < 1);
  1621. num_devices--;
  1622. }
  1623. btrfs_dev_replace_unlock(&fs_info->dev_replace, 0);
  1624. ret = btrfs_check_raid_min_devices(fs_info, num_devices - 1);
  1625. if (ret)
  1626. goto out;
  1627. ret = btrfs_find_device_by_devspec(fs_info, devid, device_path,
  1628. &device);
  1629. if (ret)
  1630. goto out;
  1631. if (device->is_tgtdev_for_dev_replace) {
  1632. ret = BTRFS_ERROR_DEV_TGT_REPLACE;
  1633. goto out;
  1634. }
  1635. if (device->writeable && fs_info->fs_devices->rw_devices == 1) {
  1636. ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
  1637. goto out;
  1638. }
  1639. if (device->writeable) {
  1640. mutex_lock(&fs_info->chunk_mutex);
  1641. list_del_init(&device->dev_alloc_list);
  1642. device->fs_devices->rw_devices--;
  1643. mutex_unlock(&fs_info->chunk_mutex);
  1644. }
  1645. mutex_unlock(&uuid_mutex);
  1646. ret = btrfs_shrink_device(device, 0);
  1647. mutex_lock(&uuid_mutex);
  1648. if (ret)
  1649. goto error_undo;
  1650. /*
  1651. * TODO: the superblock still includes this device in its num_devices
  1652. * counter although write_all_supers() is not locked out. This
  1653. * could give a filesystem state which requires a degraded mount.
  1654. */
  1655. ret = btrfs_rm_dev_item(fs_info, device);
  1656. if (ret)
  1657. goto error_undo;
  1658. device->in_fs_metadata = 0;
  1659. btrfs_scrub_cancel_dev(fs_info, device);
  1660. /*
  1661. * the device list mutex makes sure that we don't change
  1662. * the device list while someone else is writing out all
  1663. * the device supers. Whoever is writing all supers, should
  1664. * lock the device list mutex before getting the number of
  1665. * devices in the super block (super_copy). Conversely,
  1666. * whoever updates the number of devices in the super block
  1667. * (super_copy) should hold the device list mutex.
  1668. */
  1669. cur_devices = device->fs_devices;
  1670. mutex_lock(&fs_info->fs_devices->device_list_mutex);
  1671. list_del_rcu(&device->dev_list);
  1672. device->fs_devices->num_devices--;
  1673. device->fs_devices->total_devices--;
  1674. if (device->missing)
  1675. device->fs_devices->missing_devices--;
  1676. btrfs_assign_next_active_device(fs_info, device, NULL);
  1677. if (device->bdev) {
  1678. device->fs_devices->open_devices--;
  1679. /* remove sysfs entry */
  1680. btrfs_sysfs_rm_device_link(fs_info->fs_devices, device);
  1681. }
  1682. num_devices = btrfs_super_num_devices(fs_info->super_copy) - 1;
  1683. btrfs_set_super_num_devices(fs_info->super_copy, num_devices);
  1684. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  1685. /*
  1686. * at this point, the device is zero sized and detached from
  1687. * the devices list. All that's left is to zero out the old
  1688. * supers and free the device.
  1689. */
  1690. if (device->writeable)
  1691. btrfs_scratch_superblocks(device->bdev, device->name->str);
  1692. btrfs_close_bdev(device);
  1693. call_rcu(&device->rcu, free_device);
  1694. if (cur_devices->open_devices == 0) {
  1695. struct btrfs_fs_devices *fs_devices;
  1696. fs_devices = fs_info->fs_devices;
  1697. while (fs_devices) {
  1698. if (fs_devices->seed == cur_devices) {
  1699. fs_devices->seed = cur_devices->seed;
  1700. break;
  1701. }
  1702. fs_devices = fs_devices->seed;
  1703. }
  1704. cur_devices->seed = NULL;
  1705. __btrfs_close_devices(cur_devices);
  1706. free_fs_devices(cur_devices);
  1707. }
  1708. out:
  1709. mutex_unlock(&uuid_mutex);
  1710. return ret;
  1711. error_undo:
  1712. if (device->writeable) {
  1713. mutex_lock(&fs_info->chunk_mutex);
  1714. list_add(&device->dev_alloc_list,
  1715. &fs_info->fs_devices->alloc_list);
  1716. device->fs_devices->rw_devices++;
  1717. mutex_unlock(&fs_info->chunk_mutex);
  1718. }
  1719. goto out;
  1720. }
  1721. void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_fs_info *fs_info,
  1722. struct btrfs_device *srcdev)
  1723. {
  1724. struct btrfs_fs_devices *fs_devices;
  1725. WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
  1726. /*
  1727. * in case of fs with no seed, srcdev->fs_devices will point
  1728. * to fs_devices of fs_info. However when the dev being replaced is
  1729. * a seed dev it will point to the seed's local fs_devices. In short
  1730. * srcdev will have its correct fs_devices in both the cases.
  1731. */
  1732. fs_devices = srcdev->fs_devices;
  1733. list_del_rcu(&srcdev->dev_list);
  1734. list_del_rcu(&srcdev->dev_alloc_list);
  1735. fs_devices->num_devices--;
  1736. if (srcdev->missing)
  1737. fs_devices->missing_devices--;
  1738. if (srcdev->writeable)
  1739. fs_devices->rw_devices--;
  1740. if (srcdev->bdev)
  1741. fs_devices->open_devices--;
  1742. }
  1743. void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info,
  1744. struct btrfs_device *srcdev)
  1745. {
  1746. struct btrfs_fs_devices *fs_devices = srcdev->fs_devices;
  1747. if (srcdev->writeable) {
  1748. /* zero out the old super if it is writable */
  1749. btrfs_scratch_superblocks(srcdev->bdev, srcdev->name->str);
  1750. }
  1751. btrfs_close_bdev(srcdev);
  1752. call_rcu(&srcdev->rcu, free_device);
  1753. /*
  1754. * unless fs_devices is seed fs, num_devices shouldn't go
  1755. * zero
  1756. */
  1757. BUG_ON(!fs_devices->num_devices && !fs_devices->seeding);
  1758. /* if this is no devs we rather delete the fs_devices */
  1759. if (!fs_devices->num_devices) {
  1760. struct btrfs_fs_devices *tmp_fs_devices;
  1761. tmp_fs_devices = fs_info->fs_devices;
  1762. while (tmp_fs_devices) {
  1763. if (tmp_fs_devices->seed == fs_devices) {
  1764. tmp_fs_devices->seed = fs_devices->seed;
  1765. break;
  1766. }
  1767. tmp_fs_devices = tmp_fs_devices->seed;
  1768. }
  1769. fs_devices->seed = NULL;
  1770. __btrfs_close_devices(fs_devices);
  1771. free_fs_devices(fs_devices);
  1772. }
  1773. }
  1774. void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
  1775. struct btrfs_device *tgtdev)
  1776. {
  1777. mutex_lock(&uuid_mutex);
  1778. WARN_ON(!tgtdev);
  1779. mutex_lock(&fs_info->fs_devices->device_list_mutex);
  1780. btrfs_sysfs_rm_device_link(fs_info->fs_devices, tgtdev);
  1781. if (tgtdev->bdev)
  1782. fs_info->fs_devices->open_devices--;
  1783. fs_info->fs_devices->num_devices--;
  1784. btrfs_assign_next_active_device(fs_info, tgtdev, NULL);
  1785. list_del_rcu(&tgtdev->dev_list);
  1786. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  1787. mutex_unlock(&uuid_mutex);
  1788. /*
  1789. * The update_dev_time() with in btrfs_scratch_superblocks()
  1790. * may lead to a call to btrfs_show_devname() which will try
  1791. * to hold device_list_mutex. And here this device
  1792. * is already out of device list, so we don't have to hold
  1793. * the device_list_mutex lock.
  1794. */
  1795. btrfs_scratch_superblocks(tgtdev->bdev, tgtdev->name->str);
  1796. btrfs_close_bdev(tgtdev);
  1797. call_rcu(&tgtdev->rcu, free_device);
  1798. }
  1799. static int btrfs_find_device_by_path(struct btrfs_fs_info *fs_info,
  1800. const char *device_path,
  1801. struct btrfs_device **device)
  1802. {
  1803. int ret = 0;
  1804. struct btrfs_super_block *disk_super;
  1805. u64 devid;
  1806. u8 *dev_uuid;
  1807. struct block_device *bdev;
  1808. struct buffer_head *bh;
  1809. *device = NULL;
  1810. ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
  1811. fs_info->bdev_holder, 0, &bdev, &bh);
  1812. if (ret)
  1813. return ret;
  1814. disk_super = (struct btrfs_super_block *)bh->b_data;
  1815. devid = btrfs_stack_device_id(&disk_super->dev_item);
  1816. dev_uuid = disk_super->dev_item.uuid;
  1817. *device = btrfs_find_device(fs_info, devid, dev_uuid, disk_super->fsid);
  1818. brelse(bh);
  1819. if (!*device)
  1820. ret = -ENOENT;
  1821. blkdev_put(bdev, FMODE_READ);
  1822. return ret;
  1823. }
  1824. int btrfs_find_device_missing_or_by_path(struct btrfs_fs_info *fs_info,
  1825. const char *device_path,
  1826. struct btrfs_device **device)
  1827. {
  1828. *device = NULL;
  1829. if (strcmp(device_path, "missing") == 0) {
  1830. struct list_head *devices;
  1831. struct btrfs_device *tmp;
  1832. devices = &fs_info->fs_devices->devices;
  1833. /*
  1834. * It is safe to read the devices since the volume_mutex
  1835. * is held by the caller.
  1836. */
  1837. list_for_each_entry(tmp, devices, dev_list) {
  1838. if (tmp->in_fs_metadata && !tmp->bdev) {
  1839. *device = tmp;
  1840. break;
  1841. }
  1842. }
  1843. if (!*device)
  1844. return BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
  1845. return 0;
  1846. } else {
  1847. return btrfs_find_device_by_path(fs_info, device_path, device);
  1848. }
  1849. }
  1850. /*
  1851. * Lookup a device given by device id, or the path if the id is 0.
  1852. */
  1853. int btrfs_find_device_by_devspec(struct btrfs_fs_info *fs_info, u64 devid,
  1854. const char *devpath,
  1855. struct btrfs_device **device)
  1856. {
  1857. int ret;
  1858. if (devid) {
  1859. ret = 0;
  1860. *device = btrfs_find_device(fs_info, devid, NULL, NULL);
  1861. if (!*device)
  1862. ret = -ENOENT;
  1863. } else {
  1864. if (!devpath || !devpath[0])
  1865. return -EINVAL;
  1866. ret = btrfs_find_device_missing_or_by_path(fs_info, devpath,
  1867. device);
  1868. }
  1869. return ret;
  1870. }
  1871. /*
  1872. * does all the dirty work required for changing file system's UUID.
  1873. */
  1874. static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info)
  1875. {
  1876. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  1877. struct btrfs_fs_devices *old_devices;
  1878. struct btrfs_fs_devices *seed_devices;
  1879. struct btrfs_super_block *disk_super = fs_info->super_copy;
  1880. struct btrfs_device *device;
  1881. u64 super_flags;
  1882. BUG_ON(!mutex_is_locked(&uuid_mutex));
  1883. if (!fs_devices->seeding)
  1884. return -EINVAL;
  1885. seed_devices = alloc_fs_devices(NULL);
  1886. if (IS_ERR(seed_devices))
  1887. return PTR_ERR(seed_devices);
  1888. old_devices = clone_fs_devices(fs_devices);
  1889. if (IS_ERR(old_devices)) {
  1890. kfree(seed_devices);
  1891. return PTR_ERR(old_devices);
  1892. }
  1893. list_add(&old_devices->list, &fs_uuids);
  1894. memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
  1895. seed_devices->opened = 1;
  1896. INIT_LIST_HEAD(&seed_devices->devices);
  1897. INIT_LIST_HEAD(&seed_devices->alloc_list);
  1898. mutex_init(&seed_devices->device_list_mutex);
  1899. mutex_lock(&fs_info->fs_devices->device_list_mutex);
  1900. list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
  1901. synchronize_rcu);
  1902. list_for_each_entry(device, &seed_devices->devices, dev_list)
  1903. device->fs_devices = seed_devices;
  1904. mutex_lock(&fs_info->chunk_mutex);
  1905. list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
  1906. mutex_unlock(&fs_info->chunk_mutex);
  1907. fs_devices->seeding = 0;
  1908. fs_devices->num_devices = 0;
  1909. fs_devices->open_devices = 0;
  1910. fs_devices->missing_devices = 0;
  1911. fs_devices->rotating = 0;
  1912. fs_devices->seed = seed_devices;
  1913. generate_random_uuid(fs_devices->fsid);
  1914. memcpy(fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
  1915. memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
  1916. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  1917. super_flags = btrfs_super_flags(disk_super) &
  1918. ~BTRFS_SUPER_FLAG_SEEDING;
  1919. btrfs_set_super_flags(disk_super, super_flags);
  1920. return 0;
  1921. }
  1922. /*
  1923. * Store the expected generation for seed devices in device items.
  1924. */
  1925. static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
  1926. struct btrfs_fs_info *fs_info)
  1927. {
  1928. struct btrfs_root *root = fs_info->chunk_root;
  1929. struct btrfs_path *path;
  1930. struct extent_buffer *leaf;
  1931. struct btrfs_dev_item *dev_item;
  1932. struct btrfs_device *device;
  1933. struct btrfs_key key;
  1934. u8 fs_uuid[BTRFS_FSID_SIZE];
  1935. u8 dev_uuid[BTRFS_UUID_SIZE];
  1936. u64 devid;
  1937. int ret;
  1938. path = btrfs_alloc_path();
  1939. if (!path)
  1940. return -ENOMEM;
  1941. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  1942. key.offset = 0;
  1943. key.type = BTRFS_DEV_ITEM_KEY;
  1944. while (1) {
  1945. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  1946. if (ret < 0)
  1947. goto error;
  1948. leaf = path->nodes[0];
  1949. next_slot:
  1950. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  1951. ret = btrfs_next_leaf(root, path);
  1952. if (ret > 0)
  1953. break;
  1954. if (ret < 0)
  1955. goto error;
  1956. leaf = path->nodes[0];
  1957. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  1958. btrfs_release_path(path);
  1959. continue;
  1960. }
  1961. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  1962. if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
  1963. key.type != BTRFS_DEV_ITEM_KEY)
  1964. break;
  1965. dev_item = btrfs_item_ptr(leaf, path->slots[0],
  1966. struct btrfs_dev_item);
  1967. devid = btrfs_device_id(leaf, dev_item);
  1968. read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
  1969. BTRFS_UUID_SIZE);
  1970. read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
  1971. BTRFS_FSID_SIZE);
  1972. device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid);
  1973. BUG_ON(!device); /* Logic error */
  1974. if (device->fs_devices->seeding) {
  1975. btrfs_set_device_generation(leaf, dev_item,
  1976. device->generation);
  1977. btrfs_mark_buffer_dirty(leaf);
  1978. }
  1979. path->slots[0]++;
  1980. goto next_slot;
  1981. }
  1982. ret = 0;
  1983. error:
  1984. btrfs_free_path(path);
  1985. return ret;
  1986. }
  1987. int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path)
  1988. {
  1989. struct btrfs_root *root = fs_info->dev_root;
  1990. struct request_queue *q;
  1991. struct btrfs_trans_handle *trans;
  1992. struct btrfs_device *device;
  1993. struct block_device *bdev;
  1994. struct list_head *devices;
  1995. struct super_block *sb = fs_info->sb;
  1996. struct rcu_string *name;
  1997. u64 tmp;
  1998. int seeding_dev = 0;
  1999. int ret = 0;
  2000. if (sb_rdonly(sb) && !fs_info->fs_devices->seeding)
  2001. return -EROFS;
  2002. bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
  2003. fs_info->bdev_holder);
  2004. if (IS_ERR(bdev))
  2005. return PTR_ERR(bdev);
  2006. if (fs_info->fs_devices->seeding) {
  2007. seeding_dev = 1;
  2008. down_write(&sb->s_umount);
  2009. mutex_lock(&uuid_mutex);
  2010. }
  2011. filemap_write_and_wait(bdev->bd_inode->i_mapping);
  2012. devices = &fs_info->fs_devices->devices;
  2013. mutex_lock(&fs_info->fs_devices->device_list_mutex);
  2014. list_for_each_entry(device, devices, dev_list) {
  2015. if (device->bdev == bdev) {
  2016. ret = -EEXIST;
  2017. mutex_unlock(
  2018. &fs_info->fs_devices->device_list_mutex);
  2019. goto error;
  2020. }
  2021. }
  2022. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  2023. device = btrfs_alloc_device(fs_info, NULL, NULL);
  2024. if (IS_ERR(device)) {
  2025. /* we can safely leave the fs_devices entry around */
  2026. ret = PTR_ERR(device);
  2027. goto error;
  2028. }
  2029. name = rcu_string_strdup(device_path, GFP_KERNEL);
  2030. if (!name) {
  2031. kfree(device);
  2032. ret = -ENOMEM;
  2033. goto error;
  2034. }
  2035. rcu_assign_pointer(device->name, name);
  2036. trans = btrfs_start_transaction(root, 0);
  2037. if (IS_ERR(trans)) {
  2038. rcu_string_free(device->name);
  2039. kfree(device);
  2040. ret = PTR_ERR(trans);
  2041. goto error;
  2042. }
  2043. q = bdev_get_queue(bdev);
  2044. if (blk_queue_discard(q))
  2045. device->can_discard = 1;
  2046. device->writeable = 1;
  2047. device->generation = trans->transid;
  2048. device->io_width = fs_info->sectorsize;
  2049. device->io_align = fs_info->sectorsize;
  2050. device->sector_size = fs_info->sectorsize;
  2051. device->total_bytes = round_down(i_size_read(bdev->bd_inode),
  2052. fs_info->sectorsize);
  2053. device->disk_total_bytes = device->total_bytes;
  2054. device->commit_total_bytes = device->total_bytes;
  2055. device->fs_info = fs_info;
  2056. device->bdev = bdev;
  2057. device->in_fs_metadata = 1;
  2058. device->is_tgtdev_for_dev_replace = 0;
  2059. device->mode = FMODE_EXCL;
  2060. device->dev_stats_valid = 1;
  2061. set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
  2062. if (seeding_dev) {
  2063. sb->s_flags &= ~MS_RDONLY;
  2064. ret = btrfs_prepare_sprout(fs_info);
  2065. BUG_ON(ret); /* -ENOMEM */
  2066. }
  2067. device->fs_devices = fs_info->fs_devices;
  2068. mutex_lock(&fs_info->fs_devices->device_list_mutex);
  2069. mutex_lock(&fs_info->chunk_mutex);
  2070. list_add_rcu(&device->dev_list, &fs_info->fs_devices->devices);
  2071. list_add(&device->dev_alloc_list,
  2072. &fs_info->fs_devices->alloc_list);
  2073. fs_info->fs_devices->num_devices++;
  2074. fs_info->fs_devices->open_devices++;
  2075. fs_info->fs_devices->rw_devices++;
  2076. fs_info->fs_devices->total_devices++;
  2077. fs_info->fs_devices->total_rw_bytes += device->total_bytes;
  2078. atomic64_add(device->total_bytes, &fs_info->free_chunk_space);
  2079. if (!blk_queue_nonrot(q))
  2080. fs_info->fs_devices->rotating = 1;
  2081. tmp = btrfs_super_total_bytes(fs_info->super_copy);
  2082. btrfs_set_super_total_bytes(fs_info->super_copy,
  2083. round_down(tmp + device->total_bytes, fs_info->sectorsize));
  2084. tmp = btrfs_super_num_devices(fs_info->super_copy);
  2085. btrfs_set_super_num_devices(fs_info->super_copy, tmp + 1);
  2086. /* add sysfs device entry */
  2087. btrfs_sysfs_add_device_link(fs_info->fs_devices, device);
  2088. /*
  2089. * we've got more storage, clear any full flags on the space
  2090. * infos
  2091. */
  2092. btrfs_clear_space_info_full(fs_info);
  2093. mutex_unlock(&fs_info->chunk_mutex);
  2094. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  2095. if (seeding_dev) {
  2096. mutex_lock(&fs_info->chunk_mutex);
  2097. ret = init_first_rw_device(trans, fs_info);
  2098. mutex_unlock(&fs_info->chunk_mutex);
  2099. if (ret) {
  2100. btrfs_abort_transaction(trans, ret);
  2101. goto error_trans;
  2102. }
  2103. }
  2104. ret = btrfs_add_device(trans, fs_info, device);
  2105. if (ret) {
  2106. btrfs_abort_transaction(trans, ret);
  2107. goto error_trans;
  2108. }
  2109. if (seeding_dev) {
  2110. char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
  2111. ret = btrfs_finish_sprout(trans, fs_info);
  2112. if (ret) {
  2113. btrfs_abort_transaction(trans, ret);
  2114. goto error_trans;
  2115. }
  2116. /* Sprouting would change fsid of the mounted root,
  2117. * so rename the fsid on the sysfs
  2118. */
  2119. snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
  2120. fs_info->fsid);
  2121. if (kobject_rename(&fs_info->fs_devices->fsid_kobj, fsid_buf))
  2122. btrfs_warn(fs_info,
  2123. "sysfs: failed to create fsid for sprout");
  2124. }
  2125. ret = btrfs_commit_transaction(trans);
  2126. if (seeding_dev) {
  2127. mutex_unlock(&uuid_mutex);
  2128. up_write(&sb->s_umount);
  2129. if (ret) /* transaction commit */
  2130. return ret;
  2131. ret = btrfs_relocate_sys_chunks(fs_info);
  2132. if (ret < 0)
  2133. btrfs_handle_fs_error(fs_info, ret,
  2134. "Failed to relocate sys chunks after device initialization. This can be fixed using the \"btrfs balance\" command.");
  2135. trans = btrfs_attach_transaction(root);
  2136. if (IS_ERR(trans)) {
  2137. if (PTR_ERR(trans) == -ENOENT)
  2138. return 0;
  2139. return PTR_ERR(trans);
  2140. }
  2141. ret = btrfs_commit_transaction(trans);
  2142. }
  2143. /* Update ctime/mtime for libblkid */
  2144. update_dev_time(device_path);
  2145. return ret;
  2146. error_trans:
  2147. btrfs_end_transaction(trans);
  2148. rcu_string_free(device->name);
  2149. btrfs_sysfs_rm_device_link(fs_info->fs_devices, device);
  2150. kfree(device);
  2151. error:
  2152. blkdev_put(bdev, FMODE_EXCL);
  2153. if (seeding_dev) {
  2154. mutex_unlock(&uuid_mutex);
  2155. up_write(&sb->s_umount);
  2156. }
  2157. return ret;
  2158. }
  2159. int btrfs_init_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
  2160. const char *device_path,
  2161. struct btrfs_device *srcdev,
  2162. struct btrfs_device **device_out)
  2163. {
  2164. struct request_queue *q;
  2165. struct btrfs_device *device;
  2166. struct block_device *bdev;
  2167. struct list_head *devices;
  2168. struct rcu_string *name;
  2169. u64 devid = BTRFS_DEV_REPLACE_DEVID;
  2170. int ret = 0;
  2171. *device_out = NULL;
  2172. if (fs_info->fs_devices->seeding) {
  2173. btrfs_err(fs_info, "the filesystem is a seed filesystem!");
  2174. return -EINVAL;
  2175. }
  2176. bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
  2177. fs_info->bdev_holder);
  2178. if (IS_ERR(bdev)) {
  2179. btrfs_err(fs_info, "target device %s is invalid!", device_path);
  2180. return PTR_ERR(bdev);
  2181. }
  2182. filemap_write_and_wait(bdev->bd_inode->i_mapping);
  2183. devices = &fs_info->fs_devices->devices;
  2184. list_for_each_entry(device, devices, dev_list) {
  2185. if (device->bdev == bdev) {
  2186. btrfs_err(fs_info,
  2187. "target device is in the filesystem!");
  2188. ret = -EEXIST;
  2189. goto error;
  2190. }
  2191. }
  2192. if (i_size_read(bdev->bd_inode) <
  2193. btrfs_device_get_total_bytes(srcdev)) {
  2194. btrfs_err(fs_info,
  2195. "target device is smaller than source device!");
  2196. ret = -EINVAL;
  2197. goto error;
  2198. }
  2199. device = btrfs_alloc_device(NULL, &devid, NULL);
  2200. if (IS_ERR(device)) {
  2201. ret = PTR_ERR(device);
  2202. goto error;
  2203. }
  2204. name = rcu_string_strdup(device_path, GFP_KERNEL);
  2205. if (!name) {
  2206. kfree(device);
  2207. ret = -ENOMEM;
  2208. goto error;
  2209. }
  2210. rcu_assign_pointer(device->name, name);
  2211. q = bdev_get_queue(bdev);
  2212. if (blk_queue_discard(q))
  2213. device->can_discard = 1;
  2214. mutex_lock(&fs_info->fs_devices->device_list_mutex);
  2215. device->writeable = 1;
  2216. device->generation = 0;
  2217. device->io_width = fs_info->sectorsize;
  2218. device->io_align = fs_info->sectorsize;
  2219. device->sector_size = fs_info->sectorsize;
  2220. device->total_bytes = btrfs_device_get_total_bytes(srcdev);
  2221. device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev);
  2222. device->bytes_used = btrfs_device_get_bytes_used(srcdev);
  2223. ASSERT(list_empty(&srcdev->resized_list));
  2224. device->commit_total_bytes = srcdev->commit_total_bytes;
  2225. device->commit_bytes_used = device->bytes_used;
  2226. device->fs_info = fs_info;
  2227. device->bdev = bdev;
  2228. device->in_fs_metadata = 1;
  2229. device->is_tgtdev_for_dev_replace = 1;
  2230. device->mode = FMODE_EXCL;
  2231. device->dev_stats_valid = 1;
  2232. set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
  2233. device->fs_devices = fs_info->fs_devices;
  2234. list_add(&device->dev_list, &fs_info->fs_devices->devices);
  2235. fs_info->fs_devices->num_devices++;
  2236. fs_info->fs_devices->open_devices++;
  2237. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  2238. *device_out = device;
  2239. return ret;
  2240. error:
  2241. blkdev_put(bdev, FMODE_EXCL);
  2242. return ret;
  2243. }
  2244. void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
  2245. struct btrfs_device *tgtdev)
  2246. {
  2247. u32 sectorsize = fs_info->sectorsize;
  2248. WARN_ON(fs_info->fs_devices->rw_devices == 0);
  2249. tgtdev->io_width = sectorsize;
  2250. tgtdev->io_align = sectorsize;
  2251. tgtdev->sector_size = sectorsize;
  2252. tgtdev->fs_info = fs_info;
  2253. tgtdev->in_fs_metadata = 1;
  2254. }
  2255. static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
  2256. struct btrfs_device *device)
  2257. {
  2258. int ret;
  2259. struct btrfs_path *path;
  2260. struct btrfs_root *root = device->fs_info->chunk_root;
  2261. struct btrfs_dev_item *dev_item;
  2262. struct extent_buffer *leaf;
  2263. struct btrfs_key key;
  2264. path = btrfs_alloc_path();
  2265. if (!path)
  2266. return -ENOMEM;
  2267. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  2268. key.type = BTRFS_DEV_ITEM_KEY;
  2269. key.offset = device->devid;
  2270. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  2271. if (ret < 0)
  2272. goto out;
  2273. if (ret > 0) {
  2274. ret = -ENOENT;
  2275. goto out;
  2276. }
  2277. leaf = path->nodes[0];
  2278. dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
  2279. btrfs_set_device_id(leaf, dev_item, device->devid);
  2280. btrfs_set_device_type(leaf, dev_item, device->type);
  2281. btrfs_set_device_io_align(leaf, dev_item, device->io_align);
  2282. btrfs_set_device_io_width(leaf, dev_item, device->io_width);
  2283. btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
  2284. btrfs_set_device_total_bytes(leaf, dev_item,
  2285. btrfs_device_get_disk_total_bytes(device));
  2286. btrfs_set_device_bytes_used(leaf, dev_item,
  2287. btrfs_device_get_bytes_used(device));
  2288. btrfs_mark_buffer_dirty(leaf);
  2289. out:
  2290. btrfs_free_path(path);
  2291. return ret;
  2292. }
  2293. int btrfs_grow_device(struct btrfs_trans_handle *trans,
  2294. struct btrfs_device *device, u64 new_size)
  2295. {
  2296. struct btrfs_fs_info *fs_info = device->fs_info;
  2297. struct btrfs_super_block *super_copy = fs_info->super_copy;
  2298. struct btrfs_fs_devices *fs_devices;
  2299. u64 old_total;
  2300. u64 diff;
  2301. if (!device->writeable)
  2302. return -EACCES;
  2303. new_size = round_down(new_size, fs_info->sectorsize);
  2304. mutex_lock(&fs_info->chunk_mutex);
  2305. old_total = btrfs_super_total_bytes(super_copy);
  2306. diff = round_down(new_size - device->total_bytes, fs_info->sectorsize);
  2307. if (new_size <= device->total_bytes ||
  2308. device->is_tgtdev_for_dev_replace) {
  2309. mutex_unlock(&fs_info->chunk_mutex);
  2310. return -EINVAL;
  2311. }
  2312. fs_devices = fs_info->fs_devices;
  2313. btrfs_set_super_total_bytes(super_copy,
  2314. round_down(old_total + diff, fs_info->sectorsize));
  2315. device->fs_devices->total_rw_bytes += diff;
  2316. btrfs_device_set_total_bytes(device, new_size);
  2317. btrfs_device_set_disk_total_bytes(device, new_size);
  2318. btrfs_clear_space_info_full(device->fs_info);
  2319. if (list_empty(&device->resized_list))
  2320. list_add_tail(&device->resized_list,
  2321. &fs_devices->resized_devices);
  2322. mutex_unlock(&fs_info->chunk_mutex);
  2323. return btrfs_update_device(trans, device);
  2324. }
  2325. static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
  2326. struct btrfs_fs_info *fs_info, 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 = BTRFS_FIRST_CHUNK_TREE_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, u64 chunk_offset)
  2356. {
  2357. struct btrfs_super_block *super_copy = fs_info->super_copy;
  2358. struct btrfs_disk_key *disk_key;
  2359. struct btrfs_chunk *chunk;
  2360. u8 *ptr;
  2361. int ret = 0;
  2362. u32 num_stripes;
  2363. u32 array_size;
  2364. u32 len = 0;
  2365. u32 cur;
  2366. struct btrfs_key key;
  2367. mutex_lock(&fs_info->chunk_mutex);
  2368. array_size = btrfs_super_sys_array_size(super_copy);
  2369. ptr = super_copy->sys_chunk_array;
  2370. cur = 0;
  2371. while (cur < array_size) {
  2372. disk_key = (struct btrfs_disk_key *)ptr;
  2373. btrfs_disk_key_to_cpu(&key, disk_key);
  2374. len = sizeof(*disk_key);
  2375. if (key.type == BTRFS_CHUNK_ITEM_KEY) {
  2376. chunk = (struct btrfs_chunk *)(ptr + len);
  2377. num_stripes = btrfs_stack_chunk_num_stripes(chunk);
  2378. len += btrfs_chunk_item_size(num_stripes);
  2379. } else {
  2380. ret = -EIO;
  2381. break;
  2382. }
  2383. if (key.objectid == BTRFS_FIRST_CHUNK_TREE_OBJECTID &&
  2384. key.offset == chunk_offset) {
  2385. memmove(ptr, ptr + len, array_size - (cur + len));
  2386. array_size -= len;
  2387. btrfs_set_super_sys_array_size(super_copy, array_size);
  2388. } else {
  2389. ptr += len;
  2390. cur += len;
  2391. }
  2392. }
  2393. mutex_unlock(&fs_info->chunk_mutex);
  2394. return ret;
  2395. }
  2396. static struct extent_map *get_chunk_map(struct btrfs_fs_info *fs_info,
  2397. u64 logical, u64 length)
  2398. {
  2399. struct extent_map_tree *em_tree;
  2400. struct extent_map *em;
  2401. em_tree = &fs_info->mapping_tree.map_tree;
  2402. read_lock(&em_tree->lock);
  2403. em = lookup_extent_mapping(em_tree, logical, length);
  2404. read_unlock(&em_tree->lock);
  2405. if (!em) {
  2406. btrfs_crit(fs_info, "unable to find logical %llu length %llu",
  2407. logical, length);
  2408. return ERR_PTR(-EINVAL);
  2409. }
  2410. if (em->start > logical || em->start + em->len < logical) {
  2411. btrfs_crit(fs_info,
  2412. "found a bad mapping, wanted %llu-%llu, found %llu-%llu",
  2413. logical, length, em->start, em->start + em->len);
  2414. free_extent_map(em);
  2415. return ERR_PTR(-EINVAL);
  2416. }
  2417. /* callers are responsible for dropping em's ref. */
  2418. return em;
  2419. }
  2420. int btrfs_remove_chunk(struct btrfs_trans_handle *trans,
  2421. struct btrfs_fs_info *fs_info, u64 chunk_offset)
  2422. {
  2423. struct extent_map *em;
  2424. struct map_lookup *map;
  2425. u64 dev_extent_len = 0;
  2426. int i, ret = 0;
  2427. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  2428. em = get_chunk_map(fs_info, chunk_offset, 1);
  2429. if (IS_ERR(em)) {
  2430. /*
  2431. * This is a logic error, but we don't want to just rely on the
  2432. * user having built with ASSERT enabled, so if ASSERT doesn't
  2433. * do anything we still error out.
  2434. */
  2435. ASSERT(0);
  2436. return PTR_ERR(em);
  2437. }
  2438. map = em->map_lookup;
  2439. mutex_lock(&fs_info->chunk_mutex);
  2440. check_system_chunk(trans, fs_info, map->type);
  2441. mutex_unlock(&fs_info->chunk_mutex);
  2442. /*
  2443. * Take the device list mutex to prevent races with the final phase of
  2444. * a device replace operation that replaces the device object associated
  2445. * with map stripes (dev-replace.c:btrfs_dev_replace_finishing()).
  2446. */
  2447. mutex_lock(&fs_devices->device_list_mutex);
  2448. for (i = 0; i < map->num_stripes; i++) {
  2449. struct btrfs_device *device = map->stripes[i].dev;
  2450. ret = btrfs_free_dev_extent(trans, device,
  2451. map->stripes[i].physical,
  2452. &dev_extent_len);
  2453. if (ret) {
  2454. mutex_unlock(&fs_devices->device_list_mutex);
  2455. btrfs_abort_transaction(trans, ret);
  2456. goto out;
  2457. }
  2458. if (device->bytes_used > 0) {
  2459. mutex_lock(&fs_info->chunk_mutex);
  2460. btrfs_device_set_bytes_used(device,
  2461. device->bytes_used - dev_extent_len);
  2462. atomic64_add(dev_extent_len, &fs_info->free_chunk_space);
  2463. btrfs_clear_space_info_full(fs_info);
  2464. mutex_unlock(&fs_info->chunk_mutex);
  2465. }
  2466. if (map->stripes[i].dev) {
  2467. ret = btrfs_update_device(trans, map->stripes[i].dev);
  2468. if (ret) {
  2469. mutex_unlock(&fs_devices->device_list_mutex);
  2470. btrfs_abort_transaction(trans, ret);
  2471. goto out;
  2472. }
  2473. }
  2474. }
  2475. mutex_unlock(&fs_devices->device_list_mutex);
  2476. ret = btrfs_free_chunk(trans, fs_info, chunk_offset);
  2477. if (ret) {
  2478. btrfs_abort_transaction(trans, ret);
  2479. goto out;
  2480. }
  2481. trace_btrfs_chunk_free(fs_info, map, chunk_offset, em->len);
  2482. if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
  2483. ret = btrfs_del_sys_chunk(fs_info, chunk_offset);
  2484. if (ret) {
  2485. btrfs_abort_transaction(trans, ret);
  2486. goto out;
  2487. }
  2488. }
  2489. ret = btrfs_remove_block_group(trans, fs_info, chunk_offset, em);
  2490. if (ret) {
  2491. btrfs_abort_transaction(trans, ret);
  2492. goto out;
  2493. }
  2494. out:
  2495. /* once for us */
  2496. free_extent_map(em);
  2497. return ret;
  2498. }
  2499. static int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
  2500. {
  2501. struct btrfs_root *root = fs_info->chunk_root;
  2502. struct btrfs_trans_handle *trans;
  2503. int ret;
  2504. /*
  2505. * Prevent races with automatic removal of unused block groups.
  2506. * After we relocate and before we remove the chunk with offset
  2507. * chunk_offset, automatic removal of the block group can kick in,
  2508. * resulting in a failure when calling btrfs_remove_chunk() below.
  2509. *
  2510. * Make sure to acquire this mutex before doing a tree search (dev
  2511. * or chunk trees) to find chunks. Otherwise the cleaner kthread might
  2512. * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after
  2513. * we release the path used to search the chunk/dev tree and before
  2514. * the current task acquires this mutex and calls us.
  2515. */
  2516. ASSERT(mutex_is_locked(&fs_info->delete_unused_bgs_mutex));
  2517. ret = btrfs_can_relocate(fs_info, chunk_offset);
  2518. if (ret)
  2519. return -ENOSPC;
  2520. /* step one, relocate all the extents inside this chunk */
  2521. btrfs_scrub_pause(fs_info);
  2522. ret = btrfs_relocate_block_group(fs_info, chunk_offset);
  2523. btrfs_scrub_continue(fs_info);
  2524. if (ret)
  2525. return ret;
  2526. trans = btrfs_start_trans_remove_block_group(root->fs_info,
  2527. chunk_offset);
  2528. if (IS_ERR(trans)) {
  2529. ret = PTR_ERR(trans);
  2530. btrfs_handle_fs_error(root->fs_info, ret, NULL);
  2531. return ret;
  2532. }
  2533. /*
  2534. * step two, delete the device extents and the
  2535. * chunk tree entries
  2536. */
  2537. ret = btrfs_remove_chunk(trans, fs_info, chunk_offset);
  2538. btrfs_end_transaction(trans);
  2539. return ret;
  2540. }
  2541. static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info)
  2542. {
  2543. struct btrfs_root *chunk_root = fs_info->chunk_root;
  2544. struct btrfs_path *path;
  2545. struct extent_buffer *leaf;
  2546. struct btrfs_chunk *chunk;
  2547. struct btrfs_key key;
  2548. struct btrfs_key found_key;
  2549. u64 chunk_type;
  2550. bool retried = false;
  2551. int failed = 0;
  2552. int ret;
  2553. path = btrfs_alloc_path();
  2554. if (!path)
  2555. return -ENOMEM;
  2556. again:
  2557. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  2558. key.offset = (u64)-1;
  2559. key.type = BTRFS_CHUNK_ITEM_KEY;
  2560. while (1) {
  2561. mutex_lock(&fs_info->delete_unused_bgs_mutex);
  2562. ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
  2563. if (ret < 0) {
  2564. mutex_unlock(&fs_info->delete_unused_bgs_mutex);
  2565. goto error;
  2566. }
  2567. BUG_ON(ret == 0); /* Corruption */
  2568. ret = btrfs_previous_item(chunk_root, path, key.objectid,
  2569. key.type);
  2570. if (ret)
  2571. mutex_unlock(&fs_info->delete_unused_bgs_mutex);
  2572. if (ret < 0)
  2573. goto error;
  2574. if (ret > 0)
  2575. break;
  2576. leaf = path->nodes[0];
  2577. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  2578. chunk = btrfs_item_ptr(leaf, path->slots[0],
  2579. struct btrfs_chunk);
  2580. chunk_type = btrfs_chunk_type(leaf, chunk);
  2581. btrfs_release_path(path);
  2582. if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
  2583. ret = btrfs_relocate_chunk(fs_info, found_key.offset);
  2584. if (ret == -ENOSPC)
  2585. failed++;
  2586. else
  2587. BUG_ON(ret);
  2588. }
  2589. mutex_unlock(&fs_info->delete_unused_bgs_mutex);
  2590. if (found_key.offset == 0)
  2591. break;
  2592. key.offset = found_key.offset - 1;
  2593. }
  2594. ret = 0;
  2595. if (failed && !retried) {
  2596. failed = 0;
  2597. retried = true;
  2598. goto again;
  2599. } else if (WARN_ON(failed && retried)) {
  2600. ret = -ENOSPC;
  2601. }
  2602. error:
  2603. btrfs_free_path(path);
  2604. return ret;
  2605. }
  2606. static int insert_balance_item(struct btrfs_fs_info *fs_info,
  2607. struct btrfs_balance_control *bctl)
  2608. {
  2609. struct btrfs_root *root = fs_info->tree_root;
  2610. struct btrfs_trans_handle *trans;
  2611. struct btrfs_balance_item *item;
  2612. struct btrfs_disk_balance_args disk_bargs;
  2613. struct btrfs_path *path;
  2614. struct extent_buffer *leaf;
  2615. struct btrfs_key key;
  2616. int ret, err;
  2617. path = btrfs_alloc_path();
  2618. if (!path)
  2619. return -ENOMEM;
  2620. trans = btrfs_start_transaction(root, 0);
  2621. if (IS_ERR(trans)) {
  2622. btrfs_free_path(path);
  2623. return PTR_ERR(trans);
  2624. }
  2625. key.objectid = BTRFS_BALANCE_OBJECTID;
  2626. key.type = BTRFS_TEMPORARY_ITEM_KEY;
  2627. key.offset = 0;
  2628. ret = btrfs_insert_empty_item(trans, root, path, &key,
  2629. sizeof(*item));
  2630. if (ret)
  2631. goto out;
  2632. leaf = path->nodes[0];
  2633. item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
  2634. memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item));
  2635. btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
  2636. btrfs_set_balance_data(leaf, item, &disk_bargs);
  2637. btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
  2638. btrfs_set_balance_meta(leaf, item, &disk_bargs);
  2639. btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
  2640. btrfs_set_balance_sys(leaf, item, &disk_bargs);
  2641. btrfs_set_balance_flags(leaf, item, bctl->flags);
  2642. btrfs_mark_buffer_dirty(leaf);
  2643. out:
  2644. btrfs_free_path(path);
  2645. err = btrfs_commit_transaction(trans);
  2646. if (err && !ret)
  2647. ret = err;
  2648. return ret;
  2649. }
  2650. static int del_balance_item(struct btrfs_fs_info *fs_info)
  2651. {
  2652. struct btrfs_root *root = fs_info->tree_root;
  2653. struct btrfs_trans_handle *trans;
  2654. struct btrfs_path *path;
  2655. struct btrfs_key key;
  2656. int ret, err;
  2657. path = btrfs_alloc_path();
  2658. if (!path)
  2659. return -ENOMEM;
  2660. trans = btrfs_start_transaction(root, 0);
  2661. if (IS_ERR(trans)) {
  2662. btrfs_free_path(path);
  2663. return PTR_ERR(trans);
  2664. }
  2665. key.objectid = BTRFS_BALANCE_OBJECTID;
  2666. key.type = BTRFS_TEMPORARY_ITEM_KEY;
  2667. key.offset = 0;
  2668. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  2669. if (ret < 0)
  2670. goto out;
  2671. if (ret > 0) {
  2672. ret = -ENOENT;
  2673. goto out;
  2674. }
  2675. ret = btrfs_del_item(trans, root, path);
  2676. out:
  2677. btrfs_free_path(path);
  2678. err = btrfs_commit_transaction(trans);
  2679. if (err && !ret)
  2680. ret = err;
  2681. return ret;
  2682. }
  2683. /*
  2684. * This is a heuristic used to reduce the number of chunks balanced on
  2685. * resume after balance was interrupted.
  2686. */
  2687. static void update_balance_args(struct btrfs_balance_control *bctl)
  2688. {
  2689. /*
  2690. * Turn on soft mode for chunk types that were being converted.
  2691. */
  2692. if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
  2693. bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
  2694. if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
  2695. bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
  2696. if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
  2697. bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
  2698. /*
  2699. * Turn on usage filter if is not already used. The idea is
  2700. * that chunks that we have already balanced should be
  2701. * reasonably full. Don't do it for chunks that are being
  2702. * converted - that will keep us from relocating unconverted
  2703. * (albeit full) chunks.
  2704. */
  2705. if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
  2706. !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
  2707. !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
  2708. bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
  2709. bctl->data.usage = 90;
  2710. }
  2711. if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
  2712. !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
  2713. !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
  2714. bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
  2715. bctl->sys.usage = 90;
  2716. }
  2717. if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
  2718. !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
  2719. !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
  2720. bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
  2721. bctl->meta.usage = 90;
  2722. }
  2723. }
  2724. /*
  2725. * Should be called with both balance and volume mutexes held to
  2726. * serialize other volume operations (add_dev/rm_dev/resize) with
  2727. * restriper. Same goes for unset_balance_control.
  2728. */
  2729. static void set_balance_control(struct btrfs_balance_control *bctl)
  2730. {
  2731. struct btrfs_fs_info *fs_info = bctl->fs_info;
  2732. BUG_ON(fs_info->balance_ctl);
  2733. spin_lock(&fs_info->balance_lock);
  2734. fs_info->balance_ctl = bctl;
  2735. spin_unlock(&fs_info->balance_lock);
  2736. }
  2737. static void unset_balance_control(struct btrfs_fs_info *fs_info)
  2738. {
  2739. struct btrfs_balance_control *bctl = fs_info->balance_ctl;
  2740. BUG_ON(!fs_info->balance_ctl);
  2741. spin_lock(&fs_info->balance_lock);
  2742. fs_info->balance_ctl = NULL;
  2743. spin_unlock(&fs_info->balance_lock);
  2744. kfree(bctl);
  2745. }
  2746. /*
  2747. * Balance filters. Return 1 if chunk should be filtered out
  2748. * (should not be balanced).
  2749. */
  2750. static int chunk_profiles_filter(u64 chunk_type,
  2751. struct btrfs_balance_args *bargs)
  2752. {
  2753. chunk_type = chunk_to_extended(chunk_type) &
  2754. BTRFS_EXTENDED_PROFILE_MASK;
  2755. if (bargs->profiles & chunk_type)
  2756. return 0;
  2757. return 1;
  2758. }
  2759. static int chunk_usage_range_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
  2760. struct btrfs_balance_args *bargs)
  2761. {
  2762. struct btrfs_block_group_cache *cache;
  2763. u64 chunk_used;
  2764. u64 user_thresh_min;
  2765. u64 user_thresh_max;
  2766. int ret = 1;
  2767. cache = btrfs_lookup_block_group(fs_info, chunk_offset);
  2768. chunk_used = btrfs_block_group_used(&cache->item);
  2769. if (bargs->usage_min == 0)
  2770. user_thresh_min = 0;
  2771. else
  2772. user_thresh_min = div_factor_fine(cache->key.offset,
  2773. bargs->usage_min);
  2774. if (bargs->usage_max == 0)
  2775. user_thresh_max = 1;
  2776. else if (bargs->usage_max > 100)
  2777. user_thresh_max = cache->key.offset;
  2778. else
  2779. user_thresh_max = div_factor_fine(cache->key.offset,
  2780. bargs->usage_max);
  2781. if (user_thresh_min <= chunk_used && chunk_used < user_thresh_max)
  2782. ret = 0;
  2783. btrfs_put_block_group(cache);
  2784. return ret;
  2785. }
  2786. static int chunk_usage_filter(struct btrfs_fs_info *fs_info,
  2787. u64 chunk_offset, struct btrfs_balance_args *bargs)
  2788. {
  2789. struct btrfs_block_group_cache *cache;
  2790. u64 chunk_used, user_thresh;
  2791. int ret = 1;
  2792. cache = btrfs_lookup_block_group(fs_info, chunk_offset);
  2793. chunk_used = btrfs_block_group_used(&cache->item);
  2794. if (bargs->usage_min == 0)
  2795. user_thresh = 1;
  2796. else if (bargs->usage > 100)
  2797. user_thresh = cache->key.offset;
  2798. else
  2799. user_thresh = div_factor_fine(cache->key.offset,
  2800. bargs->usage);
  2801. if (chunk_used < user_thresh)
  2802. ret = 0;
  2803. btrfs_put_block_group(cache);
  2804. return ret;
  2805. }
  2806. static int chunk_devid_filter(struct extent_buffer *leaf,
  2807. struct btrfs_chunk *chunk,
  2808. struct btrfs_balance_args *bargs)
  2809. {
  2810. struct btrfs_stripe *stripe;
  2811. int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
  2812. int i;
  2813. for (i = 0; i < num_stripes; i++) {
  2814. stripe = btrfs_stripe_nr(chunk, i);
  2815. if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
  2816. return 0;
  2817. }
  2818. return 1;
  2819. }
  2820. /* [pstart, pend) */
  2821. static int chunk_drange_filter(struct extent_buffer *leaf,
  2822. struct btrfs_chunk *chunk,
  2823. struct btrfs_balance_args *bargs)
  2824. {
  2825. struct btrfs_stripe *stripe;
  2826. int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
  2827. u64 stripe_offset;
  2828. u64 stripe_length;
  2829. int factor;
  2830. int i;
  2831. if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
  2832. return 0;
  2833. if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
  2834. BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
  2835. factor = num_stripes / 2;
  2836. } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
  2837. factor = num_stripes - 1;
  2838. } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
  2839. factor = num_stripes - 2;
  2840. } else {
  2841. factor = num_stripes;
  2842. }
  2843. for (i = 0; i < num_stripes; i++) {
  2844. stripe = btrfs_stripe_nr(chunk, i);
  2845. if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
  2846. continue;
  2847. stripe_offset = btrfs_stripe_offset(leaf, stripe);
  2848. stripe_length = btrfs_chunk_length(leaf, chunk);
  2849. stripe_length = div_u64(stripe_length, factor);
  2850. if (stripe_offset < bargs->pend &&
  2851. stripe_offset + stripe_length > bargs->pstart)
  2852. return 0;
  2853. }
  2854. return 1;
  2855. }
  2856. /* [vstart, vend) */
  2857. static int chunk_vrange_filter(struct extent_buffer *leaf,
  2858. struct btrfs_chunk *chunk,
  2859. u64 chunk_offset,
  2860. struct btrfs_balance_args *bargs)
  2861. {
  2862. if (chunk_offset < bargs->vend &&
  2863. chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
  2864. /* at least part of the chunk is inside this vrange */
  2865. return 0;
  2866. return 1;
  2867. }
  2868. static int chunk_stripes_range_filter(struct extent_buffer *leaf,
  2869. struct btrfs_chunk *chunk,
  2870. struct btrfs_balance_args *bargs)
  2871. {
  2872. int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
  2873. if (bargs->stripes_min <= num_stripes
  2874. && num_stripes <= bargs->stripes_max)
  2875. return 0;
  2876. return 1;
  2877. }
  2878. static int chunk_soft_convert_filter(u64 chunk_type,
  2879. struct btrfs_balance_args *bargs)
  2880. {
  2881. if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
  2882. return 0;
  2883. chunk_type = chunk_to_extended(chunk_type) &
  2884. BTRFS_EXTENDED_PROFILE_MASK;
  2885. if (bargs->target == chunk_type)
  2886. return 1;
  2887. return 0;
  2888. }
  2889. static int should_balance_chunk(struct btrfs_fs_info *fs_info,
  2890. struct extent_buffer *leaf,
  2891. struct btrfs_chunk *chunk, u64 chunk_offset)
  2892. {
  2893. struct btrfs_balance_control *bctl = fs_info->balance_ctl;
  2894. struct btrfs_balance_args *bargs = NULL;
  2895. u64 chunk_type = btrfs_chunk_type(leaf, chunk);
  2896. /* type filter */
  2897. if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
  2898. (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
  2899. return 0;
  2900. }
  2901. if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
  2902. bargs = &bctl->data;
  2903. else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
  2904. bargs = &bctl->sys;
  2905. else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
  2906. bargs = &bctl->meta;
  2907. /* profiles filter */
  2908. if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
  2909. chunk_profiles_filter(chunk_type, bargs)) {
  2910. return 0;
  2911. }
  2912. /* usage filter */
  2913. if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
  2914. chunk_usage_filter(fs_info, chunk_offset, bargs)) {
  2915. return 0;
  2916. } else if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
  2917. chunk_usage_range_filter(fs_info, chunk_offset, bargs)) {
  2918. return 0;
  2919. }
  2920. /* devid filter */
  2921. if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
  2922. chunk_devid_filter(leaf, chunk, bargs)) {
  2923. return 0;
  2924. }
  2925. /* drange filter, makes sense only with devid filter */
  2926. if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
  2927. chunk_drange_filter(leaf, chunk, bargs)) {
  2928. return 0;
  2929. }
  2930. /* vrange filter */
  2931. if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
  2932. chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
  2933. return 0;
  2934. }
  2935. /* stripes filter */
  2936. if ((bargs->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) &&
  2937. chunk_stripes_range_filter(leaf, chunk, bargs)) {
  2938. return 0;
  2939. }
  2940. /* soft profile changing mode */
  2941. if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
  2942. chunk_soft_convert_filter(chunk_type, bargs)) {
  2943. return 0;
  2944. }
  2945. /*
  2946. * limited by count, must be the last filter
  2947. */
  2948. if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
  2949. if (bargs->limit == 0)
  2950. return 0;
  2951. else
  2952. bargs->limit--;
  2953. } else if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)) {
  2954. /*
  2955. * Same logic as the 'limit' filter; the minimum cannot be
  2956. * determined here because we do not have the global information
  2957. * about the count of all chunks that satisfy the filters.
  2958. */
  2959. if (bargs->limit_max == 0)
  2960. return 0;
  2961. else
  2962. bargs->limit_max--;
  2963. }
  2964. return 1;
  2965. }
  2966. static int __btrfs_balance(struct btrfs_fs_info *fs_info)
  2967. {
  2968. struct btrfs_balance_control *bctl = fs_info->balance_ctl;
  2969. struct btrfs_root *chunk_root = fs_info->chunk_root;
  2970. struct btrfs_root *dev_root = fs_info->dev_root;
  2971. struct list_head *devices;
  2972. struct btrfs_device *device;
  2973. u64 old_size;
  2974. u64 size_to_free;
  2975. u64 chunk_type;
  2976. struct btrfs_chunk *chunk;
  2977. struct btrfs_path *path = NULL;
  2978. struct btrfs_key key;
  2979. struct btrfs_key found_key;
  2980. struct btrfs_trans_handle *trans;
  2981. struct extent_buffer *leaf;
  2982. int slot;
  2983. int ret;
  2984. int enospc_errors = 0;
  2985. bool counting = true;
  2986. /* The single value limit and min/max limits use the same bytes in the */
  2987. u64 limit_data = bctl->data.limit;
  2988. u64 limit_meta = bctl->meta.limit;
  2989. u64 limit_sys = bctl->sys.limit;
  2990. u32 count_data = 0;
  2991. u32 count_meta = 0;
  2992. u32 count_sys = 0;
  2993. int chunk_reserved = 0;
  2994. u64 bytes_used = 0;
  2995. /* step one make some room on all the devices */
  2996. devices = &fs_info->fs_devices->devices;
  2997. list_for_each_entry(device, devices, dev_list) {
  2998. old_size = btrfs_device_get_total_bytes(device);
  2999. size_to_free = div_factor(old_size, 1);
  3000. size_to_free = min_t(u64, size_to_free, SZ_1M);
  3001. if (!device->writeable ||
  3002. btrfs_device_get_total_bytes(device) -
  3003. btrfs_device_get_bytes_used(device) > size_to_free ||
  3004. device->is_tgtdev_for_dev_replace)
  3005. continue;
  3006. ret = btrfs_shrink_device(device, old_size - size_to_free);
  3007. if (ret == -ENOSPC)
  3008. break;
  3009. if (ret) {
  3010. /* btrfs_shrink_device never returns ret > 0 */
  3011. WARN_ON(ret > 0);
  3012. goto error;
  3013. }
  3014. trans = btrfs_start_transaction(dev_root, 0);
  3015. if (IS_ERR(trans)) {
  3016. ret = PTR_ERR(trans);
  3017. btrfs_info_in_rcu(fs_info,
  3018. "resize: unable to start transaction after shrinking device %s (error %d), old size %llu, new size %llu",
  3019. rcu_str_deref(device->name), ret,
  3020. old_size, old_size - size_to_free);
  3021. goto error;
  3022. }
  3023. ret = btrfs_grow_device(trans, device, old_size);
  3024. if (ret) {
  3025. btrfs_end_transaction(trans);
  3026. /* btrfs_grow_device never returns ret > 0 */
  3027. WARN_ON(ret > 0);
  3028. btrfs_info_in_rcu(fs_info,
  3029. "resize: unable to grow device after shrinking device %s (error %d), old size %llu, new size %llu",
  3030. rcu_str_deref(device->name), ret,
  3031. old_size, old_size - size_to_free);
  3032. goto error;
  3033. }
  3034. btrfs_end_transaction(trans);
  3035. }
  3036. /* step two, relocate all the chunks */
  3037. path = btrfs_alloc_path();
  3038. if (!path) {
  3039. ret = -ENOMEM;
  3040. goto error;
  3041. }
  3042. /* zero out stat counters */
  3043. spin_lock(&fs_info->balance_lock);
  3044. memset(&bctl->stat, 0, sizeof(bctl->stat));
  3045. spin_unlock(&fs_info->balance_lock);
  3046. again:
  3047. if (!counting) {
  3048. /*
  3049. * The single value limit and min/max limits use the same bytes
  3050. * in the
  3051. */
  3052. bctl->data.limit = limit_data;
  3053. bctl->meta.limit = limit_meta;
  3054. bctl->sys.limit = limit_sys;
  3055. }
  3056. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  3057. key.offset = (u64)-1;
  3058. key.type = BTRFS_CHUNK_ITEM_KEY;
  3059. while (1) {
  3060. if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
  3061. atomic_read(&fs_info->balance_cancel_req)) {
  3062. ret = -ECANCELED;
  3063. goto error;
  3064. }
  3065. mutex_lock(&fs_info->delete_unused_bgs_mutex);
  3066. ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
  3067. if (ret < 0) {
  3068. mutex_unlock(&fs_info->delete_unused_bgs_mutex);
  3069. goto error;
  3070. }
  3071. /*
  3072. * this shouldn't happen, it means the last relocate
  3073. * failed
  3074. */
  3075. if (ret == 0)
  3076. BUG(); /* FIXME break ? */
  3077. ret = btrfs_previous_item(chunk_root, path, 0,
  3078. BTRFS_CHUNK_ITEM_KEY);
  3079. if (ret) {
  3080. mutex_unlock(&fs_info->delete_unused_bgs_mutex);
  3081. ret = 0;
  3082. break;
  3083. }
  3084. leaf = path->nodes[0];
  3085. slot = path->slots[0];
  3086. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  3087. if (found_key.objectid != key.objectid) {
  3088. mutex_unlock(&fs_info->delete_unused_bgs_mutex);
  3089. break;
  3090. }
  3091. chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
  3092. chunk_type = btrfs_chunk_type(leaf, chunk);
  3093. if (!counting) {
  3094. spin_lock(&fs_info->balance_lock);
  3095. bctl->stat.considered++;
  3096. spin_unlock(&fs_info->balance_lock);
  3097. }
  3098. ret = should_balance_chunk(fs_info, leaf, chunk,
  3099. found_key.offset);
  3100. btrfs_release_path(path);
  3101. if (!ret) {
  3102. mutex_unlock(&fs_info->delete_unused_bgs_mutex);
  3103. goto loop;
  3104. }
  3105. if (counting) {
  3106. mutex_unlock(&fs_info->delete_unused_bgs_mutex);
  3107. spin_lock(&fs_info->balance_lock);
  3108. bctl->stat.expected++;
  3109. spin_unlock(&fs_info->balance_lock);
  3110. if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
  3111. count_data++;
  3112. else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
  3113. count_sys++;
  3114. else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
  3115. count_meta++;
  3116. goto loop;
  3117. }
  3118. /*
  3119. * Apply limit_min filter, no need to check if the LIMITS
  3120. * filter is used, limit_min is 0 by default
  3121. */
  3122. if (((chunk_type & BTRFS_BLOCK_GROUP_DATA) &&
  3123. count_data < bctl->data.limit_min)
  3124. || ((chunk_type & BTRFS_BLOCK_GROUP_METADATA) &&
  3125. count_meta < bctl->meta.limit_min)
  3126. || ((chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) &&
  3127. count_sys < bctl->sys.limit_min)) {
  3128. mutex_unlock(&fs_info->delete_unused_bgs_mutex);
  3129. goto loop;
  3130. }
  3131. ASSERT(fs_info->data_sinfo);
  3132. spin_lock(&fs_info->data_sinfo->lock);
  3133. bytes_used = fs_info->data_sinfo->bytes_used;
  3134. spin_unlock(&fs_info->data_sinfo->lock);
  3135. if ((chunk_type & BTRFS_BLOCK_GROUP_DATA) &&
  3136. !chunk_reserved && !bytes_used) {
  3137. trans = btrfs_start_transaction(chunk_root, 0);
  3138. if (IS_ERR(trans)) {
  3139. mutex_unlock(&fs_info->delete_unused_bgs_mutex);
  3140. ret = PTR_ERR(trans);
  3141. goto error;
  3142. }
  3143. ret = btrfs_force_chunk_alloc(trans, fs_info,
  3144. BTRFS_BLOCK_GROUP_DATA);
  3145. btrfs_end_transaction(trans);
  3146. if (ret < 0) {
  3147. mutex_unlock(&fs_info->delete_unused_bgs_mutex);
  3148. goto error;
  3149. }
  3150. chunk_reserved = 1;
  3151. }
  3152. ret = btrfs_relocate_chunk(fs_info, found_key.offset);
  3153. mutex_unlock(&fs_info->delete_unused_bgs_mutex);
  3154. if (ret && ret != -ENOSPC)
  3155. goto error;
  3156. if (ret == -ENOSPC) {
  3157. enospc_errors++;
  3158. } else {
  3159. spin_lock(&fs_info->balance_lock);
  3160. bctl->stat.completed++;
  3161. spin_unlock(&fs_info->balance_lock);
  3162. }
  3163. loop:
  3164. if (found_key.offset == 0)
  3165. break;
  3166. key.offset = found_key.offset - 1;
  3167. }
  3168. if (counting) {
  3169. btrfs_release_path(path);
  3170. counting = false;
  3171. goto again;
  3172. }
  3173. error:
  3174. btrfs_free_path(path);
  3175. if (enospc_errors) {
  3176. btrfs_info(fs_info, "%d enospc errors during balance",
  3177. enospc_errors);
  3178. if (!ret)
  3179. ret = -ENOSPC;
  3180. }
  3181. return ret;
  3182. }
  3183. /**
  3184. * alloc_profile_is_valid - see if a given profile is valid and reduced
  3185. * @flags: profile to validate
  3186. * @extended: if true @flags is treated as an extended profile
  3187. */
  3188. static int alloc_profile_is_valid(u64 flags, int extended)
  3189. {
  3190. u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
  3191. BTRFS_BLOCK_GROUP_PROFILE_MASK);
  3192. flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
  3193. /* 1) check that all other bits are zeroed */
  3194. if (flags & ~mask)
  3195. return 0;
  3196. /* 2) see if profile is reduced */
  3197. if (flags == 0)
  3198. return !extended; /* "0" is valid for usual profiles */
  3199. /* true if exactly one bit set */
  3200. return (flags & (flags - 1)) == 0;
  3201. }
  3202. static inline int balance_need_close(struct btrfs_fs_info *fs_info)
  3203. {
  3204. /* cancel requested || normal exit path */
  3205. return atomic_read(&fs_info->balance_cancel_req) ||
  3206. (atomic_read(&fs_info->balance_pause_req) == 0 &&
  3207. atomic_read(&fs_info->balance_cancel_req) == 0);
  3208. }
  3209. static void __cancel_balance(struct btrfs_fs_info *fs_info)
  3210. {
  3211. int ret;
  3212. unset_balance_control(fs_info);
  3213. ret = del_balance_item(fs_info);
  3214. if (ret)
  3215. btrfs_handle_fs_error(fs_info, ret, NULL);
  3216. clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
  3217. }
  3218. /* Non-zero return value signifies invalidity */
  3219. static inline int validate_convert_profile(struct btrfs_balance_args *bctl_arg,
  3220. u64 allowed)
  3221. {
  3222. return ((bctl_arg->flags & BTRFS_BALANCE_ARGS_CONVERT) &&
  3223. (!alloc_profile_is_valid(bctl_arg->target, 1) ||
  3224. (bctl_arg->target & ~allowed)));
  3225. }
  3226. /*
  3227. * Should be called with both balance and volume mutexes held
  3228. */
  3229. int btrfs_balance(struct btrfs_balance_control *bctl,
  3230. struct btrfs_ioctl_balance_args *bargs)
  3231. {
  3232. struct btrfs_fs_info *fs_info = bctl->fs_info;
  3233. u64 meta_target, data_target;
  3234. u64 allowed;
  3235. int mixed = 0;
  3236. int ret;
  3237. u64 num_devices;
  3238. unsigned seq;
  3239. if (btrfs_fs_closing(fs_info) ||
  3240. atomic_read(&fs_info->balance_pause_req) ||
  3241. atomic_read(&fs_info->balance_cancel_req)) {
  3242. ret = -EINVAL;
  3243. goto out;
  3244. }
  3245. allowed = btrfs_super_incompat_flags(fs_info->super_copy);
  3246. if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
  3247. mixed = 1;
  3248. /*
  3249. * In case of mixed groups both data and meta should be picked,
  3250. * and identical options should be given for both of them.
  3251. */
  3252. allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
  3253. if (mixed && (bctl->flags & allowed)) {
  3254. if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
  3255. !(bctl->flags & BTRFS_BALANCE_METADATA) ||
  3256. memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
  3257. btrfs_err(fs_info,
  3258. "with mixed groups data and metadata balance options must be the same");
  3259. ret = -EINVAL;
  3260. goto out;
  3261. }
  3262. }
  3263. num_devices = fs_info->fs_devices->num_devices;
  3264. btrfs_dev_replace_lock(&fs_info->dev_replace, 0);
  3265. if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
  3266. BUG_ON(num_devices < 1);
  3267. num_devices--;
  3268. }
  3269. btrfs_dev_replace_unlock(&fs_info->dev_replace, 0);
  3270. allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE | BTRFS_BLOCK_GROUP_DUP;
  3271. if (num_devices > 1)
  3272. allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
  3273. if (num_devices > 2)
  3274. allowed |= BTRFS_BLOCK_GROUP_RAID5;
  3275. if (num_devices > 3)
  3276. allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
  3277. BTRFS_BLOCK_GROUP_RAID6);
  3278. if (validate_convert_profile(&bctl->data, allowed)) {
  3279. btrfs_err(fs_info,
  3280. "unable to start balance with target data profile %llu",
  3281. bctl->data.target);
  3282. ret = -EINVAL;
  3283. goto out;
  3284. }
  3285. if (validate_convert_profile(&bctl->meta, allowed)) {
  3286. btrfs_err(fs_info,
  3287. "unable to start balance with target metadata profile %llu",
  3288. bctl->meta.target);
  3289. ret = -EINVAL;
  3290. goto out;
  3291. }
  3292. if (validate_convert_profile(&bctl->sys, allowed)) {
  3293. btrfs_err(fs_info,
  3294. "unable to start balance with target system profile %llu",
  3295. bctl->sys.target);
  3296. ret = -EINVAL;
  3297. goto out;
  3298. }
  3299. /* allow to reduce meta or sys integrity only if force set */
  3300. allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
  3301. BTRFS_BLOCK_GROUP_RAID10 |
  3302. BTRFS_BLOCK_GROUP_RAID5 |
  3303. BTRFS_BLOCK_GROUP_RAID6;
  3304. do {
  3305. seq = read_seqbegin(&fs_info->profiles_lock);
  3306. if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
  3307. (fs_info->avail_system_alloc_bits & allowed) &&
  3308. !(bctl->sys.target & allowed)) ||
  3309. ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
  3310. (fs_info->avail_metadata_alloc_bits & allowed) &&
  3311. !(bctl->meta.target & allowed))) {
  3312. if (bctl->flags & BTRFS_BALANCE_FORCE) {
  3313. btrfs_info(fs_info,
  3314. "force reducing metadata integrity");
  3315. } else {
  3316. btrfs_err(fs_info,
  3317. "balance will reduce metadata integrity, use force if you want this");
  3318. ret = -EINVAL;
  3319. goto out;
  3320. }
  3321. }
  3322. } while (read_seqretry(&fs_info->profiles_lock, seq));
  3323. /* if we're not converting, the target field is uninitialized */
  3324. meta_target = (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
  3325. bctl->meta.target : fs_info->avail_metadata_alloc_bits;
  3326. data_target = (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
  3327. bctl->data.target : fs_info->avail_data_alloc_bits;
  3328. if (btrfs_get_num_tolerated_disk_barrier_failures(meta_target) <
  3329. btrfs_get_num_tolerated_disk_barrier_failures(data_target)) {
  3330. btrfs_warn(fs_info,
  3331. "metadata profile 0x%llx has lower redundancy than data profile 0x%llx",
  3332. meta_target, data_target);
  3333. }
  3334. ret = insert_balance_item(fs_info, bctl);
  3335. if (ret && ret != -EEXIST)
  3336. goto out;
  3337. if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
  3338. BUG_ON(ret == -EEXIST);
  3339. set_balance_control(bctl);
  3340. } else {
  3341. BUG_ON(ret != -EEXIST);
  3342. spin_lock(&fs_info->balance_lock);
  3343. update_balance_args(bctl);
  3344. spin_unlock(&fs_info->balance_lock);
  3345. }
  3346. atomic_inc(&fs_info->balance_running);
  3347. mutex_unlock(&fs_info->balance_mutex);
  3348. ret = __btrfs_balance(fs_info);
  3349. mutex_lock(&fs_info->balance_mutex);
  3350. atomic_dec(&fs_info->balance_running);
  3351. if (bargs) {
  3352. memset(bargs, 0, sizeof(*bargs));
  3353. update_ioctl_balance_args(fs_info, 0, bargs);
  3354. }
  3355. if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
  3356. balance_need_close(fs_info)) {
  3357. __cancel_balance(fs_info);
  3358. }
  3359. wake_up(&fs_info->balance_wait_q);
  3360. return ret;
  3361. out:
  3362. if (bctl->flags & BTRFS_BALANCE_RESUME)
  3363. __cancel_balance(fs_info);
  3364. else {
  3365. kfree(bctl);
  3366. clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
  3367. }
  3368. return ret;
  3369. }
  3370. static int balance_kthread(void *data)
  3371. {
  3372. struct btrfs_fs_info *fs_info = data;
  3373. int ret = 0;
  3374. mutex_lock(&fs_info->volume_mutex);
  3375. mutex_lock(&fs_info->balance_mutex);
  3376. if (fs_info->balance_ctl) {
  3377. btrfs_info(fs_info, "continuing balance");
  3378. ret = btrfs_balance(fs_info->balance_ctl, NULL);
  3379. }
  3380. mutex_unlock(&fs_info->balance_mutex);
  3381. mutex_unlock(&fs_info->volume_mutex);
  3382. return ret;
  3383. }
  3384. int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
  3385. {
  3386. struct task_struct *tsk;
  3387. spin_lock(&fs_info->balance_lock);
  3388. if (!fs_info->balance_ctl) {
  3389. spin_unlock(&fs_info->balance_lock);
  3390. return 0;
  3391. }
  3392. spin_unlock(&fs_info->balance_lock);
  3393. if (btrfs_test_opt(fs_info, SKIP_BALANCE)) {
  3394. btrfs_info(fs_info, "force skipping balance");
  3395. return 0;
  3396. }
  3397. tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
  3398. return PTR_ERR_OR_ZERO(tsk);
  3399. }
  3400. int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
  3401. {
  3402. struct btrfs_balance_control *bctl;
  3403. struct btrfs_balance_item *item;
  3404. struct btrfs_disk_balance_args disk_bargs;
  3405. struct btrfs_path *path;
  3406. struct extent_buffer *leaf;
  3407. struct btrfs_key key;
  3408. int ret;
  3409. path = btrfs_alloc_path();
  3410. if (!path)
  3411. return -ENOMEM;
  3412. key.objectid = BTRFS_BALANCE_OBJECTID;
  3413. key.type = BTRFS_TEMPORARY_ITEM_KEY;
  3414. key.offset = 0;
  3415. ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
  3416. if (ret < 0)
  3417. goto out;
  3418. if (ret > 0) { /* ret = -ENOENT; */
  3419. ret = 0;
  3420. goto out;
  3421. }
  3422. bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
  3423. if (!bctl) {
  3424. ret = -ENOMEM;
  3425. goto out;
  3426. }
  3427. leaf = path->nodes[0];
  3428. item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
  3429. bctl->fs_info = fs_info;
  3430. bctl->flags = btrfs_balance_flags(leaf, item);
  3431. bctl->flags |= BTRFS_BALANCE_RESUME;
  3432. btrfs_balance_data(leaf, item, &disk_bargs);
  3433. btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
  3434. btrfs_balance_meta(leaf, item, &disk_bargs);
  3435. btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
  3436. btrfs_balance_sys(leaf, item, &disk_bargs);
  3437. btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
  3438. WARN_ON(test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags));
  3439. mutex_lock(&fs_info->volume_mutex);
  3440. mutex_lock(&fs_info->balance_mutex);
  3441. set_balance_control(bctl);
  3442. mutex_unlock(&fs_info->balance_mutex);
  3443. mutex_unlock(&fs_info->volume_mutex);
  3444. out:
  3445. btrfs_free_path(path);
  3446. return ret;
  3447. }
  3448. int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
  3449. {
  3450. int ret = 0;
  3451. mutex_lock(&fs_info->balance_mutex);
  3452. if (!fs_info->balance_ctl) {
  3453. mutex_unlock(&fs_info->balance_mutex);
  3454. return -ENOTCONN;
  3455. }
  3456. if (atomic_read(&fs_info->balance_running)) {
  3457. atomic_inc(&fs_info->balance_pause_req);
  3458. mutex_unlock(&fs_info->balance_mutex);
  3459. wait_event(fs_info->balance_wait_q,
  3460. atomic_read(&fs_info->balance_running) == 0);
  3461. mutex_lock(&fs_info->balance_mutex);
  3462. /* we are good with balance_ctl ripped off from under us */
  3463. BUG_ON(atomic_read(&fs_info->balance_running));
  3464. atomic_dec(&fs_info->balance_pause_req);
  3465. } else {
  3466. ret = -ENOTCONN;
  3467. }
  3468. mutex_unlock(&fs_info->balance_mutex);
  3469. return ret;
  3470. }
  3471. int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
  3472. {
  3473. if (sb_rdonly(fs_info->sb))
  3474. return -EROFS;
  3475. mutex_lock(&fs_info->balance_mutex);
  3476. if (!fs_info->balance_ctl) {
  3477. mutex_unlock(&fs_info->balance_mutex);
  3478. return -ENOTCONN;
  3479. }
  3480. atomic_inc(&fs_info->balance_cancel_req);
  3481. /*
  3482. * if we are running just wait and return, balance item is
  3483. * deleted in btrfs_balance in this case
  3484. */
  3485. if (atomic_read(&fs_info->balance_running)) {
  3486. mutex_unlock(&fs_info->balance_mutex);
  3487. wait_event(fs_info->balance_wait_q,
  3488. atomic_read(&fs_info->balance_running) == 0);
  3489. mutex_lock(&fs_info->balance_mutex);
  3490. } else {
  3491. /* __cancel_balance needs volume_mutex */
  3492. mutex_unlock(&fs_info->balance_mutex);
  3493. mutex_lock(&fs_info->volume_mutex);
  3494. mutex_lock(&fs_info->balance_mutex);
  3495. if (fs_info->balance_ctl)
  3496. __cancel_balance(fs_info);
  3497. mutex_unlock(&fs_info->volume_mutex);
  3498. }
  3499. BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
  3500. atomic_dec(&fs_info->balance_cancel_req);
  3501. mutex_unlock(&fs_info->balance_mutex);
  3502. return 0;
  3503. }
  3504. static int btrfs_uuid_scan_kthread(void *data)
  3505. {
  3506. struct btrfs_fs_info *fs_info = data;
  3507. struct btrfs_root *root = fs_info->tree_root;
  3508. struct btrfs_key key;
  3509. struct btrfs_path *path = NULL;
  3510. int ret = 0;
  3511. struct extent_buffer *eb;
  3512. int slot;
  3513. struct btrfs_root_item root_item;
  3514. u32 item_size;
  3515. struct btrfs_trans_handle *trans = NULL;
  3516. path = btrfs_alloc_path();
  3517. if (!path) {
  3518. ret = -ENOMEM;
  3519. goto out;
  3520. }
  3521. key.objectid = 0;
  3522. key.type = BTRFS_ROOT_ITEM_KEY;
  3523. key.offset = 0;
  3524. while (1) {
  3525. ret = btrfs_search_forward(root, &key, path, 0);
  3526. if (ret) {
  3527. if (ret > 0)
  3528. ret = 0;
  3529. break;
  3530. }
  3531. if (key.type != BTRFS_ROOT_ITEM_KEY ||
  3532. (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
  3533. key.objectid != BTRFS_FS_TREE_OBJECTID) ||
  3534. key.objectid > BTRFS_LAST_FREE_OBJECTID)
  3535. goto skip;
  3536. eb = path->nodes[0];
  3537. slot = path->slots[0];
  3538. item_size = btrfs_item_size_nr(eb, slot);
  3539. if (item_size < sizeof(root_item))
  3540. goto skip;
  3541. read_extent_buffer(eb, &root_item,
  3542. btrfs_item_ptr_offset(eb, slot),
  3543. (int)sizeof(root_item));
  3544. if (btrfs_root_refs(&root_item) == 0)
  3545. goto skip;
  3546. if (!btrfs_is_empty_uuid(root_item.uuid) ||
  3547. !btrfs_is_empty_uuid(root_item.received_uuid)) {
  3548. if (trans)
  3549. goto update_tree;
  3550. btrfs_release_path(path);
  3551. /*
  3552. * 1 - subvol uuid item
  3553. * 1 - received_subvol uuid item
  3554. */
  3555. trans = btrfs_start_transaction(fs_info->uuid_root, 2);
  3556. if (IS_ERR(trans)) {
  3557. ret = PTR_ERR(trans);
  3558. break;
  3559. }
  3560. continue;
  3561. } else {
  3562. goto skip;
  3563. }
  3564. update_tree:
  3565. if (!btrfs_is_empty_uuid(root_item.uuid)) {
  3566. ret = btrfs_uuid_tree_add(trans, fs_info,
  3567. root_item.uuid,
  3568. BTRFS_UUID_KEY_SUBVOL,
  3569. key.objectid);
  3570. if (ret < 0) {
  3571. btrfs_warn(fs_info, "uuid_tree_add failed %d",
  3572. ret);
  3573. break;
  3574. }
  3575. }
  3576. if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
  3577. ret = btrfs_uuid_tree_add(trans, fs_info,
  3578. root_item.received_uuid,
  3579. BTRFS_UUID_KEY_RECEIVED_SUBVOL,
  3580. key.objectid);
  3581. if (ret < 0) {
  3582. btrfs_warn(fs_info, "uuid_tree_add failed %d",
  3583. ret);
  3584. break;
  3585. }
  3586. }
  3587. skip:
  3588. if (trans) {
  3589. ret = btrfs_end_transaction(trans);
  3590. trans = NULL;
  3591. if (ret)
  3592. break;
  3593. }
  3594. btrfs_release_path(path);
  3595. if (key.offset < (u64)-1) {
  3596. key.offset++;
  3597. } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
  3598. key.offset = 0;
  3599. key.type = BTRFS_ROOT_ITEM_KEY;
  3600. } else if (key.objectid < (u64)-1) {
  3601. key.offset = 0;
  3602. key.type = BTRFS_ROOT_ITEM_KEY;
  3603. key.objectid++;
  3604. } else {
  3605. break;
  3606. }
  3607. cond_resched();
  3608. }
  3609. out:
  3610. btrfs_free_path(path);
  3611. if (trans && !IS_ERR(trans))
  3612. btrfs_end_transaction(trans);
  3613. if (ret)
  3614. btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
  3615. else
  3616. set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags);
  3617. up(&fs_info->uuid_tree_rescan_sem);
  3618. return 0;
  3619. }
  3620. /*
  3621. * Callback for btrfs_uuid_tree_iterate().
  3622. * returns:
  3623. * 0 check succeeded, the entry is not outdated.
  3624. * < 0 if an error occurred.
  3625. * > 0 if the check failed, which means the caller shall remove the entry.
  3626. */
  3627. static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
  3628. u8 *uuid, u8 type, u64 subid)
  3629. {
  3630. struct btrfs_key key;
  3631. int ret = 0;
  3632. struct btrfs_root *subvol_root;
  3633. if (type != BTRFS_UUID_KEY_SUBVOL &&
  3634. type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
  3635. goto out;
  3636. key.objectid = subid;
  3637. key.type = BTRFS_ROOT_ITEM_KEY;
  3638. key.offset = (u64)-1;
  3639. subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
  3640. if (IS_ERR(subvol_root)) {
  3641. ret = PTR_ERR(subvol_root);
  3642. if (ret == -ENOENT)
  3643. ret = 1;
  3644. goto out;
  3645. }
  3646. switch (type) {
  3647. case BTRFS_UUID_KEY_SUBVOL:
  3648. if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
  3649. ret = 1;
  3650. break;
  3651. case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
  3652. if (memcmp(uuid, subvol_root->root_item.received_uuid,
  3653. BTRFS_UUID_SIZE))
  3654. ret = 1;
  3655. break;
  3656. }
  3657. out:
  3658. return ret;
  3659. }
  3660. static int btrfs_uuid_rescan_kthread(void *data)
  3661. {
  3662. struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
  3663. int ret;
  3664. /*
  3665. * 1st step is to iterate through the existing UUID tree and
  3666. * to delete all entries that contain outdated data.
  3667. * 2nd step is to add all missing entries to the UUID tree.
  3668. */
  3669. ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
  3670. if (ret < 0) {
  3671. btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
  3672. up(&fs_info->uuid_tree_rescan_sem);
  3673. return ret;
  3674. }
  3675. return btrfs_uuid_scan_kthread(data);
  3676. }
  3677. int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
  3678. {
  3679. struct btrfs_trans_handle *trans;
  3680. struct btrfs_root *tree_root = fs_info->tree_root;
  3681. struct btrfs_root *uuid_root;
  3682. struct task_struct *task;
  3683. int ret;
  3684. /*
  3685. * 1 - root node
  3686. * 1 - root item
  3687. */
  3688. trans = btrfs_start_transaction(tree_root, 2);
  3689. if (IS_ERR(trans))
  3690. return PTR_ERR(trans);
  3691. uuid_root = btrfs_create_tree(trans, fs_info,
  3692. BTRFS_UUID_TREE_OBJECTID);
  3693. if (IS_ERR(uuid_root)) {
  3694. ret = PTR_ERR(uuid_root);
  3695. btrfs_abort_transaction(trans, ret);
  3696. btrfs_end_transaction(trans);
  3697. return ret;
  3698. }
  3699. fs_info->uuid_root = uuid_root;
  3700. ret = btrfs_commit_transaction(trans);
  3701. if (ret)
  3702. return ret;
  3703. down(&fs_info->uuid_tree_rescan_sem);
  3704. task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
  3705. if (IS_ERR(task)) {
  3706. /* fs_info->update_uuid_tree_gen remains 0 in all error case */
  3707. btrfs_warn(fs_info, "failed to start uuid_scan task");
  3708. up(&fs_info->uuid_tree_rescan_sem);
  3709. return PTR_ERR(task);
  3710. }
  3711. return 0;
  3712. }
  3713. int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
  3714. {
  3715. struct task_struct *task;
  3716. down(&fs_info->uuid_tree_rescan_sem);
  3717. task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
  3718. if (IS_ERR(task)) {
  3719. /* fs_info->update_uuid_tree_gen remains 0 in all error case */
  3720. btrfs_warn(fs_info, "failed to start uuid_rescan task");
  3721. up(&fs_info->uuid_tree_rescan_sem);
  3722. return PTR_ERR(task);
  3723. }
  3724. return 0;
  3725. }
  3726. /*
  3727. * shrinking a device means finding all of the device extents past
  3728. * the new size, and then following the back refs to the chunks.
  3729. * The chunk relocation code actually frees the device extent
  3730. */
  3731. int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
  3732. {
  3733. struct btrfs_fs_info *fs_info = device->fs_info;
  3734. struct btrfs_root *root = fs_info->dev_root;
  3735. struct btrfs_trans_handle *trans;
  3736. struct btrfs_dev_extent *dev_extent = NULL;
  3737. struct btrfs_path *path;
  3738. u64 length;
  3739. u64 chunk_offset;
  3740. int ret;
  3741. int slot;
  3742. int failed = 0;
  3743. bool retried = false;
  3744. bool checked_pending_chunks = false;
  3745. struct extent_buffer *l;
  3746. struct btrfs_key key;
  3747. struct btrfs_super_block *super_copy = fs_info->super_copy;
  3748. u64 old_total = btrfs_super_total_bytes(super_copy);
  3749. u64 old_size = btrfs_device_get_total_bytes(device);
  3750. u64 diff;
  3751. new_size = round_down(new_size, fs_info->sectorsize);
  3752. diff = round_down(old_size - new_size, fs_info->sectorsize);
  3753. if (device->is_tgtdev_for_dev_replace)
  3754. return -EINVAL;
  3755. path = btrfs_alloc_path();
  3756. if (!path)
  3757. return -ENOMEM;
  3758. path->reada = READA_FORWARD;
  3759. mutex_lock(&fs_info->chunk_mutex);
  3760. btrfs_device_set_total_bytes(device, new_size);
  3761. if (device->writeable) {
  3762. device->fs_devices->total_rw_bytes -= diff;
  3763. atomic64_sub(diff, &fs_info->free_chunk_space);
  3764. }
  3765. mutex_unlock(&fs_info->chunk_mutex);
  3766. again:
  3767. key.objectid = device->devid;
  3768. key.offset = (u64)-1;
  3769. key.type = BTRFS_DEV_EXTENT_KEY;
  3770. do {
  3771. mutex_lock(&fs_info->delete_unused_bgs_mutex);
  3772. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3773. if (ret < 0) {
  3774. mutex_unlock(&fs_info->delete_unused_bgs_mutex);
  3775. goto done;
  3776. }
  3777. ret = btrfs_previous_item(root, path, 0, key.type);
  3778. if (ret)
  3779. mutex_unlock(&fs_info->delete_unused_bgs_mutex);
  3780. if (ret < 0)
  3781. goto done;
  3782. if (ret) {
  3783. ret = 0;
  3784. btrfs_release_path(path);
  3785. break;
  3786. }
  3787. l = path->nodes[0];
  3788. slot = path->slots[0];
  3789. btrfs_item_key_to_cpu(l, &key, path->slots[0]);
  3790. if (key.objectid != device->devid) {
  3791. mutex_unlock(&fs_info->delete_unused_bgs_mutex);
  3792. btrfs_release_path(path);
  3793. break;
  3794. }
  3795. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  3796. length = btrfs_dev_extent_length(l, dev_extent);
  3797. if (key.offset + length <= new_size) {
  3798. mutex_unlock(&fs_info->delete_unused_bgs_mutex);
  3799. btrfs_release_path(path);
  3800. break;
  3801. }
  3802. chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
  3803. btrfs_release_path(path);
  3804. ret = btrfs_relocate_chunk(fs_info, chunk_offset);
  3805. mutex_unlock(&fs_info->delete_unused_bgs_mutex);
  3806. if (ret && ret != -ENOSPC)
  3807. goto done;
  3808. if (ret == -ENOSPC)
  3809. failed++;
  3810. } while (key.offset-- > 0);
  3811. if (failed && !retried) {
  3812. failed = 0;
  3813. retried = true;
  3814. goto again;
  3815. } else if (failed && retried) {
  3816. ret = -ENOSPC;
  3817. goto done;
  3818. }
  3819. /* Shrinking succeeded, else we would be at "done". */
  3820. trans = btrfs_start_transaction(root, 0);
  3821. if (IS_ERR(trans)) {
  3822. ret = PTR_ERR(trans);
  3823. goto done;
  3824. }
  3825. mutex_lock(&fs_info->chunk_mutex);
  3826. /*
  3827. * We checked in the above loop all device extents that were already in
  3828. * the device tree. However before we have updated the device's
  3829. * total_bytes to the new size, we might have had chunk allocations that
  3830. * have not complete yet (new block groups attached to transaction
  3831. * handles), and therefore their device extents were not yet in the
  3832. * device tree and we missed them in the loop above. So if we have any
  3833. * pending chunk using a device extent that overlaps the device range
  3834. * that we can not use anymore, commit the current transaction and
  3835. * repeat the search on the device tree - this way we guarantee we will
  3836. * not have chunks using device extents that end beyond 'new_size'.
  3837. */
  3838. if (!checked_pending_chunks) {
  3839. u64 start = new_size;
  3840. u64 len = old_size - new_size;
  3841. if (contains_pending_extent(trans->transaction, device,
  3842. &start, len)) {
  3843. mutex_unlock(&fs_info->chunk_mutex);
  3844. checked_pending_chunks = true;
  3845. failed = 0;
  3846. retried = false;
  3847. ret = btrfs_commit_transaction(trans);
  3848. if (ret)
  3849. goto done;
  3850. goto again;
  3851. }
  3852. }
  3853. btrfs_device_set_disk_total_bytes(device, new_size);
  3854. if (list_empty(&device->resized_list))
  3855. list_add_tail(&device->resized_list,
  3856. &fs_info->fs_devices->resized_devices);
  3857. WARN_ON(diff > old_total);
  3858. btrfs_set_super_total_bytes(super_copy,
  3859. round_down(old_total - diff, fs_info->sectorsize));
  3860. mutex_unlock(&fs_info->chunk_mutex);
  3861. /* Now btrfs_update_device() will change the on-disk size. */
  3862. ret = btrfs_update_device(trans, device);
  3863. btrfs_end_transaction(trans);
  3864. done:
  3865. btrfs_free_path(path);
  3866. if (ret) {
  3867. mutex_lock(&fs_info->chunk_mutex);
  3868. btrfs_device_set_total_bytes(device, old_size);
  3869. if (device->writeable)
  3870. device->fs_devices->total_rw_bytes += diff;
  3871. atomic64_add(diff, &fs_info->free_chunk_space);
  3872. mutex_unlock(&fs_info->chunk_mutex);
  3873. }
  3874. return ret;
  3875. }
  3876. static int btrfs_add_system_chunk(struct btrfs_fs_info *fs_info,
  3877. struct btrfs_key *key,
  3878. struct btrfs_chunk *chunk, int item_size)
  3879. {
  3880. struct btrfs_super_block *super_copy = fs_info->super_copy;
  3881. struct btrfs_disk_key disk_key;
  3882. u32 array_size;
  3883. u8 *ptr;
  3884. mutex_lock(&fs_info->chunk_mutex);
  3885. array_size = btrfs_super_sys_array_size(super_copy);
  3886. if (array_size + item_size + sizeof(disk_key)
  3887. > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
  3888. mutex_unlock(&fs_info->chunk_mutex);
  3889. return -EFBIG;
  3890. }
  3891. ptr = super_copy->sys_chunk_array + array_size;
  3892. btrfs_cpu_key_to_disk(&disk_key, key);
  3893. memcpy(ptr, &disk_key, sizeof(disk_key));
  3894. ptr += sizeof(disk_key);
  3895. memcpy(ptr, chunk, item_size);
  3896. item_size += sizeof(disk_key);
  3897. btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
  3898. mutex_unlock(&fs_info->chunk_mutex);
  3899. return 0;
  3900. }
  3901. /*
  3902. * sort the devices in descending order by max_avail, total_avail
  3903. */
  3904. static int btrfs_cmp_device_info(const void *a, const void *b)
  3905. {
  3906. const struct btrfs_device_info *di_a = a;
  3907. const struct btrfs_device_info *di_b = b;
  3908. if (di_a->max_avail > di_b->max_avail)
  3909. return -1;
  3910. if (di_a->max_avail < di_b->max_avail)
  3911. return 1;
  3912. if (di_a->total_avail > di_b->total_avail)
  3913. return -1;
  3914. if (di_a->total_avail < di_b->total_avail)
  3915. return 1;
  3916. return 0;
  3917. }
  3918. static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
  3919. {
  3920. if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK))
  3921. return;
  3922. btrfs_set_fs_incompat(info, RAID56);
  3923. }
  3924. #define BTRFS_MAX_DEVS(r) ((BTRFS_MAX_ITEM_SIZE(r->fs_info) \
  3925. - sizeof(struct btrfs_chunk)) \
  3926. / sizeof(struct btrfs_stripe) + 1)
  3927. #define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
  3928. - 2 * sizeof(struct btrfs_disk_key) \
  3929. - 2 * sizeof(struct btrfs_chunk)) \
  3930. / sizeof(struct btrfs_stripe) + 1)
  3931. static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
  3932. u64 start, u64 type)
  3933. {
  3934. struct btrfs_fs_info *info = trans->fs_info;
  3935. struct btrfs_fs_devices *fs_devices = info->fs_devices;
  3936. struct btrfs_device *device;
  3937. struct map_lookup *map = NULL;
  3938. struct extent_map_tree *em_tree;
  3939. struct extent_map *em;
  3940. struct btrfs_device_info *devices_info = NULL;
  3941. u64 total_avail;
  3942. int num_stripes; /* total number of stripes to allocate */
  3943. int data_stripes; /* number of stripes that count for
  3944. block group size */
  3945. int sub_stripes; /* sub_stripes info for map */
  3946. int dev_stripes; /* stripes per dev */
  3947. int devs_max; /* max devs to use */
  3948. int devs_min; /* min devs needed */
  3949. int devs_increment; /* ndevs has to be a multiple of this */
  3950. int ncopies; /* how many copies to data has */
  3951. int ret;
  3952. u64 max_stripe_size;
  3953. u64 max_chunk_size;
  3954. u64 stripe_size;
  3955. u64 num_bytes;
  3956. int ndevs;
  3957. int i;
  3958. int j;
  3959. int index;
  3960. BUG_ON(!alloc_profile_is_valid(type, 0));
  3961. if (list_empty(&fs_devices->alloc_list))
  3962. return -ENOSPC;
  3963. index = __get_raid_index(type);
  3964. sub_stripes = btrfs_raid_array[index].sub_stripes;
  3965. dev_stripes = btrfs_raid_array[index].dev_stripes;
  3966. devs_max = btrfs_raid_array[index].devs_max;
  3967. devs_min = btrfs_raid_array[index].devs_min;
  3968. devs_increment = btrfs_raid_array[index].devs_increment;
  3969. ncopies = btrfs_raid_array[index].ncopies;
  3970. if (type & BTRFS_BLOCK_GROUP_DATA) {
  3971. max_stripe_size = SZ_1G;
  3972. max_chunk_size = 10 * max_stripe_size;
  3973. if (!devs_max)
  3974. devs_max = BTRFS_MAX_DEVS(info->chunk_root);
  3975. } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
  3976. /* for larger filesystems, use larger metadata chunks */
  3977. if (fs_devices->total_rw_bytes > 50ULL * SZ_1G)
  3978. max_stripe_size = SZ_1G;
  3979. else
  3980. max_stripe_size = SZ_256M;
  3981. max_chunk_size = max_stripe_size;
  3982. if (!devs_max)
  3983. devs_max = BTRFS_MAX_DEVS(info->chunk_root);
  3984. } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
  3985. max_stripe_size = SZ_32M;
  3986. max_chunk_size = 2 * max_stripe_size;
  3987. if (!devs_max)
  3988. devs_max = BTRFS_MAX_DEVS_SYS_CHUNK;
  3989. } else {
  3990. btrfs_err(info, "invalid chunk type 0x%llx requested",
  3991. type);
  3992. BUG_ON(1);
  3993. }
  3994. /* we don't want a chunk larger than 10% of writeable space */
  3995. max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
  3996. max_chunk_size);
  3997. devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info),
  3998. GFP_NOFS);
  3999. if (!devices_info)
  4000. return -ENOMEM;
  4001. /*
  4002. * in the first pass through the devices list, we gather information
  4003. * about the available holes on each device.
  4004. */
  4005. ndevs = 0;
  4006. list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
  4007. u64 max_avail;
  4008. u64 dev_offset;
  4009. if (!device->writeable) {
  4010. WARN(1, KERN_ERR
  4011. "BTRFS: read-only device in alloc_list\n");
  4012. continue;
  4013. }
  4014. if (!device->in_fs_metadata ||
  4015. device->is_tgtdev_for_dev_replace)
  4016. continue;
  4017. if (device->total_bytes > device->bytes_used)
  4018. total_avail = device->total_bytes - device->bytes_used;
  4019. else
  4020. total_avail = 0;
  4021. /* If there is no space on this device, skip it. */
  4022. if (total_avail == 0)
  4023. continue;
  4024. ret = find_free_dev_extent(trans, device,
  4025. max_stripe_size * dev_stripes,
  4026. &dev_offset, &max_avail);
  4027. if (ret && ret != -ENOSPC)
  4028. goto error;
  4029. if (ret == 0)
  4030. max_avail = max_stripe_size * dev_stripes;
  4031. if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
  4032. continue;
  4033. if (ndevs == fs_devices->rw_devices) {
  4034. WARN(1, "%s: found more than %llu devices\n",
  4035. __func__, fs_devices->rw_devices);
  4036. break;
  4037. }
  4038. devices_info[ndevs].dev_offset = dev_offset;
  4039. devices_info[ndevs].max_avail = max_avail;
  4040. devices_info[ndevs].total_avail = total_avail;
  4041. devices_info[ndevs].dev = device;
  4042. ++ndevs;
  4043. }
  4044. /*
  4045. * now sort the devices by hole size / available space
  4046. */
  4047. sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
  4048. btrfs_cmp_device_info, NULL);
  4049. /* round down to number of usable stripes */
  4050. ndevs = round_down(ndevs, devs_increment);
  4051. if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
  4052. ret = -ENOSPC;
  4053. goto error;
  4054. }
  4055. ndevs = min(ndevs, devs_max);
  4056. /*
  4057. * the primary goal is to maximize the number of stripes, so use as many
  4058. * devices as possible, even if the stripes are not maximum sized.
  4059. */
  4060. stripe_size = devices_info[ndevs-1].max_avail;
  4061. num_stripes = ndevs * dev_stripes;
  4062. /*
  4063. * this will have to be fixed for RAID1 and RAID10 over
  4064. * more drives
  4065. */
  4066. data_stripes = num_stripes / ncopies;
  4067. if (type & BTRFS_BLOCK_GROUP_RAID5)
  4068. data_stripes = num_stripes - 1;
  4069. if (type & BTRFS_BLOCK_GROUP_RAID6)
  4070. data_stripes = num_stripes - 2;
  4071. /*
  4072. * Use the number of data stripes to figure out how big this chunk
  4073. * is really going to be in terms of logical address space,
  4074. * and compare that answer with the max chunk size
  4075. */
  4076. if (stripe_size * data_stripes > max_chunk_size) {
  4077. u64 mask = (1ULL << 24) - 1;
  4078. stripe_size = div_u64(max_chunk_size, data_stripes);
  4079. /* bump the answer up to a 16MB boundary */
  4080. stripe_size = (stripe_size + mask) & ~mask;
  4081. /* but don't go higher than the limits we found
  4082. * while searching for free extents
  4083. */
  4084. if (stripe_size > devices_info[ndevs-1].max_avail)
  4085. stripe_size = devices_info[ndevs-1].max_avail;
  4086. }
  4087. stripe_size = div_u64(stripe_size, dev_stripes);
  4088. /* align to BTRFS_STRIPE_LEN */
  4089. stripe_size = round_down(stripe_size, BTRFS_STRIPE_LEN);
  4090. map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
  4091. if (!map) {
  4092. ret = -ENOMEM;
  4093. goto error;
  4094. }
  4095. map->num_stripes = num_stripes;
  4096. for (i = 0; i < ndevs; ++i) {
  4097. for (j = 0; j < dev_stripes; ++j) {
  4098. int s = i * dev_stripes + j;
  4099. map->stripes[s].dev = devices_info[i].dev;
  4100. map->stripes[s].physical = devices_info[i].dev_offset +
  4101. j * stripe_size;
  4102. }
  4103. }
  4104. map->stripe_len = BTRFS_STRIPE_LEN;
  4105. map->io_align = BTRFS_STRIPE_LEN;
  4106. map->io_width = BTRFS_STRIPE_LEN;
  4107. map->type = type;
  4108. map->sub_stripes = sub_stripes;
  4109. num_bytes = stripe_size * data_stripes;
  4110. trace_btrfs_chunk_alloc(info, map, start, num_bytes);
  4111. em = alloc_extent_map();
  4112. if (!em) {
  4113. kfree(map);
  4114. ret = -ENOMEM;
  4115. goto error;
  4116. }
  4117. set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
  4118. em->map_lookup = map;
  4119. em->start = start;
  4120. em->len = num_bytes;
  4121. em->block_start = 0;
  4122. em->block_len = em->len;
  4123. em->orig_block_len = stripe_size;
  4124. em_tree = &info->mapping_tree.map_tree;
  4125. write_lock(&em_tree->lock);
  4126. ret = add_extent_mapping(em_tree, em, 0);
  4127. if (!ret) {
  4128. list_add_tail(&em->list, &trans->transaction->pending_chunks);
  4129. refcount_inc(&em->refs);
  4130. }
  4131. write_unlock(&em_tree->lock);
  4132. if (ret) {
  4133. free_extent_map(em);
  4134. goto error;
  4135. }
  4136. ret = btrfs_make_block_group(trans, info, 0, type, start, num_bytes);
  4137. if (ret)
  4138. goto error_del_extent;
  4139. for (i = 0; i < map->num_stripes; i++) {
  4140. num_bytes = map->stripes[i].dev->bytes_used + stripe_size;
  4141. btrfs_device_set_bytes_used(map->stripes[i].dev, num_bytes);
  4142. }
  4143. atomic64_sub(stripe_size * map->num_stripes, &info->free_chunk_space);
  4144. free_extent_map(em);
  4145. check_raid56_incompat_flag(info, type);
  4146. kfree(devices_info);
  4147. return 0;
  4148. error_del_extent:
  4149. write_lock(&em_tree->lock);
  4150. remove_extent_mapping(em_tree, em);
  4151. write_unlock(&em_tree->lock);
  4152. /* One for our allocation */
  4153. free_extent_map(em);
  4154. /* One for the tree reference */
  4155. free_extent_map(em);
  4156. /* One for the pending_chunks list reference */
  4157. free_extent_map(em);
  4158. error:
  4159. kfree(devices_info);
  4160. return ret;
  4161. }
  4162. int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
  4163. struct btrfs_fs_info *fs_info,
  4164. u64 chunk_offset, u64 chunk_size)
  4165. {
  4166. struct btrfs_root *extent_root = fs_info->extent_root;
  4167. struct btrfs_root *chunk_root = fs_info->chunk_root;
  4168. struct btrfs_key key;
  4169. struct btrfs_device *device;
  4170. struct btrfs_chunk *chunk;
  4171. struct btrfs_stripe *stripe;
  4172. struct extent_map *em;
  4173. struct map_lookup *map;
  4174. size_t item_size;
  4175. u64 dev_offset;
  4176. u64 stripe_size;
  4177. int i = 0;
  4178. int ret = 0;
  4179. em = get_chunk_map(fs_info, chunk_offset, chunk_size);
  4180. if (IS_ERR(em))
  4181. return PTR_ERR(em);
  4182. map = em->map_lookup;
  4183. item_size = btrfs_chunk_item_size(map->num_stripes);
  4184. stripe_size = em->orig_block_len;
  4185. chunk = kzalloc(item_size, GFP_NOFS);
  4186. if (!chunk) {
  4187. ret = -ENOMEM;
  4188. goto out;
  4189. }
  4190. /*
  4191. * Take the device list mutex to prevent races with the final phase of
  4192. * a device replace operation that replaces the device object associated
  4193. * with the map's stripes, because the device object's id can change
  4194. * at any time during that final phase of the device replace operation
  4195. * (dev-replace.c:btrfs_dev_replace_finishing()).
  4196. */
  4197. mutex_lock(&fs_info->fs_devices->device_list_mutex);
  4198. for (i = 0; i < map->num_stripes; i++) {
  4199. device = map->stripes[i].dev;
  4200. dev_offset = map->stripes[i].physical;
  4201. ret = btrfs_update_device(trans, device);
  4202. if (ret)
  4203. break;
  4204. ret = btrfs_alloc_dev_extent(trans, device, chunk_offset,
  4205. dev_offset, stripe_size);
  4206. if (ret)
  4207. break;
  4208. }
  4209. if (ret) {
  4210. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  4211. goto out;
  4212. }
  4213. stripe = &chunk->stripe;
  4214. for (i = 0; i < map->num_stripes; i++) {
  4215. device = map->stripes[i].dev;
  4216. dev_offset = map->stripes[i].physical;
  4217. btrfs_set_stack_stripe_devid(stripe, device->devid);
  4218. btrfs_set_stack_stripe_offset(stripe, dev_offset);
  4219. memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
  4220. stripe++;
  4221. }
  4222. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  4223. btrfs_set_stack_chunk_length(chunk, chunk_size);
  4224. btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
  4225. btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
  4226. btrfs_set_stack_chunk_type(chunk, map->type);
  4227. btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
  4228. btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
  4229. btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
  4230. btrfs_set_stack_chunk_sector_size(chunk, fs_info->sectorsize);
  4231. btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
  4232. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  4233. key.type = BTRFS_CHUNK_ITEM_KEY;
  4234. key.offset = chunk_offset;
  4235. ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
  4236. if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
  4237. /*
  4238. * TODO: Cleanup of inserted chunk root in case of
  4239. * failure.
  4240. */
  4241. ret = btrfs_add_system_chunk(fs_info, &key, chunk, item_size);
  4242. }
  4243. out:
  4244. kfree(chunk);
  4245. free_extent_map(em);
  4246. return ret;
  4247. }
  4248. /*
  4249. * Chunk allocation falls into two parts. The first part does works
  4250. * that make the new allocated chunk useable, but not do any operation
  4251. * that modifies the chunk tree. The second part does the works that
  4252. * require modifying the chunk tree. This division is important for the
  4253. * bootstrap process of adding storage to a seed btrfs.
  4254. */
  4255. int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
  4256. struct btrfs_fs_info *fs_info, u64 type)
  4257. {
  4258. u64 chunk_offset;
  4259. ASSERT(mutex_is_locked(&fs_info->chunk_mutex));
  4260. chunk_offset = find_next_chunk(fs_info);
  4261. return __btrfs_alloc_chunk(trans, chunk_offset, type);
  4262. }
  4263. static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
  4264. struct btrfs_fs_info *fs_info)
  4265. {
  4266. u64 chunk_offset;
  4267. u64 sys_chunk_offset;
  4268. u64 alloc_profile;
  4269. int ret;
  4270. chunk_offset = find_next_chunk(fs_info);
  4271. alloc_profile = btrfs_metadata_alloc_profile(fs_info);
  4272. ret = __btrfs_alloc_chunk(trans, chunk_offset, alloc_profile);
  4273. if (ret)
  4274. return ret;
  4275. sys_chunk_offset = find_next_chunk(fs_info);
  4276. alloc_profile = btrfs_system_alloc_profile(fs_info);
  4277. ret = __btrfs_alloc_chunk(trans, sys_chunk_offset, alloc_profile);
  4278. return ret;
  4279. }
  4280. static inline int btrfs_chunk_max_errors(struct map_lookup *map)
  4281. {
  4282. int max_errors;
  4283. if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
  4284. BTRFS_BLOCK_GROUP_RAID10 |
  4285. BTRFS_BLOCK_GROUP_RAID5 |
  4286. BTRFS_BLOCK_GROUP_DUP)) {
  4287. max_errors = 1;
  4288. } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
  4289. max_errors = 2;
  4290. } else {
  4291. max_errors = 0;
  4292. }
  4293. return max_errors;
  4294. }
  4295. int btrfs_chunk_readonly(struct btrfs_fs_info *fs_info, u64 chunk_offset)
  4296. {
  4297. struct extent_map *em;
  4298. struct map_lookup *map;
  4299. int readonly = 0;
  4300. int miss_ndevs = 0;
  4301. int i;
  4302. em = get_chunk_map(fs_info, chunk_offset, 1);
  4303. if (IS_ERR(em))
  4304. return 1;
  4305. map = em->map_lookup;
  4306. for (i = 0; i < map->num_stripes; i++) {
  4307. if (map->stripes[i].dev->missing) {
  4308. miss_ndevs++;
  4309. continue;
  4310. }
  4311. if (!map->stripes[i].dev->writeable) {
  4312. readonly = 1;
  4313. goto end;
  4314. }
  4315. }
  4316. /*
  4317. * If the number of missing devices is larger than max errors,
  4318. * we can not write the data into that chunk successfully, so
  4319. * set it readonly.
  4320. */
  4321. if (miss_ndevs > btrfs_chunk_max_errors(map))
  4322. readonly = 1;
  4323. end:
  4324. free_extent_map(em);
  4325. return readonly;
  4326. }
  4327. void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
  4328. {
  4329. extent_map_tree_init(&tree->map_tree);
  4330. }
  4331. void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
  4332. {
  4333. struct extent_map *em;
  4334. while (1) {
  4335. write_lock(&tree->map_tree.lock);
  4336. em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
  4337. if (em)
  4338. remove_extent_mapping(&tree->map_tree, em);
  4339. write_unlock(&tree->map_tree.lock);
  4340. if (!em)
  4341. break;
  4342. /* once for us */
  4343. free_extent_map(em);
  4344. /* once for the tree */
  4345. free_extent_map(em);
  4346. }
  4347. }
  4348. int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
  4349. {
  4350. struct extent_map *em;
  4351. struct map_lookup *map;
  4352. int ret;
  4353. em = get_chunk_map(fs_info, logical, len);
  4354. if (IS_ERR(em))
  4355. /*
  4356. * We could return errors for these cases, but that could get
  4357. * ugly and we'd probably do the same thing which is just not do
  4358. * anything else and exit, so return 1 so the callers don't try
  4359. * to use other copies.
  4360. */
  4361. return 1;
  4362. map = em->map_lookup;
  4363. if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
  4364. ret = map->num_stripes;
  4365. else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
  4366. ret = map->sub_stripes;
  4367. else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
  4368. ret = 2;
  4369. else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
  4370. ret = 3;
  4371. else
  4372. ret = 1;
  4373. free_extent_map(em);
  4374. btrfs_dev_replace_lock(&fs_info->dev_replace, 0);
  4375. if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace) &&
  4376. fs_info->dev_replace.tgtdev)
  4377. ret++;
  4378. btrfs_dev_replace_unlock(&fs_info->dev_replace, 0);
  4379. return ret;
  4380. }
  4381. unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info,
  4382. u64 logical)
  4383. {
  4384. struct extent_map *em;
  4385. struct map_lookup *map;
  4386. unsigned long len = fs_info->sectorsize;
  4387. em = get_chunk_map(fs_info, logical, len);
  4388. if (!WARN_ON(IS_ERR(em))) {
  4389. map = em->map_lookup;
  4390. if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
  4391. len = map->stripe_len * nr_data_stripes(map);
  4392. free_extent_map(em);
  4393. }
  4394. return len;
  4395. }
  4396. int btrfs_is_parity_mirror(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
  4397. {
  4398. struct extent_map *em;
  4399. struct map_lookup *map;
  4400. int ret = 0;
  4401. em = get_chunk_map(fs_info, logical, len);
  4402. if(!WARN_ON(IS_ERR(em))) {
  4403. map = em->map_lookup;
  4404. if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
  4405. ret = 1;
  4406. free_extent_map(em);
  4407. }
  4408. return ret;
  4409. }
  4410. static int find_live_mirror(struct btrfs_fs_info *fs_info,
  4411. struct map_lookup *map, int first, int num,
  4412. int optimal, int dev_replace_is_ongoing)
  4413. {
  4414. int i;
  4415. int tolerance;
  4416. struct btrfs_device *srcdev;
  4417. if (dev_replace_is_ongoing &&
  4418. fs_info->dev_replace.cont_reading_from_srcdev_mode ==
  4419. BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
  4420. srcdev = fs_info->dev_replace.srcdev;
  4421. else
  4422. srcdev = NULL;
  4423. /*
  4424. * try to avoid the drive that is the source drive for a
  4425. * dev-replace procedure, only choose it if no other non-missing
  4426. * mirror is available
  4427. */
  4428. for (tolerance = 0; tolerance < 2; tolerance++) {
  4429. if (map->stripes[optimal].dev->bdev &&
  4430. (tolerance || map->stripes[optimal].dev != srcdev))
  4431. return optimal;
  4432. for (i = first; i < first + num; i++) {
  4433. if (map->stripes[i].dev->bdev &&
  4434. (tolerance || map->stripes[i].dev != srcdev))
  4435. return i;
  4436. }
  4437. }
  4438. /* we couldn't find one that doesn't fail. Just return something
  4439. * and the io error handling code will clean up eventually
  4440. */
  4441. return optimal;
  4442. }
  4443. static inline int parity_smaller(u64 a, u64 b)
  4444. {
  4445. return a > b;
  4446. }
  4447. /* Bubble-sort the stripe set to put the parity/syndrome stripes last */
  4448. static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes)
  4449. {
  4450. struct btrfs_bio_stripe s;
  4451. int i;
  4452. u64 l;
  4453. int again = 1;
  4454. while (again) {
  4455. again = 0;
  4456. for (i = 0; i < num_stripes - 1; i++) {
  4457. if (parity_smaller(bbio->raid_map[i],
  4458. bbio->raid_map[i+1])) {
  4459. s = bbio->stripes[i];
  4460. l = bbio->raid_map[i];
  4461. bbio->stripes[i] = bbio->stripes[i+1];
  4462. bbio->raid_map[i] = bbio->raid_map[i+1];
  4463. bbio->stripes[i+1] = s;
  4464. bbio->raid_map[i+1] = l;
  4465. again = 1;
  4466. }
  4467. }
  4468. }
  4469. }
  4470. static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes)
  4471. {
  4472. struct btrfs_bio *bbio = kzalloc(
  4473. /* the size of the btrfs_bio */
  4474. sizeof(struct btrfs_bio) +
  4475. /* plus the variable array for the stripes */
  4476. sizeof(struct btrfs_bio_stripe) * (total_stripes) +
  4477. /* plus the variable array for the tgt dev */
  4478. sizeof(int) * (real_stripes) +
  4479. /*
  4480. * plus the raid_map, which includes both the tgt dev
  4481. * and the stripes
  4482. */
  4483. sizeof(u64) * (total_stripes),
  4484. GFP_NOFS|__GFP_NOFAIL);
  4485. atomic_set(&bbio->error, 0);
  4486. refcount_set(&bbio->refs, 1);
  4487. return bbio;
  4488. }
  4489. void btrfs_get_bbio(struct btrfs_bio *bbio)
  4490. {
  4491. WARN_ON(!refcount_read(&bbio->refs));
  4492. refcount_inc(&bbio->refs);
  4493. }
  4494. void btrfs_put_bbio(struct btrfs_bio *bbio)
  4495. {
  4496. if (!bbio)
  4497. return;
  4498. if (refcount_dec_and_test(&bbio->refs))
  4499. kfree(bbio);
  4500. }
  4501. /* can REQ_OP_DISCARD be sent with other REQ like REQ_OP_WRITE? */
  4502. /*
  4503. * Please note that, discard won't be sent to target device of device
  4504. * replace.
  4505. */
  4506. static int __btrfs_map_block_for_discard(struct btrfs_fs_info *fs_info,
  4507. u64 logical, u64 length,
  4508. struct btrfs_bio **bbio_ret)
  4509. {
  4510. struct extent_map *em;
  4511. struct map_lookup *map;
  4512. struct btrfs_bio *bbio;
  4513. u64 offset;
  4514. u64 stripe_nr;
  4515. u64 stripe_nr_end;
  4516. u64 stripe_end_offset;
  4517. u64 stripe_cnt;
  4518. u64 stripe_len;
  4519. u64 stripe_offset;
  4520. u64 num_stripes;
  4521. u32 stripe_index;
  4522. u32 factor = 0;
  4523. u32 sub_stripes = 0;
  4524. u64 stripes_per_dev = 0;
  4525. u32 remaining_stripes = 0;
  4526. u32 last_stripe = 0;
  4527. int ret = 0;
  4528. int i;
  4529. /* discard always return a bbio */
  4530. ASSERT(bbio_ret);
  4531. em = get_chunk_map(fs_info, logical, length);
  4532. if (IS_ERR(em))
  4533. return PTR_ERR(em);
  4534. map = em->map_lookup;
  4535. /* we don't discard raid56 yet */
  4536. if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
  4537. ret = -EOPNOTSUPP;
  4538. goto out;
  4539. }
  4540. offset = logical - em->start;
  4541. length = min_t(u64, em->len - offset, length);
  4542. stripe_len = map->stripe_len;
  4543. /*
  4544. * stripe_nr counts the total number of stripes we have to stride
  4545. * to get to this block
  4546. */
  4547. stripe_nr = div64_u64(offset, stripe_len);
  4548. /* stripe_offset is the offset of this block in its stripe */
  4549. stripe_offset = offset - stripe_nr * stripe_len;
  4550. stripe_nr_end = round_up(offset + length, map->stripe_len);
  4551. stripe_nr_end = div64_u64(stripe_nr_end, map->stripe_len);
  4552. stripe_cnt = stripe_nr_end - stripe_nr;
  4553. stripe_end_offset = stripe_nr_end * map->stripe_len -
  4554. (offset + length);
  4555. /*
  4556. * after this, stripe_nr is the number of stripes on this
  4557. * device we have to walk to find the data, and stripe_index is
  4558. * the number of our device in the stripe array
  4559. */
  4560. num_stripes = 1;
  4561. stripe_index = 0;
  4562. if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
  4563. BTRFS_BLOCK_GROUP_RAID10)) {
  4564. if (map->type & BTRFS_BLOCK_GROUP_RAID0)
  4565. sub_stripes = 1;
  4566. else
  4567. sub_stripes = map->sub_stripes;
  4568. factor = map->num_stripes / sub_stripes;
  4569. num_stripes = min_t(u64, map->num_stripes,
  4570. sub_stripes * stripe_cnt);
  4571. stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
  4572. stripe_index *= sub_stripes;
  4573. stripes_per_dev = div_u64_rem(stripe_cnt, factor,
  4574. &remaining_stripes);
  4575. div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
  4576. last_stripe *= sub_stripes;
  4577. } else if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
  4578. BTRFS_BLOCK_GROUP_DUP)) {
  4579. num_stripes = map->num_stripes;
  4580. } else {
  4581. stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
  4582. &stripe_index);
  4583. }
  4584. bbio = alloc_btrfs_bio(num_stripes, 0);
  4585. if (!bbio) {
  4586. ret = -ENOMEM;
  4587. goto out;
  4588. }
  4589. for (i = 0; i < num_stripes; i++) {
  4590. bbio->stripes[i].physical =
  4591. map->stripes[stripe_index].physical +
  4592. stripe_offset + stripe_nr * map->stripe_len;
  4593. bbio->stripes[i].dev = map->stripes[stripe_index].dev;
  4594. if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
  4595. BTRFS_BLOCK_GROUP_RAID10)) {
  4596. bbio->stripes[i].length = stripes_per_dev *
  4597. map->stripe_len;
  4598. if (i / sub_stripes < remaining_stripes)
  4599. bbio->stripes[i].length +=
  4600. map->stripe_len;
  4601. /*
  4602. * Special for the first stripe and
  4603. * the last stripe:
  4604. *
  4605. * |-------|...|-------|
  4606. * |----------|
  4607. * off end_off
  4608. */
  4609. if (i < sub_stripes)
  4610. bbio->stripes[i].length -=
  4611. stripe_offset;
  4612. if (stripe_index >= last_stripe &&
  4613. stripe_index <= (last_stripe +
  4614. sub_stripes - 1))
  4615. bbio->stripes[i].length -=
  4616. stripe_end_offset;
  4617. if (i == sub_stripes - 1)
  4618. stripe_offset = 0;
  4619. } else {
  4620. bbio->stripes[i].length = length;
  4621. }
  4622. stripe_index++;
  4623. if (stripe_index == map->num_stripes) {
  4624. stripe_index = 0;
  4625. stripe_nr++;
  4626. }
  4627. }
  4628. *bbio_ret = bbio;
  4629. bbio->map_type = map->type;
  4630. bbio->num_stripes = num_stripes;
  4631. out:
  4632. free_extent_map(em);
  4633. return ret;
  4634. }
  4635. /*
  4636. * In dev-replace case, for repair case (that's the only case where the mirror
  4637. * is selected explicitly when calling btrfs_map_block), blocks left of the
  4638. * left cursor can also be read from the target drive.
  4639. *
  4640. * For REQ_GET_READ_MIRRORS, the target drive is added as the last one to the
  4641. * array of stripes.
  4642. * For READ, it also needs to be supported using the same mirror number.
  4643. *
  4644. * If the requested block is not left of the left cursor, EIO is returned. This
  4645. * can happen because btrfs_num_copies() returns one more in the dev-replace
  4646. * case.
  4647. */
  4648. static int get_extra_mirror_from_replace(struct btrfs_fs_info *fs_info,
  4649. u64 logical, u64 length,
  4650. u64 srcdev_devid, int *mirror_num,
  4651. u64 *physical)
  4652. {
  4653. struct btrfs_bio *bbio = NULL;
  4654. int num_stripes;
  4655. int index_srcdev = 0;
  4656. int found = 0;
  4657. u64 physical_of_found = 0;
  4658. int i;
  4659. int ret = 0;
  4660. ret = __btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS,
  4661. logical, &length, &bbio, 0, 0);
  4662. if (ret) {
  4663. ASSERT(bbio == NULL);
  4664. return ret;
  4665. }
  4666. num_stripes = bbio->num_stripes;
  4667. if (*mirror_num > num_stripes) {
  4668. /*
  4669. * BTRFS_MAP_GET_READ_MIRRORS does not contain this mirror,
  4670. * that means that the requested area is not left of the left
  4671. * cursor
  4672. */
  4673. btrfs_put_bbio(bbio);
  4674. return -EIO;
  4675. }
  4676. /*
  4677. * process the rest of the function using the mirror_num of the source
  4678. * drive. Therefore look it up first. At the end, patch the device
  4679. * pointer to the one of the target drive.
  4680. */
  4681. for (i = 0; i < num_stripes; i++) {
  4682. if (bbio->stripes[i].dev->devid != srcdev_devid)
  4683. continue;
  4684. /*
  4685. * In case of DUP, in order to keep it simple, only add the
  4686. * mirror with the lowest physical address
  4687. */
  4688. if (found &&
  4689. physical_of_found <= bbio->stripes[i].physical)
  4690. continue;
  4691. index_srcdev = i;
  4692. found = 1;
  4693. physical_of_found = bbio->stripes[i].physical;
  4694. }
  4695. btrfs_put_bbio(bbio);
  4696. ASSERT(found);
  4697. if (!found)
  4698. return -EIO;
  4699. *mirror_num = index_srcdev + 1;
  4700. *physical = physical_of_found;
  4701. return ret;
  4702. }
  4703. static void handle_ops_on_dev_replace(enum btrfs_map_op op,
  4704. struct btrfs_bio **bbio_ret,
  4705. struct btrfs_dev_replace *dev_replace,
  4706. int *num_stripes_ret, int *max_errors_ret)
  4707. {
  4708. struct btrfs_bio *bbio = *bbio_ret;
  4709. u64 srcdev_devid = dev_replace->srcdev->devid;
  4710. int tgtdev_indexes = 0;
  4711. int num_stripes = *num_stripes_ret;
  4712. int max_errors = *max_errors_ret;
  4713. int i;
  4714. if (op == BTRFS_MAP_WRITE) {
  4715. int index_where_to_add;
  4716. /*
  4717. * duplicate the write operations while the dev replace
  4718. * procedure is running. Since the copying of the old disk to
  4719. * the new disk takes place at run time while the filesystem is
  4720. * mounted writable, the regular write operations to the old
  4721. * disk have to be duplicated to go to the new disk as well.
  4722. *
  4723. * Note that device->missing is handled by the caller, and that
  4724. * the write to the old disk is already set up in the stripes
  4725. * array.
  4726. */
  4727. index_where_to_add = num_stripes;
  4728. for (i = 0; i < num_stripes; i++) {
  4729. if (bbio->stripes[i].dev->devid == srcdev_devid) {
  4730. /* write to new disk, too */
  4731. struct btrfs_bio_stripe *new =
  4732. bbio->stripes + index_where_to_add;
  4733. struct btrfs_bio_stripe *old =
  4734. bbio->stripes + i;
  4735. new->physical = old->physical;
  4736. new->length = old->length;
  4737. new->dev = dev_replace->tgtdev;
  4738. bbio->tgtdev_map[i] = index_where_to_add;
  4739. index_where_to_add++;
  4740. max_errors++;
  4741. tgtdev_indexes++;
  4742. }
  4743. }
  4744. num_stripes = index_where_to_add;
  4745. } else if (op == BTRFS_MAP_GET_READ_MIRRORS) {
  4746. int index_srcdev = 0;
  4747. int found = 0;
  4748. u64 physical_of_found = 0;
  4749. /*
  4750. * During the dev-replace procedure, the target drive can also
  4751. * be used to read data in case it is needed to repair a corrupt
  4752. * block elsewhere. This is possible if the requested area is
  4753. * left of the left cursor. In this area, the target drive is a
  4754. * full copy of the source drive.
  4755. */
  4756. for (i = 0; i < num_stripes; i++) {
  4757. if (bbio->stripes[i].dev->devid == srcdev_devid) {
  4758. /*
  4759. * In case of DUP, in order to keep it simple,
  4760. * only add the mirror with the lowest physical
  4761. * address
  4762. */
  4763. if (found &&
  4764. physical_of_found <=
  4765. bbio->stripes[i].physical)
  4766. continue;
  4767. index_srcdev = i;
  4768. found = 1;
  4769. physical_of_found = bbio->stripes[i].physical;
  4770. }
  4771. }
  4772. if (found) {
  4773. struct btrfs_bio_stripe *tgtdev_stripe =
  4774. bbio->stripes + num_stripes;
  4775. tgtdev_stripe->physical = physical_of_found;
  4776. tgtdev_stripe->length =
  4777. bbio->stripes[index_srcdev].length;
  4778. tgtdev_stripe->dev = dev_replace->tgtdev;
  4779. bbio->tgtdev_map[index_srcdev] = num_stripes;
  4780. tgtdev_indexes++;
  4781. num_stripes++;
  4782. }
  4783. }
  4784. *num_stripes_ret = num_stripes;
  4785. *max_errors_ret = max_errors;
  4786. bbio->num_tgtdevs = tgtdev_indexes;
  4787. *bbio_ret = bbio;
  4788. }
  4789. static bool need_full_stripe(enum btrfs_map_op op)
  4790. {
  4791. return (op == BTRFS_MAP_WRITE || op == BTRFS_MAP_GET_READ_MIRRORS);
  4792. }
  4793. static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
  4794. enum btrfs_map_op op,
  4795. u64 logical, u64 *length,
  4796. struct btrfs_bio **bbio_ret,
  4797. int mirror_num, int need_raid_map)
  4798. {
  4799. struct extent_map *em;
  4800. struct map_lookup *map;
  4801. u64 offset;
  4802. u64 stripe_offset;
  4803. u64 stripe_nr;
  4804. u64 stripe_len;
  4805. u32 stripe_index;
  4806. int i;
  4807. int ret = 0;
  4808. int num_stripes;
  4809. int max_errors = 0;
  4810. int tgtdev_indexes = 0;
  4811. struct btrfs_bio *bbio = NULL;
  4812. struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
  4813. int dev_replace_is_ongoing = 0;
  4814. int num_alloc_stripes;
  4815. int patch_the_first_stripe_for_dev_replace = 0;
  4816. u64 physical_to_patch_in_first_stripe = 0;
  4817. u64 raid56_full_stripe_start = (u64)-1;
  4818. if (op == BTRFS_MAP_DISCARD)
  4819. return __btrfs_map_block_for_discard(fs_info, logical,
  4820. *length, bbio_ret);
  4821. em = get_chunk_map(fs_info, logical, *length);
  4822. if (IS_ERR(em))
  4823. return PTR_ERR(em);
  4824. map = em->map_lookup;
  4825. offset = logical - em->start;
  4826. stripe_len = map->stripe_len;
  4827. stripe_nr = offset;
  4828. /*
  4829. * stripe_nr counts the total number of stripes we have to stride
  4830. * to get to this block
  4831. */
  4832. stripe_nr = div64_u64(stripe_nr, stripe_len);
  4833. stripe_offset = stripe_nr * stripe_len;
  4834. if (offset < stripe_offset) {
  4835. btrfs_crit(fs_info,
  4836. "stripe math has gone wrong, stripe_offset=%llu, offset=%llu, start=%llu, logical=%llu, stripe_len=%llu",
  4837. stripe_offset, offset, em->start, logical,
  4838. stripe_len);
  4839. free_extent_map(em);
  4840. return -EINVAL;
  4841. }
  4842. /* stripe_offset is the offset of this block in its stripe*/
  4843. stripe_offset = offset - stripe_offset;
  4844. /* if we're here for raid56, we need to know the stripe aligned start */
  4845. if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
  4846. unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
  4847. raid56_full_stripe_start = offset;
  4848. /* allow a write of a full stripe, but make sure we don't
  4849. * allow straddling of stripes
  4850. */
  4851. raid56_full_stripe_start = div64_u64(raid56_full_stripe_start,
  4852. full_stripe_len);
  4853. raid56_full_stripe_start *= full_stripe_len;
  4854. }
  4855. if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
  4856. u64 max_len;
  4857. /* For writes to RAID[56], allow a full stripeset across all disks.
  4858. For other RAID types and for RAID[56] reads, just allow a single
  4859. stripe (on a single disk). */
  4860. if ((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
  4861. (op == BTRFS_MAP_WRITE)) {
  4862. max_len = stripe_len * nr_data_stripes(map) -
  4863. (offset - raid56_full_stripe_start);
  4864. } else {
  4865. /* we limit the length of each bio to what fits in a stripe */
  4866. max_len = stripe_len - stripe_offset;
  4867. }
  4868. *length = min_t(u64, em->len - offset, max_len);
  4869. } else {
  4870. *length = em->len - offset;
  4871. }
  4872. /* This is for when we're called from btrfs_merge_bio_hook() and all
  4873. it cares about is the length */
  4874. if (!bbio_ret)
  4875. goto out;
  4876. btrfs_dev_replace_lock(dev_replace, 0);
  4877. dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
  4878. if (!dev_replace_is_ongoing)
  4879. btrfs_dev_replace_unlock(dev_replace, 0);
  4880. else
  4881. btrfs_dev_replace_set_lock_blocking(dev_replace);
  4882. if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
  4883. !need_full_stripe(op) && dev_replace->tgtdev != NULL) {
  4884. ret = get_extra_mirror_from_replace(fs_info, logical, *length,
  4885. dev_replace->srcdev->devid,
  4886. &mirror_num,
  4887. &physical_to_patch_in_first_stripe);
  4888. if (ret)
  4889. goto out;
  4890. else
  4891. patch_the_first_stripe_for_dev_replace = 1;
  4892. } else if (mirror_num > map->num_stripes) {
  4893. mirror_num = 0;
  4894. }
  4895. num_stripes = 1;
  4896. stripe_index = 0;
  4897. if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
  4898. stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
  4899. &stripe_index);
  4900. if (op != BTRFS_MAP_WRITE && op != BTRFS_MAP_GET_READ_MIRRORS)
  4901. mirror_num = 1;
  4902. } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
  4903. if (op == BTRFS_MAP_WRITE || op == BTRFS_MAP_GET_READ_MIRRORS)
  4904. num_stripes = map->num_stripes;
  4905. else if (mirror_num)
  4906. stripe_index = mirror_num - 1;
  4907. else {
  4908. stripe_index = find_live_mirror(fs_info, map, 0,
  4909. map->num_stripes,
  4910. current->pid % map->num_stripes,
  4911. dev_replace_is_ongoing);
  4912. mirror_num = stripe_index + 1;
  4913. }
  4914. } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
  4915. if (op == BTRFS_MAP_WRITE || op == BTRFS_MAP_GET_READ_MIRRORS) {
  4916. num_stripes = map->num_stripes;
  4917. } else if (mirror_num) {
  4918. stripe_index = mirror_num - 1;
  4919. } else {
  4920. mirror_num = 1;
  4921. }
  4922. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  4923. u32 factor = map->num_stripes / map->sub_stripes;
  4924. stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
  4925. stripe_index *= map->sub_stripes;
  4926. if (op == BTRFS_MAP_WRITE || op == BTRFS_MAP_GET_READ_MIRRORS)
  4927. num_stripes = map->sub_stripes;
  4928. else if (mirror_num)
  4929. stripe_index += mirror_num - 1;
  4930. else {
  4931. int old_stripe_index = stripe_index;
  4932. stripe_index = find_live_mirror(fs_info, map,
  4933. stripe_index,
  4934. map->sub_stripes, stripe_index +
  4935. current->pid % map->sub_stripes,
  4936. dev_replace_is_ongoing);
  4937. mirror_num = stripe_index - old_stripe_index + 1;
  4938. }
  4939. } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
  4940. if (need_raid_map &&
  4941. (op == BTRFS_MAP_WRITE || op == BTRFS_MAP_GET_READ_MIRRORS ||
  4942. mirror_num > 1)) {
  4943. /* push stripe_nr back to the start of the full stripe */
  4944. stripe_nr = div64_u64(raid56_full_stripe_start,
  4945. stripe_len * nr_data_stripes(map));
  4946. /* RAID[56] write or recovery. Return all stripes */
  4947. num_stripes = map->num_stripes;
  4948. max_errors = nr_parity_stripes(map);
  4949. *length = map->stripe_len;
  4950. stripe_index = 0;
  4951. stripe_offset = 0;
  4952. } else {
  4953. /*
  4954. * Mirror #0 or #1 means the original data block.
  4955. * Mirror #2 is RAID5 parity block.
  4956. * Mirror #3 is RAID6 Q block.
  4957. */
  4958. stripe_nr = div_u64_rem(stripe_nr,
  4959. nr_data_stripes(map), &stripe_index);
  4960. if (mirror_num > 1)
  4961. stripe_index = nr_data_stripes(map) +
  4962. mirror_num - 2;
  4963. /* We distribute the parity blocks across stripes */
  4964. div_u64_rem(stripe_nr + stripe_index, map->num_stripes,
  4965. &stripe_index);
  4966. if ((op != BTRFS_MAP_WRITE &&
  4967. op != BTRFS_MAP_GET_READ_MIRRORS) &&
  4968. mirror_num <= 1)
  4969. mirror_num = 1;
  4970. }
  4971. } else {
  4972. /*
  4973. * after this, stripe_nr is the number of stripes on this
  4974. * device we have to walk to find the data, and stripe_index is
  4975. * the number of our device in the stripe array
  4976. */
  4977. stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
  4978. &stripe_index);
  4979. mirror_num = stripe_index + 1;
  4980. }
  4981. if (stripe_index >= map->num_stripes) {
  4982. btrfs_crit(fs_info,
  4983. "stripe index math went horribly wrong, got stripe_index=%u, num_stripes=%u",
  4984. stripe_index, map->num_stripes);
  4985. ret = -EINVAL;
  4986. goto out;
  4987. }
  4988. num_alloc_stripes = num_stripes;
  4989. if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) {
  4990. if (op == BTRFS_MAP_WRITE)
  4991. num_alloc_stripes <<= 1;
  4992. if (op == BTRFS_MAP_GET_READ_MIRRORS)
  4993. num_alloc_stripes++;
  4994. tgtdev_indexes = num_stripes;
  4995. }
  4996. bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes);
  4997. if (!bbio) {
  4998. ret = -ENOMEM;
  4999. goto out;
  5000. }
  5001. if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL)
  5002. bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes);
  5003. /* build raid_map */
  5004. if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK && need_raid_map &&
  5005. (need_full_stripe(op) || mirror_num > 1)) {
  5006. u64 tmp;
  5007. unsigned rot;
  5008. bbio->raid_map = (u64 *)((void *)bbio->stripes +
  5009. sizeof(struct btrfs_bio_stripe) *
  5010. num_alloc_stripes +
  5011. sizeof(int) * tgtdev_indexes);
  5012. /* Work out the disk rotation on this stripe-set */
  5013. div_u64_rem(stripe_nr, num_stripes, &rot);
  5014. /* Fill in the logical address of each stripe */
  5015. tmp = stripe_nr * nr_data_stripes(map);
  5016. for (i = 0; i < nr_data_stripes(map); i++)
  5017. bbio->raid_map[(i+rot) % num_stripes] =
  5018. em->start + (tmp + i) * map->stripe_len;
  5019. bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
  5020. if (map->type & BTRFS_BLOCK_GROUP_RAID6)
  5021. bbio->raid_map[(i+rot+1) % num_stripes] =
  5022. RAID6_Q_STRIPE;
  5023. }
  5024. for (i = 0; i < num_stripes; i++) {
  5025. bbio->stripes[i].physical =
  5026. map->stripes[stripe_index].physical +
  5027. stripe_offset +
  5028. stripe_nr * map->stripe_len;
  5029. bbio->stripes[i].dev =
  5030. map->stripes[stripe_index].dev;
  5031. stripe_index++;
  5032. }
  5033. if (need_full_stripe(op))
  5034. max_errors = btrfs_chunk_max_errors(map);
  5035. if (bbio->raid_map)
  5036. sort_parity_stripes(bbio, num_stripes);
  5037. if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL &&
  5038. need_full_stripe(op)) {
  5039. handle_ops_on_dev_replace(op, &bbio, dev_replace, &num_stripes,
  5040. &max_errors);
  5041. }
  5042. *bbio_ret = bbio;
  5043. bbio->map_type = map->type;
  5044. bbio->num_stripes = num_stripes;
  5045. bbio->max_errors = max_errors;
  5046. bbio->mirror_num = mirror_num;
  5047. /*
  5048. * this is the case that REQ_READ && dev_replace_is_ongoing &&
  5049. * mirror_num == num_stripes + 1 && dev_replace target drive is
  5050. * available as a mirror
  5051. */
  5052. if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
  5053. WARN_ON(num_stripes > 1);
  5054. bbio->stripes[0].dev = dev_replace->tgtdev;
  5055. bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
  5056. bbio->mirror_num = map->num_stripes + 1;
  5057. }
  5058. out:
  5059. if (dev_replace_is_ongoing) {
  5060. btrfs_dev_replace_clear_lock_blocking(dev_replace);
  5061. btrfs_dev_replace_unlock(dev_replace, 0);
  5062. }
  5063. free_extent_map(em);
  5064. return ret;
  5065. }
  5066. int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
  5067. u64 logical, u64 *length,
  5068. struct btrfs_bio **bbio_ret, int mirror_num)
  5069. {
  5070. return __btrfs_map_block(fs_info, op, logical, length, bbio_ret,
  5071. mirror_num, 0);
  5072. }
  5073. /* For Scrub/replace */
  5074. int btrfs_map_sblock(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
  5075. u64 logical, u64 *length,
  5076. struct btrfs_bio **bbio_ret)
  5077. {
  5078. return __btrfs_map_block(fs_info, op, logical, length, bbio_ret, 0, 1);
  5079. }
  5080. int btrfs_rmap_block(struct btrfs_fs_info *fs_info,
  5081. u64 chunk_start, u64 physical, u64 devid,
  5082. u64 **logical, int *naddrs, int *stripe_len)
  5083. {
  5084. struct extent_map *em;
  5085. struct map_lookup *map;
  5086. u64 *buf;
  5087. u64 bytenr;
  5088. u64 length;
  5089. u64 stripe_nr;
  5090. u64 rmap_len;
  5091. int i, j, nr = 0;
  5092. em = get_chunk_map(fs_info, chunk_start, 1);
  5093. if (IS_ERR(em))
  5094. return -EIO;
  5095. map = em->map_lookup;
  5096. length = em->len;
  5097. rmap_len = map->stripe_len;
  5098. if (map->type & BTRFS_BLOCK_GROUP_RAID10)
  5099. length = div_u64(length, map->num_stripes / map->sub_stripes);
  5100. else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
  5101. length = div_u64(length, map->num_stripes);
  5102. else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
  5103. length = div_u64(length, nr_data_stripes(map));
  5104. rmap_len = map->stripe_len * nr_data_stripes(map);
  5105. }
  5106. buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS);
  5107. BUG_ON(!buf); /* -ENOMEM */
  5108. for (i = 0; i < map->num_stripes; i++) {
  5109. if (devid && map->stripes[i].dev->devid != devid)
  5110. continue;
  5111. if (map->stripes[i].physical > physical ||
  5112. map->stripes[i].physical + length <= physical)
  5113. continue;
  5114. stripe_nr = physical - map->stripes[i].physical;
  5115. stripe_nr = div64_u64(stripe_nr, map->stripe_len);
  5116. if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  5117. stripe_nr = stripe_nr * map->num_stripes + i;
  5118. stripe_nr = div_u64(stripe_nr, map->sub_stripes);
  5119. } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
  5120. stripe_nr = stripe_nr * map->num_stripes + i;
  5121. } /* else if RAID[56], multiply by nr_data_stripes().
  5122. * Alternatively, just use rmap_len below instead of
  5123. * map->stripe_len */
  5124. bytenr = chunk_start + stripe_nr * rmap_len;
  5125. WARN_ON(nr >= map->num_stripes);
  5126. for (j = 0; j < nr; j++) {
  5127. if (buf[j] == bytenr)
  5128. break;
  5129. }
  5130. if (j == nr) {
  5131. WARN_ON(nr >= map->num_stripes);
  5132. buf[nr++] = bytenr;
  5133. }
  5134. }
  5135. *logical = buf;
  5136. *naddrs = nr;
  5137. *stripe_len = rmap_len;
  5138. free_extent_map(em);
  5139. return 0;
  5140. }
  5141. static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio)
  5142. {
  5143. bio->bi_private = bbio->private;
  5144. bio->bi_end_io = bbio->end_io;
  5145. bio_endio(bio);
  5146. btrfs_put_bbio(bbio);
  5147. }
  5148. static void btrfs_end_bio(struct bio *bio)
  5149. {
  5150. struct btrfs_bio *bbio = bio->bi_private;
  5151. int is_orig_bio = 0;
  5152. if (bio->bi_status) {
  5153. atomic_inc(&bbio->error);
  5154. if (bio->bi_status == BLK_STS_IOERR ||
  5155. bio->bi_status == BLK_STS_TARGET) {
  5156. unsigned int stripe_index =
  5157. btrfs_io_bio(bio)->stripe_index;
  5158. struct btrfs_device *dev;
  5159. BUG_ON(stripe_index >= bbio->num_stripes);
  5160. dev = bbio->stripes[stripe_index].dev;
  5161. if (dev->bdev) {
  5162. if (bio_op(bio) == REQ_OP_WRITE)
  5163. btrfs_dev_stat_inc(dev,
  5164. BTRFS_DEV_STAT_WRITE_ERRS);
  5165. else
  5166. btrfs_dev_stat_inc(dev,
  5167. BTRFS_DEV_STAT_READ_ERRS);
  5168. if (bio->bi_opf & REQ_PREFLUSH)
  5169. btrfs_dev_stat_inc(dev,
  5170. BTRFS_DEV_STAT_FLUSH_ERRS);
  5171. btrfs_dev_stat_print_on_error(dev);
  5172. }
  5173. }
  5174. }
  5175. if (bio == bbio->orig_bio)
  5176. is_orig_bio = 1;
  5177. btrfs_bio_counter_dec(bbio->fs_info);
  5178. if (atomic_dec_and_test(&bbio->stripes_pending)) {
  5179. if (!is_orig_bio) {
  5180. bio_put(bio);
  5181. bio = bbio->orig_bio;
  5182. }
  5183. btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
  5184. /* only send an error to the higher layers if it is
  5185. * beyond the tolerance of the btrfs bio
  5186. */
  5187. if (atomic_read(&bbio->error) > bbio->max_errors) {
  5188. bio->bi_status = BLK_STS_IOERR;
  5189. } else {
  5190. /*
  5191. * this bio is actually up to date, we didn't
  5192. * go over the max number of errors
  5193. */
  5194. bio->bi_status = 0;
  5195. }
  5196. btrfs_end_bbio(bbio, bio);
  5197. } else if (!is_orig_bio) {
  5198. bio_put(bio);
  5199. }
  5200. }
  5201. /*
  5202. * see run_scheduled_bios for a description of why bios are collected for
  5203. * async submit.
  5204. *
  5205. * This will add one bio to the pending list for a device and make sure
  5206. * the work struct is scheduled.
  5207. */
  5208. static noinline void btrfs_schedule_bio(struct btrfs_device *device,
  5209. struct bio *bio)
  5210. {
  5211. struct btrfs_fs_info *fs_info = device->fs_info;
  5212. int should_queue = 1;
  5213. struct btrfs_pending_bios *pending_bios;
  5214. if (device->missing || !device->bdev) {
  5215. bio_io_error(bio);
  5216. return;
  5217. }
  5218. /* don't bother with additional async steps for reads, right now */
  5219. if (bio_op(bio) == REQ_OP_READ) {
  5220. bio_get(bio);
  5221. btrfsic_submit_bio(bio);
  5222. bio_put(bio);
  5223. return;
  5224. }
  5225. /*
  5226. * nr_async_bios allows us to reliably return congestion to the
  5227. * higher layers. Otherwise, the async bio makes it appear we have
  5228. * made progress against dirty pages when we've really just put it
  5229. * on a queue for later
  5230. */
  5231. atomic_inc(&fs_info->nr_async_bios);
  5232. WARN_ON(bio->bi_next);
  5233. bio->bi_next = NULL;
  5234. spin_lock(&device->io_lock);
  5235. if (op_is_sync(bio->bi_opf))
  5236. pending_bios = &device->pending_sync_bios;
  5237. else
  5238. pending_bios = &device->pending_bios;
  5239. if (pending_bios->tail)
  5240. pending_bios->tail->bi_next = bio;
  5241. pending_bios->tail = bio;
  5242. if (!pending_bios->head)
  5243. pending_bios->head = bio;
  5244. if (device->running_pending)
  5245. should_queue = 0;
  5246. spin_unlock(&device->io_lock);
  5247. if (should_queue)
  5248. btrfs_queue_work(fs_info->submit_workers, &device->work);
  5249. }
  5250. static void submit_stripe_bio(struct btrfs_bio *bbio, struct bio *bio,
  5251. u64 physical, int dev_nr, int async)
  5252. {
  5253. struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
  5254. struct btrfs_fs_info *fs_info = bbio->fs_info;
  5255. bio->bi_private = bbio;
  5256. btrfs_io_bio(bio)->stripe_index = dev_nr;
  5257. bio->bi_end_io = btrfs_end_bio;
  5258. bio->bi_iter.bi_sector = physical >> 9;
  5259. #ifdef DEBUG
  5260. {
  5261. struct rcu_string *name;
  5262. rcu_read_lock();
  5263. name = rcu_dereference(dev->name);
  5264. btrfs_debug(fs_info,
  5265. "btrfs_map_bio: rw %d 0x%x, sector=%llu, dev=%lu (%s id %llu), size=%u",
  5266. bio_op(bio), bio->bi_opf,
  5267. (u64)bio->bi_iter.bi_sector,
  5268. (u_long)dev->bdev->bd_dev, name->str, dev->devid,
  5269. bio->bi_iter.bi_size);
  5270. rcu_read_unlock();
  5271. }
  5272. #endif
  5273. bio_set_dev(bio, dev->bdev);
  5274. btrfs_bio_counter_inc_noblocked(fs_info);
  5275. if (async)
  5276. btrfs_schedule_bio(dev, bio);
  5277. else
  5278. btrfsic_submit_bio(bio);
  5279. }
  5280. static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
  5281. {
  5282. atomic_inc(&bbio->error);
  5283. if (atomic_dec_and_test(&bbio->stripes_pending)) {
  5284. /* Should be the original bio. */
  5285. WARN_ON(bio != bbio->orig_bio);
  5286. btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
  5287. bio->bi_iter.bi_sector = logical >> 9;
  5288. bio->bi_status = BLK_STS_IOERR;
  5289. btrfs_end_bbio(bbio, bio);
  5290. }
  5291. }
  5292. blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio,
  5293. int mirror_num, int async_submit)
  5294. {
  5295. struct btrfs_device *dev;
  5296. struct bio *first_bio = bio;
  5297. u64 logical = (u64)bio->bi_iter.bi_sector << 9;
  5298. u64 length = 0;
  5299. u64 map_length;
  5300. int ret;
  5301. int dev_nr;
  5302. int total_devs;
  5303. struct btrfs_bio *bbio = NULL;
  5304. length = bio->bi_iter.bi_size;
  5305. map_length = length;
  5306. btrfs_bio_counter_inc_blocked(fs_info);
  5307. ret = __btrfs_map_block(fs_info, bio_op(bio), logical,
  5308. &map_length, &bbio, mirror_num, 1);
  5309. if (ret) {
  5310. btrfs_bio_counter_dec(fs_info);
  5311. return errno_to_blk_status(ret);
  5312. }
  5313. total_devs = bbio->num_stripes;
  5314. bbio->orig_bio = first_bio;
  5315. bbio->private = first_bio->bi_private;
  5316. bbio->end_io = first_bio->bi_end_io;
  5317. bbio->fs_info = fs_info;
  5318. atomic_set(&bbio->stripes_pending, bbio->num_stripes);
  5319. if ((bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
  5320. ((bio_op(bio) == REQ_OP_WRITE) || (mirror_num > 1))) {
  5321. /* In this case, map_length has been set to the length of
  5322. a single stripe; not the whole write */
  5323. if (bio_op(bio) == REQ_OP_WRITE) {
  5324. ret = raid56_parity_write(fs_info, bio, bbio,
  5325. map_length);
  5326. } else {
  5327. ret = raid56_parity_recover(fs_info, bio, bbio,
  5328. map_length, mirror_num, 1);
  5329. }
  5330. btrfs_bio_counter_dec(fs_info);
  5331. return errno_to_blk_status(ret);
  5332. }
  5333. if (map_length < length) {
  5334. btrfs_crit(fs_info,
  5335. "mapping failed logical %llu bio len %llu len %llu",
  5336. logical, length, map_length);
  5337. BUG();
  5338. }
  5339. for (dev_nr = 0; dev_nr < total_devs; dev_nr++) {
  5340. dev = bbio->stripes[dev_nr].dev;
  5341. if (!dev || !dev->bdev ||
  5342. (bio_op(first_bio) == REQ_OP_WRITE && !dev->writeable)) {
  5343. bbio_error(bbio, first_bio, logical);
  5344. continue;
  5345. }
  5346. if (dev_nr < total_devs - 1)
  5347. bio = btrfs_bio_clone(first_bio);
  5348. else
  5349. bio = first_bio;
  5350. submit_stripe_bio(bbio, bio, bbio->stripes[dev_nr].physical,
  5351. dev_nr, async_submit);
  5352. }
  5353. btrfs_bio_counter_dec(fs_info);
  5354. return BLK_STS_OK;
  5355. }
  5356. struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
  5357. u8 *uuid, u8 *fsid)
  5358. {
  5359. struct btrfs_device *device;
  5360. struct btrfs_fs_devices *cur_devices;
  5361. cur_devices = fs_info->fs_devices;
  5362. while (cur_devices) {
  5363. if (!fsid ||
  5364. !memcmp(cur_devices->fsid, fsid, BTRFS_FSID_SIZE)) {
  5365. device = find_device(cur_devices, devid, uuid);
  5366. if (device)
  5367. return device;
  5368. }
  5369. cur_devices = cur_devices->seed;
  5370. }
  5371. return NULL;
  5372. }
  5373. static struct btrfs_device *add_missing_dev(struct btrfs_fs_devices *fs_devices,
  5374. u64 devid, u8 *dev_uuid)
  5375. {
  5376. struct btrfs_device *device;
  5377. device = btrfs_alloc_device(NULL, &devid, dev_uuid);
  5378. if (IS_ERR(device))
  5379. return NULL;
  5380. list_add(&device->dev_list, &fs_devices->devices);
  5381. device->fs_devices = fs_devices;
  5382. fs_devices->num_devices++;
  5383. device->missing = 1;
  5384. fs_devices->missing_devices++;
  5385. return device;
  5386. }
  5387. /**
  5388. * btrfs_alloc_device - allocate struct btrfs_device
  5389. * @fs_info: used only for generating a new devid, can be NULL if
  5390. * devid is provided (i.e. @devid != NULL).
  5391. * @devid: a pointer to devid for this device. If NULL a new devid
  5392. * is generated.
  5393. * @uuid: a pointer to UUID for this device. If NULL a new UUID
  5394. * is generated.
  5395. *
  5396. * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
  5397. * on error. Returned struct is not linked onto any lists and can be
  5398. * destroyed with kfree() right away.
  5399. */
  5400. struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
  5401. const u64 *devid,
  5402. const u8 *uuid)
  5403. {
  5404. struct btrfs_device *dev;
  5405. u64 tmp;
  5406. if (WARN_ON(!devid && !fs_info))
  5407. return ERR_PTR(-EINVAL);
  5408. dev = __alloc_device();
  5409. if (IS_ERR(dev))
  5410. return dev;
  5411. if (devid)
  5412. tmp = *devid;
  5413. else {
  5414. int ret;
  5415. ret = find_next_devid(fs_info, &tmp);
  5416. if (ret) {
  5417. kfree(dev);
  5418. return ERR_PTR(ret);
  5419. }
  5420. }
  5421. dev->devid = tmp;
  5422. if (uuid)
  5423. memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
  5424. else
  5425. generate_random_uuid(dev->uuid);
  5426. btrfs_init_work(&dev->work, btrfs_submit_helper,
  5427. pending_bios_fn, NULL, NULL);
  5428. return dev;
  5429. }
  5430. /* Return -EIO if any error, otherwise return 0. */
  5431. static int btrfs_check_chunk_valid(struct btrfs_fs_info *fs_info,
  5432. struct extent_buffer *leaf,
  5433. struct btrfs_chunk *chunk, u64 logical)
  5434. {
  5435. u64 length;
  5436. u64 stripe_len;
  5437. u16 num_stripes;
  5438. u16 sub_stripes;
  5439. u64 type;
  5440. length = btrfs_chunk_length(leaf, chunk);
  5441. stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
  5442. num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
  5443. sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
  5444. type = btrfs_chunk_type(leaf, chunk);
  5445. if (!num_stripes) {
  5446. btrfs_err(fs_info, "invalid chunk num_stripes: %u",
  5447. num_stripes);
  5448. return -EIO;
  5449. }
  5450. if (!IS_ALIGNED(logical, fs_info->sectorsize)) {
  5451. btrfs_err(fs_info, "invalid chunk logical %llu", logical);
  5452. return -EIO;
  5453. }
  5454. if (btrfs_chunk_sector_size(leaf, chunk) != fs_info->sectorsize) {
  5455. btrfs_err(fs_info, "invalid chunk sectorsize %u",
  5456. btrfs_chunk_sector_size(leaf, chunk));
  5457. return -EIO;
  5458. }
  5459. if (!length || !IS_ALIGNED(length, fs_info->sectorsize)) {
  5460. btrfs_err(fs_info, "invalid chunk length %llu", length);
  5461. return -EIO;
  5462. }
  5463. if (!is_power_of_2(stripe_len) || stripe_len != BTRFS_STRIPE_LEN) {
  5464. btrfs_err(fs_info, "invalid chunk stripe length: %llu",
  5465. stripe_len);
  5466. return -EIO;
  5467. }
  5468. if (~(BTRFS_BLOCK_GROUP_TYPE_MASK | BTRFS_BLOCK_GROUP_PROFILE_MASK) &
  5469. type) {
  5470. btrfs_err(fs_info, "unrecognized chunk type: %llu",
  5471. ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
  5472. BTRFS_BLOCK_GROUP_PROFILE_MASK) &
  5473. btrfs_chunk_type(leaf, chunk));
  5474. return -EIO;
  5475. }
  5476. if ((type & BTRFS_BLOCK_GROUP_RAID10 && sub_stripes != 2) ||
  5477. (type & BTRFS_BLOCK_GROUP_RAID1 && num_stripes < 1) ||
  5478. (type & BTRFS_BLOCK_GROUP_RAID5 && num_stripes < 2) ||
  5479. (type & BTRFS_BLOCK_GROUP_RAID6 && num_stripes < 3) ||
  5480. (type & BTRFS_BLOCK_GROUP_DUP && num_stripes > 2) ||
  5481. ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 &&
  5482. num_stripes != 1)) {
  5483. btrfs_err(fs_info,
  5484. "invalid num_stripes:sub_stripes %u:%u for profile %llu",
  5485. num_stripes, sub_stripes,
  5486. type & BTRFS_BLOCK_GROUP_PROFILE_MASK);
  5487. return -EIO;
  5488. }
  5489. return 0;
  5490. }
  5491. static int read_one_chunk(struct btrfs_fs_info *fs_info, struct btrfs_key *key,
  5492. struct extent_buffer *leaf,
  5493. struct btrfs_chunk *chunk)
  5494. {
  5495. struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
  5496. struct map_lookup *map;
  5497. struct extent_map *em;
  5498. u64 logical;
  5499. u64 length;
  5500. u64 devid;
  5501. u8 uuid[BTRFS_UUID_SIZE];
  5502. int num_stripes;
  5503. int ret;
  5504. int i;
  5505. logical = key->offset;
  5506. length = btrfs_chunk_length(leaf, chunk);
  5507. num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
  5508. ret = btrfs_check_chunk_valid(fs_info, leaf, chunk, logical);
  5509. if (ret)
  5510. return ret;
  5511. read_lock(&map_tree->map_tree.lock);
  5512. em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
  5513. read_unlock(&map_tree->map_tree.lock);
  5514. /* already mapped? */
  5515. if (em && em->start <= logical && em->start + em->len > logical) {
  5516. free_extent_map(em);
  5517. return 0;
  5518. } else if (em) {
  5519. free_extent_map(em);
  5520. }
  5521. em = alloc_extent_map();
  5522. if (!em)
  5523. return -ENOMEM;
  5524. map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
  5525. if (!map) {
  5526. free_extent_map(em);
  5527. return -ENOMEM;
  5528. }
  5529. set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
  5530. em->map_lookup = map;
  5531. em->start = logical;
  5532. em->len = length;
  5533. em->orig_start = 0;
  5534. em->block_start = 0;
  5535. em->block_len = em->len;
  5536. map->num_stripes = num_stripes;
  5537. map->io_width = btrfs_chunk_io_width(leaf, chunk);
  5538. map->io_align = btrfs_chunk_io_align(leaf, chunk);
  5539. map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
  5540. map->type = btrfs_chunk_type(leaf, chunk);
  5541. map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
  5542. for (i = 0; i < num_stripes; i++) {
  5543. map->stripes[i].physical =
  5544. btrfs_stripe_offset_nr(leaf, chunk, i);
  5545. devid = btrfs_stripe_devid_nr(leaf, chunk, i);
  5546. read_extent_buffer(leaf, uuid, (unsigned long)
  5547. btrfs_stripe_dev_uuid_nr(chunk, i),
  5548. BTRFS_UUID_SIZE);
  5549. map->stripes[i].dev = btrfs_find_device(fs_info, devid,
  5550. uuid, NULL);
  5551. if (!map->stripes[i].dev &&
  5552. !btrfs_test_opt(fs_info, DEGRADED)) {
  5553. free_extent_map(em);
  5554. btrfs_report_missing_device(fs_info, devid, uuid);
  5555. return -EIO;
  5556. }
  5557. if (!map->stripes[i].dev) {
  5558. map->stripes[i].dev =
  5559. add_missing_dev(fs_info->fs_devices, devid,
  5560. uuid);
  5561. if (!map->stripes[i].dev) {
  5562. free_extent_map(em);
  5563. return -EIO;
  5564. }
  5565. btrfs_report_missing_device(fs_info, devid, uuid);
  5566. }
  5567. map->stripes[i].dev->in_fs_metadata = 1;
  5568. }
  5569. write_lock(&map_tree->map_tree.lock);
  5570. ret = add_extent_mapping(&map_tree->map_tree, em, 0);
  5571. write_unlock(&map_tree->map_tree.lock);
  5572. BUG_ON(ret); /* Tree corruption */
  5573. free_extent_map(em);
  5574. return 0;
  5575. }
  5576. static void fill_device_from_item(struct extent_buffer *leaf,
  5577. struct btrfs_dev_item *dev_item,
  5578. struct btrfs_device *device)
  5579. {
  5580. unsigned long ptr;
  5581. device->devid = btrfs_device_id(leaf, dev_item);
  5582. device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
  5583. device->total_bytes = device->disk_total_bytes;
  5584. device->commit_total_bytes = device->disk_total_bytes;
  5585. device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
  5586. device->commit_bytes_used = device->bytes_used;
  5587. device->type = btrfs_device_type(leaf, dev_item);
  5588. device->io_align = btrfs_device_io_align(leaf, dev_item);
  5589. device->io_width = btrfs_device_io_width(leaf, dev_item);
  5590. device->sector_size = btrfs_device_sector_size(leaf, dev_item);
  5591. WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
  5592. device->is_tgtdev_for_dev_replace = 0;
  5593. ptr = btrfs_device_uuid(dev_item);
  5594. read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
  5595. }
  5596. static struct btrfs_fs_devices *open_seed_devices(struct btrfs_fs_info *fs_info,
  5597. u8 *fsid)
  5598. {
  5599. struct btrfs_fs_devices *fs_devices;
  5600. int ret;
  5601. BUG_ON(!mutex_is_locked(&uuid_mutex));
  5602. ASSERT(fsid);
  5603. fs_devices = fs_info->fs_devices->seed;
  5604. while (fs_devices) {
  5605. if (!memcmp(fs_devices->fsid, fsid, BTRFS_FSID_SIZE))
  5606. return fs_devices;
  5607. fs_devices = fs_devices->seed;
  5608. }
  5609. fs_devices = find_fsid(fsid);
  5610. if (!fs_devices) {
  5611. if (!btrfs_test_opt(fs_info, DEGRADED))
  5612. return ERR_PTR(-ENOENT);
  5613. fs_devices = alloc_fs_devices(fsid);
  5614. if (IS_ERR(fs_devices))
  5615. return fs_devices;
  5616. fs_devices->seeding = 1;
  5617. fs_devices->opened = 1;
  5618. return fs_devices;
  5619. }
  5620. fs_devices = clone_fs_devices(fs_devices);
  5621. if (IS_ERR(fs_devices))
  5622. return fs_devices;
  5623. ret = __btrfs_open_devices(fs_devices, FMODE_READ,
  5624. fs_info->bdev_holder);
  5625. if (ret) {
  5626. free_fs_devices(fs_devices);
  5627. fs_devices = ERR_PTR(ret);
  5628. goto out;
  5629. }
  5630. if (!fs_devices->seeding) {
  5631. __btrfs_close_devices(fs_devices);
  5632. free_fs_devices(fs_devices);
  5633. fs_devices = ERR_PTR(-EINVAL);
  5634. goto out;
  5635. }
  5636. fs_devices->seed = fs_info->fs_devices->seed;
  5637. fs_info->fs_devices->seed = fs_devices;
  5638. out:
  5639. return fs_devices;
  5640. }
  5641. static int read_one_dev(struct btrfs_fs_info *fs_info,
  5642. struct extent_buffer *leaf,
  5643. struct btrfs_dev_item *dev_item)
  5644. {
  5645. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  5646. struct btrfs_device *device;
  5647. u64 devid;
  5648. int ret;
  5649. u8 fs_uuid[BTRFS_FSID_SIZE];
  5650. u8 dev_uuid[BTRFS_UUID_SIZE];
  5651. devid = btrfs_device_id(leaf, dev_item);
  5652. read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
  5653. BTRFS_UUID_SIZE);
  5654. read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
  5655. BTRFS_FSID_SIZE);
  5656. if (memcmp(fs_uuid, fs_info->fsid, BTRFS_FSID_SIZE)) {
  5657. fs_devices = open_seed_devices(fs_info, fs_uuid);
  5658. if (IS_ERR(fs_devices))
  5659. return PTR_ERR(fs_devices);
  5660. }
  5661. device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid);
  5662. if (!device) {
  5663. if (!btrfs_test_opt(fs_info, DEGRADED)) {
  5664. btrfs_report_missing_device(fs_info, devid, dev_uuid);
  5665. return -EIO;
  5666. }
  5667. device = add_missing_dev(fs_devices, devid, dev_uuid);
  5668. if (!device)
  5669. return -ENOMEM;
  5670. btrfs_report_missing_device(fs_info, devid, dev_uuid);
  5671. } else {
  5672. if (!device->bdev) {
  5673. btrfs_report_missing_device(fs_info, devid, dev_uuid);
  5674. if (!btrfs_test_opt(fs_info, DEGRADED))
  5675. return -EIO;
  5676. }
  5677. if(!device->bdev && !device->missing) {
  5678. /*
  5679. * this happens when a device that was properly setup
  5680. * in the device info lists suddenly goes bad.
  5681. * device->bdev is NULL, and so we have to set
  5682. * device->missing to one here
  5683. */
  5684. device->fs_devices->missing_devices++;
  5685. device->missing = 1;
  5686. }
  5687. /* Move the device to its own fs_devices */
  5688. if (device->fs_devices != fs_devices) {
  5689. ASSERT(device->missing);
  5690. list_move(&device->dev_list, &fs_devices->devices);
  5691. device->fs_devices->num_devices--;
  5692. fs_devices->num_devices++;
  5693. device->fs_devices->missing_devices--;
  5694. fs_devices->missing_devices++;
  5695. device->fs_devices = fs_devices;
  5696. }
  5697. }
  5698. if (device->fs_devices != fs_info->fs_devices) {
  5699. BUG_ON(device->writeable);
  5700. if (device->generation !=
  5701. btrfs_device_generation(leaf, dev_item))
  5702. return -EINVAL;
  5703. }
  5704. fill_device_from_item(leaf, dev_item, device);
  5705. device->in_fs_metadata = 1;
  5706. if (device->writeable && !device->is_tgtdev_for_dev_replace) {
  5707. device->fs_devices->total_rw_bytes += device->total_bytes;
  5708. atomic64_add(device->total_bytes - device->bytes_used,
  5709. &fs_info->free_chunk_space);
  5710. }
  5711. ret = 0;
  5712. return ret;
  5713. }
  5714. int btrfs_read_sys_array(struct btrfs_fs_info *fs_info)
  5715. {
  5716. struct btrfs_root *root = fs_info->tree_root;
  5717. struct btrfs_super_block *super_copy = fs_info->super_copy;
  5718. struct extent_buffer *sb;
  5719. struct btrfs_disk_key *disk_key;
  5720. struct btrfs_chunk *chunk;
  5721. u8 *array_ptr;
  5722. unsigned long sb_array_offset;
  5723. int ret = 0;
  5724. u32 num_stripes;
  5725. u32 array_size;
  5726. u32 len = 0;
  5727. u32 cur_offset;
  5728. u64 type;
  5729. struct btrfs_key key;
  5730. ASSERT(BTRFS_SUPER_INFO_SIZE <= fs_info->nodesize);
  5731. /*
  5732. * This will create extent buffer of nodesize, superblock size is
  5733. * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
  5734. * overallocate but we can keep it as-is, only the first page is used.
  5735. */
  5736. sb = btrfs_find_create_tree_block(fs_info, BTRFS_SUPER_INFO_OFFSET);
  5737. if (IS_ERR(sb))
  5738. return PTR_ERR(sb);
  5739. set_extent_buffer_uptodate(sb);
  5740. btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
  5741. /*
  5742. * The sb extent buffer is artificial and just used to read the system array.
  5743. * set_extent_buffer_uptodate() call does not properly mark all it's
  5744. * pages up-to-date when the page is larger: extent does not cover the
  5745. * whole page and consequently check_page_uptodate does not find all
  5746. * the page's extents up-to-date (the hole beyond sb),
  5747. * write_extent_buffer then triggers a WARN_ON.
  5748. *
  5749. * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
  5750. * but sb spans only this function. Add an explicit SetPageUptodate call
  5751. * to silence the warning eg. on PowerPC 64.
  5752. */
  5753. if (PAGE_SIZE > BTRFS_SUPER_INFO_SIZE)
  5754. SetPageUptodate(sb->pages[0]);
  5755. write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
  5756. array_size = btrfs_super_sys_array_size(super_copy);
  5757. array_ptr = super_copy->sys_chunk_array;
  5758. sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
  5759. cur_offset = 0;
  5760. while (cur_offset < array_size) {
  5761. disk_key = (struct btrfs_disk_key *)array_ptr;
  5762. len = sizeof(*disk_key);
  5763. if (cur_offset + len > array_size)
  5764. goto out_short_read;
  5765. btrfs_disk_key_to_cpu(&key, disk_key);
  5766. array_ptr += len;
  5767. sb_array_offset += len;
  5768. cur_offset += len;
  5769. if (key.type == BTRFS_CHUNK_ITEM_KEY) {
  5770. chunk = (struct btrfs_chunk *)sb_array_offset;
  5771. /*
  5772. * At least one btrfs_chunk with one stripe must be
  5773. * present, exact stripe count check comes afterwards
  5774. */
  5775. len = btrfs_chunk_item_size(1);
  5776. if (cur_offset + len > array_size)
  5777. goto out_short_read;
  5778. num_stripes = btrfs_chunk_num_stripes(sb, chunk);
  5779. if (!num_stripes) {
  5780. btrfs_err(fs_info,
  5781. "invalid number of stripes %u in sys_array at offset %u",
  5782. num_stripes, cur_offset);
  5783. ret = -EIO;
  5784. break;
  5785. }
  5786. type = btrfs_chunk_type(sb, chunk);
  5787. if ((type & BTRFS_BLOCK_GROUP_SYSTEM) == 0) {
  5788. btrfs_err(fs_info,
  5789. "invalid chunk type %llu in sys_array at offset %u",
  5790. type, cur_offset);
  5791. ret = -EIO;
  5792. break;
  5793. }
  5794. len = btrfs_chunk_item_size(num_stripes);
  5795. if (cur_offset + len > array_size)
  5796. goto out_short_read;
  5797. ret = read_one_chunk(fs_info, &key, sb, chunk);
  5798. if (ret)
  5799. break;
  5800. } else {
  5801. btrfs_err(fs_info,
  5802. "unexpected item type %u in sys_array at offset %u",
  5803. (u32)key.type, cur_offset);
  5804. ret = -EIO;
  5805. break;
  5806. }
  5807. array_ptr += len;
  5808. sb_array_offset += len;
  5809. cur_offset += len;
  5810. }
  5811. clear_extent_buffer_uptodate(sb);
  5812. free_extent_buffer_stale(sb);
  5813. return ret;
  5814. out_short_read:
  5815. btrfs_err(fs_info, "sys_array too short to read %u bytes at offset %u",
  5816. len, cur_offset);
  5817. clear_extent_buffer_uptodate(sb);
  5818. free_extent_buffer_stale(sb);
  5819. return -EIO;
  5820. }
  5821. void btrfs_report_missing_device(struct btrfs_fs_info *fs_info, u64 devid,
  5822. u8 *uuid)
  5823. {
  5824. btrfs_warn_rl(fs_info, "devid %llu uuid %pU is missing", devid, uuid);
  5825. }
  5826. /*
  5827. * Check if all chunks in the fs are OK for read-write degraded mount
  5828. *
  5829. * Return true if all chunks meet the minimal RW mount requirements.
  5830. * Return false if any chunk doesn't meet the minimal RW mount requirements.
  5831. */
  5832. bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info)
  5833. {
  5834. struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
  5835. struct extent_map *em;
  5836. u64 next_start = 0;
  5837. bool ret = true;
  5838. read_lock(&map_tree->map_tree.lock);
  5839. em = lookup_extent_mapping(&map_tree->map_tree, 0, (u64)-1);
  5840. read_unlock(&map_tree->map_tree.lock);
  5841. /* No chunk at all? Return false anyway */
  5842. if (!em) {
  5843. ret = false;
  5844. goto out;
  5845. }
  5846. while (em) {
  5847. struct map_lookup *map;
  5848. int missing = 0;
  5849. int max_tolerated;
  5850. int i;
  5851. map = em->map_lookup;
  5852. max_tolerated =
  5853. btrfs_get_num_tolerated_disk_barrier_failures(
  5854. map->type);
  5855. for (i = 0; i < map->num_stripes; i++) {
  5856. struct btrfs_device *dev = map->stripes[i].dev;
  5857. if (!dev || !dev->bdev || dev->missing ||
  5858. dev->last_flush_error)
  5859. missing++;
  5860. }
  5861. if (missing > max_tolerated) {
  5862. btrfs_warn(fs_info,
  5863. "chunk %llu missing %d devices, max tolerance is %d for writeable mount",
  5864. em->start, missing, max_tolerated);
  5865. free_extent_map(em);
  5866. ret = false;
  5867. goto out;
  5868. }
  5869. next_start = extent_map_end(em);
  5870. free_extent_map(em);
  5871. read_lock(&map_tree->map_tree.lock);
  5872. em = lookup_extent_mapping(&map_tree->map_tree, next_start,
  5873. (u64)(-1) - next_start);
  5874. read_unlock(&map_tree->map_tree.lock);
  5875. }
  5876. out:
  5877. return ret;
  5878. }
  5879. int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info)
  5880. {
  5881. struct btrfs_root *root = fs_info->chunk_root;
  5882. struct btrfs_path *path;
  5883. struct extent_buffer *leaf;
  5884. struct btrfs_key key;
  5885. struct btrfs_key found_key;
  5886. int ret;
  5887. int slot;
  5888. u64 total_dev = 0;
  5889. path = btrfs_alloc_path();
  5890. if (!path)
  5891. return -ENOMEM;
  5892. mutex_lock(&uuid_mutex);
  5893. mutex_lock(&fs_info->chunk_mutex);
  5894. /*
  5895. * Read all device items, and then all the chunk items. All
  5896. * device items are found before any chunk item (their object id
  5897. * is smaller than the lowest possible object id for a chunk
  5898. * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
  5899. */
  5900. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  5901. key.offset = 0;
  5902. key.type = 0;
  5903. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  5904. if (ret < 0)
  5905. goto error;
  5906. while (1) {
  5907. leaf = path->nodes[0];
  5908. slot = path->slots[0];
  5909. if (slot >= btrfs_header_nritems(leaf)) {
  5910. ret = btrfs_next_leaf(root, path);
  5911. if (ret == 0)
  5912. continue;
  5913. if (ret < 0)
  5914. goto error;
  5915. break;
  5916. }
  5917. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  5918. if (found_key.type == BTRFS_DEV_ITEM_KEY) {
  5919. struct btrfs_dev_item *dev_item;
  5920. dev_item = btrfs_item_ptr(leaf, slot,
  5921. struct btrfs_dev_item);
  5922. ret = read_one_dev(fs_info, leaf, dev_item);
  5923. if (ret)
  5924. goto error;
  5925. total_dev++;
  5926. } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
  5927. struct btrfs_chunk *chunk;
  5928. chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
  5929. ret = read_one_chunk(fs_info, &found_key, leaf, chunk);
  5930. if (ret)
  5931. goto error;
  5932. }
  5933. path->slots[0]++;
  5934. }
  5935. /*
  5936. * After loading chunk tree, we've got all device information,
  5937. * do another round of validation checks.
  5938. */
  5939. if (total_dev != fs_info->fs_devices->total_devices) {
  5940. btrfs_err(fs_info,
  5941. "super_num_devices %llu mismatch with num_devices %llu found here",
  5942. btrfs_super_num_devices(fs_info->super_copy),
  5943. total_dev);
  5944. ret = -EINVAL;
  5945. goto error;
  5946. }
  5947. if (btrfs_super_total_bytes(fs_info->super_copy) <
  5948. fs_info->fs_devices->total_rw_bytes) {
  5949. btrfs_err(fs_info,
  5950. "super_total_bytes %llu mismatch with fs_devices total_rw_bytes %llu",
  5951. btrfs_super_total_bytes(fs_info->super_copy),
  5952. fs_info->fs_devices->total_rw_bytes);
  5953. ret = -EINVAL;
  5954. goto error;
  5955. }
  5956. ret = 0;
  5957. error:
  5958. mutex_unlock(&fs_info->chunk_mutex);
  5959. mutex_unlock(&uuid_mutex);
  5960. btrfs_free_path(path);
  5961. return ret;
  5962. }
  5963. void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
  5964. {
  5965. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  5966. struct btrfs_device *device;
  5967. while (fs_devices) {
  5968. mutex_lock(&fs_devices->device_list_mutex);
  5969. list_for_each_entry(device, &fs_devices->devices, dev_list)
  5970. device->fs_info = fs_info;
  5971. mutex_unlock(&fs_devices->device_list_mutex);
  5972. fs_devices = fs_devices->seed;
  5973. }
  5974. }
  5975. static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
  5976. {
  5977. int i;
  5978. for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
  5979. btrfs_dev_stat_reset(dev, i);
  5980. }
  5981. int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
  5982. {
  5983. struct btrfs_key key;
  5984. struct btrfs_key found_key;
  5985. struct btrfs_root *dev_root = fs_info->dev_root;
  5986. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  5987. struct extent_buffer *eb;
  5988. int slot;
  5989. int ret = 0;
  5990. struct btrfs_device *device;
  5991. struct btrfs_path *path = NULL;
  5992. int i;
  5993. path = btrfs_alloc_path();
  5994. if (!path) {
  5995. ret = -ENOMEM;
  5996. goto out;
  5997. }
  5998. mutex_lock(&fs_devices->device_list_mutex);
  5999. list_for_each_entry(device, &fs_devices->devices, dev_list) {
  6000. int item_size;
  6001. struct btrfs_dev_stats_item *ptr;
  6002. key.objectid = BTRFS_DEV_STATS_OBJECTID;
  6003. key.type = BTRFS_PERSISTENT_ITEM_KEY;
  6004. key.offset = device->devid;
  6005. ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
  6006. if (ret) {
  6007. __btrfs_reset_dev_stats(device);
  6008. device->dev_stats_valid = 1;
  6009. btrfs_release_path(path);
  6010. continue;
  6011. }
  6012. slot = path->slots[0];
  6013. eb = path->nodes[0];
  6014. btrfs_item_key_to_cpu(eb, &found_key, slot);
  6015. item_size = btrfs_item_size_nr(eb, slot);
  6016. ptr = btrfs_item_ptr(eb, slot,
  6017. struct btrfs_dev_stats_item);
  6018. for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
  6019. if (item_size >= (1 + i) * sizeof(__le64))
  6020. btrfs_dev_stat_set(device, i,
  6021. btrfs_dev_stats_value(eb, ptr, i));
  6022. else
  6023. btrfs_dev_stat_reset(device, i);
  6024. }
  6025. device->dev_stats_valid = 1;
  6026. btrfs_dev_stat_print_on_load(device);
  6027. btrfs_release_path(path);
  6028. }
  6029. mutex_unlock(&fs_devices->device_list_mutex);
  6030. out:
  6031. btrfs_free_path(path);
  6032. return ret < 0 ? ret : 0;
  6033. }
  6034. static int update_dev_stat_item(struct btrfs_trans_handle *trans,
  6035. struct btrfs_fs_info *fs_info,
  6036. struct btrfs_device *device)
  6037. {
  6038. struct btrfs_root *dev_root = fs_info->dev_root;
  6039. struct btrfs_path *path;
  6040. struct btrfs_key key;
  6041. struct extent_buffer *eb;
  6042. struct btrfs_dev_stats_item *ptr;
  6043. int ret;
  6044. int i;
  6045. key.objectid = BTRFS_DEV_STATS_OBJECTID;
  6046. key.type = BTRFS_PERSISTENT_ITEM_KEY;
  6047. key.offset = device->devid;
  6048. path = btrfs_alloc_path();
  6049. if (!path)
  6050. return -ENOMEM;
  6051. ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
  6052. if (ret < 0) {
  6053. btrfs_warn_in_rcu(fs_info,
  6054. "error %d while searching for dev_stats item for device %s",
  6055. ret, rcu_str_deref(device->name));
  6056. goto out;
  6057. }
  6058. if (ret == 0 &&
  6059. btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
  6060. /* need to delete old one and insert a new one */
  6061. ret = btrfs_del_item(trans, dev_root, path);
  6062. if (ret != 0) {
  6063. btrfs_warn_in_rcu(fs_info,
  6064. "delete too small dev_stats item for device %s failed %d",
  6065. rcu_str_deref(device->name), ret);
  6066. goto out;
  6067. }
  6068. ret = 1;
  6069. }
  6070. if (ret == 1) {
  6071. /* need to insert a new item */
  6072. btrfs_release_path(path);
  6073. ret = btrfs_insert_empty_item(trans, dev_root, path,
  6074. &key, sizeof(*ptr));
  6075. if (ret < 0) {
  6076. btrfs_warn_in_rcu(fs_info,
  6077. "insert dev_stats item for device %s failed %d",
  6078. rcu_str_deref(device->name), ret);
  6079. goto out;
  6080. }
  6081. }
  6082. eb = path->nodes[0];
  6083. ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
  6084. for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
  6085. btrfs_set_dev_stats_value(eb, ptr, i,
  6086. btrfs_dev_stat_read(device, i));
  6087. btrfs_mark_buffer_dirty(eb);
  6088. out:
  6089. btrfs_free_path(path);
  6090. return ret;
  6091. }
  6092. /*
  6093. * called from commit_transaction. Writes all changed device stats to disk.
  6094. */
  6095. int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
  6096. struct btrfs_fs_info *fs_info)
  6097. {
  6098. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  6099. struct btrfs_device *device;
  6100. int stats_cnt;
  6101. int ret = 0;
  6102. mutex_lock(&fs_devices->device_list_mutex);
  6103. list_for_each_entry(device, &fs_devices->devices, dev_list) {
  6104. if (!device->dev_stats_valid || !btrfs_dev_stats_dirty(device))
  6105. continue;
  6106. stats_cnt = atomic_read(&device->dev_stats_ccnt);
  6107. ret = update_dev_stat_item(trans, fs_info, device);
  6108. if (!ret)
  6109. atomic_sub(stats_cnt, &device->dev_stats_ccnt);
  6110. }
  6111. mutex_unlock(&fs_devices->device_list_mutex);
  6112. return ret;
  6113. }
  6114. void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
  6115. {
  6116. btrfs_dev_stat_inc(dev, index);
  6117. btrfs_dev_stat_print_on_error(dev);
  6118. }
  6119. static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
  6120. {
  6121. if (!dev->dev_stats_valid)
  6122. return;
  6123. btrfs_err_rl_in_rcu(dev->fs_info,
  6124. "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
  6125. rcu_str_deref(dev->name),
  6126. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
  6127. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
  6128. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
  6129. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
  6130. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
  6131. }
  6132. static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
  6133. {
  6134. int i;
  6135. for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
  6136. if (btrfs_dev_stat_read(dev, i) != 0)
  6137. break;
  6138. if (i == BTRFS_DEV_STAT_VALUES_MAX)
  6139. return; /* all values == 0, suppress message */
  6140. btrfs_info_in_rcu(dev->fs_info,
  6141. "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
  6142. rcu_str_deref(dev->name),
  6143. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
  6144. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
  6145. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
  6146. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
  6147. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
  6148. }
  6149. int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
  6150. struct btrfs_ioctl_get_dev_stats *stats)
  6151. {
  6152. struct btrfs_device *dev;
  6153. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  6154. int i;
  6155. mutex_lock(&fs_devices->device_list_mutex);
  6156. dev = btrfs_find_device(fs_info, stats->devid, NULL, NULL);
  6157. mutex_unlock(&fs_devices->device_list_mutex);
  6158. if (!dev) {
  6159. btrfs_warn(fs_info, "get dev_stats failed, device not found");
  6160. return -ENODEV;
  6161. } else if (!dev->dev_stats_valid) {
  6162. btrfs_warn(fs_info, "get dev_stats failed, not yet valid");
  6163. return -ENODEV;
  6164. } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
  6165. for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
  6166. if (stats->nr_items > i)
  6167. stats->values[i] =
  6168. btrfs_dev_stat_read_and_reset(dev, i);
  6169. else
  6170. btrfs_dev_stat_reset(dev, i);
  6171. }
  6172. } else {
  6173. for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
  6174. if (stats->nr_items > i)
  6175. stats->values[i] = btrfs_dev_stat_read(dev, i);
  6176. }
  6177. if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
  6178. stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
  6179. return 0;
  6180. }
  6181. void btrfs_scratch_superblocks(struct block_device *bdev, const char *device_path)
  6182. {
  6183. struct buffer_head *bh;
  6184. struct btrfs_super_block *disk_super;
  6185. int copy_num;
  6186. if (!bdev)
  6187. return;
  6188. for (copy_num = 0; copy_num < BTRFS_SUPER_MIRROR_MAX;
  6189. copy_num++) {
  6190. if (btrfs_read_dev_one_super(bdev, copy_num, &bh))
  6191. continue;
  6192. disk_super = (struct btrfs_super_block *)bh->b_data;
  6193. memset(&disk_super->magic, 0, sizeof(disk_super->magic));
  6194. set_buffer_dirty(bh);
  6195. sync_dirty_buffer(bh);
  6196. brelse(bh);
  6197. }
  6198. /* Notify udev that device has changed */
  6199. btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
  6200. /* Update ctime/mtime for device path for libblkid */
  6201. update_dev_time(device_path);
  6202. }
  6203. /*
  6204. * Update the size of all devices, which is used for writing out the
  6205. * super blocks.
  6206. */
  6207. void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info)
  6208. {
  6209. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  6210. struct btrfs_device *curr, *next;
  6211. if (list_empty(&fs_devices->resized_devices))
  6212. return;
  6213. mutex_lock(&fs_devices->device_list_mutex);
  6214. mutex_lock(&fs_info->chunk_mutex);
  6215. list_for_each_entry_safe(curr, next, &fs_devices->resized_devices,
  6216. resized_list) {
  6217. list_del_init(&curr->resized_list);
  6218. curr->commit_total_bytes = curr->disk_total_bytes;
  6219. }
  6220. mutex_unlock(&fs_info->chunk_mutex);
  6221. mutex_unlock(&fs_devices->device_list_mutex);
  6222. }
  6223. /* Must be invoked during the transaction commit */
  6224. void btrfs_update_commit_device_bytes_used(struct btrfs_fs_info *fs_info,
  6225. struct btrfs_transaction *transaction)
  6226. {
  6227. struct extent_map *em;
  6228. struct map_lookup *map;
  6229. struct btrfs_device *dev;
  6230. int i;
  6231. if (list_empty(&transaction->pending_chunks))
  6232. return;
  6233. /* In order to kick the device replace finish process */
  6234. mutex_lock(&fs_info->chunk_mutex);
  6235. list_for_each_entry(em, &transaction->pending_chunks, list) {
  6236. map = em->map_lookup;
  6237. for (i = 0; i < map->num_stripes; i++) {
  6238. dev = map->stripes[i].dev;
  6239. dev->commit_bytes_used = dev->bytes_used;
  6240. }
  6241. }
  6242. mutex_unlock(&fs_info->chunk_mutex);
  6243. }
  6244. void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info)
  6245. {
  6246. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  6247. while (fs_devices) {
  6248. fs_devices->fs_info = fs_info;
  6249. fs_devices = fs_devices->seed;
  6250. }
  6251. }
  6252. void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info)
  6253. {
  6254. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  6255. while (fs_devices) {
  6256. fs_devices->fs_info = NULL;
  6257. fs_devices = fs_devices->seed;
  6258. }
  6259. }