]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - fs/btrfs/volumes.c
Btrfs: Make the resizer work based on shrinking and growing devices
[linux-2.6-omap-h63xx.git] / fs / btrfs / volumes.c
1 /*
2  * Copyright (C) 2007 Oracle.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License v2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public
14  * License along with this program; if not, write to the
15  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16  * Boston, MA 021110-1307, USA.
17  */
18 #include <linux/sched.h>
19 #include <linux/bio.h>
20 #include <linux/buffer_head.h>
21 #include <linux/blkdev.h>
22 #include <asm/div64.h>
23 #include "ctree.h"
24 #include "extent_map.h"
25 #include "disk-io.h"
26 #include "transaction.h"
27 #include "print-tree.h"
28 #include "volumes.h"
29
30 struct map_lookup {
31         u64 type;
32         int io_align;
33         int io_width;
34         int stripe_len;
35         int sector_size;
36         int num_stripes;
37         int sub_stripes;
38         struct btrfs_bio_stripe stripes[];
39 };
40
41 #define map_lookup_size(n) (sizeof(struct map_lookup) + \
42                             (sizeof(struct btrfs_bio_stripe) * (n)))
43
44 static DEFINE_MUTEX(uuid_mutex);
45 static LIST_HEAD(fs_uuids);
46
47 int btrfs_cleanup_fs_uuids(void)
48 {
49         struct btrfs_fs_devices *fs_devices;
50         struct list_head *uuid_cur;
51         struct list_head *devices_cur;
52         struct btrfs_device *dev;
53
54         list_for_each(uuid_cur, &fs_uuids) {
55                 fs_devices = list_entry(uuid_cur, struct btrfs_fs_devices,
56                                         list);
57                 while(!list_empty(&fs_devices->devices)) {
58                         devices_cur = fs_devices->devices.next;
59                         dev = list_entry(devices_cur, struct btrfs_device,
60                                          dev_list);
61                         if (dev->bdev) {
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                                           u8 *uuid)
73 {
74         struct btrfs_device *dev;
75         struct list_head *cur;
76
77         list_for_each(cur, head) {
78                 dev = list_entry(cur, struct btrfs_device, dev_list);
79                 if (dev->devid == devid &&
80                     (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
81                         return dev;
82                 }
83         }
84         return NULL;
85 }
86
87 static struct btrfs_fs_devices *find_fsid(u8 *fsid)
88 {
89         struct list_head *cur;
90         struct btrfs_fs_devices *fs_devices;
91
92         list_for_each(cur, &fs_uuids) {
93                 fs_devices = list_entry(cur, struct btrfs_fs_devices, list);
94                 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
95                         return fs_devices;
96         }
97         return NULL;
98 }
99
100 static int device_list_add(const char *path,
101                            struct btrfs_super_block *disk_super,
102                            u64 devid, struct btrfs_fs_devices **fs_devices_ret)
103 {
104         struct btrfs_device *device;
105         struct btrfs_fs_devices *fs_devices;
106         u64 found_transid = btrfs_super_generation(disk_super);
107
108         fs_devices = find_fsid(disk_super->fsid);
109         if (!fs_devices) {
110                 fs_devices = kmalloc(sizeof(*fs_devices), GFP_NOFS);
111                 if (!fs_devices)
112                         return -ENOMEM;
113                 INIT_LIST_HEAD(&fs_devices->devices);
114                 INIT_LIST_HEAD(&fs_devices->alloc_list);
115                 list_add(&fs_devices->list, &fs_uuids);
116                 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
117                 fs_devices->latest_devid = devid;
118                 fs_devices->latest_trans = found_transid;
119                 fs_devices->lowest_devid = (u64)-1;
120                 fs_devices->num_devices = 0;
121                 device = NULL;
122         } else {
123                 device = __find_device(&fs_devices->devices, devid,
124                                        disk_super->dev_item.uuid);
125         }
126         if (!device) {
127                 device = kzalloc(sizeof(*device), GFP_NOFS);
128                 if (!device) {
129                         /* we can safely leave the fs_devices entry around */
130                         return -ENOMEM;
131                 }
132                 device->devid = devid;
133                 memcpy(device->uuid, disk_super->dev_item.uuid,
134                        BTRFS_UUID_SIZE);
135                 device->barriers = 1;
136                 spin_lock_init(&device->io_lock);
137                 device->name = kstrdup(path, GFP_NOFS);
138                 if (!device->name) {
139                         kfree(device);
140                         return -ENOMEM;
141                 }
142                 list_add(&device->dev_list, &fs_devices->devices);
143                 list_add(&device->dev_alloc_list, &fs_devices->alloc_list);
144                 fs_devices->num_devices++;
145         }
146
147         if (found_transid > fs_devices->latest_trans) {
148                 fs_devices->latest_devid = devid;
149                 fs_devices->latest_trans = found_transid;
150         }
151         if (fs_devices->lowest_devid > devid) {
152                 fs_devices->lowest_devid = devid;
153         }
154         *fs_devices_ret = fs_devices;
155         return 0;
156 }
157
158 int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
159 {
160         struct list_head *head = &fs_devices->devices;
161         struct list_head *cur;
162         struct btrfs_device *device;
163
164         mutex_lock(&uuid_mutex);
165         list_for_each(cur, head) {
166                 device = list_entry(cur, struct btrfs_device, dev_list);
167                 if (device->bdev) {
168                         close_bdev_excl(device->bdev);
169                 }
170                 device->bdev = NULL;
171         }
172         mutex_unlock(&uuid_mutex);
173         return 0;
174 }
175
176 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
177                        int flags, void *holder)
178 {
179         struct block_device *bdev;
180         struct list_head *head = &fs_devices->devices;
181         struct list_head *cur;
182         struct btrfs_device *device;
183         int ret;
184
185         mutex_lock(&uuid_mutex);
186         list_for_each(cur, head) {
187                 device = list_entry(cur, struct btrfs_device, dev_list);
188                 bdev = open_bdev_excl(device->name, flags, holder);
189
190                 if (IS_ERR(bdev)) {
191                         printk("open %s failed\n", device->name);
192                         ret = PTR_ERR(bdev);
193                         goto fail;
194                 }
195                 if (device->devid == fs_devices->latest_devid)
196                         fs_devices->latest_bdev = bdev;
197                 if (device->devid == fs_devices->lowest_devid) {
198                         fs_devices->lowest_bdev = bdev;
199                 }
200                 device->bdev = bdev;
201         }
202         mutex_unlock(&uuid_mutex);
203         return 0;
204 fail:
205         mutex_unlock(&uuid_mutex);
206         btrfs_close_devices(fs_devices);
207         return ret;
208 }
209
210 int btrfs_scan_one_device(const char *path, int flags, void *holder,
211                           struct btrfs_fs_devices **fs_devices_ret)
212 {
213         struct btrfs_super_block *disk_super;
214         struct block_device *bdev;
215         struct buffer_head *bh;
216         int ret;
217         u64 devid;
218         u64 transid;
219
220         mutex_lock(&uuid_mutex);
221
222         bdev = open_bdev_excl(path, flags, holder);
223
224         if (IS_ERR(bdev)) {
225                 ret = PTR_ERR(bdev);
226                 goto error;
227         }
228
229         ret = set_blocksize(bdev, 4096);
230         if (ret)
231                 goto error_close;
232         bh = __bread(bdev, BTRFS_SUPER_INFO_OFFSET / 4096, 4096);
233         if (!bh) {
234                 ret = -EIO;
235                 goto error_close;
236         }
237         disk_super = (struct btrfs_super_block *)bh->b_data;
238         if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
239             sizeof(disk_super->magic))) {
240                 ret = -EINVAL;
241                 goto error_brelse;
242         }
243         devid = le64_to_cpu(disk_super->dev_item.devid);
244         transid = btrfs_super_generation(disk_super);
245         if (disk_super->label[0])
246                 printk("device label %s ", disk_super->label);
247         else {
248                 /* FIXME, make a readl uuid parser */
249                 printk("device fsid %llx-%llx ",
250                        *(unsigned long long *)disk_super->fsid,
251                        *(unsigned long long *)(disk_super->fsid + 8));
252         }
253         printk("devid %Lu transid %Lu %s\n", devid, transid, path);
254         ret = device_list_add(path, disk_super, devid, fs_devices_ret);
255
256 error_brelse:
257         brelse(bh);
258 error_close:
259         close_bdev_excl(bdev);
260 error:
261         mutex_unlock(&uuid_mutex);
262         return ret;
263 }
264
265 /*
266  * this uses a pretty simple search, the expectation is that it is
267  * called very infrequently and that a given device has a small number
268  * of extents
269  */
270 static int find_free_dev_extent(struct btrfs_trans_handle *trans,
271                                 struct btrfs_device *device,
272                                 struct btrfs_path *path,
273                                 u64 num_bytes, u64 *start)
274 {
275         struct btrfs_key key;
276         struct btrfs_root *root = device->dev_root;
277         struct btrfs_dev_extent *dev_extent = NULL;
278         u64 hole_size = 0;
279         u64 last_byte = 0;
280         u64 search_start = 0;
281         u64 search_end = device->total_bytes;
282         int ret;
283         int slot = 0;
284         int start_found;
285         struct extent_buffer *l;
286
287         start_found = 0;
288         path->reada = 2;
289
290         /* FIXME use last free of some kind */
291
292         /* we don't want to overwrite the superblock on the drive,
293          * so we make sure to start at an offset of at least 1MB
294          */
295         search_start = max((u64)1024 * 1024, search_start);
296
297         if (root->fs_info->alloc_start + num_bytes <= device->total_bytes)
298                 search_start = max(root->fs_info->alloc_start, search_start);
299
300         key.objectid = device->devid;
301         key.offset = search_start;
302         key.type = BTRFS_DEV_EXTENT_KEY;
303         ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
304         if (ret < 0)
305                 goto error;
306         ret = btrfs_previous_item(root, path, 0, key.type);
307         if (ret < 0)
308                 goto error;
309         l = path->nodes[0];
310         btrfs_item_key_to_cpu(l, &key, path->slots[0]);
311         while (1) {
312                 l = path->nodes[0];
313                 slot = path->slots[0];
314                 if (slot >= btrfs_header_nritems(l)) {
315                         ret = btrfs_next_leaf(root, path);
316                         if (ret == 0)
317                                 continue;
318                         if (ret < 0)
319                                 goto error;
320 no_more_items:
321                         if (!start_found) {
322                                 if (search_start >= search_end) {
323                                         ret = -ENOSPC;
324                                         goto error;
325                                 }
326                                 *start = search_start;
327                                 start_found = 1;
328                                 goto check_pending;
329                         }
330                         *start = last_byte > search_start ?
331                                 last_byte : search_start;
332                         if (search_end <= *start) {
333                                 ret = -ENOSPC;
334                                 goto error;
335                         }
336                         goto check_pending;
337                 }
338                 btrfs_item_key_to_cpu(l, &key, slot);
339
340                 if (key.objectid < device->devid)
341                         goto next;
342
343                 if (key.objectid > device->devid)
344                         goto no_more_items;
345
346                 if (key.offset >= search_start && key.offset > last_byte &&
347                     start_found) {
348                         if (last_byte < search_start)
349                                 last_byte = search_start;
350                         hole_size = key.offset - last_byte;
351                         if (key.offset > last_byte &&
352                             hole_size >= num_bytes) {
353                                 *start = last_byte;
354                                 goto check_pending;
355                         }
356                 }
357                 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) {
358                         goto next;
359                 }
360
361                 start_found = 1;
362                 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
363                 last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent);
364 next:
365                 path->slots[0]++;
366                 cond_resched();
367         }
368 check_pending:
369         /* we have to make sure we didn't find an extent that has already
370          * been allocated by the map tree or the original allocation
371          */
372         btrfs_release_path(root, path);
373         BUG_ON(*start < search_start);
374
375         if (*start + num_bytes > search_end) {
376                 ret = -ENOSPC;
377                 goto error;
378         }
379         /* check for pending inserts here */
380         return 0;
381
382 error:
383         btrfs_release_path(root, path);
384         return ret;
385 }
386
387 int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
388                           struct btrfs_device *device,
389                           u64 start)
390 {
391         int ret;
392         struct btrfs_path *path;
393         struct btrfs_root *root = device->dev_root;
394         struct btrfs_key key;
395
396         path = btrfs_alloc_path();
397         if (!path)
398                 return -ENOMEM;
399
400         key.objectid = device->devid;
401         key.offset = start;
402         key.type = BTRFS_DEV_EXTENT_KEY;
403
404         ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
405         BUG_ON(ret);
406
407         ret = btrfs_del_item(trans, root, path);
408         BUG_ON(ret);
409
410         btrfs_free_path(path);
411         return ret;
412 }
413
414 int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
415                            struct btrfs_device *device,
416                            u64 chunk_tree, u64 chunk_objectid,
417                            u64 chunk_offset,
418                            u64 num_bytes, u64 *start)
419 {
420         int ret;
421         struct btrfs_path *path;
422         struct btrfs_root *root = device->dev_root;
423         struct btrfs_dev_extent *extent;
424         struct extent_buffer *leaf;
425         struct btrfs_key key;
426
427         path = btrfs_alloc_path();
428         if (!path)
429                 return -ENOMEM;
430
431         ret = find_free_dev_extent(trans, device, path, num_bytes, start);
432         if (ret) {
433                 goto err;
434         }
435
436         key.objectid = device->devid;
437         key.offset = *start;
438         key.type = BTRFS_DEV_EXTENT_KEY;
439         ret = btrfs_insert_empty_item(trans, root, path, &key,
440                                       sizeof(*extent));
441         BUG_ON(ret);
442
443         leaf = path->nodes[0];
444         extent = btrfs_item_ptr(leaf, path->slots[0],
445                                 struct btrfs_dev_extent);
446         btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
447         btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
448         btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
449
450         write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
451                     (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
452                     BTRFS_UUID_SIZE);
453
454         btrfs_set_dev_extent_length(leaf, extent, num_bytes);
455         btrfs_mark_buffer_dirty(leaf);
456 err:
457         btrfs_free_path(path);
458         return ret;
459 }
460
461 static int find_next_chunk(struct btrfs_root *root, u64 objectid, u64 *offset)
462 {
463         struct btrfs_path *path;
464         int ret;
465         struct btrfs_key key;
466         struct btrfs_chunk *chunk;
467         struct btrfs_key found_key;
468
469         path = btrfs_alloc_path();
470         BUG_ON(!path);
471
472         key.objectid = objectid;
473         key.offset = (u64)-1;
474         key.type = BTRFS_CHUNK_ITEM_KEY;
475
476         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
477         if (ret < 0)
478                 goto error;
479
480         BUG_ON(ret == 0);
481
482         ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
483         if (ret) {
484                 *offset = 0;
485         } else {
486                 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
487                                       path->slots[0]);
488                 if (found_key.objectid != objectid)
489                         *offset = 0;
490                 else {
491                         chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
492                                                struct btrfs_chunk);
493                         *offset = found_key.offset +
494                                 btrfs_chunk_length(path->nodes[0], chunk);
495                 }
496         }
497         ret = 0;
498 error:
499         btrfs_free_path(path);
500         return ret;
501 }
502
503 static int find_next_devid(struct btrfs_root *root, struct btrfs_path *path,
504                            u64 *objectid)
505 {
506         int ret;
507         struct btrfs_key key;
508         struct btrfs_key found_key;
509
510         key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
511         key.type = BTRFS_DEV_ITEM_KEY;
512         key.offset = (u64)-1;
513
514         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
515         if (ret < 0)
516                 goto error;
517
518         BUG_ON(ret == 0);
519
520         ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
521                                   BTRFS_DEV_ITEM_KEY);
522         if (ret) {
523                 *objectid = 1;
524         } else {
525                 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
526                                       path->slots[0]);
527                 *objectid = found_key.offset + 1;
528         }
529         ret = 0;
530 error:
531         btrfs_release_path(root, path);
532         return ret;
533 }
534
535 /*
536  * the device information is stored in the chunk root
537  * the btrfs_device struct should be fully filled in
538  */
539 int btrfs_add_device(struct btrfs_trans_handle *trans,
540                      struct btrfs_root *root,
541                      struct btrfs_device *device)
542 {
543         int ret;
544         struct btrfs_path *path;
545         struct btrfs_dev_item *dev_item;
546         struct extent_buffer *leaf;
547         struct btrfs_key key;
548         unsigned long ptr;
549         u64 free_devid;
550
551         root = root->fs_info->chunk_root;
552
553         path = btrfs_alloc_path();
554         if (!path)
555                 return -ENOMEM;
556
557         ret = find_next_devid(root, path, &free_devid);
558         if (ret)
559                 goto out;
560
561         key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
562         key.type = BTRFS_DEV_ITEM_KEY;
563         key.offset = free_devid;
564
565         ret = btrfs_insert_empty_item(trans, root, path, &key,
566                                       sizeof(*dev_item));
567         if (ret)
568                 goto out;
569
570         leaf = path->nodes[0];
571         dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
572
573         device->devid = free_devid;
574         btrfs_set_device_id(leaf, dev_item, device->devid);
575         btrfs_set_device_type(leaf, dev_item, device->type);
576         btrfs_set_device_io_align(leaf, dev_item, device->io_align);
577         btrfs_set_device_io_width(leaf, dev_item, device->io_width);
578         btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
579         btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
580         btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
581         btrfs_set_device_group(leaf, dev_item, 0);
582         btrfs_set_device_seek_speed(leaf, dev_item, 0);
583         btrfs_set_device_bandwidth(leaf, dev_item, 0);
584
585         ptr = (unsigned long)btrfs_device_uuid(dev_item);
586         write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
587         btrfs_mark_buffer_dirty(leaf);
588         ret = 0;
589
590 out:
591         btrfs_free_path(path);
592         return ret;
593 }
594
595 int btrfs_update_device(struct btrfs_trans_handle *trans,
596                         struct btrfs_device *device)
597 {
598         int ret;
599         struct btrfs_path *path;
600         struct btrfs_root *root;
601         struct btrfs_dev_item *dev_item;
602         struct extent_buffer *leaf;
603         struct btrfs_key key;
604
605         root = device->dev_root->fs_info->chunk_root;
606
607         path = btrfs_alloc_path();
608         if (!path)
609                 return -ENOMEM;
610
611         key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
612         key.type = BTRFS_DEV_ITEM_KEY;
613         key.offset = device->devid;
614
615         ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
616         if (ret < 0)
617                 goto out;
618
619         if (ret > 0) {
620                 ret = -ENOENT;
621                 goto out;
622         }
623
624         leaf = path->nodes[0];
625         dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
626
627         btrfs_set_device_id(leaf, dev_item, device->devid);
628         btrfs_set_device_type(leaf, dev_item, device->type);
629         btrfs_set_device_io_align(leaf, dev_item, device->io_align);
630         btrfs_set_device_io_width(leaf, dev_item, device->io_width);
631         btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
632         btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
633         btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
634         btrfs_mark_buffer_dirty(leaf);
635
636 out:
637         btrfs_free_path(path);
638         return ret;
639 }
640
641 int btrfs_grow_device(struct btrfs_trans_handle *trans,
642                       struct btrfs_device *device, u64 new_size)
643 {
644         struct btrfs_super_block *super_copy =
645                 &device->dev_root->fs_info->super_copy;
646         u64 old_total = btrfs_super_total_bytes(super_copy);
647         u64 diff = new_size - device->total_bytes;
648
649         btrfs_set_super_total_bytes(super_copy, old_total + diff);
650         return btrfs_update_device(trans, device);
651 }
652
653 static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
654                             struct btrfs_root *root,
655                             u64 chunk_tree, u64 chunk_objectid,
656                             u64 chunk_offset)
657 {
658         int ret;
659         struct btrfs_path *path;
660         struct btrfs_key key;
661
662         root = root->fs_info->chunk_root;
663         path = btrfs_alloc_path();
664         if (!path)
665                 return -ENOMEM;
666
667         key.objectid = chunk_objectid;
668         key.offset = chunk_offset;
669         key.type = BTRFS_CHUNK_ITEM_KEY;
670
671         ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
672         BUG_ON(ret);
673
674         ret = btrfs_del_item(trans, root, path);
675         BUG_ON(ret);
676
677         btrfs_free_path(path);
678         return 0;
679 }
680
681 int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
682                         chunk_offset)
683 {
684         struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
685         struct btrfs_disk_key *disk_key;
686         struct btrfs_chunk *chunk;
687         u8 *ptr;
688         int ret = 0;
689         u32 num_stripes;
690         u32 array_size;
691         u32 len = 0;
692         u32 cur;
693         struct btrfs_key key;
694
695         array_size = btrfs_super_sys_array_size(super_copy);
696
697         ptr = super_copy->sys_chunk_array;
698         cur = 0;
699
700         while (cur < array_size) {
701                 disk_key = (struct btrfs_disk_key *)ptr;
702                 btrfs_disk_key_to_cpu(&key, disk_key);
703
704                 len = sizeof(*disk_key);
705
706                 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
707                         chunk = (struct btrfs_chunk *)(ptr + len);
708                         num_stripes = btrfs_stack_chunk_num_stripes(chunk);
709                         len += btrfs_chunk_item_size(num_stripes);
710                 } else {
711                         ret = -EIO;
712                         break;
713                 }
714                 if (key.objectid == chunk_objectid &&
715                     key.offset == chunk_offset) {
716                         memmove(ptr, ptr + len, array_size - (cur + len));
717                         array_size -= len;
718                         btrfs_set_super_sys_array_size(super_copy, array_size);
719                 } else {
720                         ptr += len;
721                         cur += len;
722                 }
723         }
724         return ret;
725 }
726
727
728 int btrfs_relocate_chunk(struct btrfs_root *root,
729                          u64 chunk_tree, u64 chunk_objectid,
730                          u64 chunk_offset)
731 {
732         struct extent_map_tree *em_tree;
733         struct btrfs_root *extent_root;
734         struct btrfs_trans_handle *trans;
735         struct extent_map *em;
736         struct map_lookup *map;
737         int ret;
738         int i;
739
740         root = root->fs_info->chunk_root;
741         extent_root = root->fs_info->extent_root;
742         em_tree = &root->fs_info->mapping_tree.map_tree;
743
744         /* step one, relocate all the extents inside this chunk */
745         ret = btrfs_shrink_extent_tree(extent_root, chunk_offset);
746         BUG_ON(ret);
747
748         trans = btrfs_start_transaction(root, 1);
749         BUG_ON(!trans);
750
751         /*
752          * step two, delete the device extents and the
753          * chunk tree entries
754          */
755         spin_lock(&em_tree->lock);
756         em = lookup_extent_mapping(em_tree, chunk_offset, 1);
757         spin_unlock(&em_tree->lock);
758
759         BUG_ON(em->start > chunk_offset || em->start + em->len < chunk_offset);
760         map = (struct map_lookup *)em->bdev;
761
762         for (i = 0; i < map->num_stripes; i++) {
763                 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
764                                             map->stripes[i].physical);
765                 BUG_ON(ret);
766         }
767         ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
768                                chunk_offset);
769
770         BUG_ON(ret);
771
772         if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
773                 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
774                 BUG_ON(ret);
775                 goto out;
776         }
777
778
779
780         spin_lock(&em_tree->lock);
781         remove_extent_mapping(em_tree, em);
782         kfree(map);
783         em->bdev = NULL;
784
785         /* once for the tree */
786         free_extent_map(em);
787         spin_unlock(&em_tree->lock);
788
789 out:
790         /* once for us */
791         free_extent_map(em);
792
793         btrfs_end_transaction(trans, root);
794         return 0;
795 }
796
797 /*
798  * shrinking a device means finding all of the device extents past
799  * the new size, and then following the back refs to the chunks.
800  * The chunk relocation code actually frees the device extent
801  */
802 int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
803 {
804         struct btrfs_trans_handle *trans;
805         struct btrfs_root *root = device->dev_root;
806         struct btrfs_dev_extent *dev_extent = NULL;
807         struct btrfs_path *path;
808         u64 length;
809         u64 chunk_tree;
810         u64 chunk_objectid;
811         u64 chunk_offset;
812         int ret;
813         int slot;
814         struct extent_buffer *l;
815         struct btrfs_key key;
816         struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
817         u64 old_total = btrfs_super_total_bytes(super_copy);
818         u64 diff = device->total_bytes - new_size;
819
820
821         path = btrfs_alloc_path();
822         if (!path)
823                 return -ENOMEM;
824
825         trans = btrfs_start_transaction(root, 1);
826         if (!trans) {
827                 ret = -ENOMEM;
828                 goto done;
829         }
830
831         path->reada = 2;
832
833         device->total_bytes = new_size;
834         ret = btrfs_update_device(trans, device);
835         if (ret) {
836                 btrfs_end_transaction(trans, root);
837                 goto done;
838         }
839         WARN_ON(diff > old_total);
840         btrfs_set_super_total_bytes(super_copy, old_total - diff);
841         btrfs_end_transaction(trans, root);
842
843         key.objectid = device->devid;
844         key.offset = (u64)-1;
845         key.type = BTRFS_DEV_EXTENT_KEY;
846
847         while (1) {
848                 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
849                 if (ret < 0)
850                         goto done;
851
852                 ret = btrfs_previous_item(root, path, 0, key.type);
853                 if (ret < 0)
854                         goto done;
855                 if (ret) {
856                         ret = 0;
857                         goto done;
858                 }
859
860                 l = path->nodes[0];
861                 slot = path->slots[0];
862                 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
863
864                 if (key.objectid != device->devid)
865                         goto done;
866
867                 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
868                 length = btrfs_dev_extent_length(l, dev_extent);
869
870                 if (key.offset + length <= new_size)
871                         goto done;
872
873                 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
874                 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
875                 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
876                 btrfs_release_path(root, path);
877
878                 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
879                                            chunk_offset);
880                 if (ret)
881                         goto done;
882         }
883
884 done:
885         btrfs_free_path(path);
886         return ret;
887 }
888
889 int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
890                            struct btrfs_root *root,
891                            struct btrfs_key *key,
892                            struct btrfs_chunk *chunk, int item_size)
893 {
894         struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
895         struct btrfs_disk_key disk_key;
896         u32 array_size;
897         u8 *ptr;
898
899         array_size = btrfs_super_sys_array_size(super_copy);
900         if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
901                 return -EFBIG;
902
903         ptr = super_copy->sys_chunk_array + array_size;
904         btrfs_cpu_key_to_disk(&disk_key, key);
905         memcpy(ptr, &disk_key, sizeof(disk_key));
906         ptr += sizeof(disk_key);
907         memcpy(ptr, chunk, item_size);
908         item_size += sizeof(disk_key);
909         btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
910         return 0;
911 }
912
913 static u64 div_factor(u64 num, int factor)
914 {
915         if (factor == 10)
916                 return num;
917         num *= factor;
918         do_div(num, 10);
919         return num;
920 }
921
922 static u64 chunk_bytes_by_type(u64 type, u64 calc_size, int num_stripes,
923                                int sub_stripes)
924 {
925         if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
926                 return calc_size;
927         else if (type & BTRFS_BLOCK_GROUP_RAID10)
928                 return calc_size * (num_stripes / sub_stripes);
929         else
930                 return calc_size * num_stripes;
931 }
932
933
934 int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
935                       struct btrfs_root *extent_root, u64 *start,
936                       u64 *num_bytes, u64 type)
937 {
938         u64 dev_offset;
939         struct btrfs_fs_info *info = extent_root->fs_info;
940         struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
941         struct btrfs_path *path;
942         struct btrfs_stripe *stripes;
943         struct btrfs_device *device = NULL;
944         struct btrfs_chunk *chunk;
945         struct list_head private_devs;
946         struct list_head *dev_list;
947         struct list_head *cur;
948         struct extent_map_tree *em_tree;
949         struct map_lookup *map;
950         struct extent_map *em;
951         int min_stripe_size = 1 * 1024 * 1024;
952         u64 physical;
953         u64 calc_size = 1024 * 1024 * 1024;
954         u64 max_chunk_size = calc_size;
955         u64 min_free;
956         u64 avail;
957         u64 max_avail = 0;
958         u64 percent_max;
959         int num_stripes = 1;
960         int min_stripes = 1;
961         int sub_stripes = 0;
962         int looped = 0;
963         int ret;
964         int index;
965         int stripe_len = 64 * 1024;
966         struct btrfs_key key;
967
968         dev_list = &extent_root->fs_info->fs_devices->alloc_list;
969         if (list_empty(dev_list))
970                 return -ENOSPC;
971
972         if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
973                 num_stripes = btrfs_super_num_devices(&info->super_copy);
974                 min_stripes = 2;
975         }
976         if (type & (BTRFS_BLOCK_GROUP_DUP)) {
977                 num_stripes = 2;
978                 min_stripes = 2;
979         }
980         if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
981                 num_stripes = min_t(u64, 2,
982                                   btrfs_super_num_devices(&info->super_copy));
983                 if (num_stripes < 2)
984                         return -ENOSPC;
985                 min_stripes = 2;
986         }
987         if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
988                 num_stripes = btrfs_super_num_devices(&info->super_copy);
989                 if (num_stripes < 4)
990                         return -ENOSPC;
991                 num_stripes &= ~(u32)1;
992                 sub_stripes = 2;
993                 min_stripes = 4;
994         }
995
996         if (type & BTRFS_BLOCK_GROUP_DATA) {
997                 max_chunk_size = 10 * calc_size;
998                 min_stripe_size = 64 * 1024 * 1024;
999         } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
1000                 max_chunk_size = 4 * calc_size;
1001                 min_stripe_size = 32 * 1024 * 1024;
1002         } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
1003                 calc_size = 8 * 1024 * 1024;
1004                 max_chunk_size = calc_size * 2;
1005                 min_stripe_size = 1 * 1024 * 1024;
1006         }
1007
1008         path = btrfs_alloc_path();
1009         if (!path)
1010                 return -ENOMEM;
1011
1012         /* we don't want a chunk larger than 10% of the FS */
1013         percent_max = div_factor(btrfs_super_total_bytes(&info->super_copy), 1);
1014         max_chunk_size = min(percent_max, max_chunk_size);
1015
1016 again:
1017         if (calc_size * num_stripes > max_chunk_size) {
1018                 calc_size = max_chunk_size;
1019                 do_div(calc_size, num_stripes);
1020                 do_div(calc_size, stripe_len);
1021                 calc_size *= stripe_len;
1022         }
1023         /* we don't want tiny stripes */
1024         calc_size = max_t(u64, min_stripe_size, calc_size);
1025
1026         do_div(calc_size, stripe_len);
1027         calc_size *= stripe_len;
1028
1029         INIT_LIST_HEAD(&private_devs);
1030         cur = dev_list->next;
1031         index = 0;
1032
1033         if (type & BTRFS_BLOCK_GROUP_DUP)
1034                 min_free = calc_size * 2;
1035         else
1036                 min_free = calc_size;
1037
1038         /* we add 1MB because we never use the first 1MB of the device */
1039         min_free += 1024 * 1024;
1040
1041         /* build a private list of devices we will allocate from */
1042         while(index < num_stripes) {
1043                 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
1044
1045                 avail = device->total_bytes - device->bytes_used;
1046                 cur = cur->next;
1047
1048                 if (avail >= min_free) {
1049                         u64 ignored_start = 0;
1050                         ret = find_free_dev_extent(trans, device, path,
1051                                                    min_free,
1052                                                    &ignored_start);
1053                         if (ret == 0) {
1054                                 list_move_tail(&device->dev_alloc_list,
1055                                                &private_devs);
1056                                 index++;
1057                                 if (type & BTRFS_BLOCK_GROUP_DUP)
1058                                         index++;
1059                         }
1060                 } else if (avail > max_avail)
1061                         max_avail = avail;
1062                 if (cur == dev_list)
1063                         break;
1064         }
1065         if (index < num_stripes) {
1066                 list_splice(&private_devs, dev_list);
1067                 if (index >= min_stripes) {
1068                         num_stripes = index;
1069                         if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
1070                                 num_stripes /= sub_stripes;
1071                                 num_stripes *= sub_stripes;
1072                         }
1073                         looped = 1;
1074                         goto again;
1075                 }
1076                 if (!looped && max_avail > 0) {
1077                         looped = 1;
1078                         calc_size = max_avail;
1079                         goto again;
1080                 }
1081                 btrfs_free_path(path);
1082                 return -ENOSPC;
1083         }
1084         key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1085         key.type = BTRFS_CHUNK_ITEM_KEY;
1086         ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
1087                               &key.offset);
1088         if (ret) {
1089                 btrfs_free_path(path);
1090                 return ret;
1091         }
1092
1093         chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS);
1094         if (!chunk) {
1095                 btrfs_free_path(path);
1096                 return -ENOMEM;
1097         }
1098
1099         map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
1100         if (!map) {
1101                 kfree(chunk);
1102                 btrfs_free_path(path);
1103                 return -ENOMEM;
1104         }
1105         btrfs_free_path(path);
1106         path = NULL;
1107
1108         stripes = &chunk->stripe;
1109         *num_bytes = chunk_bytes_by_type(type, calc_size,
1110                                          num_stripes, sub_stripes);
1111
1112         index = 0;
1113 printk("new chunk type %Lu start %Lu size %Lu\n", type, key.offset, *num_bytes);
1114         while(index < num_stripes) {
1115                 struct btrfs_stripe *stripe;
1116                 BUG_ON(list_empty(&private_devs));
1117                 cur = private_devs.next;
1118                 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
1119
1120                 /* loop over this device again if we're doing a dup group */
1121                 if (!(type & BTRFS_BLOCK_GROUP_DUP) ||
1122                     (index == num_stripes - 1))
1123                         list_move_tail(&device->dev_alloc_list, dev_list);
1124
1125                 ret = btrfs_alloc_dev_extent(trans, device,
1126                              info->chunk_root->root_key.objectid,
1127                              BTRFS_FIRST_CHUNK_TREE_OBJECTID, key.offset,
1128                              calc_size, &dev_offset);
1129                 BUG_ON(ret);
1130 printk("alloc chunk start %Lu size %Lu from dev %Lu type %Lu\n", key.offset, calc_size, device->devid, type);
1131                 device->bytes_used += calc_size;
1132                 ret = btrfs_update_device(trans, device);
1133                 BUG_ON(ret);
1134
1135                 map->stripes[index].dev = device;
1136                 map->stripes[index].physical = dev_offset;
1137                 stripe = stripes + index;
1138                 btrfs_set_stack_stripe_devid(stripe, device->devid);
1139                 btrfs_set_stack_stripe_offset(stripe, dev_offset);
1140                 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
1141                 physical = dev_offset;
1142                 index++;
1143         }
1144         BUG_ON(!list_empty(&private_devs));
1145
1146         /* key was set above */
1147         btrfs_set_stack_chunk_length(chunk, *num_bytes);
1148         btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
1149         btrfs_set_stack_chunk_stripe_len(chunk, stripe_len);
1150         btrfs_set_stack_chunk_type(chunk, type);
1151         btrfs_set_stack_chunk_num_stripes(chunk, num_stripes);
1152         btrfs_set_stack_chunk_io_align(chunk, stripe_len);
1153         btrfs_set_stack_chunk_io_width(chunk, stripe_len);
1154         btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
1155         btrfs_set_stack_chunk_sub_stripes(chunk, sub_stripes);
1156         map->sector_size = extent_root->sectorsize;
1157         map->stripe_len = stripe_len;
1158         map->io_align = stripe_len;
1159         map->io_width = stripe_len;
1160         map->type = type;
1161         map->num_stripes = num_stripes;
1162         map->sub_stripes = sub_stripes;
1163
1164         ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
1165                                 btrfs_chunk_item_size(num_stripes));
1166         BUG_ON(ret);
1167         *start = key.offset;;
1168
1169         em = alloc_extent_map(GFP_NOFS);
1170         if (!em)
1171                 return -ENOMEM;
1172         em->bdev = (struct block_device *)map;
1173         em->start = key.offset;
1174         em->len = *num_bytes;
1175         em->block_start = 0;
1176
1177         if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
1178                 ret = btrfs_add_system_chunk(trans, chunk_root, &key,
1179                                     chunk, btrfs_chunk_item_size(num_stripes));
1180                 BUG_ON(ret);
1181         }
1182         kfree(chunk);
1183
1184         em_tree = &extent_root->fs_info->mapping_tree.map_tree;
1185         spin_lock(&em_tree->lock);
1186         ret = add_extent_mapping(em_tree, em);
1187         spin_unlock(&em_tree->lock);
1188         BUG_ON(ret);
1189         free_extent_map(em);
1190         return ret;
1191 }
1192
1193 void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
1194 {
1195         extent_map_tree_init(&tree->map_tree, GFP_NOFS);
1196 }
1197
1198 void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
1199 {
1200         struct extent_map *em;
1201
1202         while(1) {
1203                 spin_lock(&tree->map_tree.lock);
1204                 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
1205                 if (em)
1206                         remove_extent_mapping(&tree->map_tree, em);
1207                 spin_unlock(&tree->map_tree.lock);
1208                 if (!em)
1209                         break;
1210                 kfree(em->bdev);
1211                 /* once for us */
1212                 free_extent_map(em);
1213                 /* once for the tree */
1214                 free_extent_map(em);
1215         }
1216 }
1217
1218 int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
1219 {
1220         struct extent_map *em;
1221         struct map_lookup *map;
1222         struct extent_map_tree *em_tree = &map_tree->map_tree;
1223         int ret;
1224
1225         spin_lock(&em_tree->lock);
1226         em = lookup_extent_mapping(em_tree, logical, len);
1227         spin_unlock(&em_tree->lock);
1228         BUG_ON(!em);
1229
1230         BUG_ON(em->start > logical || em->start + em->len < logical);
1231         map = (struct map_lookup *)em->bdev;
1232         if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
1233                 ret = map->num_stripes;
1234         else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
1235                 ret = map->sub_stripes;
1236         else
1237                 ret = 1;
1238         free_extent_map(em);
1239         return ret;
1240 }
1241
1242 static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
1243                              u64 logical, u64 *length,
1244                              struct btrfs_multi_bio **multi_ret,
1245                              int mirror_num, struct page *unplug_page)
1246 {
1247         struct extent_map *em;
1248         struct map_lookup *map;
1249         struct extent_map_tree *em_tree = &map_tree->map_tree;
1250         u64 offset;
1251         u64 stripe_offset;
1252         u64 stripe_nr;
1253         int stripes_allocated = 8;
1254         int stripes_required = 1;
1255         int stripe_index;
1256         int i;
1257         int num_stripes;
1258         struct btrfs_multi_bio *multi = NULL;
1259
1260         if (multi_ret && !(rw & (1 << BIO_RW))) {
1261                 stripes_allocated = 1;
1262         }
1263 again:
1264         if (multi_ret) {
1265                 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
1266                                 GFP_NOFS);
1267                 if (!multi)
1268                         return -ENOMEM;
1269         }
1270
1271         spin_lock(&em_tree->lock);
1272         em = lookup_extent_mapping(em_tree, logical, *length);
1273         spin_unlock(&em_tree->lock);
1274
1275         if (!em && unplug_page)
1276                 return 0;
1277
1278         if (!em) {
1279                 printk("unable to find logical %Lu\n", logical);
1280                 BUG();
1281         }
1282
1283         BUG_ON(em->start > logical || em->start + em->len < logical);
1284         map = (struct map_lookup *)em->bdev;
1285         offset = logical - em->start;
1286
1287         if (mirror_num > map->num_stripes)
1288                 mirror_num = 0;
1289
1290         /* if our multi bio struct is too small, back off and try again */
1291         if (rw & (1 << BIO_RW)) {
1292                 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
1293                                  BTRFS_BLOCK_GROUP_DUP)) {
1294                         stripes_required = map->num_stripes;
1295                 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1296                         stripes_required = map->sub_stripes;
1297                 }
1298         }
1299         if (multi_ret && rw == WRITE &&
1300             stripes_allocated < stripes_required) {
1301                 stripes_allocated = map->num_stripes;
1302                 free_extent_map(em);
1303                 kfree(multi);
1304                 goto again;
1305         }
1306         stripe_nr = offset;
1307         /*
1308          * stripe_nr counts the total number of stripes we have to stride
1309          * to get to this block
1310          */
1311         do_div(stripe_nr, map->stripe_len);
1312
1313         stripe_offset = stripe_nr * map->stripe_len;
1314         BUG_ON(offset < stripe_offset);
1315
1316         /* stripe_offset is the offset of this block in its stripe*/
1317         stripe_offset = offset - stripe_offset;
1318
1319         if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
1320                          BTRFS_BLOCK_GROUP_RAID10 |
1321                          BTRFS_BLOCK_GROUP_DUP)) {
1322                 /* we limit the length of each bio to what fits in a stripe */
1323                 *length = min_t(u64, em->len - offset,
1324                               map->stripe_len - stripe_offset);
1325         } else {
1326                 *length = em->len - offset;
1327         }
1328
1329         if (!multi_ret && !unplug_page)
1330                 goto out;
1331
1332         num_stripes = 1;
1333         stripe_index = 0;
1334         if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
1335                 if (unplug_page || (rw & (1 << BIO_RW)))
1336                         num_stripes = map->num_stripes;
1337                 else if (mirror_num) {
1338                         stripe_index = mirror_num - 1;
1339                 } else {
1340                         u64 orig_stripe_nr = stripe_nr;
1341                         stripe_index = do_div(orig_stripe_nr, num_stripes);
1342                 }
1343         } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
1344                 if (rw & (1 << BIO_RW))
1345                         num_stripes = map->num_stripes;
1346                 else if (mirror_num)
1347                         stripe_index = mirror_num - 1;
1348         } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1349                 int factor = map->num_stripes / map->sub_stripes;
1350
1351                 stripe_index = do_div(stripe_nr, factor);
1352                 stripe_index *= map->sub_stripes;
1353
1354                 if (unplug_page || (rw & (1 << BIO_RW)))
1355                         num_stripes = map->sub_stripes;
1356                 else if (mirror_num)
1357                         stripe_index += mirror_num - 1;
1358                 else {
1359                         u64 orig_stripe_nr = stripe_nr;
1360                         stripe_index += do_div(orig_stripe_nr,
1361                                                map->sub_stripes);
1362                 }
1363         } else {
1364                 /*
1365                  * after this do_div call, stripe_nr is the number of stripes
1366                  * on this device we have to walk to find the data, and
1367                  * stripe_index is the number of our device in the stripe array
1368                  */
1369                 stripe_index = do_div(stripe_nr, map->num_stripes);
1370         }
1371         BUG_ON(stripe_index >= map->num_stripes);
1372
1373         for (i = 0; i < num_stripes; i++) {
1374                 if (unplug_page) {
1375                         struct btrfs_device *device;
1376                         struct backing_dev_info *bdi;
1377
1378                         device = map->stripes[stripe_index].dev;
1379                         bdi = blk_get_backing_dev_info(device->bdev);
1380                         if (bdi->unplug_io_fn) {
1381                                 bdi->unplug_io_fn(bdi, unplug_page);
1382                         }
1383                 } else {
1384                         multi->stripes[i].physical =
1385                                 map->stripes[stripe_index].physical +
1386                                 stripe_offset + stripe_nr * map->stripe_len;
1387                         multi->stripes[i].dev = map->stripes[stripe_index].dev;
1388                 }
1389                 stripe_index++;
1390         }
1391         if (multi_ret) {
1392                 *multi_ret = multi;
1393                 multi->num_stripes = num_stripes;
1394         }
1395 out:
1396         free_extent_map(em);
1397         return 0;
1398 }
1399
1400 int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
1401                       u64 logical, u64 *length,
1402                       struct btrfs_multi_bio **multi_ret, int mirror_num)
1403 {
1404         return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
1405                                  mirror_num, NULL);
1406 }
1407
1408 int btrfs_unplug_page(struct btrfs_mapping_tree *map_tree,
1409                       u64 logical, struct page *page)
1410 {
1411         u64 length = PAGE_CACHE_SIZE;
1412         return __btrfs_map_block(map_tree, READ, logical, &length,
1413                                  NULL, 0, page);
1414 }
1415
1416
1417 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
1418 static void end_bio_multi_stripe(struct bio *bio, int err)
1419 #else
1420 static int end_bio_multi_stripe(struct bio *bio,
1421                                    unsigned int bytes_done, int err)
1422 #endif
1423 {
1424         struct btrfs_multi_bio *multi = bio->bi_private;
1425
1426 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1427         if (bio->bi_size)
1428                 return 1;
1429 #endif
1430         if (err)
1431                 multi->error = err;
1432
1433         if (atomic_dec_and_test(&multi->stripes_pending)) {
1434                 bio->bi_private = multi->private;
1435                 bio->bi_end_io = multi->end_io;
1436
1437                 if (!err && multi->error)
1438                         err = multi->error;
1439                 kfree(multi);
1440
1441 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1442                 bio_endio(bio, bio->bi_size, err);
1443 #else
1444                 bio_endio(bio, err);
1445 #endif
1446         } else {
1447                 bio_put(bio);
1448         }
1449 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1450         return 0;
1451 #endif
1452 }
1453
1454 int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
1455                   int mirror_num)
1456 {
1457         struct btrfs_mapping_tree *map_tree;
1458         struct btrfs_device *dev;
1459         struct bio *first_bio = bio;
1460         u64 logical = bio->bi_sector << 9;
1461         u64 length = 0;
1462         u64 map_length;
1463         struct btrfs_multi_bio *multi = NULL;
1464         int ret;
1465         int dev_nr = 0;
1466         int total_devs = 1;
1467
1468         length = bio->bi_size;
1469         map_tree = &root->fs_info->mapping_tree;
1470         map_length = length;
1471
1472         ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
1473                               mirror_num);
1474         BUG_ON(ret);
1475
1476         total_devs = multi->num_stripes;
1477         if (map_length < length) {
1478                 printk("mapping failed logical %Lu bio len %Lu "
1479                        "len %Lu\n", logical, length, map_length);
1480                 BUG();
1481         }
1482         multi->end_io = first_bio->bi_end_io;
1483         multi->private = first_bio->bi_private;
1484         atomic_set(&multi->stripes_pending, multi->num_stripes);
1485
1486         while(dev_nr < total_devs) {
1487                 if (total_devs > 1) {
1488                         if (dev_nr < total_devs - 1) {
1489                                 bio = bio_clone(first_bio, GFP_NOFS);
1490                                 BUG_ON(!bio);
1491                         } else {
1492                                 bio = first_bio;
1493                         }
1494                         bio->bi_private = multi;
1495                         bio->bi_end_io = end_bio_multi_stripe;
1496                 }
1497                 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
1498                 dev = multi->stripes[dev_nr].dev;
1499
1500                 bio->bi_bdev = dev->bdev;
1501                 spin_lock(&dev->io_lock);
1502                 dev->total_ios++;
1503                 spin_unlock(&dev->io_lock);
1504                 submit_bio(rw, bio);
1505                 dev_nr++;
1506         }
1507         if (total_devs == 1)
1508                 kfree(multi);
1509         return 0;
1510 }
1511
1512 struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
1513                                        u8 *uuid)
1514 {
1515         struct list_head *head = &root->fs_info->fs_devices->devices;
1516
1517         return __find_device(head, devid, uuid);
1518 }
1519
1520 static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
1521                           struct extent_buffer *leaf,
1522                           struct btrfs_chunk *chunk)
1523 {
1524         struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
1525         struct map_lookup *map;
1526         struct extent_map *em;
1527         u64 logical;
1528         u64 length;
1529         u64 devid;
1530         u8 uuid[BTRFS_UUID_SIZE];
1531         int num_stripes;
1532         int ret;
1533         int i;
1534
1535         logical = key->offset;
1536         length = btrfs_chunk_length(leaf, chunk);
1537         spin_lock(&map_tree->map_tree.lock);
1538         em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
1539         spin_unlock(&map_tree->map_tree.lock);
1540
1541         /* already mapped? */
1542         if (em && em->start <= logical && em->start + em->len > logical) {
1543                 free_extent_map(em);
1544                 return 0;
1545         } else if (em) {
1546                 free_extent_map(em);
1547         }
1548
1549         map = kzalloc(sizeof(*map), GFP_NOFS);
1550         if (!map)
1551                 return -ENOMEM;
1552
1553         em = alloc_extent_map(GFP_NOFS);
1554         if (!em)
1555                 return -ENOMEM;
1556         num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
1557         map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
1558         if (!map) {
1559                 free_extent_map(em);
1560                 return -ENOMEM;
1561         }
1562
1563         em->bdev = (struct block_device *)map;
1564         em->start = logical;
1565         em->len = length;
1566         em->block_start = 0;
1567
1568         map->num_stripes = num_stripes;
1569         map->io_width = btrfs_chunk_io_width(leaf, chunk);
1570         map->io_align = btrfs_chunk_io_align(leaf, chunk);
1571         map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
1572         map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
1573         map->type = btrfs_chunk_type(leaf, chunk);
1574         map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
1575         for (i = 0; i < num_stripes; i++) {
1576                 map->stripes[i].physical =
1577                         btrfs_stripe_offset_nr(leaf, chunk, i);
1578                 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
1579                 read_extent_buffer(leaf, uuid, (unsigned long)
1580                                    btrfs_stripe_dev_uuid_nr(chunk, i),
1581                                    BTRFS_UUID_SIZE);
1582                 map->stripes[i].dev = btrfs_find_device(root, devid, uuid);
1583                 if (!map->stripes[i].dev) {
1584                         kfree(map);
1585                         free_extent_map(em);
1586                         return -EIO;
1587                 }
1588         }
1589
1590         spin_lock(&map_tree->map_tree.lock);
1591         ret = add_extent_mapping(&map_tree->map_tree, em);
1592         spin_unlock(&map_tree->map_tree.lock);
1593         BUG_ON(ret);
1594         free_extent_map(em);
1595
1596         return 0;
1597 }
1598
1599 static int fill_device_from_item(struct extent_buffer *leaf,
1600                                  struct btrfs_dev_item *dev_item,
1601                                  struct btrfs_device *device)
1602 {
1603         unsigned long ptr;
1604
1605         device->devid = btrfs_device_id(leaf, dev_item);
1606         device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
1607         device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
1608         device->type = btrfs_device_type(leaf, dev_item);
1609         device->io_align = btrfs_device_io_align(leaf, dev_item);
1610         device->io_width = btrfs_device_io_width(leaf, dev_item);
1611         device->sector_size = btrfs_device_sector_size(leaf, dev_item);
1612
1613         ptr = (unsigned long)btrfs_device_uuid(dev_item);
1614         read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1615
1616         return 0;
1617 }
1618
1619 static int read_one_dev(struct btrfs_root *root,
1620                         struct extent_buffer *leaf,
1621                         struct btrfs_dev_item *dev_item)
1622 {
1623         struct btrfs_device *device;
1624         u64 devid;
1625         int ret;
1626         u8 dev_uuid[BTRFS_UUID_SIZE];
1627
1628         devid = btrfs_device_id(leaf, dev_item);
1629         read_extent_buffer(leaf, dev_uuid,
1630                            (unsigned long)btrfs_device_uuid(dev_item),
1631                            BTRFS_UUID_SIZE);
1632         device = btrfs_find_device(root, devid, dev_uuid);
1633         if (!device) {
1634                 printk("warning devid %Lu not found already\n", devid);
1635                 device = kzalloc(sizeof(*device), GFP_NOFS);
1636                 if (!device)
1637                         return -ENOMEM;
1638                 list_add(&device->dev_list,
1639                          &root->fs_info->fs_devices->devices);
1640                 list_add(&device->dev_alloc_list,
1641                          &root->fs_info->fs_devices->alloc_list);
1642                 device->barriers = 1;
1643                 spin_lock_init(&device->io_lock);
1644         }
1645
1646         fill_device_from_item(leaf, dev_item, device);
1647         device->dev_root = root->fs_info->dev_root;
1648         ret = 0;
1649 #if 0
1650         ret = btrfs_open_device(device);
1651         if (ret) {
1652                 kfree(device);
1653         }
1654 #endif
1655         return ret;
1656 }
1657
1658 int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
1659 {
1660         struct btrfs_dev_item *dev_item;
1661
1662         dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
1663                                                      dev_item);
1664         return read_one_dev(root, buf, dev_item);
1665 }
1666
1667 int btrfs_read_sys_array(struct btrfs_root *root)
1668 {
1669         struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1670         struct extent_buffer *sb = root->fs_info->sb_buffer;
1671         struct btrfs_disk_key *disk_key;
1672         struct btrfs_chunk *chunk;
1673         u8 *ptr;
1674         unsigned long sb_ptr;
1675         int ret = 0;
1676         u32 num_stripes;
1677         u32 array_size;
1678         u32 len = 0;
1679         u32 cur;
1680         struct btrfs_key key;
1681
1682         array_size = btrfs_super_sys_array_size(super_copy);
1683
1684         ptr = super_copy->sys_chunk_array;
1685         sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
1686         cur = 0;
1687
1688         while (cur < array_size) {
1689                 disk_key = (struct btrfs_disk_key *)ptr;
1690                 btrfs_disk_key_to_cpu(&key, disk_key);
1691
1692                 len = sizeof(*disk_key);
1693                 ptr += len;
1694                 sb_ptr += len;
1695                 cur += len;
1696
1697                 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1698                         chunk = (struct btrfs_chunk *)sb_ptr;
1699                         ret = read_one_chunk(root, &key, sb, chunk);
1700                         if (ret)
1701                                 break;
1702                         num_stripes = btrfs_chunk_num_stripes(sb, chunk);
1703                         len = btrfs_chunk_item_size(num_stripes);
1704                 } else {
1705                         ret = -EIO;
1706                         break;
1707                 }
1708                 ptr += len;
1709                 sb_ptr += len;
1710                 cur += len;
1711         }
1712         return ret;
1713 }
1714
1715 int btrfs_read_chunk_tree(struct btrfs_root *root)
1716 {
1717         struct btrfs_path *path;
1718         struct extent_buffer *leaf;
1719         struct btrfs_key key;
1720         struct btrfs_key found_key;
1721         int ret;
1722         int slot;
1723
1724         root = root->fs_info->chunk_root;
1725
1726         path = btrfs_alloc_path();
1727         if (!path)
1728                 return -ENOMEM;
1729
1730         /* first we search for all of the device items, and then we
1731          * read in all of the chunk items.  This way we can create chunk
1732          * mappings that reference all of the devices that are afound
1733          */
1734         key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1735         key.offset = 0;
1736         key.type = 0;
1737 again:
1738         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1739         while(1) {
1740                 leaf = path->nodes[0];
1741                 slot = path->slots[0];
1742                 if (slot >= btrfs_header_nritems(leaf)) {
1743                         ret = btrfs_next_leaf(root, path);
1744                         if (ret == 0)
1745                                 continue;
1746                         if (ret < 0)
1747                                 goto error;
1748                         break;
1749                 }
1750                 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1751                 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
1752                         if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
1753                                 break;
1754                         if (found_key.type == BTRFS_DEV_ITEM_KEY) {
1755                                 struct btrfs_dev_item *dev_item;
1756                                 dev_item = btrfs_item_ptr(leaf, slot,
1757                                                   struct btrfs_dev_item);
1758                                 ret = read_one_dev(root, leaf, dev_item);
1759                                 BUG_ON(ret);
1760                         }
1761                 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
1762                         struct btrfs_chunk *chunk;
1763                         chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
1764                         ret = read_one_chunk(root, &found_key, leaf, chunk);
1765                 }
1766                 path->slots[0]++;
1767         }
1768         if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
1769                 key.objectid = 0;
1770                 btrfs_release_path(root, path);
1771                 goto again;
1772         }
1773
1774         btrfs_free_path(path);
1775         ret = 0;
1776 error:
1777         return ret;
1778 }
1779