volumes.c 95 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804
  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. mutex_lock(&root->fs_info->volume_mutex);
  1109. all_avail = root->fs_info->avail_data_alloc_bits |
  1110. root->fs_info->avail_system_alloc_bits |
  1111. root->fs_info->avail_metadata_alloc_bits;
  1112. if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
  1113. root->fs_info->fs_devices->num_devices <= 4) {
  1114. printk(KERN_ERR "btrfs: unable to go below four devices "
  1115. "on raid10\n");
  1116. ret = -EINVAL;
  1117. goto out;
  1118. }
  1119. if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
  1120. root->fs_info->fs_devices->num_devices <= 2) {
  1121. printk(KERN_ERR "btrfs: unable to go below two "
  1122. "devices on raid1\n");
  1123. ret = -EINVAL;
  1124. goto out;
  1125. }
  1126. if (strcmp(device_path, "missing") == 0) {
  1127. struct list_head *devices;
  1128. struct btrfs_device *tmp;
  1129. device = NULL;
  1130. devices = &root->fs_info->fs_devices->devices;
  1131. /*
  1132. * It is safe to read the devices since the volume_mutex
  1133. * is held.
  1134. */
  1135. list_for_each_entry(tmp, devices, dev_list) {
  1136. if (tmp->in_fs_metadata && !tmp->bdev) {
  1137. device = tmp;
  1138. break;
  1139. }
  1140. }
  1141. bdev = NULL;
  1142. bh = NULL;
  1143. disk_super = NULL;
  1144. if (!device) {
  1145. printk(KERN_ERR "btrfs: no missing devices found to "
  1146. "remove\n");
  1147. goto out;
  1148. }
  1149. } else {
  1150. bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL,
  1151. root->fs_info->bdev_holder);
  1152. if (IS_ERR(bdev)) {
  1153. ret = PTR_ERR(bdev);
  1154. goto out;
  1155. }
  1156. set_blocksize(bdev, 4096);
  1157. bh = btrfs_read_dev_super(bdev);
  1158. if (!bh) {
  1159. ret = -EINVAL;
  1160. goto error_close;
  1161. }
  1162. disk_super = (struct btrfs_super_block *)bh->b_data;
  1163. devid = btrfs_stack_device_id(&disk_super->dev_item);
  1164. dev_uuid = disk_super->dev_item.uuid;
  1165. device = btrfs_find_device(root, devid, dev_uuid,
  1166. disk_super->fsid);
  1167. if (!device) {
  1168. ret = -ENOENT;
  1169. goto error_brelse;
  1170. }
  1171. }
  1172. if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
  1173. printk(KERN_ERR "btrfs: unable to remove the only writeable "
  1174. "device\n");
  1175. ret = -EINVAL;
  1176. goto error_brelse;
  1177. }
  1178. if (device->writeable) {
  1179. lock_chunks(root);
  1180. list_del_init(&device->dev_alloc_list);
  1181. unlock_chunks(root);
  1182. root->fs_info->fs_devices->rw_devices--;
  1183. clear_super = true;
  1184. }
  1185. ret = btrfs_shrink_device(device, 0);
  1186. if (ret)
  1187. goto error_undo;
  1188. ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
  1189. if (ret)
  1190. goto error_undo;
  1191. spin_lock(&root->fs_info->free_chunk_lock);
  1192. root->fs_info->free_chunk_space = device->total_bytes -
  1193. device->bytes_used;
  1194. spin_unlock(&root->fs_info->free_chunk_lock);
  1195. device->in_fs_metadata = 0;
  1196. btrfs_scrub_cancel_dev(root, device);
  1197. /*
  1198. * the device list mutex makes sure that we don't change
  1199. * the device list while someone else is writing out all
  1200. * the device supers.
  1201. */
  1202. cur_devices = device->fs_devices;
  1203. mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
  1204. list_del_rcu(&device->dev_list);
  1205. device->fs_devices->num_devices--;
  1206. if (device->missing)
  1207. root->fs_info->fs_devices->missing_devices--;
  1208. next_device = list_entry(root->fs_info->fs_devices->devices.next,
  1209. struct btrfs_device, dev_list);
  1210. if (device->bdev == root->fs_info->sb->s_bdev)
  1211. root->fs_info->sb->s_bdev = next_device->bdev;
  1212. if (device->bdev == root->fs_info->fs_devices->latest_bdev)
  1213. root->fs_info->fs_devices->latest_bdev = next_device->bdev;
  1214. if (device->bdev)
  1215. device->fs_devices->open_devices--;
  1216. call_rcu(&device->rcu, free_device);
  1217. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1218. num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
  1219. btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
  1220. if (cur_devices->open_devices == 0) {
  1221. struct btrfs_fs_devices *fs_devices;
  1222. fs_devices = root->fs_info->fs_devices;
  1223. while (fs_devices) {
  1224. if (fs_devices->seed == cur_devices)
  1225. break;
  1226. fs_devices = fs_devices->seed;
  1227. }
  1228. fs_devices->seed = cur_devices->seed;
  1229. cur_devices->seed = NULL;
  1230. lock_chunks(root);
  1231. __btrfs_close_devices(cur_devices);
  1232. unlock_chunks(root);
  1233. free_fs_devices(cur_devices);
  1234. }
  1235. /*
  1236. * at this point, the device is zero sized. We want to
  1237. * remove it from the devices list and zero out the old super
  1238. */
  1239. if (clear_super) {
  1240. /* make sure this device isn't detected as part of
  1241. * the FS anymore
  1242. */
  1243. memset(&disk_super->magic, 0, sizeof(disk_super->magic));
  1244. set_buffer_dirty(bh);
  1245. sync_dirty_buffer(bh);
  1246. }
  1247. ret = 0;
  1248. error_brelse:
  1249. brelse(bh);
  1250. error_close:
  1251. if (bdev)
  1252. blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
  1253. out:
  1254. mutex_unlock(&root->fs_info->volume_mutex);
  1255. mutex_unlock(&uuid_mutex);
  1256. return ret;
  1257. error_undo:
  1258. if (device->writeable) {
  1259. lock_chunks(root);
  1260. list_add(&device->dev_alloc_list,
  1261. &root->fs_info->fs_devices->alloc_list);
  1262. unlock_chunks(root);
  1263. root->fs_info->fs_devices->rw_devices++;
  1264. }
  1265. goto error_brelse;
  1266. }
  1267. /*
  1268. * does all the dirty work required for changing file system's UUID.
  1269. */
  1270. static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans,
  1271. struct btrfs_root *root)
  1272. {
  1273. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  1274. struct btrfs_fs_devices *old_devices;
  1275. struct btrfs_fs_devices *seed_devices;
  1276. struct btrfs_super_block *disk_super = root->fs_info->super_copy;
  1277. struct btrfs_device *device;
  1278. u64 super_flags;
  1279. BUG_ON(!mutex_is_locked(&uuid_mutex));
  1280. if (!fs_devices->seeding)
  1281. return -EINVAL;
  1282. seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
  1283. if (!seed_devices)
  1284. return -ENOMEM;
  1285. old_devices = clone_fs_devices(fs_devices);
  1286. if (IS_ERR(old_devices)) {
  1287. kfree(seed_devices);
  1288. return PTR_ERR(old_devices);
  1289. }
  1290. list_add(&old_devices->list, &fs_uuids);
  1291. memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
  1292. seed_devices->opened = 1;
  1293. INIT_LIST_HEAD(&seed_devices->devices);
  1294. INIT_LIST_HEAD(&seed_devices->alloc_list);
  1295. mutex_init(&seed_devices->device_list_mutex);
  1296. mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
  1297. list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
  1298. synchronize_rcu);
  1299. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1300. list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
  1301. list_for_each_entry(device, &seed_devices->devices, dev_list) {
  1302. device->fs_devices = seed_devices;
  1303. }
  1304. fs_devices->seeding = 0;
  1305. fs_devices->num_devices = 0;
  1306. fs_devices->open_devices = 0;
  1307. fs_devices->seed = seed_devices;
  1308. generate_random_uuid(fs_devices->fsid);
  1309. memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
  1310. memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
  1311. super_flags = btrfs_super_flags(disk_super) &
  1312. ~BTRFS_SUPER_FLAG_SEEDING;
  1313. btrfs_set_super_flags(disk_super, super_flags);
  1314. return 0;
  1315. }
  1316. /*
  1317. * strore the expected generation for seed devices in device items.
  1318. */
  1319. static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
  1320. struct btrfs_root *root)
  1321. {
  1322. struct btrfs_path *path;
  1323. struct extent_buffer *leaf;
  1324. struct btrfs_dev_item *dev_item;
  1325. struct btrfs_device *device;
  1326. struct btrfs_key key;
  1327. u8 fs_uuid[BTRFS_UUID_SIZE];
  1328. u8 dev_uuid[BTRFS_UUID_SIZE];
  1329. u64 devid;
  1330. int ret;
  1331. path = btrfs_alloc_path();
  1332. if (!path)
  1333. return -ENOMEM;
  1334. root = root->fs_info->chunk_root;
  1335. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  1336. key.offset = 0;
  1337. key.type = BTRFS_DEV_ITEM_KEY;
  1338. while (1) {
  1339. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  1340. if (ret < 0)
  1341. goto error;
  1342. leaf = path->nodes[0];
  1343. next_slot:
  1344. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  1345. ret = btrfs_next_leaf(root, path);
  1346. if (ret > 0)
  1347. break;
  1348. if (ret < 0)
  1349. goto error;
  1350. leaf = path->nodes[0];
  1351. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  1352. btrfs_release_path(path);
  1353. continue;
  1354. }
  1355. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  1356. if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
  1357. key.type != BTRFS_DEV_ITEM_KEY)
  1358. break;
  1359. dev_item = btrfs_item_ptr(leaf, path->slots[0],
  1360. struct btrfs_dev_item);
  1361. devid = btrfs_device_id(leaf, dev_item);
  1362. read_extent_buffer(leaf, dev_uuid,
  1363. (unsigned long)btrfs_device_uuid(dev_item),
  1364. BTRFS_UUID_SIZE);
  1365. read_extent_buffer(leaf, fs_uuid,
  1366. (unsigned long)btrfs_device_fsid(dev_item),
  1367. BTRFS_UUID_SIZE);
  1368. device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
  1369. BUG_ON(!device);
  1370. if (device->fs_devices->seeding) {
  1371. btrfs_set_device_generation(leaf, dev_item,
  1372. device->generation);
  1373. btrfs_mark_buffer_dirty(leaf);
  1374. }
  1375. path->slots[0]++;
  1376. goto next_slot;
  1377. }
  1378. ret = 0;
  1379. error:
  1380. btrfs_free_path(path);
  1381. return ret;
  1382. }
  1383. int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
  1384. {
  1385. struct request_queue *q;
  1386. struct btrfs_trans_handle *trans;
  1387. struct btrfs_device *device;
  1388. struct block_device *bdev;
  1389. struct list_head *devices;
  1390. struct super_block *sb = root->fs_info->sb;
  1391. u64 total_bytes;
  1392. int seeding_dev = 0;
  1393. int ret = 0;
  1394. if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
  1395. return -EINVAL;
  1396. bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
  1397. root->fs_info->bdev_holder);
  1398. if (IS_ERR(bdev))
  1399. return PTR_ERR(bdev);
  1400. if (root->fs_info->fs_devices->seeding) {
  1401. seeding_dev = 1;
  1402. down_write(&sb->s_umount);
  1403. mutex_lock(&uuid_mutex);
  1404. }
  1405. filemap_write_and_wait(bdev->bd_inode->i_mapping);
  1406. mutex_lock(&root->fs_info->volume_mutex);
  1407. devices = &root->fs_info->fs_devices->devices;
  1408. /*
  1409. * we have the volume lock, so we don't need the extra
  1410. * device list mutex while reading the list here.
  1411. */
  1412. list_for_each_entry(device, devices, dev_list) {
  1413. if (device->bdev == bdev) {
  1414. ret = -EEXIST;
  1415. goto error;
  1416. }
  1417. }
  1418. device = kzalloc(sizeof(*device), GFP_NOFS);
  1419. if (!device) {
  1420. /* we can safely leave the fs_devices entry around */
  1421. ret = -ENOMEM;
  1422. goto error;
  1423. }
  1424. device->name = kstrdup(device_path, GFP_NOFS);
  1425. if (!device->name) {
  1426. kfree(device);
  1427. ret = -ENOMEM;
  1428. goto error;
  1429. }
  1430. ret = find_next_devid(root, &device->devid);
  1431. if (ret) {
  1432. kfree(device->name);
  1433. kfree(device);
  1434. goto error;
  1435. }
  1436. trans = btrfs_start_transaction(root, 0);
  1437. if (IS_ERR(trans)) {
  1438. kfree(device->name);
  1439. kfree(device);
  1440. ret = PTR_ERR(trans);
  1441. goto error;
  1442. }
  1443. lock_chunks(root);
  1444. q = bdev_get_queue(bdev);
  1445. if (blk_queue_discard(q))
  1446. device->can_discard = 1;
  1447. device->writeable = 1;
  1448. device->work.func = pending_bios_fn;
  1449. generate_random_uuid(device->uuid);
  1450. spin_lock_init(&device->io_lock);
  1451. device->generation = trans->transid;
  1452. device->io_width = root->sectorsize;
  1453. device->io_align = root->sectorsize;
  1454. device->sector_size = root->sectorsize;
  1455. device->total_bytes = i_size_read(bdev->bd_inode);
  1456. device->disk_total_bytes = device->total_bytes;
  1457. device->dev_root = root->fs_info->dev_root;
  1458. device->bdev = bdev;
  1459. device->in_fs_metadata = 1;
  1460. device->mode = FMODE_EXCL;
  1461. set_blocksize(device->bdev, 4096);
  1462. if (seeding_dev) {
  1463. sb->s_flags &= ~MS_RDONLY;
  1464. ret = btrfs_prepare_sprout(trans, root);
  1465. BUG_ON(ret);
  1466. }
  1467. device->fs_devices = root->fs_info->fs_devices;
  1468. /*
  1469. * we don't want write_supers to jump in here with our device
  1470. * half setup
  1471. */
  1472. mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
  1473. list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
  1474. list_add(&device->dev_alloc_list,
  1475. &root->fs_info->fs_devices->alloc_list);
  1476. root->fs_info->fs_devices->num_devices++;
  1477. root->fs_info->fs_devices->open_devices++;
  1478. root->fs_info->fs_devices->rw_devices++;
  1479. if (device->can_discard)
  1480. root->fs_info->fs_devices->num_can_discard++;
  1481. root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
  1482. spin_lock(&root->fs_info->free_chunk_lock);
  1483. root->fs_info->free_chunk_space += device->total_bytes;
  1484. spin_unlock(&root->fs_info->free_chunk_lock);
  1485. if (!blk_queue_nonrot(bdev_get_queue(bdev)))
  1486. root->fs_info->fs_devices->rotating = 1;
  1487. total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
  1488. btrfs_set_super_total_bytes(root->fs_info->super_copy,
  1489. total_bytes + device->total_bytes);
  1490. total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
  1491. btrfs_set_super_num_devices(root->fs_info->super_copy,
  1492. total_bytes + 1);
  1493. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1494. if (seeding_dev) {
  1495. ret = init_first_rw_device(trans, root, device);
  1496. BUG_ON(ret);
  1497. ret = btrfs_finish_sprout(trans, root);
  1498. BUG_ON(ret);
  1499. } else {
  1500. ret = btrfs_add_device(trans, root, device);
  1501. }
  1502. /*
  1503. * we've got more storage, clear any full flags on the space
  1504. * infos
  1505. */
  1506. btrfs_clear_space_info_full(root->fs_info);
  1507. unlock_chunks(root);
  1508. btrfs_commit_transaction(trans, root);
  1509. if (seeding_dev) {
  1510. mutex_unlock(&uuid_mutex);
  1511. up_write(&sb->s_umount);
  1512. ret = btrfs_relocate_sys_chunks(root);
  1513. BUG_ON(ret);
  1514. }
  1515. out:
  1516. mutex_unlock(&root->fs_info->volume_mutex);
  1517. return ret;
  1518. error:
  1519. blkdev_put(bdev, FMODE_EXCL);
  1520. if (seeding_dev) {
  1521. mutex_unlock(&uuid_mutex);
  1522. up_write(&sb->s_umount);
  1523. }
  1524. goto out;
  1525. }
  1526. static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
  1527. struct btrfs_device *device)
  1528. {
  1529. int ret;
  1530. struct btrfs_path *path;
  1531. struct btrfs_root *root;
  1532. struct btrfs_dev_item *dev_item;
  1533. struct extent_buffer *leaf;
  1534. struct btrfs_key key;
  1535. root = device->dev_root->fs_info->chunk_root;
  1536. path = btrfs_alloc_path();
  1537. if (!path)
  1538. return -ENOMEM;
  1539. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  1540. key.type = BTRFS_DEV_ITEM_KEY;
  1541. key.offset = device->devid;
  1542. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  1543. if (ret < 0)
  1544. goto out;
  1545. if (ret > 0) {
  1546. ret = -ENOENT;
  1547. goto out;
  1548. }
  1549. leaf = path->nodes[0];
  1550. dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
  1551. btrfs_set_device_id(leaf, dev_item, device->devid);
  1552. btrfs_set_device_type(leaf, dev_item, device->type);
  1553. btrfs_set_device_io_align(leaf, dev_item, device->io_align);
  1554. btrfs_set_device_io_width(leaf, dev_item, device->io_width);
  1555. btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
  1556. btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
  1557. btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
  1558. btrfs_mark_buffer_dirty(leaf);
  1559. out:
  1560. btrfs_free_path(path);
  1561. return ret;
  1562. }
  1563. static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
  1564. struct btrfs_device *device, u64 new_size)
  1565. {
  1566. struct btrfs_super_block *super_copy =
  1567. device->dev_root->fs_info->super_copy;
  1568. u64 old_total = btrfs_super_total_bytes(super_copy);
  1569. u64 diff = new_size - device->total_bytes;
  1570. if (!device->writeable)
  1571. return -EACCES;
  1572. if (new_size <= device->total_bytes)
  1573. return -EINVAL;
  1574. btrfs_set_super_total_bytes(super_copy, old_total + diff);
  1575. device->fs_devices->total_rw_bytes += diff;
  1576. device->total_bytes = new_size;
  1577. device->disk_total_bytes = new_size;
  1578. btrfs_clear_space_info_full(device->dev_root->fs_info);
  1579. return btrfs_update_device(trans, device);
  1580. }
  1581. int btrfs_grow_device(struct btrfs_trans_handle *trans,
  1582. struct btrfs_device *device, u64 new_size)
  1583. {
  1584. int ret;
  1585. lock_chunks(device->dev_root);
  1586. ret = __btrfs_grow_device(trans, device, new_size);
  1587. unlock_chunks(device->dev_root);
  1588. return ret;
  1589. }
  1590. static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
  1591. struct btrfs_root *root,
  1592. u64 chunk_tree, u64 chunk_objectid,
  1593. u64 chunk_offset)
  1594. {
  1595. int ret;
  1596. struct btrfs_path *path;
  1597. struct btrfs_key key;
  1598. root = root->fs_info->chunk_root;
  1599. path = btrfs_alloc_path();
  1600. if (!path)
  1601. return -ENOMEM;
  1602. key.objectid = chunk_objectid;
  1603. key.offset = chunk_offset;
  1604. key.type = BTRFS_CHUNK_ITEM_KEY;
  1605. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  1606. BUG_ON(ret);
  1607. ret = btrfs_del_item(trans, root, path);
  1608. btrfs_free_path(path);
  1609. return ret;
  1610. }
  1611. static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
  1612. chunk_offset)
  1613. {
  1614. struct btrfs_super_block *super_copy = root->fs_info->super_copy;
  1615. struct btrfs_disk_key *disk_key;
  1616. struct btrfs_chunk *chunk;
  1617. u8 *ptr;
  1618. int ret = 0;
  1619. u32 num_stripes;
  1620. u32 array_size;
  1621. u32 len = 0;
  1622. u32 cur;
  1623. struct btrfs_key key;
  1624. array_size = btrfs_super_sys_array_size(super_copy);
  1625. ptr = super_copy->sys_chunk_array;
  1626. cur = 0;
  1627. while (cur < array_size) {
  1628. disk_key = (struct btrfs_disk_key *)ptr;
  1629. btrfs_disk_key_to_cpu(&key, disk_key);
  1630. len = sizeof(*disk_key);
  1631. if (key.type == BTRFS_CHUNK_ITEM_KEY) {
  1632. chunk = (struct btrfs_chunk *)(ptr + len);
  1633. num_stripes = btrfs_stack_chunk_num_stripes(chunk);
  1634. len += btrfs_chunk_item_size(num_stripes);
  1635. } else {
  1636. ret = -EIO;
  1637. break;
  1638. }
  1639. if (key.objectid == chunk_objectid &&
  1640. key.offset == chunk_offset) {
  1641. memmove(ptr, ptr + len, array_size - (cur + len));
  1642. array_size -= len;
  1643. btrfs_set_super_sys_array_size(super_copy, array_size);
  1644. } else {
  1645. ptr += len;
  1646. cur += len;
  1647. }
  1648. }
  1649. return ret;
  1650. }
  1651. static int btrfs_relocate_chunk(struct btrfs_root *root,
  1652. u64 chunk_tree, u64 chunk_objectid,
  1653. u64 chunk_offset)
  1654. {
  1655. struct extent_map_tree *em_tree;
  1656. struct btrfs_root *extent_root;
  1657. struct btrfs_trans_handle *trans;
  1658. struct extent_map *em;
  1659. struct map_lookup *map;
  1660. int ret;
  1661. int i;
  1662. root = root->fs_info->chunk_root;
  1663. extent_root = root->fs_info->extent_root;
  1664. em_tree = &root->fs_info->mapping_tree.map_tree;
  1665. ret = btrfs_can_relocate(extent_root, chunk_offset);
  1666. if (ret)
  1667. return -ENOSPC;
  1668. /* step one, relocate all the extents inside this chunk */
  1669. ret = btrfs_relocate_block_group(extent_root, chunk_offset);
  1670. if (ret)
  1671. return ret;
  1672. trans = btrfs_start_transaction(root, 0);
  1673. BUG_ON(IS_ERR(trans));
  1674. lock_chunks(root);
  1675. /*
  1676. * step two, delete the device extents and the
  1677. * chunk tree entries
  1678. */
  1679. read_lock(&em_tree->lock);
  1680. em = lookup_extent_mapping(em_tree, chunk_offset, 1);
  1681. read_unlock(&em_tree->lock);
  1682. BUG_ON(em->start > chunk_offset ||
  1683. em->start + em->len < chunk_offset);
  1684. map = (struct map_lookup *)em->bdev;
  1685. for (i = 0; i < map->num_stripes; i++) {
  1686. ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
  1687. map->stripes[i].physical);
  1688. BUG_ON(ret);
  1689. if (map->stripes[i].dev) {
  1690. ret = btrfs_update_device(trans, map->stripes[i].dev);
  1691. BUG_ON(ret);
  1692. }
  1693. }
  1694. ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
  1695. chunk_offset);
  1696. BUG_ON(ret);
  1697. trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
  1698. if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
  1699. ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
  1700. BUG_ON(ret);
  1701. }
  1702. ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
  1703. BUG_ON(ret);
  1704. write_lock(&em_tree->lock);
  1705. remove_extent_mapping(em_tree, em);
  1706. write_unlock(&em_tree->lock);
  1707. kfree(map);
  1708. em->bdev = NULL;
  1709. /* once for the tree */
  1710. free_extent_map(em);
  1711. /* once for us */
  1712. free_extent_map(em);
  1713. unlock_chunks(root);
  1714. btrfs_end_transaction(trans, root);
  1715. return 0;
  1716. }
  1717. static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
  1718. {
  1719. struct btrfs_root *chunk_root = root->fs_info->chunk_root;
  1720. struct btrfs_path *path;
  1721. struct extent_buffer *leaf;
  1722. struct btrfs_chunk *chunk;
  1723. struct btrfs_key key;
  1724. struct btrfs_key found_key;
  1725. u64 chunk_tree = chunk_root->root_key.objectid;
  1726. u64 chunk_type;
  1727. bool retried = false;
  1728. int failed = 0;
  1729. int ret;
  1730. path = btrfs_alloc_path();
  1731. if (!path)
  1732. return -ENOMEM;
  1733. again:
  1734. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  1735. key.offset = (u64)-1;
  1736. key.type = BTRFS_CHUNK_ITEM_KEY;
  1737. while (1) {
  1738. ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
  1739. if (ret < 0)
  1740. goto error;
  1741. BUG_ON(ret == 0);
  1742. ret = btrfs_previous_item(chunk_root, path, key.objectid,
  1743. key.type);
  1744. if (ret < 0)
  1745. goto error;
  1746. if (ret > 0)
  1747. break;
  1748. leaf = path->nodes[0];
  1749. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  1750. chunk = btrfs_item_ptr(leaf, path->slots[0],
  1751. struct btrfs_chunk);
  1752. chunk_type = btrfs_chunk_type(leaf, chunk);
  1753. btrfs_release_path(path);
  1754. if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
  1755. ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
  1756. found_key.objectid,
  1757. found_key.offset);
  1758. if (ret == -ENOSPC)
  1759. failed++;
  1760. else if (ret)
  1761. BUG();
  1762. }
  1763. if (found_key.offset == 0)
  1764. break;
  1765. key.offset = found_key.offset - 1;
  1766. }
  1767. ret = 0;
  1768. if (failed && !retried) {
  1769. failed = 0;
  1770. retried = true;
  1771. goto again;
  1772. } else if (failed && retried) {
  1773. WARN_ON(1);
  1774. ret = -ENOSPC;
  1775. }
  1776. error:
  1777. btrfs_free_path(path);
  1778. return ret;
  1779. }
  1780. static u64 div_factor(u64 num, int factor)
  1781. {
  1782. if (factor == 10)
  1783. return num;
  1784. num *= factor;
  1785. do_div(num, 10);
  1786. return num;
  1787. }
  1788. int btrfs_balance(struct btrfs_root *dev_root)
  1789. {
  1790. int ret;
  1791. struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
  1792. struct btrfs_device *device;
  1793. u64 old_size;
  1794. u64 size_to_free;
  1795. struct btrfs_path *path;
  1796. struct btrfs_key key;
  1797. struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
  1798. struct btrfs_trans_handle *trans;
  1799. struct btrfs_key found_key;
  1800. if (dev_root->fs_info->sb->s_flags & MS_RDONLY)
  1801. return -EROFS;
  1802. if (!capable(CAP_SYS_ADMIN))
  1803. return -EPERM;
  1804. mutex_lock(&dev_root->fs_info->volume_mutex);
  1805. dev_root = dev_root->fs_info->dev_root;
  1806. /* step one make some room on all the devices */
  1807. list_for_each_entry(device, devices, dev_list) {
  1808. old_size = device->total_bytes;
  1809. size_to_free = div_factor(old_size, 1);
  1810. size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
  1811. if (!device->writeable ||
  1812. device->total_bytes - device->bytes_used > size_to_free)
  1813. continue;
  1814. ret = btrfs_shrink_device(device, old_size - size_to_free);
  1815. if (ret == -ENOSPC)
  1816. break;
  1817. BUG_ON(ret);
  1818. trans = btrfs_start_transaction(dev_root, 0);
  1819. BUG_ON(IS_ERR(trans));
  1820. ret = btrfs_grow_device(trans, device, old_size);
  1821. BUG_ON(ret);
  1822. btrfs_end_transaction(trans, dev_root);
  1823. }
  1824. /* step two, relocate all the chunks */
  1825. path = btrfs_alloc_path();
  1826. if (!path) {
  1827. ret = -ENOMEM;
  1828. goto error;
  1829. }
  1830. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  1831. key.offset = (u64)-1;
  1832. key.type = BTRFS_CHUNK_ITEM_KEY;
  1833. while (1) {
  1834. ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
  1835. if (ret < 0)
  1836. goto error;
  1837. /*
  1838. * this shouldn't happen, it means the last relocate
  1839. * failed
  1840. */
  1841. if (ret == 0)
  1842. break;
  1843. ret = btrfs_previous_item(chunk_root, path, 0,
  1844. BTRFS_CHUNK_ITEM_KEY);
  1845. if (ret)
  1846. break;
  1847. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  1848. path->slots[0]);
  1849. if (found_key.objectid != key.objectid)
  1850. break;
  1851. /* chunk zero is special */
  1852. if (found_key.offset == 0)
  1853. break;
  1854. btrfs_release_path(path);
  1855. ret = btrfs_relocate_chunk(chunk_root,
  1856. chunk_root->root_key.objectid,
  1857. found_key.objectid,
  1858. found_key.offset);
  1859. if (ret && ret != -ENOSPC)
  1860. goto error;
  1861. key.offset = found_key.offset - 1;
  1862. }
  1863. ret = 0;
  1864. error:
  1865. btrfs_free_path(path);
  1866. mutex_unlock(&dev_root->fs_info->volume_mutex);
  1867. return ret;
  1868. }
  1869. /*
  1870. * shrinking a device means finding all of the device extents past
  1871. * the new size, and then following the back refs to the chunks.
  1872. * The chunk relocation code actually frees the device extent
  1873. */
  1874. int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
  1875. {
  1876. struct btrfs_trans_handle *trans;
  1877. struct btrfs_root *root = device->dev_root;
  1878. struct btrfs_dev_extent *dev_extent = NULL;
  1879. struct btrfs_path *path;
  1880. u64 length;
  1881. u64 chunk_tree;
  1882. u64 chunk_objectid;
  1883. u64 chunk_offset;
  1884. int ret;
  1885. int slot;
  1886. int failed = 0;
  1887. bool retried = false;
  1888. struct extent_buffer *l;
  1889. struct btrfs_key key;
  1890. struct btrfs_super_block *super_copy = root->fs_info->super_copy;
  1891. u64 old_total = btrfs_super_total_bytes(super_copy);
  1892. u64 old_size = device->total_bytes;
  1893. u64 diff = device->total_bytes - new_size;
  1894. if (new_size >= device->total_bytes)
  1895. return -EINVAL;
  1896. path = btrfs_alloc_path();
  1897. if (!path)
  1898. return -ENOMEM;
  1899. path->reada = 2;
  1900. lock_chunks(root);
  1901. device->total_bytes = new_size;
  1902. if (device->writeable) {
  1903. device->fs_devices->total_rw_bytes -= diff;
  1904. spin_lock(&root->fs_info->free_chunk_lock);
  1905. root->fs_info->free_chunk_space -= diff;
  1906. spin_unlock(&root->fs_info->free_chunk_lock);
  1907. }
  1908. unlock_chunks(root);
  1909. again:
  1910. key.objectid = device->devid;
  1911. key.offset = (u64)-1;
  1912. key.type = BTRFS_DEV_EXTENT_KEY;
  1913. while (1) {
  1914. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1915. if (ret < 0)
  1916. goto done;
  1917. ret = btrfs_previous_item(root, path, 0, key.type);
  1918. if (ret < 0)
  1919. goto done;
  1920. if (ret) {
  1921. ret = 0;
  1922. btrfs_release_path(path);
  1923. break;
  1924. }
  1925. l = path->nodes[0];
  1926. slot = path->slots[0];
  1927. btrfs_item_key_to_cpu(l, &key, path->slots[0]);
  1928. if (key.objectid != device->devid) {
  1929. btrfs_release_path(path);
  1930. break;
  1931. }
  1932. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  1933. length = btrfs_dev_extent_length(l, dev_extent);
  1934. if (key.offset + length <= new_size) {
  1935. btrfs_release_path(path);
  1936. break;
  1937. }
  1938. chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
  1939. chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
  1940. chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
  1941. btrfs_release_path(path);
  1942. ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
  1943. chunk_offset);
  1944. if (ret && ret != -ENOSPC)
  1945. goto done;
  1946. if (ret == -ENOSPC)
  1947. failed++;
  1948. key.offset -= 1;
  1949. }
  1950. if (failed && !retried) {
  1951. failed = 0;
  1952. retried = true;
  1953. goto again;
  1954. } else if (failed && retried) {
  1955. ret = -ENOSPC;
  1956. lock_chunks(root);
  1957. device->total_bytes = old_size;
  1958. if (device->writeable)
  1959. device->fs_devices->total_rw_bytes += diff;
  1960. spin_lock(&root->fs_info->free_chunk_lock);
  1961. root->fs_info->free_chunk_space += diff;
  1962. spin_unlock(&root->fs_info->free_chunk_lock);
  1963. unlock_chunks(root);
  1964. goto done;
  1965. }
  1966. /* Shrinking succeeded, else we would be at "done". */
  1967. trans = btrfs_start_transaction(root, 0);
  1968. if (IS_ERR(trans)) {
  1969. ret = PTR_ERR(trans);
  1970. goto done;
  1971. }
  1972. lock_chunks(root);
  1973. device->disk_total_bytes = new_size;
  1974. /* Now btrfs_update_device() will change the on-disk size. */
  1975. ret = btrfs_update_device(trans, device);
  1976. if (ret) {
  1977. unlock_chunks(root);
  1978. btrfs_end_transaction(trans, root);
  1979. goto done;
  1980. }
  1981. WARN_ON(diff > old_total);
  1982. btrfs_set_super_total_bytes(super_copy, old_total - diff);
  1983. unlock_chunks(root);
  1984. btrfs_end_transaction(trans, root);
  1985. done:
  1986. btrfs_free_path(path);
  1987. return ret;
  1988. }
  1989. static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
  1990. struct btrfs_root *root,
  1991. struct btrfs_key *key,
  1992. struct btrfs_chunk *chunk, int item_size)
  1993. {
  1994. struct btrfs_super_block *super_copy = root->fs_info->super_copy;
  1995. struct btrfs_disk_key disk_key;
  1996. u32 array_size;
  1997. u8 *ptr;
  1998. array_size = btrfs_super_sys_array_size(super_copy);
  1999. if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
  2000. return -EFBIG;
  2001. ptr = super_copy->sys_chunk_array + array_size;
  2002. btrfs_cpu_key_to_disk(&disk_key, key);
  2003. memcpy(ptr, &disk_key, sizeof(disk_key));
  2004. ptr += sizeof(disk_key);
  2005. memcpy(ptr, chunk, item_size);
  2006. item_size += sizeof(disk_key);
  2007. btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
  2008. return 0;
  2009. }
  2010. /*
  2011. * sort the devices in descending order by max_avail, total_avail
  2012. */
  2013. static int btrfs_cmp_device_info(const void *a, const void *b)
  2014. {
  2015. const struct btrfs_device_info *di_a = a;
  2016. const struct btrfs_device_info *di_b = b;
  2017. if (di_a->max_avail > di_b->max_avail)
  2018. return -1;
  2019. if (di_a->max_avail < di_b->max_avail)
  2020. return 1;
  2021. if (di_a->total_avail > di_b->total_avail)
  2022. return -1;
  2023. if (di_a->total_avail < di_b->total_avail)
  2024. return 1;
  2025. return 0;
  2026. }
  2027. static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
  2028. struct btrfs_root *extent_root,
  2029. struct map_lookup **map_ret,
  2030. u64 *num_bytes_out, u64 *stripe_size_out,
  2031. u64 start, u64 type)
  2032. {
  2033. struct btrfs_fs_info *info = extent_root->fs_info;
  2034. struct btrfs_fs_devices *fs_devices = info->fs_devices;
  2035. struct list_head *cur;
  2036. struct map_lookup *map = NULL;
  2037. struct extent_map_tree *em_tree;
  2038. struct extent_map *em;
  2039. struct btrfs_device_info *devices_info = NULL;
  2040. u64 total_avail;
  2041. int num_stripes; /* total number of stripes to allocate */
  2042. int sub_stripes; /* sub_stripes info for map */
  2043. int dev_stripes; /* stripes per dev */
  2044. int devs_max; /* max devs to use */
  2045. int devs_min; /* min devs needed */
  2046. int devs_increment; /* ndevs has to be a multiple of this */
  2047. int ncopies; /* how many copies to data has */
  2048. int ret;
  2049. u64 max_stripe_size;
  2050. u64 max_chunk_size;
  2051. u64 stripe_size;
  2052. u64 num_bytes;
  2053. int ndevs;
  2054. int i;
  2055. int j;
  2056. if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
  2057. (type & BTRFS_BLOCK_GROUP_DUP)) {
  2058. WARN_ON(1);
  2059. type &= ~BTRFS_BLOCK_GROUP_DUP;
  2060. }
  2061. if (list_empty(&fs_devices->alloc_list))
  2062. return -ENOSPC;
  2063. sub_stripes = 1;
  2064. dev_stripes = 1;
  2065. devs_increment = 1;
  2066. ncopies = 1;
  2067. devs_max = 0; /* 0 == as many as possible */
  2068. devs_min = 1;
  2069. /*
  2070. * define the properties of each RAID type.
  2071. * FIXME: move this to a global table and use it in all RAID
  2072. * calculation code
  2073. */
  2074. if (type & (BTRFS_BLOCK_GROUP_DUP)) {
  2075. dev_stripes = 2;
  2076. ncopies = 2;
  2077. devs_max = 1;
  2078. } else if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
  2079. devs_min = 2;
  2080. } else if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
  2081. devs_increment = 2;
  2082. ncopies = 2;
  2083. devs_max = 2;
  2084. devs_min = 2;
  2085. } else if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
  2086. sub_stripes = 2;
  2087. devs_increment = 2;
  2088. ncopies = 2;
  2089. devs_min = 4;
  2090. } else {
  2091. devs_max = 1;
  2092. }
  2093. if (type & BTRFS_BLOCK_GROUP_DATA) {
  2094. max_stripe_size = 1024 * 1024 * 1024;
  2095. max_chunk_size = 10 * max_stripe_size;
  2096. } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
  2097. max_stripe_size = 256 * 1024 * 1024;
  2098. max_chunk_size = max_stripe_size;
  2099. } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
  2100. max_stripe_size = 8 * 1024 * 1024;
  2101. max_chunk_size = 2 * max_stripe_size;
  2102. } else {
  2103. printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
  2104. type);
  2105. BUG_ON(1);
  2106. }
  2107. /* we don't want a chunk larger than 10% of writeable space */
  2108. max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
  2109. max_chunk_size);
  2110. devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
  2111. GFP_NOFS);
  2112. if (!devices_info)
  2113. return -ENOMEM;
  2114. cur = fs_devices->alloc_list.next;
  2115. /*
  2116. * in the first pass through the devices list, we gather information
  2117. * about the available holes on each device.
  2118. */
  2119. ndevs = 0;
  2120. while (cur != &fs_devices->alloc_list) {
  2121. struct btrfs_device *device;
  2122. u64 max_avail;
  2123. u64 dev_offset;
  2124. device = list_entry(cur, struct btrfs_device, dev_alloc_list);
  2125. cur = cur->next;
  2126. if (!device->writeable) {
  2127. printk(KERN_ERR
  2128. "btrfs: read-only device in alloc_list\n");
  2129. WARN_ON(1);
  2130. continue;
  2131. }
  2132. if (!device->in_fs_metadata)
  2133. continue;
  2134. if (device->total_bytes > device->bytes_used)
  2135. total_avail = device->total_bytes - device->bytes_used;
  2136. else
  2137. total_avail = 0;
  2138. /* If there is no space on this device, skip it. */
  2139. if (total_avail == 0)
  2140. continue;
  2141. ret = find_free_dev_extent(trans, device,
  2142. max_stripe_size * dev_stripes,
  2143. &dev_offset, &max_avail);
  2144. if (ret && ret != -ENOSPC)
  2145. goto error;
  2146. if (ret == 0)
  2147. max_avail = max_stripe_size * dev_stripes;
  2148. if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
  2149. continue;
  2150. devices_info[ndevs].dev_offset = dev_offset;
  2151. devices_info[ndevs].max_avail = max_avail;
  2152. devices_info[ndevs].total_avail = total_avail;
  2153. devices_info[ndevs].dev = device;
  2154. ++ndevs;
  2155. }
  2156. /*
  2157. * now sort the devices by hole size / available space
  2158. */
  2159. sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
  2160. btrfs_cmp_device_info, NULL);
  2161. /* round down to number of usable stripes */
  2162. ndevs -= ndevs % devs_increment;
  2163. if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
  2164. ret = -ENOSPC;
  2165. goto error;
  2166. }
  2167. if (devs_max && ndevs > devs_max)
  2168. ndevs = devs_max;
  2169. /*
  2170. * the primary goal is to maximize the number of stripes, so use as many
  2171. * devices as possible, even if the stripes are not maximum sized.
  2172. */
  2173. stripe_size = devices_info[ndevs-1].max_avail;
  2174. num_stripes = ndevs * dev_stripes;
  2175. if (stripe_size * num_stripes > max_chunk_size * ncopies) {
  2176. stripe_size = max_chunk_size * ncopies;
  2177. do_div(stripe_size, num_stripes);
  2178. }
  2179. do_div(stripe_size, dev_stripes);
  2180. do_div(stripe_size, BTRFS_STRIPE_LEN);
  2181. stripe_size *= BTRFS_STRIPE_LEN;
  2182. map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
  2183. if (!map) {
  2184. ret = -ENOMEM;
  2185. goto error;
  2186. }
  2187. map->num_stripes = num_stripes;
  2188. for (i = 0; i < ndevs; ++i) {
  2189. for (j = 0; j < dev_stripes; ++j) {
  2190. int s = i * dev_stripes + j;
  2191. map->stripes[s].dev = devices_info[i].dev;
  2192. map->stripes[s].physical = devices_info[i].dev_offset +
  2193. j * stripe_size;
  2194. }
  2195. }
  2196. map->sector_size = extent_root->sectorsize;
  2197. map->stripe_len = BTRFS_STRIPE_LEN;
  2198. map->io_align = BTRFS_STRIPE_LEN;
  2199. map->io_width = BTRFS_STRIPE_LEN;
  2200. map->type = type;
  2201. map->sub_stripes = sub_stripes;
  2202. *map_ret = map;
  2203. num_bytes = stripe_size * (num_stripes / ncopies);
  2204. *stripe_size_out = stripe_size;
  2205. *num_bytes_out = num_bytes;
  2206. trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
  2207. em = alloc_extent_map();
  2208. if (!em) {
  2209. ret = -ENOMEM;
  2210. goto error;
  2211. }
  2212. em->bdev = (struct block_device *)map;
  2213. em->start = start;
  2214. em->len = num_bytes;
  2215. em->block_start = 0;
  2216. em->block_len = em->len;
  2217. em_tree = &extent_root->fs_info->mapping_tree.map_tree;
  2218. write_lock(&em_tree->lock);
  2219. ret = add_extent_mapping(em_tree, em);
  2220. write_unlock(&em_tree->lock);
  2221. BUG_ON(ret);
  2222. free_extent_map(em);
  2223. ret = btrfs_make_block_group(trans, extent_root, 0, type,
  2224. BTRFS_FIRST_CHUNK_TREE_OBJECTID,
  2225. start, num_bytes);
  2226. BUG_ON(ret);
  2227. for (i = 0; i < map->num_stripes; ++i) {
  2228. struct btrfs_device *device;
  2229. u64 dev_offset;
  2230. device = map->stripes[i].dev;
  2231. dev_offset = map->stripes[i].physical;
  2232. ret = btrfs_alloc_dev_extent(trans, device,
  2233. info->chunk_root->root_key.objectid,
  2234. BTRFS_FIRST_CHUNK_TREE_OBJECTID,
  2235. start, dev_offset, stripe_size);
  2236. BUG_ON(ret);
  2237. }
  2238. kfree(devices_info);
  2239. return 0;
  2240. error:
  2241. kfree(map);
  2242. kfree(devices_info);
  2243. return ret;
  2244. }
  2245. static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
  2246. struct btrfs_root *extent_root,
  2247. struct map_lookup *map, u64 chunk_offset,
  2248. u64 chunk_size, u64 stripe_size)
  2249. {
  2250. u64 dev_offset;
  2251. struct btrfs_key key;
  2252. struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
  2253. struct btrfs_device *device;
  2254. struct btrfs_chunk *chunk;
  2255. struct btrfs_stripe *stripe;
  2256. size_t item_size = btrfs_chunk_item_size(map->num_stripes);
  2257. int index = 0;
  2258. int ret;
  2259. chunk = kzalloc(item_size, GFP_NOFS);
  2260. if (!chunk)
  2261. return -ENOMEM;
  2262. index = 0;
  2263. while (index < map->num_stripes) {
  2264. device = map->stripes[index].dev;
  2265. device->bytes_used += stripe_size;
  2266. ret = btrfs_update_device(trans, device);
  2267. BUG_ON(ret);
  2268. index++;
  2269. }
  2270. spin_lock(&extent_root->fs_info->free_chunk_lock);
  2271. extent_root->fs_info->free_chunk_space -= (stripe_size *
  2272. map->num_stripes);
  2273. spin_unlock(&extent_root->fs_info->free_chunk_lock);
  2274. index = 0;
  2275. stripe = &chunk->stripe;
  2276. while (index < map->num_stripes) {
  2277. device = map->stripes[index].dev;
  2278. dev_offset = map->stripes[index].physical;
  2279. btrfs_set_stack_stripe_devid(stripe, device->devid);
  2280. btrfs_set_stack_stripe_offset(stripe, dev_offset);
  2281. memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
  2282. stripe++;
  2283. index++;
  2284. }
  2285. btrfs_set_stack_chunk_length(chunk, chunk_size);
  2286. btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
  2287. btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
  2288. btrfs_set_stack_chunk_type(chunk, map->type);
  2289. btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
  2290. btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
  2291. btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
  2292. btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
  2293. btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
  2294. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  2295. key.type = BTRFS_CHUNK_ITEM_KEY;
  2296. key.offset = chunk_offset;
  2297. ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
  2298. BUG_ON(ret);
  2299. if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
  2300. ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
  2301. item_size);
  2302. BUG_ON(ret);
  2303. }
  2304. kfree(chunk);
  2305. return 0;
  2306. }
  2307. /*
  2308. * Chunk allocation falls into two parts. The first part does works
  2309. * that make the new allocated chunk useable, but not do any operation
  2310. * that modifies the chunk tree. The second part does the works that
  2311. * require modifying the chunk tree. This division is important for the
  2312. * bootstrap process of adding storage to a seed btrfs.
  2313. */
  2314. int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
  2315. struct btrfs_root *extent_root, u64 type)
  2316. {
  2317. u64 chunk_offset;
  2318. u64 chunk_size;
  2319. u64 stripe_size;
  2320. struct map_lookup *map;
  2321. struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
  2322. int ret;
  2323. ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
  2324. &chunk_offset);
  2325. if (ret)
  2326. return ret;
  2327. ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
  2328. &stripe_size, chunk_offset, type);
  2329. if (ret)
  2330. return ret;
  2331. ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
  2332. chunk_size, stripe_size);
  2333. BUG_ON(ret);
  2334. return 0;
  2335. }
  2336. static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
  2337. struct btrfs_root *root,
  2338. struct btrfs_device *device)
  2339. {
  2340. u64 chunk_offset;
  2341. u64 sys_chunk_offset;
  2342. u64 chunk_size;
  2343. u64 sys_chunk_size;
  2344. u64 stripe_size;
  2345. u64 sys_stripe_size;
  2346. u64 alloc_profile;
  2347. struct map_lookup *map;
  2348. struct map_lookup *sys_map;
  2349. struct btrfs_fs_info *fs_info = root->fs_info;
  2350. struct btrfs_root *extent_root = fs_info->extent_root;
  2351. int ret;
  2352. ret = find_next_chunk(fs_info->chunk_root,
  2353. BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
  2354. if (ret)
  2355. return ret;
  2356. alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
  2357. (fs_info->metadata_alloc_profile &
  2358. fs_info->avail_metadata_alloc_bits);
  2359. alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
  2360. ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
  2361. &stripe_size, chunk_offset, alloc_profile);
  2362. BUG_ON(ret);
  2363. sys_chunk_offset = chunk_offset + chunk_size;
  2364. alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
  2365. (fs_info->system_alloc_profile &
  2366. fs_info->avail_system_alloc_bits);
  2367. alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
  2368. ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
  2369. &sys_chunk_size, &sys_stripe_size,
  2370. sys_chunk_offset, alloc_profile);
  2371. BUG_ON(ret);
  2372. ret = btrfs_add_device(trans, fs_info->chunk_root, device);
  2373. BUG_ON(ret);
  2374. /*
  2375. * Modifying chunk tree needs allocating new blocks from both
  2376. * system block group and metadata block group. So we only can
  2377. * do operations require modifying the chunk tree after both
  2378. * block groups were created.
  2379. */
  2380. ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
  2381. chunk_size, stripe_size);
  2382. BUG_ON(ret);
  2383. ret = __finish_chunk_alloc(trans, extent_root, sys_map,
  2384. sys_chunk_offset, sys_chunk_size,
  2385. sys_stripe_size);
  2386. BUG_ON(ret);
  2387. return 0;
  2388. }
  2389. int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
  2390. {
  2391. struct extent_map *em;
  2392. struct map_lookup *map;
  2393. struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
  2394. int readonly = 0;
  2395. int i;
  2396. read_lock(&map_tree->map_tree.lock);
  2397. em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
  2398. read_unlock(&map_tree->map_tree.lock);
  2399. if (!em)
  2400. return 1;
  2401. if (btrfs_test_opt(root, DEGRADED)) {
  2402. free_extent_map(em);
  2403. return 0;
  2404. }
  2405. map = (struct map_lookup *)em->bdev;
  2406. for (i = 0; i < map->num_stripes; i++) {
  2407. if (!map->stripes[i].dev->writeable) {
  2408. readonly = 1;
  2409. break;
  2410. }
  2411. }
  2412. free_extent_map(em);
  2413. return readonly;
  2414. }
  2415. void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
  2416. {
  2417. extent_map_tree_init(&tree->map_tree);
  2418. }
  2419. void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
  2420. {
  2421. struct extent_map *em;
  2422. while (1) {
  2423. write_lock(&tree->map_tree.lock);
  2424. em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
  2425. if (em)
  2426. remove_extent_mapping(&tree->map_tree, em);
  2427. write_unlock(&tree->map_tree.lock);
  2428. if (!em)
  2429. break;
  2430. kfree(em->bdev);
  2431. /* once for us */
  2432. free_extent_map(em);
  2433. /* once for the tree */
  2434. free_extent_map(em);
  2435. }
  2436. }
  2437. int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
  2438. {
  2439. struct extent_map *em;
  2440. struct map_lookup *map;
  2441. struct extent_map_tree *em_tree = &map_tree->map_tree;
  2442. int ret;
  2443. read_lock(&em_tree->lock);
  2444. em = lookup_extent_mapping(em_tree, logical, len);
  2445. read_unlock(&em_tree->lock);
  2446. BUG_ON(!em);
  2447. BUG_ON(em->start > logical || em->start + em->len < logical);
  2448. map = (struct map_lookup *)em->bdev;
  2449. if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
  2450. ret = map->num_stripes;
  2451. else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
  2452. ret = map->sub_stripes;
  2453. else
  2454. ret = 1;
  2455. free_extent_map(em);
  2456. return ret;
  2457. }
  2458. static int find_live_mirror(struct map_lookup *map, int first, int num,
  2459. int optimal)
  2460. {
  2461. int i;
  2462. if (map->stripes[optimal].dev->bdev)
  2463. return optimal;
  2464. for (i = first; i < first + num; i++) {
  2465. if (map->stripes[i].dev->bdev)
  2466. return i;
  2467. }
  2468. /* we couldn't find one that doesn't fail. Just return something
  2469. * and the io error handling code will clean up eventually
  2470. */
  2471. return optimal;
  2472. }
  2473. static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
  2474. u64 logical, u64 *length,
  2475. struct btrfs_bio **bbio_ret,
  2476. int mirror_num)
  2477. {
  2478. struct extent_map *em;
  2479. struct map_lookup *map;
  2480. struct extent_map_tree *em_tree = &map_tree->map_tree;
  2481. u64 offset;
  2482. u64 stripe_offset;
  2483. u64 stripe_end_offset;
  2484. u64 stripe_nr;
  2485. u64 stripe_nr_orig;
  2486. u64 stripe_nr_end;
  2487. int stripes_allocated = 8;
  2488. int stripes_required = 1;
  2489. int stripe_index;
  2490. int i;
  2491. int num_stripes;
  2492. int max_errors = 0;
  2493. struct btrfs_bio *bbio = NULL;
  2494. if (bbio_ret && !(rw & (REQ_WRITE | REQ_DISCARD)))
  2495. stripes_allocated = 1;
  2496. again:
  2497. if (bbio_ret) {
  2498. bbio = kzalloc(btrfs_bio_size(stripes_allocated),
  2499. GFP_NOFS);
  2500. if (!bbio)
  2501. return -ENOMEM;
  2502. atomic_set(&bbio->error, 0);
  2503. }
  2504. read_lock(&em_tree->lock);
  2505. em = lookup_extent_mapping(em_tree, logical, *length);
  2506. read_unlock(&em_tree->lock);
  2507. if (!em) {
  2508. printk(KERN_CRIT "unable to find logical %llu len %llu\n",
  2509. (unsigned long long)logical,
  2510. (unsigned long long)*length);
  2511. BUG();
  2512. }
  2513. BUG_ON(em->start > logical || em->start + em->len < logical);
  2514. map = (struct map_lookup *)em->bdev;
  2515. offset = logical - em->start;
  2516. if (mirror_num > map->num_stripes)
  2517. mirror_num = 0;
  2518. /* if our btrfs_bio struct is too small, back off and try again */
  2519. if (rw & REQ_WRITE) {
  2520. if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
  2521. BTRFS_BLOCK_GROUP_DUP)) {
  2522. stripes_required = map->num_stripes;
  2523. max_errors = 1;
  2524. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  2525. stripes_required = map->sub_stripes;
  2526. max_errors = 1;
  2527. }
  2528. }
  2529. if (rw & REQ_DISCARD) {
  2530. if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
  2531. BTRFS_BLOCK_GROUP_RAID1 |
  2532. BTRFS_BLOCK_GROUP_DUP |
  2533. BTRFS_BLOCK_GROUP_RAID10)) {
  2534. stripes_required = map->num_stripes;
  2535. }
  2536. }
  2537. if (bbio_ret && (rw & (REQ_WRITE | REQ_DISCARD)) &&
  2538. stripes_allocated < stripes_required) {
  2539. stripes_allocated = map->num_stripes;
  2540. free_extent_map(em);
  2541. kfree(bbio);
  2542. goto again;
  2543. }
  2544. stripe_nr = offset;
  2545. /*
  2546. * stripe_nr counts the total number of stripes we have to stride
  2547. * to get to this block
  2548. */
  2549. do_div(stripe_nr, map->stripe_len);
  2550. stripe_offset = stripe_nr * map->stripe_len;
  2551. BUG_ON(offset < stripe_offset);
  2552. /* stripe_offset is the offset of this block in its stripe*/
  2553. stripe_offset = offset - stripe_offset;
  2554. if (rw & REQ_DISCARD)
  2555. *length = min_t(u64, em->len - offset, *length);
  2556. else if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
  2557. BTRFS_BLOCK_GROUP_RAID1 |
  2558. BTRFS_BLOCK_GROUP_RAID10 |
  2559. BTRFS_BLOCK_GROUP_DUP)) {
  2560. /* we limit the length of each bio to what fits in a stripe */
  2561. *length = min_t(u64, em->len - offset,
  2562. map->stripe_len - stripe_offset);
  2563. } else {
  2564. *length = em->len - offset;
  2565. }
  2566. if (!bbio_ret)
  2567. goto out;
  2568. num_stripes = 1;
  2569. stripe_index = 0;
  2570. stripe_nr_orig = stripe_nr;
  2571. stripe_nr_end = (offset + *length + map->stripe_len - 1) &
  2572. (~(map->stripe_len - 1));
  2573. do_div(stripe_nr_end, map->stripe_len);
  2574. stripe_end_offset = stripe_nr_end * map->stripe_len -
  2575. (offset + *length);
  2576. if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
  2577. if (rw & REQ_DISCARD)
  2578. num_stripes = min_t(u64, map->num_stripes,
  2579. stripe_nr_end - stripe_nr_orig);
  2580. stripe_index = do_div(stripe_nr, map->num_stripes);
  2581. } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
  2582. if (rw & (REQ_WRITE | REQ_DISCARD))
  2583. num_stripes = map->num_stripes;
  2584. else if (mirror_num)
  2585. stripe_index = mirror_num - 1;
  2586. else {
  2587. stripe_index = find_live_mirror(map, 0,
  2588. map->num_stripes,
  2589. current->pid % map->num_stripes);
  2590. mirror_num = stripe_index + 1;
  2591. }
  2592. } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
  2593. if (rw & (REQ_WRITE | REQ_DISCARD)) {
  2594. num_stripes = map->num_stripes;
  2595. } else if (mirror_num) {
  2596. stripe_index = mirror_num - 1;
  2597. } else {
  2598. mirror_num = 1;
  2599. }
  2600. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  2601. int factor = map->num_stripes / map->sub_stripes;
  2602. stripe_index = do_div(stripe_nr, factor);
  2603. stripe_index *= map->sub_stripes;
  2604. if (rw & REQ_WRITE)
  2605. num_stripes = map->sub_stripes;
  2606. else if (rw & REQ_DISCARD)
  2607. num_stripes = min_t(u64, map->sub_stripes *
  2608. (stripe_nr_end - stripe_nr_orig),
  2609. map->num_stripes);
  2610. else if (mirror_num)
  2611. stripe_index += mirror_num - 1;
  2612. else {
  2613. stripe_index = find_live_mirror(map, stripe_index,
  2614. map->sub_stripes, stripe_index +
  2615. current->pid % map->sub_stripes);
  2616. mirror_num = stripe_index + 1;
  2617. }
  2618. } else {
  2619. /*
  2620. * after this do_div call, stripe_nr is the number of stripes
  2621. * on this device we have to walk to find the data, and
  2622. * stripe_index is the number of our device in the stripe array
  2623. */
  2624. stripe_index = do_div(stripe_nr, map->num_stripes);
  2625. mirror_num = stripe_index + 1;
  2626. }
  2627. BUG_ON(stripe_index >= map->num_stripes);
  2628. if (rw & REQ_DISCARD) {
  2629. for (i = 0; i < num_stripes; i++) {
  2630. bbio->stripes[i].physical =
  2631. map->stripes[stripe_index].physical +
  2632. stripe_offset + stripe_nr * map->stripe_len;
  2633. bbio->stripes[i].dev = map->stripes[stripe_index].dev;
  2634. if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
  2635. u64 stripes;
  2636. u32 last_stripe = 0;
  2637. int j;
  2638. div_u64_rem(stripe_nr_end - 1,
  2639. map->num_stripes,
  2640. &last_stripe);
  2641. for (j = 0; j < map->num_stripes; j++) {
  2642. u32 test;
  2643. div_u64_rem(stripe_nr_end - 1 - j,
  2644. map->num_stripes, &test);
  2645. if (test == stripe_index)
  2646. break;
  2647. }
  2648. stripes = stripe_nr_end - 1 - j;
  2649. do_div(stripes, map->num_stripes);
  2650. bbio->stripes[i].length = map->stripe_len *
  2651. (stripes - stripe_nr + 1);
  2652. if (i == 0) {
  2653. bbio->stripes[i].length -=
  2654. stripe_offset;
  2655. stripe_offset = 0;
  2656. }
  2657. if (stripe_index == last_stripe)
  2658. bbio->stripes[i].length -=
  2659. stripe_end_offset;
  2660. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  2661. u64 stripes;
  2662. int j;
  2663. int factor = map->num_stripes /
  2664. map->sub_stripes;
  2665. u32 last_stripe = 0;
  2666. div_u64_rem(stripe_nr_end - 1,
  2667. factor, &last_stripe);
  2668. last_stripe *= map->sub_stripes;
  2669. for (j = 0; j < factor; j++) {
  2670. u32 test;
  2671. div_u64_rem(stripe_nr_end - 1 - j,
  2672. factor, &test);
  2673. if (test ==
  2674. stripe_index / map->sub_stripes)
  2675. break;
  2676. }
  2677. stripes = stripe_nr_end - 1 - j;
  2678. do_div(stripes, factor);
  2679. bbio->stripes[i].length = map->stripe_len *
  2680. (stripes - stripe_nr + 1);
  2681. if (i < map->sub_stripes) {
  2682. bbio->stripes[i].length -=
  2683. stripe_offset;
  2684. if (i == map->sub_stripes - 1)
  2685. stripe_offset = 0;
  2686. }
  2687. if (stripe_index >= last_stripe &&
  2688. stripe_index <= (last_stripe +
  2689. map->sub_stripes - 1)) {
  2690. bbio->stripes[i].length -=
  2691. stripe_end_offset;
  2692. }
  2693. } else
  2694. bbio->stripes[i].length = *length;
  2695. stripe_index++;
  2696. if (stripe_index == map->num_stripes) {
  2697. /* This could only happen for RAID0/10 */
  2698. stripe_index = 0;
  2699. stripe_nr++;
  2700. }
  2701. }
  2702. } else {
  2703. for (i = 0; i < num_stripes; i++) {
  2704. bbio->stripes[i].physical =
  2705. map->stripes[stripe_index].physical +
  2706. stripe_offset +
  2707. stripe_nr * map->stripe_len;
  2708. bbio->stripes[i].dev =
  2709. map->stripes[stripe_index].dev;
  2710. stripe_index++;
  2711. }
  2712. }
  2713. if (bbio_ret) {
  2714. *bbio_ret = bbio;
  2715. bbio->num_stripes = num_stripes;
  2716. bbio->max_errors = max_errors;
  2717. bbio->mirror_num = mirror_num;
  2718. }
  2719. out:
  2720. free_extent_map(em);
  2721. return 0;
  2722. }
  2723. int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
  2724. u64 logical, u64 *length,
  2725. struct btrfs_bio **bbio_ret, int mirror_num)
  2726. {
  2727. return __btrfs_map_block(map_tree, rw, logical, length, bbio_ret,
  2728. mirror_num);
  2729. }
  2730. int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
  2731. u64 chunk_start, u64 physical, u64 devid,
  2732. u64 **logical, int *naddrs, int *stripe_len)
  2733. {
  2734. struct extent_map_tree *em_tree = &map_tree->map_tree;
  2735. struct extent_map *em;
  2736. struct map_lookup *map;
  2737. u64 *buf;
  2738. u64 bytenr;
  2739. u64 length;
  2740. u64 stripe_nr;
  2741. int i, j, nr = 0;
  2742. read_lock(&em_tree->lock);
  2743. em = lookup_extent_mapping(em_tree, chunk_start, 1);
  2744. read_unlock(&em_tree->lock);
  2745. BUG_ON(!em || em->start != chunk_start);
  2746. map = (struct map_lookup *)em->bdev;
  2747. length = em->len;
  2748. if (map->type & BTRFS_BLOCK_GROUP_RAID10)
  2749. do_div(length, map->num_stripes / map->sub_stripes);
  2750. else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
  2751. do_div(length, map->num_stripes);
  2752. buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
  2753. BUG_ON(!buf);
  2754. for (i = 0; i < map->num_stripes; i++) {
  2755. if (devid && map->stripes[i].dev->devid != devid)
  2756. continue;
  2757. if (map->stripes[i].physical > physical ||
  2758. map->stripes[i].physical + length <= physical)
  2759. continue;
  2760. stripe_nr = physical - map->stripes[i].physical;
  2761. do_div(stripe_nr, map->stripe_len);
  2762. if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  2763. stripe_nr = stripe_nr * map->num_stripes + i;
  2764. do_div(stripe_nr, map->sub_stripes);
  2765. } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
  2766. stripe_nr = stripe_nr * map->num_stripes + i;
  2767. }
  2768. bytenr = chunk_start + stripe_nr * map->stripe_len;
  2769. WARN_ON(nr >= map->num_stripes);
  2770. for (j = 0; j < nr; j++) {
  2771. if (buf[j] == bytenr)
  2772. break;
  2773. }
  2774. if (j == nr) {
  2775. WARN_ON(nr >= map->num_stripes);
  2776. buf[nr++] = bytenr;
  2777. }
  2778. }
  2779. *logical = buf;
  2780. *naddrs = nr;
  2781. *stripe_len = map->stripe_len;
  2782. free_extent_map(em);
  2783. return 0;
  2784. }
  2785. static void btrfs_end_bio(struct bio *bio, int err)
  2786. {
  2787. struct btrfs_bio *bbio = bio->bi_private;
  2788. int is_orig_bio = 0;
  2789. if (err)
  2790. atomic_inc(&bbio->error);
  2791. if (bio == bbio->orig_bio)
  2792. is_orig_bio = 1;
  2793. if (atomic_dec_and_test(&bbio->stripes_pending)) {
  2794. if (!is_orig_bio) {
  2795. bio_put(bio);
  2796. bio = bbio->orig_bio;
  2797. }
  2798. bio->bi_private = bbio->private;
  2799. bio->bi_end_io = bbio->end_io;
  2800. bio->bi_bdev = (struct block_device *)
  2801. (unsigned long)bbio->mirror_num;
  2802. /* only send an error to the higher layers if it is
  2803. * beyond the tolerance of the multi-bio
  2804. */
  2805. if (atomic_read(&bbio->error) > bbio->max_errors) {
  2806. err = -EIO;
  2807. } else {
  2808. /*
  2809. * this bio is actually up to date, we didn't
  2810. * go over the max number of errors
  2811. */
  2812. set_bit(BIO_UPTODATE, &bio->bi_flags);
  2813. err = 0;
  2814. }
  2815. kfree(bbio);
  2816. bio_endio(bio, err);
  2817. } else if (!is_orig_bio) {
  2818. bio_put(bio);
  2819. }
  2820. }
  2821. struct async_sched {
  2822. struct bio *bio;
  2823. int rw;
  2824. struct btrfs_fs_info *info;
  2825. struct btrfs_work work;
  2826. };
  2827. /*
  2828. * see run_scheduled_bios for a description of why bios are collected for
  2829. * async submit.
  2830. *
  2831. * This will add one bio to the pending list for a device and make sure
  2832. * the work struct is scheduled.
  2833. */
  2834. static noinline int schedule_bio(struct btrfs_root *root,
  2835. struct btrfs_device *device,
  2836. int rw, struct bio *bio)
  2837. {
  2838. int should_queue = 1;
  2839. struct btrfs_pending_bios *pending_bios;
  2840. /* don't bother with additional async steps for reads, right now */
  2841. if (!(rw & REQ_WRITE)) {
  2842. bio_get(bio);
  2843. submit_bio(rw, bio);
  2844. bio_put(bio);
  2845. return 0;
  2846. }
  2847. /*
  2848. * nr_async_bios allows us to reliably return congestion to the
  2849. * higher layers. Otherwise, the async bio makes it appear we have
  2850. * made progress against dirty pages when we've really just put it
  2851. * on a queue for later
  2852. */
  2853. atomic_inc(&root->fs_info->nr_async_bios);
  2854. WARN_ON(bio->bi_next);
  2855. bio->bi_next = NULL;
  2856. bio->bi_rw |= rw;
  2857. spin_lock(&device->io_lock);
  2858. if (bio->bi_rw & REQ_SYNC)
  2859. pending_bios = &device->pending_sync_bios;
  2860. else
  2861. pending_bios = &device->pending_bios;
  2862. if (pending_bios->tail)
  2863. pending_bios->tail->bi_next = bio;
  2864. pending_bios->tail = bio;
  2865. if (!pending_bios->head)
  2866. pending_bios->head = bio;
  2867. if (device->running_pending)
  2868. should_queue = 0;
  2869. spin_unlock(&device->io_lock);
  2870. if (should_queue)
  2871. btrfs_queue_worker(&root->fs_info->submit_workers,
  2872. &device->work);
  2873. return 0;
  2874. }
  2875. int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
  2876. int mirror_num, int async_submit)
  2877. {
  2878. struct btrfs_mapping_tree *map_tree;
  2879. struct btrfs_device *dev;
  2880. struct bio *first_bio = bio;
  2881. u64 logical = (u64)bio->bi_sector << 9;
  2882. u64 length = 0;
  2883. u64 map_length;
  2884. int ret;
  2885. int dev_nr = 0;
  2886. int total_devs = 1;
  2887. struct btrfs_bio *bbio = NULL;
  2888. length = bio->bi_size;
  2889. map_tree = &root->fs_info->mapping_tree;
  2890. map_length = length;
  2891. ret = btrfs_map_block(map_tree, rw, logical, &map_length, &bbio,
  2892. mirror_num);
  2893. BUG_ON(ret);
  2894. total_devs = bbio->num_stripes;
  2895. if (map_length < length) {
  2896. printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
  2897. "len %llu\n", (unsigned long long)logical,
  2898. (unsigned long long)length,
  2899. (unsigned long long)map_length);
  2900. BUG();
  2901. }
  2902. bbio->orig_bio = first_bio;
  2903. bbio->private = first_bio->bi_private;
  2904. bbio->end_io = first_bio->bi_end_io;
  2905. atomic_set(&bbio->stripes_pending, bbio->num_stripes);
  2906. while (dev_nr < total_devs) {
  2907. if (dev_nr < total_devs - 1) {
  2908. bio = bio_clone(first_bio, GFP_NOFS);
  2909. BUG_ON(!bio);
  2910. } else {
  2911. bio = first_bio;
  2912. }
  2913. bio->bi_private = bbio;
  2914. bio->bi_end_io = btrfs_end_bio;
  2915. bio->bi_sector = bbio->stripes[dev_nr].physical >> 9;
  2916. dev = bbio->stripes[dev_nr].dev;
  2917. if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
  2918. pr_debug("btrfs_map_bio: rw %d, secor=%llu, dev=%lu "
  2919. "(%s id %llu), size=%u\n", rw,
  2920. (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
  2921. dev->name, dev->devid, bio->bi_size);
  2922. bio->bi_bdev = dev->bdev;
  2923. if (async_submit)
  2924. schedule_bio(root, dev, rw, bio);
  2925. else
  2926. submit_bio(rw, bio);
  2927. } else {
  2928. bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
  2929. bio->bi_sector = logical >> 9;
  2930. bio_endio(bio, -EIO);
  2931. }
  2932. dev_nr++;
  2933. }
  2934. return 0;
  2935. }
  2936. struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
  2937. u8 *uuid, u8 *fsid)
  2938. {
  2939. struct btrfs_device *device;
  2940. struct btrfs_fs_devices *cur_devices;
  2941. cur_devices = root->fs_info->fs_devices;
  2942. while (cur_devices) {
  2943. if (!fsid ||
  2944. !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
  2945. device = __find_device(&cur_devices->devices,
  2946. devid, uuid);
  2947. if (device)
  2948. return device;
  2949. }
  2950. cur_devices = cur_devices->seed;
  2951. }
  2952. return NULL;
  2953. }
  2954. static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
  2955. u64 devid, u8 *dev_uuid)
  2956. {
  2957. struct btrfs_device *device;
  2958. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  2959. device = kzalloc(sizeof(*device), GFP_NOFS);
  2960. if (!device)
  2961. return NULL;
  2962. list_add(&device->dev_list,
  2963. &fs_devices->devices);
  2964. device->dev_root = root->fs_info->dev_root;
  2965. device->devid = devid;
  2966. device->work.func = pending_bios_fn;
  2967. device->fs_devices = fs_devices;
  2968. device->missing = 1;
  2969. fs_devices->num_devices++;
  2970. fs_devices->missing_devices++;
  2971. spin_lock_init(&device->io_lock);
  2972. INIT_LIST_HEAD(&device->dev_alloc_list);
  2973. memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
  2974. return device;
  2975. }
  2976. static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
  2977. struct extent_buffer *leaf,
  2978. struct btrfs_chunk *chunk)
  2979. {
  2980. struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
  2981. struct map_lookup *map;
  2982. struct extent_map *em;
  2983. u64 logical;
  2984. u64 length;
  2985. u64 devid;
  2986. u8 uuid[BTRFS_UUID_SIZE];
  2987. int num_stripes;
  2988. int ret;
  2989. int i;
  2990. logical = key->offset;
  2991. length = btrfs_chunk_length(leaf, chunk);
  2992. read_lock(&map_tree->map_tree.lock);
  2993. em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
  2994. read_unlock(&map_tree->map_tree.lock);
  2995. /* already mapped? */
  2996. if (em && em->start <= logical && em->start + em->len > logical) {
  2997. free_extent_map(em);
  2998. return 0;
  2999. } else if (em) {
  3000. free_extent_map(em);
  3001. }
  3002. em = alloc_extent_map();
  3003. if (!em)
  3004. return -ENOMEM;
  3005. num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
  3006. map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
  3007. if (!map) {
  3008. free_extent_map(em);
  3009. return -ENOMEM;
  3010. }
  3011. em->bdev = (struct block_device *)map;
  3012. em->start = logical;
  3013. em->len = length;
  3014. em->block_start = 0;
  3015. em->block_len = em->len;
  3016. map->num_stripes = num_stripes;
  3017. map->io_width = btrfs_chunk_io_width(leaf, chunk);
  3018. map->io_align = btrfs_chunk_io_align(leaf, chunk);
  3019. map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
  3020. map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
  3021. map->type = btrfs_chunk_type(leaf, chunk);
  3022. map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
  3023. for (i = 0; i < num_stripes; i++) {
  3024. map->stripes[i].physical =
  3025. btrfs_stripe_offset_nr(leaf, chunk, i);
  3026. devid = btrfs_stripe_devid_nr(leaf, chunk, i);
  3027. read_extent_buffer(leaf, uuid, (unsigned long)
  3028. btrfs_stripe_dev_uuid_nr(chunk, i),
  3029. BTRFS_UUID_SIZE);
  3030. map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
  3031. NULL);
  3032. if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
  3033. kfree(map);
  3034. free_extent_map(em);
  3035. return -EIO;
  3036. }
  3037. if (!map->stripes[i].dev) {
  3038. map->stripes[i].dev =
  3039. add_missing_dev(root, devid, uuid);
  3040. if (!map->stripes[i].dev) {
  3041. kfree(map);
  3042. free_extent_map(em);
  3043. return -EIO;
  3044. }
  3045. }
  3046. map->stripes[i].dev->in_fs_metadata = 1;
  3047. }
  3048. write_lock(&map_tree->map_tree.lock);
  3049. ret = add_extent_mapping(&map_tree->map_tree, em);
  3050. write_unlock(&map_tree->map_tree.lock);
  3051. BUG_ON(ret);
  3052. free_extent_map(em);
  3053. return 0;
  3054. }
  3055. static int fill_device_from_item(struct extent_buffer *leaf,
  3056. struct btrfs_dev_item *dev_item,
  3057. struct btrfs_device *device)
  3058. {
  3059. unsigned long ptr;
  3060. device->devid = btrfs_device_id(leaf, dev_item);
  3061. device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
  3062. device->total_bytes = device->disk_total_bytes;
  3063. device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
  3064. device->type = btrfs_device_type(leaf, dev_item);
  3065. device->io_align = btrfs_device_io_align(leaf, dev_item);
  3066. device->io_width = btrfs_device_io_width(leaf, dev_item);
  3067. device->sector_size = btrfs_device_sector_size(leaf, dev_item);
  3068. ptr = (unsigned long)btrfs_device_uuid(dev_item);
  3069. read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
  3070. return 0;
  3071. }
  3072. static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
  3073. {
  3074. struct btrfs_fs_devices *fs_devices;
  3075. int ret;
  3076. mutex_lock(&uuid_mutex);
  3077. fs_devices = root->fs_info->fs_devices->seed;
  3078. while (fs_devices) {
  3079. if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
  3080. ret = 0;
  3081. goto out;
  3082. }
  3083. fs_devices = fs_devices->seed;
  3084. }
  3085. fs_devices = find_fsid(fsid);
  3086. if (!fs_devices) {
  3087. ret = -ENOENT;
  3088. goto out;
  3089. }
  3090. fs_devices = clone_fs_devices(fs_devices);
  3091. if (IS_ERR(fs_devices)) {
  3092. ret = PTR_ERR(fs_devices);
  3093. goto out;
  3094. }
  3095. ret = __btrfs_open_devices(fs_devices, FMODE_READ,
  3096. root->fs_info->bdev_holder);
  3097. if (ret)
  3098. goto out;
  3099. if (!fs_devices->seeding) {
  3100. __btrfs_close_devices(fs_devices);
  3101. free_fs_devices(fs_devices);
  3102. ret = -EINVAL;
  3103. goto out;
  3104. }
  3105. fs_devices->seed = root->fs_info->fs_devices->seed;
  3106. root->fs_info->fs_devices->seed = fs_devices;
  3107. out:
  3108. mutex_unlock(&uuid_mutex);
  3109. return ret;
  3110. }
  3111. static int read_one_dev(struct btrfs_root *root,
  3112. struct extent_buffer *leaf,
  3113. struct btrfs_dev_item *dev_item)
  3114. {
  3115. struct btrfs_device *device;
  3116. u64 devid;
  3117. int ret;
  3118. u8 fs_uuid[BTRFS_UUID_SIZE];
  3119. u8 dev_uuid[BTRFS_UUID_SIZE];
  3120. devid = btrfs_device_id(leaf, dev_item);
  3121. read_extent_buffer(leaf, dev_uuid,
  3122. (unsigned long)btrfs_device_uuid(dev_item),
  3123. BTRFS_UUID_SIZE);
  3124. read_extent_buffer(leaf, fs_uuid,
  3125. (unsigned long)btrfs_device_fsid(dev_item),
  3126. BTRFS_UUID_SIZE);
  3127. if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
  3128. ret = open_seed_devices(root, fs_uuid);
  3129. if (ret && !btrfs_test_opt(root, DEGRADED))
  3130. return ret;
  3131. }
  3132. device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
  3133. if (!device || !device->bdev) {
  3134. if (!btrfs_test_opt(root, DEGRADED))
  3135. return -EIO;
  3136. if (!device) {
  3137. printk(KERN_WARNING "warning devid %llu missing\n",
  3138. (unsigned long long)devid);
  3139. device = add_missing_dev(root, devid, dev_uuid);
  3140. if (!device)
  3141. return -ENOMEM;
  3142. } else if (!device->missing) {
  3143. /*
  3144. * this happens when a device that was properly setup
  3145. * in the device info lists suddenly goes bad.
  3146. * device->bdev is NULL, and so we have to set
  3147. * device->missing to one here
  3148. */
  3149. root->fs_info->fs_devices->missing_devices++;
  3150. device->missing = 1;
  3151. }
  3152. }
  3153. if (device->fs_devices != root->fs_info->fs_devices) {
  3154. BUG_ON(device->writeable);
  3155. if (device->generation !=
  3156. btrfs_device_generation(leaf, dev_item))
  3157. return -EINVAL;
  3158. }
  3159. fill_device_from_item(leaf, dev_item, device);
  3160. device->dev_root = root->fs_info->dev_root;
  3161. device->in_fs_metadata = 1;
  3162. if (device->writeable) {
  3163. device->fs_devices->total_rw_bytes += device->total_bytes;
  3164. spin_lock(&root->fs_info->free_chunk_lock);
  3165. root->fs_info->free_chunk_space += device->total_bytes -
  3166. device->bytes_used;
  3167. spin_unlock(&root->fs_info->free_chunk_lock);
  3168. }
  3169. ret = 0;
  3170. return ret;
  3171. }
  3172. int btrfs_read_sys_array(struct btrfs_root *root)
  3173. {
  3174. struct btrfs_super_block *super_copy = root->fs_info->super_copy;
  3175. struct extent_buffer *sb;
  3176. struct btrfs_disk_key *disk_key;
  3177. struct btrfs_chunk *chunk;
  3178. u8 *ptr;
  3179. unsigned long sb_ptr;
  3180. int ret = 0;
  3181. u32 num_stripes;
  3182. u32 array_size;
  3183. u32 len = 0;
  3184. u32 cur;
  3185. struct btrfs_key key;
  3186. sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
  3187. BTRFS_SUPER_INFO_SIZE);
  3188. if (!sb)
  3189. return -ENOMEM;
  3190. btrfs_set_buffer_uptodate(sb);
  3191. btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
  3192. write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
  3193. array_size = btrfs_super_sys_array_size(super_copy);
  3194. ptr = super_copy->sys_chunk_array;
  3195. sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
  3196. cur = 0;
  3197. while (cur < array_size) {
  3198. disk_key = (struct btrfs_disk_key *)ptr;
  3199. btrfs_disk_key_to_cpu(&key, disk_key);
  3200. len = sizeof(*disk_key); ptr += len;
  3201. sb_ptr += len;
  3202. cur += len;
  3203. if (key.type == BTRFS_CHUNK_ITEM_KEY) {
  3204. chunk = (struct btrfs_chunk *)sb_ptr;
  3205. ret = read_one_chunk(root, &key, sb, chunk);
  3206. if (ret)
  3207. break;
  3208. num_stripes = btrfs_chunk_num_stripes(sb, chunk);
  3209. len = btrfs_chunk_item_size(num_stripes);
  3210. } else {
  3211. ret = -EIO;
  3212. break;
  3213. }
  3214. ptr += len;
  3215. sb_ptr += len;
  3216. cur += len;
  3217. }
  3218. free_extent_buffer(sb);
  3219. return ret;
  3220. }
  3221. int btrfs_read_chunk_tree(struct btrfs_root *root)
  3222. {
  3223. struct btrfs_path *path;
  3224. struct extent_buffer *leaf;
  3225. struct btrfs_key key;
  3226. struct btrfs_key found_key;
  3227. int ret;
  3228. int slot;
  3229. root = root->fs_info->chunk_root;
  3230. path = btrfs_alloc_path();
  3231. if (!path)
  3232. return -ENOMEM;
  3233. /* first we search for all of the device items, and then we
  3234. * read in all of the chunk items. This way we can create chunk
  3235. * mappings that reference all of the devices that are afound
  3236. */
  3237. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  3238. key.offset = 0;
  3239. key.type = 0;
  3240. again:
  3241. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3242. if (ret < 0)
  3243. goto error;
  3244. while (1) {
  3245. leaf = path->nodes[0];
  3246. slot = path->slots[0];
  3247. if (slot >= btrfs_header_nritems(leaf)) {
  3248. ret = btrfs_next_leaf(root, path);
  3249. if (ret == 0)
  3250. continue;
  3251. if (ret < 0)
  3252. goto error;
  3253. break;
  3254. }
  3255. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  3256. if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
  3257. if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
  3258. break;
  3259. if (found_key.type == BTRFS_DEV_ITEM_KEY) {
  3260. struct btrfs_dev_item *dev_item;
  3261. dev_item = btrfs_item_ptr(leaf, slot,
  3262. struct btrfs_dev_item);
  3263. ret = read_one_dev(root, leaf, dev_item);
  3264. if (ret)
  3265. goto error;
  3266. }
  3267. } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
  3268. struct btrfs_chunk *chunk;
  3269. chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
  3270. ret = read_one_chunk(root, &found_key, leaf, chunk);
  3271. if (ret)
  3272. goto error;
  3273. }
  3274. path->slots[0]++;
  3275. }
  3276. if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
  3277. key.objectid = 0;
  3278. btrfs_release_path(path);
  3279. goto again;
  3280. }
  3281. ret = 0;
  3282. error:
  3283. btrfs_free_path(path);
  3284. return ret;
  3285. }