volumes.c 187 KB

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