volumes.c 151 KB

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