volumes.c 125 KB

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