volumes.c 32 KB

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