volumes.c 155 KB

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