volumes.c 124 KB

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