Drop quick-test from the list of programs built by default
[btrfs-progs-unstable.git] / volumes.c
blob0a9ee02cb73969c90a0af607c0e631818454fa92
1 /*
2 * Copyright (C) 2007 Oracle. All rights reserved.
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.
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.
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.
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"
33 struct stripe {
34 struct btrfs_device *dev;
35 u64 physical;
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[];
50 #define map_lookup_size(n) (sizeof(struct map_lookup) + \
51 (sizeof(struct btrfs_bio_stripe) * (n)))
53 static LIST_HEAD(fs_uuids);
55 static struct btrfs_device *__find_device(struct list_head *head, u64 devid,
56 u8 *uuid)
58 struct btrfs_device *dev;
59 struct list_head *cur;
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;
68 return NULL;
71 static struct btrfs_fs_devices *find_fsid(u8 *fsid)
73 struct list_head *cur;
74 struct btrfs_fs_devices *fs_devices;
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;
81 return NULL;
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)
88 struct btrfs_device *device;
89 struct btrfs_fs_devices *fs_devices;
90 u64 found_transid = btrfs_super_generation(disk_super);
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);
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;
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;
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);
132 if (found_transid > fs_devices->latest_trans) {
133 fs_devices->latest_devid = devid;
134 fs_devices->latest_trans = found_transid;
136 if (fs_devices->lowest_devid > devid) {
137 fs_devices->lowest_devid = devid;
139 *fs_devices_ret = fs_devices;
140 return 0;
143 int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
145 struct list_head *head = &fs_devices->devices;
146 struct list_head *cur;
147 struct btrfs_device *device;
149 list_for_each(cur, head) {
150 device = list_entry(cur, struct btrfs_device, dev_list);
151 close(device->fd);
152 device->fd = -1;
154 return 0;
157 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, int flags)
159 int fd;
160 struct list_head *head = &fs_devices->devices;
161 struct list_head *cur;
162 struct btrfs_device *device;
163 int ret;
165 list_for_each(cur, head) {
166 device = list_entry(cur, struct btrfs_device, dev_list);
168 fd = open(device->name, flags);
169 if (fd < 0) {
170 ret = -errno;
171 goto fail;
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;
180 return 0;
181 fail:
182 btrfs_close_devices(fs_devices);
183 return ret;
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)
190 struct btrfs_super_block *disk_super;
191 char *buf;
192 int ret;
193 u64 devid;
194 char uuidbuf[37];
196 buf = malloc(4096);
197 if (!buf) {
198 ret = -ENOMEM;
199 goto error;
201 ret = pread(fd, buf, 4096, super_offset);
202 if (ret != 4096) {
203 ret = -EIO;
204 goto error;
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;
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);
216 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
218 error_brelse:
219 free(buf);
220 error:
221 return ret;
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
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)
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;
246 start_found = 0;
247 path->reada = 2;
249 /* FIXME use last free of some kind */
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
254 search_start = max((u64)1024 * 1024, search_start);
255 key.objectid = device->devid;
256 key.offset = search_start;
257 key.type = BTRFS_DEV_EXTENT_KEY;
258 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
259 if (ret < 0)
260 goto error;
261 ret = btrfs_previous_item(root, path, 0, key.type);
262 if (ret < 0)
263 goto error;
264 l = path->nodes[0];
265 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
266 while (1) {
267 l = path->nodes[0];
268 slot = path->slots[0];
269 if (slot >= btrfs_header_nritems(l)) {
270 ret = btrfs_next_leaf(root, path);
271 if (ret == 0)
272 continue;
273 if (ret < 0)
274 goto error;
275 no_more_items:
276 if (!start_found) {
277 if (search_start >= search_end) {
278 ret = -ENOSPC;
279 goto error;
281 *start = search_start;
282 start_found = 1;
283 goto check_pending;
285 *start = last_byte > search_start ?
286 last_byte : search_start;
287 if (search_end <= *start) {
288 ret = -ENOSPC;
289 goto error;
291 goto check_pending;
293 btrfs_item_key_to_cpu(l, &key, slot);
295 if (key.objectid < device->devid)
296 goto next;
298 if (key.objectid > device->devid)
299 goto no_more_items;
301 if (key.offset >= search_start && key.offset > last_byte &&
302 start_found) {
303 if (last_byte < search_start)
304 last_byte = search_start;
305 hole_size = key.offset - last_byte;
306 if (key.offset > last_byte &&
307 hole_size >= num_bytes) {
308 *start = last_byte;
309 goto check_pending;
312 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) {
313 goto next;
316 start_found = 1;
317 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
318 last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent);
319 next:
320 path->slots[0]++;
321 cond_resched();
323 check_pending:
324 /* we have to make sure we didn't find an extent that has already
325 * been allocated by the map tree or the original allocation
327 btrfs_release_path(root, path);
328 BUG_ON(*start < search_start);
330 if (*start + num_bytes > search_end) {
331 ret = -ENOSPC;
332 goto error;
334 /* check for pending inserts here */
335 return 0;
337 error:
338 btrfs_release_path(root, path);
339 return ret;
342 int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
343 struct btrfs_device *device,
344 u64 chunk_tree, u64 chunk_objectid,
345 u64 chunk_offset,
346 u64 num_bytes, u64 *start)
348 int ret;
349 struct btrfs_path *path;
350 struct btrfs_root *root = device->dev_root;
351 struct btrfs_dev_extent *extent;
352 struct extent_buffer *leaf;
353 struct btrfs_key key;
355 path = btrfs_alloc_path();
356 if (!path)
357 return -ENOMEM;
359 ret = find_free_dev_extent(trans, device, path, num_bytes, start);
360 if (ret) {
361 goto err;
364 key.objectid = device->devid;
365 key.offset = *start;
366 key.type = BTRFS_DEV_EXTENT_KEY;
367 ret = btrfs_insert_empty_item(trans, root, path, &key,
368 sizeof(*extent));
369 BUG_ON(ret);
371 leaf = path->nodes[0];
372 extent = btrfs_item_ptr(leaf, path->slots[0],
373 struct btrfs_dev_extent);
374 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
375 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
376 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
378 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
379 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
380 BTRFS_UUID_SIZE);
382 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
383 btrfs_mark_buffer_dirty(leaf);
384 err:
385 btrfs_free_path(path);
386 return ret;
389 static int find_next_chunk(struct btrfs_root *root, u64 objectid, u64 *offset)
391 struct btrfs_path *path;
392 int ret;
393 struct btrfs_key key;
394 struct btrfs_chunk *chunk;
395 struct btrfs_key found_key;
397 path = btrfs_alloc_path();
398 BUG_ON(!path);
400 key.objectid = objectid;
401 key.offset = (u64)-1;
402 key.type = BTRFS_CHUNK_ITEM_KEY;
404 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
405 if (ret < 0)
406 goto error;
408 BUG_ON(ret == 0);
410 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
411 if (ret) {
412 *offset = 0;
413 } else {
414 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
415 path->slots[0]);
416 if (found_key.objectid != objectid)
417 *offset = 0;
418 else {
419 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
420 struct btrfs_chunk);
421 *offset = found_key.offset +
422 btrfs_chunk_length(path->nodes[0], chunk);
425 ret = 0;
426 error:
427 btrfs_free_path(path);
428 return ret;
431 static int find_next_devid(struct btrfs_root *root, struct btrfs_path *path,
432 u64 *objectid)
434 int ret;
435 struct btrfs_key key;
436 struct btrfs_key found_key;
438 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
439 key.type = BTRFS_DEV_ITEM_KEY;
440 key.offset = (u64)-1;
442 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
443 if (ret < 0)
444 goto error;
446 BUG_ON(ret == 0);
448 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
449 BTRFS_DEV_ITEM_KEY);
450 if (ret) {
451 *objectid = 1;
452 } else {
453 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
454 path->slots[0]);
455 *objectid = found_key.offset + 1;
457 ret = 0;
458 error:
459 btrfs_release_path(root, path);
460 return ret;
464 * the device information is stored in the chunk root
465 * the btrfs_device struct should be fully filled in
467 int btrfs_add_device(struct btrfs_trans_handle *trans,
468 struct btrfs_root *root,
469 struct btrfs_device *device)
471 int ret;
472 struct btrfs_path *path;
473 struct btrfs_dev_item *dev_item;
474 struct extent_buffer *leaf;
475 struct btrfs_key key;
476 unsigned long ptr;
477 u64 free_devid;
479 root = root->fs_info->chunk_root;
481 path = btrfs_alloc_path();
482 if (!path)
483 return -ENOMEM;
485 ret = find_next_devid(root, path, &free_devid);
486 if (ret)
487 goto out;
489 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
490 key.type = BTRFS_DEV_ITEM_KEY;
491 key.offset = free_devid;
493 ret = btrfs_insert_empty_item(trans, root, path, &key,
494 sizeof(*dev_item));
495 if (ret)
496 goto out;
498 leaf = path->nodes[0];
499 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
501 device->devid = free_devid;
502 btrfs_set_device_id(leaf, dev_item, device->devid);
503 btrfs_set_device_type(leaf, dev_item, device->type);
504 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
505 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
506 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
507 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
508 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
509 btrfs_set_device_group(leaf, dev_item, 0);
510 btrfs_set_device_seek_speed(leaf, dev_item, 0);
511 btrfs_set_device_bandwidth(leaf, dev_item, 0);
513 ptr = (unsigned long)btrfs_device_uuid(dev_item);
514 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
515 btrfs_mark_buffer_dirty(leaf);
516 ret = 0;
518 out:
519 btrfs_free_path(path);
520 return ret;
523 int btrfs_update_device(struct btrfs_trans_handle *trans,
524 struct btrfs_device *device)
526 int ret;
527 struct btrfs_path *path;
528 struct btrfs_root *root;
529 struct btrfs_dev_item *dev_item;
530 struct extent_buffer *leaf;
531 struct btrfs_key key;
533 root = device->dev_root->fs_info->chunk_root;
535 path = btrfs_alloc_path();
536 if (!path)
537 return -ENOMEM;
539 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
540 key.type = BTRFS_DEV_ITEM_KEY;
541 key.offset = device->devid;
543 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
544 if (ret < 0)
545 goto out;
547 if (ret > 0) {
548 ret = -ENOENT;
549 goto out;
552 leaf = path->nodes[0];
553 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
555 btrfs_set_device_id(leaf, dev_item, device->devid);
556 btrfs_set_device_type(leaf, dev_item, device->type);
557 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
558 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
559 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
560 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
561 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
562 btrfs_mark_buffer_dirty(leaf);
564 out:
565 btrfs_free_path(path);
566 return ret;
569 int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
570 struct btrfs_root *root,
571 struct btrfs_key *key,
572 struct btrfs_chunk *chunk, int item_size)
574 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
575 struct btrfs_disk_key disk_key;
576 u32 array_size;
577 u8 *ptr;
579 array_size = btrfs_super_sys_array_size(super_copy);
580 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
581 return -EFBIG;
583 ptr = super_copy->sys_chunk_array + array_size;
584 btrfs_cpu_key_to_disk(&disk_key, key);
585 memcpy(ptr, &disk_key, sizeof(disk_key));
586 ptr += sizeof(disk_key);
587 memcpy(ptr, chunk, item_size);
588 item_size += sizeof(disk_key);
589 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
590 return 0;
593 static u64 div_factor(u64 num, int factor)
595 if (factor == 10)
596 return num;
597 num *= factor;
598 return num / 10;
601 static u64 chunk_bytes_by_type(u64 type, u64 calc_size, int num_stripes,
602 int sub_stripes)
604 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
605 return calc_size;
606 else if (type & BTRFS_BLOCK_GROUP_RAID10)
607 return calc_size * (num_stripes / sub_stripes);
608 else
609 return calc_size * num_stripes;
613 int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
614 struct btrfs_root *extent_root, u64 *start,
615 u64 *num_bytes, u64 type)
617 u64 dev_offset;
618 struct btrfs_fs_info *info = extent_root->fs_info;
619 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
620 struct btrfs_stripe *stripes;
621 struct btrfs_device *device = NULL;
622 struct btrfs_chunk *chunk;
623 struct list_head private_devs;
624 struct list_head *dev_list = &extent_root->fs_info->fs_devices->devices;
625 struct list_head *cur;
626 struct map_lookup *map;
627 int min_stripe_size = 1 * 1024 * 1024;
628 u64 physical;
629 u64 calc_size = 8 * 1024 * 1024;
630 u64 min_free;
631 u64 max_chunk_size = 4 * calc_size;
632 u64 avail;
633 u64 max_avail = 0;
634 u64 percent_max;
635 int num_stripes = 1;
636 int min_stripes = 1;
637 int sub_stripes = 0;
638 int looped = 0;
639 int ret;
640 int index;
641 int stripe_len = 64 * 1024;
642 struct btrfs_key key;
644 if (list_empty(dev_list)) {
645 return -ENOSPC;
648 if (type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
649 BTRFS_BLOCK_GROUP_RAID10 |
650 BTRFS_BLOCK_GROUP_DUP)) {
651 if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
652 calc_size = 8 * 1024 * 1024;
653 max_chunk_size = calc_size * 2;
654 min_stripe_size = 1 * 1024 * 1024;
655 } else if (type & BTRFS_BLOCK_GROUP_DATA) {
656 calc_size = 1024 * 1024 * 1024;
657 max_chunk_size = 10 * calc_size;
658 min_stripe_size = 64 * 1024 * 1024;
659 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
660 calc_size = 1024 * 1024 * 1024;
661 max_chunk_size = 4 * calc_size;
662 min_stripe_size = 32 * 1024 * 1024;
665 if (type & BTRFS_BLOCK_GROUP_RAID1) {
666 num_stripes = min_t(u64, 2,
667 btrfs_super_num_devices(&info->super_copy));
668 if (num_stripes < 2)
669 return -ENOSPC;
670 min_stripes = 2;
672 if (type & BTRFS_BLOCK_GROUP_DUP) {
673 num_stripes = 2;
674 min_stripes = 2;
676 if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
677 num_stripes = btrfs_super_num_devices(&info->super_copy);
678 min_stripes = 2;
680 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
681 num_stripes = btrfs_super_num_devices(&info->super_copy);
682 if (num_stripes < 4)
683 return -ENOSPC;
684 num_stripes &= ~(u32)1;
685 sub_stripes = 2;
686 min_stripes = 4;
689 /* we don't want a chunk larger than 10% of the FS */
690 percent_max = div_factor(btrfs_super_total_bytes(&info->super_copy), 1);
691 max_chunk_size = min(percent_max, max_chunk_size);
693 again:
694 if (chunk_bytes_by_type(type, calc_size, num_stripes, sub_stripes) >
695 max_chunk_size) {
696 calc_size = max_chunk_size;
697 calc_size /= num_stripes;
698 calc_size /= stripe_len;
699 calc_size *= stripe_len;
701 /* we don't want tiny stripes */
702 calc_size = max_t(u64, calc_size, min_stripe_size);
704 calc_size /= stripe_len;
705 calc_size *= stripe_len;
706 INIT_LIST_HEAD(&private_devs);
707 cur = dev_list->next;
708 index = 0;
710 if (type & BTRFS_BLOCK_GROUP_DUP)
711 min_free = calc_size * 2;
712 else
713 min_free = calc_size;
715 /* build a private list of devices we will allocate from */
716 while(index < num_stripes) {
717 device = list_entry(cur, struct btrfs_device, dev_list);
718 avail = device->total_bytes - device->bytes_used;
719 cur = cur->next;
720 if (avail >= min_free) {
721 list_move_tail(&device->dev_list, &private_devs);
722 index++;
723 if (type & BTRFS_BLOCK_GROUP_DUP)
724 index++;
725 } else if (avail > max_avail)
726 max_avail = avail;
727 if (cur == dev_list)
728 break;
730 if (index < num_stripes) {
731 list_splice(&private_devs, dev_list);
732 if (index >= min_stripes) {
733 num_stripes = index;
734 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
735 num_stripes /= sub_stripes;
736 num_stripes *= sub_stripes;
738 looped = 1;
739 goto again;
741 if (!looped && max_avail > 0) {
742 looped = 1;
743 calc_size = max_avail;
744 goto again;
746 return -ENOSPC;
748 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
749 key.type = BTRFS_CHUNK_ITEM_KEY;
750 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
751 &key.offset);
752 if (ret)
753 return ret;
755 chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS);
756 if (!chunk)
757 return -ENOMEM;
759 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
760 if (!map) {
761 kfree(chunk);
762 return -ENOMEM;
765 stripes = &chunk->stripe;
766 *num_bytes = chunk_bytes_by_type(type, calc_size,
767 num_stripes, sub_stripes);
768 index = 0;
769 while(index < num_stripes) {
770 struct btrfs_stripe *stripe;
771 BUG_ON(list_empty(&private_devs));
772 cur = private_devs.next;
773 device = list_entry(cur, struct btrfs_device, dev_list);
775 /* loop over this device again if we're doing a dup group */
776 if (!(type & BTRFS_BLOCK_GROUP_DUP) ||
777 (index == num_stripes - 1))
778 list_move_tail(&device->dev_list, dev_list);
780 ret = btrfs_alloc_dev_extent(trans, device,
781 info->chunk_root->root_key.objectid,
782 BTRFS_FIRST_CHUNK_TREE_OBJECTID, key.offset,
783 calc_size, &dev_offset);
784 BUG_ON(ret);
786 device->bytes_used += calc_size;
787 ret = btrfs_update_device(trans, device);
788 BUG_ON(ret);
790 map->stripes[index].dev = device;
791 map->stripes[index].physical = dev_offset;
792 stripe = stripes + index;
793 btrfs_set_stack_stripe_devid(stripe, device->devid);
794 btrfs_set_stack_stripe_offset(stripe, dev_offset);
795 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
796 physical = dev_offset;
797 index++;
799 BUG_ON(!list_empty(&private_devs));
801 /* key was set above */
802 btrfs_set_stack_chunk_length(chunk, *num_bytes);
803 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
804 btrfs_set_stack_chunk_stripe_len(chunk, stripe_len);
805 btrfs_set_stack_chunk_type(chunk, type);
806 btrfs_set_stack_chunk_num_stripes(chunk, num_stripes);
807 btrfs_set_stack_chunk_io_align(chunk, stripe_len);
808 btrfs_set_stack_chunk_io_width(chunk, stripe_len);
809 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
810 btrfs_set_stack_chunk_sub_stripes(chunk, sub_stripes);
811 map->sector_size = extent_root->sectorsize;
812 map->stripe_len = stripe_len;
813 map->io_align = stripe_len;
814 map->io_width = stripe_len;
815 map->type = type;
816 map->num_stripes = num_stripes;
817 map->sub_stripes = sub_stripes;
819 ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
820 btrfs_chunk_item_size(num_stripes));
821 BUG_ON(ret);
822 *start = key.offset;;
824 map->ce.start = key.offset;
825 map->ce.size = *num_bytes;
827 ret = insert_existing_cache_extent(
828 &extent_root->fs_info->mapping_tree.cache_tree,
829 &map->ce);
830 BUG_ON(ret);
832 if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
833 ret = btrfs_add_system_chunk(trans, chunk_root, &key,
834 chunk, btrfs_chunk_item_size(num_stripes));
835 BUG_ON(ret);
838 kfree(chunk);
839 return ret;
842 void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
844 cache_tree_init(&tree->cache_tree);
847 int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
849 struct cache_extent *ce;
850 struct map_lookup *map;
851 int ret;
852 u64 offset;
854 ce = find_first_cache_extent(&map_tree->cache_tree, logical);
855 BUG_ON(!ce);
856 BUG_ON(ce->start > logical || ce->start + ce->size < logical);
857 map = container_of(ce, struct map_lookup, ce);
859 offset = logical - ce->start;
860 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
861 ret = map->num_stripes;
862 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
863 ret = map->sub_stripes;
864 else
865 ret = 1;
866 return ret;
869 int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
870 u64 logical, u64 *length,
871 struct btrfs_multi_bio **multi_ret, int mirror_num)
873 struct cache_extent *ce;
874 struct map_lookup *map;
875 u64 offset;
876 u64 stripe_offset;
877 u64 stripe_nr;
878 int stripes_allocated = 8;
879 int stripes_required = 1;
880 int stripe_index;
881 int i;
882 struct btrfs_multi_bio *multi = NULL;
884 if (multi_ret && rw == READ) {
885 stripes_allocated = 1;
887 again:
888 if (multi_ret) {
889 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
890 GFP_NOFS);
891 if (!multi)
892 return -ENOMEM;
895 ce = find_first_cache_extent(&map_tree->cache_tree, logical);
896 BUG_ON(!ce);
897 BUG_ON(ce->start > logical || ce->start + ce->size < logical);
898 map = container_of(ce, struct map_lookup, ce);
899 offset = logical - ce->start;
901 if (rw == WRITE) {
902 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
903 BTRFS_BLOCK_GROUP_DUP)) {
904 stripes_required = map->num_stripes;
905 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
906 stripes_required = map->sub_stripes;
909 /* if our multi bio struct is too small, back off and try again */
910 if (multi_ret && rw == WRITE &&
911 stripes_allocated < stripes_required) {
912 stripes_allocated = map->num_stripes;
913 kfree(multi);
914 goto again;
916 stripe_nr = offset;
918 * stripe_nr counts the total number of stripes we have to stride
919 * to get to this block
921 stripe_nr = stripe_nr / map->stripe_len;
923 stripe_offset = stripe_nr * map->stripe_len;
924 BUG_ON(offset < stripe_offset);
926 /* stripe_offset is the offset of this block in its stripe*/
927 stripe_offset = offset - stripe_offset;
929 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
930 BTRFS_BLOCK_GROUP_RAID10 |
931 BTRFS_BLOCK_GROUP_DUP)) {
932 /* we limit the length of each bio to what fits in a stripe */
933 *length = min_t(u64, ce->size - offset,
934 map->stripe_len - stripe_offset);
935 } else {
936 *length = ce->size - offset;
939 if (!multi_ret)
940 goto out;
942 multi->num_stripes = 1;
943 stripe_index = 0;
944 if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
945 if (rw == WRITE)
946 multi->num_stripes = map->num_stripes;
947 else if (mirror_num)
948 stripe_index = mirror_num - 1;
949 else
950 stripe_index = stripe_nr % map->num_stripes;
951 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
952 int factor = map->num_stripes / map->sub_stripes;
954 stripe_index = stripe_nr % factor;
955 stripe_index *= map->sub_stripes;
957 if (rw == WRITE)
958 multi->num_stripes = map->sub_stripes;
959 else if (mirror_num)
960 stripe_index += mirror_num - 1;
961 else
962 stripe_index = stripe_nr % map->sub_stripes;
964 stripe_nr = stripe_nr / factor;
965 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
966 if (rw == WRITE)
967 multi->num_stripes = map->num_stripes;
968 else if (mirror_num)
969 stripe_index = mirror_num - 1;
970 } else {
972 * after this do_div call, stripe_nr is the number of stripes
973 * on this device we have to walk to find the data, and
974 * stripe_index is the number of our device in the stripe array
976 stripe_index = stripe_nr % map->num_stripes;
977 stripe_nr = stripe_nr / map->num_stripes;
979 BUG_ON(stripe_index >= map->num_stripes);
981 BUG_ON(stripe_index != 0 && multi->num_stripes > 1);
982 for (i = 0; i < multi->num_stripes; i++) {
983 multi->stripes[i].physical =
984 map->stripes[stripe_index].physical + stripe_offset +
985 stripe_nr * map->stripe_len;
986 multi->stripes[i].dev = map->stripes[stripe_index].dev;
987 stripe_index++;
989 *multi_ret = multi;
990 out:
991 return 0;
994 struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
995 u8 *uuid)
997 struct list_head *head = &root->fs_info->fs_devices->devices;
999 return __find_device(head, devid, uuid);
1002 int btrfs_bootstrap_super_map(struct btrfs_mapping_tree *map_tree,
1003 struct btrfs_fs_devices *fs_devices)
1005 struct map_lookup *map;
1006 u64 logical = BTRFS_SUPER_INFO_OFFSET;
1007 u64 length = BTRFS_SUPER_INFO_SIZE;
1008 int num_stripes = 0;
1009 int sub_stripes = 0;
1010 int ret;
1011 int i;
1012 struct list_head *cur;
1014 list_for_each(cur, &fs_devices->devices) {
1015 num_stripes++;
1017 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
1018 if (!map)
1019 return -ENOMEM;
1021 map->ce.start = logical;
1022 map->ce.size = length;
1023 map->num_stripes = num_stripes;
1024 map->sub_stripes = sub_stripes;
1025 map->io_width = length;
1026 map->io_align = length;
1027 map->sector_size = length;
1028 map->stripe_len = length;
1029 map->type = BTRFS_BLOCK_GROUP_RAID1;
1031 i = 0;
1032 list_for_each(cur, &fs_devices->devices) {
1033 struct btrfs_device *device = list_entry(cur,
1034 struct btrfs_device,
1035 dev_list);
1036 map->stripes[i].physical = logical;
1037 map->stripes[i].dev = device;
1038 i++;
1040 ret = insert_existing_cache_extent(&map_tree->cache_tree, &map->ce);
1041 if (ret == -EEXIST) {
1042 struct cache_extent *old;
1043 struct map_lookup *old_map;
1044 old = find_cache_extent(&map_tree->cache_tree, logical, length);
1045 old_map = container_of(old, struct map_lookup, ce);
1046 remove_cache_extent(&map_tree->cache_tree, old);
1047 kfree(old_map);
1048 ret = insert_existing_cache_extent(&map_tree->cache_tree,
1049 &map->ce);
1051 BUG_ON(ret);
1052 return 0;
1055 static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
1056 struct extent_buffer *leaf,
1057 struct btrfs_chunk *chunk)
1059 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
1060 struct map_lookup *map;
1061 struct cache_extent *ce;
1062 u64 logical;
1063 u64 length;
1064 u64 devid;
1065 u64 super_offset_diff = 0;
1066 u8 uuid[BTRFS_UUID_SIZE];
1067 int num_stripes;
1068 int ret;
1069 int i;
1071 logical = key->offset;
1072 length = btrfs_chunk_length(leaf, chunk);
1074 if (logical < BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE) {
1075 super_offset_diff = BTRFS_SUPER_INFO_OFFSET +
1076 BTRFS_SUPER_INFO_SIZE - logical;
1077 logical = BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE;
1080 ce = find_first_cache_extent(&map_tree->cache_tree, logical);
1082 /* already mapped? */
1083 if (ce && ce->start <= logical && ce->start + ce->size > logical) {
1084 return 0;
1087 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
1088 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
1089 if (!map)
1090 return -ENOMEM;
1092 map->ce.start = logical;
1093 map->ce.size = length - super_offset_diff;
1094 map->num_stripes = num_stripes;
1095 map->io_width = btrfs_chunk_io_width(leaf, chunk);
1096 map->io_align = btrfs_chunk_io_align(leaf, chunk);
1097 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
1098 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
1099 map->type = btrfs_chunk_type(leaf, chunk);
1100 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
1102 for (i = 0; i < num_stripes; i++) {
1103 map->stripes[i].physical =
1104 btrfs_stripe_offset_nr(leaf, chunk, i) +
1105 super_offset_diff;
1106 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
1107 read_extent_buffer(leaf, uuid, (unsigned long)
1108 btrfs_stripe_dev_uuid_nr(chunk, i),
1109 BTRFS_UUID_SIZE);
1110 map->stripes[i].dev = btrfs_find_device(root, devid, uuid);
1111 if (!map->stripes[i].dev) {
1112 kfree(map);
1113 return -EIO;
1117 ret = insert_existing_cache_extent(&map_tree->cache_tree, &map->ce);
1118 BUG_ON(ret);
1120 return 0;
1123 static int fill_device_from_item(struct extent_buffer *leaf,
1124 struct btrfs_dev_item *dev_item,
1125 struct btrfs_device *device)
1127 unsigned long ptr;
1129 device->devid = btrfs_device_id(leaf, dev_item);
1130 device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
1131 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
1132 device->type = btrfs_device_type(leaf, dev_item);
1133 device->io_align = btrfs_device_io_align(leaf, dev_item);
1134 device->io_width = btrfs_device_io_width(leaf, dev_item);
1135 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
1137 ptr = (unsigned long)btrfs_device_uuid(dev_item);
1138 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1140 return 0;
1143 static int read_one_dev(struct btrfs_root *root,
1144 struct extent_buffer *leaf,
1145 struct btrfs_dev_item *dev_item)
1147 struct btrfs_device *device;
1148 u64 devid;
1149 int ret = 0;
1150 u8 dev_uuid[BTRFS_UUID_SIZE];
1152 devid = btrfs_device_id(leaf, dev_item);
1153 read_extent_buffer(leaf, dev_uuid,
1154 (unsigned long)btrfs_device_uuid(dev_item),
1155 BTRFS_UUID_SIZE);
1156 device = btrfs_find_device(root, devid, dev_uuid);
1157 if (!device) {
1158 printk("warning devid %llu not found already\n",
1159 (unsigned long long)devid);
1160 device = kmalloc(sizeof(*device), GFP_NOFS);
1161 if (!device)
1162 return -ENOMEM;
1163 device->total_ios = 0;
1164 list_add(&device->dev_list,
1165 &root->fs_info->fs_devices->devices);
1168 fill_device_from_item(leaf, dev_item, device);
1169 device->dev_root = root->fs_info->dev_root;
1170 return ret;
1173 int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
1175 struct btrfs_dev_item *dev_item;
1177 dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
1178 dev_item);
1179 return read_one_dev(root, buf, dev_item);
1182 int btrfs_read_sys_array(struct btrfs_root *root)
1184 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1185 struct extent_buffer *sb = root->fs_info->sb_buffer;
1186 struct btrfs_disk_key *disk_key;
1187 struct btrfs_chunk *chunk;
1188 struct btrfs_key key;
1189 u32 num_stripes;
1190 u32 array_size;
1191 u32 len = 0;
1192 u8 *ptr;
1193 unsigned long sb_ptr;
1194 u32 cur;
1195 int ret;
1197 array_size = btrfs_super_sys_array_size(super_copy);
1200 * we do this loop twice, once for the device items and
1201 * once for all of the chunks. This way there are device
1202 * structs filled in for every chunk
1204 ptr = super_copy->sys_chunk_array;
1205 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
1206 cur = 0;
1208 while (cur < array_size) {
1209 disk_key = (struct btrfs_disk_key *)ptr;
1210 btrfs_disk_key_to_cpu(&key, disk_key);
1212 len = sizeof(*disk_key);
1213 ptr += len;
1214 sb_ptr += len;
1215 cur += len;
1217 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1218 chunk = (struct btrfs_chunk *)sb_ptr;
1219 ret = read_one_chunk(root, &key, sb, chunk);
1220 BUG_ON(ret);
1221 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
1222 len = btrfs_chunk_item_size(num_stripes);
1223 } else {
1224 BUG();
1226 ptr += len;
1227 sb_ptr += len;
1228 cur += len;
1230 return 0;
1233 int btrfs_read_chunk_tree(struct btrfs_root *root)
1235 struct btrfs_path *path;
1236 struct extent_buffer *leaf;
1237 struct btrfs_key key;
1238 struct btrfs_key found_key;
1239 int ret;
1240 int slot;
1242 root = root->fs_info->chunk_root;
1244 path = btrfs_alloc_path();
1245 if (!path)
1246 return -ENOMEM;
1248 /* first we search for all of the device items, and then we
1249 * read in all of the chunk items. This way we can create chunk
1250 * mappings that reference all of the devices that are afound
1252 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1253 key.offset = 0;
1254 key.type = 0;
1255 again:
1256 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1257 while(1) {
1258 leaf = path->nodes[0];
1259 slot = path->slots[0];
1260 if (slot >= btrfs_header_nritems(leaf)) {
1261 ret = btrfs_next_leaf(root, path);
1262 if (ret == 0)
1263 continue;
1264 if (ret < 0)
1265 goto error;
1266 break;
1268 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1269 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
1270 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
1271 break;
1272 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
1273 struct btrfs_dev_item *dev_item;
1274 dev_item = btrfs_item_ptr(leaf, slot,
1275 struct btrfs_dev_item);
1276 ret = read_one_dev(root, leaf, dev_item);
1277 BUG_ON(ret);
1279 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
1280 struct btrfs_chunk *chunk;
1281 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
1282 ret = read_one_chunk(root, &found_key, leaf, chunk);
1284 path->slots[0]++;
1286 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
1287 key.objectid = 0;
1288 btrfs_release_path(root, path);
1289 goto again;
1292 btrfs_free_path(path);
1293 ret = 0;
1294 error:
1295 return ret;
1298 struct list_head *btrfs_scanned_uuids(void)
1300 return &fs_uuids;