volumes.c 83 KB

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