volumes.c 169 KB

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