Use device uuids when scanning devices
[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 = kmalloc(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                 list_add(&device->dev_list, &fs_devices->devices);
123         }
124
125         if (found_transid > fs_devices->latest_trans) {
126                 fs_devices->latest_devid = devid;
127                 fs_devices->latest_trans = found_transid;
128         }
129         if (fs_devices->lowest_devid > devid) {
130                 fs_devices->lowest_devid = devid;
131                 printk("lowest devid now %llu\n", (unsigned long long)devid);
132         }
133         *fs_devices_ret = fs_devices;
134         return 0;
135 }
136
137 int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
138 {
139         struct list_head *head = &fs_devices->devices;
140         struct list_head *cur;
141         struct btrfs_device *device;
142
143         list_for_each(cur, head) {
144                 device = list_entry(cur, struct btrfs_device, dev_list);
145                 device->fd = 0;
146         }
147         return 0;
148 }
149
150 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, int flags)
151 {
152         int fd;
153         struct list_head *head = &fs_devices->devices;
154         struct list_head *cur;
155         struct btrfs_device *device;
156         int ret;
157
158         list_for_each(cur, head) {
159                 device = list_entry(cur, struct btrfs_device, dev_list);
160                 fd = open(device->name, flags);
161 printk("opening %s devid %llu fd %d\n", device->name,
162         (unsigned long long)device->devid, fd);
163                 if (fd < 0) {
164                         ret = -errno;
165                         goto fail;
166                 }
167                 if (device->devid == fs_devices->latest_devid)
168                         fs_devices->latest_bdev = fd;
169                 if (device->devid == fs_devices->lowest_devid)
170                         fs_devices->lowest_bdev = fd;
171                 device->fd = fd;
172         }
173         return 0;
174 fail:
175         btrfs_close_devices(fs_devices);
176         return ret;
177 }
178
179 int btrfs_scan_one_device(int fd, const char *path,
180                           struct btrfs_fs_devices **fs_devices_ret,
181                           u64 *total_devs, u64 super_offset)
182 {
183         struct btrfs_super_block *disk_super;
184         char *buf;
185         int ret;
186         u64 devid;
187
188         buf = malloc(4096);
189         if (!buf) {
190                 ret = -ENOMEM;
191                 goto error;
192         }
193         ret = pread(fd, buf, 4096, super_offset);
194         if (ret != 4096) {
195                 ret = -EIO;
196                 goto error;
197         }
198         disk_super = (struct btrfs_super_block *)buf;
199         if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
200             sizeof(disk_super->magic))) {
201                 ret = -ENOENT;
202                 goto error_brelse;
203         }
204         devid = le64_to_cpu(disk_super->dev_item.devid);
205         *total_devs = btrfs_super_num_devices(disk_super);
206         printk("found device %llu on %s\n", (unsigned long long)devid, path);
207         ret = device_list_add(path, disk_super, devid, fs_devices_ret);
208
209 error_brelse:
210         free(buf);
211 error:
212         return ret;
213 }
214
215 /*
216  * this uses a pretty simple search, the expectation is that it is
217  * called very infrequently and that a given device has a small number
218  * of extents
219  */
220 static int find_free_dev_extent(struct btrfs_trans_handle *trans,
221                                 struct btrfs_device *device,
222                                 struct btrfs_path *path,
223                                 u64 num_bytes, u64 *start)
224 {
225         struct btrfs_key key;
226         struct btrfs_root *root = device->dev_root;
227         struct btrfs_dev_extent *dev_extent = NULL;
228         u64 hole_size = 0;
229         u64 last_byte = 0;
230         u64 search_start = 0;
231         u64 search_end = device->total_bytes;
232         int ret;
233         int slot = 0;
234         int start_found;
235         struct extent_buffer *l;
236
237         start_found = 0;
238         path->reada = 2;
239
240         /* FIXME use last free of some kind */
241
242         /* we don't want to overwrite the superblock on the drive,
243          * so we make sure to start at an offset of at least 1MB
244          */
245         search_start = max((u64)1024 * 1024, search_start);
246         key.objectid = device->devid;
247         key.offset = search_start;
248         key.type = BTRFS_DEV_EXTENT_KEY;
249         ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
250         if (ret < 0)
251                 goto error;
252         ret = btrfs_previous_item(root, path, 0, key.type);
253         if (ret < 0)
254                 goto error;
255         l = path->nodes[0];
256         btrfs_item_key_to_cpu(l, &key, path->slots[0]);
257         while (1) {
258                 l = path->nodes[0];
259                 slot = path->slots[0];
260                 if (slot >= btrfs_header_nritems(l)) {
261                         ret = btrfs_next_leaf(root, path);
262                         if (ret == 0)
263                                 continue;
264                         if (ret < 0)
265                                 goto error;
266 no_more_items:
267                         if (!start_found) {
268                                 if (search_start >= search_end) {
269                                         ret = -ENOSPC;
270                                         goto error;
271                                 }
272                                 *start = search_start;
273                                 start_found = 1;
274                                 goto check_pending;
275                         }
276                         *start = last_byte > search_start ?
277                                 last_byte : search_start;
278                         if (search_end <= *start) {
279                                 ret = -ENOSPC;
280                                 goto error;
281                         }
282                         goto check_pending;
283                 }
284                 btrfs_item_key_to_cpu(l, &key, slot);
285
286                 if (key.objectid < device->devid)
287                         goto next;
288
289                 if (key.objectid > device->devid)
290                         goto no_more_items;
291
292                 if (key.offset >= search_start && key.offset > last_byte &&
293                     start_found) {
294                         if (last_byte < search_start)
295                                 last_byte = search_start;
296                         hole_size = key.offset - last_byte;
297                         if (key.offset > last_byte &&
298                             hole_size >= num_bytes) {
299                                 *start = last_byte;
300                                 goto check_pending;
301                         }
302                 }
303                 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) {
304                         goto next;
305                 }
306
307                 start_found = 1;
308                 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
309                 last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent);
310 next:
311                 path->slots[0]++;
312                 cond_resched();
313         }
314 check_pending:
315         /* we have to make sure we didn't find an extent that has already
316          * been allocated by the map tree or the original allocation
317          */
318         btrfs_release_path(root, path);
319         BUG_ON(*start < search_start);
320
321         if (*start + num_bytes > search_end) {
322                 ret = -ENOSPC;
323                 goto error;
324         }
325         /* check for pending inserts here */
326         return 0;
327
328 error:
329         btrfs_release_path(root, path);
330         return ret;
331 }
332
333 int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
334                            struct btrfs_device *device,
335                            u64 chunk_tree, u64 chunk_objectid,
336                            u64 chunk_offset,
337                            u64 num_bytes, u64 *start)
338 {
339         int ret;
340         struct btrfs_path *path;
341         struct btrfs_root *root = device->dev_root;
342         struct btrfs_dev_extent *extent;
343         struct extent_buffer *leaf;
344         struct btrfs_key key;
345
346         path = btrfs_alloc_path();
347         if (!path)
348                 return -ENOMEM;
349
350         ret = find_free_dev_extent(trans, device, path, num_bytes, start);
351         if (ret) {
352                 goto err;
353         }
354
355         key.objectid = device->devid;
356         key.offset = *start;
357         key.type = BTRFS_DEV_EXTENT_KEY;
358         ret = btrfs_insert_empty_item(trans, root, path, &key,
359                                       sizeof(*extent));
360         BUG_ON(ret);
361
362         leaf = path->nodes[0];
363         extent = btrfs_item_ptr(leaf, path->slots[0],
364                                 struct btrfs_dev_extent);
365         btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
366         btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
367         btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
368
369         write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
370                     (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
371                     BTRFS_UUID_SIZE);
372
373         btrfs_set_dev_extent_length(leaf, extent, num_bytes);
374         btrfs_mark_buffer_dirty(leaf);
375 err:
376         btrfs_free_path(path);
377         return ret;
378 }
379
380 static int find_next_chunk(struct btrfs_root *root, u64 objectid, u64 *offset)
381 {
382         struct btrfs_path *path;
383         int ret;
384         struct btrfs_key key;
385         struct btrfs_chunk *chunk;
386         struct btrfs_key found_key;
387
388         path = btrfs_alloc_path();
389         BUG_ON(!path);
390
391         key.objectid = objectid;
392         key.offset = (u64)-1;
393         key.type = BTRFS_CHUNK_ITEM_KEY;
394
395         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
396         if (ret < 0)
397                 goto error;
398
399         BUG_ON(ret == 0);
400
401         ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
402         if (ret) {
403                 *offset = 0;
404         } else {
405                 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
406                                       path->slots[0]);
407                 if (found_key.objectid != objectid)
408                         *offset = 0;
409                 else {
410                         chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
411                                                struct btrfs_chunk);
412                         *offset = found_key.offset +
413                                 btrfs_chunk_length(path->nodes[0], chunk);
414                 }
415         }
416         ret = 0;
417 error:
418         btrfs_free_path(path);
419         return ret;
420 }
421
422 static int find_next_devid(struct btrfs_root *root, struct btrfs_path *path,
423                            u64 *objectid)
424 {
425         int ret;
426         struct btrfs_key key;
427         struct btrfs_key found_key;
428
429         key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
430         key.type = BTRFS_DEV_ITEM_KEY;
431         key.offset = (u64)-1;
432
433         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
434         if (ret < 0)
435                 goto error;
436
437         BUG_ON(ret == 0);
438
439         ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
440                                   BTRFS_DEV_ITEM_KEY);
441         if (ret) {
442                 *objectid = 1;
443         } else {
444                 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
445                                       path->slots[0]);
446                 *objectid = found_key.offset + 1;
447         }
448         ret = 0;
449 error:
450         btrfs_release_path(root, path);
451         return ret;
452 }
453
454 /*
455  * the device information is stored in the chunk root
456  * the btrfs_device struct should be fully filled in
457  */
458 int btrfs_add_device(struct btrfs_trans_handle *trans,
459                      struct btrfs_root *root,
460                      struct btrfs_device *device)
461 {
462         int ret;
463         struct btrfs_path *path;
464         struct btrfs_dev_item *dev_item;
465         struct extent_buffer *leaf;
466         struct btrfs_key key;
467         unsigned long ptr;
468         u64 free_devid;
469
470         root = root->fs_info->chunk_root;
471
472         path = btrfs_alloc_path();
473         if (!path)
474                 return -ENOMEM;
475
476         ret = find_next_devid(root, path, &free_devid);
477         if (ret)
478                 goto out;
479
480         key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
481         key.type = BTRFS_DEV_ITEM_KEY;
482         key.offset = free_devid;
483
484         ret = btrfs_insert_empty_item(trans, root, path, &key,
485                                       sizeof(*dev_item));
486         if (ret)
487                 goto out;
488
489         leaf = path->nodes[0];
490         dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
491
492         device->devid = free_devid;
493         btrfs_set_device_id(leaf, dev_item, device->devid);
494         btrfs_set_device_type(leaf, dev_item, device->type);
495         btrfs_set_device_io_align(leaf, dev_item, device->io_align);
496         btrfs_set_device_io_width(leaf, dev_item, device->io_width);
497         btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
498         btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
499         btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
500         btrfs_set_device_group(leaf, dev_item, 0);
501         btrfs_set_device_seek_speed(leaf, dev_item, 0);
502         btrfs_set_device_bandwidth(leaf, dev_item, 0);
503
504         ptr = (unsigned long)btrfs_device_uuid(dev_item);
505         write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
506         btrfs_mark_buffer_dirty(leaf);
507         ret = 0;
508
509 out:
510         btrfs_free_path(path);
511         return ret;
512 }
513
514 int btrfs_update_device(struct btrfs_trans_handle *trans,
515                         struct btrfs_device *device)
516 {
517         int ret;
518         struct btrfs_path *path;
519         struct btrfs_root *root;
520         struct btrfs_dev_item *dev_item;
521         struct extent_buffer *leaf;
522         struct btrfs_key key;
523
524         root = device->dev_root->fs_info->chunk_root;
525
526         path = btrfs_alloc_path();
527         if (!path)
528                 return -ENOMEM;
529
530         key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
531         key.type = BTRFS_DEV_ITEM_KEY;
532         key.offset = device->devid;
533
534         ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
535         if (ret < 0)
536                 goto out;
537
538         if (ret > 0) {
539                 ret = -ENOENT;
540                 goto out;
541         }
542
543         leaf = path->nodes[0];
544         dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
545
546         btrfs_set_device_id(leaf, dev_item, device->devid);
547         btrfs_set_device_type(leaf, dev_item, device->type);
548         btrfs_set_device_io_align(leaf, dev_item, device->io_align);
549         btrfs_set_device_io_width(leaf, dev_item, device->io_width);
550         btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
551         btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
552         btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
553         btrfs_mark_buffer_dirty(leaf);
554
555 out:
556         btrfs_free_path(path);
557         return ret;
558 }
559
560 int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
561                            struct btrfs_root *root,
562                            struct btrfs_key *key,
563                            struct btrfs_chunk *chunk, int item_size)
564 {
565         struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
566         struct btrfs_disk_key disk_key;
567         u32 array_size;
568         u8 *ptr;
569
570         array_size = btrfs_super_sys_array_size(super_copy);
571         if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
572                 return -EFBIG;
573
574         ptr = super_copy->sys_chunk_array + array_size;
575         btrfs_cpu_key_to_disk(&disk_key, key);
576         memcpy(ptr, &disk_key, sizeof(disk_key));
577         ptr += sizeof(disk_key);
578         memcpy(ptr, chunk, item_size);
579         item_size += sizeof(disk_key);
580         btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
581         return 0;
582 }
583
584 int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
585                       struct btrfs_root *extent_root, u64 *start,
586                       u64 *num_bytes, u64 type)
587 {
588         u64 dev_offset;
589         struct btrfs_fs_info *info = extent_root->fs_info;
590         struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
591         struct btrfs_stripe *stripes;
592         struct btrfs_device *device = NULL;
593         struct btrfs_chunk *chunk;
594         struct list_head private_devs;
595         struct list_head *dev_list = &extent_root->fs_info->fs_devices->devices;
596         struct list_head *cur;
597         struct map_lookup *map;
598         u64 physical;
599         u64 calc_size = 8 * 1024 * 1024;
600         u64 min_free = calc_size;
601         u64 avail;
602         u64 max_avail = 0;
603         int num_stripes = 1;
604         int sub_stripes = 0;
605         int looped = 0;
606         int ret;
607         int index;
608         int stripe_len = 64 * 1024;
609         struct btrfs_key key;
610
611         if (list_empty(dev_list)) {
612                 return -ENOSPC;
613         }
614
615         if (type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
616                     BTRFS_BLOCK_GROUP_RAID10 |
617                     BTRFS_BLOCK_GROUP_DUP)) {
618                 if (type & BTRFS_BLOCK_GROUP_SYSTEM)
619                         calc_size = 128 * 1024 * 1024;
620                 else
621                         calc_size = 1024 * 1024 * 1024;
622         }
623         if (type & BTRFS_BLOCK_GROUP_RAID1) {
624                 num_stripes = min_t(u64, 2,
625                                   btrfs_super_num_devices(&info->super_copy));
626         }
627         if (type & BTRFS_BLOCK_GROUP_DUP)
628                 num_stripes = 2;
629         if (type & (BTRFS_BLOCK_GROUP_RAID0))
630                 num_stripes = btrfs_super_num_devices(&info->super_copy);
631         if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
632                 num_stripes = btrfs_super_num_devices(&info->super_copy);
633                 if (num_stripes < 4)
634                         return -ENOSPC;
635                 num_stripes &= ~(u32)1;
636                 sub_stripes = 2;
637         }
638 again:
639         INIT_LIST_HEAD(&private_devs);
640         cur = dev_list->next;
641         index = 0;
642
643         if (type & BTRFS_BLOCK_GROUP_DUP)
644                 min_free = calc_size * 2;
645
646         /* build a private list of devices we will allocate from */
647         while(index < num_stripes) {
648                 device = list_entry(cur, struct btrfs_device, dev_list);
649                 avail = device->total_bytes - device->bytes_used;
650                 cur = cur->next;
651                 if (avail > max_avail)
652                         max_avail = avail;
653                 if (avail >= min_free) {
654                         list_move_tail(&device->dev_list, &private_devs);
655                         index++;
656                         if (type & BTRFS_BLOCK_GROUP_DUP)
657                                 index++;
658                 }
659                 if (cur == dev_list)
660                         break;
661         }
662         if (index < num_stripes) {
663                 list_splice(&private_devs, dev_list);
664                 if (!looped && max_avail > 0) {
665                         looped = 1;
666                         calc_size = max_avail;
667                         goto again;
668                 }
669                 return -ENOSPC;
670         }
671
672         key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
673         key.type = BTRFS_CHUNK_ITEM_KEY;
674         ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
675                               &key.offset);
676         if (ret)
677                 return ret;
678
679         chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS);
680         if (!chunk)
681                 return -ENOMEM;
682
683         map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
684         if (!map) {
685                 kfree(chunk);
686                 return -ENOMEM;
687         }
688
689         stripes = &chunk->stripe;
690
691         if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
692                 *num_bytes = calc_size;
693         else if (type & BTRFS_BLOCK_GROUP_RAID10)
694                 *num_bytes = calc_size * num_stripes / sub_stripes;
695         else
696                 *num_bytes = calc_size * num_stripes;
697
698         index = 0;
699 printk("new chunk type %Lu start %Lu size %Lu\n", type, key.offset, *num_bytes);
700         while(index < num_stripes) {
701                 struct btrfs_stripe *stripe;
702                 BUG_ON(list_empty(&private_devs));
703                 cur = private_devs.next;
704                 device = list_entry(cur, struct btrfs_device, dev_list);
705
706                 /* loop over this device again if we're doing a dup group */
707                 if (!(type & BTRFS_BLOCK_GROUP_DUP) ||
708                     (index == num_stripes - 1))
709                         list_move_tail(&device->dev_list, dev_list);
710
711                 ret = btrfs_alloc_dev_extent(trans, device,
712                              info->chunk_root->root_key.objectid,
713                              BTRFS_FIRST_CHUNK_TREE_OBJECTID, key.offset,
714                              calc_size, &dev_offset);
715                 BUG_ON(ret);
716 printk("\talloc chunk size %llu from dev %llu phys %llu\n",
717         (unsigned long long)calc_size,
718         (unsigned long long)device->devid,
719         (unsigned long long)dev_offset);
720                 device->bytes_used += calc_size;
721                 ret = btrfs_update_device(trans, device);
722                 BUG_ON(ret);
723
724                 map->stripes[index].dev = device;
725                 map->stripes[index].physical = dev_offset;
726                 stripe = stripes + index;
727                 btrfs_set_stack_stripe_devid(stripe, device->devid);
728                 btrfs_set_stack_stripe_offset(stripe, dev_offset);
729                 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
730                 physical = dev_offset;
731                 index++;
732         }
733         BUG_ON(!list_empty(&private_devs));
734
735         /* key was set above */
736         btrfs_set_stack_chunk_length(chunk, *num_bytes);
737         btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
738         btrfs_set_stack_chunk_stripe_len(chunk, stripe_len);
739         btrfs_set_stack_chunk_type(chunk, type);
740         btrfs_set_stack_chunk_num_stripes(chunk, num_stripes);
741         btrfs_set_stack_chunk_io_align(chunk, stripe_len);
742         btrfs_set_stack_chunk_io_width(chunk, stripe_len);
743         btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
744         btrfs_set_stack_chunk_sub_stripes(chunk, sub_stripes);
745         map->sector_size = extent_root->sectorsize;
746         map->stripe_len = stripe_len;
747         map->io_align = stripe_len;
748         map->io_width = stripe_len;
749         map->type = type;
750         map->num_stripes = num_stripes;
751         map->sub_stripes = sub_stripes;
752
753         ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
754                                 btrfs_chunk_item_size(num_stripes));
755         BUG_ON(ret);
756         *start = key.offset;;
757
758         map->ce.start = key.offset;
759         map->ce.size = *num_bytes;
760
761         ret = insert_existing_cache_extent(
762                            &extent_root->fs_info->mapping_tree.cache_tree,
763                            &map->ce);
764         BUG_ON(ret);
765
766         if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
767                 ret = btrfs_add_system_chunk(trans, chunk_root, &key,
768                                     chunk, btrfs_chunk_item_size(num_stripes));
769                 BUG_ON(ret);
770         }
771
772         kfree(chunk);
773         return ret;
774 }
775
776 void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
777 {
778         cache_tree_init(&tree->cache_tree);
779 }
780
781 int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
782 {
783         struct cache_extent *ce;
784         struct map_lookup *map;
785         int ret;
786         u64 offset;
787
788         ce = find_first_cache_extent(&map_tree->cache_tree, logical);
789         BUG_ON(!ce);
790         BUG_ON(ce->start > logical || ce->start + ce->size < logical);
791         map = container_of(ce, struct map_lookup, ce);
792
793         offset = logical - ce->start;
794         if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
795                 ret = map->num_stripes;
796         else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
797                 ret = map->sub_stripes;
798         else
799                 ret = 1;
800         return ret;
801 }
802
803 int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
804                     u64 logical, u64 *length,
805                     struct btrfs_multi_bio **multi_ret, int mirror_num)
806 {
807         struct cache_extent *ce;
808         struct map_lookup *map;
809         u64 offset;
810         u64 stripe_offset;
811         u64 stripe_nr;
812         int stripes_allocated = 8;
813         int stripes_required = 1;
814         int stripe_index;
815         int i;
816         struct btrfs_multi_bio *multi = NULL;
817
818         if (multi_ret && rw == READ) {
819                 stripes_allocated = 1;
820         }
821 again:
822         if (multi_ret) {
823                 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
824                                 GFP_NOFS);
825                 if (!multi)
826                         return -ENOMEM;
827         }
828
829         ce = find_first_cache_extent(&map_tree->cache_tree, logical);
830         BUG_ON(!ce);
831         BUG_ON(ce->start > logical || ce->start + ce->size < logical);
832         map = container_of(ce, struct map_lookup, ce);
833         offset = logical - ce->start;
834
835         if (rw == WRITE) {
836                 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
837                                  BTRFS_BLOCK_GROUP_DUP)) {
838                         stripes_required = map->num_stripes;
839                 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
840                         stripes_required = map->sub_stripes;
841                 }
842         }
843         /* if our multi bio struct is too small, back off and try again */
844         if (multi_ret && rw == WRITE &&
845             stripes_allocated < stripes_required) {
846                 stripes_allocated = map->num_stripes;
847                 kfree(multi);
848                 goto again;
849         }
850         stripe_nr = offset;
851         /*
852          * stripe_nr counts the total number of stripes we have to stride
853          * to get to this block
854          */
855         stripe_nr = stripe_nr / map->stripe_len;
856
857         stripe_offset = stripe_nr * map->stripe_len;
858         BUG_ON(offset < stripe_offset);
859
860         /* stripe_offset is the offset of this block in its stripe*/
861         stripe_offset = offset - stripe_offset;
862
863         if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
864                          BTRFS_BLOCK_GROUP_RAID10 |
865                          BTRFS_BLOCK_GROUP_DUP)) {
866                 /* we limit the length of each bio to what fits in a stripe */
867                 *length = min_t(u64, ce->size - offset,
868                               map->stripe_len - stripe_offset);
869         } else {
870                 *length = ce->size - offset;
871         }
872
873         if (!multi_ret)
874                 goto out;
875
876         multi->num_stripes = 1;
877         stripe_index = 0;
878         if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
879                 if (rw == WRITE)
880                         multi->num_stripes = map->num_stripes;
881                 else if (mirror_num)
882                         stripe_index = mirror_num - 1;
883                 else
884                         stripe_index = stripe_nr % map->num_stripes;
885         } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
886                 int factor = map->num_stripes / map->sub_stripes;
887
888                 stripe_index = stripe_nr % factor;
889                 stripe_index *= map->sub_stripes;
890
891                 if (rw == WRITE)
892                         multi->num_stripes = map->sub_stripes;
893                 else if (mirror_num)
894                         stripe_index += mirror_num - 1;
895                 else
896                         stripe_index = stripe_nr % map->sub_stripes;
897
898                 stripe_nr = stripe_nr / factor;
899         } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
900                 if (rw == WRITE)
901                         multi->num_stripes = map->num_stripes;
902                 else if (mirror_num)
903                         stripe_index = mirror_num - 1;
904         } else {
905                 /*
906                  * after this do_div call, stripe_nr is the number of stripes
907                  * on this device we have to walk to find the data, and
908                  * stripe_index is the number of our device in the stripe array
909                  */
910                 stripe_index = stripe_nr % map->num_stripes;
911                 stripe_nr = stripe_nr / map->num_stripes;
912         }
913         BUG_ON(stripe_index >= map->num_stripes);
914
915         BUG_ON(stripe_index != 0 && multi->num_stripes > 1);
916         for (i = 0; i < multi->num_stripes; i++) {
917                 multi->stripes[i].physical =
918                         map->stripes[stripe_index].physical + stripe_offset +
919                         stripe_nr * map->stripe_len;
920                 multi->stripes[i].dev = map->stripes[stripe_index].dev;
921                 stripe_index++;
922         }
923         *multi_ret = multi;
924 out:
925         return 0;
926 }
927
928 struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
929                                        u8 *uuid)
930 {
931         struct list_head *head = &root->fs_info->fs_devices->devices;
932
933         return __find_device(head, devid, uuid);
934 }
935
936 int btrfs_bootstrap_super_map(struct btrfs_mapping_tree *map_tree,
937                               struct btrfs_fs_devices *fs_devices)
938 {
939         struct map_lookup *map;
940         u64 logical = BTRFS_SUPER_INFO_OFFSET;
941         u64 length = BTRFS_SUPER_INFO_SIZE;
942         int num_stripes = 0;
943         int sub_stripes = 0;
944         int ret;
945         int i;
946         struct list_head *cur;
947
948         list_for_each(cur, &fs_devices->devices) {
949                 num_stripes++;
950         }
951         map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
952         if (!map)
953                 return -ENOMEM;
954
955         map->ce.start = logical;
956         map->ce.size = length;
957         map->num_stripes = num_stripes;
958         map->sub_stripes = sub_stripes;
959         map->io_width = length;
960         map->io_align = length;
961         map->sector_size = length;
962         map->stripe_len = length;
963         map->type = BTRFS_BLOCK_GROUP_RAID1;
964
965         i = 0;
966         list_for_each(cur, &fs_devices->devices) {
967                 struct btrfs_device *device = list_entry(cur,
968                                                          struct btrfs_device,
969                                                          dev_list);
970                 map->stripes[i].physical = logical;
971                 map->stripes[i].dev = device;
972                 i++;
973         }
974         ret = insert_existing_cache_extent(&map_tree->cache_tree, &map->ce);
975         if (ret == -EEXIST) {
976                 struct cache_extent *old;
977                 struct map_lookup *old_map;
978                 old = find_cache_extent(&map_tree->cache_tree, logical, length);
979                 old_map = container_of(old, struct map_lookup, ce);
980                 remove_cache_extent(&map_tree->cache_tree, old);
981                 kfree(old_map);
982                 ret = insert_existing_cache_extent(&map_tree->cache_tree,
983                                                    &map->ce);
984         }
985         BUG_ON(ret);
986         return 0;
987 }
988
989 static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
990                           struct extent_buffer *leaf,
991                           struct btrfs_chunk *chunk)
992 {
993         struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
994         struct map_lookup *map;
995         struct cache_extent *ce;
996         u64 logical;
997         u64 length;
998         u64 devid;
999         u64 super_offset_diff = 0;
1000         u8 uuid[BTRFS_UUID_SIZE];
1001         int num_stripes;
1002         int ret;
1003         int i;
1004
1005         logical = key->offset;
1006         length = btrfs_chunk_length(leaf, chunk);
1007
1008         if (logical < BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE) {
1009                 super_offset_diff = BTRFS_SUPER_INFO_OFFSET +
1010                         BTRFS_SUPER_INFO_SIZE - logical;
1011                 logical = BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE;
1012         }
1013
1014         ce = find_first_cache_extent(&map_tree->cache_tree, logical);
1015
1016         /* already mapped? */
1017         if (ce && ce->start <= logical && ce->start + ce->size > logical) {
1018                 return 0;
1019         }
1020
1021         num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
1022         map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
1023         if (!map)
1024                 return -ENOMEM;
1025
1026         map->ce.start = logical;
1027         map->ce.size = length - super_offset_diff;
1028         map->num_stripes = num_stripes;
1029         map->io_width = btrfs_chunk_io_width(leaf, chunk);
1030         map->io_align = btrfs_chunk_io_align(leaf, chunk);
1031         map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
1032         map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
1033         map->type = btrfs_chunk_type(leaf, chunk);
1034         map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
1035
1036         for (i = 0; i < num_stripes; i++) {
1037                 map->stripes[i].physical =
1038                         btrfs_stripe_offset_nr(leaf, chunk, i) +
1039                                 super_offset_diff;
1040                 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
1041                 read_extent_buffer(leaf, uuid, (unsigned long)
1042                                    btrfs_stripe_dev_uuid_nr(chunk, i),
1043                                    BTRFS_UUID_SIZE);
1044                 map->stripes[i].dev = btrfs_find_device(root, devid, uuid);
1045                 if (!map->stripes[i].dev) {
1046                         kfree(map);
1047                         return -EIO;
1048                 }
1049
1050         }
1051         ret = insert_existing_cache_extent(&map_tree->cache_tree, &map->ce);
1052         BUG_ON(ret);
1053
1054         return 0;
1055 }
1056
1057 static int fill_device_from_item(struct extent_buffer *leaf,
1058                                  struct btrfs_dev_item *dev_item,
1059                                  struct btrfs_device *device)
1060 {
1061         unsigned long ptr;
1062
1063         device->devid = btrfs_device_id(leaf, dev_item);
1064         device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
1065         device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
1066         device->type = btrfs_device_type(leaf, dev_item);
1067         device->io_align = btrfs_device_io_align(leaf, dev_item);
1068         device->io_width = btrfs_device_io_width(leaf, dev_item);
1069         device->sector_size = btrfs_device_sector_size(leaf, dev_item);
1070
1071         ptr = (unsigned long)btrfs_device_uuid(dev_item);
1072         read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1073
1074         return 0;
1075 }
1076
1077 static int read_one_dev(struct btrfs_root *root,
1078                         struct extent_buffer *leaf,
1079                         struct btrfs_dev_item *dev_item)
1080 {
1081         struct btrfs_device *device;
1082         u64 devid;
1083         int ret = 0;
1084         u8 dev_uuid[BTRFS_UUID_SIZE];
1085
1086         devid = btrfs_device_id(leaf, dev_item);
1087         read_extent_buffer(leaf, dev_uuid,
1088                            (unsigned long)btrfs_device_uuid(dev_item),
1089                            BTRFS_UUID_SIZE);
1090         device = btrfs_find_device(root, devid, dev_uuid);
1091         if (!device) {
1092                 printk("warning devid %llu not found already\n",
1093                         (unsigned long long)devid);
1094                 device = kmalloc(sizeof(*device), GFP_NOFS);
1095                 if (!device)
1096                         return -ENOMEM;
1097                 device->total_ios = 0;
1098                 list_add(&device->dev_list,
1099                          &root->fs_info->fs_devices->devices);
1100         }
1101
1102         fill_device_from_item(leaf, dev_item, device);
1103         device->dev_root = root->fs_info->dev_root;
1104         return ret;
1105 }
1106
1107 int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
1108 {
1109         struct btrfs_dev_item *dev_item;
1110
1111         dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
1112                                                      dev_item);
1113         return read_one_dev(root, buf, dev_item);
1114 }
1115
1116 int btrfs_read_sys_array(struct btrfs_root *root)
1117 {
1118         struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1119         struct extent_buffer *sb = root->fs_info->sb_buffer;
1120         struct btrfs_disk_key *disk_key;
1121         struct btrfs_chunk *chunk;
1122         struct btrfs_key key;
1123         u32 num_stripes;
1124         u32 array_size;
1125         u32 len = 0;
1126         u8 *ptr;
1127         unsigned long sb_ptr;
1128         u32 cur;
1129         int ret;
1130
1131         array_size = btrfs_super_sys_array_size(super_copy);
1132
1133         /*
1134          * we do this loop twice, once for the device items and
1135          * once for all of the chunks.  This way there are device
1136          * structs filled in for every chunk
1137          */
1138         ptr = super_copy->sys_chunk_array;
1139         sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
1140         cur = 0;
1141
1142         while (cur < array_size) {
1143                 disk_key = (struct btrfs_disk_key *)ptr;
1144                 btrfs_disk_key_to_cpu(&key, disk_key);
1145
1146                 len = sizeof(*disk_key);
1147                 ptr += len;
1148                 sb_ptr += len;
1149                 cur += len;
1150
1151                 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1152                         chunk = (struct btrfs_chunk *)sb_ptr;
1153                         ret = read_one_chunk(root, &key, sb, chunk);
1154                         BUG_ON(ret);
1155                         num_stripes = btrfs_chunk_num_stripes(sb, chunk);
1156                         len = btrfs_chunk_item_size(num_stripes);
1157                 } else {
1158                         BUG();
1159                 }
1160                 ptr += len;
1161                 sb_ptr += len;
1162                 cur += len;
1163         }
1164         return 0;
1165 }
1166
1167 int btrfs_read_chunk_tree(struct btrfs_root *root)
1168 {
1169         struct btrfs_path *path;
1170         struct extent_buffer *leaf;
1171         struct btrfs_key key;
1172         struct btrfs_key found_key;
1173         int ret;
1174         int slot;
1175
1176         root = root->fs_info->chunk_root;
1177
1178         path = btrfs_alloc_path();
1179         if (!path)
1180                 return -ENOMEM;
1181
1182         /* first we search for all of the device items, and then we
1183          * read in all of the chunk items.  This way we can create chunk
1184          * mappings that reference all of the devices that are afound
1185          */
1186         key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1187         key.offset = 0;
1188         key.type = 0;
1189 again:
1190         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1191         while(1) {
1192                 leaf = path->nodes[0];
1193                 slot = path->slots[0];
1194                 if (slot >= btrfs_header_nritems(leaf)) {
1195                         ret = btrfs_next_leaf(root, path);
1196                         if (ret == 0)
1197                                 continue;
1198                         if (ret < 0)
1199                                 goto error;
1200                         break;
1201                 }
1202                 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1203                 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
1204                         if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
1205                                 break;
1206                         if (found_key.type == BTRFS_DEV_ITEM_KEY) {
1207                                 struct btrfs_dev_item *dev_item;
1208                                 dev_item = btrfs_item_ptr(leaf, slot,
1209                                                   struct btrfs_dev_item);
1210                                 ret = read_one_dev(root, leaf, dev_item);
1211                                 BUG_ON(ret);
1212                         }
1213                 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
1214                         struct btrfs_chunk *chunk;
1215                         chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
1216                         ret = read_one_chunk(root, &found_key, leaf, chunk);
1217                 }
1218                 path->slots[0]++;
1219         }
1220         if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
1221                 key.objectid = 0;
1222                 btrfs_release_path(root, path);
1223                 goto again;
1224         }
1225
1226         btrfs_free_path(path);
1227         ret = 0;
1228 error:
1229         return ret;
1230 }
1231