volumes.c 188 KB

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