volumes.c 101 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071
  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 <asm/div64.h>
  27. #include "compat.h"
  28. #include "ctree.h"
  29. #include "extent_map.h"
  30. #include "disk-io.h"
  31. #include "transaction.h"
  32. #include "print-tree.h"
  33. #include "volumes.h"
  34. #include "async-thread.h"
  35. static int init_first_rw_device(struct btrfs_trans_handle *trans,
  36. struct btrfs_root *root,
  37. struct btrfs_device *device);
  38. static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
  39. static DEFINE_MUTEX(uuid_mutex);
  40. static LIST_HEAD(fs_uuids);
  41. static void lock_chunks(struct btrfs_root *root)
  42. {
  43. mutex_lock(&root->fs_info->chunk_mutex);
  44. }
  45. static void unlock_chunks(struct btrfs_root *root)
  46. {
  47. mutex_unlock(&root->fs_info->chunk_mutex);
  48. }
  49. static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
  50. {
  51. struct btrfs_device *device;
  52. WARN_ON(fs_devices->opened);
  53. while (!list_empty(&fs_devices->devices)) {
  54. device = list_entry(fs_devices->devices.next,
  55. struct btrfs_device, dev_list);
  56. list_del(&device->dev_list);
  57. kfree(device->name);
  58. kfree(device);
  59. }
  60. kfree(fs_devices);
  61. }
  62. int btrfs_cleanup_fs_uuids(void)
  63. {
  64. struct btrfs_fs_devices *fs_devices;
  65. while (!list_empty(&fs_uuids)) {
  66. fs_devices = list_entry(fs_uuids.next,
  67. struct btrfs_fs_devices, list);
  68. list_del(&fs_devices->list);
  69. free_fs_devices(fs_devices);
  70. }
  71. return 0;
  72. }
  73. static noinline struct btrfs_device *__find_device(struct list_head *head,
  74. u64 devid, u8 *uuid)
  75. {
  76. struct btrfs_device *dev;
  77. list_for_each_entry(dev, head, dev_list) {
  78. if (dev->devid == devid &&
  79. (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
  80. return dev;
  81. }
  82. }
  83. return NULL;
  84. }
  85. static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
  86. {
  87. struct btrfs_fs_devices *fs_devices;
  88. list_for_each_entry(fs_devices, &fs_uuids, list) {
  89. if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
  90. return fs_devices;
  91. }
  92. return NULL;
  93. }
  94. static void requeue_list(struct btrfs_pending_bios *pending_bios,
  95. struct bio *head, struct bio *tail)
  96. {
  97. struct bio *old_head;
  98. old_head = pending_bios->head;
  99. pending_bios->head = head;
  100. if (pending_bios->tail)
  101. tail->bi_next = old_head;
  102. else
  103. pending_bios->tail = tail;
  104. }
  105. /*
  106. * we try to collect pending bios for a device so we don't get a large
  107. * number of procs sending bios down to the same device. This greatly
  108. * improves the schedulers ability to collect and merge the bios.
  109. *
  110. * But, it also turns into a long list of bios to process and that is sure
  111. * to eventually make the worker thread block. The solution here is to
  112. * make some progress and then put this work struct back at the end of
  113. * the list if the block device is congested. This way, multiple devices
  114. * can make progress from a single worker thread.
  115. */
  116. static noinline int run_scheduled_bios(struct btrfs_device *device)
  117. {
  118. struct bio *pending;
  119. struct backing_dev_info *bdi;
  120. struct btrfs_fs_info *fs_info;
  121. struct btrfs_pending_bios *pending_bios;
  122. struct bio *tail;
  123. struct bio *cur;
  124. int again = 0;
  125. unsigned long num_run;
  126. unsigned long batch_run = 0;
  127. unsigned long limit;
  128. unsigned long last_waited = 0;
  129. int force_reg = 0;
  130. int sync_pending = 0;
  131. struct blk_plug plug;
  132. /*
  133. * this function runs all the bios we've collected for
  134. * a particular device. We don't want to wander off to
  135. * another device without first sending all of these down.
  136. * So, setup a plug here and finish it off before we return
  137. */
  138. blk_start_plug(&plug);
  139. bdi = blk_get_backing_dev_info(device->bdev);
  140. fs_info = device->dev_root->fs_info;
  141. limit = btrfs_async_submit_limit(fs_info);
  142. limit = limit * 2 / 3;
  143. loop:
  144. spin_lock(&device->io_lock);
  145. loop_lock:
  146. num_run = 0;
  147. /* take all the bios off the list at once and process them
  148. * later on (without the lock held). But, remember the
  149. * tail and other pointers so the bios can be properly reinserted
  150. * into the list if we hit congestion
  151. */
  152. if (!force_reg && device->pending_sync_bios.head) {
  153. pending_bios = &device->pending_sync_bios;
  154. force_reg = 1;
  155. } else {
  156. pending_bios = &device->pending_bios;
  157. force_reg = 0;
  158. }
  159. pending = pending_bios->head;
  160. tail = pending_bios->tail;
  161. WARN_ON(pending && !tail);
  162. /*
  163. * if pending was null this time around, no bios need processing
  164. * at all and we can stop. Otherwise it'll loop back up again
  165. * and do an additional check so no bios are missed.
  166. *
  167. * device->running_pending is used to synchronize with the
  168. * schedule_bio code.
  169. */
  170. if (device->pending_sync_bios.head == NULL &&
  171. device->pending_bios.head == NULL) {
  172. again = 0;
  173. device->running_pending = 0;
  174. } else {
  175. again = 1;
  176. device->running_pending = 1;
  177. }
  178. pending_bios->head = NULL;
  179. pending_bios->tail = NULL;
  180. spin_unlock(&device->io_lock);
  181. while (pending) {
  182. rmb();
  183. /* we want to work on both lists, but do more bios on the
  184. * sync list than the regular list
  185. */
  186. if ((num_run > 32 &&
  187. pending_bios != &device->pending_sync_bios &&
  188. device->pending_sync_bios.head) ||
  189. (num_run > 64 && pending_bios == &device->pending_sync_bios &&
  190. device->pending_bios.head)) {
  191. spin_lock(&device->io_lock);
  192. requeue_list(pending_bios, pending, tail);
  193. goto loop_lock;
  194. }
  195. cur = pending;
  196. pending = pending->bi_next;
  197. cur->bi_next = NULL;
  198. atomic_dec(&fs_info->nr_async_bios);
  199. if (atomic_read(&fs_info->nr_async_bios) < limit &&
  200. waitqueue_active(&fs_info->async_submit_wait))
  201. wake_up(&fs_info->async_submit_wait);
  202. BUG_ON(atomic_read(&cur->bi_cnt) == 0);
  203. /*
  204. * if we're doing the sync list, record that our
  205. * plug has some sync requests on it
  206. *
  207. * If we're doing the regular list and there are
  208. * sync requests sitting around, unplug before
  209. * we add more
  210. */
  211. if (pending_bios == &device->pending_sync_bios) {
  212. sync_pending = 1;
  213. } else if (sync_pending) {
  214. blk_finish_plug(&plug);
  215. blk_start_plug(&plug);
  216. sync_pending = 0;
  217. }
  218. submit_bio(cur->bi_rw, cur);
  219. num_run++;
  220. batch_run++;
  221. if (need_resched())
  222. cond_resched();
  223. /*
  224. * we made progress, there is more work to do and the bdi
  225. * is now congested. Back off and let other work structs
  226. * run instead
  227. */
  228. if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
  229. fs_info->fs_devices->open_devices > 1) {
  230. struct io_context *ioc;
  231. ioc = current->io_context;
  232. /*
  233. * the main goal here is that we don't want to
  234. * block if we're going to be able to submit
  235. * more requests without blocking.
  236. *
  237. * This code does two great things, it pokes into
  238. * the elevator code from a filesystem _and_
  239. * it makes assumptions about how batching works.
  240. */
  241. if (ioc && ioc->nr_batch_requests > 0 &&
  242. time_before(jiffies, ioc->last_waited + HZ/50UL) &&
  243. (last_waited == 0 ||
  244. ioc->last_waited == last_waited)) {
  245. /*
  246. * we want to go through our batch of
  247. * requests and stop. So, we copy out
  248. * the ioc->last_waited time and test
  249. * against it before looping
  250. */
  251. last_waited = ioc->last_waited;
  252. if (need_resched())
  253. cond_resched();
  254. continue;
  255. }
  256. spin_lock(&device->io_lock);
  257. requeue_list(pending_bios, pending, tail);
  258. device->running_pending = 1;
  259. spin_unlock(&device->io_lock);
  260. btrfs_requeue_work(&device->work);
  261. goto done;
  262. }
  263. /* unplug every 64 requests just for good measure */
  264. if (batch_run % 64 == 0) {
  265. blk_finish_plug(&plug);
  266. blk_start_plug(&plug);
  267. sync_pending = 0;
  268. }
  269. }
  270. cond_resched();
  271. if (again)
  272. goto loop;
  273. spin_lock(&device->io_lock);
  274. if (device->pending_bios.head || device->pending_sync_bios.head)
  275. goto loop_lock;
  276. spin_unlock(&device->io_lock);
  277. done:
  278. blk_finish_plug(&plug);
  279. return 0;
  280. }
  281. static void pending_bios_fn(struct btrfs_work *work)
  282. {
  283. struct btrfs_device *device;
  284. device = container_of(work, struct btrfs_device, work);
  285. run_scheduled_bios(device);
  286. }
  287. static noinline int device_list_add(const char *path,
  288. struct btrfs_super_block *disk_super,
  289. u64 devid, struct btrfs_fs_devices **fs_devices_ret)
  290. {
  291. struct btrfs_device *device;
  292. struct btrfs_fs_devices *fs_devices;
  293. u64 found_transid = btrfs_super_generation(disk_super);
  294. char *name;
  295. fs_devices = find_fsid(disk_super->fsid);
  296. if (!fs_devices) {
  297. fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
  298. if (!fs_devices)
  299. return -ENOMEM;
  300. INIT_LIST_HEAD(&fs_devices->devices);
  301. INIT_LIST_HEAD(&fs_devices->alloc_list);
  302. list_add(&fs_devices->list, &fs_uuids);
  303. memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
  304. fs_devices->latest_devid = devid;
  305. fs_devices->latest_trans = found_transid;
  306. mutex_init(&fs_devices->device_list_mutex);
  307. device = NULL;
  308. } else {
  309. device = __find_device(&fs_devices->devices, devid,
  310. disk_super->dev_item.uuid);
  311. }
  312. if (!device) {
  313. if (fs_devices->opened)
  314. return -EBUSY;
  315. device = kzalloc(sizeof(*device), GFP_NOFS);
  316. if (!device) {
  317. /* we can safely leave the fs_devices entry around */
  318. return -ENOMEM;
  319. }
  320. device->devid = devid;
  321. device->work.func = pending_bios_fn;
  322. memcpy(device->uuid, disk_super->dev_item.uuid,
  323. BTRFS_UUID_SIZE);
  324. spin_lock_init(&device->io_lock);
  325. device->name = kstrdup(path, GFP_NOFS);
  326. if (!device->name) {
  327. kfree(device);
  328. return -ENOMEM;
  329. }
  330. INIT_LIST_HEAD(&device->dev_alloc_list);
  331. /* init readahead state */
  332. spin_lock_init(&device->reada_lock);
  333. device->reada_curr_zone = NULL;
  334. atomic_set(&device->reada_in_flight, 0);
  335. device->reada_next = 0;
  336. INIT_RADIX_TREE(&device->reada_zones, GFP_NOFS & ~__GFP_WAIT);
  337. INIT_RADIX_TREE(&device->reada_extents, GFP_NOFS & ~__GFP_WAIT);
  338. mutex_lock(&fs_devices->device_list_mutex);
  339. list_add_rcu(&device->dev_list, &fs_devices->devices);
  340. mutex_unlock(&fs_devices->device_list_mutex);
  341. device->fs_devices = fs_devices;
  342. fs_devices->num_devices++;
  343. } else if (!device->name || strcmp(device->name, path)) {
  344. name = kstrdup(path, GFP_NOFS);
  345. if (!name)
  346. return -ENOMEM;
  347. kfree(device->name);
  348. device->name = name;
  349. if (device->missing) {
  350. fs_devices->missing_devices--;
  351. device->missing = 0;
  352. }
  353. }
  354. if (found_transid > fs_devices->latest_trans) {
  355. fs_devices->latest_devid = devid;
  356. fs_devices->latest_trans = found_transid;
  357. }
  358. *fs_devices_ret = fs_devices;
  359. return 0;
  360. }
  361. static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
  362. {
  363. struct btrfs_fs_devices *fs_devices;
  364. struct btrfs_device *device;
  365. struct btrfs_device *orig_dev;
  366. fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
  367. if (!fs_devices)
  368. return ERR_PTR(-ENOMEM);
  369. INIT_LIST_HEAD(&fs_devices->devices);
  370. INIT_LIST_HEAD(&fs_devices->alloc_list);
  371. INIT_LIST_HEAD(&fs_devices->list);
  372. mutex_init(&fs_devices->device_list_mutex);
  373. fs_devices->latest_devid = orig->latest_devid;
  374. fs_devices->latest_trans = orig->latest_trans;
  375. memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
  376. /* We have held the volume lock, it is safe to get the devices. */
  377. list_for_each_entry(orig_dev, &orig->devices, dev_list) {
  378. device = kzalloc(sizeof(*device), GFP_NOFS);
  379. if (!device)
  380. goto error;
  381. device->name = kstrdup(orig_dev->name, GFP_NOFS);
  382. if (!device->name) {
  383. kfree(device);
  384. goto error;
  385. }
  386. device->devid = orig_dev->devid;
  387. device->work.func = pending_bios_fn;
  388. memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
  389. spin_lock_init(&device->io_lock);
  390. INIT_LIST_HEAD(&device->dev_list);
  391. INIT_LIST_HEAD(&device->dev_alloc_list);
  392. list_add(&device->dev_list, &fs_devices->devices);
  393. device->fs_devices = fs_devices;
  394. fs_devices->num_devices++;
  395. }
  396. return fs_devices;
  397. error:
  398. free_fs_devices(fs_devices);
  399. return ERR_PTR(-ENOMEM);
  400. }
  401. int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
  402. {
  403. struct btrfs_device *device, *next;
  404. mutex_lock(&uuid_mutex);
  405. again:
  406. /* This is the initialized path, it is safe to release the devices. */
  407. list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
  408. if (device->in_fs_metadata)
  409. continue;
  410. if (device->bdev) {
  411. blkdev_put(device->bdev, device->mode);
  412. device->bdev = NULL;
  413. fs_devices->open_devices--;
  414. }
  415. if (device->writeable) {
  416. list_del_init(&device->dev_alloc_list);
  417. device->writeable = 0;
  418. fs_devices->rw_devices--;
  419. }
  420. list_del_init(&device->dev_list);
  421. fs_devices->num_devices--;
  422. kfree(device->name);
  423. kfree(device);
  424. }
  425. if (fs_devices->seed) {
  426. fs_devices = fs_devices->seed;
  427. goto again;
  428. }
  429. mutex_unlock(&uuid_mutex);
  430. return 0;
  431. }
  432. static void __free_device(struct work_struct *work)
  433. {
  434. struct btrfs_device *device;
  435. device = container_of(work, struct btrfs_device, rcu_work);
  436. if (device->bdev)
  437. blkdev_put(device->bdev, device->mode);
  438. kfree(device->name);
  439. kfree(device);
  440. }
  441. static void free_device(struct rcu_head *head)
  442. {
  443. struct btrfs_device *device;
  444. device = container_of(head, struct btrfs_device, rcu);
  445. INIT_WORK(&device->rcu_work, __free_device);
  446. schedule_work(&device->rcu_work);
  447. }
  448. static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
  449. {
  450. struct btrfs_device *device;
  451. if (--fs_devices->opened > 0)
  452. return 0;
  453. mutex_lock(&fs_devices->device_list_mutex);
  454. list_for_each_entry(device, &fs_devices->devices, dev_list) {
  455. struct btrfs_device *new_device;
  456. if (device->bdev)
  457. fs_devices->open_devices--;
  458. if (device->writeable) {
  459. list_del_init(&device->dev_alloc_list);
  460. fs_devices->rw_devices--;
  461. }
  462. if (device->can_discard)
  463. fs_devices->num_can_discard--;
  464. new_device = kmalloc(sizeof(*new_device), GFP_NOFS);
  465. BUG_ON(!new_device);
  466. memcpy(new_device, device, sizeof(*new_device));
  467. new_device->name = kstrdup(device->name, GFP_NOFS);
  468. BUG_ON(device->name && !new_device->name);
  469. new_device->bdev = NULL;
  470. new_device->writeable = 0;
  471. new_device->in_fs_metadata = 0;
  472. new_device->can_discard = 0;
  473. list_replace_rcu(&device->dev_list, &new_device->dev_list);
  474. call_rcu(&device->rcu, free_device);
  475. }
  476. mutex_unlock(&fs_devices->device_list_mutex);
  477. WARN_ON(fs_devices->open_devices);
  478. WARN_ON(fs_devices->rw_devices);
  479. fs_devices->opened = 0;
  480. fs_devices->seeding = 0;
  481. return 0;
  482. }
  483. int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
  484. {
  485. struct btrfs_fs_devices *seed_devices = NULL;
  486. int ret;
  487. mutex_lock(&uuid_mutex);
  488. ret = __btrfs_close_devices(fs_devices);
  489. if (!fs_devices->opened) {
  490. seed_devices = fs_devices->seed;
  491. fs_devices->seed = NULL;
  492. }
  493. mutex_unlock(&uuid_mutex);
  494. while (seed_devices) {
  495. fs_devices = seed_devices;
  496. seed_devices = fs_devices->seed;
  497. __btrfs_close_devices(fs_devices);
  498. free_fs_devices(fs_devices);
  499. }
  500. return ret;
  501. }
  502. static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
  503. fmode_t flags, void *holder)
  504. {
  505. struct request_queue *q;
  506. struct block_device *bdev;
  507. struct list_head *head = &fs_devices->devices;
  508. struct btrfs_device *device;
  509. struct block_device *latest_bdev = NULL;
  510. struct buffer_head *bh;
  511. struct btrfs_super_block *disk_super;
  512. u64 latest_devid = 0;
  513. u64 latest_transid = 0;
  514. u64 devid;
  515. int seeding = 1;
  516. int ret = 0;
  517. flags |= FMODE_EXCL;
  518. list_for_each_entry(device, head, dev_list) {
  519. if (device->bdev)
  520. continue;
  521. if (!device->name)
  522. continue;
  523. bdev = blkdev_get_by_path(device->name, flags, holder);
  524. if (IS_ERR(bdev)) {
  525. printk(KERN_INFO "open %s failed\n", device->name);
  526. goto error;
  527. }
  528. set_blocksize(bdev, 4096);
  529. bh = btrfs_read_dev_super(bdev);
  530. if (!bh)
  531. goto error_close;
  532. disk_super = (struct btrfs_super_block *)bh->b_data;
  533. devid = btrfs_stack_device_id(&disk_super->dev_item);
  534. if (devid != device->devid)
  535. goto error_brelse;
  536. if (memcmp(device->uuid, disk_super->dev_item.uuid,
  537. BTRFS_UUID_SIZE))
  538. goto error_brelse;
  539. device->generation = btrfs_super_generation(disk_super);
  540. if (!latest_transid || device->generation > latest_transid) {
  541. latest_devid = devid;
  542. latest_transid = device->generation;
  543. latest_bdev = bdev;
  544. }
  545. if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
  546. device->writeable = 0;
  547. } else {
  548. device->writeable = !bdev_read_only(bdev);
  549. seeding = 0;
  550. }
  551. q = bdev_get_queue(bdev);
  552. if (blk_queue_discard(q)) {
  553. device->can_discard = 1;
  554. fs_devices->num_can_discard++;
  555. }
  556. device->bdev = bdev;
  557. device->in_fs_metadata = 0;
  558. device->mode = flags;
  559. if (!blk_queue_nonrot(bdev_get_queue(bdev)))
  560. fs_devices->rotating = 1;
  561. fs_devices->open_devices++;
  562. if (device->writeable) {
  563. fs_devices->rw_devices++;
  564. list_add(&device->dev_alloc_list,
  565. &fs_devices->alloc_list);
  566. }
  567. brelse(bh);
  568. continue;
  569. error_brelse:
  570. brelse(bh);
  571. error_close:
  572. blkdev_put(bdev, flags);
  573. error:
  574. continue;
  575. }
  576. if (fs_devices->open_devices == 0) {
  577. ret = -EINVAL;
  578. goto out;
  579. }
  580. fs_devices->seeding = seeding;
  581. fs_devices->opened = 1;
  582. fs_devices->latest_bdev = latest_bdev;
  583. fs_devices->latest_devid = latest_devid;
  584. fs_devices->latest_trans = latest_transid;
  585. fs_devices->total_rw_bytes = 0;
  586. out:
  587. return ret;
  588. }
  589. int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
  590. fmode_t flags, void *holder)
  591. {
  592. int ret;
  593. mutex_lock(&uuid_mutex);
  594. if (fs_devices->opened) {
  595. fs_devices->opened++;
  596. ret = 0;
  597. } else {
  598. ret = __btrfs_open_devices(fs_devices, flags, holder);
  599. }
  600. mutex_unlock(&uuid_mutex);
  601. return ret;
  602. }
  603. int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
  604. struct btrfs_fs_devices **fs_devices_ret)
  605. {
  606. struct btrfs_super_block *disk_super;
  607. struct block_device *bdev;
  608. struct buffer_head *bh;
  609. int ret;
  610. u64 devid;
  611. u64 transid;
  612. mutex_lock(&uuid_mutex);
  613. flags |= FMODE_EXCL;
  614. bdev = blkdev_get_by_path(path, flags, holder);
  615. if (IS_ERR(bdev)) {
  616. ret = PTR_ERR(bdev);
  617. goto error;
  618. }
  619. ret = set_blocksize(bdev, 4096);
  620. if (ret)
  621. goto error_close;
  622. bh = btrfs_read_dev_super(bdev);
  623. if (!bh) {
  624. ret = -EINVAL;
  625. goto error_close;
  626. }
  627. disk_super = (struct btrfs_super_block *)bh->b_data;
  628. devid = btrfs_stack_device_id(&disk_super->dev_item);
  629. transid = btrfs_super_generation(disk_super);
  630. if (disk_super->label[0])
  631. printk(KERN_INFO "device label %s ", disk_super->label);
  632. else
  633. printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
  634. printk(KERN_CONT "devid %llu transid %llu %s\n",
  635. (unsigned long long)devid, (unsigned long long)transid, path);
  636. ret = device_list_add(path, disk_super, devid, fs_devices_ret);
  637. brelse(bh);
  638. error_close:
  639. blkdev_put(bdev, flags);
  640. error:
  641. mutex_unlock(&uuid_mutex);
  642. return ret;
  643. }
  644. /* helper to account the used device space in the range */
  645. int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
  646. u64 end, u64 *length)
  647. {
  648. struct btrfs_key key;
  649. struct btrfs_root *root = device->dev_root;
  650. struct btrfs_dev_extent *dev_extent;
  651. struct btrfs_path *path;
  652. u64 extent_end;
  653. int ret;
  654. int slot;
  655. struct extent_buffer *l;
  656. *length = 0;
  657. if (start >= device->total_bytes)
  658. return 0;
  659. path = btrfs_alloc_path();
  660. if (!path)
  661. return -ENOMEM;
  662. path->reada = 2;
  663. key.objectid = device->devid;
  664. key.offset = start;
  665. key.type = BTRFS_DEV_EXTENT_KEY;
  666. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  667. if (ret < 0)
  668. goto out;
  669. if (ret > 0) {
  670. ret = btrfs_previous_item(root, path, key.objectid, key.type);
  671. if (ret < 0)
  672. goto out;
  673. }
  674. while (1) {
  675. l = path->nodes[0];
  676. slot = path->slots[0];
  677. if (slot >= btrfs_header_nritems(l)) {
  678. ret = btrfs_next_leaf(root, path);
  679. if (ret == 0)
  680. continue;
  681. if (ret < 0)
  682. goto out;
  683. break;
  684. }
  685. btrfs_item_key_to_cpu(l, &key, slot);
  686. if (key.objectid < device->devid)
  687. goto next;
  688. if (key.objectid > device->devid)
  689. break;
  690. if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
  691. goto next;
  692. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  693. extent_end = key.offset + btrfs_dev_extent_length(l,
  694. dev_extent);
  695. if (key.offset <= start && extent_end > end) {
  696. *length = end - start + 1;
  697. break;
  698. } else if (key.offset <= start && extent_end > start)
  699. *length += extent_end - start;
  700. else if (key.offset > start && extent_end <= end)
  701. *length += extent_end - key.offset;
  702. else if (key.offset > start && key.offset <= end) {
  703. *length += end - key.offset + 1;
  704. break;
  705. } else if (key.offset > end)
  706. break;
  707. next:
  708. path->slots[0]++;
  709. }
  710. ret = 0;
  711. out:
  712. btrfs_free_path(path);
  713. return ret;
  714. }
  715. /*
  716. * find_free_dev_extent - find free space in the specified device
  717. * @trans: transaction handler
  718. * @device: the device which we search the free space in
  719. * @num_bytes: the size of the free space that we need
  720. * @start: store the start of the free space.
  721. * @len: the size of the free space. that we find, or the size of the max
  722. * free space if we don't find suitable free space
  723. *
  724. * this uses a pretty simple search, the expectation is that it is
  725. * called very infrequently and that a given device has a small number
  726. * of extents
  727. *
  728. * @start is used to store the start of the free space if we find. But if we
  729. * don't find suitable free space, it will be used to store the start position
  730. * of the max free space.
  731. *
  732. * @len is used to store the size of the free space that we find.
  733. * But if we don't find suitable free space, it is used to store the size of
  734. * the max free space.
  735. */
  736. int find_free_dev_extent(struct btrfs_trans_handle *trans,
  737. 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(trans, 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_trans_handle *trans,
  1269. struct btrfs_root *root)
  1270. {
  1271. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  1272. struct btrfs_fs_devices *old_devices;
  1273. struct btrfs_fs_devices *seed_devices;
  1274. struct btrfs_super_block *disk_super = root->fs_info->super_copy;
  1275. struct btrfs_device *device;
  1276. u64 super_flags;
  1277. BUG_ON(!mutex_is_locked(&uuid_mutex));
  1278. if (!fs_devices->seeding)
  1279. return -EINVAL;
  1280. seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
  1281. if (!seed_devices)
  1282. return -ENOMEM;
  1283. old_devices = clone_fs_devices(fs_devices);
  1284. if (IS_ERR(old_devices)) {
  1285. kfree(seed_devices);
  1286. return PTR_ERR(old_devices);
  1287. }
  1288. list_add(&old_devices->list, &fs_uuids);
  1289. memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
  1290. seed_devices->opened = 1;
  1291. INIT_LIST_HEAD(&seed_devices->devices);
  1292. INIT_LIST_HEAD(&seed_devices->alloc_list);
  1293. mutex_init(&seed_devices->device_list_mutex);
  1294. mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
  1295. list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
  1296. synchronize_rcu);
  1297. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1298. list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
  1299. list_for_each_entry(device, &seed_devices->devices, dev_list) {
  1300. device->fs_devices = seed_devices;
  1301. }
  1302. fs_devices->seeding = 0;
  1303. fs_devices->num_devices = 0;
  1304. fs_devices->open_devices = 0;
  1305. fs_devices->seed = seed_devices;
  1306. generate_random_uuid(fs_devices->fsid);
  1307. memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
  1308. memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
  1309. super_flags = btrfs_super_flags(disk_super) &
  1310. ~BTRFS_SUPER_FLAG_SEEDING;
  1311. btrfs_set_super_flags(disk_super, super_flags);
  1312. return 0;
  1313. }
  1314. /*
  1315. * strore the expected generation for seed devices in device items.
  1316. */
  1317. static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
  1318. struct btrfs_root *root)
  1319. {
  1320. struct btrfs_path *path;
  1321. struct extent_buffer *leaf;
  1322. struct btrfs_dev_item *dev_item;
  1323. struct btrfs_device *device;
  1324. struct btrfs_key key;
  1325. u8 fs_uuid[BTRFS_UUID_SIZE];
  1326. u8 dev_uuid[BTRFS_UUID_SIZE];
  1327. u64 devid;
  1328. int ret;
  1329. path = btrfs_alloc_path();
  1330. if (!path)
  1331. return -ENOMEM;
  1332. root = root->fs_info->chunk_root;
  1333. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  1334. key.offset = 0;
  1335. key.type = BTRFS_DEV_ITEM_KEY;
  1336. while (1) {
  1337. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  1338. if (ret < 0)
  1339. goto error;
  1340. leaf = path->nodes[0];
  1341. next_slot:
  1342. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  1343. ret = btrfs_next_leaf(root, path);
  1344. if (ret > 0)
  1345. break;
  1346. if (ret < 0)
  1347. goto error;
  1348. leaf = path->nodes[0];
  1349. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  1350. btrfs_release_path(path);
  1351. continue;
  1352. }
  1353. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  1354. if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
  1355. key.type != BTRFS_DEV_ITEM_KEY)
  1356. break;
  1357. dev_item = btrfs_item_ptr(leaf, path->slots[0],
  1358. struct btrfs_dev_item);
  1359. devid = btrfs_device_id(leaf, dev_item);
  1360. read_extent_buffer(leaf, dev_uuid,
  1361. (unsigned long)btrfs_device_uuid(dev_item),
  1362. BTRFS_UUID_SIZE);
  1363. read_extent_buffer(leaf, fs_uuid,
  1364. (unsigned long)btrfs_device_fsid(dev_item),
  1365. BTRFS_UUID_SIZE);
  1366. device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
  1367. BUG_ON(!device);
  1368. if (device->fs_devices->seeding) {
  1369. btrfs_set_device_generation(leaf, dev_item,
  1370. device->generation);
  1371. btrfs_mark_buffer_dirty(leaf);
  1372. }
  1373. path->slots[0]++;
  1374. goto next_slot;
  1375. }
  1376. ret = 0;
  1377. error:
  1378. btrfs_free_path(path);
  1379. return ret;
  1380. }
  1381. int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
  1382. {
  1383. struct request_queue *q;
  1384. struct btrfs_trans_handle *trans;
  1385. struct btrfs_device *device;
  1386. struct block_device *bdev;
  1387. struct list_head *devices;
  1388. struct super_block *sb = root->fs_info->sb;
  1389. u64 total_bytes;
  1390. int seeding_dev = 0;
  1391. int ret = 0;
  1392. if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
  1393. return -EINVAL;
  1394. bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
  1395. root->fs_info->bdev_holder);
  1396. if (IS_ERR(bdev))
  1397. return PTR_ERR(bdev);
  1398. if (root->fs_info->fs_devices->seeding) {
  1399. seeding_dev = 1;
  1400. down_write(&sb->s_umount);
  1401. mutex_lock(&uuid_mutex);
  1402. }
  1403. filemap_write_and_wait(bdev->bd_inode->i_mapping);
  1404. devices = &root->fs_info->fs_devices->devices;
  1405. /*
  1406. * we have the volume lock, so we don't need the extra
  1407. * device list mutex while reading the list here.
  1408. */
  1409. list_for_each_entry(device, devices, dev_list) {
  1410. if (device->bdev == bdev) {
  1411. ret = -EEXIST;
  1412. goto error;
  1413. }
  1414. }
  1415. device = kzalloc(sizeof(*device), GFP_NOFS);
  1416. if (!device) {
  1417. /* we can safely leave the fs_devices entry around */
  1418. ret = -ENOMEM;
  1419. goto error;
  1420. }
  1421. device->name = kstrdup(device_path, GFP_NOFS);
  1422. if (!device->name) {
  1423. kfree(device);
  1424. ret = -ENOMEM;
  1425. goto error;
  1426. }
  1427. ret = find_next_devid(root, &device->devid);
  1428. if (ret) {
  1429. kfree(device->name);
  1430. kfree(device);
  1431. goto error;
  1432. }
  1433. trans = btrfs_start_transaction(root, 0);
  1434. if (IS_ERR(trans)) {
  1435. kfree(device->name);
  1436. kfree(device);
  1437. ret = PTR_ERR(trans);
  1438. goto error;
  1439. }
  1440. lock_chunks(root);
  1441. q = bdev_get_queue(bdev);
  1442. if (blk_queue_discard(q))
  1443. device->can_discard = 1;
  1444. device->writeable = 1;
  1445. device->work.func = pending_bios_fn;
  1446. generate_random_uuid(device->uuid);
  1447. spin_lock_init(&device->io_lock);
  1448. device->generation = trans->transid;
  1449. device->io_width = root->sectorsize;
  1450. device->io_align = root->sectorsize;
  1451. device->sector_size = root->sectorsize;
  1452. device->total_bytes = i_size_read(bdev->bd_inode);
  1453. device->disk_total_bytes = device->total_bytes;
  1454. device->dev_root = root->fs_info->dev_root;
  1455. device->bdev = bdev;
  1456. device->in_fs_metadata = 1;
  1457. device->mode = FMODE_EXCL;
  1458. set_blocksize(device->bdev, 4096);
  1459. if (seeding_dev) {
  1460. sb->s_flags &= ~MS_RDONLY;
  1461. ret = btrfs_prepare_sprout(trans, root);
  1462. BUG_ON(ret);
  1463. }
  1464. device->fs_devices = root->fs_info->fs_devices;
  1465. /*
  1466. * we don't want write_supers to jump in here with our device
  1467. * half setup
  1468. */
  1469. mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
  1470. list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
  1471. list_add(&device->dev_alloc_list,
  1472. &root->fs_info->fs_devices->alloc_list);
  1473. root->fs_info->fs_devices->num_devices++;
  1474. root->fs_info->fs_devices->open_devices++;
  1475. root->fs_info->fs_devices->rw_devices++;
  1476. if (device->can_discard)
  1477. root->fs_info->fs_devices->num_can_discard++;
  1478. root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
  1479. spin_lock(&root->fs_info->free_chunk_lock);
  1480. root->fs_info->free_chunk_space += device->total_bytes;
  1481. spin_unlock(&root->fs_info->free_chunk_lock);
  1482. if (!blk_queue_nonrot(bdev_get_queue(bdev)))
  1483. root->fs_info->fs_devices->rotating = 1;
  1484. total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
  1485. btrfs_set_super_total_bytes(root->fs_info->super_copy,
  1486. total_bytes + device->total_bytes);
  1487. total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
  1488. btrfs_set_super_num_devices(root->fs_info->super_copy,
  1489. total_bytes + 1);
  1490. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1491. if (seeding_dev) {
  1492. ret = init_first_rw_device(trans, root, device);
  1493. BUG_ON(ret);
  1494. ret = btrfs_finish_sprout(trans, root);
  1495. BUG_ON(ret);
  1496. } else {
  1497. ret = btrfs_add_device(trans, root, device);
  1498. }
  1499. /*
  1500. * we've got more storage, clear any full flags on the space
  1501. * infos
  1502. */
  1503. btrfs_clear_space_info_full(root->fs_info);
  1504. unlock_chunks(root);
  1505. btrfs_commit_transaction(trans, root);
  1506. if (seeding_dev) {
  1507. mutex_unlock(&uuid_mutex);
  1508. up_write(&sb->s_umount);
  1509. ret = btrfs_relocate_sys_chunks(root);
  1510. BUG_ON(ret);
  1511. }
  1512. return ret;
  1513. error:
  1514. blkdev_put(bdev, FMODE_EXCL);
  1515. if (seeding_dev) {
  1516. mutex_unlock(&uuid_mutex);
  1517. up_write(&sb->s_umount);
  1518. }
  1519. return ret;
  1520. }
  1521. static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
  1522. struct btrfs_device *device)
  1523. {
  1524. int ret;
  1525. struct btrfs_path *path;
  1526. struct btrfs_root *root;
  1527. struct btrfs_dev_item *dev_item;
  1528. struct extent_buffer *leaf;
  1529. struct btrfs_key key;
  1530. root = device->dev_root->fs_info->chunk_root;
  1531. path = btrfs_alloc_path();
  1532. if (!path)
  1533. return -ENOMEM;
  1534. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  1535. key.type = BTRFS_DEV_ITEM_KEY;
  1536. key.offset = device->devid;
  1537. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  1538. if (ret < 0)
  1539. goto out;
  1540. if (ret > 0) {
  1541. ret = -ENOENT;
  1542. goto out;
  1543. }
  1544. leaf = path->nodes[0];
  1545. dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
  1546. btrfs_set_device_id(leaf, dev_item, device->devid);
  1547. btrfs_set_device_type(leaf, dev_item, device->type);
  1548. btrfs_set_device_io_align(leaf, dev_item, device->io_align);
  1549. btrfs_set_device_io_width(leaf, dev_item, device->io_width);
  1550. btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
  1551. btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
  1552. btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
  1553. btrfs_mark_buffer_dirty(leaf);
  1554. out:
  1555. btrfs_free_path(path);
  1556. return ret;
  1557. }
  1558. static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
  1559. struct btrfs_device *device, u64 new_size)
  1560. {
  1561. struct btrfs_super_block *super_copy =
  1562. device->dev_root->fs_info->super_copy;
  1563. u64 old_total = btrfs_super_total_bytes(super_copy);
  1564. u64 diff = new_size - device->total_bytes;
  1565. if (!device->writeable)
  1566. return -EACCES;
  1567. if (new_size <= device->total_bytes)
  1568. return -EINVAL;
  1569. btrfs_set_super_total_bytes(super_copy, old_total + diff);
  1570. device->fs_devices->total_rw_bytes += diff;
  1571. device->total_bytes = new_size;
  1572. device->disk_total_bytes = new_size;
  1573. btrfs_clear_space_info_full(device->dev_root->fs_info);
  1574. return btrfs_update_device(trans, device);
  1575. }
  1576. int btrfs_grow_device(struct btrfs_trans_handle *trans,
  1577. struct btrfs_device *device, u64 new_size)
  1578. {
  1579. int ret;
  1580. lock_chunks(device->dev_root);
  1581. ret = __btrfs_grow_device(trans, device, new_size);
  1582. unlock_chunks(device->dev_root);
  1583. return ret;
  1584. }
  1585. static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
  1586. struct btrfs_root *root,
  1587. u64 chunk_tree, u64 chunk_objectid,
  1588. u64 chunk_offset)
  1589. {
  1590. int ret;
  1591. struct btrfs_path *path;
  1592. struct btrfs_key key;
  1593. root = root->fs_info->chunk_root;
  1594. path = btrfs_alloc_path();
  1595. if (!path)
  1596. return -ENOMEM;
  1597. key.objectid = chunk_objectid;
  1598. key.offset = chunk_offset;
  1599. key.type = BTRFS_CHUNK_ITEM_KEY;
  1600. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  1601. BUG_ON(ret);
  1602. ret = btrfs_del_item(trans, root, path);
  1603. btrfs_free_path(path);
  1604. return ret;
  1605. }
  1606. static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
  1607. chunk_offset)
  1608. {
  1609. struct btrfs_super_block *super_copy = root->fs_info->super_copy;
  1610. struct btrfs_disk_key *disk_key;
  1611. struct btrfs_chunk *chunk;
  1612. u8 *ptr;
  1613. int ret = 0;
  1614. u32 num_stripes;
  1615. u32 array_size;
  1616. u32 len = 0;
  1617. u32 cur;
  1618. struct btrfs_key key;
  1619. array_size = btrfs_super_sys_array_size(super_copy);
  1620. ptr = super_copy->sys_chunk_array;
  1621. cur = 0;
  1622. while (cur < array_size) {
  1623. disk_key = (struct btrfs_disk_key *)ptr;
  1624. btrfs_disk_key_to_cpu(&key, disk_key);
  1625. len = sizeof(*disk_key);
  1626. if (key.type == BTRFS_CHUNK_ITEM_KEY) {
  1627. chunk = (struct btrfs_chunk *)(ptr + len);
  1628. num_stripes = btrfs_stack_chunk_num_stripes(chunk);
  1629. len += btrfs_chunk_item_size(num_stripes);
  1630. } else {
  1631. ret = -EIO;
  1632. break;
  1633. }
  1634. if (key.objectid == chunk_objectid &&
  1635. key.offset == chunk_offset) {
  1636. memmove(ptr, ptr + len, array_size - (cur + len));
  1637. array_size -= len;
  1638. btrfs_set_super_sys_array_size(super_copy, array_size);
  1639. } else {
  1640. ptr += len;
  1641. cur += len;
  1642. }
  1643. }
  1644. return ret;
  1645. }
  1646. static int btrfs_relocate_chunk(struct btrfs_root *root,
  1647. u64 chunk_tree, u64 chunk_objectid,
  1648. u64 chunk_offset)
  1649. {
  1650. struct extent_map_tree *em_tree;
  1651. struct btrfs_root *extent_root;
  1652. struct btrfs_trans_handle *trans;
  1653. struct extent_map *em;
  1654. struct map_lookup *map;
  1655. int ret;
  1656. int i;
  1657. root = root->fs_info->chunk_root;
  1658. extent_root = root->fs_info->extent_root;
  1659. em_tree = &root->fs_info->mapping_tree.map_tree;
  1660. ret = btrfs_can_relocate(extent_root, chunk_offset);
  1661. if (ret)
  1662. return -ENOSPC;
  1663. /* step one, relocate all the extents inside this chunk */
  1664. ret = btrfs_relocate_block_group(extent_root, chunk_offset);
  1665. if (ret)
  1666. return ret;
  1667. trans = btrfs_start_transaction(root, 0);
  1668. BUG_ON(IS_ERR(trans));
  1669. lock_chunks(root);
  1670. /*
  1671. * step two, delete the device extents and the
  1672. * chunk tree entries
  1673. */
  1674. read_lock(&em_tree->lock);
  1675. em = lookup_extent_mapping(em_tree, chunk_offset, 1);
  1676. read_unlock(&em_tree->lock);
  1677. BUG_ON(em->start > chunk_offset ||
  1678. em->start + em->len < chunk_offset);
  1679. map = (struct map_lookup *)em->bdev;
  1680. for (i = 0; i < map->num_stripes; i++) {
  1681. ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
  1682. map->stripes[i].physical);
  1683. BUG_ON(ret);
  1684. if (map->stripes[i].dev) {
  1685. ret = btrfs_update_device(trans, map->stripes[i].dev);
  1686. BUG_ON(ret);
  1687. }
  1688. }
  1689. ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
  1690. chunk_offset);
  1691. BUG_ON(ret);
  1692. trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
  1693. if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
  1694. ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
  1695. BUG_ON(ret);
  1696. }
  1697. ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
  1698. BUG_ON(ret);
  1699. write_lock(&em_tree->lock);
  1700. remove_extent_mapping(em_tree, em);
  1701. write_unlock(&em_tree->lock);
  1702. kfree(map);
  1703. em->bdev = NULL;
  1704. /* once for the tree */
  1705. free_extent_map(em);
  1706. /* once for us */
  1707. free_extent_map(em);
  1708. unlock_chunks(root);
  1709. btrfs_end_transaction(trans, root);
  1710. return 0;
  1711. }
  1712. static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
  1713. {
  1714. struct btrfs_root *chunk_root = root->fs_info->chunk_root;
  1715. struct btrfs_path *path;
  1716. struct extent_buffer *leaf;
  1717. struct btrfs_chunk *chunk;
  1718. struct btrfs_key key;
  1719. struct btrfs_key found_key;
  1720. u64 chunk_tree = chunk_root->root_key.objectid;
  1721. u64 chunk_type;
  1722. bool retried = false;
  1723. int failed = 0;
  1724. int ret;
  1725. path = btrfs_alloc_path();
  1726. if (!path)
  1727. return -ENOMEM;
  1728. again:
  1729. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  1730. key.offset = (u64)-1;
  1731. key.type = BTRFS_CHUNK_ITEM_KEY;
  1732. while (1) {
  1733. ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
  1734. if (ret < 0)
  1735. goto error;
  1736. BUG_ON(ret == 0);
  1737. ret = btrfs_previous_item(chunk_root, path, key.objectid,
  1738. key.type);
  1739. if (ret < 0)
  1740. goto error;
  1741. if (ret > 0)
  1742. break;
  1743. leaf = path->nodes[0];
  1744. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  1745. chunk = btrfs_item_ptr(leaf, path->slots[0],
  1746. struct btrfs_chunk);
  1747. chunk_type = btrfs_chunk_type(leaf, chunk);
  1748. btrfs_release_path(path);
  1749. if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
  1750. ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
  1751. found_key.objectid,
  1752. found_key.offset);
  1753. if (ret == -ENOSPC)
  1754. failed++;
  1755. else if (ret)
  1756. BUG();
  1757. }
  1758. if (found_key.offset == 0)
  1759. break;
  1760. key.offset = found_key.offset - 1;
  1761. }
  1762. ret = 0;
  1763. if (failed && !retried) {
  1764. failed = 0;
  1765. retried = true;
  1766. goto again;
  1767. } else if (failed && retried) {
  1768. WARN_ON(1);
  1769. ret = -ENOSPC;
  1770. }
  1771. error:
  1772. btrfs_free_path(path);
  1773. return ret;
  1774. }
  1775. /*
  1776. * Should be called with both balance and volume mutexes held to
  1777. * serialize other volume operations (add_dev/rm_dev/resize) with
  1778. * restriper. Same goes for unset_balance_control.
  1779. */
  1780. static void set_balance_control(struct btrfs_balance_control *bctl)
  1781. {
  1782. struct btrfs_fs_info *fs_info = bctl->fs_info;
  1783. BUG_ON(fs_info->balance_ctl);
  1784. spin_lock(&fs_info->balance_lock);
  1785. fs_info->balance_ctl = bctl;
  1786. spin_unlock(&fs_info->balance_lock);
  1787. }
  1788. static void unset_balance_control(struct btrfs_fs_info *fs_info)
  1789. {
  1790. struct btrfs_balance_control *bctl = fs_info->balance_ctl;
  1791. BUG_ON(!fs_info->balance_ctl);
  1792. spin_lock(&fs_info->balance_lock);
  1793. fs_info->balance_ctl = NULL;
  1794. spin_unlock(&fs_info->balance_lock);
  1795. kfree(bctl);
  1796. }
  1797. /*
  1798. * Balance filters. Return 1 if chunk should be filtered out
  1799. * (should not be balanced).
  1800. */
  1801. static int chunk_profiles_filter(u64 chunk_profile,
  1802. struct btrfs_balance_args *bargs)
  1803. {
  1804. chunk_profile &= BTRFS_BLOCK_GROUP_PROFILE_MASK;
  1805. if (chunk_profile == 0)
  1806. chunk_profile = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
  1807. if (bargs->profiles & chunk_profile)
  1808. return 0;
  1809. return 1;
  1810. }
  1811. static u64 div_factor_fine(u64 num, int factor)
  1812. {
  1813. if (factor <= 0)
  1814. return 0;
  1815. if (factor >= 100)
  1816. return num;
  1817. num *= factor;
  1818. do_div(num, 100);
  1819. return num;
  1820. }
  1821. static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
  1822. struct btrfs_balance_args *bargs)
  1823. {
  1824. struct btrfs_block_group_cache *cache;
  1825. u64 chunk_used, user_thresh;
  1826. int ret = 1;
  1827. cache = btrfs_lookup_block_group(fs_info, chunk_offset);
  1828. chunk_used = btrfs_block_group_used(&cache->item);
  1829. user_thresh = div_factor_fine(cache->key.offset, bargs->usage);
  1830. if (chunk_used < user_thresh)
  1831. ret = 0;
  1832. btrfs_put_block_group(cache);
  1833. return ret;
  1834. }
  1835. static int chunk_devid_filter(struct extent_buffer *leaf,
  1836. struct btrfs_chunk *chunk,
  1837. struct btrfs_balance_args *bargs)
  1838. {
  1839. struct btrfs_stripe *stripe;
  1840. int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
  1841. int i;
  1842. for (i = 0; i < num_stripes; i++) {
  1843. stripe = btrfs_stripe_nr(chunk, i);
  1844. if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
  1845. return 0;
  1846. }
  1847. return 1;
  1848. }
  1849. /* [pstart, pend) */
  1850. static int chunk_drange_filter(struct extent_buffer *leaf,
  1851. struct btrfs_chunk *chunk,
  1852. u64 chunk_offset,
  1853. struct btrfs_balance_args *bargs)
  1854. {
  1855. struct btrfs_stripe *stripe;
  1856. int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
  1857. u64 stripe_offset;
  1858. u64 stripe_length;
  1859. int factor;
  1860. int i;
  1861. if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
  1862. return 0;
  1863. if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
  1864. BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10))
  1865. factor = 2;
  1866. else
  1867. factor = 1;
  1868. factor = num_stripes / factor;
  1869. for (i = 0; i < num_stripes; i++) {
  1870. stripe = btrfs_stripe_nr(chunk, i);
  1871. if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
  1872. continue;
  1873. stripe_offset = btrfs_stripe_offset(leaf, stripe);
  1874. stripe_length = btrfs_chunk_length(leaf, chunk);
  1875. do_div(stripe_length, factor);
  1876. if (stripe_offset < bargs->pend &&
  1877. stripe_offset + stripe_length > bargs->pstart)
  1878. return 0;
  1879. }
  1880. return 1;
  1881. }
  1882. /* [vstart, vend) */
  1883. static int chunk_vrange_filter(struct extent_buffer *leaf,
  1884. struct btrfs_chunk *chunk,
  1885. u64 chunk_offset,
  1886. struct btrfs_balance_args *bargs)
  1887. {
  1888. if (chunk_offset < bargs->vend &&
  1889. chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
  1890. /* at least part of the chunk is inside this vrange */
  1891. return 0;
  1892. return 1;
  1893. }
  1894. static int should_balance_chunk(struct btrfs_root *root,
  1895. struct extent_buffer *leaf,
  1896. struct btrfs_chunk *chunk, u64 chunk_offset)
  1897. {
  1898. struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
  1899. struct btrfs_balance_args *bargs = NULL;
  1900. u64 chunk_type = btrfs_chunk_type(leaf, chunk);
  1901. /* type filter */
  1902. if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
  1903. (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
  1904. return 0;
  1905. }
  1906. if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
  1907. bargs = &bctl->data;
  1908. else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
  1909. bargs = &bctl->sys;
  1910. else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
  1911. bargs = &bctl->meta;
  1912. /* profiles filter */
  1913. if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
  1914. chunk_profiles_filter(chunk_type, bargs)) {
  1915. return 0;
  1916. }
  1917. /* usage filter */
  1918. if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
  1919. chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
  1920. return 0;
  1921. }
  1922. /* devid filter */
  1923. if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
  1924. chunk_devid_filter(leaf, chunk, bargs)) {
  1925. return 0;
  1926. }
  1927. /* drange filter, makes sense only with devid filter */
  1928. if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
  1929. chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
  1930. return 0;
  1931. }
  1932. /* vrange filter */
  1933. if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
  1934. chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
  1935. return 0;
  1936. }
  1937. return 1;
  1938. }
  1939. static u64 div_factor(u64 num, int factor)
  1940. {
  1941. if (factor == 10)
  1942. return num;
  1943. num *= factor;
  1944. do_div(num, 10);
  1945. return num;
  1946. }
  1947. static int __btrfs_balance(struct btrfs_fs_info *fs_info)
  1948. {
  1949. struct btrfs_root *chunk_root = fs_info->chunk_root;
  1950. struct btrfs_root *dev_root = fs_info->dev_root;
  1951. struct list_head *devices;
  1952. struct btrfs_device *device;
  1953. u64 old_size;
  1954. u64 size_to_free;
  1955. struct btrfs_chunk *chunk;
  1956. struct btrfs_path *path;
  1957. struct btrfs_key key;
  1958. struct btrfs_key found_key;
  1959. struct btrfs_trans_handle *trans;
  1960. struct extent_buffer *leaf;
  1961. int slot;
  1962. int ret;
  1963. int enospc_errors = 0;
  1964. /* step one make some room on all the devices */
  1965. devices = &fs_info->fs_devices->devices;
  1966. list_for_each_entry(device, devices, dev_list) {
  1967. old_size = device->total_bytes;
  1968. size_to_free = div_factor(old_size, 1);
  1969. size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
  1970. if (!device->writeable ||
  1971. device->total_bytes - device->bytes_used > size_to_free)
  1972. continue;
  1973. ret = btrfs_shrink_device(device, old_size - size_to_free);
  1974. if (ret == -ENOSPC)
  1975. break;
  1976. BUG_ON(ret);
  1977. trans = btrfs_start_transaction(dev_root, 0);
  1978. BUG_ON(IS_ERR(trans));
  1979. ret = btrfs_grow_device(trans, device, old_size);
  1980. BUG_ON(ret);
  1981. btrfs_end_transaction(trans, dev_root);
  1982. }
  1983. /* step two, relocate all the chunks */
  1984. path = btrfs_alloc_path();
  1985. if (!path) {
  1986. ret = -ENOMEM;
  1987. goto error;
  1988. }
  1989. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  1990. key.offset = (u64)-1;
  1991. key.type = BTRFS_CHUNK_ITEM_KEY;
  1992. while (1) {
  1993. ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
  1994. if (ret < 0)
  1995. goto error;
  1996. /*
  1997. * this shouldn't happen, it means the last relocate
  1998. * failed
  1999. */
  2000. if (ret == 0)
  2001. BUG(); /* FIXME break ? */
  2002. ret = btrfs_previous_item(chunk_root, path, 0,
  2003. BTRFS_CHUNK_ITEM_KEY);
  2004. if (ret) {
  2005. ret = 0;
  2006. break;
  2007. }
  2008. leaf = path->nodes[0];
  2009. slot = path->slots[0];
  2010. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  2011. if (found_key.objectid != key.objectid)
  2012. break;
  2013. /* chunk zero is special */
  2014. if (found_key.offset == 0)
  2015. break;
  2016. chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
  2017. ret = should_balance_chunk(chunk_root, leaf, chunk,
  2018. found_key.offset);
  2019. btrfs_release_path(path);
  2020. if (!ret)
  2021. goto loop;
  2022. ret = btrfs_relocate_chunk(chunk_root,
  2023. chunk_root->root_key.objectid,
  2024. found_key.objectid,
  2025. found_key.offset);
  2026. if (ret && ret != -ENOSPC)
  2027. goto error;
  2028. if (ret == -ENOSPC)
  2029. enospc_errors++;
  2030. loop:
  2031. key.offset = found_key.offset - 1;
  2032. }
  2033. error:
  2034. btrfs_free_path(path);
  2035. if (enospc_errors) {
  2036. printk(KERN_INFO "btrfs: %d enospc errors during balance\n",
  2037. enospc_errors);
  2038. if (!ret)
  2039. ret = -ENOSPC;
  2040. }
  2041. return ret;
  2042. }
  2043. static void __cancel_balance(struct btrfs_fs_info *fs_info)
  2044. {
  2045. unset_balance_control(fs_info);
  2046. }
  2047. void update_ioctl_balance_args(struct btrfs_fs_info *fs_info,
  2048. struct btrfs_ioctl_balance_args *bargs);
  2049. /*
  2050. * Should be called with both balance and volume mutexes held
  2051. */
  2052. int btrfs_balance(struct btrfs_balance_control *bctl,
  2053. struct btrfs_ioctl_balance_args *bargs)
  2054. {
  2055. struct btrfs_fs_info *fs_info = bctl->fs_info;
  2056. u64 allowed;
  2057. int ret;
  2058. if (btrfs_fs_closing(fs_info)) {
  2059. ret = -EINVAL;
  2060. goto out;
  2061. }
  2062. /*
  2063. * In case of mixed groups both data and meta should be picked,
  2064. * and identical options should be given for both of them.
  2065. */
  2066. allowed = btrfs_super_incompat_flags(fs_info->super_copy);
  2067. if ((allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) &&
  2068. (bctl->flags & (BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA))) {
  2069. if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
  2070. !(bctl->flags & BTRFS_BALANCE_METADATA) ||
  2071. memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
  2072. printk(KERN_ERR "btrfs: with mixed groups data and "
  2073. "metadata balance options must be the same\n");
  2074. ret = -EINVAL;
  2075. goto out;
  2076. }
  2077. }
  2078. set_balance_control(bctl);
  2079. mutex_unlock(&fs_info->balance_mutex);
  2080. ret = __btrfs_balance(fs_info);
  2081. mutex_lock(&fs_info->balance_mutex);
  2082. if (bargs) {
  2083. memset(bargs, 0, sizeof(*bargs));
  2084. update_ioctl_balance_args(fs_info, bargs);
  2085. }
  2086. __cancel_balance(fs_info);
  2087. return ret;
  2088. out:
  2089. kfree(bctl);
  2090. return ret;
  2091. }
  2092. /*
  2093. * shrinking a device means finding all of the device extents past
  2094. * the new size, and then following the back refs to the chunks.
  2095. * The chunk relocation code actually frees the device extent
  2096. */
  2097. int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
  2098. {
  2099. struct btrfs_trans_handle *trans;
  2100. struct btrfs_root *root = device->dev_root;
  2101. struct btrfs_dev_extent *dev_extent = NULL;
  2102. struct btrfs_path *path;
  2103. u64 length;
  2104. u64 chunk_tree;
  2105. u64 chunk_objectid;
  2106. u64 chunk_offset;
  2107. int ret;
  2108. int slot;
  2109. int failed = 0;
  2110. bool retried = false;
  2111. struct extent_buffer *l;
  2112. struct btrfs_key key;
  2113. struct btrfs_super_block *super_copy = root->fs_info->super_copy;
  2114. u64 old_total = btrfs_super_total_bytes(super_copy);
  2115. u64 old_size = device->total_bytes;
  2116. u64 diff = device->total_bytes - new_size;
  2117. if (new_size >= device->total_bytes)
  2118. return -EINVAL;
  2119. path = btrfs_alloc_path();
  2120. if (!path)
  2121. return -ENOMEM;
  2122. path->reada = 2;
  2123. lock_chunks(root);
  2124. device->total_bytes = new_size;
  2125. if (device->writeable) {
  2126. device->fs_devices->total_rw_bytes -= diff;
  2127. spin_lock(&root->fs_info->free_chunk_lock);
  2128. root->fs_info->free_chunk_space -= diff;
  2129. spin_unlock(&root->fs_info->free_chunk_lock);
  2130. }
  2131. unlock_chunks(root);
  2132. again:
  2133. key.objectid = device->devid;
  2134. key.offset = (u64)-1;
  2135. key.type = BTRFS_DEV_EXTENT_KEY;
  2136. while (1) {
  2137. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2138. if (ret < 0)
  2139. goto done;
  2140. ret = btrfs_previous_item(root, path, 0, key.type);
  2141. if (ret < 0)
  2142. goto done;
  2143. if (ret) {
  2144. ret = 0;
  2145. btrfs_release_path(path);
  2146. break;
  2147. }
  2148. l = path->nodes[0];
  2149. slot = path->slots[0];
  2150. btrfs_item_key_to_cpu(l, &key, path->slots[0]);
  2151. if (key.objectid != device->devid) {
  2152. btrfs_release_path(path);
  2153. break;
  2154. }
  2155. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  2156. length = btrfs_dev_extent_length(l, dev_extent);
  2157. if (key.offset + length <= new_size) {
  2158. btrfs_release_path(path);
  2159. break;
  2160. }
  2161. chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
  2162. chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
  2163. chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
  2164. btrfs_release_path(path);
  2165. ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
  2166. chunk_offset);
  2167. if (ret && ret != -ENOSPC)
  2168. goto done;
  2169. if (ret == -ENOSPC)
  2170. failed++;
  2171. key.offset -= 1;
  2172. }
  2173. if (failed && !retried) {
  2174. failed = 0;
  2175. retried = true;
  2176. goto again;
  2177. } else if (failed && retried) {
  2178. ret = -ENOSPC;
  2179. lock_chunks(root);
  2180. device->total_bytes = old_size;
  2181. if (device->writeable)
  2182. device->fs_devices->total_rw_bytes += diff;
  2183. spin_lock(&root->fs_info->free_chunk_lock);
  2184. root->fs_info->free_chunk_space += diff;
  2185. spin_unlock(&root->fs_info->free_chunk_lock);
  2186. unlock_chunks(root);
  2187. goto done;
  2188. }
  2189. /* Shrinking succeeded, else we would be at "done". */
  2190. trans = btrfs_start_transaction(root, 0);
  2191. if (IS_ERR(trans)) {
  2192. ret = PTR_ERR(trans);
  2193. goto done;
  2194. }
  2195. lock_chunks(root);
  2196. device->disk_total_bytes = new_size;
  2197. /* Now btrfs_update_device() will change the on-disk size. */
  2198. ret = btrfs_update_device(trans, device);
  2199. if (ret) {
  2200. unlock_chunks(root);
  2201. btrfs_end_transaction(trans, root);
  2202. goto done;
  2203. }
  2204. WARN_ON(diff > old_total);
  2205. btrfs_set_super_total_bytes(super_copy, old_total - diff);
  2206. unlock_chunks(root);
  2207. btrfs_end_transaction(trans, root);
  2208. done:
  2209. btrfs_free_path(path);
  2210. return ret;
  2211. }
  2212. static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
  2213. struct btrfs_root *root,
  2214. struct btrfs_key *key,
  2215. struct btrfs_chunk *chunk, int item_size)
  2216. {
  2217. struct btrfs_super_block *super_copy = root->fs_info->super_copy;
  2218. struct btrfs_disk_key disk_key;
  2219. u32 array_size;
  2220. u8 *ptr;
  2221. array_size = btrfs_super_sys_array_size(super_copy);
  2222. if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
  2223. return -EFBIG;
  2224. ptr = super_copy->sys_chunk_array + array_size;
  2225. btrfs_cpu_key_to_disk(&disk_key, key);
  2226. memcpy(ptr, &disk_key, sizeof(disk_key));
  2227. ptr += sizeof(disk_key);
  2228. memcpy(ptr, chunk, item_size);
  2229. item_size += sizeof(disk_key);
  2230. btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
  2231. return 0;
  2232. }
  2233. /*
  2234. * sort the devices in descending order by max_avail, total_avail
  2235. */
  2236. static int btrfs_cmp_device_info(const void *a, const void *b)
  2237. {
  2238. const struct btrfs_device_info *di_a = a;
  2239. const struct btrfs_device_info *di_b = b;
  2240. if (di_a->max_avail > di_b->max_avail)
  2241. return -1;
  2242. if (di_a->max_avail < di_b->max_avail)
  2243. return 1;
  2244. if (di_a->total_avail > di_b->total_avail)
  2245. return -1;
  2246. if (di_a->total_avail < di_b->total_avail)
  2247. return 1;
  2248. return 0;
  2249. }
  2250. static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
  2251. struct btrfs_root *extent_root,
  2252. struct map_lookup **map_ret,
  2253. u64 *num_bytes_out, u64 *stripe_size_out,
  2254. u64 start, u64 type)
  2255. {
  2256. struct btrfs_fs_info *info = extent_root->fs_info;
  2257. struct btrfs_fs_devices *fs_devices = info->fs_devices;
  2258. struct list_head *cur;
  2259. struct map_lookup *map = NULL;
  2260. struct extent_map_tree *em_tree;
  2261. struct extent_map *em;
  2262. struct btrfs_device_info *devices_info = NULL;
  2263. u64 total_avail;
  2264. int num_stripes; /* total number of stripes to allocate */
  2265. int sub_stripes; /* sub_stripes info for map */
  2266. int dev_stripes; /* stripes per dev */
  2267. int devs_max; /* max devs to use */
  2268. int devs_min; /* min devs needed */
  2269. int devs_increment; /* ndevs has to be a multiple of this */
  2270. int ncopies; /* how many copies to data has */
  2271. int ret;
  2272. u64 max_stripe_size;
  2273. u64 max_chunk_size;
  2274. u64 stripe_size;
  2275. u64 num_bytes;
  2276. int ndevs;
  2277. int i;
  2278. int j;
  2279. if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
  2280. (type & BTRFS_BLOCK_GROUP_DUP)) {
  2281. WARN_ON(1);
  2282. type &= ~BTRFS_BLOCK_GROUP_DUP;
  2283. }
  2284. if (list_empty(&fs_devices->alloc_list))
  2285. return -ENOSPC;
  2286. sub_stripes = 1;
  2287. dev_stripes = 1;
  2288. devs_increment = 1;
  2289. ncopies = 1;
  2290. devs_max = 0; /* 0 == as many as possible */
  2291. devs_min = 1;
  2292. /*
  2293. * define the properties of each RAID type.
  2294. * FIXME: move this to a global table and use it in all RAID
  2295. * calculation code
  2296. */
  2297. if (type & (BTRFS_BLOCK_GROUP_DUP)) {
  2298. dev_stripes = 2;
  2299. ncopies = 2;
  2300. devs_max = 1;
  2301. } else if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
  2302. devs_min = 2;
  2303. } else if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
  2304. devs_increment = 2;
  2305. ncopies = 2;
  2306. devs_max = 2;
  2307. devs_min = 2;
  2308. } else if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
  2309. sub_stripes = 2;
  2310. devs_increment = 2;
  2311. ncopies = 2;
  2312. devs_min = 4;
  2313. } else {
  2314. devs_max = 1;
  2315. }
  2316. if (type & BTRFS_BLOCK_GROUP_DATA) {
  2317. max_stripe_size = 1024 * 1024 * 1024;
  2318. max_chunk_size = 10 * max_stripe_size;
  2319. } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
  2320. max_stripe_size = 256 * 1024 * 1024;
  2321. max_chunk_size = max_stripe_size;
  2322. } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
  2323. max_stripe_size = 8 * 1024 * 1024;
  2324. max_chunk_size = 2 * max_stripe_size;
  2325. } else {
  2326. printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
  2327. type);
  2328. BUG_ON(1);
  2329. }
  2330. /* we don't want a chunk larger than 10% of writeable space */
  2331. max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
  2332. max_chunk_size);
  2333. devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
  2334. GFP_NOFS);
  2335. if (!devices_info)
  2336. return -ENOMEM;
  2337. cur = fs_devices->alloc_list.next;
  2338. /*
  2339. * in the first pass through the devices list, we gather information
  2340. * about the available holes on each device.
  2341. */
  2342. ndevs = 0;
  2343. while (cur != &fs_devices->alloc_list) {
  2344. struct btrfs_device *device;
  2345. u64 max_avail;
  2346. u64 dev_offset;
  2347. device = list_entry(cur, struct btrfs_device, dev_alloc_list);
  2348. cur = cur->next;
  2349. if (!device->writeable) {
  2350. printk(KERN_ERR
  2351. "btrfs: read-only device in alloc_list\n");
  2352. WARN_ON(1);
  2353. continue;
  2354. }
  2355. if (!device->in_fs_metadata)
  2356. continue;
  2357. if (device->total_bytes > device->bytes_used)
  2358. total_avail = device->total_bytes - device->bytes_used;
  2359. else
  2360. total_avail = 0;
  2361. /* If there is no space on this device, skip it. */
  2362. if (total_avail == 0)
  2363. continue;
  2364. ret = find_free_dev_extent(trans, device,
  2365. max_stripe_size * dev_stripes,
  2366. &dev_offset, &max_avail);
  2367. if (ret && ret != -ENOSPC)
  2368. goto error;
  2369. if (ret == 0)
  2370. max_avail = max_stripe_size * dev_stripes;
  2371. if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
  2372. continue;
  2373. devices_info[ndevs].dev_offset = dev_offset;
  2374. devices_info[ndevs].max_avail = max_avail;
  2375. devices_info[ndevs].total_avail = total_avail;
  2376. devices_info[ndevs].dev = device;
  2377. ++ndevs;
  2378. }
  2379. /*
  2380. * now sort the devices by hole size / available space
  2381. */
  2382. sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
  2383. btrfs_cmp_device_info, NULL);
  2384. /* round down to number of usable stripes */
  2385. ndevs -= ndevs % devs_increment;
  2386. if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
  2387. ret = -ENOSPC;
  2388. goto error;
  2389. }
  2390. if (devs_max && ndevs > devs_max)
  2391. ndevs = devs_max;
  2392. /*
  2393. * the primary goal is to maximize the number of stripes, so use as many
  2394. * devices as possible, even if the stripes are not maximum sized.
  2395. */
  2396. stripe_size = devices_info[ndevs-1].max_avail;
  2397. num_stripes = ndevs * dev_stripes;
  2398. if (stripe_size * num_stripes > max_chunk_size * ncopies) {
  2399. stripe_size = max_chunk_size * ncopies;
  2400. do_div(stripe_size, num_stripes);
  2401. }
  2402. do_div(stripe_size, dev_stripes);
  2403. do_div(stripe_size, BTRFS_STRIPE_LEN);
  2404. stripe_size *= BTRFS_STRIPE_LEN;
  2405. map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
  2406. if (!map) {
  2407. ret = -ENOMEM;
  2408. goto error;
  2409. }
  2410. map->num_stripes = num_stripes;
  2411. for (i = 0; i < ndevs; ++i) {
  2412. for (j = 0; j < dev_stripes; ++j) {
  2413. int s = i * dev_stripes + j;
  2414. map->stripes[s].dev = devices_info[i].dev;
  2415. map->stripes[s].physical = devices_info[i].dev_offset +
  2416. j * stripe_size;
  2417. }
  2418. }
  2419. map->sector_size = extent_root->sectorsize;
  2420. map->stripe_len = BTRFS_STRIPE_LEN;
  2421. map->io_align = BTRFS_STRIPE_LEN;
  2422. map->io_width = BTRFS_STRIPE_LEN;
  2423. map->type = type;
  2424. map->sub_stripes = sub_stripes;
  2425. *map_ret = map;
  2426. num_bytes = stripe_size * (num_stripes / ncopies);
  2427. *stripe_size_out = stripe_size;
  2428. *num_bytes_out = num_bytes;
  2429. trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
  2430. em = alloc_extent_map();
  2431. if (!em) {
  2432. ret = -ENOMEM;
  2433. goto error;
  2434. }
  2435. em->bdev = (struct block_device *)map;
  2436. em->start = start;
  2437. em->len = num_bytes;
  2438. em->block_start = 0;
  2439. em->block_len = em->len;
  2440. em_tree = &extent_root->fs_info->mapping_tree.map_tree;
  2441. write_lock(&em_tree->lock);
  2442. ret = add_extent_mapping(em_tree, em);
  2443. write_unlock(&em_tree->lock);
  2444. BUG_ON(ret);
  2445. free_extent_map(em);
  2446. ret = btrfs_make_block_group(trans, extent_root, 0, type,
  2447. BTRFS_FIRST_CHUNK_TREE_OBJECTID,
  2448. start, num_bytes);
  2449. BUG_ON(ret);
  2450. for (i = 0; i < map->num_stripes; ++i) {
  2451. struct btrfs_device *device;
  2452. u64 dev_offset;
  2453. device = map->stripes[i].dev;
  2454. dev_offset = map->stripes[i].physical;
  2455. ret = btrfs_alloc_dev_extent(trans, device,
  2456. info->chunk_root->root_key.objectid,
  2457. BTRFS_FIRST_CHUNK_TREE_OBJECTID,
  2458. start, dev_offset, stripe_size);
  2459. BUG_ON(ret);
  2460. }
  2461. kfree(devices_info);
  2462. return 0;
  2463. error:
  2464. kfree(map);
  2465. kfree(devices_info);
  2466. return ret;
  2467. }
  2468. static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
  2469. struct btrfs_root *extent_root,
  2470. struct map_lookup *map, u64 chunk_offset,
  2471. u64 chunk_size, u64 stripe_size)
  2472. {
  2473. u64 dev_offset;
  2474. struct btrfs_key key;
  2475. struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
  2476. struct btrfs_device *device;
  2477. struct btrfs_chunk *chunk;
  2478. struct btrfs_stripe *stripe;
  2479. size_t item_size = btrfs_chunk_item_size(map->num_stripes);
  2480. int index = 0;
  2481. int ret;
  2482. chunk = kzalloc(item_size, GFP_NOFS);
  2483. if (!chunk)
  2484. return -ENOMEM;
  2485. index = 0;
  2486. while (index < map->num_stripes) {
  2487. device = map->stripes[index].dev;
  2488. device->bytes_used += stripe_size;
  2489. ret = btrfs_update_device(trans, device);
  2490. BUG_ON(ret);
  2491. index++;
  2492. }
  2493. spin_lock(&extent_root->fs_info->free_chunk_lock);
  2494. extent_root->fs_info->free_chunk_space -= (stripe_size *
  2495. map->num_stripes);
  2496. spin_unlock(&extent_root->fs_info->free_chunk_lock);
  2497. index = 0;
  2498. stripe = &chunk->stripe;
  2499. while (index < map->num_stripes) {
  2500. device = map->stripes[index].dev;
  2501. dev_offset = map->stripes[index].physical;
  2502. btrfs_set_stack_stripe_devid(stripe, device->devid);
  2503. btrfs_set_stack_stripe_offset(stripe, dev_offset);
  2504. memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
  2505. stripe++;
  2506. index++;
  2507. }
  2508. btrfs_set_stack_chunk_length(chunk, chunk_size);
  2509. btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
  2510. btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
  2511. btrfs_set_stack_chunk_type(chunk, map->type);
  2512. btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
  2513. btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
  2514. btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
  2515. btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
  2516. btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
  2517. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  2518. key.type = BTRFS_CHUNK_ITEM_KEY;
  2519. key.offset = chunk_offset;
  2520. ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
  2521. BUG_ON(ret);
  2522. if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
  2523. ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
  2524. item_size);
  2525. BUG_ON(ret);
  2526. }
  2527. kfree(chunk);
  2528. return 0;
  2529. }
  2530. /*
  2531. * Chunk allocation falls into two parts. The first part does works
  2532. * that make the new allocated chunk useable, but not do any operation
  2533. * that modifies the chunk tree. The second part does the works that
  2534. * require modifying the chunk tree. This division is important for the
  2535. * bootstrap process of adding storage to a seed btrfs.
  2536. */
  2537. int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
  2538. struct btrfs_root *extent_root, u64 type)
  2539. {
  2540. u64 chunk_offset;
  2541. u64 chunk_size;
  2542. u64 stripe_size;
  2543. struct map_lookup *map;
  2544. struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
  2545. int ret;
  2546. ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
  2547. &chunk_offset);
  2548. if (ret)
  2549. return ret;
  2550. ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
  2551. &stripe_size, chunk_offset, type);
  2552. if (ret)
  2553. return ret;
  2554. ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
  2555. chunk_size, stripe_size);
  2556. BUG_ON(ret);
  2557. return 0;
  2558. }
  2559. static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
  2560. struct btrfs_root *root,
  2561. struct btrfs_device *device)
  2562. {
  2563. u64 chunk_offset;
  2564. u64 sys_chunk_offset;
  2565. u64 chunk_size;
  2566. u64 sys_chunk_size;
  2567. u64 stripe_size;
  2568. u64 sys_stripe_size;
  2569. u64 alloc_profile;
  2570. struct map_lookup *map;
  2571. struct map_lookup *sys_map;
  2572. struct btrfs_fs_info *fs_info = root->fs_info;
  2573. struct btrfs_root *extent_root = fs_info->extent_root;
  2574. int ret;
  2575. ret = find_next_chunk(fs_info->chunk_root,
  2576. BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
  2577. if (ret)
  2578. return ret;
  2579. alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
  2580. fs_info->avail_metadata_alloc_bits;
  2581. alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
  2582. ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
  2583. &stripe_size, chunk_offset, alloc_profile);
  2584. BUG_ON(ret);
  2585. sys_chunk_offset = chunk_offset + chunk_size;
  2586. alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
  2587. fs_info->avail_system_alloc_bits;
  2588. alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
  2589. ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
  2590. &sys_chunk_size, &sys_stripe_size,
  2591. sys_chunk_offset, alloc_profile);
  2592. BUG_ON(ret);
  2593. ret = btrfs_add_device(trans, fs_info->chunk_root, device);
  2594. BUG_ON(ret);
  2595. /*
  2596. * Modifying chunk tree needs allocating new blocks from both
  2597. * system block group and metadata block group. So we only can
  2598. * do operations require modifying the chunk tree after both
  2599. * block groups were created.
  2600. */
  2601. ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
  2602. chunk_size, stripe_size);
  2603. BUG_ON(ret);
  2604. ret = __finish_chunk_alloc(trans, extent_root, sys_map,
  2605. sys_chunk_offset, sys_chunk_size,
  2606. sys_stripe_size);
  2607. BUG_ON(ret);
  2608. return 0;
  2609. }
  2610. int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
  2611. {
  2612. struct extent_map *em;
  2613. struct map_lookup *map;
  2614. struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
  2615. int readonly = 0;
  2616. int i;
  2617. read_lock(&map_tree->map_tree.lock);
  2618. em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
  2619. read_unlock(&map_tree->map_tree.lock);
  2620. if (!em)
  2621. return 1;
  2622. if (btrfs_test_opt(root, DEGRADED)) {
  2623. free_extent_map(em);
  2624. return 0;
  2625. }
  2626. map = (struct map_lookup *)em->bdev;
  2627. for (i = 0; i < map->num_stripes; i++) {
  2628. if (!map->stripes[i].dev->writeable) {
  2629. readonly = 1;
  2630. break;
  2631. }
  2632. }
  2633. free_extent_map(em);
  2634. return readonly;
  2635. }
  2636. void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
  2637. {
  2638. extent_map_tree_init(&tree->map_tree);
  2639. }
  2640. void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
  2641. {
  2642. struct extent_map *em;
  2643. while (1) {
  2644. write_lock(&tree->map_tree.lock);
  2645. em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
  2646. if (em)
  2647. remove_extent_mapping(&tree->map_tree, em);
  2648. write_unlock(&tree->map_tree.lock);
  2649. if (!em)
  2650. break;
  2651. kfree(em->bdev);
  2652. /* once for us */
  2653. free_extent_map(em);
  2654. /* once for the tree */
  2655. free_extent_map(em);
  2656. }
  2657. }
  2658. int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
  2659. {
  2660. struct extent_map *em;
  2661. struct map_lookup *map;
  2662. struct extent_map_tree *em_tree = &map_tree->map_tree;
  2663. int ret;
  2664. read_lock(&em_tree->lock);
  2665. em = lookup_extent_mapping(em_tree, logical, len);
  2666. read_unlock(&em_tree->lock);
  2667. BUG_ON(!em);
  2668. BUG_ON(em->start > logical || em->start + em->len < logical);
  2669. map = (struct map_lookup *)em->bdev;
  2670. if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
  2671. ret = map->num_stripes;
  2672. else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
  2673. ret = map->sub_stripes;
  2674. else
  2675. ret = 1;
  2676. free_extent_map(em);
  2677. return ret;
  2678. }
  2679. static int find_live_mirror(struct map_lookup *map, int first, int num,
  2680. int optimal)
  2681. {
  2682. int i;
  2683. if (map->stripes[optimal].dev->bdev)
  2684. return optimal;
  2685. for (i = first; i < first + num; i++) {
  2686. if (map->stripes[i].dev->bdev)
  2687. return i;
  2688. }
  2689. /* we couldn't find one that doesn't fail. Just return something
  2690. * and the io error handling code will clean up eventually
  2691. */
  2692. return optimal;
  2693. }
  2694. static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
  2695. u64 logical, u64 *length,
  2696. struct btrfs_bio **bbio_ret,
  2697. int mirror_num)
  2698. {
  2699. struct extent_map *em;
  2700. struct map_lookup *map;
  2701. struct extent_map_tree *em_tree = &map_tree->map_tree;
  2702. u64 offset;
  2703. u64 stripe_offset;
  2704. u64 stripe_end_offset;
  2705. u64 stripe_nr;
  2706. u64 stripe_nr_orig;
  2707. u64 stripe_nr_end;
  2708. int stripes_allocated = 8;
  2709. int stripes_required = 1;
  2710. int stripe_index;
  2711. int i;
  2712. int num_stripes;
  2713. int max_errors = 0;
  2714. struct btrfs_bio *bbio = NULL;
  2715. if (bbio_ret && !(rw & (REQ_WRITE | REQ_DISCARD)))
  2716. stripes_allocated = 1;
  2717. again:
  2718. if (bbio_ret) {
  2719. bbio = kzalloc(btrfs_bio_size(stripes_allocated),
  2720. GFP_NOFS);
  2721. if (!bbio)
  2722. return -ENOMEM;
  2723. atomic_set(&bbio->error, 0);
  2724. }
  2725. read_lock(&em_tree->lock);
  2726. em = lookup_extent_mapping(em_tree, logical, *length);
  2727. read_unlock(&em_tree->lock);
  2728. if (!em) {
  2729. printk(KERN_CRIT "unable to find logical %llu len %llu\n",
  2730. (unsigned long long)logical,
  2731. (unsigned long long)*length);
  2732. BUG();
  2733. }
  2734. BUG_ON(em->start > logical || em->start + em->len < logical);
  2735. map = (struct map_lookup *)em->bdev;
  2736. offset = logical - em->start;
  2737. if (mirror_num > map->num_stripes)
  2738. mirror_num = 0;
  2739. /* if our btrfs_bio struct is too small, back off and try again */
  2740. if (rw & REQ_WRITE) {
  2741. if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
  2742. BTRFS_BLOCK_GROUP_DUP)) {
  2743. stripes_required = map->num_stripes;
  2744. max_errors = 1;
  2745. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  2746. stripes_required = map->sub_stripes;
  2747. max_errors = 1;
  2748. }
  2749. }
  2750. if (rw & REQ_DISCARD) {
  2751. if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK)
  2752. stripes_required = map->num_stripes;
  2753. }
  2754. if (bbio_ret && (rw & (REQ_WRITE | REQ_DISCARD)) &&
  2755. stripes_allocated < stripes_required) {
  2756. stripes_allocated = map->num_stripes;
  2757. free_extent_map(em);
  2758. kfree(bbio);
  2759. goto again;
  2760. }
  2761. stripe_nr = offset;
  2762. /*
  2763. * stripe_nr counts the total number of stripes we have to stride
  2764. * to get to this block
  2765. */
  2766. do_div(stripe_nr, map->stripe_len);
  2767. stripe_offset = stripe_nr * map->stripe_len;
  2768. BUG_ON(offset < stripe_offset);
  2769. /* stripe_offset is the offset of this block in its stripe*/
  2770. stripe_offset = offset - stripe_offset;
  2771. if (rw & REQ_DISCARD)
  2772. *length = min_t(u64, em->len - offset, *length);
  2773. else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
  2774. /* we limit the length of each bio to what fits in a stripe */
  2775. *length = min_t(u64, em->len - offset,
  2776. map->stripe_len - stripe_offset);
  2777. } else {
  2778. *length = em->len - offset;
  2779. }
  2780. if (!bbio_ret)
  2781. goto out;
  2782. num_stripes = 1;
  2783. stripe_index = 0;
  2784. stripe_nr_orig = stripe_nr;
  2785. stripe_nr_end = (offset + *length + map->stripe_len - 1) &
  2786. (~(map->stripe_len - 1));
  2787. do_div(stripe_nr_end, map->stripe_len);
  2788. stripe_end_offset = stripe_nr_end * map->stripe_len -
  2789. (offset + *length);
  2790. if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
  2791. if (rw & REQ_DISCARD)
  2792. num_stripes = min_t(u64, map->num_stripes,
  2793. stripe_nr_end - stripe_nr_orig);
  2794. stripe_index = do_div(stripe_nr, map->num_stripes);
  2795. } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
  2796. if (rw & (REQ_WRITE | REQ_DISCARD))
  2797. num_stripes = map->num_stripes;
  2798. else if (mirror_num)
  2799. stripe_index = mirror_num - 1;
  2800. else {
  2801. stripe_index = find_live_mirror(map, 0,
  2802. map->num_stripes,
  2803. current->pid % map->num_stripes);
  2804. mirror_num = stripe_index + 1;
  2805. }
  2806. } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
  2807. if (rw & (REQ_WRITE | REQ_DISCARD)) {
  2808. num_stripes = map->num_stripes;
  2809. } else if (mirror_num) {
  2810. stripe_index = mirror_num - 1;
  2811. } else {
  2812. mirror_num = 1;
  2813. }
  2814. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  2815. int factor = map->num_stripes / map->sub_stripes;
  2816. stripe_index = do_div(stripe_nr, factor);
  2817. stripe_index *= map->sub_stripes;
  2818. if (rw & REQ_WRITE)
  2819. num_stripes = map->sub_stripes;
  2820. else if (rw & REQ_DISCARD)
  2821. num_stripes = min_t(u64, map->sub_stripes *
  2822. (stripe_nr_end - stripe_nr_orig),
  2823. map->num_stripes);
  2824. else if (mirror_num)
  2825. stripe_index += mirror_num - 1;
  2826. else {
  2827. stripe_index = find_live_mirror(map, stripe_index,
  2828. map->sub_stripes, stripe_index +
  2829. current->pid % map->sub_stripes);
  2830. mirror_num = stripe_index + 1;
  2831. }
  2832. } else {
  2833. /*
  2834. * after this do_div call, stripe_nr is the number of stripes
  2835. * on this device we have to walk to find the data, and
  2836. * stripe_index is the number of our device in the stripe array
  2837. */
  2838. stripe_index = do_div(stripe_nr, map->num_stripes);
  2839. mirror_num = stripe_index + 1;
  2840. }
  2841. BUG_ON(stripe_index >= map->num_stripes);
  2842. if (rw & REQ_DISCARD) {
  2843. for (i = 0; i < num_stripes; i++) {
  2844. bbio->stripes[i].physical =
  2845. map->stripes[stripe_index].physical +
  2846. stripe_offset + stripe_nr * map->stripe_len;
  2847. bbio->stripes[i].dev = map->stripes[stripe_index].dev;
  2848. if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
  2849. u64 stripes;
  2850. u32 last_stripe = 0;
  2851. int j;
  2852. div_u64_rem(stripe_nr_end - 1,
  2853. map->num_stripes,
  2854. &last_stripe);
  2855. for (j = 0; j < map->num_stripes; j++) {
  2856. u32 test;
  2857. div_u64_rem(stripe_nr_end - 1 - j,
  2858. map->num_stripes, &test);
  2859. if (test == stripe_index)
  2860. break;
  2861. }
  2862. stripes = stripe_nr_end - 1 - j;
  2863. do_div(stripes, map->num_stripes);
  2864. bbio->stripes[i].length = map->stripe_len *
  2865. (stripes - stripe_nr + 1);
  2866. if (i == 0) {
  2867. bbio->stripes[i].length -=
  2868. stripe_offset;
  2869. stripe_offset = 0;
  2870. }
  2871. if (stripe_index == last_stripe)
  2872. bbio->stripes[i].length -=
  2873. stripe_end_offset;
  2874. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  2875. u64 stripes;
  2876. int j;
  2877. int factor = map->num_stripes /
  2878. map->sub_stripes;
  2879. u32 last_stripe = 0;
  2880. div_u64_rem(stripe_nr_end - 1,
  2881. factor, &last_stripe);
  2882. last_stripe *= map->sub_stripes;
  2883. for (j = 0; j < factor; j++) {
  2884. u32 test;
  2885. div_u64_rem(stripe_nr_end - 1 - j,
  2886. factor, &test);
  2887. if (test ==
  2888. stripe_index / map->sub_stripes)
  2889. break;
  2890. }
  2891. stripes = stripe_nr_end - 1 - j;
  2892. do_div(stripes, factor);
  2893. bbio->stripes[i].length = map->stripe_len *
  2894. (stripes - stripe_nr + 1);
  2895. if (i < map->sub_stripes) {
  2896. bbio->stripes[i].length -=
  2897. stripe_offset;
  2898. if (i == map->sub_stripes - 1)
  2899. stripe_offset = 0;
  2900. }
  2901. if (stripe_index >= last_stripe &&
  2902. stripe_index <= (last_stripe +
  2903. map->sub_stripes - 1)) {
  2904. bbio->stripes[i].length -=
  2905. stripe_end_offset;
  2906. }
  2907. } else
  2908. bbio->stripes[i].length = *length;
  2909. stripe_index++;
  2910. if (stripe_index == map->num_stripes) {
  2911. /* This could only happen for RAID0/10 */
  2912. stripe_index = 0;
  2913. stripe_nr++;
  2914. }
  2915. }
  2916. } else {
  2917. for (i = 0; i < num_stripes; i++) {
  2918. bbio->stripes[i].physical =
  2919. map->stripes[stripe_index].physical +
  2920. stripe_offset +
  2921. stripe_nr * map->stripe_len;
  2922. bbio->stripes[i].dev =
  2923. map->stripes[stripe_index].dev;
  2924. stripe_index++;
  2925. }
  2926. }
  2927. if (bbio_ret) {
  2928. *bbio_ret = bbio;
  2929. bbio->num_stripes = num_stripes;
  2930. bbio->max_errors = max_errors;
  2931. bbio->mirror_num = mirror_num;
  2932. }
  2933. out:
  2934. free_extent_map(em);
  2935. return 0;
  2936. }
  2937. int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
  2938. u64 logical, u64 *length,
  2939. struct btrfs_bio **bbio_ret, int mirror_num)
  2940. {
  2941. return __btrfs_map_block(map_tree, rw, logical, length, bbio_ret,
  2942. mirror_num);
  2943. }
  2944. int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
  2945. u64 chunk_start, u64 physical, u64 devid,
  2946. u64 **logical, int *naddrs, int *stripe_len)
  2947. {
  2948. struct extent_map_tree *em_tree = &map_tree->map_tree;
  2949. struct extent_map *em;
  2950. struct map_lookup *map;
  2951. u64 *buf;
  2952. u64 bytenr;
  2953. u64 length;
  2954. u64 stripe_nr;
  2955. int i, j, nr = 0;
  2956. read_lock(&em_tree->lock);
  2957. em = lookup_extent_mapping(em_tree, chunk_start, 1);
  2958. read_unlock(&em_tree->lock);
  2959. BUG_ON(!em || em->start != chunk_start);
  2960. map = (struct map_lookup *)em->bdev;
  2961. length = em->len;
  2962. if (map->type & BTRFS_BLOCK_GROUP_RAID10)
  2963. do_div(length, map->num_stripes / map->sub_stripes);
  2964. else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
  2965. do_div(length, map->num_stripes);
  2966. buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
  2967. BUG_ON(!buf);
  2968. for (i = 0; i < map->num_stripes; i++) {
  2969. if (devid && map->stripes[i].dev->devid != devid)
  2970. continue;
  2971. if (map->stripes[i].physical > physical ||
  2972. map->stripes[i].physical + length <= physical)
  2973. continue;
  2974. stripe_nr = physical - map->stripes[i].physical;
  2975. do_div(stripe_nr, map->stripe_len);
  2976. if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  2977. stripe_nr = stripe_nr * map->num_stripes + i;
  2978. do_div(stripe_nr, map->sub_stripes);
  2979. } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
  2980. stripe_nr = stripe_nr * map->num_stripes + i;
  2981. }
  2982. bytenr = chunk_start + stripe_nr * map->stripe_len;
  2983. WARN_ON(nr >= map->num_stripes);
  2984. for (j = 0; j < nr; j++) {
  2985. if (buf[j] == bytenr)
  2986. break;
  2987. }
  2988. if (j == nr) {
  2989. WARN_ON(nr >= map->num_stripes);
  2990. buf[nr++] = bytenr;
  2991. }
  2992. }
  2993. *logical = buf;
  2994. *naddrs = nr;
  2995. *stripe_len = map->stripe_len;
  2996. free_extent_map(em);
  2997. return 0;
  2998. }
  2999. static void btrfs_end_bio(struct bio *bio, int err)
  3000. {
  3001. struct btrfs_bio *bbio = bio->bi_private;
  3002. int is_orig_bio = 0;
  3003. if (err)
  3004. atomic_inc(&bbio->error);
  3005. if (bio == bbio->orig_bio)
  3006. is_orig_bio = 1;
  3007. if (atomic_dec_and_test(&bbio->stripes_pending)) {
  3008. if (!is_orig_bio) {
  3009. bio_put(bio);
  3010. bio = bbio->orig_bio;
  3011. }
  3012. bio->bi_private = bbio->private;
  3013. bio->bi_end_io = bbio->end_io;
  3014. bio->bi_bdev = (struct block_device *)
  3015. (unsigned long)bbio->mirror_num;
  3016. /* only send an error to the higher layers if it is
  3017. * beyond the tolerance of the multi-bio
  3018. */
  3019. if (atomic_read(&bbio->error) > bbio->max_errors) {
  3020. err = -EIO;
  3021. } else {
  3022. /*
  3023. * this bio is actually up to date, we didn't
  3024. * go over the max number of errors
  3025. */
  3026. set_bit(BIO_UPTODATE, &bio->bi_flags);
  3027. err = 0;
  3028. }
  3029. kfree(bbio);
  3030. bio_endio(bio, err);
  3031. } else if (!is_orig_bio) {
  3032. bio_put(bio);
  3033. }
  3034. }
  3035. struct async_sched {
  3036. struct bio *bio;
  3037. int rw;
  3038. struct btrfs_fs_info *info;
  3039. struct btrfs_work work;
  3040. };
  3041. /*
  3042. * see run_scheduled_bios for a description of why bios are collected for
  3043. * async submit.
  3044. *
  3045. * This will add one bio to the pending list for a device and make sure
  3046. * the work struct is scheduled.
  3047. */
  3048. static noinline int schedule_bio(struct btrfs_root *root,
  3049. struct btrfs_device *device,
  3050. int rw, struct bio *bio)
  3051. {
  3052. int should_queue = 1;
  3053. struct btrfs_pending_bios *pending_bios;
  3054. /* don't bother with additional async steps for reads, right now */
  3055. if (!(rw & REQ_WRITE)) {
  3056. bio_get(bio);
  3057. submit_bio(rw, bio);
  3058. bio_put(bio);
  3059. return 0;
  3060. }
  3061. /*
  3062. * nr_async_bios allows us to reliably return congestion to the
  3063. * higher layers. Otherwise, the async bio makes it appear we have
  3064. * made progress against dirty pages when we've really just put it
  3065. * on a queue for later
  3066. */
  3067. atomic_inc(&root->fs_info->nr_async_bios);
  3068. WARN_ON(bio->bi_next);
  3069. bio->bi_next = NULL;
  3070. bio->bi_rw |= rw;
  3071. spin_lock(&device->io_lock);
  3072. if (bio->bi_rw & REQ_SYNC)
  3073. pending_bios = &device->pending_sync_bios;
  3074. else
  3075. pending_bios = &device->pending_bios;
  3076. if (pending_bios->tail)
  3077. pending_bios->tail->bi_next = bio;
  3078. pending_bios->tail = bio;
  3079. if (!pending_bios->head)
  3080. pending_bios->head = bio;
  3081. if (device->running_pending)
  3082. should_queue = 0;
  3083. spin_unlock(&device->io_lock);
  3084. if (should_queue)
  3085. btrfs_queue_worker(&root->fs_info->submit_workers,
  3086. &device->work);
  3087. return 0;
  3088. }
  3089. int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
  3090. int mirror_num, int async_submit)
  3091. {
  3092. struct btrfs_mapping_tree *map_tree;
  3093. struct btrfs_device *dev;
  3094. struct bio *first_bio = bio;
  3095. u64 logical = (u64)bio->bi_sector << 9;
  3096. u64 length = 0;
  3097. u64 map_length;
  3098. int ret;
  3099. int dev_nr = 0;
  3100. int total_devs = 1;
  3101. struct btrfs_bio *bbio = NULL;
  3102. length = bio->bi_size;
  3103. map_tree = &root->fs_info->mapping_tree;
  3104. map_length = length;
  3105. ret = btrfs_map_block(map_tree, rw, logical, &map_length, &bbio,
  3106. mirror_num);
  3107. BUG_ON(ret);
  3108. total_devs = bbio->num_stripes;
  3109. if (map_length < length) {
  3110. printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
  3111. "len %llu\n", (unsigned long long)logical,
  3112. (unsigned long long)length,
  3113. (unsigned long long)map_length);
  3114. BUG();
  3115. }
  3116. bbio->orig_bio = first_bio;
  3117. bbio->private = first_bio->bi_private;
  3118. bbio->end_io = first_bio->bi_end_io;
  3119. atomic_set(&bbio->stripes_pending, bbio->num_stripes);
  3120. while (dev_nr < total_devs) {
  3121. if (dev_nr < total_devs - 1) {
  3122. bio = bio_clone(first_bio, GFP_NOFS);
  3123. BUG_ON(!bio);
  3124. } else {
  3125. bio = first_bio;
  3126. }
  3127. bio->bi_private = bbio;
  3128. bio->bi_end_io = btrfs_end_bio;
  3129. bio->bi_sector = bbio->stripes[dev_nr].physical >> 9;
  3130. dev = bbio->stripes[dev_nr].dev;
  3131. if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
  3132. pr_debug("btrfs_map_bio: rw %d, secor=%llu, dev=%lu "
  3133. "(%s id %llu), size=%u\n", rw,
  3134. (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
  3135. dev->name, dev->devid, bio->bi_size);
  3136. bio->bi_bdev = dev->bdev;
  3137. if (async_submit)
  3138. schedule_bio(root, dev, rw, bio);
  3139. else
  3140. submit_bio(rw, bio);
  3141. } else {
  3142. bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
  3143. bio->bi_sector = logical >> 9;
  3144. bio_endio(bio, -EIO);
  3145. }
  3146. dev_nr++;
  3147. }
  3148. return 0;
  3149. }
  3150. struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
  3151. u8 *uuid, u8 *fsid)
  3152. {
  3153. struct btrfs_device *device;
  3154. struct btrfs_fs_devices *cur_devices;
  3155. cur_devices = root->fs_info->fs_devices;
  3156. while (cur_devices) {
  3157. if (!fsid ||
  3158. !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
  3159. device = __find_device(&cur_devices->devices,
  3160. devid, uuid);
  3161. if (device)
  3162. return device;
  3163. }
  3164. cur_devices = cur_devices->seed;
  3165. }
  3166. return NULL;
  3167. }
  3168. static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
  3169. u64 devid, u8 *dev_uuid)
  3170. {
  3171. struct btrfs_device *device;
  3172. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  3173. device = kzalloc(sizeof(*device), GFP_NOFS);
  3174. if (!device)
  3175. return NULL;
  3176. list_add(&device->dev_list,
  3177. &fs_devices->devices);
  3178. device->dev_root = root->fs_info->dev_root;
  3179. device->devid = devid;
  3180. device->work.func = pending_bios_fn;
  3181. device->fs_devices = fs_devices;
  3182. device->missing = 1;
  3183. fs_devices->num_devices++;
  3184. fs_devices->missing_devices++;
  3185. spin_lock_init(&device->io_lock);
  3186. INIT_LIST_HEAD(&device->dev_alloc_list);
  3187. memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
  3188. return device;
  3189. }
  3190. static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
  3191. struct extent_buffer *leaf,
  3192. struct btrfs_chunk *chunk)
  3193. {
  3194. struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
  3195. struct map_lookup *map;
  3196. struct extent_map *em;
  3197. u64 logical;
  3198. u64 length;
  3199. u64 devid;
  3200. u8 uuid[BTRFS_UUID_SIZE];
  3201. int num_stripes;
  3202. int ret;
  3203. int i;
  3204. logical = key->offset;
  3205. length = btrfs_chunk_length(leaf, chunk);
  3206. read_lock(&map_tree->map_tree.lock);
  3207. em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
  3208. read_unlock(&map_tree->map_tree.lock);
  3209. /* already mapped? */
  3210. if (em && em->start <= logical && em->start + em->len > logical) {
  3211. free_extent_map(em);
  3212. return 0;
  3213. } else if (em) {
  3214. free_extent_map(em);
  3215. }
  3216. em = alloc_extent_map();
  3217. if (!em)
  3218. return -ENOMEM;
  3219. num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
  3220. map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
  3221. if (!map) {
  3222. free_extent_map(em);
  3223. return -ENOMEM;
  3224. }
  3225. em->bdev = (struct block_device *)map;
  3226. em->start = logical;
  3227. em->len = length;
  3228. em->block_start = 0;
  3229. em->block_len = em->len;
  3230. map->num_stripes = num_stripes;
  3231. map->io_width = btrfs_chunk_io_width(leaf, chunk);
  3232. map->io_align = btrfs_chunk_io_align(leaf, chunk);
  3233. map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
  3234. map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
  3235. map->type = btrfs_chunk_type(leaf, chunk);
  3236. map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
  3237. for (i = 0; i < num_stripes; i++) {
  3238. map->stripes[i].physical =
  3239. btrfs_stripe_offset_nr(leaf, chunk, i);
  3240. devid = btrfs_stripe_devid_nr(leaf, chunk, i);
  3241. read_extent_buffer(leaf, uuid, (unsigned long)
  3242. btrfs_stripe_dev_uuid_nr(chunk, i),
  3243. BTRFS_UUID_SIZE);
  3244. map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
  3245. NULL);
  3246. if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
  3247. kfree(map);
  3248. free_extent_map(em);
  3249. return -EIO;
  3250. }
  3251. if (!map->stripes[i].dev) {
  3252. map->stripes[i].dev =
  3253. add_missing_dev(root, devid, uuid);
  3254. if (!map->stripes[i].dev) {
  3255. kfree(map);
  3256. free_extent_map(em);
  3257. return -EIO;
  3258. }
  3259. }
  3260. map->stripes[i].dev->in_fs_metadata = 1;
  3261. }
  3262. write_lock(&map_tree->map_tree.lock);
  3263. ret = add_extent_mapping(&map_tree->map_tree, em);
  3264. write_unlock(&map_tree->map_tree.lock);
  3265. BUG_ON(ret);
  3266. free_extent_map(em);
  3267. return 0;
  3268. }
  3269. static int fill_device_from_item(struct extent_buffer *leaf,
  3270. struct btrfs_dev_item *dev_item,
  3271. struct btrfs_device *device)
  3272. {
  3273. unsigned long ptr;
  3274. device->devid = btrfs_device_id(leaf, dev_item);
  3275. device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
  3276. device->total_bytes = device->disk_total_bytes;
  3277. device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
  3278. device->type = btrfs_device_type(leaf, dev_item);
  3279. device->io_align = btrfs_device_io_align(leaf, dev_item);
  3280. device->io_width = btrfs_device_io_width(leaf, dev_item);
  3281. device->sector_size = btrfs_device_sector_size(leaf, dev_item);
  3282. ptr = (unsigned long)btrfs_device_uuid(dev_item);
  3283. read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
  3284. return 0;
  3285. }
  3286. static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
  3287. {
  3288. struct btrfs_fs_devices *fs_devices;
  3289. int ret;
  3290. mutex_lock(&uuid_mutex);
  3291. fs_devices = root->fs_info->fs_devices->seed;
  3292. while (fs_devices) {
  3293. if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
  3294. ret = 0;
  3295. goto out;
  3296. }
  3297. fs_devices = fs_devices->seed;
  3298. }
  3299. fs_devices = find_fsid(fsid);
  3300. if (!fs_devices) {
  3301. ret = -ENOENT;
  3302. goto out;
  3303. }
  3304. fs_devices = clone_fs_devices(fs_devices);
  3305. if (IS_ERR(fs_devices)) {
  3306. ret = PTR_ERR(fs_devices);
  3307. goto out;
  3308. }
  3309. ret = __btrfs_open_devices(fs_devices, FMODE_READ,
  3310. root->fs_info->bdev_holder);
  3311. if (ret)
  3312. goto out;
  3313. if (!fs_devices->seeding) {
  3314. __btrfs_close_devices(fs_devices);
  3315. free_fs_devices(fs_devices);
  3316. ret = -EINVAL;
  3317. goto out;
  3318. }
  3319. fs_devices->seed = root->fs_info->fs_devices->seed;
  3320. root->fs_info->fs_devices->seed = fs_devices;
  3321. out:
  3322. mutex_unlock(&uuid_mutex);
  3323. return ret;
  3324. }
  3325. static int read_one_dev(struct btrfs_root *root,
  3326. struct extent_buffer *leaf,
  3327. struct btrfs_dev_item *dev_item)
  3328. {
  3329. struct btrfs_device *device;
  3330. u64 devid;
  3331. int ret;
  3332. u8 fs_uuid[BTRFS_UUID_SIZE];
  3333. u8 dev_uuid[BTRFS_UUID_SIZE];
  3334. devid = btrfs_device_id(leaf, dev_item);
  3335. read_extent_buffer(leaf, dev_uuid,
  3336. (unsigned long)btrfs_device_uuid(dev_item),
  3337. BTRFS_UUID_SIZE);
  3338. read_extent_buffer(leaf, fs_uuid,
  3339. (unsigned long)btrfs_device_fsid(dev_item),
  3340. BTRFS_UUID_SIZE);
  3341. if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
  3342. ret = open_seed_devices(root, fs_uuid);
  3343. if (ret && !btrfs_test_opt(root, DEGRADED))
  3344. return ret;
  3345. }
  3346. device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
  3347. if (!device || !device->bdev) {
  3348. if (!btrfs_test_opt(root, DEGRADED))
  3349. return -EIO;
  3350. if (!device) {
  3351. printk(KERN_WARNING "warning devid %llu missing\n",
  3352. (unsigned long long)devid);
  3353. device = add_missing_dev(root, devid, dev_uuid);
  3354. if (!device)
  3355. return -ENOMEM;
  3356. } else if (!device->missing) {
  3357. /*
  3358. * this happens when a device that was properly setup
  3359. * in the device info lists suddenly goes bad.
  3360. * device->bdev is NULL, and so we have to set
  3361. * device->missing to one here
  3362. */
  3363. root->fs_info->fs_devices->missing_devices++;
  3364. device->missing = 1;
  3365. }
  3366. }
  3367. if (device->fs_devices != root->fs_info->fs_devices) {
  3368. BUG_ON(device->writeable);
  3369. if (device->generation !=
  3370. btrfs_device_generation(leaf, dev_item))
  3371. return -EINVAL;
  3372. }
  3373. fill_device_from_item(leaf, dev_item, device);
  3374. device->dev_root = root->fs_info->dev_root;
  3375. device->in_fs_metadata = 1;
  3376. if (device->writeable) {
  3377. device->fs_devices->total_rw_bytes += device->total_bytes;
  3378. spin_lock(&root->fs_info->free_chunk_lock);
  3379. root->fs_info->free_chunk_space += device->total_bytes -
  3380. device->bytes_used;
  3381. spin_unlock(&root->fs_info->free_chunk_lock);
  3382. }
  3383. ret = 0;
  3384. return ret;
  3385. }
  3386. int btrfs_read_sys_array(struct btrfs_root *root)
  3387. {
  3388. struct btrfs_super_block *super_copy = root->fs_info->super_copy;
  3389. struct extent_buffer *sb;
  3390. struct btrfs_disk_key *disk_key;
  3391. struct btrfs_chunk *chunk;
  3392. u8 *ptr;
  3393. unsigned long sb_ptr;
  3394. int ret = 0;
  3395. u32 num_stripes;
  3396. u32 array_size;
  3397. u32 len = 0;
  3398. u32 cur;
  3399. struct btrfs_key key;
  3400. sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
  3401. BTRFS_SUPER_INFO_SIZE);
  3402. if (!sb)
  3403. return -ENOMEM;
  3404. btrfs_set_buffer_uptodate(sb);
  3405. btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
  3406. write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
  3407. array_size = btrfs_super_sys_array_size(super_copy);
  3408. ptr = super_copy->sys_chunk_array;
  3409. sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
  3410. cur = 0;
  3411. while (cur < array_size) {
  3412. disk_key = (struct btrfs_disk_key *)ptr;
  3413. btrfs_disk_key_to_cpu(&key, disk_key);
  3414. len = sizeof(*disk_key); ptr += len;
  3415. sb_ptr += len;
  3416. cur += len;
  3417. if (key.type == BTRFS_CHUNK_ITEM_KEY) {
  3418. chunk = (struct btrfs_chunk *)sb_ptr;
  3419. ret = read_one_chunk(root, &key, sb, chunk);
  3420. if (ret)
  3421. break;
  3422. num_stripes = btrfs_chunk_num_stripes(sb, chunk);
  3423. len = btrfs_chunk_item_size(num_stripes);
  3424. } else {
  3425. ret = -EIO;
  3426. break;
  3427. }
  3428. ptr += len;
  3429. sb_ptr += len;
  3430. cur += len;
  3431. }
  3432. free_extent_buffer(sb);
  3433. return ret;
  3434. }
  3435. int btrfs_read_chunk_tree(struct btrfs_root *root)
  3436. {
  3437. struct btrfs_path *path;
  3438. struct extent_buffer *leaf;
  3439. struct btrfs_key key;
  3440. struct btrfs_key found_key;
  3441. int ret;
  3442. int slot;
  3443. root = root->fs_info->chunk_root;
  3444. path = btrfs_alloc_path();
  3445. if (!path)
  3446. return -ENOMEM;
  3447. /* first we search for all of the device items, and then we
  3448. * read in all of the chunk items. This way we can create chunk
  3449. * mappings that reference all of the devices that are afound
  3450. */
  3451. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  3452. key.offset = 0;
  3453. key.type = 0;
  3454. again:
  3455. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3456. if (ret < 0)
  3457. goto error;
  3458. while (1) {
  3459. leaf = path->nodes[0];
  3460. slot = path->slots[0];
  3461. if (slot >= btrfs_header_nritems(leaf)) {
  3462. ret = btrfs_next_leaf(root, path);
  3463. if (ret == 0)
  3464. continue;
  3465. if (ret < 0)
  3466. goto error;
  3467. break;
  3468. }
  3469. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  3470. if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
  3471. if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
  3472. break;
  3473. if (found_key.type == BTRFS_DEV_ITEM_KEY) {
  3474. struct btrfs_dev_item *dev_item;
  3475. dev_item = btrfs_item_ptr(leaf, slot,
  3476. struct btrfs_dev_item);
  3477. ret = read_one_dev(root, leaf, dev_item);
  3478. if (ret)
  3479. goto error;
  3480. }
  3481. } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
  3482. struct btrfs_chunk *chunk;
  3483. chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
  3484. ret = read_one_chunk(root, &found_key, leaf, chunk);
  3485. if (ret)
  3486. goto error;
  3487. }
  3488. path->slots[0]++;
  3489. }
  3490. if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
  3491. key.objectid = 0;
  3492. btrfs_release_path(path);
  3493. goto again;
  3494. }
  3495. ret = 0;
  3496. error:
  3497. btrfs_free_path(path);
  3498. return ret;
  3499. }