volumes.c 155 KB

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