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