Btrfs: Drop some verbose printks
[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>
f2d8d74d 21#include <linux/blkdev.h>
788f20eb 22#include <linux/random.h>
593060d7 23#include <asm/div64.h>
0b86a832
CM
24#include "ctree.h"
25#include "extent_map.h"
26#include "disk-io.h"
27#include "transaction.h"
28#include "print-tree.h"
29#include "volumes.h"
30
593060d7
CM
31struct map_lookup {
32 u64 type;
33 int io_align;
34 int io_width;
35 int stripe_len;
36 int sector_size;
37 int num_stripes;
321aecc6 38 int sub_stripes;
cea9e445 39 struct btrfs_bio_stripe stripes[];
593060d7
CM
40};
41
42#define map_lookup_size(n) (sizeof(struct map_lookup) + \
cea9e445 43 (sizeof(struct btrfs_bio_stripe) * (n)))
593060d7 44
8a4b83cc
CM
45static DEFINE_MUTEX(uuid_mutex);
46static LIST_HEAD(fs_uuids);
47
48int btrfs_cleanup_fs_uuids(void)
49{
50 struct btrfs_fs_devices *fs_devices;
51 struct list_head *uuid_cur;
52 struct list_head *devices_cur;
53 struct btrfs_device *dev;
54
55 list_for_each(uuid_cur, &fs_uuids) {
56 fs_devices = list_entry(uuid_cur, struct btrfs_fs_devices,
57 list);
58 while(!list_empty(&fs_devices->devices)) {
59 devices_cur = fs_devices->devices.next;
60 dev = list_entry(devices_cur, struct btrfs_device,
61 dev_list);
8a4b83cc 62 if (dev->bdev) {
8a4b83cc
CM
63 close_bdev_excl(dev->bdev);
64 }
65 list_del(&dev->dev_list);
66 kfree(dev);
67 }
68 }
69 return 0;
70}
71
a443755f
CM
72static struct btrfs_device *__find_device(struct list_head *head, u64 devid,
73 u8 *uuid)
8a4b83cc
CM
74{
75 struct btrfs_device *dev;
76 struct list_head *cur;
77
78 list_for_each(cur, head) {
79 dev = list_entry(cur, struct btrfs_device, dev_list);
a443755f 80 if (dev->devid == devid &&
8f18cf13 81 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
8a4b83cc 82 return dev;
a443755f 83 }
8a4b83cc
CM
84 }
85 return NULL;
86}
87
88static struct btrfs_fs_devices *find_fsid(u8 *fsid)
89{
90 struct list_head *cur;
91 struct btrfs_fs_devices *fs_devices;
92
93 list_for_each(cur, &fs_uuids) {
94 fs_devices = list_entry(cur, struct btrfs_fs_devices, list);
95 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
96 return fs_devices;
97 }
98 return NULL;
99}
100
101static int device_list_add(const char *path,
102 struct btrfs_super_block *disk_super,
103 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
104{
105 struct btrfs_device *device;
106 struct btrfs_fs_devices *fs_devices;
107 u64 found_transid = btrfs_super_generation(disk_super);
108
109 fs_devices = find_fsid(disk_super->fsid);
110 if (!fs_devices) {
111 fs_devices = kmalloc(sizeof(*fs_devices), GFP_NOFS);
112 if (!fs_devices)
113 return -ENOMEM;
114 INIT_LIST_HEAD(&fs_devices->devices);
b3075717 115 INIT_LIST_HEAD(&fs_devices->alloc_list);
8a4b83cc
CM
116 list_add(&fs_devices->list, &fs_uuids);
117 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
118 fs_devices->latest_devid = devid;
119 fs_devices->latest_trans = found_transid;
120 fs_devices->lowest_devid = (u64)-1;
121 fs_devices->num_devices = 0;
122 device = NULL;
123 } else {
a443755f
CM
124 device = __find_device(&fs_devices->devices, devid,
125 disk_super->dev_item.uuid);
8a4b83cc
CM
126 }
127 if (!device) {
128 device = kzalloc(sizeof(*device), GFP_NOFS);
129 if (!device) {
130 /* we can safely leave the fs_devices entry around */
131 return -ENOMEM;
132 }
133 device->devid = devid;
a443755f
CM
134 memcpy(device->uuid, disk_super->dev_item.uuid,
135 BTRFS_UUID_SIZE);
f2984462 136 device->barriers = 1;
b248a415 137 spin_lock_init(&device->io_lock);
8a4b83cc
CM
138 device->name = kstrdup(path, GFP_NOFS);
139 if (!device->name) {
140 kfree(device);
141 return -ENOMEM;
142 }
143 list_add(&device->dev_list, &fs_devices->devices);
b3075717 144 list_add(&device->dev_alloc_list, &fs_devices->alloc_list);
8a4b83cc
CM
145 fs_devices->num_devices++;
146 }
147
148 if (found_transid > fs_devices->latest_trans) {
149 fs_devices->latest_devid = devid;
150 fs_devices->latest_trans = found_transid;
151 }
152 if (fs_devices->lowest_devid > devid) {
153 fs_devices->lowest_devid = devid;
8a4b83cc
CM
154 }
155 *fs_devices_ret = fs_devices;
156 return 0;
157}
158
159int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
160{
161 struct list_head *head = &fs_devices->devices;
162 struct list_head *cur;
163 struct btrfs_device *device;
164
165 mutex_lock(&uuid_mutex);
166 list_for_each(cur, head) {
167 device = list_entry(cur, struct btrfs_device, dev_list);
168 if (device->bdev) {
169 close_bdev_excl(device->bdev);
8a4b83cc
CM
170 }
171 device->bdev = NULL;
172 }
173 mutex_unlock(&uuid_mutex);
174 return 0;
175}
176
177int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
178 int flags, void *holder)
179{
180 struct block_device *bdev;
181 struct list_head *head = &fs_devices->devices;
182 struct list_head *cur;
183 struct btrfs_device *device;
184 int ret;
185
186 mutex_lock(&uuid_mutex);
187 list_for_each(cur, head) {
188 device = list_entry(cur, struct btrfs_device, dev_list);
189 bdev = open_bdev_excl(device->name, flags, holder);
e17cade2 190
8a4b83cc
CM
191 if (IS_ERR(bdev)) {
192 printk("open %s failed\n", device->name);
193 ret = PTR_ERR(bdev);
194 goto fail;
195 }
196 if (device->devid == fs_devices->latest_devid)
197 fs_devices->latest_bdev = bdev;
198 if (device->devid == fs_devices->lowest_devid) {
199 fs_devices->lowest_bdev = bdev;
8a4b83cc
CM
200 }
201 device->bdev = bdev;
202 }
203 mutex_unlock(&uuid_mutex);
204 return 0;
205fail:
206 mutex_unlock(&uuid_mutex);
207 btrfs_close_devices(fs_devices);
208 return ret;
209}
210
211int btrfs_scan_one_device(const char *path, int flags, void *holder,
212 struct btrfs_fs_devices **fs_devices_ret)
213{
214 struct btrfs_super_block *disk_super;
215 struct block_device *bdev;
216 struct buffer_head *bh;
217 int ret;
218 u64 devid;
f2984462 219 u64 transid;
8a4b83cc
CM
220
221 mutex_lock(&uuid_mutex);
222
8a4b83cc
CM
223 bdev = open_bdev_excl(path, flags, holder);
224
225 if (IS_ERR(bdev)) {
8a4b83cc
CM
226 ret = PTR_ERR(bdev);
227 goto error;
228 }
229
230 ret = set_blocksize(bdev, 4096);
231 if (ret)
232 goto error_close;
233 bh = __bread(bdev, BTRFS_SUPER_INFO_OFFSET / 4096, 4096);
234 if (!bh) {
235 ret = -EIO;
236 goto error_close;
237 }
238 disk_super = (struct btrfs_super_block *)bh->b_data;
239 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
240 sizeof(disk_super->magic))) {
e58ca020 241 ret = -EINVAL;
8a4b83cc
CM
242 goto error_brelse;
243 }
244 devid = le64_to_cpu(disk_super->dev_item.devid);
f2984462 245 transid = btrfs_super_generation(disk_super);
7ae9c09d
CM
246 if (disk_super->label[0])
247 printk("device label %s ", disk_super->label);
248 else {
249 /* FIXME, make a readl uuid parser */
250 printk("device fsid %llx-%llx ",
251 *(unsigned long long *)disk_super->fsid,
252 *(unsigned long long *)(disk_super->fsid + 8));
253 }
254 printk("devid %Lu transid %Lu %s\n", devid, transid, path);
8a4b83cc
CM
255 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
256
257error_brelse:
258 brelse(bh);
259error_close:
260 close_bdev_excl(bdev);
8a4b83cc
CM
261error:
262 mutex_unlock(&uuid_mutex);
263 return ret;
264}
0b86a832
CM
265
266/*
267 * this uses a pretty simple search, the expectation is that it is
268 * called very infrequently and that a given device has a small number
269 * of extents
270 */
271static int find_free_dev_extent(struct btrfs_trans_handle *trans,
272 struct btrfs_device *device,
273 struct btrfs_path *path,
274 u64 num_bytes, u64 *start)
275{
276 struct btrfs_key key;
277 struct btrfs_root *root = device->dev_root;
278 struct btrfs_dev_extent *dev_extent = NULL;
279 u64 hole_size = 0;
280 u64 last_byte = 0;
281 u64 search_start = 0;
282 u64 search_end = device->total_bytes;
283 int ret;
284 int slot = 0;
285 int start_found;
286 struct extent_buffer *l;
287
288 start_found = 0;
289 path->reada = 2;
290
291 /* FIXME use last free of some kind */
292
8a4b83cc
CM
293 /* we don't want to overwrite the superblock on the drive,
294 * so we make sure to start at an offset of at least 1MB
295 */
296 search_start = max((u64)1024 * 1024, search_start);
8f18cf13
CM
297
298 if (root->fs_info->alloc_start + num_bytes <= device->total_bytes)
299 search_start = max(root->fs_info->alloc_start, search_start);
300
0b86a832
CM
301 key.objectid = device->devid;
302 key.offset = search_start;
303 key.type = BTRFS_DEV_EXTENT_KEY;
304 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
305 if (ret < 0)
306 goto error;
307 ret = btrfs_previous_item(root, path, 0, key.type);
308 if (ret < 0)
309 goto error;
310 l = path->nodes[0];
311 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
312 while (1) {
313 l = path->nodes[0];
314 slot = path->slots[0];
315 if (slot >= btrfs_header_nritems(l)) {
316 ret = btrfs_next_leaf(root, path);
317 if (ret == 0)
318 continue;
319 if (ret < 0)
320 goto error;
321no_more_items:
322 if (!start_found) {
323 if (search_start >= search_end) {
324 ret = -ENOSPC;
325 goto error;
326 }
327 *start = search_start;
328 start_found = 1;
329 goto check_pending;
330 }
331 *start = last_byte > search_start ?
332 last_byte : search_start;
333 if (search_end <= *start) {
334 ret = -ENOSPC;
335 goto error;
336 }
337 goto check_pending;
338 }
339 btrfs_item_key_to_cpu(l, &key, slot);
340
341 if (key.objectid < device->devid)
342 goto next;
343
344 if (key.objectid > device->devid)
345 goto no_more_items;
346
347 if (key.offset >= search_start && key.offset > last_byte &&
348 start_found) {
349 if (last_byte < search_start)
350 last_byte = search_start;
351 hole_size = key.offset - last_byte;
352 if (key.offset > last_byte &&
353 hole_size >= num_bytes) {
354 *start = last_byte;
355 goto check_pending;
356 }
357 }
358 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) {
359 goto next;
360 }
361
362 start_found = 1;
363 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
364 last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent);
365next:
366 path->slots[0]++;
367 cond_resched();
368 }
369check_pending:
370 /* we have to make sure we didn't find an extent that has already
371 * been allocated by the map tree or the original allocation
372 */
373 btrfs_release_path(root, path);
374 BUG_ON(*start < search_start);
375
6324fbf3 376 if (*start + num_bytes > search_end) {
0b86a832
CM
377 ret = -ENOSPC;
378 goto error;
379 }
380 /* check for pending inserts here */
381 return 0;
382
383error:
384 btrfs_release_path(root, path);
385 return ret;
386}
387
8f18cf13
CM
388int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
389 struct btrfs_device *device,
390 u64 start)
391{
392 int ret;
393 struct btrfs_path *path;
394 struct btrfs_root *root = device->dev_root;
395 struct btrfs_key key;
396
397 path = btrfs_alloc_path();
398 if (!path)
399 return -ENOMEM;
400
401 key.objectid = device->devid;
402 key.offset = start;
403 key.type = BTRFS_DEV_EXTENT_KEY;
404
405 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
406 BUG_ON(ret);
407
408 ret = btrfs_del_item(trans, root, path);
409 BUG_ON(ret);
410
411 btrfs_free_path(path);
412 return ret;
413}
414
0b86a832
CM
415int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
416 struct btrfs_device *device,
e17cade2
CM
417 u64 chunk_tree, u64 chunk_objectid,
418 u64 chunk_offset,
419 u64 num_bytes, u64 *start)
0b86a832
CM
420{
421 int ret;
422 struct btrfs_path *path;
423 struct btrfs_root *root = device->dev_root;
424 struct btrfs_dev_extent *extent;
425 struct extent_buffer *leaf;
426 struct btrfs_key key;
427
428 path = btrfs_alloc_path();
429 if (!path)
430 return -ENOMEM;
431
432 ret = find_free_dev_extent(trans, device, path, num_bytes, start);
6324fbf3 433 if (ret) {
0b86a832 434 goto err;
6324fbf3 435 }
0b86a832
CM
436
437 key.objectid = device->devid;
438 key.offset = *start;
439 key.type = BTRFS_DEV_EXTENT_KEY;
440 ret = btrfs_insert_empty_item(trans, root, path, &key,
441 sizeof(*extent));
442 BUG_ON(ret);
443
444 leaf = path->nodes[0];
445 extent = btrfs_item_ptr(leaf, path->slots[0],
446 struct btrfs_dev_extent);
e17cade2
CM
447 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
448 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
449 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
450
451 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
452 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
453 BTRFS_UUID_SIZE);
454
0b86a832
CM
455 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
456 btrfs_mark_buffer_dirty(leaf);
457err:
458 btrfs_free_path(path);
459 return ret;
460}
461
e17cade2 462static int find_next_chunk(struct btrfs_root *root, u64 objectid, u64 *offset)
0b86a832
CM
463{
464 struct btrfs_path *path;
465 int ret;
466 struct btrfs_key key;
e17cade2 467 struct btrfs_chunk *chunk;
0b86a832
CM
468 struct btrfs_key found_key;
469
470 path = btrfs_alloc_path();
471 BUG_ON(!path);
472
e17cade2 473 key.objectid = objectid;
0b86a832
CM
474 key.offset = (u64)-1;
475 key.type = BTRFS_CHUNK_ITEM_KEY;
476
477 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
478 if (ret < 0)
479 goto error;
480
481 BUG_ON(ret == 0);
482
483 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
484 if (ret) {
e17cade2 485 *offset = 0;
0b86a832
CM
486 } else {
487 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
488 path->slots[0]);
e17cade2
CM
489 if (found_key.objectid != objectid)
490 *offset = 0;
491 else {
492 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
493 struct btrfs_chunk);
494 *offset = found_key.offset +
495 btrfs_chunk_length(path->nodes[0], chunk);
496 }
0b86a832
CM
497 }
498 ret = 0;
499error:
500 btrfs_free_path(path);
501 return ret;
502}
503
0b86a832
CM
504static int find_next_devid(struct btrfs_root *root, struct btrfs_path *path,
505 u64 *objectid)
506{
507 int ret;
508 struct btrfs_key key;
509 struct btrfs_key found_key;
510
511 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
512 key.type = BTRFS_DEV_ITEM_KEY;
513 key.offset = (u64)-1;
514
515 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
516 if (ret < 0)
517 goto error;
518
519 BUG_ON(ret == 0);
520
521 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
522 BTRFS_DEV_ITEM_KEY);
523 if (ret) {
524 *objectid = 1;
525 } else {
526 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
527 path->slots[0]);
528 *objectid = found_key.offset + 1;
529 }
530 ret = 0;
531error:
532 btrfs_release_path(root, path);
533 return ret;
534}
535
536/*
537 * the device information is stored in the chunk root
538 * the btrfs_device struct should be fully filled in
539 */
540int btrfs_add_device(struct btrfs_trans_handle *trans,
541 struct btrfs_root *root,
542 struct btrfs_device *device)
543{
544 int ret;
545 struct btrfs_path *path;
546 struct btrfs_dev_item *dev_item;
547 struct extent_buffer *leaf;
548 struct btrfs_key key;
549 unsigned long ptr;
550 u64 free_devid;
551
552 root = root->fs_info->chunk_root;
553
554 path = btrfs_alloc_path();
555 if (!path)
556 return -ENOMEM;
557
558 ret = find_next_devid(root, path, &free_devid);
559 if (ret)
560 goto out;
561
562 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
563 key.type = BTRFS_DEV_ITEM_KEY;
564 key.offset = free_devid;
565
566 ret = btrfs_insert_empty_item(trans, root, path, &key,
0d81ba5d 567 sizeof(*dev_item));
0b86a832
CM
568 if (ret)
569 goto out;
570
571 leaf = path->nodes[0];
572 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
573
8a4b83cc 574 device->devid = free_devid;
0b86a832
CM
575 btrfs_set_device_id(leaf, dev_item, device->devid);
576 btrfs_set_device_type(leaf, dev_item, device->type);
577 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
578 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
579 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
0b86a832
CM
580 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
581 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
e17cade2
CM
582 btrfs_set_device_group(leaf, dev_item, 0);
583 btrfs_set_device_seek_speed(leaf, dev_item, 0);
584 btrfs_set_device_bandwidth(leaf, dev_item, 0);
0b86a832 585
0b86a832 586 ptr = (unsigned long)btrfs_device_uuid(dev_item);
e17cade2 587 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
0b86a832
CM
588 btrfs_mark_buffer_dirty(leaf);
589 ret = 0;
590
591out:
592 btrfs_free_path(path);
593 return ret;
594}
8f18cf13 595
788f20eb
CM
596int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
597{
598 struct btrfs_trans_handle *trans;
599 struct btrfs_device *device;
600 struct block_device *bdev;
601 struct list_head *cur;
602 struct list_head *devices;
603 u64 total_bytes;
604 int ret = 0;
605
606
607 bdev = open_bdev_excl(device_path, 0, root->fs_info->bdev_holder);
608 if (!bdev) {
609 return -EIO;
610 }
611 mutex_lock(&root->fs_info->fs_mutex);
612 trans = btrfs_start_transaction(root, 1);
613 devices = &root->fs_info->fs_devices->devices;
614 list_for_each(cur, devices) {
615 device = list_entry(cur, struct btrfs_device, dev_list);
616 if (device->bdev == bdev) {
617 ret = -EEXIST;
618 goto out;
619 }
620 }
621
622 device = kzalloc(sizeof(*device), GFP_NOFS);
623 if (!device) {
624 /* we can safely leave the fs_devices entry around */
625 ret = -ENOMEM;
626 goto out_close_bdev;
627 }
628
629 device->barriers = 1;
630 generate_random_uuid(device->uuid);
631 spin_lock_init(&device->io_lock);
632 device->name = kstrdup(device_path, GFP_NOFS);
633 if (!device->name) {
634 kfree(device);
635 goto out_close_bdev;
636 }
637 device->io_width = root->sectorsize;
638 device->io_align = root->sectorsize;
639 device->sector_size = root->sectorsize;
640 device->total_bytes = i_size_read(bdev->bd_inode);
641 device->dev_root = root->fs_info->dev_root;
642 device->bdev = bdev;
643
644 ret = btrfs_add_device(trans, root, device);
645 if (ret)
646 goto out_close_bdev;
647
648 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
649 btrfs_set_super_total_bytes(&root->fs_info->super_copy,
650 total_bytes + device->total_bytes);
651
652 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
653 btrfs_set_super_num_devices(&root->fs_info->super_copy,
654 total_bytes + 1);
655
656 list_add(&device->dev_list, &root->fs_info->fs_devices->devices);
657 list_add(&device->dev_alloc_list,
658 &root->fs_info->fs_devices->alloc_list);
659 root->fs_info->fs_devices->num_devices++;
660out:
661 btrfs_end_transaction(trans, root);
662 mutex_unlock(&root->fs_info->fs_mutex);
663 return ret;
664
665out_close_bdev:
666 close_bdev_excl(bdev);
667 goto out;
668}
669
0b86a832
CM
670int btrfs_update_device(struct btrfs_trans_handle *trans,
671 struct btrfs_device *device)
672{
673 int ret;
674 struct btrfs_path *path;
675 struct btrfs_root *root;
676 struct btrfs_dev_item *dev_item;
677 struct extent_buffer *leaf;
678 struct btrfs_key key;
679
680 root = device->dev_root->fs_info->chunk_root;
681
682 path = btrfs_alloc_path();
683 if (!path)
684 return -ENOMEM;
685
686 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
687 key.type = BTRFS_DEV_ITEM_KEY;
688 key.offset = device->devid;
689
690 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
691 if (ret < 0)
692 goto out;
693
694 if (ret > 0) {
695 ret = -ENOENT;
696 goto out;
697 }
698
699 leaf = path->nodes[0];
700 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
701
702 btrfs_set_device_id(leaf, dev_item, device->devid);
703 btrfs_set_device_type(leaf, dev_item, device->type);
704 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
705 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
706 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
0b86a832
CM
707 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
708 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
709 btrfs_mark_buffer_dirty(leaf);
710
711out:
712 btrfs_free_path(path);
713 return ret;
714}
715
8f18cf13
CM
716int btrfs_grow_device(struct btrfs_trans_handle *trans,
717 struct btrfs_device *device, u64 new_size)
718{
719 struct btrfs_super_block *super_copy =
720 &device->dev_root->fs_info->super_copy;
721 u64 old_total = btrfs_super_total_bytes(super_copy);
722 u64 diff = new_size - device->total_bytes;
723
724 btrfs_set_super_total_bytes(super_copy, old_total + diff);
725 return btrfs_update_device(trans, device);
726}
727
728static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
729 struct btrfs_root *root,
730 u64 chunk_tree, u64 chunk_objectid,
731 u64 chunk_offset)
732{
733 int ret;
734 struct btrfs_path *path;
735 struct btrfs_key key;
736
737 root = root->fs_info->chunk_root;
738 path = btrfs_alloc_path();
739 if (!path)
740 return -ENOMEM;
741
742 key.objectid = chunk_objectid;
743 key.offset = chunk_offset;
744 key.type = BTRFS_CHUNK_ITEM_KEY;
745
746 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
747 BUG_ON(ret);
748
749 ret = btrfs_del_item(trans, root, path);
750 BUG_ON(ret);
751
752 btrfs_free_path(path);
753 return 0;
754}
755
756int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
757 chunk_offset)
758{
759 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
760 struct btrfs_disk_key *disk_key;
761 struct btrfs_chunk *chunk;
762 u8 *ptr;
763 int ret = 0;
764 u32 num_stripes;
765 u32 array_size;
766 u32 len = 0;
767 u32 cur;
768 struct btrfs_key key;
769
770 array_size = btrfs_super_sys_array_size(super_copy);
771
772 ptr = super_copy->sys_chunk_array;
773 cur = 0;
774
775 while (cur < array_size) {
776 disk_key = (struct btrfs_disk_key *)ptr;
777 btrfs_disk_key_to_cpu(&key, disk_key);
778
779 len = sizeof(*disk_key);
780
781 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
782 chunk = (struct btrfs_chunk *)(ptr + len);
783 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
784 len += btrfs_chunk_item_size(num_stripes);
785 } else {
786 ret = -EIO;
787 break;
788 }
789 if (key.objectid == chunk_objectid &&
790 key.offset == chunk_offset) {
791 memmove(ptr, ptr + len, array_size - (cur + len));
792 array_size -= len;
793 btrfs_set_super_sys_array_size(super_copy, array_size);
794 } else {
795 ptr += len;
796 cur += len;
797 }
798 }
799 return ret;
800}
801
802
803int btrfs_relocate_chunk(struct btrfs_root *root,
804 u64 chunk_tree, u64 chunk_objectid,
805 u64 chunk_offset)
806{
807 struct extent_map_tree *em_tree;
808 struct btrfs_root *extent_root;
809 struct btrfs_trans_handle *trans;
810 struct extent_map *em;
811 struct map_lookup *map;
812 int ret;
813 int i;
814
815 root = root->fs_info->chunk_root;
816 extent_root = root->fs_info->extent_root;
817 em_tree = &root->fs_info->mapping_tree.map_tree;
818
819 /* step one, relocate all the extents inside this chunk */
820 ret = btrfs_shrink_extent_tree(extent_root, chunk_offset);
821 BUG_ON(ret);
822
823 trans = btrfs_start_transaction(root, 1);
824 BUG_ON(!trans);
825
826 /*
827 * step two, delete the device extents and the
828 * chunk tree entries
829 */
830 spin_lock(&em_tree->lock);
831 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
832 spin_unlock(&em_tree->lock);
833
834 BUG_ON(em->start > chunk_offset || em->start + em->len < chunk_offset);
835 map = (struct map_lookup *)em->bdev;
836
837 for (i = 0; i < map->num_stripes; i++) {
838 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
839 map->stripes[i].physical);
840 BUG_ON(ret);
841 }
842 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
843 chunk_offset);
844
845 BUG_ON(ret);
846
847 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
848 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
849 BUG_ON(ret);
850 goto out;
851 }
852
853
854
855 spin_lock(&em_tree->lock);
856 remove_extent_mapping(em_tree, em);
857 kfree(map);
858 em->bdev = NULL;
859
860 /* once for the tree */
861 free_extent_map(em);
862 spin_unlock(&em_tree->lock);
863
864out:
865 /* once for us */
866 free_extent_map(em);
867
868 btrfs_end_transaction(trans, root);
869 return 0;
870}
871
ec44a35c
CM
872static u64 div_factor(u64 num, int factor)
873{
874 if (factor == 10)
875 return num;
876 num *= factor;
877 do_div(num, 10);
878 return num;
879}
880
881
882int btrfs_balance(struct btrfs_root *dev_root)
883{
884 int ret;
885 struct list_head *cur;
886 struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
887 struct btrfs_device *device;
888 u64 old_size;
889 u64 size_to_free;
890 struct btrfs_path *path;
891 struct btrfs_key key;
892 struct btrfs_chunk *chunk;
893 struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
894 struct btrfs_trans_handle *trans;
895 struct btrfs_key found_key;
896
897
898 dev_root = dev_root->fs_info->dev_root;
899
900 mutex_lock(&dev_root->fs_info->fs_mutex);
901 /* step one make some room on all the devices */
902 list_for_each(cur, devices) {
903 device = list_entry(cur, struct btrfs_device, dev_list);
904 old_size = device->total_bytes;
905 size_to_free = div_factor(old_size, 1);
906 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
907 if (device->total_bytes - device->bytes_used > size_to_free)
908 continue;
909
910 ret = btrfs_shrink_device(device, old_size - size_to_free);
911 BUG_ON(ret);
912
913 trans = btrfs_start_transaction(dev_root, 1);
914 BUG_ON(!trans);
915
916 ret = btrfs_grow_device(trans, device, old_size);
917 BUG_ON(ret);
918
919 btrfs_end_transaction(trans, dev_root);
920 }
921
922 /* step two, relocate all the chunks */
923 path = btrfs_alloc_path();
924 BUG_ON(!path);
925
926 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
927 key.offset = (u64)-1;
928 key.type = BTRFS_CHUNK_ITEM_KEY;
929
930 while(1) {
931 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
932 if (ret < 0)
933 goto error;
934
935 /*
936 * this shouldn't happen, it means the last relocate
937 * failed
938 */
939 if (ret == 0)
940 break;
941
942 ret = btrfs_previous_item(chunk_root, path, 0,
943 BTRFS_CHUNK_ITEM_KEY);
944 if (ret) {
945 break;
946 }
947 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
948 path->slots[0]);
949 if (found_key.objectid != key.objectid)
950 break;
951 chunk = btrfs_item_ptr(path->nodes[0],
952 path->slots[0],
953 struct btrfs_chunk);
954 key.offset = found_key.offset;
955 /* chunk zero is special */
956 if (key.offset == 0)
957 break;
958
959 ret = btrfs_relocate_chunk(chunk_root,
960 chunk_root->root_key.objectid,
961 found_key.objectid,
962 found_key.offset);
963 BUG_ON(ret);
964 btrfs_release_path(chunk_root, path);
965 }
966 ret = 0;
967error:
968 btrfs_free_path(path);
969 mutex_unlock(&dev_root->fs_info->fs_mutex);
970 return ret;
971}
972
8f18cf13
CM
973/*
974 * shrinking a device means finding all of the device extents past
975 * the new size, and then following the back refs to the chunks.
976 * The chunk relocation code actually frees the device extent
977 */
978int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
979{
980 struct btrfs_trans_handle *trans;
981 struct btrfs_root *root = device->dev_root;
982 struct btrfs_dev_extent *dev_extent = NULL;
983 struct btrfs_path *path;
984 u64 length;
985 u64 chunk_tree;
986 u64 chunk_objectid;
987 u64 chunk_offset;
988 int ret;
989 int slot;
990 struct extent_buffer *l;
991 struct btrfs_key key;
992 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
993 u64 old_total = btrfs_super_total_bytes(super_copy);
994 u64 diff = device->total_bytes - new_size;
995
996
997 path = btrfs_alloc_path();
998 if (!path)
999 return -ENOMEM;
1000
1001 trans = btrfs_start_transaction(root, 1);
1002 if (!trans) {
1003 ret = -ENOMEM;
1004 goto done;
1005 }
1006
1007 path->reada = 2;
1008
1009 device->total_bytes = new_size;
1010 ret = btrfs_update_device(trans, device);
1011 if (ret) {
1012 btrfs_end_transaction(trans, root);
1013 goto done;
1014 }
1015 WARN_ON(diff > old_total);
1016 btrfs_set_super_total_bytes(super_copy, old_total - diff);
1017 btrfs_end_transaction(trans, root);
1018
1019 key.objectid = device->devid;
1020 key.offset = (u64)-1;
1021 key.type = BTRFS_DEV_EXTENT_KEY;
1022
1023 while (1) {
1024 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1025 if (ret < 0)
1026 goto done;
1027
1028 ret = btrfs_previous_item(root, path, 0, key.type);
1029 if (ret < 0)
1030 goto done;
1031 if (ret) {
1032 ret = 0;
1033 goto done;
1034 }
1035
1036 l = path->nodes[0];
1037 slot = path->slots[0];
1038 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
1039
1040 if (key.objectid != device->devid)
1041 goto done;
1042
1043 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
1044 length = btrfs_dev_extent_length(l, dev_extent);
1045
1046 if (key.offset + length <= new_size)
1047 goto done;
1048
1049 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
1050 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
1051 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
1052 btrfs_release_path(root, path);
1053
1054 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
1055 chunk_offset);
1056 if (ret)
1057 goto done;
1058 }
1059
1060done:
1061 btrfs_free_path(path);
1062 return ret;
1063}
1064
0b86a832
CM
1065int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
1066 struct btrfs_root *root,
1067 struct btrfs_key *key,
1068 struct btrfs_chunk *chunk, int item_size)
1069{
1070 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1071 struct btrfs_disk_key disk_key;
1072 u32 array_size;
1073 u8 *ptr;
1074
1075 array_size = btrfs_super_sys_array_size(super_copy);
1076 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
1077 return -EFBIG;
1078
1079 ptr = super_copy->sys_chunk_array + array_size;
1080 btrfs_cpu_key_to_disk(&disk_key, key);
1081 memcpy(ptr, &disk_key, sizeof(disk_key));
1082 ptr += sizeof(disk_key);
1083 memcpy(ptr, chunk, item_size);
1084 item_size += sizeof(disk_key);
1085 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
1086 return 0;
1087}
1088
9b3f68b9
CM
1089static u64 chunk_bytes_by_type(u64 type, u64 calc_size, int num_stripes,
1090 int sub_stripes)
1091{
1092 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
1093 return calc_size;
1094 else if (type & BTRFS_BLOCK_GROUP_RAID10)
1095 return calc_size * (num_stripes / sub_stripes);
1096 else
1097 return calc_size * num_stripes;
1098}
1099
1100
0b86a832
CM
1101int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
1102 struct btrfs_root *extent_root, u64 *start,
6324fbf3 1103 u64 *num_bytes, u64 type)
0b86a832
CM
1104{
1105 u64 dev_offset;
593060d7 1106 struct btrfs_fs_info *info = extent_root->fs_info;
0b86a832 1107 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
8f18cf13 1108 struct btrfs_path *path;
0b86a832
CM
1109 struct btrfs_stripe *stripes;
1110 struct btrfs_device *device = NULL;
1111 struct btrfs_chunk *chunk;
6324fbf3 1112 struct list_head private_devs;
b3075717 1113 struct list_head *dev_list;
6324fbf3 1114 struct list_head *cur;
0b86a832
CM
1115 struct extent_map_tree *em_tree;
1116 struct map_lookup *map;
1117 struct extent_map *em;
a40a90a0 1118 int min_stripe_size = 1 * 1024 * 1024;
0b86a832
CM
1119 u64 physical;
1120 u64 calc_size = 1024 * 1024 * 1024;
9b3f68b9
CM
1121 u64 max_chunk_size = calc_size;
1122 u64 min_free;
6324fbf3
CM
1123 u64 avail;
1124 u64 max_avail = 0;
9b3f68b9 1125 u64 percent_max;
6324fbf3 1126 int num_stripes = 1;
a40a90a0 1127 int min_stripes = 1;
321aecc6 1128 int sub_stripes = 0;
6324fbf3 1129 int looped = 0;
0b86a832 1130 int ret;
6324fbf3 1131 int index;
593060d7 1132 int stripe_len = 64 * 1024;
0b86a832
CM
1133 struct btrfs_key key;
1134
ec44a35c
CM
1135 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
1136 (type & BTRFS_BLOCK_GROUP_DUP)) {
1137 WARN_ON(1);
1138 type &= ~BTRFS_BLOCK_GROUP_DUP;
1139 }
b3075717 1140 dev_list = &extent_root->fs_info->fs_devices->alloc_list;
6324fbf3
CM
1141 if (list_empty(dev_list))
1142 return -ENOSPC;
593060d7 1143
a40a90a0 1144 if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
593060d7 1145 num_stripes = btrfs_super_num_devices(&info->super_copy);
a40a90a0
CM
1146 min_stripes = 2;
1147 }
1148 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
611f0e00 1149 num_stripes = 2;
a40a90a0
CM
1150 min_stripes = 2;
1151 }
8790d502
CM
1152 if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
1153 num_stripes = min_t(u64, 2,
1154 btrfs_super_num_devices(&info->super_copy));
9b3f68b9
CM
1155 if (num_stripes < 2)
1156 return -ENOSPC;
a40a90a0 1157 min_stripes = 2;
8790d502 1158 }
321aecc6
CM
1159 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
1160 num_stripes = btrfs_super_num_devices(&info->super_copy);
1161 if (num_stripes < 4)
1162 return -ENOSPC;
1163 num_stripes &= ~(u32)1;
1164 sub_stripes = 2;
a40a90a0 1165 min_stripes = 4;
321aecc6 1166 }
9b3f68b9
CM
1167
1168 if (type & BTRFS_BLOCK_GROUP_DATA) {
1169 max_chunk_size = 10 * calc_size;
a40a90a0 1170 min_stripe_size = 64 * 1024 * 1024;
9b3f68b9
CM
1171 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
1172 max_chunk_size = 4 * calc_size;
a40a90a0
CM
1173 min_stripe_size = 32 * 1024 * 1024;
1174 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
1175 calc_size = 8 * 1024 * 1024;
1176 max_chunk_size = calc_size * 2;
1177 min_stripe_size = 1 * 1024 * 1024;
9b3f68b9
CM
1178 }
1179
8f18cf13
CM
1180 path = btrfs_alloc_path();
1181 if (!path)
1182 return -ENOMEM;
1183
9b3f68b9
CM
1184 /* we don't want a chunk larger than 10% of the FS */
1185 percent_max = div_factor(btrfs_super_total_bytes(&info->super_copy), 1);
1186 max_chunk_size = min(percent_max, max_chunk_size);
1187
a40a90a0 1188again:
9b3f68b9
CM
1189 if (calc_size * num_stripes > max_chunk_size) {
1190 calc_size = max_chunk_size;
1191 do_div(calc_size, num_stripes);
1192 do_div(calc_size, stripe_len);
1193 calc_size *= stripe_len;
1194 }
1195 /* we don't want tiny stripes */
a40a90a0 1196 calc_size = max_t(u64, min_stripe_size, calc_size);
9b3f68b9 1197
9b3f68b9
CM
1198 do_div(calc_size, stripe_len);
1199 calc_size *= stripe_len;
1200
6324fbf3
CM
1201 INIT_LIST_HEAD(&private_devs);
1202 cur = dev_list->next;
1203 index = 0;
611f0e00
CM
1204
1205 if (type & BTRFS_BLOCK_GROUP_DUP)
1206 min_free = calc_size * 2;
9b3f68b9
CM
1207 else
1208 min_free = calc_size;
611f0e00 1209
ad5bd91e
CM
1210 /* we add 1MB because we never use the first 1MB of the device */
1211 min_free += 1024 * 1024;
1212
6324fbf3
CM
1213 /* build a private list of devices we will allocate from */
1214 while(index < num_stripes) {
b3075717 1215 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
611f0e00 1216
6324fbf3
CM
1217 avail = device->total_bytes - device->bytes_used;
1218 cur = cur->next;
8f18cf13 1219
611f0e00 1220 if (avail >= min_free) {
8f18cf13
CM
1221 u64 ignored_start = 0;
1222 ret = find_free_dev_extent(trans, device, path,
1223 min_free,
1224 &ignored_start);
1225 if (ret == 0) {
1226 list_move_tail(&device->dev_alloc_list,
1227 &private_devs);
611f0e00 1228 index++;
8f18cf13
CM
1229 if (type & BTRFS_BLOCK_GROUP_DUP)
1230 index++;
1231 }
a40a90a0
CM
1232 } else if (avail > max_avail)
1233 max_avail = avail;
6324fbf3
CM
1234 if (cur == dev_list)
1235 break;
1236 }
1237 if (index < num_stripes) {
1238 list_splice(&private_devs, dev_list);
a40a90a0
CM
1239 if (index >= min_stripes) {
1240 num_stripes = index;
1241 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
1242 num_stripes /= sub_stripes;
1243 num_stripes *= sub_stripes;
1244 }
1245 looped = 1;
1246 goto again;
1247 }
6324fbf3
CM
1248 if (!looped && max_avail > 0) {
1249 looped = 1;
1250 calc_size = max_avail;
1251 goto again;
1252 }
8f18cf13 1253 btrfs_free_path(path);
6324fbf3
CM
1254 return -ENOSPC;
1255 }
e17cade2
CM
1256 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1257 key.type = BTRFS_CHUNK_ITEM_KEY;
1258 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
1259 &key.offset);
8f18cf13
CM
1260 if (ret) {
1261 btrfs_free_path(path);
0b86a832 1262 return ret;
8f18cf13 1263 }
0b86a832 1264
0b86a832 1265 chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS);
8f18cf13
CM
1266 if (!chunk) {
1267 btrfs_free_path(path);
0b86a832 1268 return -ENOMEM;
8f18cf13 1269 }
0b86a832 1270
593060d7
CM
1271 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
1272 if (!map) {
1273 kfree(chunk);
8f18cf13 1274 btrfs_free_path(path);
593060d7
CM
1275 return -ENOMEM;
1276 }
8f18cf13
CM
1277 btrfs_free_path(path);
1278 path = NULL;
593060d7 1279
0b86a832 1280 stripes = &chunk->stripe;
9b3f68b9
CM
1281 *num_bytes = chunk_bytes_by_type(type, calc_size,
1282 num_stripes, sub_stripes);
0b86a832 1283
6324fbf3 1284 index = 0;
0b86a832 1285 while(index < num_stripes) {
e17cade2 1286 struct btrfs_stripe *stripe;
6324fbf3
CM
1287 BUG_ON(list_empty(&private_devs));
1288 cur = private_devs.next;
b3075717 1289 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
611f0e00
CM
1290
1291 /* loop over this device again if we're doing a dup group */
1292 if (!(type & BTRFS_BLOCK_GROUP_DUP) ||
1293 (index == num_stripes - 1))
b3075717 1294 list_move_tail(&device->dev_alloc_list, dev_list);
0b86a832
CM
1295
1296 ret = btrfs_alloc_dev_extent(trans, device,
e17cade2
CM
1297 info->chunk_root->root_key.objectid,
1298 BTRFS_FIRST_CHUNK_TREE_OBJECTID, key.offset,
1299 calc_size, &dev_offset);
0b86a832 1300 BUG_ON(ret);
0b86a832
CM
1301 device->bytes_used += calc_size;
1302 ret = btrfs_update_device(trans, device);
1303 BUG_ON(ret);
1304
593060d7
CM
1305 map->stripes[index].dev = device;
1306 map->stripes[index].physical = dev_offset;
e17cade2
CM
1307 stripe = stripes + index;
1308 btrfs_set_stack_stripe_devid(stripe, device->devid);
1309 btrfs_set_stack_stripe_offset(stripe, dev_offset);
1310 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
0b86a832
CM
1311 physical = dev_offset;
1312 index++;
1313 }
6324fbf3 1314 BUG_ON(!list_empty(&private_devs));
0b86a832 1315
e17cade2
CM
1316 /* key was set above */
1317 btrfs_set_stack_chunk_length(chunk, *num_bytes);
0b86a832 1318 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
593060d7 1319 btrfs_set_stack_chunk_stripe_len(chunk, stripe_len);
0b86a832
CM
1320 btrfs_set_stack_chunk_type(chunk, type);
1321 btrfs_set_stack_chunk_num_stripes(chunk, num_stripes);
593060d7
CM
1322 btrfs_set_stack_chunk_io_align(chunk, stripe_len);
1323 btrfs_set_stack_chunk_io_width(chunk, stripe_len);
0b86a832 1324 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
321aecc6 1325 btrfs_set_stack_chunk_sub_stripes(chunk, sub_stripes);
593060d7
CM
1326 map->sector_size = extent_root->sectorsize;
1327 map->stripe_len = stripe_len;
1328 map->io_align = stripe_len;
1329 map->io_width = stripe_len;
1330 map->type = type;
1331 map->num_stripes = num_stripes;
321aecc6 1332 map->sub_stripes = sub_stripes;
0b86a832
CM
1333
1334 ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
1335 btrfs_chunk_item_size(num_stripes));
1336 BUG_ON(ret);
e17cade2 1337 *start = key.offset;;
0b86a832
CM
1338
1339 em = alloc_extent_map(GFP_NOFS);
1340 if (!em)
1341 return -ENOMEM;
0b86a832 1342 em->bdev = (struct block_device *)map;
e17cade2
CM
1343 em->start = key.offset;
1344 em->len = *num_bytes;
0b86a832
CM
1345 em->block_start = 0;
1346
8f18cf13
CM
1347 if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
1348 ret = btrfs_add_system_chunk(trans, chunk_root, &key,
1349 chunk, btrfs_chunk_item_size(num_stripes));
1350 BUG_ON(ret);
1351 }
0b86a832
CM
1352 kfree(chunk);
1353
1354 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
1355 spin_lock(&em_tree->lock);
1356 ret = add_extent_mapping(em_tree, em);
0b86a832 1357 spin_unlock(&em_tree->lock);
b248a415 1358 BUG_ON(ret);
0b86a832
CM
1359 free_extent_map(em);
1360 return ret;
1361}
1362
1363void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
1364{
1365 extent_map_tree_init(&tree->map_tree, GFP_NOFS);
1366}
1367
1368void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
1369{
1370 struct extent_map *em;
1371
1372 while(1) {
1373 spin_lock(&tree->map_tree.lock);
1374 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
1375 if (em)
1376 remove_extent_mapping(&tree->map_tree, em);
1377 spin_unlock(&tree->map_tree.lock);
1378 if (!em)
1379 break;
1380 kfree(em->bdev);
1381 /* once for us */
1382 free_extent_map(em);
1383 /* once for the tree */
1384 free_extent_map(em);
1385 }
1386}
1387
f188591e
CM
1388int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
1389{
1390 struct extent_map *em;
1391 struct map_lookup *map;
1392 struct extent_map_tree *em_tree = &map_tree->map_tree;
1393 int ret;
1394
1395 spin_lock(&em_tree->lock);
1396 em = lookup_extent_mapping(em_tree, logical, len);
b248a415 1397 spin_unlock(&em_tree->lock);
f188591e
CM
1398 BUG_ON(!em);
1399
1400 BUG_ON(em->start > logical || em->start + em->len < logical);
1401 map = (struct map_lookup *)em->bdev;
1402 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
1403 ret = map->num_stripes;
321aecc6
CM
1404 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
1405 ret = map->sub_stripes;
f188591e
CM
1406 else
1407 ret = 1;
1408 free_extent_map(em);
f188591e
CM
1409 return ret;
1410}
1411
f2d8d74d
CM
1412static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
1413 u64 logical, u64 *length,
1414 struct btrfs_multi_bio **multi_ret,
1415 int mirror_num, struct page *unplug_page)
0b86a832
CM
1416{
1417 struct extent_map *em;
1418 struct map_lookup *map;
1419 struct extent_map_tree *em_tree = &map_tree->map_tree;
1420 u64 offset;
593060d7
CM
1421 u64 stripe_offset;
1422 u64 stripe_nr;
cea9e445 1423 int stripes_allocated = 8;
321aecc6 1424 int stripes_required = 1;
593060d7 1425 int stripe_index;
cea9e445 1426 int i;
f2d8d74d 1427 int num_stripes;
cea9e445 1428 struct btrfs_multi_bio *multi = NULL;
0b86a832 1429
cea9e445
CM
1430 if (multi_ret && !(rw & (1 << BIO_RW))) {
1431 stripes_allocated = 1;
1432 }
1433again:
1434 if (multi_ret) {
1435 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
1436 GFP_NOFS);
1437 if (!multi)
1438 return -ENOMEM;
1439 }
0b86a832
CM
1440
1441 spin_lock(&em_tree->lock);
1442 em = lookup_extent_mapping(em_tree, logical, *length);
b248a415 1443 spin_unlock(&em_tree->lock);
f2d8d74d
CM
1444
1445 if (!em && unplug_page)
1446 return 0;
1447
3b951516
CM
1448 if (!em) {
1449 printk("unable to find logical %Lu\n", logical);
f2d8d74d 1450 BUG();
3b951516 1451 }
0b86a832
CM
1452
1453 BUG_ON(em->start > logical || em->start + em->len < logical);
1454 map = (struct map_lookup *)em->bdev;
1455 offset = logical - em->start;
593060d7 1456
f188591e
CM
1457 if (mirror_num > map->num_stripes)
1458 mirror_num = 0;
1459
cea9e445 1460 /* if our multi bio struct is too small, back off and try again */
321aecc6
CM
1461 if (rw & (1 << BIO_RW)) {
1462 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
1463 BTRFS_BLOCK_GROUP_DUP)) {
1464 stripes_required = map->num_stripes;
1465 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1466 stripes_required = map->sub_stripes;
1467 }
1468 }
1469 if (multi_ret && rw == WRITE &&
1470 stripes_allocated < stripes_required) {
cea9e445 1471 stripes_allocated = map->num_stripes;
cea9e445
CM
1472 free_extent_map(em);
1473 kfree(multi);
1474 goto again;
1475 }
593060d7
CM
1476 stripe_nr = offset;
1477 /*
1478 * stripe_nr counts the total number of stripes we have to stride
1479 * to get to this block
1480 */
1481 do_div(stripe_nr, map->stripe_len);
1482
1483 stripe_offset = stripe_nr * map->stripe_len;
1484 BUG_ON(offset < stripe_offset);
1485
1486 /* stripe_offset is the offset of this block in its stripe*/
1487 stripe_offset = offset - stripe_offset;
1488
cea9e445 1489 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
321aecc6 1490 BTRFS_BLOCK_GROUP_RAID10 |
cea9e445
CM
1491 BTRFS_BLOCK_GROUP_DUP)) {
1492 /* we limit the length of each bio to what fits in a stripe */
1493 *length = min_t(u64, em->len - offset,
1494 map->stripe_len - stripe_offset);
1495 } else {
1496 *length = em->len - offset;
1497 }
f2d8d74d
CM
1498
1499 if (!multi_ret && !unplug_page)
cea9e445
CM
1500 goto out;
1501
f2d8d74d 1502 num_stripes = 1;
cea9e445 1503 stripe_index = 0;
8790d502 1504 if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
f2d8d74d
CM
1505 if (unplug_page || (rw & (1 << BIO_RW)))
1506 num_stripes = map->num_stripes;
f188591e
CM
1507 else if (mirror_num) {
1508 stripe_index = mirror_num - 1;
1509 } else {
3c12ac72
CM
1510 u64 orig_stripe_nr = stripe_nr;
1511 stripe_index = do_div(orig_stripe_nr, num_stripes);
8790d502 1512 }
611f0e00 1513 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
cea9e445 1514 if (rw & (1 << BIO_RW))
f2d8d74d 1515 num_stripes = map->num_stripes;
f188591e
CM
1516 else if (mirror_num)
1517 stripe_index = mirror_num - 1;
321aecc6
CM
1518 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1519 int factor = map->num_stripes / map->sub_stripes;
321aecc6
CM
1520
1521 stripe_index = do_div(stripe_nr, factor);
1522 stripe_index *= map->sub_stripes;
1523
f2d8d74d
CM
1524 if (unplug_page || (rw & (1 << BIO_RW)))
1525 num_stripes = map->sub_stripes;
321aecc6
CM
1526 else if (mirror_num)
1527 stripe_index += mirror_num - 1;
3c12ac72
CM
1528 else {
1529 u64 orig_stripe_nr = stripe_nr;
1530 stripe_index += do_div(orig_stripe_nr,
1531 map->sub_stripes);
1532 }
8790d502
CM
1533 } else {
1534 /*
1535 * after this do_div call, stripe_nr is the number of stripes
1536 * on this device we have to walk to find the data, and
1537 * stripe_index is the number of our device in the stripe array
1538 */
1539 stripe_index = do_div(stripe_nr, map->num_stripes);
1540 }
593060d7 1541 BUG_ON(stripe_index >= map->num_stripes);
cea9e445 1542
f2d8d74d
CM
1543 for (i = 0; i < num_stripes; i++) {
1544 if (unplug_page) {
1545 struct btrfs_device *device;
1546 struct backing_dev_info *bdi;
1547
1548 device = map->stripes[stripe_index].dev;
1549 bdi = blk_get_backing_dev_info(device->bdev);
1550 if (bdi->unplug_io_fn) {
1551 bdi->unplug_io_fn(bdi, unplug_page);
1552 }
1553 } else {
1554 multi->stripes[i].physical =
1555 map->stripes[stripe_index].physical +
1556 stripe_offset + stripe_nr * map->stripe_len;
1557 multi->stripes[i].dev = map->stripes[stripe_index].dev;
1558 }
cea9e445 1559 stripe_index++;
593060d7 1560 }
f2d8d74d
CM
1561 if (multi_ret) {
1562 *multi_ret = multi;
1563 multi->num_stripes = num_stripes;
1564 }
cea9e445 1565out:
0b86a832 1566 free_extent_map(em);
0b86a832
CM
1567 return 0;
1568}
1569
f2d8d74d
CM
1570int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
1571 u64 logical, u64 *length,
1572 struct btrfs_multi_bio **multi_ret, int mirror_num)
1573{
1574 return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
1575 mirror_num, NULL);
1576}
1577
1578int btrfs_unplug_page(struct btrfs_mapping_tree *map_tree,
1579 u64 logical, struct page *page)
1580{
1581 u64 length = PAGE_CACHE_SIZE;
1582 return __btrfs_map_block(map_tree, READ, logical, &length,
1583 NULL, 0, page);
1584}
1585
1586
8790d502
CM
1587#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
1588static void end_bio_multi_stripe(struct bio *bio, int err)
1589#else
1590static int end_bio_multi_stripe(struct bio *bio,
1591 unsigned int bytes_done, int err)
1592#endif
1593{
cea9e445 1594 struct btrfs_multi_bio *multi = bio->bi_private;
8790d502
CM
1595
1596#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1597 if (bio->bi_size)
1598 return 1;
1599#endif
1600 if (err)
1601 multi->error = err;
1602
cea9e445 1603 if (atomic_dec_and_test(&multi->stripes_pending)) {
8790d502
CM
1604 bio->bi_private = multi->private;
1605 bio->bi_end_io = multi->end_io;
1606
1607 if (!err && multi->error)
1608 err = multi->error;
1609 kfree(multi);
1610
73f61b2a
M
1611#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1612 bio_endio(bio, bio->bi_size, err);
1613#else
8790d502 1614 bio_endio(bio, err);
73f61b2a 1615#endif
8790d502
CM
1616 } else {
1617 bio_put(bio);
1618 }
1619#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1620 return 0;
1621#endif
1622}
1623
f188591e
CM
1624int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
1625 int mirror_num)
0b86a832
CM
1626{
1627 struct btrfs_mapping_tree *map_tree;
1628 struct btrfs_device *dev;
8790d502 1629 struct bio *first_bio = bio;
0b86a832 1630 u64 logical = bio->bi_sector << 9;
0b86a832
CM
1631 u64 length = 0;
1632 u64 map_length;
cea9e445 1633 struct btrfs_multi_bio *multi = NULL;
0b86a832 1634 int ret;
8790d502
CM
1635 int dev_nr = 0;
1636 int total_devs = 1;
0b86a832 1637
f2d8d74d 1638 length = bio->bi_size;
0b86a832
CM
1639 map_tree = &root->fs_info->mapping_tree;
1640 map_length = length;
cea9e445 1641
f188591e
CM
1642 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
1643 mirror_num);
cea9e445
CM
1644 BUG_ON(ret);
1645
1646 total_devs = multi->num_stripes;
1647 if (map_length < length) {
1648 printk("mapping failed logical %Lu bio len %Lu "
1649 "len %Lu\n", logical, length, map_length);
1650 BUG();
1651 }
1652 multi->end_io = first_bio->bi_end_io;
1653 multi->private = first_bio->bi_private;
1654 atomic_set(&multi->stripes_pending, multi->num_stripes);
1655
8790d502 1656 while(dev_nr < total_devs) {
8790d502 1657 if (total_devs > 1) {
8790d502
CM
1658 if (dev_nr < total_devs - 1) {
1659 bio = bio_clone(first_bio, GFP_NOFS);
1660 BUG_ON(!bio);
1661 } else {
1662 bio = first_bio;
1663 }
1664 bio->bi_private = multi;
1665 bio->bi_end_io = end_bio_multi_stripe;
1666 }
cea9e445
CM
1667 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
1668 dev = multi->stripes[dev_nr].dev;
e1c4b745 1669
8790d502
CM
1670 bio->bi_bdev = dev->bdev;
1671 spin_lock(&dev->io_lock);
1672 dev->total_ios++;
1673 spin_unlock(&dev->io_lock);
1674 submit_bio(rw, bio);
1675 dev_nr++;
1676 }
cea9e445
CM
1677 if (total_devs == 1)
1678 kfree(multi);
0b86a832
CM
1679 return 0;
1680}
1681
a443755f
CM
1682struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
1683 u8 *uuid)
0b86a832 1684{
8a4b83cc 1685 struct list_head *head = &root->fs_info->fs_devices->devices;
0b86a832 1686
a443755f 1687 return __find_device(head, devid, uuid);
0b86a832
CM
1688}
1689
1690static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
1691 struct extent_buffer *leaf,
1692 struct btrfs_chunk *chunk)
1693{
1694 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
1695 struct map_lookup *map;
1696 struct extent_map *em;
1697 u64 logical;
1698 u64 length;
1699 u64 devid;
a443755f 1700 u8 uuid[BTRFS_UUID_SIZE];
593060d7 1701 int num_stripes;
0b86a832 1702 int ret;
593060d7 1703 int i;
0b86a832 1704
e17cade2
CM
1705 logical = key->offset;
1706 length = btrfs_chunk_length(leaf, chunk);
0b86a832
CM
1707 spin_lock(&map_tree->map_tree.lock);
1708 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
b248a415 1709 spin_unlock(&map_tree->map_tree.lock);
0b86a832
CM
1710
1711 /* already mapped? */
1712 if (em && em->start <= logical && em->start + em->len > logical) {
1713 free_extent_map(em);
0b86a832
CM
1714 return 0;
1715 } else if (em) {
1716 free_extent_map(em);
1717 }
0b86a832
CM
1718
1719 map = kzalloc(sizeof(*map), GFP_NOFS);
1720 if (!map)
1721 return -ENOMEM;
1722
1723 em = alloc_extent_map(GFP_NOFS);
1724 if (!em)
1725 return -ENOMEM;
593060d7
CM
1726 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
1727 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
0b86a832
CM
1728 if (!map) {
1729 free_extent_map(em);
1730 return -ENOMEM;
1731 }
1732
1733 em->bdev = (struct block_device *)map;
1734 em->start = logical;
1735 em->len = length;
1736 em->block_start = 0;
1737
593060d7
CM
1738 map->num_stripes = num_stripes;
1739 map->io_width = btrfs_chunk_io_width(leaf, chunk);
1740 map->io_align = btrfs_chunk_io_align(leaf, chunk);
1741 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
1742 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
1743 map->type = btrfs_chunk_type(leaf, chunk);
321aecc6 1744 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
593060d7
CM
1745 for (i = 0; i < num_stripes; i++) {
1746 map->stripes[i].physical =
1747 btrfs_stripe_offset_nr(leaf, chunk, i);
1748 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
a443755f
CM
1749 read_extent_buffer(leaf, uuid, (unsigned long)
1750 btrfs_stripe_dev_uuid_nr(chunk, i),
1751 BTRFS_UUID_SIZE);
1752 map->stripes[i].dev = btrfs_find_device(root, devid, uuid);
593060d7
CM
1753 if (!map->stripes[i].dev) {
1754 kfree(map);
1755 free_extent_map(em);
1756 return -EIO;
1757 }
0b86a832
CM
1758 }
1759
1760 spin_lock(&map_tree->map_tree.lock);
1761 ret = add_extent_mapping(&map_tree->map_tree, em);
0b86a832 1762 spin_unlock(&map_tree->map_tree.lock);
b248a415 1763 BUG_ON(ret);
0b86a832
CM
1764 free_extent_map(em);
1765
1766 return 0;
1767}
1768
1769static int fill_device_from_item(struct extent_buffer *leaf,
1770 struct btrfs_dev_item *dev_item,
1771 struct btrfs_device *device)
1772{
1773 unsigned long ptr;
0b86a832
CM
1774
1775 device->devid = btrfs_device_id(leaf, dev_item);
1776 device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
1777 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
1778 device->type = btrfs_device_type(leaf, dev_item);
1779 device->io_align = btrfs_device_io_align(leaf, dev_item);
1780 device->io_width = btrfs_device_io_width(leaf, dev_item);
1781 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
0b86a832
CM
1782
1783 ptr = (unsigned long)btrfs_device_uuid(dev_item);
e17cade2 1784 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
0b86a832 1785
0b86a832
CM
1786 return 0;
1787}
1788
0d81ba5d 1789static int read_one_dev(struct btrfs_root *root,
0b86a832
CM
1790 struct extent_buffer *leaf,
1791 struct btrfs_dev_item *dev_item)
1792{
1793 struct btrfs_device *device;
1794 u64 devid;
1795 int ret;
a443755f
CM
1796 u8 dev_uuid[BTRFS_UUID_SIZE];
1797
0b86a832 1798 devid = btrfs_device_id(leaf, dev_item);
a443755f
CM
1799 read_extent_buffer(leaf, dev_uuid,
1800 (unsigned long)btrfs_device_uuid(dev_item),
1801 BTRFS_UUID_SIZE);
1802 device = btrfs_find_device(root, devid, dev_uuid);
6324fbf3 1803 if (!device) {
8a4b83cc 1804 printk("warning devid %Lu not found already\n", devid);
f2984462 1805 device = kzalloc(sizeof(*device), GFP_NOFS);
6324fbf3
CM
1806 if (!device)
1807 return -ENOMEM;
8a4b83cc
CM
1808 list_add(&device->dev_list,
1809 &root->fs_info->fs_devices->devices);
b3075717
CM
1810 list_add(&device->dev_alloc_list,
1811 &root->fs_info->fs_devices->alloc_list);
b248a415 1812 device->barriers = 1;
8790d502 1813 spin_lock_init(&device->io_lock);
6324fbf3 1814 }
0b86a832
CM
1815
1816 fill_device_from_item(leaf, dev_item, device);
1817 device->dev_root = root->fs_info->dev_root;
0b86a832
CM
1818 ret = 0;
1819#if 0
1820 ret = btrfs_open_device(device);
1821 if (ret) {
1822 kfree(device);
1823 }
1824#endif
1825 return ret;
1826}
1827
0d81ba5d
CM
1828int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
1829{
1830 struct btrfs_dev_item *dev_item;
1831
1832 dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
1833 dev_item);
1834 return read_one_dev(root, buf, dev_item);
1835}
1836
0b86a832
CM
1837int btrfs_read_sys_array(struct btrfs_root *root)
1838{
1839 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1840 struct extent_buffer *sb = root->fs_info->sb_buffer;
1841 struct btrfs_disk_key *disk_key;
0b86a832 1842 struct btrfs_chunk *chunk;
84eed90f
CM
1843 u8 *ptr;
1844 unsigned long sb_ptr;
1845 int ret = 0;
0b86a832
CM
1846 u32 num_stripes;
1847 u32 array_size;
1848 u32 len = 0;
0b86a832 1849 u32 cur;
84eed90f 1850 struct btrfs_key key;
0b86a832
CM
1851
1852 array_size = btrfs_super_sys_array_size(super_copy);
1853
0b86a832
CM
1854 ptr = super_copy->sys_chunk_array;
1855 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
1856 cur = 0;
1857
1858 while (cur < array_size) {
1859 disk_key = (struct btrfs_disk_key *)ptr;
1860 btrfs_disk_key_to_cpu(&key, disk_key);
1861
1862 len = sizeof(*disk_key);
1863 ptr += len;
1864 sb_ptr += len;
1865 cur += len;
1866
0d81ba5d 1867 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
0b86a832 1868 chunk = (struct btrfs_chunk *)sb_ptr;
0d81ba5d 1869 ret = read_one_chunk(root, &key, sb, chunk);
84eed90f
CM
1870 if (ret)
1871 break;
0b86a832
CM
1872 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
1873 len = btrfs_chunk_item_size(num_stripes);
1874 } else {
84eed90f
CM
1875 ret = -EIO;
1876 break;
0b86a832
CM
1877 }
1878 ptr += len;
1879 sb_ptr += len;
1880 cur += len;
1881 }
84eed90f 1882 return ret;
0b86a832
CM
1883}
1884
1885int btrfs_read_chunk_tree(struct btrfs_root *root)
1886{
1887 struct btrfs_path *path;
1888 struct extent_buffer *leaf;
1889 struct btrfs_key key;
1890 struct btrfs_key found_key;
1891 int ret;
1892 int slot;
1893
1894 root = root->fs_info->chunk_root;
1895
1896 path = btrfs_alloc_path();
1897 if (!path)
1898 return -ENOMEM;
1899
1900 /* first we search for all of the device items, and then we
1901 * read in all of the chunk items. This way we can create chunk
1902 * mappings that reference all of the devices that are afound
1903 */
1904 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1905 key.offset = 0;
1906 key.type = 0;
1907again:
1908 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1909 while(1) {
1910 leaf = path->nodes[0];
1911 slot = path->slots[0];
1912 if (slot >= btrfs_header_nritems(leaf)) {
1913 ret = btrfs_next_leaf(root, path);
1914 if (ret == 0)
1915 continue;
1916 if (ret < 0)
1917 goto error;
1918 break;
1919 }
1920 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1921 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
1922 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
1923 break;
1924 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
1925 struct btrfs_dev_item *dev_item;
1926 dev_item = btrfs_item_ptr(leaf, slot,
1927 struct btrfs_dev_item);
0d81ba5d 1928 ret = read_one_dev(root, leaf, dev_item);
0b86a832
CM
1929 BUG_ON(ret);
1930 }
1931 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
1932 struct btrfs_chunk *chunk;
1933 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
1934 ret = read_one_chunk(root, &found_key, leaf, chunk);
1935 }
1936 path->slots[0]++;
1937 }
1938 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
1939 key.objectid = 0;
1940 btrfs_release_path(root, path);
1941 goto again;
1942 }
1943
1944 btrfs_free_path(path);
1945 ret = 0;
1946error:
1947 return ret;
1948}
1949