Btrfs: Write out all super blocks on commit, and bring back proper barrier support
[safe/jmp/linux-2.6] / fs / btrfs / volumes.c
1 /*
2  * Copyright (C) 2007 Oracle.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License v2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public
14  * License along with this program; if not, write to the
15  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16  * Boston, MA 021110-1307, USA.
17  */
18 #include <linux/sched.h>
19 #include <linux/bio.h>
20 #include <linux/buffer_head.h>
21 #include <asm/div64.h>
22 #include "ctree.h"
23 #include "extent_map.h"
24 #include "disk-io.h"
25 #include "transaction.h"
26 #include "print-tree.h"
27 #include "volumes.h"
28
29 struct map_lookup {
30         u64 type;
31         int io_align;
32         int io_width;
33         int stripe_len;
34         int sector_size;
35         int num_stripes;
36         struct btrfs_bio_stripe stripes[];
37 };
38
39 #define map_lookup_size(n) (sizeof(struct map_lookup) + \
40                             (sizeof(struct btrfs_bio_stripe) * (n)))
41
42 static DEFINE_MUTEX(uuid_mutex);
43 static LIST_HEAD(fs_uuids);
44
45 int btrfs_cleanup_fs_uuids(void)
46 {
47         struct btrfs_fs_devices *fs_devices;
48         struct list_head *uuid_cur;
49         struct list_head *devices_cur;
50         struct btrfs_device *dev;
51
52         list_for_each(uuid_cur, &fs_uuids) {
53                 fs_devices = list_entry(uuid_cur, struct btrfs_fs_devices,
54                                         list);
55                 while(!list_empty(&fs_devices->devices)) {
56                         devices_cur = fs_devices->devices.next;
57                         dev = list_entry(devices_cur, struct btrfs_device,
58                                          dev_list);
59                         printk("uuid cleanup finds %s\n", dev->name);
60                         if (dev->bdev) {
61                                 printk("closing\n");
62                                 close_bdev_excl(dev->bdev);
63                         }
64                         list_del(&dev->dev_list);
65                         kfree(dev);
66                 }
67         }
68         return 0;
69 }
70
71 static struct btrfs_device *__find_device(struct list_head *head, u64 devid)
72 {
73         struct btrfs_device *dev;
74         struct list_head *cur;
75
76         list_for_each(cur, head) {
77                 dev = list_entry(cur, struct btrfs_device, dev_list);
78                 if (dev->devid == devid)
79                         return dev;
80         }
81         return NULL;
82 }
83
84 static struct btrfs_fs_devices *find_fsid(u8 *fsid)
85 {
86         struct list_head *cur;
87         struct btrfs_fs_devices *fs_devices;
88
89         list_for_each(cur, &fs_uuids) {
90                 fs_devices = list_entry(cur, struct btrfs_fs_devices, list);
91                 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
92                         return fs_devices;
93         }
94         return NULL;
95 }
96
97 static int device_list_add(const char *path,
98                            struct btrfs_super_block *disk_super,
99                            u64 devid, struct btrfs_fs_devices **fs_devices_ret)
100 {
101         struct btrfs_device *device;
102         struct btrfs_fs_devices *fs_devices;
103         u64 found_transid = btrfs_super_generation(disk_super);
104
105         fs_devices = find_fsid(disk_super->fsid);
106         if (!fs_devices) {
107                 fs_devices = kmalloc(sizeof(*fs_devices), GFP_NOFS);
108                 if (!fs_devices)
109                         return -ENOMEM;
110                 INIT_LIST_HEAD(&fs_devices->devices);
111                 list_add(&fs_devices->list, &fs_uuids);
112                 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
113                 fs_devices->latest_devid = devid;
114                 fs_devices->latest_trans = found_transid;
115                 fs_devices->lowest_devid = (u64)-1;
116                 fs_devices->num_devices = 0;
117                 device = NULL;
118         } else {
119                 device = __find_device(&fs_devices->devices, devid);
120         }
121         if (!device) {
122                 device = kzalloc(sizeof(*device), GFP_NOFS);
123                 if (!device) {
124                         /* we can safely leave the fs_devices entry around */
125                         return -ENOMEM;
126                 }
127                 device->devid = devid;
128                 device->barriers = 1;
129                 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                 bio_endio(bio, err);
964         } else {
965                 bio_put(bio);
966         }
967 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
968         return 0;
969 #endif
970 }
971
972 int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
973                   int mirror_num)
974 {
975         struct btrfs_mapping_tree *map_tree;
976         struct btrfs_device *dev;
977         struct bio *first_bio = bio;
978         u64 logical = bio->bi_sector << 9;
979         u64 length = 0;
980         u64 map_length;
981         struct bio_vec *bvec;
982         struct btrfs_multi_bio *multi = NULL;
983         int i;
984         int ret;
985         int dev_nr = 0;
986         int total_devs = 1;
987
988         bio_for_each_segment(bvec, bio, i) {
989                 length += bvec->bv_len;
990         }
991
992         map_tree = &root->fs_info->mapping_tree;
993         map_length = length;
994
995         ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
996                               mirror_num);
997         BUG_ON(ret);
998
999         total_devs = multi->num_stripes;
1000         if (map_length < length) {
1001                 printk("mapping failed logical %Lu bio len %Lu "
1002                        "len %Lu\n", logical, length, map_length);
1003                 BUG();
1004         }
1005         multi->end_io = first_bio->bi_end_io;
1006         multi->private = first_bio->bi_private;
1007         atomic_set(&multi->stripes_pending, multi->num_stripes);
1008
1009         while(dev_nr < total_devs) {
1010                 if (total_devs > 1) {
1011                         if (dev_nr < total_devs - 1) {
1012                                 bio = bio_clone(first_bio, GFP_NOFS);
1013                                 BUG_ON(!bio);
1014                         } else {
1015                                 bio = first_bio;
1016                         }
1017                         bio->bi_private = multi;
1018                         bio->bi_end_io = end_bio_multi_stripe;
1019                 }
1020                 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
1021                 dev = multi->stripes[dev_nr].dev;
1022                 bio->bi_bdev = dev->bdev;
1023                 spin_lock(&dev->io_lock);
1024                 dev->total_ios++;
1025                 spin_unlock(&dev->io_lock);
1026                 submit_bio(rw, bio);
1027                 dev_nr++;
1028         }
1029         if (total_devs == 1)
1030                 kfree(multi);
1031         return 0;
1032 }
1033
1034 struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid)
1035 {
1036         struct list_head *head = &root->fs_info->fs_devices->devices;
1037
1038         return __find_device(head, devid);
1039 }
1040
1041 static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
1042                           struct extent_buffer *leaf,
1043                           struct btrfs_chunk *chunk)
1044 {
1045         struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
1046         struct map_lookup *map;
1047         struct extent_map *em;
1048         u64 logical;
1049         u64 length;
1050         u64 devid;
1051         int num_stripes;
1052         int ret;
1053         int i;
1054
1055         logical = key->objectid;
1056         length = key->offset;
1057         spin_lock(&map_tree->map_tree.lock);
1058         em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
1059
1060         /* already mapped? */
1061         if (em && em->start <= logical && em->start + em->len > logical) {
1062                 free_extent_map(em);
1063                 spin_unlock(&map_tree->map_tree.lock);
1064                 return 0;
1065         } else if (em) {
1066                 free_extent_map(em);
1067         }
1068         spin_unlock(&map_tree->map_tree.lock);
1069
1070         map = kzalloc(sizeof(*map), GFP_NOFS);
1071         if (!map)
1072                 return -ENOMEM;
1073
1074         em = alloc_extent_map(GFP_NOFS);
1075         if (!em)
1076                 return -ENOMEM;
1077         num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
1078         map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
1079         if (!map) {
1080                 free_extent_map(em);
1081                 return -ENOMEM;
1082         }
1083
1084         em->bdev = (struct block_device *)map;
1085         em->start = logical;
1086         em->len = length;
1087         em->block_start = 0;
1088
1089         map->num_stripes = num_stripes;
1090         map->io_width = btrfs_chunk_io_width(leaf, chunk);
1091         map->io_align = btrfs_chunk_io_align(leaf, chunk);
1092         map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
1093         map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
1094         map->type = btrfs_chunk_type(leaf, chunk);
1095         for (i = 0; i < num_stripes; i++) {
1096                 map->stripes[i].physical =
1097                         btrfs_stripe_offset_nr(leaf, chunk, i);
1098                 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
1099                 map->stripes[i].dev = btrfs_find_device(root, devid);
1100                 if (!map->stripes[i].dev) {
1101                         kfree(map);
1102                         free_extent_map(em);
1103                         return -EIO;
1104                 }
1105         }
1106
1107         spin_lock(&map_tree->map_tree.lock);
1108         ret = add_extent_mapping(&map_tree->map_tree, em);
1109         BUG_ON(ret);
1110         spin_unlock(&map_tree->map_tree.lock);
1111         free_extent_map(em);
1112
1113         return 0;
1114 }
1115
1116 static int fill_device_from_item(struct extent_buffer *leaf,
1117                                  struct btrfs_dev_item *dev_item,
1118                                  struct btrfs_device *device)
1119 {
1120         unsigned long ptr;
1121
1122         device->devid = btrfs_device_id(leaf, dev_item);
1123         device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
1124         device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
1125         device->type = btrfs_device_type(leaf, dev_item);
1126         device->io_align = btrfs_device_io_align(leaf, dev_item);
1127         device->io_width = btrfs_device_io_width(leaf, dev_item);
1128         device->sector_size = btrfs_device_sector_size(leaf, dev_item);
1129
1130         ptr = (unsigned long)btrfs_device_uuid(dev_item);
1131         read_extent_buffer(leaf, device->uuid, ptr, BTRFS_DEV_UUID_SIZE);
1132
1133         return 0;
1134 }
1135
1136 static int read_one_dev(struct btrfs_root *root,
1137                         struct extent_buffer *leaf,
1138                         struct btrfs_dev_item *dev_item)
1139 {
1140         struct btrfs_device *device;
1141         u64 devid;
1142         int ret;
1143
1144         devid = btrfs_device_id(leaf, dev_item);
1145         device = btrfs_find_device(root, devid);
1146         if (!device) {
1147                 printk("warning devid %Lu not found already\n", devid);
1148                 device = kzalloc(sizeof(*device), GFP_NOFS);
1149                 if (!device)
1150                         return -ENOMEM;
1151                 list_add(&device->dev_list,
1152                          &root->fs_info->fs_devices->devices);
1153                 device->total_ios = 0;
1154                 spin_lock_init(&device->io_lock);
1155         }
1156
1157         fill_device_from_item(leaf, dev_item, device);
1158         device->dev_root = root->fs_info->dev_root;
1159         ret = 0;
1160 #if 0
1161         ret = btrfs_open_device(device);
1162         if (ret) {
1163                 kfree(device);
1164         }
1165 #endif
1166         return ret;
1167 }
1168
1169 int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
1170 {
1171         struct btrfs_dev_item *dev_item;
1172
1173         dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
1174                                                      dev_item);
1175         return read_one_dev(root, buf, dev_item);
1176 }
1177
1178 int btrfs_read_sys_array(struct btrfs_root *root)
1179 {
1180         struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1181         struct extent_buffer *sb = root->fs_info->sb_buffer;
1182         struct btrfs_disk_key *disk_key;
1183         struct btrfs_chunk *chunk;
1184         struct btrfs_key key;
1185         u32 num_stripes;
1186         u32 array_size;
1187         u32 len = 0;
1188         u8 *ptr;
1189         unsigned long sb_ptr;
1190         u32 cur;
1191         int ret;
1192
1193         array_size = btrfs_super_sys_array_size(super_copy);
1194
1195         /*
1196          * we do this loop twice, once for the device items and
1197          * once for all of the chunks.  This way there are device
1198          * structs filled in for every chunk
1199          */
1200         ptr = super_copy->sys_chunk_array;
1201         sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
1202         cur = 0;
1203
1204         while (cur < array_size) {
1205                 disk_key = (struct btrfs_disk_key *)ptr;
1206                 btrfs_disk_key_to_cpu(&key, disk_key);
1207
1208                 len = sizeof(*disk_key);
1209                 ptr += len;
1210                 sb_ptr += len;
1211                 cur += len;
1212
1213                 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1214                         chunk = (struct btrfs_chunk *)sb_ptr;
1215                         ret = read_one_chunk(root, &key, sb, chunk);
1216                         BUG_ON(ret);
1217                         num_stripes = btrfs_chunk_num_stripes(sb, chunk);
1218                         len = btrfs_chunk_item_size(num_stripes);
1219                 } else {
1220                         BUG();
1221                 }
1222                 ptr += len;
1223                 sb_ptr += len;
1224                 cur += len;
1225         }
1226         return 0;
1227 }
1228
1229 int btrfs_read_chunk_tree(struct btrfs_root *root)
1230 {
1231         struct btrfs_path *path;
1232         struct extent_buffer *leaf;
1233         struct btrfs_key key;
1234         struct btrfs_key found_key;
1235         int ret;
1236         int slot;
1237
1238         root = root->fs_info->chunk_root;
1239
1240         path = btrfs_alloc_path();
1241         if (!path)
1242                 return -ENOMEM;
1243
1244         /* first we search for all of the device items, and then we
1245          * read in all of the chunk items.  This way we can create chunk
1246          * mappings that reference all of the devices that are afound
1247          */
1248         key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1249         key.offset = 0;
1250         key.type = 0;
1251 again:
1252         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1253         while(1) {
1254                 leaf = path->nodes[0];
1255                 slot = path->slots[0];
1256                 if (slot >= btrfs_header_nritems(leaf)) {
1257                         ret = btrfs_next_leaf(root, path);
1258                         if (ret == 0)
1259                                 continue;
1260                         if (ret < 0)
1261                                 goto error;
1262                         break;
1263                 }
1264                 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1265                 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
1266                         if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
1267                                 break;
1268                         if (found_key.type == BTRFS_DEV_ITEM_KEY) {
1269                                 struct btrfs_dev_item *dev_item;
1270                                 dev_item = btrfs_item_ptr(leaf, slot,
1271                                                   struct btrfs_dev_item);
1272                                 ret = read_one_dev(root, leaf, dev_item);
1273                                 BUG_ON(ret);
1274                         }
1275                 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
1276                         struct btrfs_chunk *chunk;
1277                         chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
1278                         ret = read_one_chunk(root, &found_key, leaf, chunk);
1279                 }
1280                 path->slots[0]++;
1281         }
1282         if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
1283                 key.objectid = 0;
1284                 btrfs_release_path(root, path);
1285                 goto again;
1286         }
1287
1288         btrfs_free_path(path);
1289         ret = 0;
1290 error:
1291         return ret;
1292 }
1293