volumes.c 129 KB

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