volumes.c 154 KB

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