volumes.c 37 KB

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