volumes.c 34 KB

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