volumes.c 172 KB

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