volumes.c 160 KB

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