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