volumes.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320
  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 <asm/div64.h>
  22. #include "ctree.h"
  23. #include "extent_map.h"
  24. #include "disk-io.h"
  25. #include "transaction.h"
  26. #include "print-tree.h"
  27. #include "volumes.h"
  28. struct map_lookup {
  29. u64 type;
  30. int io_align;
  31. int io_width;
  32. int stripe_len;
  33. int sector_size;
  34. int num_stripes;
  35. struct btrfs_bio_stripe stripes[];
  36. };
  37. #define map_lookup_size(n) (sizeof(struct map_lookup) + \
  38. (sizeof(struct btrfs_bio_stripe) * (n)))
  39. static DEFINE_MUTEX(uuid_mutex);
  40. static LIST_HEAD(fs_uuids);
  41. int btrfs_cleanup_fs_uuids(void)
  42. {
  43. struct btrfs_fs_devices *fs_devices;
  44. struct list_head *uuid_cur;
  45. struct list_head *devices_cur;
  46. struct btrfs_device *dev;
  47. list_for_each(uuid_cur, &fs_uuids) {
  48. fs_devices = list_entry(uuid_cur, struct btrfs_fs_devices,
  49. list);
  50. while(!list_empty(&fs_devices->devices)) {
  51. devices_cur = fs_devices->devices.next;
  52. dev = list_entry(devices_cur, struct btrfs_device,
  53. dev_list);
  54. printk("uuid cleanup finds %s\n", dev->name);
  55. if (dev->bdev) {
  56. printk("closing\n");
  57. close_bdev_excl(dev->bdev);
  58. }
  59. list_del(&dev->dev_list);
  60. kfree(dev);
  61. }
  62. }
  63. return 0;
  64. }
  65. static struct btrfs_device *__find_device(struct list_head *head, u64 devid)
  66. {
  67. struct btrfs_device *dev;
  68. struct list_head *cur;
  69. list_for_each(cur, head) {
  70. dev = list_entry(cur, struct btrfs_device, dev_list);
  71. if (dev->devid == devid)
  72. return dev;
  73. }
  74. return NULL;
  75. }
  76. static struct btrfs_fs_devices *find_fsid(u8 *fsid)
  77. {
  78. struct list_head *cur;
  79. struct btrfs_fs_devices *fs_devices;
  80. list_for_each(cur, &fs_uuids) {
  81. fs_devices = list_entry(cur, struct btrfs_fs_devices, list);
  82. if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
  83. return fs_devices;
  84. }
  85. return NULL;
  86. }
  87. static int device_list_add(const char *path,
  88. struct btrfs_super_block *disk_super,
  89. u64 devid, struct btrfs_fs_devices **fs_devices_ret)
  90. {
  91. struct btrfs_device *device;
  92. struct btrfs_fs_devices *fs_devices;
  93. u64 found_transid = btrfs_super_generation(disk_super);
  94. fs_devices = find_fsid(disk_super->fsid);
  95. if (!fs_devices) {
  96. fs_devices = kmalloc(sizeof(*fs_devices), GFP_NOFS);
  97. if (!fs_devices)
  98. return -ENOMEM;
  99. INIT_LIST_HEAD(&fs_devices->devices);
  100. list_add(&fs_devices->list, &fs_uuids);
  101. memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
  102. fs_devices->latest_devid = devid;
  103. fs_devices->latest_trans = found_transid;
  104. fs_devices->lowest_devid = (u64)-1;
  105. fs_devices->num_devices = 0;
  106. device = NULL;
  107. } else {
  108. device = __find_device(&fs_devices->devices, devid);
  109. }
  110. if (!device) {
  111. device = kzalloc(sizeof(*device), GFP_NOFS);
  112. if (!device) {
  113. /* we can safely leave the fs_devices entry around */
  114. return -ENOMEM;
  115. }
  116. device->devid = devid;
  117. device->barriers = 1;
  118. spin_lock_init(&device->io_lock);
  119. device->name = kstrdup(path, GFP_NOFS);
  120. if (!device->name) {
  121. kfree(device);
  122. return -ENOMEM;
  123. }
  124. list_add(&device->dev_list, &fs_devices->devices);
  125. fs_devices->num_devices++;
  126. }
  127. if (found_transid > fs_devices->latest_trans) {
  128. fs_devices->latest_devid = devid;
  129. fs_devices->latest_trans = found_transid;
  130. }
  131. if (fs_devices->lowest_devid > devid) {
  132. fs_devices->lowest_devid = devid;
  133. printk("lowest devid now %Lu\n", devid);
  134. }
  135. *fs_devices_ret = fs_devices;
  136. return 0;
  137. }
  138. int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
  139. {
  140. struct list_head *head = &fs_devices->devices;
  141. struct list_head *cur;
  142. struct btrfs_device *device;
  143. mutex_lock(&uuid_mutex);
  144. list_for_each(cur, head) {
  145. device = list_entry(cur, struct btrfs_device, dev_list);
  146. if (device->bdev) {
  147. close_bdev_excl(device->bdev);
  148. printk("close devices closes %s\n", device->name);
  149. }
  150. device->bdev = NULL;
  151. }
  152. mutex_unlock(&uuid_mutex);
  153. return 0;
  154. }
  155. int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
  156. int flags, void *holder)
  157. {
  158. struct block_device *bdev;
  159. struct list_head *head = &fs_devices->devices;
  160. struct list_head *cur;
  161. struct btrfs_device *device;
  162. int ret;
  163. mutex_lock(&uuid_mutex);
  164. list_for_each(cur, head) {
  165. device = list_entry(cur, struct btrfs_device, dev_list);
  166. bdev = open_bdev_excl(device->name, flags, holder);
  167. if (IS_ERR(bdev)) {
  168. printk("open %s failed\n", device->name);
  169. ret = PTR_ERR(bdev);
  170. goto fail;
  171. }
  172. if (device->devid == fs_devices->latest_devid)
  173. fs_devices->latest_bdev = bdev;
  174. if (device->devid == fs_devices->lowest_devid) {
  175. fs_devices->lowest_bdev = bdev;
  176. }
  177. device->bdev = bdev;
  178. }
  179. mutex_unlock(&uuid_mutex);
  180. return 0;
  181. fail:
  182. mutex_unlock(&uuid_mutex);
  183. btrfs_close_devices(fs_devices);
  184. return ret;
  185. }
  186. int btrfs_scan_one_device(const char *path, int flags, void *holder,
  187. struct btrfs_fs_devices **fs_devices_ret)
  188. {
  189. struct btrfs_super_block *disk_super;
  190. struct block_device *bdev;
  191. struct buffer_head *bh;
  192. int ret;
  193. u64 devid;
  194. u64 transid;
  195. mutex_lock(&uuid_mutex);
  196. printk("scan one opens %s\n", path);
  197. bdev = open_bdev_excl(path, flags, holder);
  198. if (IS_ERR(bdev)) {
  199. printk("open failed\n");
  200. ret = PTR_ERR(bdev);
  201. goto error;
  202. }
  203. ret = set_blocksize(bdev, 4096);
  204. if (ret)
  205. goto error_close;
  206. bh = __bread(bdev, BTRFS_SUPER_INFO_OFFSET / 4096, 4096);
  207. if (!bh) {
  208. ret = -EIO;
  209. goto error_close;
  210. }
  211. disk_super = (struct btrfs_super_block *)bh->b_data;
  212. if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
  213. sizeof(disk_super->magic))) {
  214. printk("no btrfs found on %s\n", path);
  215. ret = -EINVAL;
  216. goto error_brelse;
  217. }
  218. devid = le64_to_cpu(disk_super->dev_item.devid);
  219. transid = btrfs_super_generation(disk_super);
  220. printk("found device %Lu transid %Lu on %s\n", devid, transid, path);
  221. ret = device_list_add(path, disk_super, devid, fs_devices_ret);
  222. error_brelse:
  223. brelse(bh);
  224. error_close:
  225. close_bdev_excl(bdev);
  226. error:
  227. mutex_unlock(&uuid_mutex);
  228. return ret;
  229. }
  230. /*
  231. * this uses a pretty simple search, the expectation is that it is
  232. * called very infrequently and that a given device has a small number
  233. * of extents
  234. */
  235. static int find_free_dev_extent(struct btrfs_trans_handle *trans,
  236. struct btrfs_device *device,
  237. struct btrfs_path *path,
  238. u64 num_bytes, u64 *start)
  239. {
  240. struct btrfs_key key;
  241. struct btrfs_root *root = device->dev_root;
  242. struct btrfs_dev_extent *dev_extent = NULL;
  243. u64 hole_size = 0;
  244. u64 last_byte = 0;
  245. u64 search_start = 0;
  246. u64 search_end = device->total_bytes;
  247. int ret;
  248. int slot = 0;
  249. int start_found;
  250. struct extent_buffer *l;
  251. start_found = 0;
  252. path->reada = 2;
  253. /* FIXME use last free of some kind */
  254. /* we don't want to overwrite the superblock on the drive,
  255. * so we make sure to start at an offset of at least 1MB
  256. */
  257. search_start = max((u64)1024 * 1024, search_start);
  258. key.objectid = device->devid;
  259. key.offset = search_start;
  260. key.type = BTRFS_DEV_EXTENT_KEY;
  261. ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
  262. if (ret < 0)
  263. goto error;
  264. ret = btrfs_previous_item(root, path, 0, key.type);
  265. if (ret < 0)
  266. goto error;
  267. l = path->nodes[0];
  268. btrfs_item_key_to_cpu(l, &key, path->slots[0]);
  269. while (1) {
  270. l = path->nodes[0];
  271. slot = path->slots[0];
  272. if (slot >= btrfs_header_nritems(l)) {
  273. ret = btrfs_next_leaf(root, path);
  274. if (ret == 0)
  275. continue;
  276. if (ret < 0)
  277. goto error;
  278. no_more_items:
  279. if (!start_found) {
  280. if (search_start >= search_end) {
  281. ret = -ENOSPC;
  282. goto error;
  283. }
  284. *start = search_start;
  285. start_found = 1;
  286. goto check_pending;
  287. }
  288. *start = last_byte > search_start ?
  289. last_byte : search_start;
  290. if (search_end <= *start) {
  291. ret = -ENOSPC;
  292. goto error;
  293. }
  294. goto check_pending;
  295. }
  296. btrfs_item_key_to_cpu(l, &key, slot);
  297. if (key.objectid < device->devid)
  298. goto next;
  299. if (key.objectid > device->devid)
  300. goto no_more_items;
  301. if (key.offset >= search_start && key.offset > last_byte &&
  302. start_found) {
  303. if (last_byte < search_start)
  304. last_byte = search_start;
  305. hole_size = key.offset - last_byte;
  306. if (key.offset > last_byte &&
  307. hole_size >= num_bytes) {
  308. *start = last_byte;
  309. goto check_pending;
  310. }
  311. }
  312. if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) {
  313. goto next;
  314. }
  315. start_found = 1;
  316. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  317. last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent);
  318. next:
  319. path->slots[0]++;
  320. cond_resched();
  321. }
  322. check_pending:
  323. /* we have to make sure we didn't find an extent that has already
  324. * been allocated by the map tree or the original allocation
  325. */
  326. btrfs_release_path(root, path);
  327. BUG_ON(*start < search_start);
  328. if (*start + num_bytes > search_end) {
  329. ret = -ENOSPC;
  330. goto error;
  331. }
  332. /* check for pending inserts here */
  333. return 0;
  334. error:
  335. btrfs_release_path(root, path);
  336. return ret;
  337. }
  338. int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
  339. struct btrfs_device *device,
  340. u64 chunk_tree, u64 chunk_objectid,
  341. u64 chunk_offset,
  342. u64 num_bytes, u64 *start)
  343. {
  344. int ret;
  345. struct btrfs_path *path;
  346. struct btrfs_root *root = device->dev_root;
  347. struct btrfs_dev_extent *extent;
  348. struct extent_buffer *leaf;
  349. struct btrfs_key key;
  350. path = btrfs_alloc_path();
  351. if (!path)
  352. return -ENOMEM;
  353. ret = find_free_dev_extent(trans, device, path, num_bytes, start);
  354. if (ret) {
  355. goto err;
  356. }
  357. key.objectid = device->devid;
  358. key.offset = *start;
  359. key.type = BTRFS_DEV_EXTENT_KEY;
  360. ret = btrfs_insert_empty_item(trans, root, path, &key,
  361. sizeof(*extent));
  362. BUG_ON(ret);
  363. leaf = path->nodes[0];
  364. extent = btrfs_item_ptr(leaf, path->slots[0],
  365. struct btrfs_dev_extent);
  366. btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
  367. btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
  368. btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
  369. write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
  370. (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
  371. BTRFS_UUID_SIZE);
  372. btrfs_set_dev_extent_length(leaf, extent, num_bytes);
  373. btrfs_mark_buffer_dirty(leaf);
  374. err:
  375. btrfs_free_path(path);
  376. return ret;
  377. }
  378. static int find_next_chunk(struct btrfs_root *root, u64 objectid, u64 *offset)
  379. {
  380. struct btrfs_path *path;
  381. int ret;
  382. struct btrfs_key key;
  383. struct btrfs_chunk *chunk;
  384. struct btrfs_key found_key;
  385. path = btrfs_alloc_path();
  386. BUG_ON(!path);
  387. key.objectid = objectid;
  388. key.offset = (u64)-1;
  389. key.type = BTRFS_CHUNK_ITEM_KEY;
  390. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  391. if (ret < 0)
  392. goto error;
  393. BUG_ON(ret == 0);
  394. ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
  395. if (ret) {
  396. *offset = 0;
  397. } else {
  398. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  399. path->slots[0]);
  400. if (found_key.objectid != objectid)
  401. *offset = 0;
  402. else {
  403. chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
  404. struct btrfs_chunk);
  405. *offset = found_key.offset +
  406. btrfs_chunk_length(path->nodes[0], chunk);
  407. }
  408. }
  409. ret = 0;
  410. error:
  411. btrfs_free_path(path);
  412. return ret;
  413. }
  414. static int find_next_devid(struct btrfs_root *root, struct btrfs_path *path,
  415. u64 *objectid)
  416. {
  417. int ret;
  418. struct btrfs_key key;
  419. struct btrfs_key found_key;
  420. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  421. key.type = BTRFS_DEV_ITEM_KEY;
  422. key.offset = (u64)-1;
  423. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  424. if (ret < 0)
  425. goto error;
  426. BUG_ON(ret == 0);
  427. ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
  428. BTRFS_DEV_ITEM_KEY);
  429. if (ret) {
  430. *objectid = 1;
  431. } else {
  432. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  433. path->slots[0]);
  434. *objectid = found_key.offset + 1;
  435. }
  436. ret = 0;
  437. error:
  438. btrfs_release_path(root, path);
  439. return ret;
  440. }
  441. /*
  442. * the device information is stored in the chunk root
  443. * the btrfs_device struct should be fully filled in
  444. */
  445. int btrfs_add_device(struct btrfs_trans_handle *trans,
  446. struct btrfs_root *root,
  447. struct btrfs_device *device)
  448. {
  449. int ret;
  450. struct btrfs_path *path;
  451. struct btrfs_dev_item *dev_item;
  452. struct extent_buffer *leaf;
  453. struct btrfs_key key;
  454. unsigned long ptr;
  455. u64 free_devid;
  456. root = root->fs_info->chunk_root;
  457. path = btrfs_alloc_path();
  458. if (!path)
  459. return -ENOMEM;
  460. ret = find_next_devid(root, path, &free_devid);
  461. if (ret)
  462. goto out;
  463. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  464. key.type = BTRFS_DEV_ITEM_KEY;
  465. key.offset = free_devid;
  466. ret = btrfs_insert_empty_item(trans, root, path, &key,
  467. sizeof(*dev_item));
  468. if (ret)
  469. goto out;
  470. leaf = path->nodes[0];
  471. dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
  472. device->devid = free_devid;
  473. btrfs_set_device_id(leaf, dev_item, device->devid);
  474. btrfs_set_device_type(leaf, dev_item, device->type);
  475. btrfs_set_device_io_align(leaf, dev_item, device->io_align);
  476. btrfs_set_device_io_width(leaf, dev_item, device->io_width);
  477. btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
  478. btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
  479. btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
  480. btrfs_set_device_group(leaf, dev_item, 0);
  481. btrfs_set_device_seek_speed(leaf, dev_item, 0);
  482. btrfs_set_device_bandwidth(leaf, dev_item, 0);
  483. ptr = (unsigned long)btrfs_device_uuid(dev_item);
  484. write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
  485. btrfs_mark_buffer_dirty(leaf);
  486. ret = 0;
  487. out:
  488. btrfs_free_path(path);
  489. return ret;
  490. }
  491. int btrfs_update_device(struct btrfs_trans_handle *trans,
  492. struct btrfs_device *device)
  493. {
  494. int ret;
  495. struct btrfs_path *path;
  496. struct btrfs_root *root;
  497. struct btrfs_dev_item *dev_item;
  498. struct extent_buffer *leaf;
  499. struct btrfs_key key;
  500. root = device->dev_root->fs_info->chunk_root;
  501. path = btrfs_alloc_path();
  502. if (!path)
  503. return -ENOMEM;
  504. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  505. key.type = BTRFS_DEV_ITEM_KEY;
  506. key.offset = device->devid;
  507. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  508. if (ret < 0)
  509. goto out;
  510. if (ret > 0) {
  511. ret = -ENOENT;
  512. goto out;
  513. }
  514. leaf = path->nodes[0];
  515. dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
  516. btrfs_set_device_id(leaf, dev_item, device->devid);
  517. btrfs_set_device_type(leaf, dev_item, device->type);
  518. btrfs_set_device_io_align(leaf, dev_item, device->io_align);
  519. btrfs_set_device_io_width(leaf, dev_item, device->io_width);
  520. btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
  521. btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
  522. btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
  523. btrfs_mark_buffer_dirty(leaf);
  524. out:
  525. btrfs_free_path(path);
  526. return ret;
  527. }
  528. int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
  529. struct btrfs_root *root,
  530. struct btrfs_key *key,
  531. struct btrfs_chunk *chunk, int item_size)
  532. {
  533. struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
  534. struct btrfs_disk_key disk_key;
  535. u32 array_size;
  536. u8 *ptr;
  537. array_size = btrfs_super_sys_array_size(super_copy);
  538. if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
  539. return -EFBIG;
  540. ptr = super_copy->sys_chunk_array + array_size;
  541. btrfs_cpu_key_to_disk(&disk_key, key);
  542. memcpy(ptr, &disk_key, sizeof(disk_key));
  543. ptr += sizeof(disk_key);
  544. memcpy(ptr, chunk, item_size);
  545. item_size += sizeof(disk_key);
  546. btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
  547. return 0;
  548. }
  549. int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
  550. struct btrfs_root *extent_root, u64 *start,
  551. u64 *num_bytes, u64 type)
  552. {
  553. u64 dev_offset;
  554. struct btrfs_fs_info *info = extent_root->fs_info;
  555. struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
  556. struct btrfs_stripe *stripes;
  557. struct btrfs_device *device = NULL;
  558. struct btrfs_chunk *chunk;
  559. struct list_head private_devs;
  560. struct list_head *dev_list = &extent_root->fs_info->fs_devices->devices;
  561. struct list_head *cur;
  562. struct extent_map_tree *em_tree;
  563. struct map_lookup *map;
  564. struct extent_map *em;
  565. u64 physical;
  566. u64 calc_size = 1024 * 1024 * 1024;
  567. u64 min_free = calc_size;
  568. u64 avail;
  569. u64 max_avail = 0;
  570. int num_stripes = 1;
  571. int looped = 0;
  572. int ret;
  573. int index;
  574. int stripe_len = 64 * 1024;
  575. struct btrfs_key key;
  576. if (list_empty(dev_list))
  577. return -ENOSPC;
  578. if (type & (BTRFS_BLOCK_GROUP_RAID0))
  579. num_stripes = btrfs_super_num_devices(&info->super_copy);
  580. if (type & (BTRFS_BLOCK_GROUP_DUP))
  581. num_stripes = 2;
  582. if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
  583. num_stripes = min_t(u64, 2,
  584. btrfs_super_num_devices(&info->super_copy));
  585. }
  586. again:
  587. INIT_LIST_HEAD(&private_devs);
  588. cur = dev_list->next;
  589. index = 0;
  590. if (type & BTRFS_BLOCK_GROUP_DUP)
  591. min_free = calc_size * 2;
  592. /* build a private list of devices we will allocate from */
  593. while(index < num_stripes) {
  594. device = list_entry(cur, struct btrfs_device, dev_list);
  595. avail = device->total_bytes - device->bytes_used;
  596. cur = cur->next;
  597. if (avail > max_avail)
  598. max_avail = avail;
  599. if (avail >= min_free) {
  600. list_move_tail(&device->dev_list, &private_devs);
  601. index++;
  602. if (type & BTRFS_BLOCK_GROUP_DUP)
  603. index++;
  604. }
  605. if (cur == dev_list)
  606. break;
  607. }
  608. if (index < num_stripes) {
  609. list_splice(&private_devs, dev_list);
  610. if (!looped && max_avail > 0) {
  611. looped = 1;
  612. calc_size = max_avail;
  613. goto again;
  614. }
  615. return -ENOSPC;
  616. }
  617. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  618. key.type = BTRFS_CHUNK_ITEM_KEY;
  619. ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
  620. &key.offset);
  621. if (ret)
  622. return ret;
  623. chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS);
  624. if (!chunk)
  625. return -ENOMEM;
  626. map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
  627. if (!map) {
  628. kfree(chunk);
  629. return -ENOMEM;
  630. }
  631. stripes = &chunk->stripe;
  632. if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
  633. *num_bytes = calc_size;
  634. else
  635. *num_bytes = calc_size * num_stripes;
  636. index = 0;
  637. printk("new chunk type %Lu start %Lu size %Lu\n", type, key.offset, *num_bytes);
  638. while(index < num_stripes) {
  639. struct btrfs_stripe *stripe;
  640. BUG_ON(list_empty(&private_devs));
  641. cur = private_devs.next;
  642. device = list_entry(cur, struct btrfs_device, dev_list);
  643. /* loop over this device again if we're doing a dup group */
  644. if (!(type & BTRFS_BLOCK_GROUP_DUP) ||
  645. (index == num_stripes - 1))
  646. list_move_tail(&device->dev_list, dev_list);
  647. ret = btrfs_alloc_dev_extent(trans, device,
  648. info->chunk_root->root_key.objectid,
  649. BTRFS_FIRST_CHUNK_TREE_OBJECTID, key.offset,
  650. calc_size, &dev_offset);
  651. BUG_ON(ret);
  652. printk("alloc chunk start %Lu size %Lu from dev %Lu type %Lu\n", key.offset, calc_size, device->devid, type);
  653. device->bytes_used += calc_size;
  654. ret = btrfs_update_device(trans, device);
  655. BUG_ON(ret);
  656. map->stripes[index].dev = device;
  657. map->stripes[index].physical = dev_offset;
  658. stripe = stripes + index;
  659. btrfs_set_stack_stripe_devid(stripe, device->devid);
  660. btrfs_set_stack_stripe_offset(stripe, dev_offset);
  661. memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
  662. physical = dev_offset;
  663. index++;
  664. }
  665. BUG_ON(!list_empty(&private_devs));
  666. /* key was set above */
  667. btrfs_set_stack_chunk_length(chunk, *num_bytes);
  668. btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
  669. btrfs_set_stack_chunk_stripe_len(chunk, stripe_len);
  670. btrfs_set_stack_chunk_type(chunk, type);
  671. btrfs_set_stack_chunk_num_stripes(chunk, num_stripes);
  672. btrfs_set_stack_chunk_io_align(chunk, stripe_len);
  673. btrfs_set_stack_chunk_io_width(chunk, stripe_len);
  674. btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
  675. map->sector_size = extent_root->sectorsize;
  676. map->stripe_len = stripe_len;
  677. map->io_align = stripe_len;
  678. map->io_width = stripe_len;
  679. map->type = type;
  680. map->num_stripes = num_stripes;
  681. ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
  682. btrfs_chunk_item_size(num_stripes));
  683. BUG_ON(ret);
  684. *start = key.offset;;
  685. em = alloc_extent_map(GFP_NOFS);
  686. if (!em)
  687. return -ENOMEM;
  688. em->bdev = (struct block_device *)map;
  689. em->start = key.offset;
  690. em->len = *num_bytes;
  691. em->block_start = 0;
  692. kfree(chunk);
  693. em_tree = &extent_root->fs_info->mapping_tree.map_tree;
  694. spin_lock(&em_tree->lock);
  695. ret = add_extent_mapping(em_tree, em);
  696. spin_unlock(&em_tree->lock);
  697. BUG_ON(ret);
  698. free_extent_map(em);
  699. return ret;
  700. }
  701. void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
  702. {
  703. extent_map_tree_init(&tree->map_tree, GFP_NOFS);
  704. }
  705. void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
  706. {
  707. struct extent_map *em;
  708. while(1) {
  709. spin_lock(&tree->map_tree.lock);
  710. em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
  711. if (em)
  712. remove_extent_mapping(&tree->map_tree, em);
  713. spin_unlock(&tree->map_tree.lock);
  714. if (!em)
  715. break;
  716. kfree(em->bdev);
  717. /* once for us */
  718. free_extent_map(em);
  719. /* once for the tree */
  720. free_extent_map(em);
  721. }
  722. }
  723. int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
  724. {
  725. struct extent_map *em;
  726. struct map_lookup *map;
  727. struct extent_map_tree *em_tree = &map_tree->map_tree;
  728. int ret;
  729. spin_lock(&em_tree->lock);
  730. em = lookup_extent_mapping(em_tree, logical, len);
  731. spin_unlock(&em_tree->lock);
  732. BUG_ON(!em);
  733. BUG_ON(em->start > logical || em->start + em->len < logical);
  734. map = (struct map_lookup *)em->bdev;
  735. if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
  736. ret = map->num_stripes;
  737. else
  738. ret = 1;
  739. free_extent_map(em);
  740. return ret;
  741. }
  742. int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
  743. u64 logical, u64 *length,
  744. struct btrfs_multi_bio **multi_ret, int mirror_num)
  745. {
  746. struct extent_map *em;
  747. struct map_lookup *map;
  748. struct extent_map_tree *em_tree = &map_tree->map_tree;
  749. u64 offset;
  750. u64 stripe_offset;
  751. u64 stripe_nr;
  752. int stripes_allocated = 8;
  753. int stripe_index;
  754. int i;
  755. struct btrfs_multi_bio *multi = NULL;
  756. if (multi_ret && !(rw & (1 << BIO_RW))) {
  757. stripes_allocated = 1;
  758. }
  759. again:
  760. if (multi_ret) {
  761. multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
  762. GFP_NOFS);
  763. if (!multi)
  764. return -ENOMEM;
  765. }
  766. spin_lock(&em_tree->lock);
  767. em = lookup_extent_mapping(em_tree, logical, *length);
  768. spin_unlock(&em_tree->lock);
  769. BUG_ON(!em);
  770. BUG_ON(em->start > logical || em->start + em->len < logical);
  771. map = (struct map_lookup *)em->bdev;
  772. offset = logical - em->start;
  773. if (mirror_num > map->num_stripes)
  774. mirror_num = 0;
  775. /* if our multi bio struct is too small, back off and try again */
  776. if (multi_ret && (rw & (1 << BIO_RW)) &&
  777. stripes_allocated < map->num_stripes &&
  778. ((map->type & BTRFS_BLOCK_GROUP_RAID1) ||
  779. (map->type & BTRFS_BLOCK_GROUP_DUP))) {
  780. stripes_allocated = map->num_stripes;
  781. free_extent_map(em);
  782. kfree(multi);
  783. goto again;
  784. }
  785. stripe_nr = offset;
  786. /*
  787. * stripe_nr counts the total number of stripes we have to stride
  788. * to get to this block
  789. */
  790. do_div(stripe_nr, map->stripe_len);
  791. stripe_offset = stripe_nr * map->stripe_len;
  792. BUG_ON(offset < stripe_offset);
  793. /* stripe_offset is the offset of this block in its stripe*/
  794. stripe_offset = offset - stripe_offset;
  795. if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
  796. BTRFS_BLOCK_GROUP_DUP)) {
  797. /* we limit the length of each bio to what fits in a stripe */
  798. *length = min_t(u64, em->len - offset,
  799. map->stripe_len - stripe_offset);
  800. } else {
  801. *length = em->len - offset;
  802. }
  803. if (!multi_ret)
  804. goto out;
  805. multi->num_stripes = 1;
  806. stripe_index = 0;
  807. if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
  808. if (rw & (1 << BIO_RW))
  809. multi->num_stripes = map->num_stripes;
  810. else if (mirror_num) {
  811. stripe_index = mirror_num - 1;
  812. } else {
  813. int i;
  814. u64 least = (u64)-1;
  815. struct btrfs_device *cur;
  816. for (i = 0; i < map->num_stripes; i++) {
  817. cur = map->stripes[i].dev;
  818. spin_lock(&cur->io_lock);
  819. if (cur->total_ios < least) {
  820. least = cur->total_ios;
  821. stripe_index = i;
  822. }
  823. spin_unlock(&cur->io_lock);
  824. }
  825. }
  826. } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
  827. if (rw & (1 << BIO_RW))
  828. multi->num_stripes = map->num_stripes;
  829. else if (mirror_num)
  830. stripe_index = mirror_num - 1;
  831. } else {
  832. /*
  833. * after this do_div call, stripe_nr is the number of stripes
  834. * on this device we have to walk to find the data, and
  835. * stripe_index is the number of our device in the stripe array
  836. */
  837. stripe_index = do_div(stripe_nr, map->num_stripes);
  838. }
  839. BUG_ON(stripe_index >= map->num_stripes);
  840. BUG_ON(stripe_index != 0 && multi->num_stripes > 1);
  841. for (i = 0; i < multi->num_stripes; i++) {
  842. multi->stripes[i].physical =
  843. map->stripes[stripe_index].physical + stripe_offset +
  844. stripe_nr * map->stripe_len;
  845. multi->stripes[i].dev = map->stripes[stripe_index].dev;
  846. stripe_index++;
  847. }
  848. *multi_ret = multi;
  849. out:
  850. free_extent_map(em);
  851. return 0;
  852. }
  853. #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
  854. static void end_bio_multi_stripe(struct bio *bio, int err)
  855. #else
  856. static int end_bio_multi_stripe(struct bio *bio,
  857. unsigned int bytes_done, int err)
  858. #endif
  859. {
  860. struct btrfs_multi_bio *multi = bio->bi_private;
  861. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
  862. if (bio->bi_size)
  863. return 1;
  864. #endif
  865. if (err)
  866. multi->error = err;
  867. if (atomic_dec_and_test(&multi->stripes_pending)) {
  868. bio->bi_private = multi->private;
  869. bio->bi_end_io = multi->end_io;
  870. if (!err && multi->error)
  871. err = multi->error;
  872. kfree(multi);
  873. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
  874. bio_endio(bio, bio->bi_size, err);
  875. #else
  876. bio_endio(bio, err);
  877. #endif
  878. } else {
  879. bio_put(bio);
  880. }
  881. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
  882. return 0;
  883. #endif
  884. }
  885. int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
  886. int mirror_num)
  887. {
  888. struct btrfs_mapping_tree *map_tree;
  889. struct btrfs_device *dev;
  890. struct bio *first_bio = bio;
  891. u64 logical = bio->bi_sector << 9;
  892. u64 length = 0;
  893. u64 map_length;
  894. struct bio_vec *bvec;
  895. struct btrfs_multi_bio *multi = NULL;
  896. int i;
  897. int ret;
  898. int dev_nr = 0;
  899. int total_devs = 1;
  900. bio_for_each_segment(bvec, bio, i) {
  901. length += bvec->bv_len;
  902. }
  903. map_tree = &root->fs_info->mapping_tree;
  904. map_length = length;
  905. ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
  906. mirror_num);
  907. BUG_ON(ret);
  908. total_devs = multi->num_stripes;
  909. if (map_length < length) {
  910. printk("mapping failed logical %Lu bio len %Lu "
  911. "len %Lu\n", logical, length, map_length);
  912. BUG();
  913. }
  914. multi->end_io = first_bio->bi_end_io;
  915. multi->private = first_bio->bi_private;
  916. atomic_set(&multi->stripes_pending, multi->num_stripes);
  917. while(dev_nr < total_devs) {
  918. if (total_devs > 1) {
  919. if (dev_nr < total_devs - 1) {
  920. bio = bio_clone(first_bio, GFP_NOFS);
  921. BUG_ON(!bio);
  922. } else {
  923. bio = first_bio;
  924. }
  925. bio->bi_private = multi;
  926. bio->bi_end_io = end_bio_multi_stripe;
  927. }
  928. bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
  929. dev = multi->stripes[dev_nr].dev;
  930. bio->bi_bdev = dev->bdev;
  931. spin_lock(&dev->io_lock);
  932. dev->total_ios++;
  933. spin_unlock(&dev->io_lock);
  934. submit_bio(rw, bio);
  935. dev_nr++;
  936. }
  937. if (total_devs == 1)
  938. kfree(multi);
  939. return 0;
  940. }
  941. struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid)
  942. {
  943. struct list_head *head = &root->fs_info->fs_devices->devices;
  944. return __find_device(head, devid);
  945. }
  946. static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
  947. struct extent_buffer *leaf,
  948. struct btrfs_chunk *chunk)
  949. {
  950. struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
  951. struct map_lookup *map;
  952. struct extent_map *em;
  953. u64 logical;
  954. u64 length;
  955. u64 devid;
  956. int num_stripes;
  957. int ret;
  958. int i;
  959. logical = key->offset;
  960. length = btrfs_chunk_length(leaf, chunk);
  961. spin_lock(&map_tree->map_tree.lock);
  962. em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
  963. spin_unlock(&map_tree->map_tree.lock);
  964. /* already mapped? */
  965. if (em && em->start <= logical && em->start + em->len > logical) {
  966. free_extent_map(em);
  967. return 0;
  968. } else if (em) {
  969. free_extent_map(em);
  970. }
  971. map = kzalloc(sizeof(*map), GFP_NOFS);
  972. if (!map)
  973. return -ENOMEM;
  974. em = alloc_extent_map(GFP_NOFS);
  975. if (!em)
  976. return -ENOMEM;
  977. num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
  978. map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
  979. if (!map) {
  980. free_extent_map(em);
  981. return -ENOMEM;
  982. }
  983. em->bdev = (struct block_device *)map;
  984. em->start = logical;
  985. em->len = length;
  986. em->block_start = 0;
  987. map->num_stripes = num_stripes;
  988. map->io_width = btrfs_chunk_io_width(leaf, chunk);
  989. map->io_align = btrfs_chunk_io_align(leaf, chunk);
  990. map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
  991. map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
  992. map->type = btrfs_chunk_type(leaf, chunk);
  993. for (i = 0; i < num_stripes; i++) {
  994. map->stripes[i].physical =
  995. btrfs_stripe_offset_nr(leaf, chunk, i);
  996. devid = btrfs_stripe_devid_nr(leaf, chunk, i);
  997. map->stripes[i].dev = btrfs_find_device(root, devid);
  998. if (!map->stripes[i].dev) {
  999. kfree(map);
  1000. free_extent_map(em);
  1001. return -EIO;
  1002. }
  1003. }
  1004. spin_lock(&map_tree->map_tree.lock);
  1005. ret = add_extent_mapping(&map_tree->map_tree, em);
  1006. spin_unlock(&map_tree->map_tree.lock);
  1007. BUG_ON(ret);
  1008. free_extent_map(em);
  1009. return 0;
  1010. }
  1011. static int fill_device_from_item(struct extent_buffer *leaf,
  1012. struct btrfs_dev_item *dev_item,
  1013. struct btrfs_device *device)
  1014. {
  1015. unsigned long ptr;
  1016. device->devid = btrfs_device_id(leaf, dev_item);
  1017. device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
  1018. device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
  1019. device->type = btrfs_device_type(leaf, dev_item);
  1020. device->io_align = btrfs_device_io_align(leaf, dev_item);
  1021. device->io_width = btrfs_device_io_width(leaf, dev_item);
  1022. device->sector_size = btrfs_device_sector_size(leaf, dev_item);
  1023. ptr = (unsigned long)btrfs_device_uuid(dev_item);
  1024. read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
  1025. return 0;
  1026. }
  1027. static int read_one_dev(struct btrfs_root *root,
  1028. struct extent_buffer *leaf,
  1029. struct btrfs_dev_item *dev_item)
  1030. {
  1031. struct btrfs_device *device;
  1032. u64 devid;
  1033. int ret;
  1034. devid = btrfs_device_id(leaf, dev_item);
  1035. device = btrfs_find_device(root, devid);
  1036. if (!device) {
  1037. printk("warning devid %Lu not found already\n", devid);
  1038. device = kzalloc(sizeof(*device), GFP_NOFS);
  1039. if (!device)
  1040. return -ENOMEM;
  1041. list_add(&device->dev_list,
  1042. &root->fs_info->fs_devices->devices);
  1043. device->barriers = 1;
  1044. spin_lock_init(&device->io_lock);
  1045. }
  1046. fill_device_from_item(leaf, dev_item, device);
  1047. device->dev_root = root->fs_info->dev_root;
  1048. ret = 0;
  1049. #if 0
  1050. ret = btrfs_open_device(device);
  1051. if (ret) {
  1052. kfree(device);
  1053. }
  1054. #endif
  1055. return ret;
  1056. }
  1057. int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
  1058. {
  1059. struct btrfs_dev_item *dev_item;
  1060. dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
  1061. dev_item);
  1062. return read_one_dev(root, buf, dev_item);
  1063. }
  1064. int btrfs_read_sys_array(struct btrfs_root *root)
  1065. {
  1066. struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
  1067. struct extent_buffer *sb = root->fs_info->sb_buffer;
  1068. struct btrfs_disk_key *disk_key;
  1069. struct btrfs_chunk *chunk;
  1070. struct btrfs_key key;
  1071. u32 num_stripes;
  1072. u32 array_size;
  1073. u32 len = 0;
  1074. u8 *ptr;
  1075. unsigned long sb_ptr;
  1076. u32 cur;
  1077. int ret;
  1078. array_size = btrfs_super_sys_array_size(super_copy);
  1079. /*
  1080. * we do this loop twice, once for the device items and
  1081. * once for all of the chunks. This way there are device
  1082. * structs filled in for every chunk
  1083. */
  1084. ptr = super_copy->sys_chunk_array;
  1085. sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
  1086. cur = 0;
  1087. while (cur < array_size) {
  1088. disk_key = (struct btrfs_disk_key *)ptr;
  1089. btrfs_disk_key_to_cpu(&key, disk_key);
  1090. len = sizeof(*disk_key);
  1091. ptr += len;
  1092. sb_ptr += len;
  1093. cur += len;
  1094. if (key.type == BTRFS_CHUNK_ITEM_KEY) {
  1095. chunk = (struct btrfs_chunk *)sb_ptr;
  1096. ret = read_one_chunk(root, &key, sb, chunk);
  1097. BUG_ON(ret);
  1098. num_stripes = btrfs_chunk_num_stripes(sb, chunk);
  1099. len = btrfs_chunk_item_size(num_stripes);
  1100. } else {
  1101. BUG();
  1102. }
  1103. ptr += len;
  1104. sb_ptr += len;
  1105. cur += len;
  1106. }
  1107. return 0;
  1108. }
  1109. int btrfs_read_chunk_tree(struct btrfs_root *root)
  1110. {
  1111. struct btrfs_path *path;
  1112. struct extent_buffer *leaf;
  1113. struct btrfs_key key;
  1114. struct btrfs_key found_key;
  1115. int ret;
  1116. int slot;
  1117. root = root->fs_info->chunk_root;
  1118. path = btrfs_alloc_path();
  1119. if (!path)
  1120. return -ENOMEM;
  1121. /* first we search for all of the device items, and then we
  1122. * read in all of the chunk items. This way we can create chunk
  1123. * mappings that reference all of the devices that are afound
  1124. */
  1125. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  1126. key.offset = 0;
  1127. key.type = 0;
  1128. again:
  1129. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1130. while(1) {
  1131. leaf = path->nodes[0];
  1132. slot = path->slots[0];
  1133. if (slot >= btrfs_header_nritems(leaf)) {
  1134. ret = btrfs_next_leaf(root, path);
  1135. if (ret == 0)
  1136. continue;
  1137. if (ret < 0)
  1138. goto error;
  1139. break;
  1140. }
  1141. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  1142. if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
  1143. if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
  1144. break;
  1145. if (found_key.type == BTRFS_DEV_ITEM_KEY) {
  1146. struct btrfs_dev_item *dev_item;
  1147. dev_item = btrfs_item_ptr(leaf, slot,
  1148. struct btrfs_dev_item);
  1149. ret = read_one_dev(root, leaf, dev_item);
  1150. BUG_ON(ret);
  1151. }
  1152. } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
  1153. struct btrfs_chunk *chunk;
  1154. chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
  1155. ret = read_one_chunk(root, &found_key, leaf, chunk);
  1156. }
  1157. path->slots[0]++;
  1158. }
  1159. if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
  1160. key.objectid = 0;
  1161. btrfs_release_path(root, path);
  1162. goto again;
  1163. }
  1164. btrfs_free_path(path);
  1165. ret = 0;
  1166. error:
  1167. return ret;
  1168. }