volumes.c 173 KB

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