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