volumes.c 154 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999
  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. ret = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET;
  1262. goto out;
  1263. }
  1264. if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) {
  1265. ret = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET;
  1266. goto out;
  1267. }
  1268. if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) &&
  1269. root->fs_info->fs_devices->rw_devices <= 2) {
  1270. ret = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET;
  1271. goto out;
  1272. }
  1273. if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) &&
  1274. root->fs_info->fs_devices->rw_devices <= 3) {
  1275. ret = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET;
  1276. goto out;
  1277. }
  1278. if (strcmp(device_path, "missing") == 0) {
  1279. struct list_head *devices;
  1280. struct btrfs_device *tmp;
  1281. device = NULL;
  1282. devices = &root->fs_info->fs_devices->devices;
  1283. /*
  1284. * It is safe to read the devices since the volume_mutex
  1285. * is held.
  1286. */
  1287. list_for_each_entry(tmp, devices, dev_list) {
  1288. if (tmp->in_fs_metadata &&
  1289. !tmp->is_tgtdev_for_dev_replace &&
  1290. !tmp->bdev) {
  1291. device = tmp;
  1292. break;
  1293. }
  1294. }
  1295. bdev = NULL;
  1296. bh = NULL;
  1297. disk_super = NULL;
  1298. if (!device) {
  1299. ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
  1300. goto out;
  1301. }
  1302. } else {
  1303. ret = btrfs_get_bdev_and_sb(device_path,
  1304. FMODE_WRITE | FMODE_EXCL,
  1305. root->fs_info->bdev_holder, 0,
  1306. &bdev, &bh);
  1307. if (ret)
  1308. goto out;
  1309. disk_super = (struct btrfs_super_block *)bh->b_data;
  1310. devid = btrfs_stack_device_id(&disk_super->dev_item);
  1311. dev_uuid = disk_super->dev_item.uuid;
  1312. device = btrfs_find_device(root->fs_info, devid, dev_uuid,
  1313. disk_super->fsid);
  1314. if (!device) {
  1315. ret = -ENOENT;
  1316. goto error_brelse;
  1317. }
  1318. }
  1319. if (device->is_tgtdev_for_dev_replace) {
  1320. ret = BTRFS_ERROR_DEV_TGT_REPLACE;
  1321. goto error_brelse;
  1322. }
  1323. if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
  1324. ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
  1325. goto error_brelse;
  1326. }
  1327. if (device->writeable) {
  1328. lock_chunks(root);
  1329. list_del_init(&device->dev_alloc_list);
  1330. unlock_chunks(root);
  1331. root->fs_info->fs_devices->rw_devices--;
  1332. clear_super = true;
  1333. }
  1334. ret = btrfs_shrink_device(device, 0);
  1335. if (ret)
  1336. goto error_undo;
  1337. /*
  1338. * TODO: the superblock still includes this device in its num_devices
  1339. * counter although write_all_supers() is not locked out. This
  1340. * could give a filesystem state which requires a degraded mount.
  1341. */
  1342. ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
  1343. if (ret)
  1344. goto error_undo;
  1345. spin_lock(&root->fs_info->free_chunk_lock);
  1346. root->fs_info->free_chunk_space = device->total_bytes -
  1347. device->bytes_used;
  1348. spin_unlock(&root->fs_info->free_chunk_lock);
  1349. device->in_fs_metadata = 0;
  1350. btrfs_scrub_cancel_dev(root->fs_info, device);
  1351. /*
  1352. * the device list mutex makes sure that we don't change
  1353. * the device list while someone else is writing out all
  1354. * the device supers.
  1355. */
  1356. cur_devices = device->fs_devices;
  1357. mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
  1358. list_del_rcu(&device->dev_list);
  1359. device->fs_devices->num_devices--;
  1360. device->fs_devices->total_devices--;
  1361. if (device->missing)
  1362. root->fs_info->fs_devices->missing_devices--;
  1363. next_device = list_entry(root->fs_info->fs_devices->devices.next,
  1364. struct btrfs_device, dev_list);
  1365. if (device->bdev == root->fs_info->sb->s_bdev)
  1366. root->fs_info->sb->s_bdev = next_device->bdev;
  1367. if (device->bdev == root->fs_info->fs_devices->latest_bdev)
  1368. root->fs_info->fs_devices->latest_bdev = next_device->bdev;
  1369. if (device->bdev)
  1370. device->fs_devices->open_devices--;
  1371. call_rcu(&device->rcu, free_device);
  1372. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1373. num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
  1374. btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
  1375. if (cur_devices->open_devices == 0) {
  1376. struct btrfs_fs_devices *fs_devices;
  1377. fs_devices = root->fs_info->fs_devices;
  1378. while (fs_devices) {
  1379. if (fs_devices->seed == cur_devices)
  1380. break;
  1381. fs_devices = fs_devices->seed;
  1382. }
  1383. fs_devices->seed = cur_devices->seed;
  1384. cur_devices->seed = NULL;
  1385. lock_chunks(root);
  1386. __btrfs_close_devices(cur_devices);
  1387. unlock_chunks(root);
  1388. free_fs_devices(cur_devices);
  1389. }
  1390. root->fs_info->num_tolerated_disk_barrier_failures =
  1391. btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
  1392. /*
  1393. * at this point, the device is zero sized. We want to
  1394. * remove it from the devices list and zero out the old super
  1395. */
  1396. if (clear_super && disk_super) {
  1397. /* make sure this device isn't detected as part of
  1398. * the FS anymore
  1399. */
  1400. memset(&disk_super->magic, 0, sizeof(disk_super->magic));
  1401. set_buffer_dirty(bh);
  1402. sync_dirty_buffer(bh);
  1403. }
  1404. ret = 0;
  1405. /* Notify udev that device has changed */
  1406. if (bdev)
  1407. btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
  1408. error_brelse:
  1409. brelse(bh);
  1410. if (bdev)
  1411. blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
  1412. out:
  1413. mutex_unlock(&uuid_mutex);
  1414. return ret;
  1415. error_undo:
  1416. if (device->writeable) {
  1417. lock_chunks(root);
  1418. list_add(&device->dev_alloc_list,
  1419. &root->fs_info->fs_devices->alloc_list);
  1420. unlock_chunks(root);
  1421. root->fs_info->fs_devices->rw_devices++;
  1422. }
  1423. goto error_brelse;
  1424. }
  1425. void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info *fs_info,
  1426. struct btrfs_device *srcdev)
  1427. {
  1428. WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
  1429. list_del_rcu(&srcdev->dev_list);
  1430. list_del_rcu(&srcdev->dev_alloc_list);
  1431. fs_info->fs_devices->num_devices--;
  1432. if (srcdev->missing) {
  1433. fs_info->fs_devices->missing_devices--;
  1434. fs_info->fs_devices->rw_devices++;
  1435. }
  1436. if (srcdev->can_discard)
  1437. fs_info->fs_devices->num_can_discard--;
  1438. if (srcdev->bdev)
  1439. fs_info->fs_devices->open_devices--;
  1440. call_rcu(&srcdev->rcu, free_device);
  1441. }
  1442. void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
  1443. struct btrfs_device *tgtdev)
  1444. {
  1445. struct btrfs_device *next_device;
  1446. WARN_ON(!tgtdev);
  1447. mutex_lock(&fs_info->fs_devices->device_list_mutex);
  1448. if (tgtdev->bdev) {
  1449. btrfs_scratch_superblock(tgtdev);
  1450. fs_info->fs_devices->open_devices--;
  1451. }
  1452. fs_info->fs_devices->num_devices--;
  1453. if (tgtdev->can_discard)
  1454. fs_info->fs_devices->num_can_discard++;
  1455. next_device = list_entry(fs_info->fs_devices->devices.next,
  1456. struct btrfs_device, dev_list);
  1457. if (tgtdev->bdev == fs_info->sb->s_bdev)
  1458. fs_info->sb->s_bdev = next_device->bdev;
  1459. if (tgtdev->bdev == fs_info->fs_devices->latest_bdev)
  1460. fs_info->fs_devices->latest_bdev = next_device->bdev;
  1461. list_del_rcu(&tgtdev->dev_list);
  1462. call_rcu(&tgtdev->rcu, free_device);
  1463. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  1464. }
  1465. static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
  1466. struct btrfs_device **device)
  1467. {
  1468. int ret = 0;
  1469. struct btrfs_super_block *disk_super;
  1470. u64 devid;
  1471. u8 *dev_uuid;
  1472. struct block_device *bdev;
  1473. struct buffer_head *bh;
  1474. *device = NULL;
  1475. ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
  1476. root->fs_info->bdev_holder, 0, &bdev, &bh);
  1477. if (ret)
  1478. return ret;
  1479. disk_super = (struct btrfs_super_block *)bh->b_data;
  1480. devid = btrfs_stack_device_id(&disk_super->dev_item);
  1481. dev_uuid = disk_super->dev_item.uuid;
  1482. *device = btrfs_find_device(root->fs_info, devid, dev_uuid,
  1483. disk_super->fsid);
  1484. brelse(bh);
  1485. if (!*device)
  1486. ret = -ENOENT;
  1487. blkdev_put(bdev, FMODE_READ);
  1488. return ret;
  1489. }
  1490. int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
  1491. char *device_path,
  1492. struct btrfs_device **device)
  1493. {
  1494. *device = NULL;
  1495. if (strcmp(device_path, "missing") == 0) {
  1496. struct list_head *devices;
  1497. struct btrfs_device *tmp;
  1498. devices = &root->fs_info->fs_devices->devices;
  1499. /*
  1500. * It is safe to read the devices since the volume_mutex
  1501. * is held by the caller.
  1502. */
  1503. list_for_each_entry(tmp, devices, dev_list) {
  1504. if (tmp->in_fs_metadata && !tmp->bdev) {
  1505. *device = tmp;
  1506. break;
  1507. }
  1508. }
  1509. if (!*device) {
  1510. pr_err("btrfs: no missing device found\n");
  1511. return -ENOENT;
  1512. }
  1513. return 0;
  1514. } else {
  1515. return btrfs_find_device_by_path(root, device_path, device);
  1516. }
  1517. }
  1518. /*
  1519. * does all the dirty work required for changing file system's UUID.
  1520. */
  1521. static int btrfs_prepare_sprout(struct btrfs_root *root)
  1522. {
  1523. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  1524. struct btrfs_fs_devices *old_devices;
  1525. struct btrfs_fs_devices *seed_devices;
  1526. struct btrfs_super_block *disk_super = root->fs_info->super_copy;
  1527. struct btrfs_device *device;
  1528. u64 super_flags;
  1529. BUG_ON(!mutex_is_locked(&uuid_mutex));
  1530. if (!fs_devices->seeding)
  1531. return -EINVAL;
  1532. seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
  1533. if (!seed_devices)
  1534. return -ENOMEM;
  1535. old_devices = clone_fs_devices(fs_devices);
  1536. if (IS_ERR(old_devices)) {
  1537. kfree(seed_devices);
  1538. return PTR_ERR(old_devices);
  1539. }
  1540. list_add(&old_devices->list, &fs_uuids);
  1541. memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
  1542. seed_devices->opened = 1;
  1543. INIT_LIST_HEAD(&seed_devices->devices);
  1544. INIT_LIST_HEAD(&seed_devices->alloc_list);
  1545. mutex_init(&seed_devices->device_list_mutex);
  1546. mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
  1547. list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
  1548. synchronize_rcu);
  1549. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1550. list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
  1551. list_for_each_entry(device, &seed_devices->devices, dev_list) {
  1552. device->fs_devices = seed_devices;
  1553. }
  1554. fs_devices->seeding = 0;
  1555. fs_devices->num_devices = 0;
  1556. fs_devices->open_devices = 0;
  1557. fs_devices->total_devices = 0;
  1558. fs_devices->seed = seed_devices;
  1559. generate_random_uuid(fs_devices->fsid);
  1560. memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
  1561. memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
  1562. super_flags = btrfs_super_flags(disk_super) &
  1563. ~BTRFS_SUPER_FLAG_SEEDING;
  1564. btrfs_set_super_flags(disk_super, super_flags);
  1565. return 0;
  1566. }
  1567. /*
  1568. * strore the expected generation for seed devices in device items.
  1569. */
  1570. static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
  1571. struct btrfs_root *root)
  1572. {
  1573. struct btrfs_path *path;
  1574. struct extent_buffer *leaf;
  1575. struct btrfs_dev_item *dev_item;
  1576. struct btrfs_device *device;
  1577. struct btrfs_key key;
  1578. u8 fs_uuid[BTRFS_UUID_SIZE];
  1579. u8 dev_uuid[BTRFS_UUID_SIZE];
  1580. u64 devid;
  1581. int ret;
  1582. path = btrfs_alloc_path();
  1583. if (!path)
  1584. return -ENOMEM;
  1585. root = root->fs_info->chunk_root;
  1586. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  1587. key.offset = 0;
  1588. key.type = BTRFS_DEV_ITEM_KEY;
  1589. while (1) {
  1590. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  1591. if (ret < 0)
  1592. goto error;
  1593. leaf = path->nodes[0];
  1594. next_slot:
  1595. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  1596. ret = btrfs_next_leaf(root, path);
  1597. if (ret > 0)
  1598. break;
  1599. if (ret < 0)
  1600. goto error;
  1601. leaf = path->nodes[0];
  1602. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  1603. btrfs_release_path(path);
  1604. continue;
  1605. }
  1606. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  1607. if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
  1608. key.type != BTRFS_DEV_ITEM_KEY)
  1609. break;
  1610. dev_item = btrfs_item_ptr(leaf, path->slots[0],
  1611. struct btrfs_dev_item);
  1612. devid = btrfs_device_id(leaf, dev_item);
  1613. read_extent_buffer(leaf, dev_uuid,
  1614. (unsigned long)btrfs_device_uuid(dev_item),
  1615. BTRFS_UUID_SIZE);
  1616. read_extent_buffer(leaf, fs_uuid,
  1617. (unsigned long)btrfs_device_fsid(dev_item),
  1618. BTRFS_UUID_SIZE);
  1619. device = btrfs_find_device(root->fs_info, devid, dev_uuid,
  1620. fs_uuid);
  1621. BUG_ON(!device); /* Logic error */
  1622. if (device->fs_devices->seeding) {
  1623. btrfs_set_device_generation(leaf, dev_item,
  1624. device->generation);
  1625. btrfs_mark_buffer_dirty(leaf);
  1626. }
  1627. path->slots[0]++;
  1628. goto next_slot;
  1629. }
  1630. ret = 0;
  1631. error:
  1632. btrfs_free_path(path);
  1633. return ret;
  1634. }
  1635. int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
  1636. {
  1637. struct request_queue *q;
  1638. struct btrfs_trans_handle *trans;
  1639. struct btrfs_device *device;
  1640. struct block_device *bdev;
  1641. struct list_head *devices;
  1642. struct super_block *sb = root->fs_info->sb;
  1643. struct rcu_string *name;
  1644. u64 total_bytes;
  1645. int seeding_dev = 0;
  1646. int ret = 0;
  1647. if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
  1648. return -EROFS;
  1649. bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
  1650. root->fs_info->bdev_holder);
  1651. if (IS_ERR(bdev))
  1652. return PTR_ERR(bdev);
  1653. if (root->fs_info->fs_devices->seeding) {
  1654. seeding_dev = 1;
  1655. down_write(&sb->s_umount);
  1656. mutex_lock(&uuid_mutex);
  1657. }
  1658. filemap_write_and_wait(bdev->bd_inode->i_mapping);
  1659. devices = &root->fs_info->fs_devices->devices;
  1660. mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
  1661. list_for_each_entry(device, devices, dev_list) {
  1662. if (device->bdev == bdev) {
  1663. ret = -EEXIST;
  1664. mutex_unlock(
  1665. &root->fs_info->fs_devices->device_list_mutex);
  1666. goto error;
  1667. }
  1668. }
  1669. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1670. device = kzalloc(sizeof(*device), GFP_NOFS);
  1671. if (!device) {
  1672. /* we can safely leave the fs_devices entry around */
  1673. ret = -ENOMEM;
  1674. goto error;
  1675. }
  1676. name = rcu_string_strdup(device_path, GFP_NOFS);
  1677. if (!name) {
  1678. kfree(device);
  1679. ret = -ENOMEM;
  1680. goto error;
  1681. }
  1682. rcu_assign_pointer(device->name, name);
  1683. ret = find_next_devid(root, &device->devid);
  1684. if (ret) {
  1685. rcu_string_free(device->name);
  1686. kfree(device);
  1687. goto error;
  1688. }
  1689. trans = btrfs_start_transaction(root, 0);
  1690. if (IS_ERR(trans)) {
  1691. rcu_string_free(device->name);
  1692. kfree(device);
  1693. ret = PTR_ERR(trans);
  1694. goto error;
  1695. }
  1696. lock_chunks(root);
  1697. q = bdev_get_queue(bdev);
  1698. if (blk_queue_discard(q))
  1699. device->can_discard = 1;
  1700. device->writeable = 1;
  1701. device->work.func = pending_bios_fn;
  1702. generate_random_uuid(device->uuid);
  1703. spin_lock_init(&device->io_lock);
  1704. device->generation = trans->transid;
  1705. device->io_width = root->sectorsize;
  1706. device->io_align = root->sectorsize;
  1707. device->sector_size = root->sectorsize;
  1708. device->total_bytes = i_size_read(bdev->bd_inode);
  1709. device->disk_total_bytes = device->total_bytes;
  1710. device->dev_root = root->fs_info->dev_root;
  1711. device->bdev = bdev;
  1712. device->in_fs_metadata = 1;
  1713. device->is_tgtdev_for_dev_replace = 0;
  1714. device->mode = FMODE_EXCL;
  1715. set_blocksize(device->bdev, 4096);
  1716. if (seeding_dev) {
  1717. sb->s_flags &= ~MS_RDONLY;
  1718. ret = btrfs_prepare_sprout(root);
  1719. BUG_ON(ret); /* -ENOMEM */
  1720. }
  1721. device->fs_devices = root->fs_info->fs_devices;
  1722. mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
  1723. list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
  1724. list_add(&device->dev_alloc_list,
  1725. &root->fs_info->fs_devices->alloc_list);
  1726. root->fs_info->fs_devices->num_devices++;
  1727. root->fs_info->fs_devices->open_devices++;
  1728. root->fs_info->fs_devices->rw_devices++;
  1729. root->fs_info->fs_devices->total_devices++;
  1730. if (device->can_discard)
  1731. root->fs_info->fs_devices->num_can_discard++;
  1732. root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
  1733. spin_lock(&root->fs_info->free_chunk_lock);
  1734. root->fs_info->free_chunk_space += device->total_bytes;
  1735. spin_unlock(&root->fs_info->free_chunk_lock);
  1736. if (!blk_queue_nonrot(bdev_get_queue(bdev)))
  1737. root->fs_info->fs_devices->rotating = 1;
  1738. total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
  1739. btrfs_set_super_total_bytes(root->fs_info->super_copy,
  1740. total_bytes + device->total_bytes);
  1741. total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
  1742. btrfs_set_super_num_devices(root->fs_info->super_copy,
  1743. total_bytes + 1);
  1744. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1745. if (seeding_dev) {
  1746. ret = init_first_rw_device(trans, root, device);
  1747. if (ret) {
  1748. btrfs_abort_transaction(trans, root, ret);
  1749. goto error_trans;
  1750. }
  1751. ret = btrfs_finish_sprout(trans, root);
  1752. if (ret) {
  1753. btrfs_abort_transaction(trans, root, ret);
  1754. goto error_trans;
  1755. }
  1756. } else {
  1757. ret = btrfs_add_device(trans, root, device);
  1758. if (ret) {
  1759. btrfs_abort_transaction(trans, root, ret);
  1760. goto error_trans;
  1761. }
  1762. }
  1763. /*
  1764. * we've got more storage, clear any full flags on the space
  1765. * infos
  1766. */
  1767. btrfs_clear_space_info_full(root->fs_info);
  1768. unlock_chunks(root);
  1769. root->fs_info->num_tolerated_disk_barrier_failures =
  1770. btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
  1771. ret = btrfs_commit_transaction(trans, root);
  1772. if (seeding_dev) {
  1773. mutex_unlock(&uuid_mutex);
  1774. up_write(&sb->s_umount);
  1775. if (ret) /* transaction commit */
  1776. return ret;
  1777. ret = btrfs_relocate_sys_chunks(root);
  1778. if (ret < 0)
  1779. btrfs_error(root->fs_info, ret,
  1780. "Failed to relocate sys chunks after "
  1781. "device initialization. This can be fixed "
  1782. "using the \"btrfs balance\" command.");
  1783. trans = btrfs_attach_transaction(root);
  1784. if (IS_ERR(trans)) {
  1785. if (PTR_ERR(trans) == -ENOENT)
  1786. return 0;
  1787. return PTR_ERR(trans);
  1788. }
  1789. ret = btrfs_commit_transaction(trans, root);
  1790. }
  1791. return ret;
  1792. error_trans:
  1793. unlock_chunks(root);
  1794. btrfs_end_transaction(trans, root);
  1795. rcu_string_free(device->name);
  1796. kfree(device);
  1797. error:
  1798. blkdev_put(bdev, FMODE_EXCL);
  1799. if (seeding_dev) {
  1800. mutex_unlock(&uuid_mutex);
  1801. up_write(&sb->s_umount);
  1802. }
  1803. return ret;
  1804. }
  1805. int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
  1806. struct btrfs_device **device_out)
  1807. {
  1808. struct request_queue *q;
  1809. struct btrfs_device *device;
  1810. struct block_device *bdev;
  1811. struct btrfs_fs_info *fs_info = root->fs_info;
  1812. struct list_head *devices;
  1813. struct rcu_string *name;
  1814. int ret = 0;
  1815. *device_out = NULL;
  1816. if (fs_info->fs_devices->seeding)
  1817. return -EINVAL;
  1818. bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
  1819. fs_info->bdev_holder);
  1820. if (IS_ERR(bdev))
  1821. return PTR_ERR(bdev);
  1822. filemap_write_and_wait(bdev->bd_inode->i_mapping);
  1823. devices = &fs_info->fs_devices->devices;
  1824. list_for_each_entry(device, devices, dev_list) {
  1825. if (device->bdev == bdev) {
  1826. ret = -EEXIST;
  1827. goto error;
  1828. }
  1829. }
  1830. device = kzalloc(sizeof(*device), GFP_NOFS);
  1831. if (!device) {
  1832. ret = -ENOMEM;
  1833. goto error;
  1834. }
  1835. name = rcu_string_strdup(device_path, GFP_NOFS);
  1836. if (!name) {
  1837. kfree(device);
  1838. ret = -ENOMEM;
  1839. goto error;
  1840. }
  1841. rcu_assign_pointer(device->name, name);
  1842. q = bdev_get_queue(bdev);
  1843. if (blk_queue_discard(q))
  1844. device->can_discard = 1;
  1845. mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
  1846. device->writeable = 1;
  1847. device->work.func = pending_bios_fn;
  1848. generate_random_uuid(device->uuid);
  1849. device->devid = BTRFS_DEV_REPLACE_DEVID;
  1850. spin_lock_init(&device->io_lock);
  1851. device->generation = 0;
  1852. device->io_width = root->sectorsize;
  1853. device->io_align = root->sectorsize;
  1854. device->sector_size = root->sectorsize;
  1855. device->total_bytes = i_size_read(bdev->bd_inode);
  1856. device->disk_total_bytes = device->total_bytes;
  1857. device->dev_root = fs_info->dev_root;
  1858. device->bdev = bdev;
  1859. device->in_fs_metadata = 1;
  1860. device->is_tgtdev_for_dev_replace = 1;
  1861. device->mode = FMODE_EXCL;
  1862. set_blocksize(device->bdev, 4096);
  1863. device->fs_devices = fs_info->fs_devices;
  1864. list_add(&device->dev_list, &fs_info->fs_devices->devices);
  1865. fs_info->fs_devices->num_devices++;
  1866. fs_info->fs_devices->open_devices++;
  1867. if (device->can_discard)
  1868. fs_info->fs_devices->num_can_discard++;
  1869. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1870. *device_out = device;
  1871. return ret;
  1872. error:
  1873. blkdev_put(bdev, FMODE_EXCL);
  1874. return ret;
  1875. }
  1876. void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
  1877. struct btrfs_device *tgtdev)
  1878. {
  1879. WARN_ON(fs_info->fs_devices->rw_devices == 0);
  1880. tgtdev->io_width = fs_info->dev_root->sectorsize;
  1881. tgtdev->io_align = fs_info->dev_root->sectorsize;
  1882. tgtdev->sector_size = fs_info->dev_root->sectorsize;
  1883. tgtdev->dev_root = fs_info->dev_root;
  1884. tgtdev->in_fs_metadata = 1;
  1885. }
  1886. static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
  1887. struct btrfs_device *device)
  1888. {
  1889. int ret;
  1890. struct btrfs_path *path;
  1891. struct btrfs_root *root;
  1892. struct btrfs_dev_item *dev_item;
  1893. struct extent_buffer *leaf;
  1894. struct btrfs_key key;
  1895. root = device->dev_root->fs_info->chunk_root;
  1896. path = btrfs_alloc_path();
  1897. if (!path)
  1898. return -ENOMEM;
  1899. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  1900. key.type = BTRFS_DEV_ITEM_KEY;
  1901. key.offset = device->devid;
  1902. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  1903. if (ret < 0)
  1904. goto out;
  1905. if (ret > 0) {
  1906. ret = -ENOENT;
  1907. goto out;
  1908. }
  1909. leaf = path->nodes[0];
  1910. dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
  1911. btrfs_set_device_id(leaf, dev_item, device->devid);
  1912. btrfs_set_device_type(leaf, dev_item, device->type);
  1913. btrfs_set_device_io_align(leaf, dev_item, device->io_align);
  1914. btrfs_set_device_io_width(leaf, dev_item, device->io_width);
  1915. btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
  1916. btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
  1917. btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
  1918. btrfs_mark_buffer_dirty(leaf);
  1919. out:
  1920. btrfs_free_path(path);
  1921. return ret;
  1922. }
  1923. static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
  1924. struct btrfs_device *device, u64 new_size)
  1925. {
  1926. struct btrfs_super_block *super_copy =
  1927. device->dev_root->fs_info->super_copy;
  1928. u64 old_total = btrfs_super_total_bytes(super_copy);
  1929. u64 diff = new_size - device->total_bytes;
  1930. if (!device->writeable)
  1931. return -EACCES;
  1932. if (new_size <= device->total_bytes ||
  1933. device->is_tgtdev_for_dev_replace)
  1934. return -EINVAL;
  1935. btrfs_set_super_total_bytes(super_copy, old_total + diff);
  1936. device->fs_devices->total_rw_bytes += diff;
  1937. device->total_bytes = new_size;
  1938. device->disk_total_bytes = new_size;
  1939. btrfs_clear_space_info_full(device->dev_root->fs_info);
  1940. return btrfs_update_device(trans, device);
  1941. }
  1942. int btrfs_grow_device(struct btrfs_trans_handle *trans,
  1943. struct btrfs_device *device, u64 new_size)
  1944. {
  1945. int ret;
  1946. lock_chunks(device->dev_root);
  1947. ret = __btrfs_grow_device(trans, device, new_size);
  1948. unlock_chunks(device->dev_root);
  1949. return ret;
  1950. }
  1951. static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
  1952. struct btrfs_root *root,
  1953. u64 chunk_tree, u64 chunk_objectid,
  1954. u64 chunk_offset)
  1955. {
  1956. int ret;
  1957. struct btrfs_path *path;
  1958. struct btrfs_key key;
  1959. root = root->fs_info->chunk_root;
  1960. path = btrfs_alloc_path();
  1961. if (!path)
  1962. return -ENOMEM;
  1963. key.objectid = chunk_objectid;
  1964. key.offset = chunk_offset;
  1965. key.type = BTRFS_CHUNK_ITEM_KEY;
  1966. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  1967. if (ret < 0)
  1968. goto out;
  1969. else if (ret > 0) { /* Logic error or corruption */
  1970. btrfs_error(root->fs_info, -ENOENT,
  1971. "Failed lookup while freeing chunk.");
  1972. ret = -ENOENT;
  1973. goto out;
  1974. }
  1975. ret = btrfs_del_item(trans, root, path);
  1976. if (ret < 0)
  1977. btrfs_error(root->fs_info, ret,
  1978. "Failed to delete chunk item.");
  1979. out:
  1980. btrfs_free_path(path);
  1981. return ret;
  1982. }
  1983. static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
  1984. chunk_offset)
  1985. {
  1986. struct btrfs_super_block *super_copy = root->fs_info->super_copy;
  1987. struct btrfs_disk_key *disk_key;
  1988. struct btrfs_chunk *chunk;
  1989. u8 *ptr;
  1990. int ret = 0;
  1991. u32 num_stripes;
  1992. u32 array_size;
  1993. u32 len = 0;
  1994. u32 cur;
  1995. struct btrfs_key key;
  1996. array_size = btrfs_super_sys_array_size(super_copy);
  1997. ptr = super_copy->sys_chunk_array;
  1998. cur = 0;
  1999. while (cur < array_size) {
  2000. disk_key = (struct btrfs_disk_key *)ptr;
  2001. btrfs_disk_key_to_cpu(&key, disk_key);
  2002. len = sizeof(*disk_key);
  2003. if (key.type == BTRFS_CHUNK_ITEM_KEY) {
  2004. chunk = (struct btrfs_chunk *)(ptr + len);
  2005. num_stripes = btrfs_stack_chunk_num_stripes(chunk);
  2006. len += btrfs_chunk_item_size(num_stripes);
  2007. } else {
  2008. ret = -EIO;
  2009. break;
  2010. }
  2011. if (key.objectid == chunk_objectid &&
  2012. key.offset == chunk_offset) {
  2013. memmove(ptr, ptr + len, array_size - (cur + len));
  2014. array_size -= len;
  2015. btrfs_set_super_sys_array_size(super_copy, array_size);
  2016. } else {
  2017. ptr += len;
  2018. cur += len;
  2019. }
  2020. }
  2021. return ret;
  2022. }
  2023. static int btrfs_relocate_chunk(struct btrfs_root *root,
  2024. u64 chunk_tree, u64 chunk_objectid,
  2025. u64 chunk_offset)
  2026. {
  2027. struct extent_map_tree *em_tree;
  2028. struct btrfs_root *extent_root;
  2029. struct btrfs_trans_handle *trans;
  2030. struct extent_map *em;
  2031. struct map_lookup *map;
  2032. int ret;
  2033. int i;
  2034. root = root->fs_info->chunk_root;
  2035. extent_root = root->fs_info->extent_root;
  2036. em_tree = &root->fs_info->mapping_tree.map_tree;
  2037. ret = btrfs_can_relocate(extent_root, chunk_offset);
  2038. if (ret)
  2039. return -ENOSPC;
  2040. /* step one, relocate all the extents inside this chunk */
  2041. ret = btrfs_relocate_block_group(extent_root, chunk_offset);
  2042. if (ret)
  2043. return ret;
  2044. trans = btrfs_start_transaction(root, 0);
  2045. if (IS_ERR(trans)) {
  2046. ret = PTR_ERR(trans);
  2047. btrfs_std_error(root->fs_info, ret);
  2048. return ret;
  2049. }
  2050. lock_chunks(root);
  2051. /*
  2052. * step two, delete the device extents and the
  2053. * chunk tree entries
  2054. */
  2055. read_lock(&em_tree->lock);
  2056. em = lookup_extent_mapping(em_tree, chunk_offset, 1);
  2057. read_unlock(&em_tree->lock);
  2058. BUG_ON(!em || em->start > chunk_offset ||
  2059. em->start + em->len < chunk_offset);
  2060. map = (struct map_lookup *)em->bdev;
  2061. for (i = 0; i < map->num_stripes; i++) {
  2062. ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
  2063. map->stripes[i].physical);
  2064. BUG_ON(ret);
  2065. if (map->stripes[i].dev) {
  2066. ret = btrfs_update_device(trans, map->stripes[i].dev);
  2067. BUG_ON(ret);
  2068. }
  2069. }
  2070. ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
  2071. chunk_offset);
  2072. BUG_ON(ret);
  2073. trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
  2074. if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
  2075. ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
  2076. BUG_ON(ret);
  2077. }
  2078. ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
  2079. BUG_ON(ret);
  2080. write_lock(&em_tree->lock);
  2081. remove_extent_mapping(em_tree, em);
  2082. write_unlock(&em_tree->lock);
  2083. kfree(map);
  2084. em->bdev = NULL;
  2085. /* once for the tree */
  2086. free_extent_map(em);
  2087. /* once for us */
  2088. free_extent_map(em);
  2089. unlock_chunks(root);
  2090. btrfs_end_transaction(trans, root);
  2091. return 0;
  2092. }
  2093. static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
  2094. {
  2095. struct btrfs_root *chunk_root = root->fs_info->chunk_root;
  2096. struct btrfs_path *path;
  2097. struct extent_buffer *leaf;
  2098. struct btrfs_chunk *chunk;
  2099. struct btrfs_key key;
  2100. struct btrfs_key found_key;
  2101. u64 chunk_tree = chunk_root->root_key.objectid;
  2102. u64 chunk_type;
  2103. bool retried = false;
  2104. int failed = 0;
  2105. int ret;
  2106. path = btrfs_alloc_path();
  2107. if (!path)
  2108. return -ENOMEM;
  2109. again:
  2110. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  2111. key.offset = (u64)-1;
  2112. key.type = BTRFS_CHUNK_ITEM_KEY;
  2113. while (1) {
  2114. ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
  2115. if (ret < 0)
  2116. goto error;
  2117. BUG_ON(ret == 0); /* Corruption */
  2118. ret = btrfs_previous_item(chunk_root, path, key.objectid,
  2119. key.type);
  2120. if (ret < 0)
  2121. goto error;
  2122. if (ret > 0)
  2123. break;
  2124. leaf = path->nodes[0];
  2125. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  2126. chunk = btrfs_item_ptr(leaf, path->slots[0],
  2127. struct btrfs_chunk);
  2128. chunk_type = btrfs_chunk_type(leaf, chunk);
  2129. btrfs_release_path(path);
  2130. if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
  2131. ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
  2132. found_key.objectid,
  2133. found_key.offset);
  2134. if (ret == -ENOSPC)
  2135. failed++;
  2136. else if (ret)
  2137. BUG();
  2138. }
  2139. if (found_key.offset == 0)
  2140. break;
  2141. key.offset = found_key.offset - 1;
  2142. }
  2143. ret = 0;
  2144. if (failed && !retried) {
  2145. failed = 0;
  2146. retried = true;
  2147. goto again;
  2148. } else if (failed && retried) {
  2149. WARN_ON(1);
  2150. ret = -ENOSPC;
  2151. }
  2152. error:
  2153. btrfs_free_path(path);
  2154. return ret;
  2155. }
  2156. static int insert_balance_item(struct btrfs_root *root,
  2157. struct btrfs_balance_control *bctl)
  2158. {
  2159. struct btrfs_trans_handle *trans;
  2160. struct btrfs_balance_item *item;
  2161. struct btrfs_disk_balance_args disk_bargs;
  2162. struct btrfs_path *path;
  2163. struct extent_buffer *leaf;
  2164. struct btrfs_key key;
  2165. int ret, err;
  2166. path = btrfs_alloc_path();
  2167. if (!path)
  2168. return -ENOMEM;
  2169. trans = btrfs_start_transaction(root, 0);
  2170. if (IS_ERR(trans)) {
  2171. btrfs_free_path(path);
  2172. return PTR_ERR(trans);
  2173. }
  2174. key.objectid = BTRFS_BALANCE_OBJECTID;
  2175. key.type = BTRFS_BALANCE_ITEM_KEY;
  2176. key.offset = 0;
  2177. ret = btrfs_insert_empty_item(trans, root, path, &key,
  2178. sizeof(*item));
  2179. if (ret)
  2180. goto out;
  2181. leaf = path->nodes[0];
  2182. item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
  2183. memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
  2184. btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
  2185. btrfs_set_balance_data(leaf, item, &disk_bargs);
  2186. btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
  2187. btrfs_set_balance_meta(leaf, item, &disk_bargs);
  2188. btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
  2189. btrfs_set_balance_sys(leaf, item, &disk_bargs);
  2190. btrfs_set_balance_flags(leaf, item, bctl->flags);
  2191. btrfs_mark_buffer_dirty(leaf);
  2192. out:
  2193. btrfs_free_path(path);
  2194. err = btrfs_commit_transaction(trans, root);
  2195. if (err && !ret)
  2196. ret = err;
  2197. return ret;
  2198. }
  2199. static int del_balance_item(struct btrfs_root *root)
  2200. {
  2201. struct btrfs_trans_handle *trans;
  2202. struct btrfs_path *path;
  2203. struct btrfs_key key;
  2204. int ret, err;
  2205. path = btrfs_alloc_path();
  2206. if (!path)
  2207. return -ENOMEM;
  2208. trans = btrfs_start_transaction(root, 0);
  2209. if (IS_ERR(trans)) {
  2210. btrfs_free_path(path);
  2211. return PTR_ERR(trans);
  2212. }
  2213. key.objectid = BTRFS_BALANCE_OBJECTID;
  2214. key.type = BTRFS_BALANCE_ITEM_KEY;
  2215. key.offset = 0;
  2216. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  2217. if (ret < 0)
  2218. goto out;
  2219. if (ret > 0) {
  2220. ret = -ENOENT;
  2221. goto out;
  2222. }
  2223. ret = btrfs_del_item(trans, root, path);
  2224. out:
  2225. btrfs_free_path(path);
  2226. err = btrfs_commit_transaction(trans, root);
  2227. if (err && !ret)
  2228. ret = err;
  2229. return ret;
  2230. }
  2231. /*
  2232. * This is a heuristic used to reduce the number of chunks balanced on
  2233. * resume after balance was interrupted.
  2234. */
  2235. static void update_balance_args(struct btrfs_balance_control *bctl)
  2236. {
  2237. /*
  2238. * Turn on soft mode for chunk types that were being converted.
  2239. */
  2240. if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
  2241. bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
  2242. if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
  2243. bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
  2244. if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
  2245. bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
  2246. /*
  2247. * Turn on usage filter if is not already used. The idea is
  2248. * that chunks that we have already balanced should be
  2249. * reasonably full. Don't do it for chunks that are being
  2250. * converted - that will keep us from relocating unconverted
  2251. * (albeit full) chunks.
  2252. */
  2253. if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
  2254. !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
  2255. bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
  2256. bctl->data.usage = 90;
  2257. }
  2258. if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
  2259. !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
  2260. bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
  2261. bctl->sys.usage = 90;
  2262. }
  2263. if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
  2264. !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
  2265. bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
  2266. bctl->meta.usage = 90;
  2267. }
  2268. }
  2269. /*
  2270. * Should be called with both balance and volume mutexes held to
  2271. * serialize other volume operations (add_dev/rm_dev/resize) with
  2272. * restriper. Same goes for unset_balance_control.
  2273. */
  2274. static void set_balance_control(struct btrfs_balance_control *bctl)
  2275. {
  2276. struct btrfs_fs_info *fs_info = bctl->fs_info;
  2277. BUG_ON(fs_info->balance_ctl);
  2278. spin_lock(&fs_info->balance_lock);
  2279. fs_info->balance_ctl = bctl;
  2280. spin_unlock(&fs_info->balance_lock);
  2281. }
  2282. static void unset_balance_control(struct btrfs_fs_info *fs_info)
  2283. {
  2284. struct btrfs_balance_control *bctl = fs_info->balance_ctl;
  2285. BUG_ON(!fs_info->balance_ctl);
  2286. spin_lock(&fs_info->balance_lock);
  2287. fs_info->balance_ctl = NULL;
  2288. spin_unlock(&fs_info->balance_lock);
  2289. kfree(bctl);
  2290. }
  2291. /*
  2292. * Balance filters. Return 1 if chunk should be filtered out
  2293. * (should not be balanced).
  2294. */
  2295. static int chunk_profiles_filter(u64 chunk_type,
  2296. struct btrfs_balance_args *bargs)
  2297. {
  2298. chunk_type = chunk_to_extended(chunk_type) &
  2299. BTRFS_EXTENDED_PROFILE_MASK;
  2300. if (bargs->profiles & chunk_type)
  2301. return 0;
  2302. return 1;
  2303. }
  2304. static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
  2305. struct btrfs_balance_args *bargs)
  2306. {
  2307. struct btrfs_block_group_cache *cache;
  2308. u64 chunk_used, user_thresh;
  2309. int ret = 1;
  2310. cache = btrfs_lookup_block_group(fs_info, chunk_offset);
  2311. chunk_used = btrfs_block_group_used(&cache->item);
  2312. if (bargs->usage == 0)
  2313. user_thresh = 1;
  2314. else if (bargs->usage > 100)
  2315. user_thresh = cache->key.offset;
  2316. else
  2317. user_thresh = div_factor_fine(cache->key.offset,
  2318. bargs->usage);
  2319. if (chunk_used < user_thresh)
  2320. ret = 0;
  2321. btrfs_put_block_group(cache);
  2322. return ret;
  2323. }
  2324. static int chunk_devid_filter(struct extent_buffer *leaf,
  2325. struct btrfs_chunk *chunk,
  2326. struct btrfs_balance_args *bargs)
  2327. {
  2328. struct btrfs_stripe *stripe;
  2329. int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
  2330. int i;
  2331. for (i = 0; i < num_stripes; i++) {
  2332. stripe = btrfs_stripe_nr(chunk, i);
  2333. if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
  2334. return 0;
  2335. }
  2336. return 1;
  2337. }
  2338. /* [pstart, pend) */
  2339. static int chunk_drange_filter(struct extent_buffer *leaf,
  2340. struct btrfs_chunk *chunk,
  2341. u64 chunk_offset,
  2342. struct btrfs_balance_args *bargs)
  2343. {
  2344. struct btrfs_stripe *stripe;
  2345. int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
  2346. u64 stripe_offset;
  2347. u64 stripe_length;
  2348. int factor;
  2349. int i;
  2350. if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
  2351. return 0;
  2352. if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
  2353. BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
  2354. factor = num_stripes / 2;
  2355. } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
  2356. factor = num_stripes - 1;
  2357. } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
  2358. factor = num_stripes - 2;
  2359. } else {
  2360. factor = num_stripes;
  2361. }
  2362. for (i = 0; i < num_stripes; i++) {
  2363. stripe = btrfs_stripe_nr(chunk, i);
  2364. if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
  2365. continue;
  2366. stripe_offset = btrfs_stripe_offset(leaf, stripe);
  2367. stripe_length = btrfs_chunk_length(leaf, chunk);
  2368. do_div(stripe_length, factor);
  2369. if (stripe_offset < bargs->pend &&
  2370. stripe_offset + stripe_length > bargs->pstart)
  2371. return 0;
  2372. }
  2373. return 1;
  2374. }
  2375. /* [vstart, vend) */
  2376. static int chunk_vrange_filter(struct extent_buffer *leaf,
  2377. struct btrfs_chunk *chunk,
  2378. u64 chunk_offset,
  2379. struct btrfs_balance_args *bargs)
  2380. {
  2381. if (chunk_offset < bargs->vend &&
  2382. chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
  2383. /* at least part of the chunk is inside this vrange */
  2384. return 0;
  2385. return 1;
  2386. }
  2387. static int chunk_soft_convert_filter(u64 chunk_type,
  2388. struct btrfs_balance_args *bargs)
  2389. {
  2390. if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
  2391. return 0;
  2392. chunk_type = chunk_to_extended(chunk_type) &
  2393. BTRFS_EXTENDED_PROFILE_MASK;
  2394. if (bargs->target == chunk_type)
  2395. return 1;
  2396. return 0;
  2397. }
  2398. static int should_balance_chunk(struct btrfs_root *root,
  2399. struct extent_buffer *leaf,
  2400. struct btrfs_chunk *chunk, u64 chunk_offset)
  2401. {
  2402. struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
  2403. struct btrfs_balance_args *bargs = NULL;
  2404. u64 chunk_type = btrfs_chunk_type(leaf, chunk);
  2405. /* type filter */
  2406. if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
  2407. (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
  2408. return 0;
  2409. }
  2410. if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
  2411. bargs = &bctl->data;
  2412. else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
  2413. bargs = &bctl->sys;
  2414. else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
  2415. bargs = &bctl->meta;
  2416. /* profiles filter */
  2417. if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
  2418. chunk_profiles_filter(chunk_type, bargs)) {
  2419. return 0;
  2420. }
  2421. /* usage filter */
  2422. if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
  2423. chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
  2424. return 0;
  2425. }
  2426. /* devid filter */
  2427. if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
  2428. chunk_devid_filter(leaf, chunk, bargs)) {
  2429. return 0;
  2430. }
  2431. /* drange filter, makes sense only with devid filter */
  2432. if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
  2433. chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
  2434. return 0;
  2435. }
  2436. /* vrange filter */
  2437. if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
  2438. chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
  2439. return 0;
  2440. }
  2441. /* soft profile changing mode */
  2442. if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
  2443. chunk_soft_convert_filter(chunk_type, bargs)) {
  2444. return 0;
  2445. }
  2446. return 1;
  2447. }
  2448. static int __btrfs_balance(struct btrfs_fs_info *fs_info)
  2449. {
  2450. struct btrfs_balance_control *bctl = fs_info->balance_ctl;
  2451. struct btrfs_root *chunk_root = fs_info->chunk_root;
  2452. struct btrfs_root *dev_root = fs_info->dev_root;
  2453. struct list_head *devices;
  2454. struct btrfs_device *device;
  2455. u64 old_size;
  2456. u64 size_to_free;
  2457. struct btrfs_chunk *chunk;
  2458. struct btrfs_path *path;
  2459. struct btrfs_key key;
  2460. struct btrfs_key found_key;
  2461. struct btrfs_trans_handle *trans;
  2462. struct extent_buffer *leaf;
  2463. int slot;
  2464. int ret;
  2465. int enospc_errors = 0;
  2466. bool counting = true;
  2467. /* step one make some room on all the devices */
  2468. devices = &fs_info->fs_devices->devices;
  2469. list_for_each_entry(device, devices, dev_list) {
  2470. old_size = device->total_bytes;
  2471. size_to_free = div_factor(old_size, 1);
  2472. size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
  2473. if (!device->writeable ||
  2474. device->total_bytes - device->bytes_used > size_to_free ||
  2475. device->is_tgtdev_for_dev_replace)
  2476. continue;
  2477. ret = btrfs_shrink_device(device, old_size - size_to_free);
  2478. if (ret == -ENOSPC)
  2479. break;
  2480. BUG_ON(ret);
  2481. trans = btrfs_start_transaction(dev_root, 0);
  2482. BUG_ON(IS_ERR(trans));
  2483. ret = btrfs_grow_device(trans, device, old_size);
  2484. BUG_ON(ret);
  2485. btrfs_end_transaction(trans, dev_root);
  2486. }
  2487. /* step two, relocate all the chunks */
  2488. path = btrfs_alloc_path();
  2489. if (!path) {
  2490. ret = -ENOMEM;
  2491. goto error;
  2492. }
  2493. /* zero out stat counters */
  2494. spin_lock(&fs_info->balance_lock);
  2495. memset(&bctl->stat, 0, sizeof(bctl->stat));
  2496. spin_unlock(&fs_info->balance_lock);
  2497. again:
  2498. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  2499. key.offset = (u64)-1;
  2500. key.type = BTRFS_CHUNK_ITEM_KEY;
  2501. while (1) {
  2502. if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
  2503. atomic_read(&fs_info->balance_cancel_req)) {
  2504. ret = -ECANCELED;
  2505. goto error;
  2506. }
  2507. ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
  2508. if (ret < 0)
  2509. goto error;
  2510. /*
  2511. * this shouldn't happen, it means the last relocate
  2512. * failed
  2513. */
  2514. if (ret == 0)
  2515. BUG(); /* FIXME break ? */
  2516. ret = btrfs_previous_item(chunk_root, path, 0,
  2517. BTRFS_CHUNK_ITEM_KEY);
  2518. if (ret) {
  2519. ret = 0;
  2520. break;
  2521. }
  2522. leaf = path->nodes[0];
  2523. slot = path->slots[0];
  2524. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  2525. if (found_key.objectid != key.objectid)
  2526. break;
  2527. /* chunk zero is special */
  2528. if (found_key.offset == 0)
  2529. break;
  2530. chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
  2531. if (!counting) {
  2532. spin_lock(&fs_info->balance_lock);
  2533. bctl->stat.considered++;
  2534. spin_unlock(&fs_info->balance_lock);
  2535. }
  2536. ret = should_balance_chunk(chunk_root, leaf, chunk,
  2537. found_key.offset);
  2538. btrfs_release_path(path);
  2539. if (!ret)
  2540. goto loop;
  2541. if (counting) {
  2542. spin_lock(&fs_info->balance_lock);
  2543. bctl->stat.expected++;
  2544. spin_unlock(&fs_info->balance_lock);
  2545. goto loop;
  2546. }
  2547. ret = btrfs_relocate_chunk(chunk_root,
  2548. chunk_root->root_key.objectid,
  2549. found_key.objectid,
  2550. found_key.offset);
  2551. if (ret && ret != -ENOSPC)
  2552. goto error;
  2553. if (ret == -ENOSPC) {
  2554. enospc_errors++;
  2555. } else {
  2556. spin_lock(&fs_info->balance_lock);
  2557. bctl->stat.completed++;
  2558. spin_unlock(&fs_info->balance_lock);
  2559. }
  2560. loop:
  2561. key.offset = found_key.offset - 1;
  2562. }
  2563. if (counting) {
  2564. btrfs_release_path(path);
  2565. counting = false;
  2566. goto again;
  2567. }
  2568. error:
  2569. btrfs_free_path(path);
  2570. if (enospc_errors) {
  2571. printk(KERN_INFO "btrfs: %d enospc errors during balance\n",
  2572. enospc_errors);
  2573. if (!ret)
  2574. ret = -ENOSPC;
  2575. }
  2576. return ret;
  2577. }
  2578. /**
  2579. * alloc_profile_is_valid - see if a given profile is valid and reduced
  2580. * @flags: profile to validate
  2581. * @extended: if true @flags is treated as an extended profile
  2582. */
  2583. static int alloc_profile_is_valid(u64 flags, int extended)
  2584. {
  2585. u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
  2586. BTRFS_BLOCK_GROUP_PROFILE_MASK);
  2587. flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
  2588. /* 1) check that all other bits are zeroed */
  2589. if (flags & ~mask)
  2590. return 0;
  2591. /* 2) see if profile is reduced */
  2592. if (flags == 0)
  2593. return !extended; /* "0" is valid for usual profiles */
  2594. /* true if exactly one bit set */
  2595. return (flags & (flags - 1)) == 0;
  2596. }
  2597. static inline int balance_need_close(struct btrfs_fs_info *fs_info)
  2598. {
  2599. /* cancel requested || normal exit path */
  2600. return atomic_read(&fs_info->balance_cancel_req) ||
  2601. (atomic_read(&fs_info->balance_pause_req) == 0 &&
  2602. atomic_read(&fs_info->balance_cancel_req) == 0);
  2603. }
  2604. static void __cancel_balance(struct btrfs_fs_info *fs_info)
  2605. {
  2606. int ret;
  2607. unset_balance_control(fs_info);
  2608. ret = del_balance_item(fs_info->tree_root);
  2609. if (ret)
  2610. btrfs_std_error(fs_info, ret);
  2611. atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
  2612. }
  2613. void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
  2614. struct btrfs_ioctl_balance_args *bargs);
  2615. /*
  2616. * Should be called with both balance and volume mutexes held
  2617. */
  2618. int btrfs_balance(struct btrfs_balance_control *bctl,
  2619. struct btrfs_ioctl_balance_args *bargs)
  2620. {
  2621. struct btrfs_fs_info *fs_info = bctl->fs_info;
  2622. u64 allowed;
  2623. int mixed = 0;
  2624. int ret;
  2625. u64 num_devices;
  2626. unsigned seq;
  2627. if (btrfs_fs_closing(fs_info) ||
  2628. atomic_read(&fs_info->balance_pause_req) ||
  2629. atomic_read(&fs_info->balance_cancel_req)) {
  2630. ret = -EINVAL;
  2631. goto out;
  2632. }
  2633. allowed = btrfs_super_incompat_flags(fs_info->super_copy);
  2634. if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
  2635. mixed = 1;
  2636. /*
  2637. * In case of mixed groups both data and meta should be picked,
  2638. * and identical options should be given for both of them.
  2639. */
  2640. allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
  2641. if (mixed && (bctl->flags & allowed)) {
  2642. if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
  2643. !(bctl->flags & BTRFS_BALANCE_METADATA) ||
  2644. memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
  2645. printk(KERN_ERR "btrfs: with mixed groups data and "
  2646. "metadata balance options must be the same\n");
  2647. ret = -EINVAL;
  2648. goto out;
  2649. }
  2650. }
  2651. num_devices = fs_info->fs_devices->num_devices;
  2652. btrfs_dev_replace_lock(&fs_info->dev_replace);
  2653. if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
  2654. BUG_ON(num_devices < 1);
  2655. num_devices--;
  2656. }
  2657. btrfs_dev_replace_unlock(&fs_info->dev_replace);
  2658. allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
  2659. if (num_devices == 1)
  2660. allowed |= BTRFS_BLOCK_GROUP_DUP;
  2661. else if (num_devices > 1)
  2662. allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
  2663. if (num_devices > 2)
  2664. allowed |= BTRFS_BLOCK_GROUP_RAID5;
  2665. if (num_devices > 3)
  2666. allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
  2667. BTRFS_BLOCK_GROUP_RAID6);
  2668. if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
  2669. (!alloc_profile_is_valid(bctl->data.target, 1) ||
  2670. (bctl->data.target & ~allowed))) {
  2671. printk(KERN_ERR "btrfs: unable to start balance with target "
  2672. "data profile %llu\n",
  2673. (unsigned long long)bctl->data.target);
  2674. ret = -EINVAL;
  2675. goto out;
  2676. }
  2677. if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
  2678. (!alloc_profile_is_valid(bctl->meta.target, 1) ||
  2679. (bctl->meta.target & ~allowed))) {
  2680. printk(KERN_ERR "btrfs: unable to start balance with target "
  2681. "metadata profile %llu\n",
  2682. (unsigned long long)bctl->meta.target);
  2683. ret = -EINVAL;
  2684. goto out;
  2685. }
  2686. if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
  2687. (!alloc_profile_is_valid(bctl->sys.target, 1) ||
  2688. (bctl->sys.target & ~allowed))) {
  2689. printk(KERN_ERR "btrfs: unable to start balance with target "
  2690. "system profile %llu\n",
  2691. (unsigned long long)bctl->sys.target);
  2692. ret = -EINVAL;
  2693. goto out;
  2694. }
  2695. /* allow dup'ed data chunks only in mixed mode */
  2696. if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
  2697. (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
  2698. printk(KERN_ERR "btrfs: dup for data is not allowed\n");
  2699. ret = -EINVAL;
  2700. goto out;
  2701. }
  2702. /* allow to reduce meta or sys integrity only if force set */
  2703. allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
  2704. BTRFS_BLOCK_GROUP_RAID10 |
  2705. BTRFS_BLOCK_GROUP_RAID5 |
  2706. BTRFS_BLOCK_GROUP_RAID6;
  2707. do {
  2708. seq = read_seqbegin(&fs_info->profiles_lock);
  2709. if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
  2710. (fs_info->avail_system_alloc_bits & allowed) &&
  2711. !(bctl->sys.target & allowed)) ||
  2712. ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
  2713. (fs_info->avail_metadata_alloc_bits & allowed) &&
  2714. !(bctl->meta.target & allowed))) {
  2715. if (bctl->flags & BTRFS_BALANCE_FORCE) {
  2716. printk(KERN_INFO "btrfs: force reducing metadata "
  2717. "integrity\n");
  2718. } else {
  2719. printk(KERN_ERR "btrfs: balance will reduce metadata "
  2720. "integrity, use force if you want this\n");
  2721. ret = -EINVAL;
  2722. goto out;
  2723. }
  2724. }
  2725. } while (read_seqretry(&fs_info->profiles_lock, seq));
  2726. if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
  2727. int num_tolerated_disk_barrier_failures;
  2728. u64 target = bctl->sys.target;
  2729. num_tolerated_disk_barrier_failures =
  2730. btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
  2731. if (num_tolerated_disk_barrier_failures > 0 &&
  2732. (target &
  2733. (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
  2734. BTRFS_AVAIL_ALLOC_BIT_SINGLE)))
  2735. num_tolerated_disk_barrier_failures = 0;
  2736. else if (num_tolerated_disk_barrier_failures > 1 &&
  2737. (target &
  2738. (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)))
  2739. num_tolerated_disk_barrier_failures = 1;
  2740. fs_info->num_tolerated_disk_barrier_failures =
  2741. num_tolerated_disk_barrier_failures;
  2742. }
  2743. ret = insert_balance_item(fs_info->tree_root, bctl);
  2744. if (ret && ret != -EEXIST)
  2745. goto out;
  2746. if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
  2747. BUG_ON(ret == -EEXIST);
  2748. set_balance_control(bctl);
  2749. } else {
  2750. BUG_ON(ret != -EEXIST);
  2751. spin_lock(&fs_info->balance_lock);
  2752. update_balance_args(bctl);
  2753. spin_unlock(&fs_info->balance_lock);
  2754. }
  2755. atomic_inc(&fs_info->balance_running);
  2756. mutex_unlock(&fs_info->balance_mutex);
  2757. ret = __btrfs_balance(fs_info);
  2758. mutex_lock(&fs_info->balance_mutex);
  2759. atomic_dec(&fs_info->balance_running);
  2760. if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
  2761. fs_info->num_tolerated_disk_barrier_failures =
  2762. btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
  2763. }
  2764. if (bargs) {
  2765. memset(bargs, 0, sizeof(*bargs));
  2766. update_ioctl_balance_args(fs_info, 0, bargs);
  2767. }
  2768. if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
  2769. balance_need_close(fs_info)) {
  2770. __cancel_balance(fs_info);
  2771. }
  2772. wake_up(&fs_info->balance_wait_q);
  2773. return ret;
  2774. out:
  2775. if (bctl->flags & BTRFS_BALANCE_RESUME)
  2776. __cancel_balance(fs_info);
  2777. else {
  2778. kfree(bctl);
  2779. atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
  2780. }
  2781. return ret;
  2782. }
  2783. static int balance_kthread(void *data)
  2784. {
  2785. struct btrfs_fs_info *fs_info = data;
  2786. int ret = 0;
  2787. mutex_lock(&fs_info->volume_mutex);
  2788. mutex_lock(&fs_info->balance_mutex);
  2789. if (fs_info->balance_ctl) {
  2790. printk(KERN_INFO "btrfs: continuing balance\n");
  2791. ret = btrfs_balance(fs_info->balance_ctl, NULL);
  2792. }
  2793. mutex_unlock(&fs_info->balance_mutex);
  2794. mutex_unlock(&fs_info->volume_mutex);
  2795. return ret;
  2796. }
  2797. int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
  2798. {
  2799. struct task_struct *tsk;
  2800. spin_lock(&fs_info->balance_lock);
  2801. if (!fs_info->balance_ctl) {
  2802. spin_unlock(&fs_info->balance_lock);
  2803. return 0;
  2804. }
  2805. spin_unlock(&fs_info->balance_lock);
  2806. if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
  2807. printk(KERN_INFO "btrfs: force skipping balance\n");
  2808. return 0;
  2809. }
  2810. tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
  2811. if (IS_ERR(tsk))
  2812. return PTR_ERR(tsk);
  2813. return 0;
  2814. }
  2815. int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
  2816. {
  2817. struct btrfs_balance_control *bctl;
  2818. struct btrfs_balance_item *item;
  2819. struct btrfs_disk_balance_args disk_bargs;
  2820. struct btrfs_path *path;
  2821. struct extent_buffer *leaf;
  2822. struct btrfs_key key;
  2823. int ret;
  2824. path = btrfs_alloc_path();
  2825. if (!path)
  2826. return -ENOMEM;
  2827. key.objectid = BTRFS_BALANCE_OBJECTID;
  2828. key.type = BTRFS_BALANCE_ITEM_KEY;
  2829. key.offset = 0;
  2830. ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
  2831. if (ret < 0)
  2832. goto out;
  2833. if (ret > 0) { /* ret = -ENOENT; */
  2834. ret = 0;
  2835. goto out;
  2836. }
  2837. bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
  2838. if (!bctl) {
  2839. ret = -ENOMEM;
  2840. goto out;
  2841. }
  2842. leaf = path->nodes[0];
  2843. item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
  2844. bctl->fs_info = fs_info;
  2845. bctl->flags = btrfs_balance_flags(leaf, item);
  2846. bctl->flags |= BTRFS_BALANCE_RESUME;
  2847. btrfs_balance_data(leaf, item, &disk_bargs);
  2848. btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
  2849. btrfs_balance_meta(leaf, item, &disk_bargs);
  2850. btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
  2851. btrfs_balance_sys(leaf, item, &disk_bargs);
  2852. btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
  2853. WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
  2854. mutex_lock(&fs_info->volume_mutex);
  2855. mutex_lock(&fs_info->balance_mutex);
  2856. set_balance_control(bctl);
  2857. mutex_unlock(&fs_info->balance_mutex);
  2858. mutex_unlock(&fs_info->volume_mutex);
  2859. out:
  2860. btrfs_free_path(path);
  2861. return ret;
  2862. }
  2863. int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
  2864. {
  2865. int ret = 0;
  2866. mutex_lock(&fs_info->balance_mutex);
  2867. if (!fs_info->balance_ctl) {
  2868. mutex_unlock(&fs_info->balance_mutex);
  2869. return -ENOTCONN;
  2870. }
  2871. if (atomic_read(&fs_info->balance_running)) {
  2872. atomic_inc(&fs_info->balance_pause_req);
  2873. mutex_unlock(&fs_info->balance_mutex);
  2874. wait_event(fs_info->balance_wait_q,
  2875. atomic_read(&fs_info->balance_running) == 0);
  2876. mutex_lock(&fs_info->balance_mutex);
  2877. /* we are good with balance_ctl ripped off from under us */
  2878. BUG_ON(atomic_read(&fs_info->balance_running));
  2879. atomic_dec(&fs_info->balance_pause_req);
  2880. } else {
  2881. ret = -ENOTCONN;
  2882. }
  2883. mutex_unlock(&fs_info->balance_mutex);
  2884. return ret;
  2885. }
  2886. int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
  2887. {
  2888. mutex_lock(&fs_info->balance_mutex);
  2889. if (!fs_info->balance_ctl) {
  2890. mutex_unlock(&fs_info->balance_mutex);
  2891. return -ENOTCONN;
  2892. }
  2893. atomic_inc(&fs_info->balance_cancel_req);
  2894. /*
  2895. * if we are running just wait and return, balance item is
  2896. * deleted in btrfs_balance in this case
  2897. */
  2898. if (atomic_read(&fs_info->balance_running)) {
  2899. mutex_unlock(&fs_info->balance_mutex);
  2900. wait_event(fs_info->balance_wait_q,
  2901. atomic_read(&fs_info->balance_running) == 0);
  2902. mutex_lock(&fs_info->balance_mutex);
  2903. } else {
  2904. /* __cancel_balance needs volume_mutex */
  2905. mutex_unlock(&fs_info->balance_mutex);
  2906. mutex_lock(&fs_info->volume_mutex);
  2907. mutex_lock(&fs_info->balance_mutex);
  2908. if (fs_info->balance_ctl)
  2909. __cancel_balance(fs_info);
  2910. mutex_unlock(&fs_info->volume_mutex);
  2911. }
  2912. BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
  2913. atomic_dec(&fs_info->balance_cancel_req);
  2914. mutex_unlock(&fs_info->balance_mutex);
  2915. return 0;
  2916. }
  2917. /*
  2918. * shrinking a device means finding all of the device extents past
  2919. * the new size, and then following the back refs to the chunks.
  2920. * The chunk relocation code actually frees the device extent
  2921. */
  2922. int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
  2923. {
  2924. struct btrfs_trans_handle *trans;
  2925. struct btrfs_root *root = device->dev_root;
  2926. struct btrfs_dev_extent *dev_extent = NULL;
  2927. struct btrfs_path *path;
  2928. u64 length;
  2929. u64 chunk_tree;
  2930. u64 chunk_objectid;
  2931. u64 chunk_offset;
  2932. int ret;
  2933. int slot;
  2934. int failed = 0;
  2935. bool retried = false;
  2936. struct extent_buffer *l;
  2937. struct btrfs_key key;
  2938. struct btrfs_super_block *super_copy = root->fs_info->super_copy;
  2939. u64 old_total = btrfs_super_total_bytes(super_copy);
  2940. u64 old_size = device->total_bytes;
  2941. u64 diff = device->total_bytes - new_size;
  2942. if (device->is_tgtdev_for_dev_replace)
  2943. return -EINVAL;
  2944. path = btrfs_alloc_path();
  2945. if (!path)
  2946. return -ENOMEM;
  2947. path->reada = 2;
  2948. lock_chunks(root);
  2949. device->total_bytes = new_size;
  2950. if (device->writeable) {
  2951. device->fs_devices->total_rw_bytes -= diff;
  2952. spin_lock(&root->fs_info->free_chunk_lock);
  2953. root->fs_info->free_chunk_space -= diff;
  2954. spin_unlock(&root->fs_info->free_chunk_lock);
  2955. }
  2956. unlock_chunks(root);
  2957. again:
  2958. key.objectid = device->devid;
  2959. key.offset = (u64)-1;
  2960. key.type = BTRFS_DEV_EXTENT_KEY;
  2961. do {
  2962. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2963. if (ret < 0)
  2964. goto done;
  2965. ret = btrfs_previous_item(root, path, 0, key.type);
  2966. if (ret < 0)
  2967. goto done;
  2968. if (ret) {
  2969. ret = 0;
  2970. btrfs_release_path(path);
  2971. break;
  2972. }
  2973. l = path->nodes[0];
  2974. slot = path->slots[0];
  2975. btrfs_item_key_to_cpu(l, &key, path->slots[0]);
  2976. if (key.objectid != device->devid) {
  2977. btrfs_release_path(path);
  2978. break;
  2979. }
  2980. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  2981. length = btrfs_dev_extent_length(l, dev_extent);
  2982. if (key.offset + length <= new_size) {
  2983. btrfs_release_path(path);
  2984. break;
  2985. }
  2986. chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
  2987. chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
  2988. chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
  2989. btrfs_release_path(path);
  2990. ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
  2991. chunk_offset);
  2992. if (ret && ret != -ENOSPC)
  2993. goto done;
  2994. if (ret == -ENOSPC)
  2995. failed++;
  2996. } while (key.offset-- > 0);
  2997. if (failed && !retried) {
  2998. failed = 0;
  2999. retried = true;
  3000. goto again;
  3001. } else if (failed && retried) {
  3002. ret = -ENOSPC;
  3003. lock_chunks(root);
  3004. device->total_bytes = old_size;
  3005. if (device->writeable)
  3006. device->fs_devices->total_rw_bytes += diff;
  3007. spin_lock(&root->fs_info->free_chunk_lock);
  3008. root->fs_info->free_chunk_space += diff;
  3009. spin_unlock(&root->fs_info->free_chunk_lock);
  3010. unlock_chunks(root);
  3011. goto done;
  3012. }
  3013. /* Shrinking succeeded, else we would be at "done". */
  3014. trans = btrfs_start_transaction(root, 0);
  3015. if (IS_ERR(trans)) {
  3016. ret = PTR_ERR(trans);
  3017. goto done;
  3018. }
  3019. lock_chunks(root);
  3020. device->disk_total_bytes = new_size;
  3021. /* Now btrfs_update_device() will change the on-disk size. */
  3022. ret = btrfs_update_device(trans, device);
  3023. if (ret) {
  3024. unlock_chunks(root);
  3025. btrfs_end_transaction(trans, root);
  3026. goto done;
  3027. }
  3028. WARN_ON(diff > old_total);
  3029. btrfs_set_super_total_bytes(super_copy, old_total - diff);
  3030. unlock_chunks(root);
  3031. btrfs_end_transaction(trans, root);
  3032. done:
  3033. btrfs_free_path(path);
  3034. return ret;
  3035. }
  3036. static int btrfs_add_system_chunk(struct btrfs_root *root,
  3037. struct btrfs_key *key,
  3038. struct btrfs_chunk *chunk, int item_size)
  3039. {
  3040. struct btrfs_super_block *super_copy = root->fs_info->super_copy;
  3041. struct btrfs_disk_key disk_key;
  3042. u32 array_size;
  3043. u8 *ptr;
  3044. array_size = btrfs_super_sys_array_size(super_copy);
  3045. if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
  3046. return -EFBIG;
  3047. ptr = super_copy->sys_chunk_array + array_size;
  3048. btrfs_cpu_key_to_disk(&disk_key, key);
  3049. memcpy(ptr, &disk_key, sizeof(disk_key));
  3050. ptr += sizeof(disk_key);
  3051. memcpy(ptr, chunk, item_size);
  3052. item_size += sizeof(disk_key);
  3053. btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
  3054. return 0;
  3055. }
  3056. /*
  3057. * sort the devices in descending order by max_avail, total_avail
  3058. */
  3059. static int btrfs_cmp_device_info(const void *a, const void *b)
  3060. {
  3061. const struct btrfs_device_info *di_a = a;
  3062. const struct btrfs_device_info *di_b = b;
  3063. if (di_a->max_avail > di_b->max_avail)
  3064. return -1;
  3065. if (di_a->max_avail < di_b->max_avail)
  3066. return 1;
  3067. if (di_a->total_avail > di_b->total_avail)
  3068. return -1;
  3069. if (di_a->total_avail < di_b->total_avail)
  3070. return 1;
  3071. return 0;
  3072. }
  3073. static struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
  3074. [BTRFS_RAID_RAID10] = {
  3075. .sub_stripes = 2,
  3076. .dev_stripes = 1,
  3077. .devs_max = 0, /* 0 == as many as possible */
  3078. .devs_min = 4,
  3079. .devs_increment = 2,
  3080. .ncopies = 2,
  3081. },
  3082. [BTRFS_RAID_RAID1] = {
  3083. .sub_stripes = 1,
  3084. .dev_stripes = 1,
  3085. .devs_max = 2,
  3086. .devs_min = 2,
  3087. .devs_increment = 2,
  3088. .ncopies = 2,
  3089. },
  3090. [BTRFS_RAID_DUP] = {
  3091. .sub_stripes = 1,
  3092. .dev_stripes = 2,
  3093. .devs_max = 1,
  3094. .devs_min = 1,
  3095. .devs_increment = 1,
  3096. .ncopies = 2,
  3097. },
  3098. [BTRFS_RAID_RAID0] = {
  3099. .sub_stripes = 1,
  3100. .dev_stripes = 1,
  3101. .devs_max = 0,
  3102. .devs_min = 2,
  3103. .devs_increment = 1,
  3104. .ncopies = 1,
  3105. },
  3106. [BTRFS_RAID_SINGLE] = {
  3107. .sub_stripes = 1,
  3108. .dev_stripes = 1,
  3109. .devs_max = 1,
  3110. .devs_min = 1,
  3111. .devs_increment = 1,
  3112. .ncopies = 1,
  3113. },
  3114. [BTRFS_RAID_RAID5] = {
  3115. .sub_stripes = 1,
  3116. .dev_stripes = 1,
  3117. .devs_max = 0,
  3118. .devs_min = 2,
  3119. .devs_increment = 1,
  3120. .ncopies = 2,
  3121. },
  3122. [BTRFS_RAID_RAID6] = {
  3123. .sub_stripes = 1,
  3124. .dev_stripes = 1,
  3125. .devs_max = 0,
  3126. .devs_min = 3,
  3127. .devs_increment = 1,
  3128. .ncopies = 3,
  3129. },
  3130. };
  3131. static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
  3132. {
  3133. /* TODO allow them to set a preferred stripe size */
  3134. return 64 * 1024;
  3135. }
  3136. static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
  3137. {
  3138. if (!(type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)))
  3139. return;
  3140. btrfs_set_fs_incompat(info, RAID56);
  3141. }
  3142. static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
  3143. struct btrfs_root *extent_root,
  3144. struct map_lookup **map_ret,
  3145. u64 *num_bytes_out, u64 *stripe_size_out,
  3146. u64 start, u64 type)
  3147. {
  3148. struct btrfs_fs_info *info = extent_root->fs_info;
  3149. struct btrfs_fs_devices *fs_devices = info->fs_devices;
  3150. struct list_head *cur;
  3151. struct map_lookup *map = NULL;
  3152. struct extent_map_tree *em_tree;
  3153. struct extent_map *em;
  3154. struct btrfs_device_info *devices_info = NULL;
  3155. u64 total_avail;
  3156. int num_stripes; /* total number of stripes to allocate */
  3157. int data_stripes; /* number of stripes that count for
  3158. block group size */
  3159. int sub_stripes; /* sub_stripes info for map */
  3160. int dev_stripes; /* stripes per dev */
  3161. int devs_max; /* max devs to use */
  3162. int devs_min; /* min devs needed */
  3163. int devs_increment; /* ndevs has to be a multiple of this */
  3164. int ncopies; /* how many copies to data has */
  3165. int ret;
  3166. u64 max_stripe_size;
  3167. u64 max_chunk_size;
  3168. u64 stripe_size;
  3169. u64 num_bytes;
  3170. u64 raid_stripe_len = BTRFS_STRIPE_LEN;
  3171. int ndevs;
  3172. int i;
  3173. int j;
  3174. int index;
  3175. BUG_ON(!alloc_profile_is_valid(type, 0));
  3176. if (list_empty(&fs_devices->alloc_list))
  3177. return -ENOSPC;
  3178. index = __get_raid_index(type);
  3179. sub_stripes = btrfs_raid_array[index].sub_stripes;
  3180. dev_stripes = btrfs_raid_array[index].dev_stripes;
  3181. devs_max = btrfs_raid_array[index].devs_max;
  3182. devs_min = btrfs_raid_array[index].devs_min;
  3183. devs_increment = btrfs_raid_array[index].devs_increment;
  3184. ncopies = btrfs_raid_array[index].ncopies;
  3185. if (type & BTRFS_BLOCK_GROUP_DATA) {
  3186. max_stripe_size = 1024 * 1024 * 1024;
  3187. max_chunk_size = 10 * max_stripe_size;
  3188. } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
  3189. /* for larger filesystems, use larger metadata chunks */
  3190. if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
  3191. max_stripe_size = 1024 * 1024 * 1024;
  3192. else
  3193. max_stripe_size = 256 * 1024 * 1024;
  3194. max_chunk_size = max_stripe_size;
  3195. } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
  3196. max_stripe_size = 32 * 1024 * 1024;
  3197. max_chunk_size = 2 * max_stripe_size;
  3198. } else {
  3199. printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
  3200. type);
  3201. BUG_ON(1);
  3202. }
  3203. /* we don't want a chunk larger than 10% of writeable space */
  3204. max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
  3205. max_chunk_size);
  3206. devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
  3207. GFP_NOFS);
  3208. if (!devices_info)
  3209. return -ENOMEM;
  3210. cur = fs_devices->alloc_list.next;
  3211. /*
  3212. * in the first pass through the devices list, we gather information
  3213. * about the available holes on each device.
  3214. */
  3215. ndevs = 0;
  3216. while (cur != &fs_devices->alloc_list) {
  3217. struct btrfs_device *device;
  3218. u64 max_avail;
  3219. u64 dev_offset;
  3220. device = list_entry(cur, struct btrfs_device, dev_alloc_list);
  3221. cur = cur->next;
  3222. if (!device->writeable) {
  3223. WARN(1, KERN_ERR
  3224. "btrfs: read-only device in alloc_list\n");
  3225. continue;
  3226. }
  3227. if (!device->in_fs_metadata ||
  3228. device->is_tgtdev_for_dev_replace)
  3229. continue;
  3230. if (device->total_bytes > device->bytes_used)
  3231. total_avail = device->total_bytes - device->bytes_used;
  3232. else
  3233. total_avail = 0;
  3234. /* If there is no space on this device, skip it. */
  3235. if (total_avail == 0)
  3236. continue;
  3237. ret = find_free_dev_extent(device,
  3238. max_stripe_size * dev_stripes,
  3239. &dev_offset, &max_avail);
  3240. if (ret && ret != -ENOSPC)
  3241. goto error;
  3242. if (ret == 0)
  3243. max_avail = max_stripe_size * dev_stripes;
  3244. if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
  3245. continue;
  3246. if (ndevs == fs_devices->rw_devices) {
  3247. WARN(1, "%s: found more than %llu devices\n",
  3248. __func__, fs_devices->rw_devices);
  3249. break;
  3250. }
  3251. devices_info[ndevs].dev_offset = dev_offset;
  3252. devices_info[ndevs].max_avail = max_avail;
  3253. devices_info[ndevs].total_avail = total_avail;
  3254. devices_info[ndevs].dev = device;
  3255. ++ndevs;
  3256. }
  3257. /*
  3258. * now sort the devices by hole size / available space
  3259. */
  3260. sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
  3261. btrfs_cmp_device_info, NULL);
  3262. /* round down to number of usable stripes */
  3263. ndevs -= ndevs % devs_increment;
  3264. if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
  3265. ret = -ENOSPC;
  3266. goto error;
  3267. }
  3268. if (devs_max && ndevs > devs_max)
  3269. ndevs = devs_max;
  3270. /*
  3271. * the primary goal is to maximize the number of stripes, so use as many
  3272. * devices as possible, even if the stripes are not maximum sized.
  3273. */
  3274. stripe_size = devices_info[ndevs-1].max_avail;
  3275. num_stripes = ndevs * dev_stripes;
  3276. /*
  3277. * this will have to be fixed for RAID1 and RAID10 over
  3278. * more drives
  3279. */
  3280. data_stripes = num_stripes / ncopies;
  3281. if (type & BTRFS_BLOCK_GROUP_RAID5) {
  3282. raid_stripe_len = find_raid56_stripe_len(ndevs - 1,
  3283. btrfs_super_stripesize(info->super_copy));
  3284. data_stripes = num_stripes - 1;
  3285. }
  3286. if (type & BTRFS_BLOCK_GROUP_RAID6) {
  3287. raid_stripe_len = find_raid56_stripe_len(ndevs - 2,
  3288. btrfs_super_stripesize(info->super_copy));
  3289. data_stripes = num_stripes - 2;
  3290. }
  3291. /*
  3292. * Use the number of data stripes to figure out how big this chunk
  3293. * is really going to be in terms of logical address space,
  3294. * and compare that answer with the max chunk size
  3295. */
  3296. if (stripe_size * data_stripes > max_chunk_size) {
  3297. u64 mask = (1ULL << 24) - 1;
  3298. stripe_size = max_chunk_size;
  3299. do_div(stripe_size, data_stripes);
  3300. /* bump the answer up to a 16MB boundary */
  3301. stripe_size = (stripe_size + mask) & ~mask;
  3302. /* but don't go higher than the limits we found
  3303. * while searching for free extents
  3304. */
  3305. if (stripe_size > devices_info[ndevs-1].max_avail)
  3306. stripe_size = devices_info[ndevs-1].max_avail;
  3307. }
  3308. do_div(stripe_size, dev_stripes);
  3309. /* align to BTRFS_STRIPE_LEN */
  3310. do_div(stripe_size, raid_stripe_len);
  3311. stripe_size *= raid_stripe_len;
  3312. map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
  3313. if (!map) {
  3314. ret = -ENOMEM;
  3315. goto error;
  3316. }
  3317. map->num_stripes = num_stripes;
  3318. for (i = 0; i < ndevs; ++i) {
  3319. for (j = 0; j < dev_stripes; ++j) {
  3320. int s = i * dev_stripes + j;
  3321. map->stripes[s].dev = devices_info[i].dev;
  3322. map->stripes[s].physical = devices_info[i].dev_offset +
  3323. j * stripe_size;
  3324. }
  3325. }
  3326. map->sector_size = extent_root->sectorsize;
  3327. map->stripe_len = raid_stripe_len;
  3328. map->io_align = raid_stripe_len;
  3329. map->io_width = raid_stripe_len;
  3330. map->type = type;
  3331. map->sub_stripes = sub_stripes;
  3332. *map_ret = map;
  3333. num_bytes = stripe_size * data_stripes;
  3334. *stripe_size_out = stripe_size;
  3335. *num_bytes_out = num_bytes;
  3336. trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
  3337. em = alloc_extent_map();
  3338. if (!em) {
  3339. ret = -ENOMEM;
  3340. goto error;
  3341. }
  3342. em->bdev = (struct block_device *)map;
  3343. em->start = start;
  3344. em->len = num_bytes;
  3345. em->block_start = 0;
  3346. em->block_len = em->len;
  3347. em_tree = &extent_root->fs_info->mapping_tree.map_tree;
  3348. write_lock(&em_tree->lock);
  3349. ret = add_extent_mapping(em_tree, em, 0);
  3350. write_unlock(&em_tree->lock);
  3351. if (ret) {
  3352. free_extent_map(em);
  3353. goto error;
  3354. }
  3355. for (i = 0; i < map->num_stripes; ++i) {
  3356. struct btrfs_device *device;
  3357. u64 dev_offset;
  3358. device = map->stripes[i].dev;
  3359. dev_offset = map->stripes[i].physical;
  3360. ret = btrfs_alloc_dev_extent(trans, device,
  3361. info->chunk_root->root_key.objectid,
  3362. BTRFS_FIRST_CHUNK_TREE_OBJECTID,
  3363. start, dev_offset, stripe_size);
  3364. if (ret)
  3365. goto error_dev_extent;
  3366. }
  3367. ret = btrfs_make_block_group(trans, extent_root, 0, type,
  3368. BTRFS_FIRST_CHUNK_TREE_OBJECTID,
  3369. start, num_bytes);
  3370. if (ret) {
  3371. i = map->num_stripes - 1;
  3372. goto error_dev_extent;
  3373. }
  3374. free_extent_map(em);
  3375. check_raid56_incompat_flag(extent_root->fs_info, type);
  3376. kfree(devices_info);
  3377. return 0;
  3378. error_dev_extent:
  3379. for (; i >= 0; i--) {
  3380. struct btrfs_device *device;
  3381. int err;
  3382. device = map->stripes[i].dev;
  3383. err = btrfs_free_dev_extent(trans, device, start);
  3384. if (err) {
  3385. btrfs_abort_transaction(trans, extent_root, err);
  3386. break;
  3387. }
  3388. }
  3389. write_lock(&em_tree->lock);
  3390. remove_extent_mapping(em_tree, em);
  3391. write_unlock(&em_tree->lock);
  3392. /* One for our allocation */
  3393. free_extent_map(em);
  3394. /* One for the tree reference */
  3395. free_extent_map(em);
  3396. error:
  3397. kfree(map);
  3398. kfree(devices_info);
  3399. return ret;
  3400. }
  3401. static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
  3402. struct btrfs_root *extent_root,
  3403. struct map_lookup *map, u64 chunk_offset,
  3404. u64 chunk_size, u64 stripe_size)
  3405. {
  3406. u64 dev_offset;
  3407. struct btrfs_key key;
  3408. struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
  3409. struct btrfs_device *device;
  3410. struct btrfs_chunk *chunk;
  3411. struct btrfs_stripe *stripe;
  3412. size_t item_size = btrfs_chunk_item_size(map->num_stripes);
  3413. int index = 0;
  3414. int ret;
  3415. chunk = kzalloc(item_size, GFP_NOFS);
  3416. if (!chunk)
  3417. return -ENOMEM;
  3418. index = 0;
  3419. while (index < map->num_stripes) {
  3420. device = map->stripes[index].dev;
  3421. device->bytes_used += stripe_size;
  3422. ret = btrfs_update_device(trans, device);
  3423. if (ret)
  3424. goto out_free;
  3425. index++;
  3426. }
  3427. spin_lock(&extent_root->fs_info->free_chunk_lock);
  3428. extent_root->fs_info->free_chunk_space -= (stripe_size *
  3429. map->num_stripes);
  3430. spin_unlock(&extent_root->fs_info->free_chunk_lock);
  3431. index = 0;
  3432. stripe = &chunk->stripe;
  3433. while (index < map->num_stripes) {
  3434. device = map->stripes[index].dev;
  3435. dev_offset = map->stripes[index].physical;
  3436. btrfs_set_stack_stripe_devid(stripe, device->devid);
  3437. btrfs_set_stack_stripe_offset(stripe, dev_offset);
  3438. memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
  3439. stripe++;
  3440. index++;
  3441. }
  3442. btrfs_set_stack_chunk_length(chunk, chunk_size);
  3443. btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
  3444. btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
  3445. btrfs_set_stack_chunk_type(chunk, map->type);
  3446. btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
  3447. btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
  3448. btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
  3449. btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
  3450. btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
  3451. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  3452. key.type = BTRFS_CHUNK_ITEM_KEY;
  3453. key.offset = chunk_offset;
  3454. ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
  3455. if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
  3456. /*
  3457. * TODO: Cleanup of inserted chunk root in case of
  3458. * failure.
  3459. */
  3460. ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
  3461. item_size);
  3462. }
  3463. out_free:
  3464. kfree(chunk);
  3465. return ret;
  3466. }
  3467. /*
  3468. * Chunk allocation falls into two parts. The first part does works
  3469. * that make the new allocated chunk useable, but not do any operation
  3470. * that modifies the chunk tree. The second part does the works that
  3471. * require modifying the chunk tree. This division is important for the
  3472. * bootstrap process of adding storage to a seed btrfs.
  3473. */
  3474. int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
  3475. struct btrfs_root *extent_root, u64 type)
  3476. {
  3477. u64 chunk_offset;
  3478. u64 chunk_size;
  3479. u64 stripe_size;
  3480. struct map_lookup *map;
  3481. struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
  3482. int ret;
  3483. ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
  3484. &chunk_offset);
  3485. if (ret)
  3486. return ret;
  3487. ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
  3488. &stripe_size, chunk_offset, type);
  3489. if (ret)
  3490. return ret;
  3491. ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
  3492. chunk_size, stripe_size);
  3493. if (ret)
  3494. return ret;
  3495. return 0;
  3496. }
  3497. static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
  3498. struct btrfs_root *root,
  3499. struct btrfs_device *device)
  3500. {
  3501. u64 chunk_offset;
  3502. u64 sys_chunk_offset;
  3503. u64 chunk_size;
  3504. u64 sys_chunk_size;
  3505. u64 stripe_size;
  3506. u64 sys_stripe_size;
  3507. u64 alloc_profile;
  3508. struct map_lookup *map;
  3509. struct map_lookup *sys_map;
  3510. struct btrfs_fs_info *fs_info = root->fs_info;
  3511. struct btrfs_root *extent_root = fs_info->extent_root;
  3512. int ret;
  3513. ret = find_next_chunk(fs_info->chunk_root,
  3514. BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
  3515. if (ret)
  3516. return ret;
  3517. alloc_profile = btrfs_get_alloc_profile(extent_root, 0);
  3518. ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
  3519. &stripe_size, chunk_offset, alloc_profile);
  3520. if (ret)
  3521. return ret;
  3522. sys_chunk_offset = chunk_offset + chunk_size;
  3523. alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0);
  3524. ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
  3525. &sys_chunk_size, &sys_stripe_size,
  3526. sys_chunk_offset, alloc_profile);
  3527. if (ret) {
  3528. btrfs_abort_transaction(trans, root, ret);
  3529. goto out;
  3530. }
  3531. ret = btrfs_add_device(trans, fs_info->chunk_root, device);
  3532. if (ret) {
  3533. btrfs_abort_transaction(trans, root, ret);
  3534. goto out;
  3535. }
  3536. /*
  3537. * Modifying chunk tree needs allocating new blocks from both
  3538. * system block group and metadata block group. So we only can
  3539. * do operations require modifying the chunk tree after both
  3540. * block groups were created.
  3541. */
  3542. ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
  3543. chunk_size, stripe_size);
  3544. if (ret) {
  3545. btrfs_abort_transaction(trans, root, ret);
  3546. goto out;
  3547. }
  3548. ret = __finish_chunk_alloc(trans, extent_root, sys_map,
  3549. sys_chunk_offset, sys_chunk_size,
  3550. sys_stripe_size);
  3551. if (ret)
  3552. btrfs_abort_transaction(trans, root, ret);
  3553. out:
  3554. return ret;
  3555. }
  3556. int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
  3557. {
  3558. struct extent_map *em;
  3559. struct map_lookup *map;
  3560. struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
  3561. int readonly = 0;
  3562. int i;
  3563. read_lock(&map_tree->map_tree.lock);
  3564. em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
  3565. read_unlock(&map_tree->map_tree.lock);
  3566. if (!em)
  3567. return 1;
  3568. if (btrfs_test_opt(root, DEGRADED)) {
  3569. free_extent_map(em);
  3570. return 0;
  3571. }
  3572. map = (struct map_lookup *)em->bdev;
  3573. for (i = 0; i < map->num_stripes; i++) {
  3574. if (!map->stripes[i].dev->writeable) {
  3575. readonly = 1;
  3576. break;
  3577. }
  3578. }
  3579. free_extent_map(em);
  3580. return readonly;
  3581. }
  3582. void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
  3583. {
  3584. extent_map_tree_init(&tree->map_tree);
  3585. }
  3586. void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
  3587. {
  3588. struct extent_map *em;
  3589. while (1) {
  3590. write_lock(&tree->map_tree.lock);
  3591. em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
  3592. if (em)
  3593. remove_extent_mapping(&tree->map_tree, em);
  3594. write_unlock(&tree->map_tree.lock);
  3595. if (!em)
  3596. break;
  3597. kfree(em->bdev);
  3598. /* once for us */
  3599. free_extent_map(em);
  3600. /* once for the tree */
  3601. free_extent_map(em);
  3602. }
  3603. }
  3604. int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
  3605. {
  3606. struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
  3607. struct extent_map *em;
  3608. struct map_lookup *map;
  3609. struct extent_map_tree *em_tree = &map_tree->map_tree;
  3610. int ret;
  3611. read_lock(&em_tree->lock);
  3612. em = lookup_extent_mapping(em_tree, logical, len);
  3613. read_unlock(&em_tree->lock);
  3614. /*
  3615. * We could return errors for these cases, but that could get ugly and
  3616. * we'd probably do the same thing which is just not do anything else
  3617. * and exit, so return 1 so the callers don't try to use other copies.
  3618. */
  3619. if (!em) {
  3620. btrfs_emerg(fs_info, "No mapping for %Lu-%Lu\n", logical,
  3621. logical+len);
  3622. return 1;
  3623. }
  3624. if (em->start > logical || em->start + em->len < logical) {
  3625. btrfs_emerg(fs_info, "Invalid mapping for %Lu-%Lu, got "
  3626. "%Lu-%Lu\n", logical, logical+len, em->start,
  3627. em->start + em->len);
  3628. return 1;
  3629. }
  3630. map = (struct map_lookup *)em->bdev;
  3631. if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
  3632. ret = map->num_stripes;
  3633. else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
  3634. ret = map->sub_stripes;
  3635. else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
  3636. ret = 2;
  3637. else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
  3638. ret = 3;
  3639. else
  3640. ret = 1;
  3641. free_extent_map(em);
  3642. btrfs_dev_replace_lock(&fs_info->dev_replace);
  3643. if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))
  3644. ret++;
  3645. btrfs_dev_replace_unlock(&fs_info->dev_replace);
  3646. return ret;
  3647. }
  3648. unsigned long btrfs_full_stripe_len(struct btrfs_root *root,
  3649. struct btrfs_mapping_tree *map_tree,
  3650. u64 logical)
  3651. {
  3652. struct extent_map *em;
  3653. struct map_lookup *map;
  3654. struct extent_map_tree *em_tree = &map_tree->map_tree;
  3655. unsigned long len = root->sectorsize;
  3656. read_lock(&em_tree->lock);
  3657. em = lookup_extent_mapping(em_tree, logical, len);
  3658. read_unlock(&em_tree->lock);
  3659. BUG_ON(!em);
  3660. BUG_ON(em->start > logical || em->start + em->len < logical);
  3661. map = (struct map_lookup *)em->bdev;
  3662. if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
  3663. BTRFS_BLOCK_GROUP_RAID6)) {
  3664. len = map->stripe_len * nr_data_stripes(map);
  3665. }
  3666. free_extent_map(em);
  3667. return len;
  3668. }
  3669. int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree,
  3670. u64 logical, u64 len, int mirror_num)
  3671. {
  3672. struct extent_map *em;
  3673. struct map_lookup *map;
  3674. struct extent_map_tree *em_tree = &map_tree->map_tree;
  3675. int ret = 0;
  3676. read_lock(&em_tree->lock);
  3677. em = lookup_extent_mapping(em_tree, logical, len);
  3678. read_unlock(&em_tree->lock);
  3679. BUG_ON(!em);
  3680. BUG_ON(em->start > logical || em->start + em->len < logical);
  3681. map = (struct map_lookup *)em->bdev;
  3682. if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
  3683. BTRFS_BLOCK_GROUP_RAID6))
  3684. ret = 1;
  3685. free_extent_map(em);
  3686. return ret;
  3687. }
  3688. static int find_live_mirror(struct btrfs_fs_info *fs_info,
  3689. struct map_lookup *map, int first, int num,
  3690. int optimal, int dev_replace_is_ongoing)
  3691. {
  3692. int i;
  3693. int tolerance;
  3694. struct btrfs_device *srcdev;
  3695. if (dev_replace_is_ongoing &&
  3696. fs_info->dev_replace.cont_reading_from_srcdev_mode ==
  3697. BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
  3698. srcdev = fs_info->dev_replace.srcdev;
  3699. else
  3700. srcdev = NULL;
  3701. /*
  3702. * try to avoid the drive that is the source drive for a
  3703. * dev-replace procedure, only choose it if no other non-missing
  3704. * mirror is available
  3705. */
  3706. for (tolerance = 0; tolerance < 2; tolerance++) {
  3707. if (map->stripes[optimal].dev->bdev &&
  3708. (tolerance || map->stripes[optimal].dev != srcdev))
  3709. return optimal;
  3710. for (i = first; i < first + num; i++) {
  3711. if (map->stripes[i].dev->bdev &&
  3712. (tolerance || map->stripes[i].dev != srcdev))
  3713. return i;
  3714. }
  3715. }
  3716. /* we couldn't find one that doesn't fail. Just return something
  3717. * and the io error handling code will clean up eventually
  3718. */
  3719. return optimal;
  3720. }
  3721. static inline int parity_smaller(u64 a, u64 b)
  3722. {
  3723. return a > b;
  3724. }
  3725. /* Bubble-sort the stripe set to put the parity/syndrome stripes last */
  3726. static void sort_parity_stripes(struct btrfs_bio *bbio, u64 *raid_map)
  3727. {
  3728. struct btrfs_bio_stripe s;
  3729. int i;
  3730. u64 l;
  3731. int again = 1;
  3732. while (again) {
  3733. again = 0;
  3734. for (i = 0; i < bbio->num_stripes - 1; i++) {
  3735. if (parity_smaller(raid_map[i], raid_map[i+1])) {
  3736. s = bbio->stripes[i];
  3737. l = raid_map[i];
  3738. bbio->stripes[i] = bbio->stripes[i+1];
  3739. raid_map[i] = raid_map[i+1];
  3740. bbio->stripes[i+1] = s;
  3741. raid_map[i+1] = l;
  3742. again = 1;
  3743. }
  3744. }
  3745. }
  3746. }
  3747. static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
  3748. u64 logical, u64 *length,
  3749. struct btrfs_bio **bbio_ret,
  3750. int mirror_num, u64 **raid_map_ret)
  3751. {
  3752. struct extent_map *em;
  3753. struct map_lookup *map;
  3754. struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
  3755. struct extent_map_tree *em_tree = &map_tree->map_tree;
  3756. u64 offset;
  3757. u64 stripe_offset;
  3758. u64 stripe_end_offset;
  3759. u64 stripe_nr;
  3760. u64 stripe_nr_orig;
  3761. u64 stripe_nr_end;
  3762. u64 stripe_len;
  3763. u64 *raid_map = NULL;
  3764. int stripe_index;
  3765. int i;
  3766. int ret = 0;
  3767. int num_stripes;
  3768. int max_errors = 0;
  3769. struct btrfs_bio *bbio = NULL;
  3770. struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
  3771. int dev_replace_is_ongoing = 0;
  3772. int num_alloc_stripes;
  3773. int patch_the_first_stripe_for_dev_replace = 0;
  3774. u64 physical_to_patch_in_first_stripe = 0;
  3775. u64 raid56_full_stripe_start = (u64)-1;
  3776. read_lock(&em_tree->lock);
  3777. em = lookup_extent_mapping(em_tree, logical, *length);
  3778. read_unlock(&em_tree->lock);
  3779. if (!em) {
  3780. btrfs_crit(fs_info, "unable to find logical %llu len %llu",
  3781. (unsigned long long)logical,
  3782. (unsigned long long)*length);
  3783. return -EINVAL;
  3784. }
  3785. if (em->start > logical || em->start + em->len < logical) {
  3786. btrfs_crit(fs_info, "found a bad mapping, wanted %Lu, "
  3787. "found %Lu-%Lu\n", logical, em->start,
  3788. em->start + em->len);
  3789. return -EINVAL;
  3790. }
  3791. map = (struct map_lookup *)em->bdev;
  3792. offset = logical - em->start;
  3793. if (mirror_num > map->num_stripes)
  3794. mirror_num = 0;
  3795. stripe_len = map->stripe_len;
  3796. stripe_nr = offset;
  3797. /*
  3798. * stripe_nr counts the total number of stripes we have to stride
  3799. * to get to this block
  3800. */
  3801. do_div(stripe_nr, stripe_len);
  3802. stripe_offset = stripe_nr * stripe_len;
  3803. BUG_ON(offset < stripe_offset);
  3804. /* stripe_offset is the offset of this block in its stripe*/
  3805. stripe_offset = offset - stripe_offset;
  3806. /* if we're here for raid56, we need to know the stripe aligned start */
  3807. if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
  3808. unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
  3809. raid56_full_stripe_start = offset;
  3810. /* allow a write of a full stripe, but make sure we don't
  3811. * allow straddling of stripes
  3812. */
  3813. do_div(raid56_full_stripe_start, full_stripe_len);
  3814. raid56_full_stripe_start *= full_stripe_len;
  3815. }
  3816. if (rw & REQ_DISCARD) {
  3817. /* we don't discard raid56 yet */
  3818. if (map->type &
  3819. (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
  3820. ret = -EOPNOTSUPP;
  3821. goto out;
  3822. }
  3823. *length = min_t(u64, em->len - offset, *length);
  3824. } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
  3825. u64 max_len;
  3826. /* For writes to RAID[56], allow a full stripeset across all disks.
  3827. For other RAID types and for RAID[56] reads, just allow a single
  3828. stripe (on a single disk). */
  3829. if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6) &&
  3830. (rw & REQ_WRITE)) {
  3831. max_len = stripe_len * nr_data_stripes(map) -
  3832. (offset - raid56_full_stripe_start);
  3833. } else {
  3834. /* we limit the length of each bio to what fits in a stripe */
  3835. max_len = stripe_len - stripe_offset;
  3836. }
  3837. *length = min_t(u64, em->len - offset, max_len);
  3838. } else {
  3839. *length = em->len - offset;
  3840. }
  3841. /* This is for when we're called from btrfs_merge_bio_hook() and all
  3842. it cares about is the length */
  3843. if (!bbio_ret)
  3844. goto out;
  3845. btrfs_dev_replace_lock(dev_replace);
  3846. dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
  3847. if (!dev_replace_is_ongoing)
  3848. btrfs_dev_replace_unlock(dev_replace);
  3849. if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
  3850. !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) &&
  3851. dev_replace->tgtdev != NULL) {
  3852. /*
  3853. * in dev-replace case, for repair case (that's the only
  3854. * case where the mirror is selected explicitly when
  3855. * calling btrfs_map_block), blocks left of the left cursor
  3856. * can also be read from the target drive.
  3857. * For REQ_GET_READ_MIRRORS, the target drive is added as
  3858. * the last one to the array of stripes. For READ, it also
  3859. * needs to be supported using the same mirror number.
  3860. * If the requested block is not left of the left cursor,
  3861. * EIO is returned. This can happen because btrfs_num_copies()
  3862. * returns one more in the dev-replace case.
  3863. */
  3864. u64 tmp_length = *length;
  3865. struct btrfs_bio *tmp_bbio = NULL;
  3866. int tmp_num_stripes;
  3867. u64 srcdev_devid = dev_replace->srcdev->devid;
  3868. int index_srcdev = 0;
  3869. int found = 0;
  3870. u64 physical_of_found = 0;
  3871. ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS,
  3872. logical, &tmp_length, &tmp_bbio, 0, NULL);
  3873. if (ret) {
  3874. WARN_ON(tmp_bbio != NULL);
  3875. goto out;
  3876. }
  3877. tmp_num_stripes = tmp_bbio->num_stripes;
  3878. if (mirror_num > tmp_num_stripes) {
  3879. /*
  3880. * REQ_GET_READ_MIRRORS does not contain this
  3881. * mirror, that means that the requested area
  3882. * is not left of the left cursor
  3883. */
  3884. ret = -EIO;
  3885. kfree(tmp_bbio);
  3886. goto out;
  3887. }
  3888. /*
  3889. * process the rest of the function using the mirror_num
  3890. * of the source drive. Therefore look it up first.
  3891. * At the end, patch the device pointer to the one of the
  3892. * target drive.
  3893. */
  3894. for (i = 0; i < tmp_num_stripes; i++) {
  3895. if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) {
  3896. /*
  3897. * In case of DUP, in order to keep it
  3898. * simple, only add the mirror with the
  3899. * lowest physical address
  3900. */
  3901. if (found &&
  3902. physical_of_found <=
  3903. tmp_bbio->stripes[i].physical)
  3904. continue;
  3905. index_srcdev = i;
  3906. found = 1;
  3907. physical_of_found =
  3908. tmp_bbio->stripes[i].physical;
  3909. }
  3910. }
  3911. if (found) {
  3912. mirror_num = index_srcdev + 1;
  3913. patch_the_first_stripe_for_dev_replace = 1;
  3914. physical_to_patch_in_first_stripe = physical_of_found;
  3915. } else {
  3916. WARN_ON(1);
  3917. ret = -EIO;
  3918. kfree(tmp_bbio);
  3919. goto out;
  3920. }
  3921. kfree(tmp_bbio);
  3922. } else if (mirror_num > map->num_stripes) {
  3923. mirror_num = 0;
  3924. }
  3925. num_stripes = 1;
  3926. stripe_index = 0;
  3927. stripe_nr_orig = stripe_nr;
  3928. stripe_nr_end = ALIGN(offset + *length, map->stripe_len);
  3929. do_div(stripe_nr_end, map->stripe_len);
  3930. stripe_end_offset = stripe_nr_end * map->stripe_len -
  3931. (offset + *length);
  3932. if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
  3933. if (rw & REQ_DISCARD)
  3934. num_stripes = min_t(u64, map->num_stripes,
  3935. stripe_nr_end - stripe_nr_orig);
  3936. stripe_index = do_div(stripe_nr, map->num_stripes);
  3937. } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
  3938. if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))
  3939. num_stripes = map->num_stripes;
  3940. else if (mirror_num)
  3941. stripe_index = mirror_num - 1;
  3942. else {
  3943. stripe_index = find_live_mirror(fs_info, map, 0,
  3944. map->num_stripes,
  3945. current->pid % map->num_stripes,
  3946. dev_replace_is_ongoing);
  3947. mirror_num = stripe_index + 1;
  3948. }
  3949. } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
  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. mirror_num = 1;
  3956. }
  3957. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  3958. int factor = map->num_stripes / map->sub_stripes;
  3959. stripe_index = do_div(stripe_nr, factor);
  3960. stripe_index *= map->sub_stripes;
  3961. if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
  3962. num_stripes = map->sub_stripes;
  3963. else if (rw & REQ_DISCARD)
  3964. num_stripes = min_t(u64, map->sub_stripes *
  3965. (stripe_nr_end - stripe_nr_orig),
  3966. map->num_stripes);
  3967. else if (mirror_num)
  3968. stripe_index += mirror_num - 1;
  3969. else {
  3970. int old_stripe_index = stripe_index;
  3971. stripe_index = find_live_mirror(fs_info, map,
  3972. stripe_index,
  3973. map->sub_stripes, stripe_index +
  3974. current->pid % map->sub_stripes,
  3975. dev_replace_is_ongoing);
  3976. mirror_num = stripe_index - old_stripe_index + 1;
  3977. }
  3978. } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
  3979. BTRFS_BLOCK_GROUP_RAID6)) {
  3980. u64 tmp;
  3981. if (bbio_ret && ((rw & REQ_WRITE) || mirror_num > 1)
  3982. && raid_map_ret) {
  3983. int i, rot;
  3984. /* push stripe_nr back to the start of the full stripe */
  3985. stripe_nr = raid56_full_stripe_start;
  3986. do_div(stripe_nr, stripe_len);
  3987. stripe_index = do_div(stripe_nr, nr_data_stripes(map));
  3988. /* RAID[56] write or recovery. Return all stripes */
  3989. num_stripes = map->num_stripes;
  3990. max_errors = nr_parity_stripes(map);
  3991. raid_map = kmalloc(sizeof(u64) * num_stripes,
  3992. GFP_NOFS);
  3993. if (!raid_map) {
  3994. ret = -ENOMEM;
  3995. goto out;
  3996. }
  3997. /* Work out the disk rotation on this stripe-set */
  3998. tmp = stripe_nr;
  3999. rot = do_div(tmp, num_stripes);
  4000. /* Fill in the logical address of each stripe */
  4001. tmp = stripe_nr * nr_data_stripes(map);
  4002. for (i = 0; i < nr_data_stripes(map); i++)
  4003. raid_map[(i+rot) % num_stripes] =
  4004. em->start + (tmp + i) * map->stripe_len;
  4005. raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
  4006. if (map->type & BTRFS_BLOCK_GROUP_RAID6)
  4007. raid_map[(i+rot+1) % num_stripes] =
  4008. RAID6_Q_STRIPE;
  4009. *length = map->stripe_len;
  4010. stripe_index = 0;
  4011. stripe_offset = 0;
  4012. } else {
  4013. /*
  4014. * Mirror #0 or #1 means the original data block.
  4015. * Mirror #2 is RAID5 parity block.
  4016. * Mirror #3 is RAID6 Q block.
  4017. */
  4018. stripe_index = do_div(stripe_nr, nr_data_stripes(map));
  4019. if (mirror_num > 1)
  4020. stripe_index = nr_data_stripes(map) +
  4021. mirror_num - 2;
  4022. /* We distribute the parity blocks across stripes */
  4023. tmp = stripe_nr + stripe_index;
  4024. stripe_index = do_div(tmp, map->num_stripes);
  4025. }
  4026. } else {
  4027. /*
  4028. * after this do_div call, stripe_nr is the number of stripes
  4029. * on this device we have to walk to find the data, and
  4030. * stripe_index is the number of our device in the stripe array
  4031. */
  4032. stripe_index = do_div(stripe_nr, map->num_stripes);
  4033. mirror_num = stripe_index + 1;
  4034. }
  4035. BUG_ON(stripe_index >= map->num_stripes);
  4036. num_alloc_stripes = num_stripes;
  4037. if (dev_replace_is_ongoing) {
  4038. if (rw & (REQ_WRITE | REQ_DISCARD))
  4039. num_alloc_stripes <<= 1;
  4040. if (rw & REQ_GET_READ_MIRRORS)
  4041. num_alloc_stripes++;
  4042. }
  4043. bbio = kzalloc(btrfs_bio_size(num_alloc_stripes), GFP_NOFS);
  4044. if (!bbio) {
  4045. ret = -ENOMEM;
  4046. goto out;
  4047. }
  4048. atomic_set(&bbio->error, 0);
  4049. if (rw & REQ_DISCARD) {
  4050. int factor = 0;
  4051. int sub_stripes = 0;
  4052. u64 stripes_per_dev = 0;
  4053. u32 remaining_stripes = 0;
  4054. u32 last_stripe = 0;
  4055. if (map->type &
  4056. (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
  4057. if (map->type & BTRFS_BLOCK_GROUP_RAID0)
  4058. sub_stripes = 1;
  4059. else
  4060. sub_stripes = map->sub_stripes;
  4061. factor = map->num_stripes / sub_stripes;
  4062. stripes_per_dev = div_u64_rem(stripe_nr_end -
  4063. stripe_nr_orig,
  4064. factor,
  4065. &remaining_stripes);
  4066. div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
  4067. last_stripe *= sub_stripes;
  4068. }
  4069. for (i = 0; i < num_stripes; i++) {
  4070. bbio->stripes[i].physical =
  4071. map->stripes[stripe_index].physical +
  4072. stripe_offset + stripe_nr * map->stripe_len;
  4073. bbio->stripes[i].dev = map->stripes[stripe_index].dev;
  4074. if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
  4075. BTRFS_BLOCK_GROUP_RAID10)) {
  4076. bbio->stripes[i].length = stripes_per_dev *
  4077. map->stripe_len;
  4078. if (i / sub_stripes < remaining_stripes)
  4079. bbio->stripes[i].length +=
  4080. map->stripe_len;
  4081. /*
  4082. * Special for the first stripe and
  4083. * the last stripe:
  4084. *
  4085. * |-------|...|-------|
  4086. * |----------|
  4087. * off end_off
  4088. */
  4089. if (i < sub_stripes)
  4090. bbio->stripes[i].length -=
  4091. stripe_offset;
  4092. if (stripe_index >= last_stripe &&
  4093. stripe_index <= (last_stripe +
  4094. sub_stripes - 1))
  4095. bbio->stripes[i].length -=
  4096. stripe_end_offset;
  4097. if (i == sub_stripes - 1)
  4098. stripe_offset = 0;
  4099. } else
  4100. bbio->stripes[i].length = *length;
  4101. stripe_index++;
  4102. if (stripe_index == map->num_stripes) {
  4103. /* This could only happen for RAID0/10 */
  4104. stripe_index = 0;
  4105. stripe_nr++;
  4106. }
  4107. }
  4108. } else {
  4109. for (i = 0; i < num_stripes; i++) {
  4110. bbio->stripes[i].physical =
  4111. map->stripes[stripe_index].physical +
  4112. stripe_offset +
  4113. stripe_nr * map->stripe_len;
  4114. bbio->stripes[i].dev =
  4115. map->stripes[stripe_index].dev;
  4116. stripe_index++;
  4117. }
  4118. }
  4119. if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) {
  4120. if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
  4121. BTRFS_BLOCK_GROUP_RAID10 |
  4122. BTRFS_BLOCK_GROUP_RAID5 |
  4123. BTRFS_BLOCK_GROUP_DUP)) {
  4124. max_errors = 1;
  4125. } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
  4126. max_errors = 2;
  4127. }
  4128. }
  4129. if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) &&
  4130. dev_replace->tgtdev != NULL) {
  4131. int index_where_to_add;
  4132. u64 srcdev_devid = dev_replace->srcdev->devid;
  4133. /*
  4134. * duplicate the write operations while the dev replace
  4135. * procedure is running. Since the copying of the old disk
  4136. * to the new disk takes place at run time while the
  4137. * filesystem is mounted writable, the regular write
  4138. * operations to the old disk have to be duplicated to go
  4139. * to the new disk as well.
  4140. * Note that device->missing is handled by the caller, and
  4141. * that the write to the old disk is already set up in the
  4142. * stripes array.
  4143. */
  4144. index_where_to_add = num_stripes;
  4145. for (i = 0; i < num_stripes; i++) {
  4146. if (bbio->stripes[i].dev->devid == srcdev_devid) {
  4147. /* write to new disk, too */
  4148. struct btrfs_bio_stripe *new =
  4149. bbio->stripes + index_where_to_add;
  4150. struct btrfs_bio_stripe *old =
  4151. bbio->stripes + i;
  4152. new->physical = old->physical;
  4153. new->length = old->length;
  4154. new->dev = dev_replace->tgtdev;
  4155. index_where_to_add++;
  4156. max_errors++;
  4157. }
  4158. }
  4159. num_stripes = index_where_to_add;
  4160. } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) &&
  4161. dev_replace->tgtdev != NULL) {
  4162. u64 srcdev_devid = dev_replace->srcdev->devid;
  4163. int index_srcdev = 0;
  4164. int found = 0;
  4165. u64 physical_of_found = 0;
  4166. /*
  4167. * During the dev-replace procedure, the target drive can
  4168. * also be used to read data in case it is needed to repair
  4169. * a corrupt block elsewhere. This is possible if the
  4170. * requested area is left of the left cursor. In this area,
  4171. * the target drive is a full copy of the source drive.
  4172. */
  4173. for (i = 0; i < num_stripes; i++) {
  4174. if (bbio->stripes[i].dev->devid == srcdev_devid) {
  4175. /*
  4176. * In case of DUP, in order to keep it
  4177. * simple, only add the mirror with the
  4178. * lowest physical address
  4179. */
  4180. if (found &&
  4181. physical_of_found <=
  4182. bbio->stripes[i].physical)
  4183. continue;
  4184. index_srcdev = i;
  4185. found = 1;
  4186. physical_of_found = bbio->stripes[i].physical;
  4187. }
  4188. }
  4189. if (found) {
  4190. u64 length = map->stripe_len;
  4191. if (physical_of_found + length <=
  4192. dev_replace->cursor_left) {
  4193. struct btrfs_bio_stripe *tgtdev_stripe =
  4194. bbio->stripes + num_stripes;
  4195. tgtdev_stripe->physical = physical_of_found;
  4196. tgtdev_stripe->length =
  4197. bbio->stripes[index_srcdev].length;
  4198. tgtdev_stripe->dev = dev_replace->tgtdev;
  4199. num_stripes++;
  4200. }
  4201. }
  4202. }
  4203. *bbio_ret = bbio;
  4204. bbio->num_stripes = num_stripes;
  4205. bbio->max_errors = max_errors;
  4206. bbio->mirror_num = mirror_num;
  4207. /*
  4208. * this is the case that REQ_READ && dev_replace_is_ongoing &&
  4209. * mirror_num == num_stripes + 1 && dev_replace target drive is
  4210. * available as a mirror
  4211. */
  4212. if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
  4213. WARN_ON(num_stripes > 1);
  4214. bbio->stripes[0].dev = dev_replace->tgtdev;
  4215. bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
  4216. bbio->mirror_num = map->num_stripes + 1;
  4217. }
  4218. if (raid_map) {
  4219. sort_parity_stripes(bbio, raid_map);
  4220. *raid_map_ret = raid_map;
  4221. }
  4222. out:
  4223. if (dev_replace_is_ongoing)
  4224. btrfs_dev_replace_unlock(dev_replace);
  4225. free_extent_map(em);
  4226. return ret;
  4227. }
  4228. int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
  4229. u64 logical, u64 *length,
  4230. struct btrfs_bio **bbio_ret, int mirror_num)
  4231. {
  4232. return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
  4233. mirror_num, NULL);
  4234. }
  4235. int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
  4236. u64 chunk_start, u64 physical, u64 devid,
  4237. u64 **logical, int *naddrs, int *stripe_len)
  4238. {
  4239. struct extent_map_tree *em_tree = &map_tree->map_tree;
  4240. struct extent_map *em;
  4241. struct map_lookup *map;
  4242. u64 *buf;
  4243. u64 bytenr;
  4244. u64 length;
  4245. u64 stripe_nr;
  4246. u64 rmap_len;
  4247. int i, j, nr = 0;
  4248. read_lock(&em_tree->lock);
  4249. em = lookup_extent_mapping(em_tree, chunk_start, 1);
  4250. read_unlock(&em_tree->lock);
  4251. if (!em) {
  4252. printk(KERN_ERR "btrfs: couldn't find em for chunk %Lu\n",
  4253. chunk_start);
  4254. return -EIO;
  4255. }
  4256. if (em->start != chunk_start) {
  4257. printk(KERN_ERR "btrfs: bad chunk start, em=%Lu, wanted=%Lu\n",
  4258. em->start, chunk_start);
  4259. free_extent_map(em);
  4260. return -EIO;
  4261. }
  4262. map = (struct map_lookup *)em->bdev;
  4263. length = em->len;
  4264. rmap_len = map->stripe_len;
  4265. if (map->type & BTRFS_BLOCK_GROUP_RAID10)
  4266. do_div(length, map->num_stripes / map->sub_stripes);
  4267. else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
  4268. do_div(length, map->num_stripes);
  4269. else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
  4270. BTRFS_BLOCK_GROUP_RAID6)) {
  4271. do_div(length, nr_data_stripes(map));
  4272. rmap_len = map->stripe_len * nr_data_stripes(map);
  4273. }
  4274. buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
  4275. BUG_ON(!buf); /* -ENOMEM */
  4276. for (i = 0; i < map->num_stripes; i++) {
  4277. if (devid && map->stripes[i].dev->devid != devid)
  4278. continue;
  4279. if (map->stripes[i].physical > physical ||
  4280. map->stripes[i].physical + length <= physical)
  4281. continue;
  4282. stripe_nr = physical - map->stripes[i].physical;
  4283. do_div(stripe_nr, map->stripe_len);
  4284. if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  4285. stripe_nr = stripe_nr * map->num_stripes + i;
  4286. do_div(stripe_nr, map->sub_stripes);
  4287. } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
  4288. stripe_nr = stripe_nr * map->num_stripes + i;
  4289. } /* else if RAID[56], multiply by nr_data_stripes().
  4290. * Alternatively, just use rmap_len below instead of
  4291. * map->stripe_len */
  4292. bytenr = chunk_start + stripe_nr * rmap_len;
  4293. WARN_ON(nr >= map->num_stripes);
  4294. for (j = 0; j < nr; j++) {
  4295. if (buf[j] == bytenr)
  4296. break;
  4297. }
  4298. if (j == nr) {
  4299. WARN_ON(nr >= map->num_stripes);
  4300. buf[nr++] = bytenr;
  4301. }
  4302. }
  4303. *logical = buf;
  4304. *naddrs = nr;
  4305. *stripe_len = rmap_len;
  4306. free_extent_map(em);
  4307. return 0;
  4308. }
  4309. static void btrfs_end_bio(struct bio *bio, int err)
  4310. {
  4311. struct btrfs_bio *bbio = bio->bi_private;
  4312. int is_orig_bio = 0;
  4313. if (err) {
  4314. atomic_inc(&bbio->error);
  4315. if (err == -EIO || err == -EREMOTEIO) {
  4316. unsigned int stripe_index =
  4317. btrfs_io_bio(bio)->stripe_index;
  4318. struct btrfs_device *dev;
  4319. BUG_ON(stripe_index >= bbio->num_stripes);
  4320. dev = bbio->stripes[stripe_index].dev;
  4321. if (dev->bdev) {
  4322. if (bio->bi_rw & WRITE)
  4323. btrfs_dev_stat_inc(dev,
  4324. BTRFS_DEV_STAT_WRITE_ERRS);
  4325. else
  4326. btrfs_dev_stat_inc(dev,
  4327. BTRFS_DEV_STAT_READ_ERRS);
  4328. if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
  4329. btrfs_dev_stat_inc(dev,
  4330. BTRFS_DEV_STAT_FLUSH_ERRS);
  4331. btrfs_dev_stat_print_on_error(dev);
  4332. }
  4333. }
  4334. }
  4335. if (bio == bbio->orig_bio)
  4336. is_orig_bio = 1;
  4337. if (atomic_dec_and_test(&bbio->stripes_pending)) {
  4338. if (!is_orig_bio) {
  4339. bio_put(bio);
  4340. bio = bbio->orig_bio;
  4341. }
  4342. bio->bi_private = bbio->private;
  4343. bio->bi_end_io = bbio->end_io;
  4344. btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
  4345. /* only send an error to the higher layers if it is
  4346. * beyond the tolerance of the btrfs bio
  4347. */
  4348. if (atomic_read(&bbio->error) > bbio->max_errors) {
  4349. err = -EIO;
  4350. } else {
  4351. /*
  4352. * this bio is actually up to date, we didn't
  4353. * go over the max number of errors
  4354. */
  4355. set_bit(BIO_UPTODATE, &bio->bi_flags);
  4356. err = 0;
  4357. }
  4358. kfree(bbio);
  4359. bio_endio(bio, err);
  4360. } else if (!is_orig_bio) {
  4361. bio_put(bio);
  4362. }
  4363. }
  4364. struct async_sched {
  4365. struct bio *bio;
  4366. int rw;
  4367. struct btrfs_fs_info *info;
  4368. struct btrfs_work work;
  4369. };
  4370. /*
  4371. * see run_scheduled_bios for a description of why bios are collected for
  4372. * async submit.
  4373. *
  4374. * This will add one bio to the pending list for a device and make sure
  4375. * the work struct is scheduled.
  4376. */
  4377. static noinline void btrfs_schedule_bio(struct btrfs_root *root,
  4378. struct btrfs_device *device,
  4379. int rw, struct bio *bio)
  4380. {
  4381. int should_queue = 1;
  4382. struct btrfs_pending_bios *pending_bios;
  4383. if (device->missing || !device->bdev) {
  4384. bio_endio(bio, -EIO);
  4385. return;
  4386. }
  4387. /* don't bother with additional async steps for reads, right now */
  4388. if (!(rw & REQ_WRITE)) {
  4389. bio_get(bio);
  4390. btrfsic_submit_bio(rw, bio);
  4391. bio_put(bio);
  4392. return;
  4393. }
  4394. /*
  4395. * nr_async_bios allows us to reliably return congestion to the
  4396. * higher layers. Otherwise, the async bio makes it appear we have
  4397. * made progress against dirty pages when we've really just put it
  4398. * on a queue for later
  4399. */
  4400. atomic_inc(&root->fs_info->nr_async_bios);
  4401. WARN_ON(bio->bi_next);
  4402. bio->bi_next = NULL;
  4403. bio->bi_rw |= rw;
  4404. spin_lock(&device->io_lock);
  4405. if (bio->bi_rw & REQ_SYNC)
  4406. pending_bios = &device->pending_sync_bios;
  4407. else
  4408. pending_bios = &device->pending_bios;
  4409. if (pending_bios->tail)
  4410. pending_bios->tail->bi_next = bio;
  4411. pending_bios->tail = bio;
  4412. if (!pending_bios->head)
  4413. pending_bios->head = bio;
  4414. if (device->running_pending)
  4415. should_queue = 0;
  4416. spin_unlock(&device->io_lock);
  4417. if (should_queue)
  4418. btrfs_queue_worker(&root->fs_info->submit_workers,
  4419. &device->work);
  4420. }
  4421. static int bio_size_ok(struct block_device *bdev, struct bio *bio,
  4422. sector_t sector)
  4423. {
  4424. struct bio_vec *prev;
  4425. struct request_queue *q = bdev_get_queue(bdev);
  4426. unsigned short max_sectors = queue_max_sectors(q);
  4427. struct bvec_merge_data bvm = {
  4428. .bi_bdev = bdev,
  4429. .bi_sector = sector,
  4430. .bi_rw = bio->bi_rw,
  4431. };
  4432. if (bio->bi_vcnt == 0) {
  4433. WARN_ON(1);
  4434. return 1;
  4435. }
  4436. prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
  4437. if ((bio->bi_size >> 9) > max_sectors)
  4438. return 0;
  4439. if (!q->merge_bvec_fn)
  4440. return 1;
  4441. bvm.bi_size = bio->bi_size - prev->bv_len;
  4442. if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len)
  4443. return 0;
  4444. return 1;
  4445. }
  4446. static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
  4447. struct bio *bio, u64 physical, int dev_nr,
  4448. int rw, int async)
  4449. {
  4450. struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
  4451. bio->bi_private = bbio;
  4452. btrfs_io_bio(bio)->stripe_index = dev_nr;
  4453. bio->bi_end_io = btrfs_end_bio;
  4454. bio->bi_sector = physical >> 9;
  4455. #ifdef DEBUG
  4456. {
  4457. struct rcu_string *name;
  4458. rcu_read_lock();
  4459. name = rcu_dereference(dev->name);
  4460. pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
  4461. "(%s id %llu), size=%u\n", rw,
  4462. (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
  4463. name->str, dev->devid, bio->bi_size);
  4464. rcu_read_unlock();
  4465. }
  4466. #endif
  4467. bio->bi_bdev = dev->bdev;
  4468. if (async)
  4469. btrfs_schedule_bio(root, dev, rw, bio);
  4470. else
  4471. btrfsic_submit_bio(rw, bio);
  4472. }
  4473. static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
  4474. struct bio *first_bio, struct btrfs_device *dev,
  4475. int dev_nr, int rw, int async)
  4476. {
  4477. struct bio_vec *bvec = first_bio->bi_io_vec;
  4478. struct bio *bio;
  4479. int nr_vecs = bio_get_nr_vecs(dev->bdev);
  4480. u64 physical = bbio->stripes[dev_nr].physical;
  4481. again:
  4482. bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS);
  4483. if (!bio)
  4484. return -ENOMEM;
  4485. while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) {
  4486. if (bio_add_page(bio, bvec->bv_page, bvec->bv_len,
  4487. bvec->bv_offset) < bvec->bv_len) {
  4488. u64 len = bio->bi_size;
  4489. atomic_inc(&bbio->stripes_pending);
  4490. submit_stripe_bio(root, bbio, bio, physical, dev_nr,
  4491. rw, async);
  4492. physical += len;
  4493. goto again;
  4494. }
  4495. bvec++;
  4496. }
  4497. submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async);
  4498. return 0;
  4499. }
  4500. static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
  4501. {
  4502. atomic_inc(&bbio->error);
  4503. if (atomic_dec_and_test(&bbio->stripes_pending)) {
  4504. bio->bi_private = bbio->private;
  4505. bio->bi_end_io = bbio->end_io;
  4506. btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
  4507. bio->bi_sector = logical >> 9;
  4508. kfree(bbio);
  4509. bio_endio(bio, -EIO);
  4510. }
  4511. }
  4512. int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
  4513. int mirror_num, int async_submit)
  4514. {
  4515. struct btrfs_device *dev;
  4516. struct bio *first_bio = bio;
  4517. u64 logical = (u64)bio->bi_sector << 9;
  4518. u64 length = 0;
  4519. u64 map_length;
  4520. u64 *raid_map = NULL;
  4521. int ret;
  4522. int dev_nr = 0;
  4523. int total_devs = 1;
  4524. struct btrfs_bio *bbio = NULL;
  4525. length = bio->bi_size;
  4526. map_length = length;
  4527. ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio,
  4528. mirror_num, &raid_map);
  4529. if (ret) /* -ENOMEM */
  4530. return ret;
  4531. total_devs = bbio->num_stripes;
  4532. bbio->orig_bio = first_bio;
  4533. bbio->private = first_bio->bi_private;
  4534. bbio->end_io = first_bio->bi_end_io;
  4535. atomic_set(&bbio->stripes_pending, bbio->num_stripes);
  4536. if (raid_map) {
  4537. /* In this case, map_length has been set to the length of
  4538. a single stripe; not the whole write */
  4539. if (rw & WRITE) {
  4540. return raid56_parity_write(root, bio, bbio,
  4541. raid_map, map_length);
  4542. } else {
  4543. return raid56_parity_recover(root, bio, bbio,
  4544. raid_map, map_length,
  4545. mirror_num);
  4546. }
  4547. }
  4548. if (map_length < length) {
  4549. btrfs_crit(root->fs_info, "mapping failed logical %llu bio len %llu len %llu",
  4550. (unsigned long long)logical,
  4551. (unsigned long long)length,
  4552. (unsigned long long)map_length);
  4553. BUG();
  4554. }
  4555. while (dev_nr < total_devs) {
  4556. dev = bbio->stripes[dev_nr].dev;
  4557. if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
  4558. bbio_error(bbio, first_bio, logical);
  4559. dev_nr++;
  4560. continue;
  4561. }
  4562. /*
  4563. * Check and see if we're ok with this bio based on it's size
  4564. * and offset with the given device.
  4565. */
  4566. if (!bio_size_ok(dev->bdev, first_bio,
  4567. bbio->stripes[dev_nr].physical >> 9)) {
  4568. ret = breakup_stripe_bio(root, bbio, first_bio, dev,
  4569. dev_nr, rw, async_submit);
  4570. BUG_ON(ret);
  4571. dev_nr++;
  4572. continue;
  4573. }
  4574. if (dev_nr < total_devs - 1) {
  4575. bio = btrfs_bio_clone(first_bio, GFP_NOFS);
  4576. BUG_ON(!bio); /* -ENOMEM */
  4577. } else {
  4578. bio = first_bio;
  4579. }
  4580. submit_stripe_bio(root, bbio, bio,
  4581. bbio->stripes[dev_nr].physical, dev_nr, rw,
  4582. async_submit);
  4583. dev_nr++;
  4584. }
  4585. return 0;
  4586. }
  4587. struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
  4588. u8 *uuid, u8 *fsid)
  4589. {
  4590. struct btrfs_device *device;
  4591. struct btrfs_fs_devices *cur_devices;
  4592. cur_devices = fs_info->fs_devices;
  4593. while (cur_devices) {
  4594. if (!fsid ||
  4595. !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
  4596. device = __find_device(&cur_devices->devices,
  4597. devid, uuid);
  4598. if (device)
  4599. return device;
  4600. }
  4601. cur_devices = cur_devices->seed;
  4602. }
  4603. return NULL;
  4604. }
  4605. static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
  4606. u64 devid, u8 *dev_uuid)
  4607. {
  4608. struct btrfs_device *device;
  4609. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  4610. device = kzalloc(sizeof(*device), GFP_NOFS);
  4611. if (!device)
  4612. return NULL;
  4613. list_add(&device->dev_list,
  4614. &fs_devices->devices);
  4615. device->devid = devid;
  4616. device->work.func = pending_bios_fn;
  4617. device->fs_devices = fs_devices;
  4618. device->missing = 1;
  4619. fs_devices->num_devices++;
  4620. fs_devices->missing_devices++;
  4621. spin_lock_init(&device->io_lock);
  4622. INIT_LIST_HEAD(&device->dev_alloc_list);
  4623. memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
  4624. return device;
  4625. }
  4626. static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
  4627. struct extent_buffer *leaf,
  4628. struct btrfs_chunk *chunk)
  4629. {
  4630. struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
  4631. struct map_lookup *map;
  4632. struct extent_map *em;
  4633. u64 logical;
  4634. u64 length;
  4635. u64 devid;
  4636. u8 uuid[BTRFS_UUID_SIZE];
  4637. int num_stripes;
  4638. int ret;
  4639. int i;
  4640. logical = key->offset;
  4641. length = btrfs_chunk_length(leaf, chunk);
  4642. read_lock(&map_tree->map_tree.lock);
  4643. em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
  4644. read_unlock(&map_tree->map_tree.lock);
  4645. /* already mapped? */
  4646. if (em && em->start <= logical && em->start + em->len > logical) {
  4647. free_extent_map(em);
  4648. return 0;
  4649. } else if (em) {
  4650. free_extent_map(em);
  4651. }
  4652. em = alloc_extent_map();
  4653. if (!em)
  4654. return -ENOMEM;
  4655. num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
  4656. map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
  4657. if (!map) {
  4658. free_extent_map(em);
  4659. return -ENOMEM;
  4660. }
  4661. em->bdev = (struct block_device *)map;
  4662. em->start = logical;
  4663. em->len = length;
  4664. em->orig_start = 0;
  4665. em->block_start = 0;
  4666. em->block_len = em->len;
  4667. map->num_stripes = num_stripes;
  4668. map->io_width = btrfs_chunk_io_width(leaf, chunk);
  4669. map->io_align = btrfs_chunk_io_align(leaf, chunk);
  4670. map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
  4671. map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
  4672. map->type = btrfs_chunk_type(leaf, chunk);
  4673. map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
  4674. for (i = 0; i < num_stripes; i++) {
  4675. map->stripes[i].physical =
  4676. btrfs_stripe_offset_nr(leaf, chunk, i);
  4677. devid = btrfs_stripe_devid_nr(leaf, chunk, i);
  4678. read_extent_buffer(leaf, uuid, (unsigned long)
  4679. btrfs_stripe_dev_uuid_nr(chunk, i),
  4680. BTRFS_UUID_SIZE);
  4681. map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
  4682. uuid, NULL);
  4683. if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
  4684. kfree(map);
  4685. free_extent_map(em);
  4686. return -EIO;
  4687. }
  4688. if (!map->stripes[i].dev) {
  4689. map->stripes[i].dev =
  4690. add_missing_dev(root, devid, uuid);
  4691. if (!map->stripes[i].dev) {
  4692. kfree(map);
  4693. free_extent_map(em);
  4694. return -EIO;
  4695. }
  4696. }
  4697. map->stripes[i].dev->in_fs_metadata = 1;
  4698. }
  4699. write_lock(&map_tree->map_tree.lock);
  4700. ret = add_extent_mapping(&map_tree->map_tree, em, 0);
  4701. write_unlock(&map_tree->map_tree.lock);
  4702. BUG_ON(ret); /* Tree corruption */
  4703. free_extent_map(em);
  4704. return 0;
  4705. }
  4706. static void fill_device_from_item(struct extent_buffer *leaf,
  4707. struct btrfs_dev_item *dev_item,
  4708. struct btrfs_device *device)
  4709. {
  4710. unsigned long ptr;
  4711. device->devid = btrfs_device_id(leaf, dev_item);
  4712. device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
  4713. device->total_bytes = device->disk_total_bytes;
  4714. device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
  4715. device->type = btrfs_device_type(leaf, dev_item);
  4716. device->io_align = btrfs_device_io_align(leaf, dev_item);
  4717. device->io_width = btrfs_device_io_width(leaf, dev_item);
  4718. device->sector_size = btrfs_device_sector_size(leaf, dev_item);
  4719. WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
  4720. device->is_tgtdev_for_dev_replace = 0;
  4721. ptr = (unsigned long)btrfs_device_uuid(dev_item);
  4722. read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
  4723. }
  4724. static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
  4725. {
  4726. struct btrfs_fs_devices *fs_devices;
  4727. int ret;
  4728. BUG_ON(!mutex_is_locked(&uuid_mutex));
  4729. fs_devices = root->fs_info->fs_devices->seed;
  4730. while (fs_devices) {
  4731. if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
  4732. ret = 0;
  4733. goto out;
  4734. }
  4735. fs_devices = fs_devices->seed;
  4736. }
  4737. fs_devices = find_fsid(fsid);
  4738. if (!fs_devices) {
  4739. ret = -ENOENT;
  4740. goto out;
  4741. }
  4742. fs_devices = clone_fs_devices(fs_devices);
  4743. if (IS_ERR(fs_devices)) {
  4744. ret = PTR_ERR(fs_devices);
  4745. goto out;
  4746. }
  4747. ret = __btrfs_open_devices(fs_devices, FMODE_READ,
  4748. root->fs_info->bdev_holder);
  4749. if (ret) {
  4750. free_fs_devices(fs_devices);
  4751. goto out;
  4752. }
  4753. if (!fs_devices->seeding) {
  4754. __btrfs_close_devices(fs_devices);
  4755. free_fs_devices(fs_devices);
  4756. ret = -EINVAL;
  4757. goto out;
  4758. }
  4759. fs_devices->seed = root->fs_info->fs_devices->seed;
  4760. root->fs_info->fs_devices->seed = fs_devices;
  4761. out:
  4762. return ret;
  4763. }
  4764. static int read_one_dev(struct btrfs_root *root,
  4765. struct extent_buffer *leaf,
  4766. struct btrfs_dev_item *dev_item)
  4767. {
  4768. struct btrfs_device *device;
  4769. u64 devid;
  4770. int ret;
  4771. u8 fs_uuid[BTRFS_UUID_SIZE];
  4772. u8 dev_uuid[BTRFS_UUID_SIZE];
  4773. devid = btrfs_device_id(leaf, dev_item);
  4774. read_extent_buffer(leaf, dev_uuid,
  4775. (unsigned long)btrfs_device_uuid(dev_item),
  4776. BTRFS_UUID_SIZE);
  4777. read_extent_buffer(leaf, fs_uuid,
  4778. (unsigned long)btrfs_device_fsid(dev_item),
  4779. BTRFS_UUID_SIZE);
  4780. if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
  4781. ret = open_seed_devices(root, fs_uuid);
  4782. if (ret && !btrfs_test_opt(root, DEGRADED))
  4783. return ret;
  4784. }
  4785. device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid);
  4786. if (!device || !device->bdev) {
  4787. if (!btrfs_test_opt(root, DEGRADED))
  4788. return -EIO;
  4789. if (!device) {
  4790. btrfs_warn(root->fs_info, "devid %llu missing",
  4791. (unsigned long long)devid);
  4792. device = add_missing_dev(root, devid, dev_uuid);
  4793. if (!device)
  4794. return -ENOMEM;
  4795. } else if (!device->missing) {
  4796. /*
  4797. * this happens when a device that was properly setup
  4798. * in the device info lists suddenly goes bad.
  4799. * device->bdev is NULL, and so we have to set
  4800. * device->missing to one here
  4801. */
  4802. root->fs_info->fs_devices->missing_devices++;
  4803. device->missing = 1;
  4804. }
  4805. }
  4806. if (device->fs_devices != root->fs_info->fs_devices) {
  4807. BUG_ON(device->writeable);
  4808. if (device->generation !=
  4809. btrfs_device_generation(leaf, dev_item))
  4810. return -EINVAL;
  4811. }
  4812. fill_device_from_item(leaf, dev_item, device);
  4813. device->in_fs_metadata = 1;
  4814. if (device->writeable && !device->is_tgtdev_for_dev_replace) {
  4815. device->fs_devices->total_rw_bytes += device->total_bytes;
  4816. spin_lock(&root->fs_info->free_chunk_lock);
  4817. root->fs_info->free_chunk_space += device->total_bytes -
  4818. device->bytes_used;
  4819. spin_unlock(&root->fs_info->free_chunk_lock);
  4820. }
  4821. ret = 0;
  4822. return ret;
  4823. }
  4824. int btrfs_read_sys_array(struct btrfs_root *root)
  4825. {
  4826. struct btrfs_super_block *super_copy = root->fs_info->super_copy;
  4827. struct extent_buffer *sb;
  4828. struct btrfs_disk_key *disk_key;
  4829. struct btrfs_chunk *chunk;
  4830. u8 *ptr;
  4831. unsigned long sb_ptr;
  4832. int ret = 0;
  4833. u32 num_stripes;
  4834. u32 array_size;
  4835. u32 len = 0;
  4836. u32 cur;
  4837. struct btrfs_key key;
  4838. sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
  4839. BTRFS_SUPER_INFO_SIZE);
  4840. if (!sb)
  4841. return -ENOMEM;
  4842. btrfs_set_buffer_uptodate(sb);
  4843. btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
  4844. /*
  4845. * The sb extent buffer is artifical and just used to read the system array.
  4846. * btrfs_set_buffer_uptodate() call does not properly mark all it's
  4847. * pages up-to-date when the page is larger: extent does not cover the
  4848. * whole page and consequently check_page_uptodate does not find all
  4849. * the page's extents up-to-date (the hole beyond sb),
  4850. * write_extent_buffer then triggers a WARN_ON.
  4851. *
  4852. * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
  4853. * but sb spans only this function. Add an explicit SetPageUptodate call
  4854. * to silence the warning eg. on PowerPC 64.
  4855. */
  4856. if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
  4857. SetPageUptodate(sb->pages[0]);
  4858. write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
  4859. array_size = btrfs_super_sys_array_size(super_copy);
  4860. ptr = super_copy->sys_chunk_array;
  4861. sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
  4862. cur = 0;
  4863. while (cur < array_size) {
  4864. disk_key = (struct btrfs_disk_key *)ptr;
  4865. btrfs_disk_key_to_cpu(&key, disk_key);
  4866. len = sizeof(*disk_key); ptr += len;
  4867. sb_ptr += len;
  4868. cur += len;
  4869. if (key.type == BTRFS_CHUNK_ITEM_KEY) {
  4870. chunk = (struct btrfs_chunk *)sb_ptr;
  4871. ret = read_one_chunk(root, &key, sb, chunk);
  4872. if (ret)
  4873. break;
  4874. num_stripes = btrfs_chunk_num_stripes(sb, chunk);
  4875. len = btrfs_chunk_item_size(num_stripes);
  4876. } else {
  4877. ret = -EIO;
  4878. break;
  4879. }
  4880. ptr += len;
  4881. sb_ptr += len;
  4882. cur += len;
  4883. }
  4884. free_extent_buffer(sb);
  4885. return ret;
  4886. }
  4887. int btrfs_read_chunk_tree(struct btrfs_root *root)
  4888. {
  4889. struct btrfs_path *path;
  4890. struct extent_buffer *leaf;
  4891. struct btrfs_key key;
  4892. struct btrfs_key found_key;
  4893. int ret;
  4894. int slot;
  4895. root = root->fs_info->chunk_root;
  4896. path = btrfs_alloc_path();
  4897. if (!path)
  4898. return -ENOMEM;
  4899. mutex_lock(&uuid_mutex);
  4900. lock_chunks(root);
  4901. /* first we search for all of the device items, and then we
  4902. * read in all of the chunk items. This way we can create chunk
  4903. * mappings that reference all of the devices that are afound
  4904. */
  4905. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  4906. key.offset = 0;
  4907. key.type = 0;
  4908. again:
  4909. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  4910. if (ret < 0)
  4911. goto error;
  4912. while (1) {
  4913. leaf = path->nodes[0];
  4914. slot = path->slots[0];
  4915. if (slot >= btrfs_header_nritems(leaf)) {
  4916. ret = btrfs_next_leaf(root, path);
  4917. if (ret == 0)
  4918. continue;
  4919. if (ret < 0)
  4920. goto error;
  4921. break;
  4922. }
  4923. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  4924. if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
  4925. if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
  4926. break;
  4927. if (found_key.type == BTRFS_DEV_ITEM_KEY) {
  4928. struct btrfs_dev_item *dev_item;
  4929. dev_item = btrfs_item_ptr(leaf, slot,
  4930. struct btrfs_dev_item);
  4931. ret = read_one_dev(root, leaf, dev_item);
  4932. if (ret)
  4933. goto error;
  4934. }
  4935. } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
  4936. struct btrfs_chunk *chunk;
  4937. chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
  4938. ret = read_one_chunk(root, &found_key, leaf, chunk);
  4939. if (ret)
  4940. goto error;
  4941. }
  4942. path->slots[0]++;
  4943. }
  4944. if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
  4945. key.objectid = 0;
  4946. btrfs_release_path(path);
  4947. goto again;
  4948. }
  4949. ret = 0;
  4950. error:
  4951. unlock_chunks(root);
  4952. mutex_unlock(&uuid_mutex);
  4953. btrfs_free_path(path);
  4954. return ret;
  4955. }
  4956. void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
  4957. {
  4958. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  4959. struct btrfs_device *device;
  4960. mutex_lock(&fs_devices->device_list_mutex);
  4961. list_for_each_entry(device, &fs_devices->devices, dev_list)
  4962. device->dev_root = fs_info->dev_root;
  4963. mutex_unlock(&fs_devices->device_list_mutex);
  4964. }
  4965. static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
  4966. {
  4967. int i;
  4968. for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
  4969. btrfs_dev_stat_reset(dev, i);
  4970. }
  4971. int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
  4972. {
  4973. struct btrfs_key key;
  4974. struct btrfs_key found_key;
  4975. struct btrfs_root *dev_root = fs_info->dev_root;
  4976. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  4977. struct extent_buffer *eb;
  4978. int slot;
  4979. int ret = 0;
  4980. struct btrfs_device *device;
  4981. struct btrfs_path *path = NULL;
  4982. int i;
  4983. path = btrfs_alloc_path();
  4984. if (!path) {
  4985. ret = -ENOMEM;
  4986. goto out;
  4987. }
  4988. mutex_lock(&fs_devices->device_list_mutex);
  4989. list_for_each_entry(device, &fs_devices->devices, dev_list) {
  4990. int item_size;
  4991. struct btrfs_dev_stats_item *ptr;
  4992. key.objectid = 0;
  4993. key.type = BTRFS_DEV_STATS_KEY;
  4994. key.offset = device->devid;
  4995. ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
  4996. if (ret) {
  4997. __btrfs_reset_dev_stats(device);
  4998. device->dev_stats_valid = 1;
  4999. btrfs_release_path(path);
  5000. continue;
  5001. }
  5002. slot = path->slots[0];
  5003. eb = path->nodes[0];
  5004. btrfs_item_key_to_cpu(eb, &found_key, slot);
  5005. item_size = btrfs_item_size_nr(eb, slot);
  5006. ptr = btrfs_item_ptr(eb, slot,
  5007. struct btrfs_dev_stats_item);
  5008. for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
  5009. if (item_size >= (1 + i) * sizeof(__le64))
  5010. btrfs_dev_stat_set(device, i,
  5011. btrfs_dev_stats_value(eb, ptr, i));
  5012. else
  5013. btrfs_dev_stat_reset(device, i);
  5014. }
  5015. device->dev_stats_valid = 1;
  5016. btrfs_dev_stat_print_on_load(device);
  5017. btrfs_release_path(path);
  5018. }
  5019. mutex_unlock(&fs_devices->device_list_mutex);
  5020. out:
  5021. btrfs_free_path(path);
  5022. return ret < 0 ? ret : 0;
  5023. }
  5024. static int update_dev_stat_item(struct btrfs_trans_handle *trans,
  5025. struct btrfs_root *dev_root,
  5026. struct btrfs_device *device)
  5027. {
  5028. struct btrfs_path *path;
  5029. struct btrfs_key key;
  5030. struct extent_buffer *eb;
  5031. struct btrfs_dev_stats_item *ptr;
  5032. int ret;
  5033. int i;
  5034. key.objectid = 0;
  5035. key.type = BTRFS_DEV_STATS_KEY;
  5036. key.offset = device->devid;
  5037. path = btrfs_alloc_path();
  5038. BUG_ON(!path);
  5039. ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
  5040. if (ret < 0) {
  5041. printk_in_rcu(KERN_WARNING "btrfs: error %d while searching for dev_stats item for device %s!\n",
  5042. ret, rcu_str_deref(device->name));
  5043. goto out;
  5044. }
  5045. if (ret == 0 &&
  5046. btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
  5047. /* need to delete old one and insert a new one */
  5048. ret = btrfs_del_item(trans, dev_root, path);
  5049. if (ret != 0) {
  5050. printk_in_rcu(KERN_WARNING "btrfs: delete too small dev_stats item for device %s failed %d!\n",
  5051. rcu_str_deref(device->name), ret);
  5052. goto out;
  5053. }
  5054. ret = 1;
  5055. }
  5056. if (ret == 1) {
  5057. /* need to insert a new item */
  5058. btrfs_release_path(path);
  5059. ret = btrfs_insert_empty_item(trans, dev_root, path,
  5060. &key, sizeof(*ptr));
  5061. if (ret < 0) {
  5062. printk_in_rcu(KERN_WARNING "btrfs: insert dev_stats item for device %s failed %d!\n",
  5063. rcu_str_deref(device->name), ret);
  5064. goto out;
  5065. }
  5066. }
  5067. eb = path->nodes[0];
  5068. ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
  5069. for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
  5070. btrfs_set_dev_stats_value(eb, ptr, i,
  5071. btrfs_dev_stat_read(device, i));
  5072. btrfs_mark_buffer_dirty(eb);
  5073. out:
  5074. btrfs_free_path(path);
  5075. return ret;
  5076. }
  5077. /*
  5078. * called from commit_transaction. Writes all changed device stats to disk.
  5079. */
  5080. int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
  5081. struct btrfs_fs_info *fs_info)
  5082. {
  5083. struct btrfs_root *dev_root = fs_info->dev_root;
  5084. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  5085. struct btrfs_device *device;
  5086. int ret = 0;
  5087. mutex_lock(&fs_devices->device_list_mutex);
  5088. list_for_each_entry(device, &fs_devices->devices, dev_list) {
  5089. if (!device->dev_stats_valid || !device->dev_stats_dirty)
  5090. continue;
  5091. ret = update_dev_stat_item(trans, dev_root, device);
  5092. if (!ret)
  5093. device->dev_stats_dirty = 0;
  5094. }
  5095. mutex_unlock(&fs_devices->device_list_mutex);
  5096. return ret;
  5097. }
  5098. void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
  5099. {
  5100. btrfs_dev_stat_inc(dev, index);
  5101. btrfs_dev_stat_print_on_error(dev);
  5102. }
  5103. static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
  5104. {
  5105. if (!dev->dev_stats_valid)
  5106. return;
  5107. printk_ratelimited_in_rcu(KERN_ERR
  5108. "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
  5109. rcu_str_deref(dev->name),
  5110. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
  5111. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
  5112. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
  5113. btrfs_dev_stat_read(dev,
  5114. BTRFS_DEV_STAT_CORRUPTION_ERRS),
  5115. btrfs_dev_stat_read(dev,
  5116. BTRFS_DEV_STAT_GENERATION_ERRS));
  5117. }
  5118. static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
  5119. {
  5120. int i;
  5121. for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
  5122. if (btrfs_dev_stat_read(dev, i) != 0)
  5123. break;
  5124. if (i == BTRFS_DEV_STAT_VALUES_MAX)
  5125. return; /* all values == 0, suppress message */
  5126. printk_in_rcu(KERN_INFO "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
  5127. rcu_str_deref(dev->name),
  5128. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
  5129. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
  5130. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
  5131. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
  5132. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
  5133. }
  5134. int btrfs_get_dev_stats(struct btrfs_root *root,
  5135. struct btrfs_ioctl_get_dev_stats *stats)
  5136. {
  5137. struct btrfs_device *dev;
  5138. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  5139. int i;
  5140. mutex_lock(&fs_devices->device_list_mutex);
  5141. dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL);
  5142. mutex_unlock(&fs_devices->device_list_mutex);
  5143. if (!dev) {
  5144. printk(KERN_WARNING
  5145. "btrfs: get dev_stats failed, device not found\n");
  5146. return -ENODEV;
  5147. } else if (!dev->dev_stats_valid) {
  5148. printk(KERN_WARNING
  5149. "btrfs: get dev_stats failed, not yet valid\n");
  5150. return -ENODEV;
  5151. } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
  5152. for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
  5153. if (stats->nr_items > i)
  5154. stats->values[i] =
  5155. btrfs_dev_stat_read_and_reset(dev, i);
  5156. else
  5157. btrfs_dev_stat_reset(dev, i);
  5158. }
  5159. } else {
  5160. for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
  5161. if (stats->nr_items > i)
  5162. stats->values[i] = btrfs_dev_stat_read(dev, i);
  5163. }
  5164. if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
  5165. stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
  5166. return 0;
  5167. }
  5168. int btrfs_scratch_superblock(struct btrfs_device *device)
  5169. {
  5170. struct buffer_head *bh;
  5171. struct btrfs_super_block *disk_super;
  5172. bh = btrfs_read_dev_super(device->bdev);
  5173. if (!bh)
  5174. return -EINVAL;
  5175. disk_super = (struct btrfs_super_block *)bh->b_data;
  5176. memset(&disk_super->magic, 0, sizeof(disk_super->magic));
  5177. set_buffer_dirty(bh);
  5178. sync_dirty_buffer(bh);
  5179. brelse(bh);
  5180. return 0;
  5181. }