volumes.c 178 KB

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