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