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