btrfs-progs: Fix printf format casting errors
[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         struct stripe stripes[];
47 };
48
49 #define map_lookup_size(n) (sizeof(struct map_lookup) + \
50                             (sizeof(struct stripe) * (n)))
51
52 static LIST_HEAD(fs_uuids);
53
54 static struct btrfs_device *__find_device(struct list_head *head, u64 devid)
55 {
56         struct btrfs_device *dev;
57         struct list_head *cur;
58
59         list_for_each(cur, head) {
60                 dev = list_entry(cur, struct btrfs_device, dev_list);
61                 if (dev->devid == devid)
62                         return dev;
63         }
64         return NULL;
65 }
66
67 static struct btrfs_fs_devices *find_fsid(u8 *fsid)
68 {
69         struct list_head *cur;
70         struct btrfs_fs_devices *fs_devices;
71
72         list_for_each(cur, &fs_uuids) {
73                 fs_devices = list_entry(cur, struct btrfs_fs_devices, list);
74                 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
75                         return fs_devices;
76         }
77         return NULL;
78 }
79
80 static int device_list_add(const char *path,
81                            struct btrfs_super_block *disk_super,
82                            u64 devid, struct btrfs_fs_devices **fs_devices_ret)
83 {
84         struct btrfs_device *device;
85         struct btrfs_fs_devices *fs_devices;
86         u64 found_transid = btrfs_super_generation(disk_super);
87
88         fs_devices = find_fsid(disk_super->fsid);
89         if (!fs_devices) {
90                 fs_devices = kmalloc(sizeof(*fs_devices), GFP_NOFS);
91                 if (!fs_devices)
92                         return -ENOMEM;
93                 INIT_LIST_HEAD(&fs_devices->devices);
94                 list_add(&fs_devices->list, &fs_uuids);
95                 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
96                 fs_devices->latest_devid = devid;
97                 fs_devices->latest_trans = found_transid;
98                 fs_devices->lowest_devid = (u64)-1;
99                 device = NULL;
100         } else {
101                 device = __find_device(&fs_devices->devices, devid);
102         }
103         if (!device) {
104                 device = kzalloc(sizeof(*device), GFP_NOFS);
105                 if (!device) {
106                         /* we can safely leave the fs_devices entry around */
107                         return -ENOMEM;
108                 }
109                 device->devid = devid;
110                 device->name = kstrdup(path, GFP_NOFS);
111                 if (!device->name) {
112                         kfree(device);
113                         return -ENOMEM;
114                 }
115                 list_add(&device->dev_list, &fs_devices->devices);
116         }
117
118         if (found_transid > fs_devices->latest_trans) {
119                 fs_devices->latest_devid = devid;
120                 fs_devices->latest_trans = found_transid;
121         }
122         if (fs_devices->lowest_devid > devid) {
123                 fs_devices->lowest_devid = devid;
124                 printk("lowest devid now %llu\n", (unsigned long long)devid);
125         }
126         *fs_devices_ret = fs_devices;
127         return 0;
128 }
129
130 int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
131 {
132         struct list_head *head = &fs_devices->devices;
133         struct list_head *cur;
134         struct btrfs_device *device;
135
136         list_for_each(cur, head) {
137                 device = list_entry(cur, struct btrfs_device, dev_list);
138                 device->fd = 0;
139         }
140         return 0;
141 }
142
143 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, int flags)
144 {
145         int fd;
146         struct list_head *head = &fs_devices->devices;
147         struct list_head *cur;
148         struct btrfs_device *device;
149         int ret;
150
151         list_for_each(cur, head) {
152                 device = list_entry(cur, struct btrfs_device, dev_list);
153                 fd = open(device->name, flags);
154 printk("opening %s devid %llu fd %d\n", device->name,
155         (unsigned long long)device->devid, fd);
156                 if (fd < 0) {
157                         ret = -errno;
158                         goto fail;
159                 }
160                 if (device->devid == fs_devices->latest_devid)
161                         fs_devices->latest_bdev = fd;
162                 if (device->devid == fs_devices->lowest_devid)
163                         fs_devices->lowest_bdev = fd;
164                 device->fd = fd;
165         }
166         return 0;
167 fail:
168         btrfs_close_devices(fs_devices);
169         return ret;
170 }
171
172 int btrfs_scan_one_device(int fd, const char *path,
173                           struct btrfs_fs_devices **fs_devices_ret,
174                           u64 *total_devs, u64 super_offset)
175 {
176         struct btrfs_super_block *disk_super;
177         char *buf;
178         int ret;
179         u64 devid;
180
181         buf = malloc(4096);
182         if (!buf) {
183                 ret = -ENOMEM;
184                 goto error;
185         }
186         ret = pread(fd, buf, 4096, super_offset);
187         if (ret != 4096) {
188                 ret = -EIO;
189                 goto error;
190         }
191         disk_super = (struct btrfs_super_block *)buf;
192         if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
193             sizeof(disk_super->magic))) {
194                 ret = -ENOENT;
195                 goto error_brelse;
196         }
197         devid = le64_to_cpu(disk_super->dev_item.devid);
198         *total_devs = btrfs_super_num_devices(disk_super);
199         printk("found device %llu on %s\n", (unsigned long long)devid, path);
200         ret = device_list_add(path, disk_super, devid, fs_devices_ret);
201
202 error_brelse:
203         free(buf);
204 error:
205         return ret;
206 }
207
208 /*
209  * this uses a pretty simple search, the expectation is that it is
210  * called very infrequently and that a given device has a small number
211  * of extents
212  */
213 static int find_free_dev_extent(struct btrfs_trans_handle *trans,
214                                 struct btrfs_device *device,
215                                 struct btrfs_path *path,
216                                 u64 num_bytes, u64 *start)
217 {
218         struct btrfs_key key;
219         struct btrfs_root *root = device->dev_root;
220         struct btrfs_dev_extent *dev_extent = NULL;
221         u64 hole_size = 0;
222         u64 last_byte = 0;
223         u64 search_start = 0;
224         u64 search_end = device->total_bytes;
225         int ret;
226         int slot = 0;
227         int start_found;
228         struct extent_buffer *l;
229
230         start_found = 0;
231         path->reada = 2;
232
233         /* FIXME use last free of some kind */
234
235         key.objectid = device->devid;
236         key.offset = search_start;
237         key.type = BTRFS_DEV_EXTENT_KEY;
238         ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
239         if (ret < 0)
240                 goto error;
241         ret = btrfs_previous_item(root, path, 0, key.type);
242         if (ret < 0)
243                 goto error;
244         l = path->nodes[0];
245         btrfs_item_key_to_cpu(l, &key, path->slots[0]);
246         while (1) {
247                 l = path->nodes[0];
248                 slot = path->slots[0];
249                 if (slot >= btrfs_header_nritems(l)) {
250                         ret = btrfs_next_leaf(root, path);
251                         if (ret == 0)
252                                 continue;
253                         if (ret < 0)
254                                 goto error;
255 no_more_items:
256                         if (!start_found) {
257                                 if (search_start >= search_end) {
258                                         ret = -ENOSPC;
259                                         goto error;
260                                 }
261                                 *start = search_start;
262                                 start_found = 1;
263                                 goto check_pending;
264                         }
265                         *start = last_byte > search_start ?
266                                 last_byte : search_start;
267                         if (search_end <= *start) {
268                                 ret = -ENOSPC;
269                                 goto error;
270                         }
271                         goto check_pending;
272                 }
273                 btrfs_item_key_to_cpu(l, &key, slot);
274
275                 if (key.objectid < device->devid)
276                         goto next;
277
278                 if (key.objectid > device->devid)
279                         goto no_more_items;
280
281                 if (key.offset >= search_start && key.offset > last_byte &&
282                     start_found) {
283                         if (last_byte < search_start)
284                                 last_byte = search_start;
285                         hole_size = key.offset - last_byte;
286                         if (key.offset > last_byte &&
287                             hole_size >= num_bytes) {
288                                 *start = last_byte;
289                                 goto check_pending;
290                         }
291                 }
292                 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) {
293                         goto next;
294                 }
295
296                 start_found = 1;
297                 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
298                 last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent);
299 next:
300                 path->slots[0]++;
301                 cond_resched();
302         }
303 check_pending:
304         /* we have to make sure we didn't find an extent that has already
305          * been allocated by the map tree or the original allocation
306          */
307         btrfs_release_path(root, path);
308         BUG_ON(*start < search_start);
309
310         if (*start + num_bytes > search_end) {
311                 ret = -ENOSPC;
312                 goto error;
313         }
314         /* check for pending inserts here */
315         return 0;
316
317 error:
318         btrfs_release_path(root, path);
319         return ret;
320 }
321
322 int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
323                            struct btrfs_device *device,
324                            u64 owner, u64 num_bytes, u64 *start)
325 {
326         int ret;
327         struct btrfs_path *path;
328         struct btrfs_root *root = device->dev_root;
329         struct btrfs_dev_extent *extent;
330         struct extent_buffer *leaf;
331         struct btrfs_key key;
332
333         path = btrfs_alloc_path();
334         if (!path)
335                 return -ENOMEM;
336
337         ret = find_free_dev_extent(trans, device, path, num_bytes, start);
338         if (ret) {
339                 goto err;
340         }
341
342         key.objectid = device->devid;
343         key.offset = *start;
344         key.type = BTRFS_DEV_EXTENT_KEY;
345         ret = btrfs_insert_empty_item(trans, root, path, &key,
346                                       sizeof(*extent));
347         BUG_ON(ret);
348
349         leaf = path->nodes[0];
350         extent = btrfs_item_ptr(leaf, path->slots[0],
351                                 struct btrfs_dev_extent);
352         btrfs_set_dev_extent_owner(leaf, extent, owner);
353         btrfs_set_dev_extent_length(leaf, extent, num_bytes);
354         btrfs_mark_buffer_dirty(leaf);
355 err:
356         btrfs_free_path(path);
357         return ret;
358 }
359
360 static int find_next_chunk(struct btrfs_root *root, u64 *objectid)
361 {
362         struct btrfs_path *path;
363         int ret;
364         struct btrfs_key key;
365         struct btrfs_key found_key;
366
367         path = btrfs_alloc_path();
368         BUG_ON(!path);
369
370         key.objectid = (u64)-1;
371         key.offset = (u64)-1;
372         key.type = BTRFS_CHUNK_ITEM_KEY;
373
374         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
375         if (ret < 0)
376                 goto error;
377
378         BUG_ON(ret == 0);
379
380         ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
381         if (ret) {
382                 *objectid = 0;
383         } else {
384                 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
385                                       path->slots[0]);
386                 *objectid = found_key.objectid + found_key.offset;
387         }
388         ret = 0;
389 error:
390         btrfs_free_path(path);
391         return ret;
392 }
393
394 static int find_next_devid(struct btrfs_root *root, struct btrfs_path *path,
395                            u64 *objectid)
396 {
397         int ret;
398         struct btrfs_key key;
399         struct btrfs_key found_key;
400
401         key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
402         key.type = BTRFS_DEV_ITEM_KEY;
403         key.offset = (u64)-1;
404
405         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
406         if (ret < 0)
407                 goto error;
408
409         BUG_ON(ret == 0);
410
411         ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
412                                   BTRFS_DEV_ITEM_KEY);
413         if (ret) {
414                 *objectid = 1;
415         } else {
416                 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
417                                       path->slots[0]);
418                 *objectid = found_key.offset + 1;
419         }
420         ret = 0;
421 error:
422         btrfs_release_path(root, path);
423         return ret;
424 }
425
426 /*
427  * the device information is stored in the chunk root
428  * the btrfs_device struct should be fully filled in
429  */
430 int btrfs_add_device(struct btrfs_trans_handle *trans,
431                      struct btrfs_root *root,
432                      struct btrfs_device *device)
433 {
434         int ret;
435         struct btrfs_path *path;
436         struct btrfs_dev_item *dev_item;
437         struct extent_buffer *leaf;
438         struct btrfs_key key;
439         unsigned long ptr;
440         u64 free_devid;
441
442         root = root->fs_info->chunk_root;
443
444         path = btrfs_alloc_path();
445         if (!path)
446                 return -ENOMEM;
447
448         ret = find_next_devid(root, path, &free_devid);
449         if (ret)
450                 goto out;
451
452         key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
453         key.type = BTRFS_DEV_ITEM_KEY;
454         key.offset = free_devid;
455
456         ret = btrfs_insert_empty_item(trans, root, path, &key,
457                                       sizeof(*dev_item));
458         if (ret)
459                 goto out;
460
461         leaf = path->nodes[0];
462         dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
463
464         device->devid = free_devid;
465         btrfs_set_device_id(leaf, dev_item, device->devid);
466         btrfs_set_device_type(leaf, dev_item, device->type);
467         btrfs_set_device_io_align(leaf, dev_item, device->io_align);
468         btrfs_set_device_io_width(leaf, dev_item, device->io_width);
469         btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
470         btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
471         btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
472
473         ptr = (unsigned long)btrfs_device_uuid(dev_item);
474         write_extent_buffer(leaf, device->uuid, ptr, BTRFS_DEV_UUID_SIZE);
475         btrfs_mark_buffer_dirty(leaf);
476         ret = 0;
477
478 out:
479         btrfs_free_path(path);
480         return ret;
481 }
482
483 int btrfs_update_device(struct btrfs_trans_handle *trans,
484                         struct btrfs_device *device)
485 {
486         int ret;
487         struct btrfs_path *path;
488         struct btrfs_root *root;
489         struct btrfs_dev_item *dev_item;
490         struct extent_buffer *leaf;
491         struct btrfs_key key;
492
493         root = device->dev_root->fs_info->chunk_root;
494
495         path = btrfs_alloc_path();
496         if (!path)
497                 return -ENOMEM;
498
499         key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
500         key.type = BTRFS_DEV_ITEM_KEY;
501         key.offset = device->devid;
502
503         ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
504         if (ret < 0)
505                 goto out;
506
507         if (ret > 0) {
508                 ret = -ENOENT;
509                 goto out;
510         }
511
512         leaf = path->nodes[0];
513         dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
514
515         btrfs_set_device_id(leaf, dev_item, device->devid);
516         btrfs_set_device_type(leaf, dev_item, device->type);
517         btrfs_set_device_io_align(leaf, dev_item, device->io_align);
518         btrfs_set_device_io_width(leaf, dev_item, device->io_width);
519         btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
520         btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
521         btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
522         btrfs_mark_buffer_dirty(leaf);
523
524 out:
525         btrfs_free_path(path);
526         return ret;
527 }
528
529 int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
530                            struct btrfs_root *root,
531                            struct btrfs_key *key,
532                            struct btrfs_chunk *chunk, int item_size)
533 {
534         struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
535         struct btrfs_disk_key disk_key;
536         u32 array_size;
537         u8 *ptr;
538
539         array_size = btrfs_super_sys_array_size(super_copy);
540         if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
541                 return -EFBIG;
542
543         ptr = super_copy->sys_chunk_array + array_size;
544         btrfs_cpu_key_to_disk(&disk_key, key);
545         memcpy(ptr, &disk_key, sizeof(disk_key));
546         ptr += sizeof(disk_key);
547         memcpy(ptr, chunk, item_size);
548         item_size += sizeof(disk_key);
549         btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
550         return 0;
551 }
552
553 int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
554                       struct btrfs_root *extent_root, u64 *start,
555                       u64 *num_bytes, u64 type)
556 {
557         u64 dev_offset;
558         struct btrfs_fs_info *info = extent_root->fs_info;
559         struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
560         struct btrfs_stripe *stripes;
561         struct btrfs_device *device = NULL;
562         struct btrfs_chunk *chunk;
563         struct list_head private_devs;
564         struct list_head *dev_list = &extent_root->fs_info->fs_devices->devices;
565         struct list_head *cur;
566         struct map_lookup *map;
567         u64 physical;
568         u64 calc_size = 8 * 1024 * 1024;
569         u64 avail;
570         u64 max_avail = 0;
571         int num_stripes = 1;
572         int looped = 0;
573         int ret;
574         int index;
575         int stripe_len = 64 * 1024;
576         struct btrfs_key key;
577
578         if (list_empty(dev_list))
579                 return -ENOSPC;
580
581         if (type & BTRFS_BLOCK_GROUP_RAID0)
582                 num_stripes = btrfs_super_num_devices(&info->super_copy);
583         if (type & BTRFS_BLOCK_GROUP_DATA)
584                 stripe_len = 64 * 1024;
585         if (type & (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_SYSTEM))
586                 stripe_len = 32 * 1024;
587 again:
588         INIT_LIST_HEAD(&private_devs);
589         cur = dev_list->next;
590         index = 0;
591         /* build a private list of devices we will allocate from */
592         while(index < num_stripes) {
593                 device = list_entry(cur, struct btrfs_device, dev_list);
594                 avail = device->total_bytes - device->bytes_used;
595                 cur = cur->next;
596                 if (avail > max_avail)
597                         max_avail = avail;
598                 if (avail >= calc_size) {
599                         list_move_tail(&device->dev_list, &private_devs);
600                         index++;
601                 }
602                 if (cur == dev_list)
603                         break;
604         }
605         if (index < num_stripes) {
606                 list_splice(&private_devs, dev_list);
607                 if (!looped && max_avail > 0) {
608                         looped = 1;
609                         calc_size = max_avail;
610                         goto again;
611                 }
612                 return -ENOSPC;
613         }
614
615         ret = find_next_chunk(chunk_root, &key.objectid);
616         if (ret)
617                 return ret;
618
619         chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS);
620         if (!chunk)
621                 return -ENOMEM;
622
623         map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
624         if (!map) {
625                 kfree(chunk);
626                 return -ENOMEM;
627         }
628
629         stripes = &chunk->stripe;
630
631         *num_bytes = calc_size * num_stripes;
632         index = 0;
633         while(index < num_stripes) {
634                 BUG_ON(list_empty(&private_devs));
635                 cur = private_devs.next;
636                 device = list_entry(cur, struct btrfs_device, dev_list);
637                 list_move_tail(&device->dev_list, dev_list);
638
639                 ret = btrfs_alloc_dev_extent(trans, device,
640                                              key.objectid,
641                                              calc_size, &dev_offset);
642                 BUG_ON(ret);
643 printk("alloc chunk size %llu from dev %llu\n",
644         (unsigned long long)calc_size,
645         (unsigned long long)device->devid);
646                 device->bytes_used += calc_size;
647                 ret = btrfs_update_device(trans, device);
648                 BUG_ON(ret);
649
650                 map->stripes[index].dev = device;
651                 map->stripes[index].physical = dev_offset;
652                 btrfs_set_stack_stripe_devid(stripes + index, device->devid);
653                 btrfs_set_stack_stripe_offset(stripes + index, dev_offset);
654                 physical = dev_offset;
655                 index++;
656         }
657         BUG_ON(!list_empty(&private_devs));
658
659         /* key.objectid was set above */
660         key.offset = *num_bytes;
661         key.type = BTRFS_CHUNK_ITEM_KEY;
662         btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
663         btrfs_set_stack_chunk_stripe_len(chunk, stripe_len);
664         btrfs_set_stack_chunk_type(chunk, type);
665         btrfs_set_stack_chunk_num_stripes(chunk, num_stripes);
666         btrfs_set_stack_chunk_io_align(chunk, stripe_len);
667         btrfs_set_stack_chunk_io_width(chunk, stripe_len);
668         btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
669         map->sector_size = extent_root->sectorsize;
670         map->stripe_len = stripe_len;
671         map->io_align = stripe_len;
672         map->io_width = stripe_len;
673         map->type = type;
674         map->num_stripes = num_stripes;
675
676         ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
677                                 btrfs_chunk_item_size(num_stripes));
678         BUG_ON(ret);
679         *start = key.objectid;
680
681         map->ce.start = key.objectid;
682         map->ce.size = key.offset;
683
684         ret = insert_existing_cache_extent(
685                            &extent_root->fs_info->mapping_tree.cache_tree,
686                            &map->ce);
687         BUG_ON(ret);
688
689         kfree(chunk);
690         return ret;
691 }
692
693 void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
694 {
695         cache_tree_init(&tree->cache_tree);
696 }
697
698 int btrfs_map_block(struct btrfs_mapping_tree *map_tree,
699                     u64 logical, u64 *phys, u64 *length,
700                     struct btrfs_device **dev)
701 {
702         struct cache_extent *ce;
703         struct map_lookup *map;
704         u64 offset;
705         u64 stripe_offset;
706         u64 stripe_nr;
707         int stripe_index;
708
709         ce = find_first_cache_extent(&map_tree->cache_tree, logical);
710         BUG_ON(!ce);
711         BUG_ON(ce->start > logical || ce->start + ce->size < logical);
712         map = container_of(ce, struct map_lookup, ce);
713         offset = logical - ce->start;
714
715         stripe_nr = offset;
716         /*
717          * stripe_nr counts the total number of stripes we have to stride
718          * to get to this block
719          */
720         stripe_nr = stripe_nr / map->stripe_len;
721
722         stripe_offset = stripe_nr * map->stripe_len;
723         BUG_ON(offset < stripe_offset);
724
725         /* stripe_offset is the offset of this block in its stripe*/
726         stripe_offset = offset - stripe_offset;
727
728         /*
729          * after this do_div call, stripe_nr is the number of stripes
730          * on this device we have to walk to find the data, and
731          * stripe_index is the number of our device in the stripe array
732          */
733         stripe_index = stripe_nr % map->num_stripes;
734         stripe_nr = stripe_nr / map->num_stripes;
735
736         BUG_ON(stripe_index >= map->num_stripes);
737
738         *phys = map->stripes[stripe_index].physical + stripe_offset +
739                 stripe_nr * map->stripe_len;
740
741         if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
742                 /* we limit the length of each bio to what fits in a stripe */
743                 *length = min_t(u64, ce->size - offset,
744                               map->stripe_len - stripe_offset);
745         } else {
746                 *length = ce->size - offset;
747         }
748         *dev = map->stripes[stripe_index].dev;
749         return 0;
750 }
751
752 struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid)
753 {
754         struct list_head *head = &root->fs_info->fs_devices->devices;
755
756         return __find_device(head, devid);
757 }
758
759 static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
760                           struct extent_buffer *leaf,
761                           struct btrfs_chunk *chunk)
762 {
763         struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
764         struct map_lookup *map;
765         struct cache_extent *ce;
766         u64 logical;
767         u64 length;
768         u64 devid;
769         int num_stripes;
770         int ret;
771         int i;
772
773         logical = key->objectid;
774         length = key->offset;
775         ce = find_first_cache_extent(&map_tree->cache_tree, logical);
776
777         /* already mapped? */
778         if (ce && ce->start <= logical && ce->start + ce->size > logical) {
779                 return 0;
780         }
781
782         num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
783         map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
784         if (!map)
785                 return -ENOMEM;
786
787         map->ce.start = logical;
788         map->ce.size = length;
789
790         map->num_stripes = num_stripes;
791         map->io_width = btrfs_chunk_io_width(leaf, chunk);
792         map->io_align = btrfs_chunk_io_align(leaf, chunk);
793         map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
794         map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
795         map->type = btrfs_chunk_type(leaf, chunk);
796         for (i = 0; i < num_stripes; i++) {
797                 map->stripes[i].physical =
798                         btrfs_stripe_offset_nr(leaf, chunk, i);
799                 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
800                 map->stripes[i].dev = btrfs_find_device(root, devid);
801                 if (!map->stripes[i].dev) {
802                         kfree(map);
803                         return -EIO;
804                 }
805
806         }
807         ret = insert_existing_cache_extent(&map_tree->cache_tree, &map->ce);
808         BUG_ON(ret);
809
810         return 0;
811 }
812
813 static int fill_device_from_item(struct extent_buffer *leaf,
814                                  struct btrfs_dev_item *dev_item,
815                                  struct btrfs_device *device)
816 {
817         unsigned long ptr;
818
819         device->devid = btrfs_device_id(leaf, dev_item);
820         device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
821         device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
822         device->type = btrfs_device_type(leaf, dev_item);
823         device->io_align = btrfs_device_io_align(leaf, dev_item);
824         device->io_width = btrfs_device_io_width(leaf, dev_item);
825         device->sector_size = btrfs_device_sector_size(leaf, dev_item);
826
827         ptr = (unsigned long)btrfs_device_uuid(dev_item);
828         read_extent_buffer(leaf, device->uuid, ptr, BTRFS_DEV_UUID_SIZE);
829
830         return 0;
831 }
832
833 static int read_one_dev(struct btrfs_root *root,
834                         struct extent_buffer *leaf,
835                         struct btrfs_dev_item *dev_item)
836 {
837         struct btrfs_device *device;
838         u64 devid;
839         int ret = 0;
840
841         devid = btrfs_device_id(leaf, dev_item);
842         device = btrfs_find_device(root, devid);
843         if (!device) {
844                 printk("warning devid %llu not found already\n",
845                         (unsigned long long)devid);
846                 device = kmalloc(sizeof(*device), GFP_NOFS);
847                 if (!device)
848                         return -ENOMEM;
849                 list_add(&device->dev_list,
850                          &root->fs_info->fs_devices->devices);
851         }
852
853         fill_device_from_item(leaf, dev_item, device);
854         device->dev_root = root->fs_info->dev_root;
855         return ret;
856 }
857
858 int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
859 {
860         struct btrfs_dev_item *dev_item;
861
862         dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
863                                                      dev_item);
864         return read_one_dev(root, buf, dev_item);
865 }
866
867 int btrfs_read_sys_array(struct btrfs_root *root)
868 {
869         struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
870         struct extent_buffer *sb = root->fs_info->sb_buffer;
871         struct btrfs_disk_key *disk_key;
872         struct btrfs_chunk *chunk;
873         struct btrfs_key key;
874         u32 num_stripes;
875         u32 array_size;
876         u32 len = 0;
877         u8 *ptr;
878         unsigned long sb_ptr;
879         u32 cur;
880         int ret;
881
882         array_size = btrfs_super_sys_array_size(super_copy);
883
884         /*
885          * we do this loop twice, once for the device items and
886          * once for all of the chunks.  This way there are device
887          * structs filled in for every chunk
888          */
889         ptr = super_copy->sys_chunk_array;
890         sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
891         cur = 0;
892
893         while (cur < array_size) {
894                 disk_key = (struct btrfs_disk_key *)ptr;
895                 btrfs_disk_key_to_cpu(&key, disk_key);
896
897                 len = sizeof(*disk_key);
898                 ptr += len;
899                 sb_ptr += len;
900                 cur += len;
901
902                 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
903                         chunk = (struct btrfs_chunk *)sb_ptr;
904                         ret = read_one_chunk(root, &key, sb, chunk);
905                         BUG_ON(ret);
906                         num_stripes = btrfs_chunk_num_stripes(sb, chunk);
907                         len = btrfs_chunk_item_size(num_stripes);
908                 } else {
909                         BUG();
910                 }
911                 ptr += len;
912                 sb_ptr += len;
913                 cur += len;
914         }
915         return 0;
916 }
917
918 int btrfs_read_chunk_tree(struct btrfs_root *root)
919 {
920         struct btrfs_path *path;
921         struct extent_buffer *leaf;
922         struct btrfs_key key;
923         struct btrfs_key found_key;
924         int ret;
925         int slot;
926
927         root = root->fs_info->chunk_root;
928
929         path = btrfs_alloc_path();
930         if (!path)
931                 return -ENOMEM;
932
933         /* first we search for all of the device items, and then we
934          * read in all of the chunk items.  This way we can create chunk
935          * mappings that reference all of the devices that are afound
936          */
937         key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
938         key.offset = 0;
939         key.type = 0;
940 again:
941         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
942         while(1) {
943                 leaf = path->nodes[0];
944                 slot = path->slots[0];
945                 if (slot >= btrfs_header_nritems(leaf)) {
946                         ret = btrfs_next_leaf(root, path);
947                         if (ret == 0)
948                                 continue;
949                         if (ret < 0)
950                                 goto error;
951                         break;
952                 }
953                 btrfs_item_key_to_cpu(leaf, &found_key, slot);
954                 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
955                         if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
956                                 break;
957                         if (found_key.type == BTRFS_DEV_ITEM_KEY) {
958                                 struct btrfs_dev_item *dev_item;
959                                 dev_item = btrfs_item_ptr(leaf, slot,
960                                                   struct btrfs_dev_item);
961                                 ret = read_one_dev(root, leaf, dev_item);
962                                 BUG_ON(ret);
963                         }
964                 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
965                         struct btrfs_chunk *chunk;
966                         chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
967                         ret = read_one_chunk(root, &found_key, leaf, chunk);
968                 }
969                 path->slots[0]++;
970         }
971         if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
972                 key.objectid = 0;
973                 btrfs_release_path(root, path);
974                 goto again;
975         }
976
977         btrfs_free_path(path);
978         ret = 0;
979 error:
980         return ret;
981 }
982