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