volumes.c 95 KB

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