volumes.c 187 KB

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