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