volumes.c 186 KB

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