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