volumes.c 138 KB

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