volumes.c 194 KB

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