volumes.c 35 KB

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