volumes.c 154 KB

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