volumes.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565
  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 <asm/div64.h>
  24. #include "ctree.h"
  25. #include "extent_map.h"
  26. #include "disk-io.h"
  27. #include "transaction.h"
  28. #include "print-tree.h"
  29. #include "volumes.h"
  30. #include "async-thread.h"
  31. struct map_lookup {
  32. u64 type;
  33. int io_align;
  34. int io_width;
  35. int stripe_len;
  36. int sector_size;
  37. int num_stripes;
  38. int sub_stripes;
  39. struct btrfs_bio_stripe stripes[];
  40. };
  41. #define map_lookup_size(n) (sizeof(struct map_lookup) + \
  42. (sizeof(struct btrfs_bio_stripe) * (n)))
  43. static DEFINE_MUTEX(uuid_mutex);
  44. static LIST_HEAD(fs_uuids);
  45. void btrfs_lock_volumes(void)
  46. {
  47. mutex_lock(&uuid_mutex);
  48. }
  49. void btrfs_unlock_volumes(void)
  50. {
  51. mutex_unlock(&uuid_mutex);
  52. }
  53. static void lock_chunks(struct btrfs_root *root)
  54. {
  55. mutex_lock(&root->fs_info->alloc_mutex);
  56. mutex_lock(&root->fs_info->chunk_mutex);
  57. }
  58. static void unlock_chunks(struct btrfs_root *root)
  59. {
  60. mutex_unlock(&root->fs_info->chunk_mutex);
  61. mutex_unlock(&root->fs_info->alloc_mutex);
  62. }
  63. int btrfs_cleanup_fs_uuids(void)
  64. {
  65. struct btrfs_fs_devices *fs_devices;
  66. struct list_head *uuid_cur;
  67. struct list_head *devices_cur;
  68. struct btrfs_device *dev;
  69. list_for_each(uuid_cur, &fs_uuids) {
  70. fs_devices = list_entry(uuid_cur, struct btrfs_fs_devices,
  71. list);
  72. while(!list_empty(&fs_devices->devices)) {
  73. devices_cur = fs_devices->devices.next;
  74. dev = list_entry(devices_cur, struct btrfs_device,
  75. dev_list);
  76. if (dev->bdev) {
  77. close_bdev_excl(dev->bdev);
  78. fs_devices->open_devices--;
  79. }
  80. list_del(&dev->dev_list);
  81. kfree(dev->name);
  82. kfree(dev);
  83. }
  84. }
  85. return 0;
  86. }
  87. static noinline struct btrfs_device *__find_device(struct list_head *head,
  88. u64 devid, u8 *uuid)
  89. {
  90. struct btrfs_device *dev;
  91. struct list_head *cur;
  92. list_for_each(cur, head) {
  93. dev = list_entry(cur, struct btrfs_device, dev_list);
  94. if (dev->devid == devid &&
  95. (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
  96. return dev;
  97. }
  98. }
  99. return NULL;
  100. }
  101. static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
  102. {
  103. struct list_head *cur;
  104. struct btrfs_fs_devices *fs_devices;
  105. list_for_each(cur, &fs_uuids) {
  106. fs_devices = list_entry(cur, struct btrfs_fs_devices, list);
  107. if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
  108. return fs_devices;
  109. }
  110. return NULL;
  111. }
  112. /*
  113. * we try to collect pending bios for a device so we don't get a large
  114. * number of procs sending bios down to the same device. This greatly
  115. * improves the schedulers ability to collect and merge the bios.
  116. *
  117. * But, it also turns into a long list of bios to process and that is sure
  118. * to eventually make the worker thread block. The solution here is to
  119. * make some progress and then put this work struct back at the end of
  120. * the list if the block device is congested. This way, multiple devices
  121. * can make progress from a single worker thread.
  122. */
  123. static int noinline run_scheduled_bios(struct btrfs_device *device)
  124. {
  125. struct bio *pending;
  126. struct backing_dev_info *bdi;
  127. struct btrfs_fs_info *fs_info;
  128. struct bio *tail;
  129. struct bio *cur;
  130. int again = 0;
  131. unsigned long num_run = 0;
  132. unsigned long limit;
  133. bdi = device->bdev->bd_inode->i_mapping->backing_dev_info;
  134. fs_info = device->dev_root->fs_info;
  135. limit = btrfs_async_submit_limit(fs_info);
  136. limit = limit * 2 / 3;
  137. loop:
  138. spin_lock(&device->io_lock);
  139. /* take all the bios off the list at once and process them
  140. * later on (without the lock held). But, remember the
  141. * tail and other pointers so the bios can be properly reinserted
  142. * into the list if we hit congestion
  143. */
  144. pending = device->pending_bios;
  145. tail = device->pending_bio_tail;
  146. WARN_ON(pending && !tail);
  147. device->pending_bios = NULL;
  148. device->pending_bio_tail = NULL;
  149. /*
  150. * if pending was null this time around, no bios need processing
  151. * at all and we can stop. Otherwise it'll loop back up again
  152. * and do an additional check so no bios are missed.
  153. *
  154. * device->running_pending is used to synchronize with the
  155. * schedule_bio code.
  156. */
  157. if (pending) {
  158. again = 1;
  159. device->running_pending = 1;
  160. } else {
  161. again = 0;
  162. device->running_pending = 0;
  163. }
  164. spin_unlock(&device->io_lock);
  165. while(pending) {
  166. cur = pending;
  167. pending = pending->bi_next;
  168. cur->bi_next = NULL;
  169. atomic_dec(&fs_info->nr_async_bios);
  170. if (atomic_read(&fs_info->nr_async_bios) < limit &&
  171. waitqueue_active(&fs_info->async_submit_wait))
  172. wake_up(&fs_info->async_submit_wait);
  173. BUG_ON(atomic_read(&cur->bi_cnt) == 0);
  174. bio_get(cur);
  175. submit_bio(cur->bi_rw, cur);
  176. bio_put(cur);
  177. num_run++;
  178. /*
  179. * we made progress, there is more work to do and the bdi
  180. * is now congested. Back off and let other work structs
  181. * run instead
  182. */
  183. if (pending && bdi_write_congested(bdi)) {
  184. struct bio *old_head;
  185. spin_lock(&device->io_lock);
  186. old_head = device->pending_bios;
  187. device->pending_bios = pending;
  188. if (device->pending_bio_tail)
  189. tail->bi_next = old_head;
  190. else
  191. device->pending_bio_tail = tail;
  192. spin_unlock(&device->io_lock);
  193. btrfs_requeue_work(&device->work);
  194. goto done;
  195. }
  196. }
  197. if (again)
  198. goto loop;
  199. done:
  200. return 0;
  201. }
  202. void pending_bios_fn(struct btrfs_work *work)
  203. {
  204. struct btrfs_device *device;
  205. device = container_of(work, struct btrfs_device, work);
  206. run_scheduled_bios(device);
  207. }
  208. static noinline int device_list_add(const char *path,
  209. struct btrfs_super_block *disk_super,
  210. u64 devid, struct btrfs_fs_devices **fs_devices_ret)
  211. {
  212. struct btrfs_device *device;
  213. struct btrfs_fs_devices *fs_devices;
  214. u64 found_transid = btrfs_super_generation(disk_super);
  215. fs_devices = find_fsid(disk_super->fsid);
  216. if (!fs_devices) {
  217. fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
  218. if (!fs_devices)
  219. return -ENOMEM;
  220. INIT_LIST_HEAD(&fs_devices->devices);
  221. INIT_LIST_HEAD(&fs_devices->alloc_list);
  222. list_add(&fs_devices->list, &fs_uuids);
  223. memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
  224. fs_devices->latest_devid = devid;
  225. fs_devices->latest_trans = found_transid;
  226. device = NULL;
  227. } else {
  228. device = __find_device(&fs_devices->devices, devid,
  229. disk_super->dev_item.uuid);
  230. }
  231. if (!device) {
  232. device = kzalloc(sizeof(*device), GFP_NOFS);
  233. if (!device) {
  234. /* we can safely leave the fs_devices entry around */
  235. return -ENOMEM;
  236. }
  237. device->devid = devid;
  238. device->work.func = pending_bios_fn;
  239. memcpy(device->uuid, disk_super->dev_item.uuid,
  240. BTRFS_UUID_SIZE);
  241. device->barriers = 1;
  242. spin_lock_init(&device->io_lock);
  243. device->name = kstrdup(path, GFP_NOFS);
  244. if (!device->name) {
  245. kfree(device);
  246. return -ENOMEM;
  247. }
  248. list_add(&device->dev_list, &fs_devices->devices);
  249. list_add(&device->dev_alloc_list, &fs_devices->alloc_list);
  250. fs_devices->num_devices++;
  251. }
  252. if (found_transid > fs_devices->latest_trans) {
  253. fs_devices->latest_devid = devid;
  254. fs_devices->latest_trans = found_transid;
  255. }
  256. *fs_devices_ret = fs_devices;
  257. return 0;
  258. }
  259. int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
  260. {
  261. struct list_head *head = &fs_devices->devices;
  262. struct list_head *cur;
  263. struct btrfs_device *device;
  264. mutex_lock(&uuid_mutex);
  265. again:
  266. list_for_each(cur, head) {
  267. device = list_entry(cur, struct btrfs_device, dev_list);
  268. if (!device->in_fs_metadata) {
  269. struct block_device *bdev;
  270. list_del(&device->dev_list);
  271. list_del(&device->dev_alloc_list);
  272. fs_devices->num_devices--;
  273. if (device->bdev) {
  274. bdev = device->bdev;
  275. fs_devices->open_devices--;
  276. mutex_unlock(&uuid_mutex);
  277. close_bdev_excl(bdev);
  278. mutex_lock(&uuid_mutex);
  279. }
  280. kfree(device->name);
  281. kfree(device);
  282. goto again;
  283. }
  284. }
  285. mutex_unlock(&uuid_mutex);
  286. return 0;
  287. }
  288. int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
  289. {
  290. struct list_head *head = &fs_devices->devices;
  291. struct list_head *cur;
  292. struct btrfs_device *device;
  293. mutex_lock(&uuid_mutex);
  294. list_for_each(cur, head) {
  295. device = list_entry(cur, struct btrfs_device, dev_list);
  296. if (device->bdev) {
  297. close_bdev_excl(device->bdev);
  298. fs_devices->open_devices--;
  299. }
  300. device->bdev = NULL;
  301. device->in_fs_metadata = 0;
  302. }
  303. fs_devices->mounted = 0;
  304. mutex_unlock(&uuid_mutex);
  305. return 0;
  306. }
  307. int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
  308. int flags, void *holder)
  309. {
  310. struct block_device *bdev;
  311. struct list_head *head = &fs_devices->devices;
  312. struct list_head *cur;
  313. struct btrfs_device *device;
  314. struct block_device *latest_bdev = NULL;
  315. struct buffer_head *bh;
  316. struct btrfs_super_block *disk_super;
  317. u64 latest_devid = 0;
  318. u64 latest_transid = 0;
  319. u64 transid;
  320. u64 devid;
  321. int ret = 0;
  322. mutex_lock(&uuid_mutex);
  323. if (fs_devices->mounted)
  324. goto out;
  325. list_for_each(cur, head) {
  326. device = list_entry(cur, struct btrfs_device, dev_list);
  327. if (device->bdev)
  328. continue;
  329. if (!device->name)
  330. continue;
  331. bdev = open_bdev_excl(device->name, flags, holder);
  332. if (IS_ERR(bdev)) {
  333. printk("open %s failed\n", device->name);
  334. goto error;
  335. }
  336. set_blocksize(bdev, 4096);
  337. bh = __bread(bdev, BTRFS_SUPER_INFO_OFFSET / 4096, 4096);
  338. if (!bh)
  339. goto error_close;
  340. disk_super = (struct btrfs_super_block *)bh->b_data;
  341. if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
  342. sizeof(disk_super->magic)))
  343. goto error_brelse;
  344. devid = le64_to_cpu(disk_super->dev_item.devid);
  345. if (devid != device->devid)
  346. goto error_brelse;
  347. transid = btrfs_super_generation(disk_super);
  348. if (!latest_transid || transid > latest_transid) {
  349. latest_devid = devid;
  350. latest_transid = transid;
  351. latest_bdev = bdev;
  352. }
  353. device->bdev = bdev;
  354. device->in_fs_metadata = 0;
  355. fs_devices->open_devices++;
  356. continue;
  357. error_brelse:
  358. brelse(bh);
  359. error_close:
  360. close_bdev_excl(bdev);
  361. error:
  362. continue;
  363. }
  364. if (fs_devices->open_devices == 0) {
  365. ret = -EIO;
  366. goto out;
  367. }
  368. fs_devices->mounted = 1;
  369. fs_devices->latest_bdev = latest_bdev;
  370. fs_devices->latest_devid = latest_devid;
  371. fs_devices->latest_trans = latest_transid;
  372. out:
  373. mutex_unlock(&uuid_mutex);
  374. return ret;
  375. }
  376. int btrfs_scan_one_device(const char *path, int flags, void *holder,
  377. struct btrfs_fs_devices **fs_devices_ret)
  378. {
  379. struct btrfs_super_block *disk_super;
  380. struct block_device *bdev;
  381. struct buffer_head *bh;
  382. int ret;
  383. u64 devid;
  384. u64 transid;
  385. mutex_lock(&uuid_mutex);
  386. bdev = open_bdev_excl(path, flags, holder);
  387. if (IS_ERR(bdev)) {
  388. ret = PTR_ERR(bdev);
  389. goto error;
  390. }
  391. ret = set_blocksize(bdev, 4096);
  392. if (ret)
  393. goto error_close;
  394. bh = __bread(bdev, BTRFS_SUPER_INFO_OFFSET / 4096, 4096);
  395. if (!bh) {
  396. ret = -EIO;
  397. goto error_close;
  398. }
  399. disk_super = (struct btrfs_super_block *)bh->b_data;
  400. if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
  401. sizeof(disk_super->magic))) {
  402. ret = -EINVAL;
  403. goto error_brelse;
  404. }
  405. devid = le64_to_cpu(disk_super->dev_item.devid);
  406. transid = btrfs_super_generation(disk_super);
  407. if (disk_super->label[0])
  408. printk("device label %s ", disk_super->label);
  409. else {
  410. /* FIXME, make a readl uuid parser */
  411. printk("device fsid %llx-%llx ",
  412. *(unsigned long long *)disk_super->fsid,
  413. *(unsigned long long *)(disk_super->fsid + 8));
  414. }
  415. printk("devid %Lu transid %Lu %s\n", devid, transid, path);
  416. ret = device_list_add(path, disk_super, devid, fs_devices_ret);
  417. error_brelse:
  418. brelse(bh);
  419. error_close:
  420. close_bdev_excl(bdev);
  421. error:
  422. mutex_unlock(&uuid_mutex);
  423. return ret;
  424. }
  425. /*
  426. * this uses a pretty simple search, the expectation is that it is
  427. * called very infrequently and that a given device has a small number
  428. * of extents
  429. */
  430. static noinline int find_free_dev_extent(struct btrfs_trans_handle *trans,
  431. struct btrfs_device *device,
  432. struct btrfs_path *path,
  433. u64 num_bytes, u64 *start)
  434. {
  435. struct btrfs_key key;
  436. struct btrfs_root *root = device->dev_root;
  437. struct btrfs_dev_extent *dev_extent = NULL;
  438. u64 hole_size = 0;
  439. u64 last_byte = 0;
  440. u64 search_start = 0;
  441. u64 search_end = device->total_bytes;
  442. int ret;
  443. int slot = 0;
  444. int start_found;
  445. struct extent_buffer *l;
  446. start_found = 0;
  447. path->reada = 2;
  448. /* FIXME use last free of some kind */
  449. /* we don't want to overwrite the superblock on the drive,
  450. * so we make sure to start at an offset of at least 1MB
  451. */
  452. search_start = max((u64)1024 * 1024, search_start);
  453. if (root->fs_info->alloc_start + num_bytes <= device->total_bytes)
  454. search_start = max(root->fs_info->alloc_start, search_start);
  455. key.objectid = device->devid;
  456. key.offset = search_start;
  457. key.type = BTRFS_DEV_EXTENT_KEY;
  458. ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
  459. if (ret < 0)
  460. goto error;
  461. ret = btrfs_previous_item(root, path, 0, key.type);
  462. if (ret < 0)
  463. goto error;
  464. l = path->nodes[0];
  465. btrfs_item_key_to_cpu(l, &key, path->slots[0]);
  466. while (1) {
  467. l = path->nodes[0];
  468. slot = path->slots[0];
  469. if (slot >= btrfs_header_nritems(l)) {
  470. ret = btrfs_next_leaf(root, path);
  471. if (ret == 0)
  472. continue;
  473. if (ret < 0)
  474. goto error;
  475. no_more_items:
  476. if (!start_found) {
  477. if (search_start >= search_end) {
  478. ret = -ENOSPC;
  479. goto error;
  480. }
  481. *start = search_start;
  482. start_found = 1;
  483. goto check_pending;
  484. }
  485. *start = last_byte > search_start ?
  486. last_byte : search_start;
  487. if (search_end <= *start) {
  488. ret = -ENOSPC;
  489. goto error;
  490. }
  491. goto check_pending;
  492. }
  493. btrfs_item_key_to_cpu(l, &key, slot);
  494. if (key.objectid < device->devid)
  495. goto next;
  496. if (key.objectid > device->devid)
  497. goto no_more_items;
  498. if (key.offset >= search_start && key.offset > last_byte &&
  499. start_found) {
  500. if (last_byte < search_start)
  501. last_byte = search_start;
  502. hole_size = key.offset - last_byte;
  503. if (key.offset > last_byte &&
  504. hole_size >= num_bytes) {
  505. *start = last_byte;
  506. goto check_pending;
  507. }
  508. }
  509. if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) {
  510. goto next;
  511. }
  512. start_found = 1;
  513. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  514. last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent);
  515. next:
  516. path->slots[0]++;
  517. cond_resched();
  518. }
  519. check_pending:
  520. /* we have to make sure we didn't find an extent that has already
  521. * been allocated by the map tree or the original allocation
  522. */
  523. btrfs_release_path(root, path);
  524. BUG_ON(*start < search_start);
  525. if (*start + num_bytes > search_end) {
  526. ret = -ENOSPC;
  527. goto error;
  528. }
  529. /* check for pending inserts here */
  530. return 0;
  531. error:
  532. btrfs_release_path(root, path);
  533. return ret;
  534. }
  535. int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
  536. struct btrfs_device *device,
  537. u64 start)
  538. {
  539. int ret;
  540. struct btrfs_path *path;
  541. struct btrfs_root *root = device->dev_root;
  542. struct btrfs_key key;
  543. struct btrfs_key found_key;
  544. struct extent_buffer *leaf = NULL;
  545. struct btrfs_dev_extent *extent = NULL;
  546. path = btrfs_alloc_path();
  547. if (!path)
  548. return -ENOMEM;
  549. key.objectid = device->devid;
  550. key.offset = start;
  551. key.type = BTRFS_DEV_EXTENT_KEY;
  552. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  553. if (ret > 0) {
  554. ret = btrfs_previous_item(root, path, key.objectid,
  555. BTRFS_DEV_EXTENT_KEY);
  556. BUG_ON(ret);
  557. leaf = path->nodes[0];
  558. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  559. extent = btrfs_item_ptr(leaf, path->slots[0],
  560. struct btrfs_dev_extent);
  561. BUG_ON(found_key.offset > start || found_key.offset +
  562. btrfs_dev_extent_length(leaf, extent) < start);
  563. ret = 0;
  564. } else if (ret == 0) {
  565. leaf = path->nodes[0];
  566. extent = btrfs_item_ptr(leaf, path->slots[0],
  567. struct btrfs_dev_extent);
  568. }
  569. BUG_ON(ret);
  570. if (device->bytes_used > 0)
  571. device->bytes_used -= btrfs_dev_extent_length(leaf, extent);
  572. ret = btrfs_del_item(trans, root, path);
  573. BUG_ON(ret);
  574. btrfs_free_path(path);
  575. return ret;
  576. }
  577. int noinline btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
  578. struct btrfs_device *device,
  579. u64 chunk_tree, u64 chunk_objectid,
  580. u64 chunk_offset,
  581. u64 num_bytes, u64 *start)
  582. {
  583. int ret;
  584. struct btrfs_path *path;
  585. struct btrfs_root *root = device->dev_root;
  586. struct btrfs_dev_extent *extent;
  587. struct extent_buffer *leaf;
  588. struct btrfs_key key;
  589. WARN_ON(!device->in_fs_metadata);
  590. path = btrfs_alloc_path();
  591. if (!path)
  592. return -ENOMEM;
  593. ret = find_free_dev_extent(trans, device, path, num_bytes, start);
  594. if (ret) {
  595. goto err;
  596. }
  597. key.objectid = device->devid;
  598. key.offset = *start;
  599. key.type = BTRFS_DEV_EXTENT_KEY;
  600. ret = btrfs_insert_empty_item(trans, root, path, &key,
  601. sizeof(*extent));
  602. BUG_ON(ret);
  603. leaf = path->nodes[0];
  604. extent = btrfs_item_ptr(leaf, path->slots[0],
  605. struct btrfs_dev_extent);
  606. btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
  607. btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
  608. btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
  609. write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
  610. (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
  611. BTRFS_UUID_SIZE);
  612. btrfs_set_dev_extent_length(leaf, extent, num_bytes);
  613. btrfs_mark_buffer_dirty(leaf);
  614. err:
  615. btrfs_free_path(path);
  616. return ret;
  617. }
  618. static noinline int find_next_chunk(struct btrfs_root *root,
  619. u64 objectid, u64 *offset)
  620. {
  621. struct btrfs_path *path;
  622. int ret;
  623. struct btrfs_key key;
  624. struct btrfs_chunk *chunk;
  625. struct btrfs_key found_key;
  626. path = btrfs_alloc_path();
  627. BUG_ON(!path);
  628. key.objectid = objectid;
  629. key.offset = (u64)-1;
  630. key.type = BTRFS_CHUNK_ITEM_KEY;
  631. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  632. if (ret < 0)
  633. goto error;
  634. BUG_ON(ret == 0);
  635. ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
  636. if (ret) {
  637. *offset = 0;
  638. } else {
  639. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  640. path->slots[0]);
  641. if (found_key.objectid != objectid)
  642. *offset = 0;
  643. else {
  644. chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
  645. struct btrfs_chunk);
  646. *offset = found_key.offset +
  647. btrfs_chunk_length(path->nodes[0], chunk);
  648. }
  649. }
  650. ret = 0;
  651. error:
  652. btrfs_free_path(path);
  653. return ret;
  654. }
  655. static noinline int find_next_devid(struct btrfs_root *root,
  656. struct btrfs_path *path, u64 *objectid)
  657. {
  658. int ret;
  659. struct btrfs_key key;
  660. struct btrfs_key found_key;
  661. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  662. key.type = BTRFS_DEV_ITEM_KEY;
  663. key.offset = (u64)-1;
  664. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  665. if (ret < 0)
  666. goto error;
  667. BUG_ON(ret == 0);
  668. ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
  669. BTRFS_DEV_ITEM_KEY);
  670. if (ret) {
  671. *objectid = 1;
  672. } else {
  673. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  674. path->slots[0]);
  675. *objectid = found_key.offset + 1;
  676. }
  677. ret = 0;
  678. error:
  679. btrfs_release_path(root, path);
  680. return ret;
  681. }
  682. /*
  683. * the device information is stored in the chunk root
  684. * the btrfs_device struct should be fully filled in
  685. */
  686. int btrfs_add_device(struct btrfs_trans_handle *trans,
  687. struct btrfs_root *root,
  688. struct btrfs_device *device)
  689. {
  690. int ret;
  691. struct btrfs_path *path;
  692. struct btrfs_dev_item *dev_item;
  693. struct extent_buffer *leaf;
  694. struct btrfs_key key;
  695. unsigned long ptr;
  696. u64 free_devid = 0;
  697. root = root->fs_info->chunk_root;
  698. path = btrfs_alloc_path();
  699. if (!path)
  700. return -ENOMEM;
  701. ret = find_next_devid(root, path, &free_devid);
  702. if (ret)
  703. goto out;
  704. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  705. key.type = BTRFS_DEV_ITEM_KEY;
  706. key.offset = free_devid;
  707. ret = btrfs_insert_empty_item(trans, root, path, &key,
  708. sizeof(*dev_item));
  709. if (ret)
  710. goto out;
  711. leaf = path->nodes[0];
  712. dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
  713. device->devid = free_devid;
  714. btrfs_set_device_id(leaf, dev_item, device->devid);
  715. btrfs_set_device_type(leaf, dev_item, device->type);
  716. btrfs_set_device_io_align(leaf, dev_item, device->io_align);
  717. btrfs_set_device_io_width(leaf, dev_item, device->io_width);
  718. btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
  719. btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
  720. btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
  721. btrfs_set_device_group(leaf, dev_item, 0);
  722. btrfs_set_device_seek_speed(leaf, dev_item, 0);
  723. btrfs_set_device_bandwidth(leaf, dev_item, 0);
  724. ptr = (unsigned long)btrfs_device_uuid(dev_item);
  725. write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
  726. btrfs_mark_buffer_dirty(leaf);
  727. ret = 0;
  728. out:
  729. btrfs_free_path(path);
  730. return ret;
  731. }
  732. static int btrfs_rm_dev_item(struct btrfs_root *root,
  733. struct btrfs_device *device)
  734. {
  735. int ret;
  736. struct btrfs_path *path;
  737. struct block_device *bdev = device->bdev;
  738. struct btrfs_device *next_dev;
  739. struct btrfs_key key;
  740. u64 total_bytes;
  741. struct btrfs_fs_devices *fs_devices;
  742. struct btrfs_trans_handle *trans;
  743. root = root->fs_info->chunk_root;
  744. path = btrfs_alloc_path();
  745. if (!path)
  746. return -ENOMEM;
  747. trans = btrfs_start_transaction(root, 1);
  748. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  749. key.type = BTRFS_DEV_ITEM_KEY;
  750. key.offset = device->devid;
  751. lock_chunks(root);
  752. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  753. if (ret < 0)
  754. goto out;
  755. if (ret > 0) {
  756. ret = -ENOENT;
  757. goto out;
  758. }
  759. ret = btrfs_del_item(trans, root, path);
  760. if (ret)
  761. goto out;
  762. /*
  763. * at this point, the device is zero sized. We want to
  764. * remove it from the devices list and zero out the old super
  765. */
  766. list_del_init(&device->dev_list);
  767. list_del_init(&device->dev_alloc_list);
  768. fs_devices = root->fs_info->fs_devices;
  769. next_dev = list_entry(fs_devices->devices.next, struct btrfs_device,
  770. dev_list);
  771. if (bdev == root->fs_info->sb->s_bdev)
  772. root->fs_info->sb->s_bdev = next_dev->bdev;
  773. if (bdev == fs_devices->latest_bdev)
  774. fs_devices->latest_bdev = next_dev->bdev;
  775. total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
  776. btrfs_set_super_num_devices(&root->fs_info->super_copy,
  777. total_bytes - 1);
  778. out:
  779. btrfs_free_path(path);
  780. unlock_chunks(root);
  781. btrfs_commit_transaction(trans, root);
  782. return ret;
  783. }
  784. int btrfs_rm_device(struct btrfs_root *root, char *device_path)
  785. {
  786. struct btrfs_device *device;
  787. struct block_device *bdev;
  788. struct buffer_head *bh = NULL;
  789. struct btrfs_super_block *disk_super;
  790. u64 all_avail;
  791. u64 devid;
  792. int ret = 0;
  793. mutex_lock(&uuid_mutex);
  794. mutex_lock(&root->fs_info->volume_mutex);
  795. all_avail = root->fs_info->avail_data_alloc_bits |
  796. root->fs_info->avail_system_alloc_bits |
  797. root->fs_info->avail_metadata_alloc_bits;
  798. if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
  799. btrfs_super_num_devices(&root->fs_info->super_copy) <= 4) {
  800. printk("btrfs: unable to go below four devices on raid10\n");
  801. ret = -EINVAL;
  802. goto out;
  803. }
  804. if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
  805. btrfs_super_num_devices(&root->fs_info->super_copy) <= 2) {
  806. printk("btrfs: unable to go below two devices on raid1\n");
  807. ret = -EINVAL;
  808. goto out;
  809. }
  810. if (strcmp(device_path, "missing") == 0) {
  811. struct list_head *cur;
  812. struct list_head *devices;
  813. struct btrfs_device *tmp;
  814. device = NULL;
  815. devices = &root->fs_info->fs_devices->devices;
  816. list_for_each(cur, devices) {
  817. tmp = list_entry(cur, struct btrfs_device, dev_list);
  818. if (tmp->in_fs_metadata && !tmp->bdev) {
  819. device = tmp;
  820. break;
  821. }
  822. }
  823. bdev = NULL;
  824. bh = NULL;
  825. disk_super = NULL;
  826. if (!device) {
  827. printk("btrfs: no missing devices found to remove\n");
  828. goto out;
  829. }
  830. } else {
  831. bdev = open_bdev_excl(device_path, 0,
  832. root->fs_info->bdev_holder);
  833. if (IS_ERR(bdev)) {
  834. ret = PTR_ERR(bdev);
  835. goto out;
  836. }
  837. bh = __bread(bdev, BTRFS_SUPER_INFO_OFFSET / 4096, 4096);
  838. if (!bh) {
  839. ret = -EIO;
  840. goto error_close;
  841. }
  842. disk_super = (struct btrfs_super_block *)bh->b_data;
  843. if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
  844. sizeof(disk_super->magic))) {
  845. ret = -ENOENT;
  846. goto error_brelse;
  847. }
  848. if (memcmp(disk_super->fsid, root->fs_info->fsid,
  849. BTRFS_FSID_SIZE)) {
  850. ret = -ENOENT;
  851. goto error_brelse;
  852. }
  853. devid = le64_to_cpu(disk_super->dev_item.devid);
  854. device = btrfs_find_device(root, devid, NULL);
  855. if (!device) {
  856. ret = -ENOENT;
  857. goto error_brelse;
  858. }
  859. }
  860. root->fs_info->fs_devices->num_devices--;
  861. root->fs_info->fs_devices->open_devices--;
  862. ret = btrfs_shrink_device(device, 0);
  863. if (ret)
  864. goto error_brelse;
  865. ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
  866. if (ret)
  867. goto error_brelse;
  868. if (bh) {
  869. /* make sure this device isn't detected as part of
  870. * the FS anymore
  871. */
  872. memset(&disk_super->magic, 0, sizeof(disk_super->magic));
  873. set_buffer_dirty(bh);
  874. sync_dirty_buffer(bh);
  875. brelse(bh);
  876. }
  877. if (device->bdev) {
  878. /* one close for the device struct or super_block */
  879. close_bdev_excl(device->bdev);
  880. }
  881. if (bdev) {
  882. /* one close for us */
  883. close_bdev_excl(bdev);
  884. }
  885. kfree(device->name);
  886. kfree(device);
  887. ret = 0;
  888. goto out;
  889. error_brelse:
  890. brelse(bh);
  891. error_close:
  892. if (bdev)
  893. close_bdev_excl(bdev);
  894. out:
  895. mutex_unlock(&root->fs_info->volume_mutex);
  896. mutex_unlock(&uuid_mutex);
  897. return ret;
  898. }
  899. int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
  900. {
  901. struct btrfs_trans_handle *trans;
  902. struct btrfs_device *device;
  903. struct block_device *bdev;
  904. struct list_head *cur;
  905. struct list_head *devices;
  906. u64 total_bytes;
  907. int ret = 0;
  908. bdev = open_bdev_excl(device_path, 0, root->fs_info->bdev_holder);
  909. if (!bdev) {
  910. return -EIO;
  911. }
  912. mutex_lock(&root->fs_info->volume_mutex);
  913. trans = btrfs_start_transaction(root, 1);
  914. lock_chunks(root);
  915. devices = &root->fs_info->fs_devices->devices;
  916. list_for_each(cur, devices) {
  917. device = list_entry(cur, struct btrfs_device, dev_list);
  918. if (device->bdev == bdev) {
  919. ret = -EEXIST;
  920. goto out;
  921. }
  922. }
  923. device = kzalloc(sizeof(*device), GFP_NOFS);
  924. if (!device) {
  925. /* we can safely leave the fs_devices entry around */
  926. ret = -ENOMEM;
  927. goto out_close_bdev;
  928. }
  929. device->barriers = 1;
  930. device->work.func = pending_bios_fn;
  931. generate_random_uuid(device->uuid);
  932. spin_lock_init(&device->io_lock);
  933. device->name = kstrdup(device_path, GFP_NOFS);
  934. if (!device->name) {
  935. kfree(device);
  936. goto out_close_bdev;
  937. }
  938. device->io_width = root->sectorsize;
  939. device->io_align = root->sectorsize;
  940. device->sector_size = root->sectorsize;
  941. device->total_bytes = i_size_read(bdev->bd_inode);
  942. device->dev_root = root->fs_info->dev_root;
  943. device->bdev = bdev;
  944. device->in_fs_metadata = 1;
  945. ret = btrfs_add_device(trans, root, device);
  946. if (ret)
  947. goto out_close_bdev;
  948. set_blocksize(device->bdev, 4096);
  949. total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
  950. btrfs_set_super_total_bytes(&root->fs_info->super_copy,
  951. total_bytes + device->total_bytes);
  952. total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
  953. btrfs_set_super_num_devices(&root->fs_info->super_copy,
  954. total_bytes + 1);
  955. list_add(&device->dev_list, &root->fs_info->fs_devices->devices);
  956. list_add(&device->dev_alloc_list,
  957. &root->fs_info->fs_devices->alloc_list);
  958. root->fs_info->fs_devices->num_devices++;
  959. root->fs_info->fs_devices->open_devices++;
  960. out:
  961. unlock_chunks(root);
  962. btrfs_end_transaction(trans, root);
  963. mutex_unlock(&root->fs_info->volume_mutex);
  964. return ret;
  965. out_close_bdev:
  966. close_bdev_excl(bdev);
  967. goto out;
  968. }
  969. int noinline btrfs_update_device(struct btrfs_trans_handle *trans,
  970. struct btrfs_device *device)
  971. {
  972. int ret;
  973. struct btrfs_path *path;
  974. struct btrfs_root *root;
  975. struct btrfs_dev_item *dev_item;
  976. struct extent_buffer *leaf;
  977. struct btrfs_key key;
  978. root = device->dev_root->fs_info->chunk_root;
  979. path = btrfs_alloc_path();
  980. if (!path)
  981. return -ENOMEM;
  982. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  983. key.type = BTRFS_DEV_ITEM_KEY;
  984. key.offset = device->devid;
  985. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  986. if (ret < 0)
  987. goto out;
  988. if (ret > 0) {
  989. ret = -ENOENT;
  990. goto out;
  991. }
  992. leaf = path->nodes[0];
  993. dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
  994. btrfs_set_device_id(leaf, dev_item, device->devid);
  995. btrfs_set_device_type(leaf, dev_item, device->type);
  996. btrfs_set_device_io_align(leaf, dev_item, device->io_align);
  997. btrfs_set_device_io_width(leaf, dev_item, device->io_width);
  998. btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
  999. btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
  1000. btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
  1001. btrfs_mark_buffer_dirty(leaf);
  1002. out:
  1003. btrfs_free_path(path);
  1004. return ret;
  1005. }
  1006. static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
  1007. struct btrfs_device *device, u64 new_size)
  1008. {
  1009. struct btrfs_super_block *super_copy =
  1010. &device->dev_root->fs_info->super_copy;
  1011. u64 old_total = btrfs_super_total_bytes(super_copy);
  1012. u64 diff = new_size - device->total_bytes;
  1013. btrfs_set_super_total_bytes(super_copy, old_total + diff);
  1014. return btrfs_update_device(trans, device);
  1015. }
  1016. int btrfs_grow_device(struct btrfs_trans_handle *trans,
  1017. struct btrfs_device *device, u64 new_size)
  1018. {
  1019. int ret;
  1020. lock_chunks(device->dev_root);
  1021. ret = __btrfs_grow_device(trans, device, new_size);
  1022. unlock_chunks(device->dev_root);
  1023. return ret;
  1024. }
  1025. static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
  1026. struct btrfs_root *root,
  1027. u64 chunk_tree, u64 chunk_objectid,
  1028. u64 chunk_offset)
  1029. {
  1030. int ret;
  1031. struct btrfs_path *path;
  1032. struct btrfs_key key;
  1033. root = root->fs_info->chunk_root;
  1034. path = btrfs_alloc_path();
  1035. if (!path)
  1036. return -ENOMEM;
  1037. key.objectid = chunk_objectid;
  1038. key.offset = chunk_offset;
  1039. key.type = BTRFS_CHUNK_ITEM_KEY;
  1040. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  1041. BUG_ON(ret);
  1042. ret = btrfs_del_item(trans, root, path);
  1043. BUG_ON(ret);
  1044. btrfs_free_path(path);
  1045. return 0;
  1046. }
  1047. int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
  1048. chunk_offset)
  1049. {
  1050. struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
  1051. struct btrfs_disk_key *disk_key;
  1052. struct btrfs_chunk *chunk;
  1053. u8 *ptr;
  1054. int ret = 0;
  1055. u32 num_stripes;
  1056. u32 array_size;
  1057. u32 len = 0;
  1058. u32 cur;
  1059. struct btrfs_key key;
  1060. array_size = btrfs_super_sys_array_size(super_copy);
  1061. ptr = super_copy->sys_chunk_array;
  1062. cur = 0;
  1063. while (cur < array_size) {
  1064. disk_key = (struct btrfs_disk_key *)ptr;
  1065. btrfs_disk_key_to_cpu(&key, disk_key);
  1066. len = sizeof(*disk_key);
  1067. if (key.type == BTRFS_CHUNK_ITEM_KEY) {
  1068. chunk = (struct btrfs_chunk *)(ptr + len);
  1069. num_stripes = btrfs_stack_chunk_num_stripes(chunk);
  1070. len += btrfs_chunk_item_size(num_stripes);
  1071. } else {
  1072. ret = -EIO;
  1073. break;
  1074. }
  1075. if (key.objectid == chunk_objectid &&
  1076. key.offset == chunk_offset) {
  1077. memmove(ptr, ptr + len, array_size - (cur + len));
  1078. array_size -= len;
  1079. btrfs_set_super_sys_array_size(super_copy, array_size);
  1080. } else {
  1081. ptr += len;
  1082. cur += len;
  1083. }
  1084. }
  1085. return ret;
  1086. }
  1087. int btrfs_relocate_chunk(struct btrfs_root *root,
  1088. u64 chunk_tree, u64 chunk_objectid,
  1089. u64 chunk_offset)
  1090. {
  1091. struct extent_map_tree *em_tree;
  1092. struct btrfs_root *extent_root;
  1093. struct btrfs_trans_handle *trans;
  1094. struct extent_map *em;
  1095. struct map_lookup *map;
  1096. int ret;
  1097. int i;
  1098. printk("btrfs relocating chunk %llu\n",
  1099. (unsigned long long)chunk_offset);
  1100. root = root->fs_info->chunk_root;
  1101. extent_root = root->fs_info->extent_root;
  1102. em_tree = &root->fs_info->mapping_tree.map_tree;
  1103. /* step one, relocate all the extents inside this chunk */
  1104. ret = btrfs_shrink_extent_tree(extent_root, chunk_offset);
  1105. BUG_ON(ret);
  1106. trans = btrfs_start_transaction(root, 1);
  1107. BUG_ON(!trans);
  1108. lock_chunks(root);
  1109. /*
  1110. * step two, delete the device extents and the
  1111. * chunk tree entries
  1112. */
  1113. spin_lock(&em_tree->lock);
  1114. em = lookup_extent_mapping(em_tree, chunk_offset, 1);
  1115. spin_unlock(&em_tree->lock);
  1116. BUG_ON(em->start > chunk_offset ||
  1117. em->start + em->len < chunk_offset);
  1118. map = (struct map_lookup *)em->bdev;
  1119. for (i = 0; i < map->num_stripes; i++) {
  1120. ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
  1121. map->stripes[i].physical);
  1122. BUG_ON(ret);
  1123. if (map->stripes[i].dev) {
  1124. ret = btrfs_update_device(trans, map->stripes[i].dev);
  1125. BUG_ON(ret);
  1126. }
  1127. }
  1128. ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
  1129. chunk_offset);
  1130. BUG_ON(ret);
  1131. if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
  1132. ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
  1133. BUG_ON(ret);
  1134. }
  1135. spin_lock(&em_tree->lock);
  1136. remove_extent_mapping(em_tree, em);
  1137. kfree(map);
  1138. em->bdev = NULL;
  1139. /* once for the tree */
  1140. free_extent_map(em);
  1141. spin_unlock(&em_tree->lock);
  1142. /* once for us */
  1143. free_extent_map(em);
  1144. unlock_chunks(root);
  1145. btrfs_end_transaction(trans, root);
  1146. return 0;
  1147. }
  1148. static u64 div_factor(u64 num, int factor)
  1149. {
  1150. if (factor == 10)
  1151. return num;
  1152. num *= factor;
  1153. do_div(num, 10);
  1154. return num;
  1155. }
  1156. int btrfs_balance(struct btrfs_root *dev_root)
  1157. {
  1158. int ret;
  1159. struct list_head *cur;
  1160. struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
  1161. struct btrfs_device *device;
  1162. u64 old_size;
  1163. u64 size_to_free;
  1164. struct btrfs_path *path;
  1165. struct btrfs_key key;
  1166. struct btrfs_chunk *chunk;
  1167. struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
  1168. struct btrfs_trans_handle *trans;
  1169. struct btrfs_key found_key;
  1170. mutex_lock(&dev_root->fs_info->volume_mutex);
  1171. dev_root = dev_root->fs_info->dev_root;
  1172. /* step one make some room on all the devices */
  1173. list_for_each(cur, devices) {
  1174. device = list_entry(cur, struct btrfs_device, dev_list);
  1175. old_size = device->total_bytes;
  1176. size_to_free = div_factor(old_size, 1);
  1177. size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
  1178. if (device->total_bytes - device->bytes_used > size_to_free)
  1179. continue;
  1180. ret = btrfs_shrink_device(device, old_size - size_to_free);
  1181. BUG_ON(ret);
  1182. trans = btrfs_start_transaction(dev_root, 1);
  1183. BUG_ON(!trans);
  1184. ret = btrfs_grow_device(trans, device, old_size);
  1185. BUG_ON(ret);
  1186. btrfs_end_transaction(trans, dev_root);
  1187. }
  1188. /* step two, relocate all the chunks */
  1189. path = btrfs_alloc_path();
  1190. BUG_ON(!path);
  1191. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  1192. key.offset = (u64)-1;
  1193. key.type = BTRFS_CHUNK_ITEM_KEY;
  1194. while(1) {
  1195. ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
  1196. if (ret < 0)
  1197. goto error;
  1198. /*
  1199. * this shouldn't happen, it means the last relocate
  1200. * failed
  1201. */
  1202. if (ret == 0)
  1203. break;
  1204. ret = btrfs_previous_item(chunk_root, path, 0,
  1205. BTRFS_CHUNK_ITEM_KEY);
  1206. if (ret)
  1207. break;
  1208. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  1209. path->slots[0]);
  1210. if (found_key.objectid != key.objectid)
  1211. break;
  1212. chunk = btrfs_item_ptr(path->nodes[0],
  1213. path->slots[0],
  1214. struct btrfs_chunk);
  1215. key.offset = found_key.offset;
  1216. /* chunk zero is special */
  1217. if (key.offset == 0)
  1218. break;
  1219. btrfs_release_path(chunk_root, path);
  1220. ret = btrfs_relocate_chunk(chunk_root,
  1221. chunk_root->root_key.objectid,
  1222. found_key.objectid,
  1223. found_key.offset);
  1224. BUG_ON(ret);
  1225. }
  1226. ret = 0;
  1227. error:
  1228. btrfs_free_path(path);
  1229. mutex_unlock(&dev_root->fs_info->volume_mutex);
  1230. return ret;
  1231. }
  1232. /*
  1233. * shrinking a device means finding all of the device extents past
  1234. * the new size, and then following the back refs to the chunks.
  1235. * The chunk relocation code actually frees the device extent
  1236. */
  1237. int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
  1238. {
  1239. struct btrfs_trans_handle *trans;
  1240. struct btrfs_root *root = device->dev_root;
  1241. struct btrfs_dev_extent *dev_extent = NULL;
  1242. struct btrfs_path *path;
  1243. u64 length;
  1244. u64 chunk_tree;
  1245. u64 chunk_objectid;
  1246. u64 chunk_offset;
  1247. int ret;
  1248. int slot;
  1249. struct extent_buffer *l;
  1250. struct btrfs_key key;
  1251. struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
  1252. u64 old_total = btrfs_super_total_bytes(super_copy);
  1253. u64 diff = device->total_bytes - new_size;
  1254. path = btrfs_alloc_path();
  1255. if (!path)
  1256. return -ENOMEM;
  1257. trans = btrfs_start_transaction(root, 1);
  1258. if (!trans) {
  1259. ret = -ENOMEM;
  1260. goto done;
  1261. }
  1262. path->reada = 2;
  1263. lock_chunks(root);
  1264. device->total_bytes = new_size;
  1265. ret = btrfs_update_device(trans, device);
  1266. if (ret) {
  1267. unlock_chunks(root);
  1268. btrfs_end_transaction(trans, root);
  1269. goto done;
  1270. }
  1271. WARN_ON(diff > old_total);
  1272. btrfs_set_super_total_bytes(super_copy, old_total - diff);
  1273. unlock_chunks(root);
  1274. btrfs_end_transaction(trans, root);
  1275. key.objectid = device->devid;
  1276. key.offset = (u64)-1;
  1277. key.type = BTRFS_DEV_EXTENT_KEY;
  1278. while (1) {
  1279. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1280. if (ret < 0)
  1281. goto done;
  1282. ret = btrfs_previous_item(root, path, 0, key.type);
  1283. if (ret < 0)
  1284. goto done;
  1285. if (ret) {
  1286. ret = 0;
  1287. goto done;
  1288. }
  1289. l = path->nodes[0];
  1290. slot = path->slots[0];
  1291. btrfs_item_key_to_cpu(l, &key, path->slots[0]);
  1292. if (key.objectid != device->devid)
  1293. goto done;
  1294. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  1295. length = btrfs_dev_extent_length(l, dev_extent);
  1296. if (key.offset + length <= new_size)
  1297. goto done;
  1298. chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
  1299. chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
  1300. chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
  1301. btrfs_release_path(root, path);
  1302. ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
  1303. chunk_offset);
  1304. if (ret)
  1305. goto done;
  1306. }
  1307. done:
  1308. btrfs_free_path(path);
  1309. return ret;
  1310. }
  1311. int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
  1312. struct btrfs_root *root,
  1313. struct btrfs_key *key,
  1314. struct btrfs_chunk *chunk, int item_size)
  1315. {
  1316. struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
  1317. struct btrfs_disk_key disk_key;
  1318. u32 array_size;
  1319. u8 *ptr;
  1320. array_size = btrfs_super_sys_array_size(super_copy);
  1321. if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
  1322. return -EFBIG;
  1323. ptr = super_copy->sys_chunk_array + array_size;
  1324. btrfs_cpu_key_to_disk(&disk_key, key);
  1325. memcpy(ptr, &disk_key, sizeof(disk_key));
  1326. ptr += sizeof(disk_key);
  1327. memcpy(ptr, chunk, item_size);
  1328. item_size += sizeof(disk_key);
  1329. btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
  1330. return 0;
  1331. }
  1332. static u64 noinline chunk_bytes_by_type(u64 type, u64 calc_size,
  1333. int num_stripes, int sub_stripes)
  1334. {
  1335. if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
  1336. return calc_size;
  1337. else if (type & BTRFS_BLOCK_GROUP_RAID10)
  1338. return calc_size * (num_stripes / sub_stripes);
  1339. else
  1340. return calc_size * num_stripes;
  1341. }
  1342. int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
  1343. struct btrfs_root *extent_root, u64 *start,
  1344. u64 *num_bytes, u64 type)
  1345. {
  1346. u64 dev_offset;
  1347. struct btrfs_fs_info *info = extent_root->fs_info;
  1348. struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
  1349. struct btrfs_path *path;
  1350. struct btrfs_stripe *stripes;
  1351. struct btrfs_device *device = NULL;
  1352. struct btrfs_chunk *chunk;
  1353. struct list_head private_devs;
  1354. struct list_head *dev_list;
  1355. struct list_head *cur;
  1356. struct extent_map_tree *em_tree;
  1357. struct map_lookup *map;
  1358. struct extent_map *em;
  1359. int min_stripe_size = 1 * 1024 * 1024;
  1360. u64 physical;
  1361. u64 calc_size = 1024 * 1024 * 1024;
  1362. u64 max_chunk_size = calc_size;
  1363. u64 min_free;
  1364. u64 avail;
  1365. u64 max_avail = 0;
  1366. u64 percent_max;
  1367. int num_stripes = 1;
  1368. int min_stripes = 1;
  1369. int sub_stripes = 0;
  1370. int looped = 0;
  1371. int ret;
  1372. int index;
  1373. int stripe_len = 64 * 1024;
  1374. struct btrfs_key key;
  1375. if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
  1376. (type & BTRFS_BLOCK_GROUP_DUP)) {
  1377. WARN_ON(1);
  1378. type &= ~BTRFS_BLOCK_GROUP_DUP;
  1379. }
  1380. dev_list = &extent_root->fs_info->fs_devices->alloc_list;
  1381. if (list_empty(dev_list))
  1382. return -ENOSPC;
  1383. if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
  1384. num_stripes = extent_root->fs_info->fs_devices->open_devices;
  1385. min_stripes = 2;
  1386. }
  1387. if (type & (BTRFS_BLOCK_GROUP_DUP)) {
  1388. num_stripes = 2;
  1389. min_stripes = 2;
  1390. }
  1391. if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
  1392. num_stripes = min_t(u64, 2,
  1393. extent_root->fs_info->fs_devices->open_devices);
  1394. if (num_stripes < 2)
  1395. return -ENOSPC;
  1396. min_stripes = 2;
  1397. }
  1398. if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
  1399. num_stripes = extent_root->fs_info->fs_devices->open_devices;
  1400. if (num_stripes < 4)
  1401. return -ENOSPC;
  1402. num_stripes &= ~(u32)1;
  1403. sub_stripes = 2;
  1404. min_stripes = 4;
  1405. }
  1406. if (type & BTRFS_BLOCK_GROUP_DATA) {
  1407. max_chunk_size = 10 * calc_size;
  1408. min_stripe_size = 64 * 1024 * 1024;
  1409. } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
  1410. max_chunk_size = 4 * calc_size;
  1411. min_stripe_size = 32 * 1024 * 1024;
  1412. } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
  1413. calc_size = 8 * 1024 * 1024;
  1414. max_chunk_size = calc_size * 2;
  1415. min_stripe_size = 1 * 1024 * 1024;
  1416. }
  1417. path = btrfs_alloc_path();
  1418. if (!path)
  1419. return -ENOMEM;
  1420. /* we don't want a chunk larger than 10% of the FS */
  1421. percent_max = div_factor(btrfs_super_total_bytes(&info->super_copy), 1);
  1422. max_chunk_size = min(percent_max, max_chunk_size);
  1423. again:
  1424. if (calc_size * num_stripes > max_chunk_size) {
  1425. calc_size = max_chunk_size;
  1426. do_div(calc_size, num_stripes);
  1427. do_div(calc_size, stripe_len);
  1428. calc_size *= stripe_len;
  1429. }
  1430. /* we don't want tiny stripes */
  1431. calc_size = max_t(u64, min_stripe_size, calc_size);
  1432. do_div(calc_size, stripe_len);
  1433. calc_size *= stripe_len;
  1434. INIT_LIST_HEAD(&private_devs);
  1435. cur = dev_list->next;
  1436. index = 0;
  1437. if (type & BTRFS_BLOCK_GROUP_DUP)
  1438. min_free = calc_size * 2;
  1439. else
  1440. min_free = calc_size;
  1441. /*
  1442. * we add 1MB because we never use the first 1MB of the device, unless
  1443. * we've looped, then we are likely allocating the maximum amount of
  1444. * space left already
  1445. */
  1446. if (!looped)
  1447. min_free += 1024 * 1024;
  1448. /* build a private list of devices we will allocate from */
  1449. while(index < num_stripes) {
  1450. device = list_entry(cur, struct btrfs_device, dev_alloc_list);
  1451. if (device->total_bytes > device->bytes_used)
  1452. avail = device->total_bytes - device->bytes_used;
  1453. else
  1454. avail = 0;
  1455. cur = cur->next;
  1456. if (device->in_fs_metadata && avail >= min_free) {
  1457. u64 ignored_start = 0;
  1458. ret = find_free_dev_extent(trans, device, path,
  1459. min_free,
  1460. &ignored_start);
  1461. if (ret == 0) {
  1462. list_move_tail(&device->dev_alloc_list,
  1463. &private_devs);
  1464. index++;
  1465. if (type & BTRFS_BLOCK_GROUP_DUP)
  1466. index++;
  1467. }
  1468. } else if (device->in_fs_metadata && avail > max_avail)
  1469. max_avail = avail;
  1470. if (cur == dev_list)
  1471. break;
  1472. }
  1473. if (index < num_stripes) {
  1474. list_splice(&private_devs, dev_list);
  1475. if (index >= min_stripes) {
  1476. num_stripes = index;
  1477. if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
  1478. num_stripes /= sub_stripes;
  1479. num_stripes *= sub_stripes;
  1480. }
  1481. looped = 1;
  1482. goto again;
  1483. }
  1484. if (!looped && max_avail > 0) {
  1485. looped = 1;
  1486. calc_size = max_avail;
  1487. goto again;
  1488. }
  1489. btrfs_free_path(path);
  1490. return -ENOSPC;
  1491. }
  1492. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  1493. key.type = BTRFS_CHUNK_ITEM_KEY;
  1494. ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
  1495. &key.offset);
  1496. if (ret) {
  1497. btrfs_free_path(path);
  1498. return ret;
  1499. }
  1500. chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS);
  1501. if (!chunk) {
  1502. btrfs_free_path(path);
  1503. return -ENOMEM;
  1504. }
  1505. map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
  1506. if (!map) {
  1507. kfree(chunk);
  1508. btrfs_free_path(path);
  1509. return -ENOMEM;
  1510. }
  1511. btrfs_free_path(path);
  1512. path = NULL;
  1513. stripes = &chunk->stripe;
  1514. *num_bytes = chunk_bytes_by_type(type, calc_size,
  1515. num_stripes, sub_stripes);
  1516. index = 0;
  1517. while(index < num_stripes) {
  1518. struct btrfs_stripe *stripe;
  1519. BUG_ON(list_empty(&private_devs));
  1520. cur = private_devs.next;
  1521. device = list_entry(cur, struct btrfs_device, dev_alloc_list);
  1522. /* loop over this device again if we're doing a dup group */
  1523. if (!(type & BTRFS_BLOCK_GROUP_DUP) ||
  1524. (index == num_stripes - 1))
  1525. list_move_tail(&device->dev_alloc_list, dev_list);
  1526. ret = btrfs_alloc_dev_extent(trans, device,
  1527. info->chunk_root->root_key.objectid,
  1528. BTRFS_FIRST_CHUNK_TREE_OBJECTID, key.offset,
  1529. calc_size, &dev_offset);
  1530. BUG_ON(ret);
  1531. device->bytes_used += calc_size;
  1532. ret = btrfs_update_device(trans, device);
  1533. BUG_ON(ret);
  1534. map->stripes[index].dev = device;
  1535. map->stripes[index].physical = dev_offset;
  1536. stripe = stripes + index;
  1537. btrfs_set_stack_stripe_devid(stripe, device->devid);
  1538. btrfs_set_stack_stripe_offset(stripe, dev_offset);
  1539. memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
  1540. physical = dev_offset;
  1541. index++;
  1542. }
  1543. BUG_ON(!list_empty(&private_devs));
  1544. /* key was set above */
  1545. btrfs_set_stack_chunk_length(chunk, *num_bytes);
  1546. btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
  1547. btrfs_set_stack_chunk_stripe_len(chunk, stripe_len);
  1548. btrfs_set_stack_chunk_type(chunk, type);
  1549. btrfs_set_stack_chunk_num_stripes(chunk, num_stripes);
  1550. btrfs_set_stack_chunk_io_align(chunk, stripe_len);
  1551. btrfs_set_stack_chunk_io_width(chunk, stripe_len);
  1552. btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
  1553. btrfs_set_stack_chunk_sub_stripes(chunk, sub_stripes);
  1554. map->sector_size = extent_root->sectorsize;
  1555. map->stripe_len = stripe_len;
  1556. map->io_align = stripe_len;
  1557. map->io_width = stripe_len;
  1558. map->type = type;
  1559. map->num_stripes = num_stripes;
  1560. map->sub_stripes = sub_stripes;
  1561. ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
  1562. btrfs_chunk_item_size(num_stripes));
  1563. BUG_ON(ret);
  1564. *start = key.offset;;
  1565. em = alloc_extent_map(GFP_NOFS);
  1566. if (!em)
  1567. return -ENOMEM;
  1568. em->bdev = (struct block_device *)map;
  1569. em->start = key.offset;
  1570. em->len = *num_bytes;
  1571. em->block_start = 0;
  1572. if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
  1573. ret = btrfs_add_system_chunk(trans, chunk_root, &key,
  1574. chunk, btrfs_chunk_item_size(num_stripes));
  1575. BUG_ON(ret);
  1576. }
  1577. kfree(chunk);
  1578. em_tree = &extent_root->fs_info->mapping_tree.map_tree;
  1579. spin_lock(&em_tree->lock);
  1580. ret = add_extent_mapping(em_tree, em);
  1581. spin_unlock(&em_tree->lock);
  1582. BUG_ON(ret);
  1583. free_extent_map(em);
  1584. return ret;
  1585. }
  1586. void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
  1587. {
  1588. extent_map_tree_init(&tree->map_tree, GFP_NOFS);
  1589. }
  1590. void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
  1591. {
  1592. struct extent_map *em;
  1593. while(1) {
  1594. spin_lock(&tree->map_tree.lock);
  1595. em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
  1596. if (em)
  1597. remove_extent_mapping(&tree->map_tree, em);
  1598. spin_unlock(&tree->map_tree.lock);
  1599. if (!em)
  1600. break;
  1601. kfree(em->bdev);
  1602. /* once for us */
  1603. free_extent_map(em);
  1604. /* once for the tree */
  1605. free_extent_map(em);
  1606. }
  1607. }
  1608. int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
  1609. {
  1610. struct extent_map *em;
  1611. struct map_lookup *map;
  1612. struct extent_map_tree *em_tree = &map_tree->map_tree;
  1613. int ret;
  1614. spin_lock(&em_tree->lock);
  1615. em = lookup_extent_mapping(em_tree, logical, len);
  1616. spin_unlock(&em_tree->lock);
  1617. BUG_ON(!em);
  1618. BUG_ON(em->start > logical || em->start + em->len < logical);
  1619. map = (struct map_lookup *)em->bdev;
  1620. if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
  1621. ret = map->num_stripes;
  1622. else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
  1623. ret = map->sub_stripes;
  1624. else
  1625. ret = 1;
  1626. free_extent_map(em);
  1627. return ret;
  1628. }
  1629. static int find_live_mirror(struct map_lookup *map, int first, int num,
  1630. int optimal)
  1631. {
  1632. int i;
  1633. if (map->stripes[optimal].dev->bdev)
  1634. return optimal;
  1635. for (i = first; i < first + num; i++) {
  1636. if (map->stripes[i].dev->bdev)
  1637. return i;
  1638. }
  1639. /* we couldn't find one that doesn't fail. Just return something
  1640. * and the io error handling code will clean up eventually
  1641. */
  1642. return optimal;
  1643. }
  1644. static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
  1645. u64 logical, u64 *length,
  1646. struct btrfs_multi_bio **multi_ret,
  1647. int mirror_num, struct page *unplug_page)
  1648. {
  1649. struct extent_map *em;
  1650. struct map_lookup *map;
  1651. struct extent_map_tree *em_tree = &map_tree->map_tree;
  1652. u64 offset;
  1653. u64 stripe_offset;
  1654. u64 stripe_nr;
  1655. int stripes_allocated = 8;
  1656. int stripes_required = 1;
  1657. int stripe_index;
  1658. int i;
  1659. int num_stripes;
  1660. int max_errors = 0;
  1661. struct btrfs_multi_bio *multi = NULL;
  1662. if (multi_ret && !(rw & (1 << BIO_RW))) {
  1663. stripes_allocated = 1;
  1664. }
  1665. again:
  1666. if (multi_ret) {
  1667. multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
  1668. GFP_NOFS);
  1669. if (!multi)
  1670. return -ENOMEM;
  1671. atomic_set(&multi->error, 0);
  1672. }
  1673. spin_lock(&em_tree->lock);
  1674. em = lookup_extent_mapping(em_tree, logical, *length);
  1675. spin_unlock(&em_tree->lock);
  1676. if (!em && unplug_page)
  1677. return 0;
  1678. if (!em) {
  1679. printk("unable to find logical %Lu len %Lu\n", logical, *length);
  1680. BUG();
  1681. }
  1682. BUG_ON(em->start > logical || em->start + em->len < logical);
  1683. map = (struct map_lookup *)em->bdev;
  1684. offset = logical - em->start;
  1685. if (mirror_num > map->num_stripes)
  1686. mirror_num = 0;
  1687. /* if our multi bio struct is too small, back off and try again */
  1688. if (rw & (1 << BIO_RW)) {
  1689. if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
  1690. BTRFS_BLOCK_GROUP_DUP)) {
  1691. stripes_required = map->num_stripes;
  1692. max_errors = 1;
  1693. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  1694. stripes_required = map->sub_stripes;
  1695. max_errors = 1;
  1696. }
  1697. }
  1698. if (multi_ret && rw == WRITE &&
  1699. stripes_allocated < stripes_required) {
  1700. stripes_allocated = map->num_stripes;
  1701. free_extent_map(em);
  1702. kfree(multi);
  1703. goto again;
  1704. }
  1705. stripe_nr = offset;
  1706. /*
  1707. * stripe_nr counts the total number of stripes we have to stride
  1708. * to get to this block
  1709. */
  1710. do_div(stripe_nr, map->stripe_len);
  1711. stripe_offset = stripe_nr * map->stripe_len;
  1712. BUG_ON(offset < stripe_offset);
  1713. /* stripe_offset is the offset of this block in its stripe*/
  1714. stripe_offset = offset - stripe_offset;
  1715. if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
  1716. BTRFS_BLOCK_GROUP_RAID10 |
  1717. BTRFS_BLOCK_GROUP_DUP)) {
  1718. /* we limit the length of each bio to what fits in a stripe */
  1719. *length = min_t(u64, em->len - offset,
  1720. map->stripe_len - stripe_offset);
  1721. } else {
  1722. *length = em->len - offset;
  1723. }
  1724. if (!multi_ret && !unplug_page)
  1725. goto out;
  1726. num_stripes = 1;
  1727. stripe_index = 0;
  1728. if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
  1729. if (unplug_page || (rw & (1 << BIO_RW)))
  1730. num_stripes = map->num_stripes;
  1731. else if (mirror_num)
  1732. stripe_index = mirror_num - 1;
  1733. else {
  1734. stripe_index = find_live_mirror(map, 0,
  1735. map->num_stripes,
  1736. current->pid % map->num_stripes);
  1737. }
  1738. } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
  1739. if (rw & (1 << BIO_RW))
  1740. num_stripes = map->num_stripes;
  1741. else if (mirror_num)
  1742. stripe_index = mirror_num - 1;
  1743. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  1744. int factor = map->num_stripes / map->sub_stripes;
  1745. stripe_index = do_div(stripe_nr, factor);
  1746. stripe_index *= map->sub_stripes;
  1747. if (unplug_page || (rw & (1 << BIO_RW)))
  1748. num_stripes = map->sub_stripes;
  1749. else if (mirror_num)
  1750. stripe_index += mirror_num - 1;
  1751. else {
  1752. stripe_index = find_live_mirror(map, stripe_index,
  1753. map->sub_stripes, stripe_index +
  1754. current->pid % map->sub_stripes);
  1755. }
  1756. } else {
  1757. /*
  1758. * after this do_div call, stripe_nr is the number of stripes
  1759. * on this device we have to walk to find the data, and
  1760. * stripe_index is the number of our device in the stripe array
  1761. */
  1762. stripe_index = do_div(stripe_nr, map->num_stripes);
  1763. }
  1764. BUG_ON(stripe_index >= map->num_stripes);
  1765. for (i = 0; i < num_stripes; i++) {
  1766. if (unplug_page) {
  1767. struct btrfs_device *device;
  1768. struct backing_dev_info *bdi;
  1769. device = map->stripes[stripe_index].dev;
  1770. if (device->bdev) {
  1771. bdi = blk_get_backing_dev_info(device->bdev);
  1772. if (bdi->unplug_io_fn) {
  1773. bdi->unplug_io_fn(bdi, unplug_page);
  1774. }
  1775. }
  1776. } else {
  1777. multi->stripes[i].physical =
  1778. map->stripes[stripe_index].physical +
  1779. stripe_offset + stripe_nr * map->stripe_len;
  1780. multi->stripes[i].dev = map->stripes[stripe_index].dev;
  1781. }
  1782. stripe_index++;
  1783. }
  1784. if (multi_ret) {
  1785. *multi_ret = multi;
  1786. multi->num_stripes = num_stripes;
  1787. multi->max_errors = max_errors;
  1788. }
  1789. out:
  1790. free_extent_map(em);
  1791. return 0;
  1792. }
  1793. int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
  1794. u64 logical, u64 *length,
  1795. struct btrfs_multi_bio **multi_ret, int mirror_num)
  1796. {
  1797. return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
  1798. mirror_num, NULL);
  1799. }
  1800. int btrfs_unplug_page(struct btrfs_mapping_tree *map_tree,
  1801. u64 logical, struct page *page)
  1802. {
  1803. u64 length = PAGE_CACHE_SIZE;
  1804. return __btrfs_map_block(map_tree, READ, logical, &length,
  1805. NULL, 0, page);
  1806. }
  1807. #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
  1808. static void end_bio_multi_stripe(struct bio *bio, int err)
  1809. #else
  1810. static int end_bio_multi_stripe(struct bio *bio,
  1811. unsigned int bytes_done, int err)
  1812. #endif
  1813. {
  1814. struct btrfs_multi_bio *multi = bio->bi_private;
  1815. int is_orig_bio = 0;
  1816. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
  1817. if (bio->bi_size)
  1818. return 1;
  1819. #endif
  1820. if (err)
  1821. atomic_inc(&multi->error);
  1822. if (bio == multi->orig_bio)
  1823. is_orig_bio = 1;
  1824. if (atomic_dec_and_test(&multi->stripes_pending)) {
  1825. if (!is_orig_bio) {
  1826. bio_put(bio);
  1827. bio = multi->orig_bio;
  1828. }
  1829. bio->bi_private = multi->private;
  1830. bio->bi_end_io = multi->end_io;
  1831. /* only send an error to the higher layers if it is
  1832. * beyond the tolerance of the multi-bio
  1833. */
  1834. if (atomic_read(&multi->error) > multi->max_errors) {
  1835. err = -EIO;
  1836. } else if (err) {
  1837. /*
  1838. * this bio is actually up to date, we didn't
  1839. * go over the max number of errors
  1840. */
  1841. set_bit(BIO_UPTODATE, &bio->bi_flags);
  1842. err = 0;
  1843. }
  1844. kfree(multi);
  1845. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
  1846. bio_endio(bio, bio->bi_size, err);
  1847. #else
  1848. bio_endio(bio, err);
  1849. #endif
  1850. } else if (!is_orig_bio) {
  1851. bio_put(bio);
  1852. }
  1853. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
  1854. return 0;
  1855. #endif
  1856. }
  1857. struct async_sched {
  1858. struct bio *bio;
  1859. int rw;
  1860. struct btrfs_fs_info *info;
  1861. struct btrfs_work work;
  1862. };
  1863. /*
  1864. * see run_scheduled_bios for a description of why bios are collected for
  1865. * async submit.
  1866. *
  1867. * This will add one bio to the pending list for a device and make sure
  1868. * the work struct is scheduled.
  1869. */
  1870. static int noinline schedule_bio(struct btrfs_root *root,
  1871. struct btrfs_device *device,
  1872. int rw, struct bio *bio)
  1873. {
  1874. int should_queue = 1;
  1875. /* don't bother with additional async steps for reads, right now */
  1876. if (!(rw & (1 << BIO_RW))) {
  1877. bio_get(bio);
  1878. submit_bio(rw, bio);
  1879. bio_put(bio);
  1880. return 0;
  1881. }
  1882. /*
  1883. * nr_async_bios allows us to reliably return congestion to the
  1884. * higher layers. Otherwise, the async bio makes it appear we have
  1885. * made progress against dirty pages when we've really just put it
  1886. * on a queue for later
  1887. */
  1888. atomic_inc(&root->fs_info->nr_async_bios);
  1889. WARN_ON(bio->bi_next);
  1890. bio->bi_next = NULL;
  1891. bio->bi_rw |= rw;
  1892. spin_lock(&device->io_lock);
  1893. if (device->pending_bio_tail)
  1894. device->pending_bio_tail->bi_next = bio;
  1895. device->pending_bio_tail = bio;
  1896. if (!device->pending_bios)
  1897. device->pending_bios = bio;
  1898. if (device->running_pending)
  1899. should_queue = 0;
  1900. spin_unlock(&device->io_lock);
  1901. if (should_queue)
  1902. btrfs_queue_worker(&root->fs_info->submit_workers,
  1903. &device->work);
  1904. return 0;
  1905. }
  1906. int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
  1907. int mirror_num, int async_submit)
  1908. {
  1909. struct btrfs_mapping_tree *map_tree;
  1910. struct btrfs_device *dev;
  1911. struct bio *first_bio = bio;
  1912. u64 logical = bio->bi_sector << 9;
  1913. u64 length = 0;
  1914. u64 map_length;
  1915. struct btrfs_multi_bio *multi = NULL;
  1916. int ret;
  1917. int dev_nr = 0;
  1918. int total_devs = 1;
  1919. length = bio->bi_size;
  1920. map_tree = &root->fs_info->mapping_tree;
  1921. map_length = length;
  1922. ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
  1923. mirror_num);
  1924. BUG_ON(ret);
  1925. total_devs = multi->num_stripes;
  1926. if (map_length < length) {
  1927. printk("mapping failed logical %Lu bio len %Lu "
  1928. "len %Lu\n", logical, length, map_length);
  1929. BUG();
  1930. }
  1931. multi->end_io = first_bio->bi_end_io;
  1932. multi->private = first_bio->bi_private;
  1933. multi->orig_bio = first_bio;
  1934. atomic_set(&multi->stripes_pending, multi->num_stripes);
  1935. while(dev_nr < total_devs) {
  1936. if (total_devs > 1) {
  1937. if (dev_nr < total_devs - 1) {
  1938. bio = bio_clone(first_bio, GFP_NOFS);
  1939. BUG_ON(!bio);
  1940. } else {
  1941. bio = first_bio;
  1942. }
  1943. bio->bi_private = multi;
  1944. bio->bi_end_io = end_bio_multi_stripe;
  1945. }
  1946. bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
  1947. dev = multi->stripes[dev_nr].dev;
  1948. if (dev && dev->bdev) {
  1949. bio->bi_bdev = dev->bdev;
  1950. if (async_submit)
  1951. schedule_bio(root, dev, rw, bio);
  1952. else
  1953. submit_bio(rw, bio);
  1954. } else {
  1955. bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
  1956. bio->bi_sector = logical >> 9;
  1957. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
  1958. bio_endio(bio, bio->bi_size, -EIO);
  1959. #else
  1960. bio_endio(bio, -EIO);
  1961. #endif
  1962. }
  1963. dev_nr++;
  1964. }
  1965. if (total_devs == 1)
  1966. kfree(multi);
  1967. return 0;
  1968. }
  1969. struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
  1970. u8 *uuid)
  1971. {
  1972. struct list_head *head = &root->fs_info->fs_devices->devices;
  1973. return __find_device(head, devid, uuid);
  1974. }
  1975. static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
  1976. u64 devid, u8 *dev_uuid)
  1977. {
  1978. struct btrfs_device *device;
  1979. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  1980. device = kzalloc(sizeof(*device), GFP_NOFS);
  1981. list_add(&device->dev_list,
  1982. &fs_devices->devices);
  1983. list_add(&device->dev_alloc_list,
  1984. &fs_devices->alloc_list);
  1985. device->barriers = 1;
  1986. device->dev_root = root->fs_info->dev_root;
  1987. device->devid = devid;
  1988. device->work.func = pending_bios_fn;
  1989. fs_devices->num_devices++;
  1990. spin_lock_init(&device->io_lock);
  1991. memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
  1992. return device;
  1993. }
  1994. static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
  1995. struct extent_buffer *leaf,
  1996. struct btrfs_chunk *chunk)
  1997. {
  1998. struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
  1999. struct map_lookup *map;
  2000. struct extent_map *em;
  2001. u64 logical;
  2002. u64 length;
  2003. u64 devid;
  2004. u8 uuid[BTRFS_UUID_SIZE];
  2005. int num_stripes;
  2006. int ret;
  2007. int i;
  2008. logical = key->offset;
  2009. length = btrfs_chunk_length(leaf, chunk);
  2010. spin_lock(&map_tree->map_tree.lock);
  2011. em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
  2012. spin_unlock(&map_tree->map_tree.lock);
  2013. /* already mapped? */
  2014. if (em && em->start <= logical && em->start + em->len > logical) {
  2015. free_extent_map(em);
  2016. return 0;
  2017. } else if (em) {
  2018. free_extent_map(em);
  2019. }
  2020. map = kzalloc(sizeof(*map), GFP_NOFS);
  2021. if (!map)
  2022. return -ENOMEM;
  2023. em = alloc_extent_map(GFP_NOFS);
  2024. if (!em)
  2025. return -ENOMEM;
  2026. num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
  2027. map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
  2028. if (!map) {
  2029. free_extent_map(em);
  2030. return -ENOMEM;
  2031. }
  2032. em->bdev = (struct block_device *)map;
  2033. em->start = logical;
  2034. em->len = length;
  2035. em->block_start = 0;
  2036. map->num_stripes = num_stripes;
  2037. map->io_width = btrfs_chunk_io_width(leaf, chunk);
  2038. map->io_align = btrfs_chunk_io_align(leaf, chunk);
  2039. map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
  2040. map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
  2041. map->type = btrfs_chunk_type(leaf, chunk);
  2042. map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
  2043. for (i = 0; i < num_stripes; i++) {
  2044. map->stripes[i].physical =
  2045. btrfs_stripe_offset_nr(leaf, chunk, i);
  2046. devid = btrfs_stripe_devid_nr(leaf, chunk, i);
  2047. read_extent_buffer(leaf, uuid, (unsigned long)
  2048. btrfs_stripe_dev_uuid_nr(chunk, i),
  2049. BTRFS_UUID_SIZE);
  2050. map->stripes[i].dev = btrfs_find_device(root, devid, uuid);
  2051. if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
  2052. kfree(map);
  2053. free_extent_map(em);
  2054. return -EIO;
  2055. }
  2056. if (!map->stripes[i].dev) {
  2057. map->stripes[i].dev =
  2058. add_missing_dev(root, devid, uuid);
  2059. if (!map->stripes[i].dev) {
  2060. kfree(map);
  2061. free_extent_map(em);
  2062. return -EIO;
  2063. }
  2064. }
  2065. map->stripes[i].dev->in_fs_metadata = 1;
  2066. }
  2067. spin_lock(&map_tree->map_tree.lock);
  2068. ret = add_extent_mapping(&map_tree->map_tree, em);
  2069. spin_unlock(&map_tree->map_tree.lock);
  2070. BUG_ON(ret);
  2071. free_extent_map(em);
  2072. return 0;
  2073. }
  2074. static int fill_device_from_item(struct extent_buffer *leaf,
  2075. struct btrfs_dev_item *dev_item,
  2076. struct btrfs_device *device)
  2077. {
  2078. unsigned long ptr;
  2079. device->devid = btrfs_device_id(leaf, dev_item);
  2080. device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
  2081. device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
  2082. device->type = btrfs_device_type(leaf, dev_item);
  2083. device->io_align = btrfs_device_io_align(leaf, dev_item);
  2084. device->io_width = btrfs_device_io_width(leaf, dev_item);
  2085. device->sector_size = btrfs_device_sector_size(leaf, dev_item);
  2086. ptr = (unsigned long)btrfs_device_uuid(dev_item);
  2087. read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
  2088. return 0;
  2089. }
  2090. static int read_one_dev(struct btrfs_root *root,
  2091. struct extent_buffer *leaf,
  2092. struct btrfs_dev_item *dev_item)
  2093. {
  2094. struct btrfs_device *device;
  2095. u64 devid;
  2096. int ret;
  2097. u8 dev_uuid[BTRFS_UUID_SIZE];
  2098. devid = btrfs_device_id(leaf, dev_item);
  2099. read_extent_buffer(leaf, dev_uuid,
  2100. (unsigned long)btrfs_device_uuid(dev_item),
  2101. BTRFS_UUID_SIZE);
  2102. device = btrfs_find_device(root, devid, dev_uuid);
  2103. if (!device) {
  2104. printk("warning devid %Lu missing\n", devid);
  2105. device = add_missing_dev(root, devid, dev_uuid);
  2106. if (!device)
  2107. return -ENOMEM;
  2108. }
  2109. fill_device_from_item(leaf, dev_item, device);
  2110. device->dev_root = root->fs_info->dev_root;
  2111. device->in_fs_metadata = 1;
  2112. ret = 0;
  2113. #if 0
  2114. ret = btrfs_open_device(device);
  2115. if (ret) {
  2116. kfree(device);
  2117. }
  2118. #endif
  2119. return ret;
  2120. }
  2121. int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
  2122. {
  2123. struct btrfs_dev_item *dev_item;
  2124. dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
  2125. dev_item);
  2126. return read_one_dev(root, buf, dev_item);
  2127. }
  2128. int btrfs_read_sys_array(struct btrfs_root *root)
  2129. {
  2130. struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
  2131. struct extent_buffer *sb;
  2132. struct btrfs_disk_key *disk_key;
  2133. struct btrfs_chunk *chunk;
  2134. u8 *ptr;
  2135. unsigned long sb_ptr;
  2136. int ret = 0;
  2137. u32 num_stripes;
  2138. u32 array_size;
  2139. u32 len = 0;
  2140. u32 cur;
  2141. struct btrfs_key key;
  2142. sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
  2143. BTRFS_SUPER_INFO_SIZE);
  2144. if (!sb)
  2145. return -ENOMEM;
  2146. btrfs_set_buffer_uptodate(sb);
  2147. write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
  2148. array_size = btrfs_super_sys_array_size(super_copy);
  2149. ptr = super_copy->sys_chunk_array;
  2150. sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
  2151. cur = 0;
  2152. while (cur < array_size) {
  2153. disk_key = (struct btrfs_disk_key *)ptr;
  2154. btrfs_disk_key_to_cpu(&key, disk_key);
  2155. len = sizeof(*disk_key); ptr += len;
  2156. sb_ptr += len;
  2157. cur += len;
  2158. if (key.type == BTRFS_CHUNK_ITEM_KEY) {
  2159. chunk = (struct btrfs_chunk *)sb_ptr;
  2160. ret = read_one_chunk(root, &key, sb, chunk);
  2161. if (ret)
  2162. break;
  2163. num_stripes = btrfs_chunk_num_stripes(sb, chunk);
  2164. len = btrfs_chunk_item_size(num_stripes);
  2165. } else {
  2166. ret = -EIO;
  2167. break;
  2168. }
  2169. ptr += len;
  2170. sb_ptr += len;
  2171. cur += len;
  2172. }
  2173. free_extent_buffer(sb);
  2174. return ret;
  2175. }
  2176. int btrfs_read_chunk_tree(struct btrfs_root *root)
  2177. {
  2178. struct btrfs_path *path;
  2179. struct extent_buffer *leaf;
  2180. struct btrfs_key key;
  2181. struct btrfs_key found_key;
  2182. int ret;
  2183. int slot;
  2184. root = root->fs_info->chunk_root;
  2185. path = btrfs_alloc_path();
  2186. if (!path)
  2187. return -ENOMEM;
  2188. /* first we search for all of the device items, and then we
  2189. * read in all of the chunk items. This way we can create chunk
  2190. * mappings that reference all of the devices that are afound
  2191. */
  2192. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  2193. key.offset = 0;
  2194. key.type = 0;
  2195. again:
  2196. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2197. while(1) {
  2198. leaf = path->nodes[0];
  2199. slot = path->slots[0];
  2200. if (slot >= btrfs_header_nritems(leaf)) {
  2201. ret = btrfs_next_leaf(root, path);
  2202. if (ret == 0)
  2203. continue;
  2204. if (ret < 0)
  2205. goto error;
  2206. break;
  2207. }
  2208. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  2209. if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
  2210. if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
  2211. break;
  2212. if (found_key.type == BTRFS_DEV_ITEM_KEY) {
  2213. struct btrfs_dev_item *dev_item;
  2214. dev_item = btrfs_item_ptr(leaf, slot,
  2215. struct btrfs_dev_item);
  2216. ret = read_one_dev(root, leaf, dev_item);
  2217. BUG_ON(ret);
  2218. }
  2219. } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
  2220. struct btrfs_chunk *chunk;
  2221. chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
  2222. ret = read_one_chunk(root, &found_key, leaf, chunk);
  2223. }
  2224. path->slots[0]++;
  2225. }
  2226. if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
  2227. key.objectid = 0;
  2228. btrfs_release_path(root, path);
  2229. goto again;
  2230. }
  2231. btrfs_free_path(path);
  2232. ret = 0;
  2233. error:
  2234. return ret;
  2235. }