volumes.c 183 KB

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