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