volumes.c 125 KB

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