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