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