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