volumes.c 113 KB

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