volumes.c 86 KB

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