volumes.c 146 KB

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