volumes.c 95 KB

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