volumes.c 181 KB

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