volumes.c 91 KB

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