volumes.c 125 KB

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