volumes.c 164 KB

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