volumes.c 95 KB

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