volumes.c 187 KB

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