volumes.c 189 KB

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