volumes.c 111 KB

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