mkfs.btrfs: free buffers allocated by pretty_sizes
[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 calc_size = 8 * 1024 * 1024;
647         u64 min_free;
648         u64 max_chunk_size = 4 * calc_size;
649         u64 avail;
650         u64 max_avail = 0;
651         u64 percent_max;
652         int num_stripes = 1;
653         int min_stripes = 1;
654         int sub_stripes = 0;
655         int looped = 0;
656         int ret;
657         int index;
658         int stripe_len = 64 * 1024;
659         struct btrfs_key key;
660
661         if (list_empty(dev_list)) {
662                 return -ENOSPC;
663         }
664
665         if (type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
666                     BTRFS_BLOCK_GROUP_RAID10 |
667                     BTRFS_BLOCK_GROUP_DUP)) {
668                 if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
669                         calc_size = 8 * 1024 * 1024;
670                         max_chunk_size = calc_size * 2;
671                         min_stripe_size = 1 * 1024 * 1024;
672                 } else if (type & BTRFS_BLOCK_GROUP_DATA) {
673                         calc_size = 1024 * 1024 * 1024;
674                         max_chunk_size = 10 * calc_size;
675                         min_stripe_size = 64 * 1024 * 1024;
676                 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
677                         calc_size = 1024 * 1024 * 1024;
678                         max_chunk_size = 4 * calc_size;
679                         min_stripe_size = 32 * 1024 * 1024;
680                 }
681         }
682         if (type & BTRFS_BLOCK_GROUP_RAID1) {
683                 num_stripes = min_t(u64, 2,
684                                   btrfs_super_num_devices(&info->super_copy));
685                 if (num_stripes < 2)
686                         return -ENOSPC;
687                 min_stripes = 2;
688         }
689         if (type & BTRFS_BLOCK_GROUP_DUP) {
690                 num_stripes = 2;
691                 min_stripes = 2;
692         }
693         if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
694                 num_stripes = btrfs_super_num_devices(&info->super_copy);
695                 min_stripes = 2;
696         }
697         if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
698                 num_stripes = btrfs_super_num_devices(&info->super_copy);
699                 if (num_stripes < 4)
700                         return -ENOSPC;
701                 num_stripes &= ~(u32)1;
702                 sub_stripes = 2;
703                 min_stripes = 4;
704         }
705
706         /* we don't want a chunk larger than 10% of the FS */
707         percent_max = div_factor(btrfs_super_total_bytes(&info->super_copy), 1);
708         max_chunk_size = min(percent_max, max_chunk_size);
709
710 again:
711         if (chunk_bytes_by_type(type, calc_size, num_stripes, sub_stripes) >
712             max_chunk_size) {
713                 calc_size = max_chunk_size;
714                 calc_size /= num_stripes;
715                 calc_size /= stripe_len;
716                 calc_size *= stripe_len;
717         }
718         /* we don't want tiny stripes */
719         calc_size = max_t(u64, calc_size, min_stripe_size);
720
721         calc_size /= stripe_len;
722         calc_size *= stripe_len;
723         INIT_LIST_HEAD(&private_devs);
724         cur = dev_list->next;
725         index = 0;
726
727         if (type & BTRFS_BLOCK_GROUP_DUP)
728                 min_free = calc_size * 2;
729         else
730                 min_free = calc_size;
731
732         /* build a private list of devices we will allocate from */
733         while(index < num_stripes) {
734                 device = list_entry(cur, struct btrfs_device, dev_list);
735                 avail = device->total_bytes - device->bytes_used;
736                 cur = cur->next;
737                 if (avail >= min_free) {
738                         list_move_tail(&device->dev_list, &private_devs);
739                         index++;
740                         if (type & BTRFS_BLOCK_GROUP_DUP)
741                                 index++;
742                 } else if (avail > max_avail)
743                         max_avail = avail;
744                 if (cur == dev_list)
745                         break;
746         }
747         if (index < num_stripes) {
748                 list_splice(&private_devs, dev_list);
749                 if (index >= min_stripes) {
750                         num_stripes = index;
751                         if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
752                                 num_stripes /= sub_stripes;
753                                 num_stripes *= sub_stripes;
754                         }
755                         looped = 1;
756                         goto again;
757                 }
758                 if (!looped && max_avail > 0) {
759                         looped = 1;
760                         calc_size = max_avail;
761                         goto again;
762                 }
763                 return -ENOSPC;
764         }
765         key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
766         key.type = BTRFS_CHUNK_ITEM_KEY;
767         ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
768                               &key.offset);
769         if (ret)
770                 return ret;
771
772         chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS);
773         if (!chunk)
774                 return -ENOMEM;
775
776         map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
777         if (!map) {
778                 kfree(chunk);
779                 return -ENOMEM;
780         }
781
782         stripes = &chunk->stripe;
783         *num_bytes = chunk_bytes_by_type(type, calc_size,
784                                          num_stripes, sub_stripes);
785         index = 0;
786         while(index < num_stripes) {
787                 struct btrfs_stripe *stripe;
788                 BUG_ON(list_empty(&private_devs));
789                 cur = private_devs.next;
790                 device = list_entry(cur, struct btrfs_device, dev_list);
791
792                 /* loop over this device again if we're doing a dup group */
793                 if (!(type & BTRFS_BLOCK_GROUP_DUP) ||
794                     (index == num_stripes - 1))
795                         list_move_tail(&device->dev_list, dev_list);
796
797                 ret = btrfs_alloc_dev_extent(trans, device,
798                              info->chunk_root->root_key.objectid,
799                              BTRFS_FIRST_CHUNK_TREE_OBJECTID, key.offset,
800                              calc_size, &dev_offset);
801                 BUG_ON(ret);
802
803                 device->bytes_used += calc_size;
804                 ret = btrfs_update_device(trans, device);
805                 BUG_ON(ret);
806
807                 map->stripes[index].dev = device;
808                 map->stripes[index].physical = dev_offset;
809                 stripe = stripes + index;
810                 btrfs_set_stack_stripe_devid(stripe, device->devid);
811                 btrfs_set_stack_stripe_offset(stripe, dev_offset);
812                 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
813                 index++;
814         }
815         BUG_ON(!list_empty(&private_devs));
816
817         /* key was set above */
818         btrfs_set_stack_chunk_length(chunk, *num_bytes);
819         btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
820         btrfs_set_stack_chunk_stripe_len(chunk, stripe_len);
821         btrfs_set_stack_chunk_type(chunk, type);
822         btrfs_set_stack_chunk_num_stripes(chunk, num_stripes);
823         btrfs_set_stack_chunk_io_align(chunk, stripe_len);
824         btrfs_set_stack_chunk_io_width(chunk, stripe_len);
825         btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
826         btrfs_set_stack_chunk_sub_stripes(chunk, sub_stripes);
827         map->sector_size = extent_root->sectorsize;
828         map->stripe_len = stripe_len;
829         map->io_align = stripe_len;
830         map->io_width = stripe_len;
831         map->type = type;
832         map->num_stripes = num_stripes;
833         map->sub_stripes = sub_stripes;
834
835         ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
836                                 btrfs_chunk_item_size(num_stripes));
837         BUG_ON(ret);
838         *start = key.offset;;
839
840         map->ce.start = key.offset;
841         map->ce.size = *num_bytes;
842
843         ret = insert_existing_cache_extent(
844                            &extent_root->fs_info->mapping_tree.cache_tree,
845                            &map->ce);
846         BUG_ON(ret);
847
848         if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
849                 ret = btrfs_add_system_chunk(trans, chunk_root, &key,
850                                     chunk, btrfs_chunk_item_size(num_stripes));
851                 BUG_ON(ret);
852         }
853
854         kfree(chunk);
855         return ret;
856 }
857
858 int btrfs_alloc_data_chunk(struct btrfs_trans_handle *trans,
859                            struct btrfs_root *extent_root, u64 *start,
860                            u64 num_bytes, u64 type)
861 {
862         u64 dev_offset;
863         struct btrfs_fs_info *info = extent_root->fs_info;
864         struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
865         struct btrfs_stripe *stripes;
866         struct btrfs_device *device = NULL;
867         struct btrfs_chunk *chunk;
868         struct list_head *dev_list = &extent_root->fs_info->fs_devices->devices;
869         struct list_head *cur;
870         struct map_lookup *map;
871         u64 physical;
872         u64 calc_size = 8 * 1024 * 1024;
873         int num_stripes = 1;
874         int sub_stripes = 0;
875         int ret;
876         int index;
877         int stripe_len = 64 * 1024;
878         struct btrfs_key key;
879
880         key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
881         key.type = BTRFS_CHUNK_ITEM_KEY;
882         ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
883                               &key.offset);
884         if (ret)
885                 return ret;
886
887         chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS);
888         if (!chunk)
889                 return -ENOMEM;
890
891         map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
892         if (!map) {
893                 kfree(chunk);
894                 return -ENOMEM;
895         }
896
897         stripes = &chunk->stripe;
898         calc_size = num_bytes;
899
900         index = 0;
901         cur = dev_list->next;
902         device = list_entry(cur, struct btrfs_device, dev_list);
903
904         while (index < num_stripes) {
905                 struct btrfs_stripe *stripe;
906
907                 ret = btrfs_alloc_dev_extent(trans, device,
908                              info->chunk_root->root_key.objectid,
909                              BTRFS_FIRST_CHUNK_TREE_OBJECTID, key.offset,
910                              calc_size, &dev_offset);
911                 BUG_ON(ret);
912
913                 device->bytes_used += calc_size;
914                 ret = btrfs_update_device(trans, device);
915                 BUG_ON(ret);
916
917                 map->stripes[index].dev = device;
918                 map->stripes[index].physical = dev_offset;
919                 stripe = stripes + index;
920                 btrfs_set_stack_stripe_devid(stripe, device->devid);
921                 btrfs_set_stack_stripe_offset(stripe, dev_offset);
922                 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
923                 physical = dev_offset;
924                 index++;
925         }
926
927         /* key was set above */
928         btrfs_set_stack_chunk_length(chunk, num_bytes);
929         btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
930         btrfs_set_stack_chunk_stripe_len(chunk, stripe_len);
931         btrfs_set_stack_chunk_type(chunk, type);
932         btrfs_set_stack_chunk_num_stripes(chunk, num_stripes);
933         btrfs_set_stack_chunk_io_align(chunk, stripe_len);
934         btrfs_set_stack_chunk_io_width(chunk, stripe_len);
935         btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
936         btrfs_set_stack_chunk_sub_stripes(chunk, sub_stripes);
937         map->sector_size = extent_root->sectorsize;
938         map->stripe_len = stripe_len;
939         map->io_align = stripe_len;
940         map->io_width = stripe_len;
941         map->type = type;
942         map->num_stripes = num_stripes;
943         map->sub_stripes = sub_stripes;
944
945         ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
946                                 btrfs_chunk_item_size(num_stripes));
947         BUG_ON(ret);
948         *start = key.offset;
949
950         map->ce.start = key.offset;
951         map->ce.size = num_bytes;
952
953         ret = insert_existing_cache_extent(
954                            &extent_root->fs_info->mapping_tree.cache_tree,
955                            &map->ce);
956         BUG_ON(ret);
957
958         kfree(chunk);
959         return ret;
960 }
961
962 void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
963 {
964         cache_tree_init(&tree->cache_tree);
965 }
966
967 int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
968 {
969         struct cache_extent *ce;
970         struct map_lookup *map;
971         int ret;
972
973         ce = find_first_cache_extent(&map_tree->cache_tree, logical);
974         BUG_ON(!ce);
975         BUG_ON(ce->start > logical || ce->start + ce->size < logical);
976         map = container_of(ce, struct map_lookup, ce);
977
978         if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
979                 ret = map->num_stripes;
980         else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
981                 ret = map->sub_stripes;
982         else
983                 ret = 1;
984         return ret;
985 }
986
987 int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
988                      u64 chunk_start, u64 physical, u64 devid,
989                      u64 **logical, int *naddrs, int *stripe_len)
990 {
991         struct cache_extent *ce;
992         struct map_lookup *map;
993         u64 *buf;
994         u64 bytenr;
995         u64 length;
996         u64 stripe_nr;
997         int i, j, nr = 0;
998
999         ce = find_first_cache_extent(&map_tree->cache_tree, chunk_start);
1000         BUG_ON(!ce);
1001         map = container_of(ce, struct map_lookup, ce);
1002
1003         length = ce->size;
1004         if (map->type & BTRFS_BLOCK_GROUP_RAID10)
1005                 length = ce->size / (map->num_stripes / map->sub_stripes);
1006         else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
1007                 length = ce->size / map->num_stripes;
1008
1009         buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
1010
1011         for (i = 0; i < map->num_stripes; i++) {
1012                 if (devid && map->stripes[i].dev->devid != devid)
1013                         continue;
1014                 if (map->stripes[i].physical > physical ||
1015                     map->stripes[i].physical + length <= physical)
1016                         continue;
1017
1018                 stripe_nr = (physical - map->stripes[i].physical) /
1019                             map->stripe_len;
1020
1021                 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1022                         stripe_nr = (stripe_nr * map->num_stripes + i) /
1023                                     map->sub_stripes;
1024                 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
1025                         stripe_nr = stripe_nr * map->num_stripes + i;
1026                 }
1027                 bytenr = ce->start + stripe_nr * map->stripe_len;
1028                 for (j = 0; j < nr; j++) {
1029                         if (buf[j] == bytenr)
1030                                 break;
1031                 }
1032                 if (j == nr)
1033                         buf[nr++] = bytenr;
1034         }
1035
1036         *logical = buf;
1037         *naddrs = nr;
1038         *stripe_len = map->stripe_len;
1039
1040         return 0;
1041 }
1042
1043 int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
1044                     u64 logical, u64 *length,
1045                     struct btrfs_multi_bio **multi_ret, int mirror_num)
1046 {
1047         struct cache_extent *ce;
1048         struct map_lookup *map;
1049         u64 offset;
1050         u64 stripe_offset;
1051         u64 stripe_nr;
1052         int stripes_allocated = 8;
1053         int stripes_required = 1;
1054         int stripe_index;
1055         int i;
1056         struct btrfs_multi_bio *multi = NULL;
1057
1058         if (multi_ret && rw == READ) {
1059                 stripes_allocated = 1;
1060         }
1061 again:
1062         if (multi_ret) {
1063                 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
1064                                 GFP_NOFS);
1065                 if (!multi)
1066                         return -ENOMEM;
1067         }
1068
1069         ce = find_first_cache_extent(&map_tree->cache_tree, logical);
1070         BUG_ON(!ce);
1071         BUG_ON(ce->start > logical || ce->start + ce->size < logical);
1072         map = container_of(ce, struct map_lookup, ce);
1073         offset = logical - ce->start;
1074
1075         if (rw == WRITE) {
1076                 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
1077                                  BTRFS_BLOCK_GROUP_DUP)) {
1078                         stripes_required = map->num_stripes;
1079                 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1080                         stripes_required = map->sub_stripes;
1081                 }
1082         }
1083         /* if our multi bio struct is too small, back off and try again */
1084         if (multi_ret && rw == WRITE &&
1085             stripes_allocated < stripes_required) {
1086                 stripes_allocated = map->num_stripes;
1087                 kfree(multi);
1088                 goto again;
1089         }
1090         stripe_nr = offset;
1091         /*
1092          * stripe_nr counts the total number of stripes we have to stride
1093          * to get to this block
1094          */
1095         stripe_nr = stripe_nr / map->stripe_len;
1096
1097         stripe_offset = stripe_nr * map->stripe_len;
1098         BUG_ON(offset < stripe_offset);
1099
1100         /* stripe_offset is the offset of this block in its stripe*/
1101         stripe_offset = offset - stripe_offset;
1102
1103         if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
1104                          BTRFS_BLOCK_GROUP_RAID10 |
1105                          BTRFS_BLOCK_GROUP_DUP)) {
1106                 /* we limit the length of each bio to what fits in a stripe */
1107                 *length = min_t(u64, ce->size - offset,
1108                               map->stripe_len - stripe_offset);
1109         } else {
1110                 *length = ce->size - offset;
1111         }
1112
1113         if (!multi_ret)
1114                 goto out;
1115
1116         multi->num_stripes = 1;
1117         stripe_index = 0;
1118         if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
1119                 if (rw == WRITE)
1120                         multi->num_stripes = map->num_stripes;
1121                 else if (mirror_num)
1122                         stripe_index = mirror_num - 1;
1123                 else
1124                         stripe_index = stripe_nr % map->num_stripes;
1125         } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1126                 int factor = map->num_stripes / map->sub_stripes;
1127
1128                 stripe_index = stripe_nr % factor;
1129                 stripe_index *= map->sub_stripes;
1130
1131                 if (rw == WRITE)
1132                         multi->num_stripes = map->sub_stripes;
1133                 else if (mirror_num)
1134                         stripe_index += mirror_num - 1;
1135                 else
1136                         stripe_index = stripe_nr % map->sub_stripes;
1137
1138                 stripe_nr = stripe_nr / factor;
1139         } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
1140                 if (rw == WRITE)
1141                         multi->num_stripes = map->num_stripes;
1142                 else if (mirror_num)
1143                         stripe_index = mirror_num - 1;
1144         } else {
1145                 /*
1146                  * after this do_div call, stripe_nr is the number of stripes
1147                  * on this device we have to walk to find the data, and
1148                  * stripe_index is the number of our device in the stripe array
1149                  */
1150                 stripe_index = stripe_nr % map->num_stripes;
1151                 stripe_nr = stripe_nr / map->num_stripes;
1152         }
1153         BUG_ON(stripe_index >= map->num_stripes);
1154
1155         for (i = 0; i < multi->num_stripes; i++) {
1156                 multi->stripes[i].physical =
1157                         map->stripes[stripe_index].physical + stripe_offset +
1158                         stripe_nr * map->stripe_len;
1159                 multi->stripes[i].dev = map->stripes[stripe_index].dev;
1160                 stripe_index++;
1161         }
1162         *multi_ret = multi;
1163 out:
1164         return 0;
1165 }
1166
1167 struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
1168                                        u8 *uuid, u8 *fsid)
1169 {
1170         struct btrfs_device *device;
1171         struct btrfs_fs_devices *cur_devices;
1172
1173         cur_devices = root->fs_info->fs_devices;
1174         while (cur_devices) {
1175                 if (!fsid ||
1176                     !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
1177                         device = __find_device(&cur_devices->devices,
1178                                                devid, uuid);
1179                         if (device)
1180                                 return device;
1181                 }
1182                 cur_devices = cur_devices->seed;
1183         }
1184         return NULL;
1185 }
1186
1187 int btrfs_bootstrap_super_map(struct btrfs_mapping_tree *map_tree,
1188                               struct btrfs_fs_devices *fs_devices)
1189 {
1190         struct map_lookup *map;
1191         u64 logical = BTRFS_SUPER_INFO_OFFSET;
1192         u64 length = BTRFS_SUPER_INFO_SIZE;
1193         int num_stripes = 0;
1194         int sub_stripes = 0;
1195         int ret;
1196         int i;
1197         struct list_head *cur;
1198
1199         list_for_each(cur, &fs_devices->devices) {
1200                 num_stripes++;
1201         }
1202         map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
1203         if (!map)
1204                 return -ENOMEM;
1205
1206         map->ce.start = logical;
1207         map->ce.size = length;
1208         map->num_stripes = num_stripes;
1209         map->sub_stripes = sub_stripes;
1210         map->io_width = length;
1211         map->io_align = length;
1212         map->sector_size = length;
1213         map->stripe_len = length;
1214         map->type = BTRFS_BLOCK_GROUP_RAID1;
1215
1216         i = 0;
1217         list_for_each(cur, &fs_devices->devices) {
1218                 struct btrfs_device *device = list_entry(cur,
1219                                                          struct btrfs_device,
1220                                                          dev_list);
1221                 map->stripes[i].physical = logical;
1222                 map->stripes[i].dev = device;
1223                 i++;
1224         }
1225         ret = insert_existing_cache_extent(&map_tree->cache_tree, &map->ce);
1226         if (ret == -EEXIST) {
1227                 struct cache_extent *old;
1228                 struct map_lookup *old_map;
1229                 old = find_cache_extent(&map_tree->cache_tree, logical, length);
1230                 old_map = container_of(old, struct map_lookup, ce);
1231                 remove_cache_extent(&map_tree->cache_tree, old);
1232                 kfree(old_map);
1233                 ret = insert_existing_cache_extent(&map_tree->cache_tree,
1234                                                    &map->ce);
1235         }
1236         BUG_ON(ret);
1237         return 0;
1238 }
1239
1240 int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
1241 {
1242         struct cache_extent *ce;
1243         struct map_lookup *map;
1244         struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
1245         int readonly = 0;
1246         int i;
1247
1248         ce = find_first_cache_extent(&map_tree->cache_tree, chunk_offset);
1249         BUG_ON(!ce);
1250
1251         map = container_of(ce, struct map_lookup, ce);
1252         for (i = 0; i < map->num_stripes; i++) {
1253                 if (!map->stripes[i].dev->writeable) {
1254                         readonly = 1;
1255                         break;
1256                 }
1257         }
1258
1259         return readonly;
1260 }
1261
1262 static struct btrfs_device *fill_missing_device(u64 devid)
1263 {
1264         struct btrfs_device *device;
1265
1266         device = kzalloc(sizeof(*device), GFP_NOFS);
1267         device->devid = devid;
1268         device->fd = -1;
1269         return device;
1270 }
1271
1272 static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
1273                           struct extent_buffer *leaf,
1274                           struct btrfs_chunk *chunk)
1275 {
1276         struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
1277         struct map_lookup *map;
1278         struct cache_extent *ce;
1279         u64 logical;
1280         u64 length;
1281         u64 devid;
1282         u8 uuid[BTRFS_UUID_SIZE];
1283         int num_stripes;
1284         int ret;
1285         int i;
1286
1287         logical = key->offset;
1288         length = btrfs_chunk_length(leaf, chunk);
1289
1290         ce = find_first_cache_extent(&map_tree->cache_tree, logical);
1291
1292         /* already mapped? */
1293         if (ce && ce->start <= logical && ce->start + ce->size > logical) {
1294                 return 0;
1295         }
1296
1297         num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
1298         map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
1299         if (!map)
1300                 return -ENOMEM;
1301
1302         map->ce.start = logical;
1303         map->ce.size = length;
1304         map->num_stripes = num_stripes;
1305         map->io_width = btrfs_chunk_io_width(leaf, chunk);
1306         map->io_align = btrfs_chunk_io_align(leaf, chunk);
1307         map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
1308         map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
1309         map->type = btrfs_chunk_type(leaf, chunk);
1310         map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
1311
1312         for (i = 0; i < num_stripes; i++) {
1313                 map->stripes[i].physical =
1314                         btrfs_stripe_offset_nr(leaf, chunk, i);
1315                 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
1316                 read_extent_buffer(leaf, uuid, (unsigned long)
1317                                    btrfs_stripe_dev_uuid_nr(chunk, i),
1318                                    BTRFS_UUID_SIZE);
1319                 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
1320                                                         NULL);
1321                 if (!map->stripes[i].dev) {
1322                         map->stripes[i].dev = fill_missing_device(devid);
1323                         printf("warning, device %llu is missing\n",
1324                                (unsigned long long)devid);
1325                 }
1326
1327         }
1328         ret = insert_existing_cache_extent(&map_tree->cache_tree, &map->ce);
1329         BUG_ON(ret);
1330
1331         return 0;
1332 }
1333
1334 static int fill_device_from_item(struct extent_buffer *leaf,
1335                                  struct btrfs_dev_item *dev_item,
1336                                  struct btrfs_device *device)
1337 {
1338         unsigned long ptr;
1339
1340         device->devid = btrfs_device_id(leaf, dev_item);
1341         device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
1342         device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
1343         device->type = btrfs_device_type(leaf, dev_item);
1344         device->io_align = btrfs_device_io_align(leaf, dev_item);
1345         device->io_width = btrfs_device_io_width(leaf, dev_item);
1346         device->sector_size = btrfs_device_sector_size(leaf, dev_item);
1347
1348         ptr = (unsigned long)btrfs_device_uuid(dev_item);
1349         read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1350
1351         return 0;
1352 }
1353
1354 static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
1355 {
1356         struct btrfs_fs_devices *fs_devices;
1357         int ret;
1358
1359         fs_devices = root->fs_info->fs_devices->seed;
1360         while (fs_devices) {
1361                 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
1362                         ret = 0;
1363                         goto out;
1364                 }
1365                 fs_devices = fs_devices->seed;
1366         }
1367
1368         fs_devices = find_fsid(fsid);
1369         if (!fs_devices) {
1370                 ret = -ENOENT;
1371                 goto out;
1372         }
1373
1374         ret = btrfs_open_devices(fs_devices, O_RDONLY);
1375         if (ret)
1376                 goto out;
1377
1378         fs_devices->seed = root->fs_info->fs_devices->seed;
1379         root->fs_info->fs_devices->seed = fs_devices;
1380 out:
1381         return ret;
1382 }
1383
1384 static int read_one_dev(struct btrfs_root *root,
1385                         struct extent_buffer *leaf,
1386                         struct btrfs_dev_item *dev_item)
1387 {
1388         struct btrfs_device *device;
1389         u64 devid;
1390         int ret = 0;
1391         u8 fs_uuid[BTRFS_UUID_SIZE];
1392         u8 dev_uuid[BTRFS_UUID_SIZE];
1393
1394         devid = btrfs_device_id(leaf, dev_item);
1395         read_extent_buffer(leaf, dev_uuid,
1396                            (unsigned long)btrfs_device_uuid(dev_item),
1397                            BTRFS_UUID_SIZE);
1398         read_extent_buffer(leaf, fs_uuid,
1399                            (unsigned long)btrfs_device_fsid(dev_item),
1400                            BTRFS_UUID_SIZE);
1401
1402         if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
1403                 ret = open_seed_devices(root, fs_uuid);
1404                 if (ret)
1405                         return ret;
1406         }
1407
1408         device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1409         if (!device) {
1410                 printk("warning devid %llu not found already\n",
1411                         (unsigned long long)devid);
1412                 device = kmalloc(sizeof(*device), GFP_NOFS);
1413                 if (!device)
1414                         return -ENOMEM;
1415                 device->total_ios = 0;
1416                 list_add(&device->dev_list,
1417                          &root->fs_info->fs_devices->devices);
1418         }
1419
1420         fill_device_from_item(leaf, dev_item, device);
1421         device->dev_root = root->fs_info->dev_root;
1422         return ret;
1423 }
1424
1425 int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
1426 {
1427         struct btrfs_dev_item *dev_item;
1428
1429         dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
1430                                                      dev_item);
1431         return read_one_dev(root, buf, dev_item);
1432 }
1433
1434 int btrfs_read_sys_array(struct btrfs_root *root)
1435 {
1436         struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1437         struct extent_buffer *sb;
1438         struct btrfs_disk_key *disk_key;
1439         struct btrfs_chunk *chunk;
1440         struct btrfs_key key;
1441         u32 num_stripes;
1442         u32 array_size;
1443         u32 len = 0;
1444         u8 *ptr;
1445         unsigned long sb_ptr;
1446         u32 cur;
1447         int ret;
1448
1449         sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
1450                                           BTRFS_SUPER_INFO_SIZE);
1451         if (!sb)
1452                 return -ENOMEM;
1453         btrfs_set_buffer_uptodate(sb);
1454         write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
1455         array_size = btrfs_super_sys_array_size(super_copy);
1456
1457         /*
1458          * we do this loop twice, once for the device items and
1459          * once for all of the chunks.  This way there are device
1460          * structs filled in for every chunk
1461          */
1462         ptr = super_copy->sys_chunk_array;
1463         sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
1464         cur = 0;
1465
1466         while (cur < array_size) {
1467                 disk_key = (struct btrfs_disk_key *)ptr;
1468                 btrfs_disk_key_to_cpu(&key, disk_key);
1469
1470                 len = sizeof(*disk_key);
1471                 ptr += len;
1472                 sb_ptr += len;
1473                 cur += len;
1474
1475                 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1476                         chunk = (struct btrfs_chunk *)sb_ptr;
1477                         ret = read_one_chunk(root, &key, sb, chunk);
1478                         BUG_ON(ret);
1479                         num_stripes = btrfs_chunk_num_stripes(sb, chunk);
1480                         len = btrfs_chunk_item_size(num_stripes);
1481                 } else {
1482                         BUG();
1483                 }
1484                 ptr += len;
1485                 sb_ptr += len;
1486                 cur += len;
1487         }
1488         free_extent_buffer(sb);
1489         return 0;
1490 }
1491
1492 int btrfs_read_chunk_tree(struct btrfs_root *root)
1493 {
1494         struct btrfs_path *path;
1495         struct extent_buffer *leaf;
1496         struct btrfs_key key;
1497         struct btrfs_key found_key;
1498         int ret;
1499         int slot;
1500
1501         root = root->fs_info->chunk_root;
1502
1503         path = btrfs_alloc_path();
1504         if (!path)
1505                 return -ENOMEM;
1506
1507         /* first we search for all of the device items, and then we
1508          * read in all of the chunk items.  This way we can create chunk
1509          * mappings that reference all of the devices that are afound
1510          */
1511         key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1512         key.offset = 0;
1513         key.type = 0;
1514 again:
1515         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1516         while(1) {
1517                 leaf = path->nodes[0];
1518                 slot = path->slots[0];
1519                 if (slot >= btrfs_header_nritems(leaf)) {
1520                         ret = btrfs_next_leaf(root, path);
1521                         if (ret == 0)
1522                                 continue;
1523                         if (ret < 0)
1524                                 goto error;
1525                         break;
1526                 }
1527                 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1528                 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
1529                         if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
1530                                 break;
1531                         if (found_key.type == BTRFS_DEV_ITEM_KEY) {
1532                                 struct btrfs_dev_item *dev_item;
1533                                 dev_item = btrfs_item_ptr(leaf, slot,
1534                                                   struct btrfs_dev_item);
1535                                 ret = read_one_dev(root, leaf, dev_item);
1536                                 BUG_ON(ret);
1537                         }
1538                 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
1539                         struct btrfs_chunk *chunk;
1540                         chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
1541                         ret = read_one_chunk(root, &found_key, leaf, chunk);
1542                         BUG_ON(ret);
1543                 }
1544                 path->slots[0]++;
1545         }
1546         if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
1547                 key.objectid = 0;
1548                 btrfs_release_path(root, path);
1549                 goto again;
1550         }
1551
1552         btrfs_free_path(path);
1553         ret = 0;
1554 error:
1555         return ret;
1556 }
1557
1558 struct list_head *btrfs_scanned_uuids(void)
1559 {
1560         return &fs_uuids;
1561 }