volumes.c 187 KB

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