volumes.c 176 KB

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