volumes.c 146 KB

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