volumes.c 160 KB

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