Btrfs image tool
[btrfs-progs-unstable.git] / volumes.c
bloba5519930aa420793e580e23ffe052f15065fe4dc
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 = kzalloc(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);
130 device->fs_devices = fs_devices;
133 if (found_transid > fs_devices->latest_trans) {
134 fs_devices->latest_devid = devid;
135 fs_devices->latest_trans = found_transid;
137 if (fs_devices->lowest_devid > devid) {
138 fs_devices->lowest_devid = devid;
140 *fs_devices_ret = fs_devices;
141 return 0;
144 int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
146 struct btrfs_fs_devices *seed_devices;
147 struct list_head *cur;
148 struct btrfs_device *device;
149 again:
150 list_for_each(cur, &fs_devices->devices) {
151 device = list_entry(cur, struct btrfs_device, dev_list);
152 close(device->fd);
153 device->fd = -1;
154 device->writeable = 0;
157 seed_devices = fs_devices->seed;
158 fs_devices->seed = NULL;
159 if (seed_devices) {
160 fs_devices = seed_devices;
161 goto again;
164 return 0;
167 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, int flags)
169 int fd;
170 struct list_head *head = &fs_devices->devices;
171 struct list_head *cur;
172 struct btrfs_device *device;
173 int ret;
175 list_for_each(cur, head) {
176 device = list_entry(cur, struct btrfs_device, dev_list);
178 fd = open(device->name, flags);
179 if (fd < 0) {
180 ret = -errno;
181 goto fail;
184 if (device->devid == fs_devices->latest_devid)
185 fs_devices->latest_bdev = fd;
186 if (device->devid == fs_devices->lowest_devid)
187 fs_devices->lowest_bdev = fd;
188 device->fd = fd;
189 if (flags == O_RDWR)
190 device->writeable = 1;
192 return 0;
193 fail:
194 btrfs_close_devices(fs_devices);
195 return ret;
198 int btrfs_scan_one_device(int fd, const char *path,
199 struct btrfs_fs_devices **fs_devices_ret,
200 u64 *total_devs, u64 super_offset)
202 struct btrfs_super_block *disk_super;
203 char *buf;
204 int ret;
205 u64 devid;
206 char uuidbuf[37];
208 buf = malloc(4096);
209 if (!buf) {
210 ret = -ENOMEM;
211 goto error;
213 ret = pread(fd, buf, 4096, super_offset);
214 if (ret != 4096) {
215 ret = -EIO;
216 goto error;
218 disk_super = (struct btrfs_super_block *)buf;
219 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
220 sizeof(disk_super->magic))) {
221 ret = -ENOENT;
222 goto error_brelse;
224 devid = le64_to_cpu(disk_super->dev_item.devid);
225 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_METADUMP)
226 *total_devs = 1;
227 else
228 *total_devs = btrfs_super_num_devices(disk_super);
229 uuid_unparse(disk_super->fsid, uuidbuf);
231 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
233 error_brelse:
234 free(buf);
235 error:
236 return ret;
240 * this uses a pretty simple search, the expectation is that it is
241 * called very infrequently and that a given device has a small number
242 * of extents
244 static int find_free_dev_extent(struct btrfs_trans_handle *trans,
245 struct btrfs_device *device,
246 struct btrfs_path *path,
247 u64 num_bytes, u64 *start)
249 struct btrfs_key key;
250 struct btrfs_root *root = device->dev_root;
251 struct btrfs_dev_extent *dev_extent = NULL;
252 u64 hole_size = 0;
253 u64 last_byte = 0;
254 u64 search_start = 0;
255 u64 search_end = device->total_bytes;
256 int ret;
257 int slot = 0;
258 int start_found;
259 struct extent_buffer *l;
261 start_found = 0;
262 path->reada = 2;
264 /* FIXME use last free of some kind */
266 /* we don't want to overwrite the superblock on the drive,
267 * so we make sure to start at an offset of at least 1MB
269 search_start = max((u64)1024 * 1024, search_start);
271 if (root->fs_info->alloc_start + num_bytes <= device->total_bytes)
272 search_start = max(root->fs_info->alloc_start, search_start);
274 key.objectid = device->devid;
275 key.offset = search_start;
276 key.type = BTRFS_DEV_EXTENT_KEY;
277 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
278 if (ret < 0)
279 goto error;
280 ret = btrfs_previous_item(root, path, 0, key.type);
281 if (ret < 0)
282 goto error;
283 l = path->nodes[0];
284 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
285 while (1) {
286 l = path->nodes[0];
287 slot = path->slots[0];
288 if (slot >= btrfs_header_nritems(l)) {
289 ret = btrfs_next_leaf(root, path);
290 if (ret == 0)
291 continue;
292 if (ret < 0)
293 goto error;
294 no_more_items:
295 if (!start_found) {
296 if (search_start >= search_end) {
297 ret = -ENOSPC;
298 goto error;
300 *start = search_start;
301 start_found = 1;
302 goto check_pending;
304 *start = last_byte > search_start ?
305 last_byte : search_start;
306 if (search_end <= *start) {
307 ret = -ENOSPC;
308 goto error;
310 goto check_pending;
312 btrfs_item_key_to_cpu(l, &key, slot);
314 if (key.objectid < device->devid)
315 goto next;
317 if (key.objectid > device->devid)
318 goto no_more_items;
320 if (key.offset >= search_start && key.offset > last_byte &&
321 start_found) {
322 if (last_byte < search_start)
323 last_byte = search_start;
324 hole_size = key.offset - last_byte;
325 if (key.offset > last_byte &&
326 hole_size >= num_bytes) {
327 *start = last_byte;
328 goto check_pending;
331 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) {
332 goto next;
335 start_found = 1;
336 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
337 last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent);
338 next:
339 path->slots[0]++;
340 cond_resched();
342 check_pending:
343 /* we have to make sure we didn't find an extent that has already
344 * been allocated by the map tree or the original allocation
346 btrfs_release_path(root, path);
347 BUG_ON(*start < search_start);
349 if (*start + num_bytes > search_end) {
350 ret = -ENOSPC;
351 goto error;
353 /* check for pending inserts here */
354 return 0;
356 error:
357 btrfs_release_path(root, path);
358 return ret;
361 int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
362 struct btrfs_device *device,
363 u64 chunk_tree, u64 chunk_objectid,
364 u64 chunk_offset,
365 u64 num_bytes, u64 *start)
367 int ret;
368 struct btrfs_path *path;
369 struct btrfs_root *root = device->dev_root;
370 struct btrfs_dev_extent *extent;
371 struct extent_buffer *leaf;
372 struct btrfs_key key;
374 path = btrfs_alloc_path();
375 if (!path)
376 return -ENOMEM;
378 ret = find_free_dev_extent(trans, device, path, num_bytes, start);
379 if (ret) {
380 goto err;
383 key.objectid = device->devid;
384 key.offset = *start;
385 key.type = BTRFS_DEV_EXTENT_KEY;
386 ret = btrfs_insert_empty_item(trans, root, path, &key,
387 sizeof(*extent));
388 BUG_ON(ret);
390 leaf = path->nodes[0];
391 extent = btrfs_item_ptr(leaf, path->slots[0],
392 struct btrfs_dev_extent);
393 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
394 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
395 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
397 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
398 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
399 BTRFS_UUID_SIZE);
401 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
402 btrfs_mark_buffer_dirty(leaf);
403 err:
404 btrfs_free_path(path);
405 return ret;
408 static int find_next_chunk(struct btrfs_root *root, u64 objectid, u64 *offset)
410 struct btrfs_path *path;
411 int ret;
412 struct btrfs_key key;
413 struct btrfs_chunk *chunk;
414 struct btrfs_key found_key;
416 path = btrfs_alloc_path();
417 BUG_ON(!path);
419 key.objectid = objectid;
420 key.offset = (u64)-1;
421 key.type = BTRFS_CHUNK_ITEM_KEY;
423 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
424 if (ret < 0)
425 goto error;
427 BUG_ON(ret == 0);
429 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
430 if (ret) {
431 *offset = 0;
432 } else {
433 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
434 path->slots[0]);
435 if (found_key.objectid != objectid)
436 *offset = 0;
437 else {
438 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
439 struct btrfs_chunk);
440 *offset = found_key.offset +
441 btrfs_chunk_length(path->nodes[0], chunk);
444 ret = 0;
445 error:
446 btrfs_free_path(path);
447 return ret;
450 static int find_next_devid(struct btrfs_root *root, struct btrfs_path *path,
451 u64 *objectid)
453 int ret;
454 struct btrfs_key key;
455 struct btrfs_key found_key;
457 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
458 key.type = BTRFS_DEV_ITEM_KEY;
459 key.offset = (u64)-1;
461 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
462 if (ret < 0)
463 goto error;
465 BUG_ON(ret == 0);
467 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
468 BTRFS_DEV_ITEM_KEY);
469 if (ret) {
470 *objectid = 1;
471 } else {
472 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
473 path->slots[0]);
474 *objectid = found_key.offset + 1;
476 ret = 0;
477 error:
478 btrfs_release_path(root, path);
479 return ret;
483 * the device information is stored in the chunk root
484 * the btrfs_device struct should be fully filled in
486 int btrfs_add_device(struct btrfs_trans_handle *trans,
487 struct btrfs_root *root,
488 struct btrfs_device *device)
490 int ret;
491 struct btrfs_path *path;
492 struct btrfs_dev_item *dev_item;
493 struct extent_buffer *leaf;
494 struct btrfs_key key;
495 unsigned long ptr;
496 u64 free_devid = 0;
498 root = root->fs_info->chunk_root;
500 path = btrfs_alloc_path();
501 if (!path)
502 return -ENOMEM;
504 ret = find_next_devid(root, path, &free_devid);
505 if (ret)
506 goto out;
508 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
509 key.type = BTRFS_DEV_ITEM_KEY;
510 key.offset = free_devid;
512 ret = btrfs_insert_empty_item(trans, root, path, &key,
513 sizeof(*dev_item));
514 if (ret)
515 goto out;
517 leaf = path->nodes[0];
518 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
520 device->devid = free_devid;
521 btrfs_set_device_id(leaf, dev_item, device->devid);
522 btrfs_set_device_generation(leaf, dev_item, 0);
523 btrfs_set_device_type(leaf, dev_item, device->type);
524 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
525 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
526 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
527 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
528 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
529 btrfs_set_device_group(leaf, dev_item, 0);
530 btrfs_set_device_seek_speed(leaf, dev_item, 0);
531 btrfs_set_device_bandwidth(leaf, dev_item, 0);
533 ptr = (unsigned long)btrfs_device_uuid(dev_item);
534 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
535 ptr = (unsigned long)btrfs_device_fsid(dev_item);
536 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
537 btrfs_mark_buffer_dirty(leaf);
538 ret = 0;
540 out:
541 btrfs_free_path(path);
542 return ret;
545 int btrfs_update_device(struct btrfs_trans_handle *trans,
546 struct btrfs_device *device)
548 int ret;
549 struct btrfs_path *path;
550 struct btrfs_root *root;
551 struct btrfs_dev_item *dev_item;
552 struct extent_buffer *leaf;
553 struct btrfs_key key;
555 root = device->dev_root->fs_info->chunk_root;
557 path = btrfs_alloc_path();
558 if (!path)
559 return -ENOMEM;
561 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
562 key.type = BTRFS_DEV_ITEM_KEY;
563 key.offset = device->devid;
565 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
566 if (ret < 0)
567 goto out;
569 if (ret > 0) {
570 ret = -ENOENT;
571 goto out;
574 leaf = path->nodes[0];
575 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
577 btrfs_set_device_id(leaf, dev_item, device->devid);
578 btrfs_set_device_type(leaf, dev_item, device->type);
579 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
580 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
581 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
582 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
583 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
584 btrfs_mark_buffer_dirty(leaf);
586 out:
587 btrfs_free_path(path);
588 return ret;
591 int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
592 struct btrfs_root *root,
593 struct btrfs_key *key,
594 struct btrfs_chunk *chunk, int item_size)
596 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
597 struct btrfs_disk_key disk_key;
598 u32 array_size;
599 u8 *ptr;
601 array_size = btrfs_super_sys_array_size(super_copy);
602 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
603 return -EFBIG;
605 ptr = super_copy->sys_chunk_array + array_size;
606 btrfs_cpu_key_to_disk(&disk_key, key);
607 memcpy(ptr, &disk_key, sizeof(disk_key));
608 ptr += sizeof(disk_key);
609 memcpy(ptr, chunk, item_size);
610 item_size += sizeof(disk_key);
611 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
612 return 0;
615 static u64 div_factor(u64 num, int factor)
617 if (factor == 10)
618 return num;
619 num *= factor;
620 return num / 10;
623 static u64 chunk_bytes_by_type(u64 type, u64 calc_size, int num_stripes,
624 int sub_stripes)
626 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
627 return calc_size;
628 else if (type & BTRFS_BLOCK_GROUP_RAID10)
629 return calc_size * (num_stripes / sub_stripes);
630 else
631 return calc_size * num_stripes;
635 int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
636 struct btrfs_root *extent_root, u64 *start,
637 u64 *num_bytes, u64 type)
639 u64 dev_offset;
640 struct btrfs_fs_info *info = extent_root->fs_info;
641 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
642 struct btrfs_stripe *stripes;
643 struct btrfs_device *device = NULL;
644 struct btrfs_chunk *chunk;
645 struct list_head private_devs;
646 struct list_head *dev_list = &extent_root->fs_info->fs_devices->devices;
647 struct list_head *cur;
648 struct map_lookup *map;
649 int min_stripe_size = 1 * 1024 * 1024;
650 u64 physical;
651 u64 calc_size = 8 * 1024 * 1024;
652 u64 min_free;
653 u64 max_chunk_size = 4 * calc_size;
654 u64 avail;
655 u64 max_avail = 0;
656 u64 percent_max;
657 int num_stripes = 1;
658 int min_stripes = 1;
659 int sub_stripes = 0;
660 int looped = 0;
661 int ret;
662 int index;
663 int stripe_len = 64 * 1024;
664 struct btrfs_key key;
666 if (list_empty(dev_list)) {
667 return -ENOSPC;
670 if (type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
671 BTRFS_BLOCK_GROUP_RAID10 |
672 BTRFS_BLOCK_GROUP_DUP)) {
673 if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
674 calc_size = 8 * 1024 * 1024;
675 max_chunk_size = calc_size * 2;
676 min_stripe_size = 1 * 1024 * 1024;
677 } else if (type & BTRFS_BLOCK_GROUP_DATA) {
678 calc_size = 1024 * 1024 * 1024;
679 max_chunk_size = 10 * calc_size;
680 min_stripe_size = 64 * 1024 * 1024;
681 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
682 calc_size = 1024 * 1024 * 1024;
683 max_chunk_size = 4 * calc_size;
684 min_stripe_size = 32 * 1024 * 1024;
687 if (type & BTRFS_BLOCK_GROUP_RAID1) {
688 num_stripes = min_t(u64, 2,
689 btrfs_super_num_devices(&info->super_copy));
690 if (num_stripes < 2)
691 return -ENOSPC;
692 min_stripes = 2;
694 if (type & BTRFS_BLOCK_GROUP_DUP) {
695 num_stripes = 2;
696 min_stripes = 2;
698 if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
699 num_stripes = btrfs_super_num_devices(&info->super_copy);
700 min_stripes = 2;
702 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
703 num_stripes = btrfs_super_num_devices(&info->super_copy);
704 if (num_stripes < 4)
705 return -ENOSPC;
706 num_stripes &= ~(u32)1;
707 sub_stripes = 2;
708 min_stripes = 4;
711 /* we don't want a chunk larger than 10% of the FS */
712 percent_max = div_factor(btrfs_super_total_bytes(&info->super_copy), 1);
713 max_chunk_size = min(percent_max, max_chunk_size);
715 again:
716 if (chunk_bytes_by_type(type, calc_size, num_stripes, sub_stripes) >
717 max_chunk_size) {
718 calc_size = max_chunk_size;
719 calc_size /= num_stripes;
720 calc_size /= stripe_len;
721 calc_size *= stripe_len;
723 /* we don't want tiny stripes */
724 calc_size = max_t(u64, calc_size, min_stripe_size);
726 calc_size /= stripe_len;
727 calc_size *= stripe_len;
728 INIT_LIST_HEAD(&private_devs);
729 cur = dev_list->next;
730 index = 0;
732 if (type & BTRFS_BLOCK_GROUP_DUP)
733 min_free = calc_size * 2;
734 else
735 min_free = calc_size;
737 /* build a private list of devices we will allocate from */
738 while(index < num_stripes) {
739 device = list_entry(cur, struct btrfs_device, dev_list);
740 avail = device->total_bytes - device->bytes_used;
741 cur = cur->next;
742 if (avail >= min_free) {
743 list_move_tail(&device->dev_list, &private_devs);
744 index++;
745 if (type & BTRFS_BLOCK_GROUP_DUP)
746 index++;
747 } else if (avail > max_avail)
748 max_avail = avail;
749 if (cur == dev_list)
750 break;
752 if (index < num_stripes) {
753 list_splice(&private_devs, dev_list);
754 if (index >= min_stripes) {
755 num_stripes = index;
756 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
757 num_stripes /= sub_stripes;
758 num_stripes *= sub_stripes;
760 looped = 1;
761 goto again;
763 if (!looped && max_avail > 0) {
764 looped = 1;
765 calc_size = max_avail;
766 goto again;
768 return -ENOSPC;
770 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
771 key.type = BTRFS_CHUNK_ITEM_KEY;
772 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
773 &key.offset);
774 if (ret)
775 return ret;
777 chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS);
778 if (!chunk)
779 return -ENOMEM;
781 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
782 if (!map) {
783 kfree(chunk);
784 return -ENOMEM;
787 stripes = &chunk->stripe;
788 *num_bytes = chunk_bytes_by_type(type, calc_size,
789 num_stripes, sub_stripes);
790 index = 0;
791 while(index < num_stripes) {
792 struct btrfs_stripe *stripe;
793 BUG_ON(list_empty(&private_devs));
794 cur = private_devs.next;
795 device = list_entry(cur, struct btrfs_device, dev_list);
797 /* loop over this device again if we're doing a dup group */
798 if (!(type & BTRFS_BLOCK_GROUP_DUP) ||
799 (index == num_stripes - 1))
800 list_move_tail(&device->dev_list, dev_list);
802 ret = btrfs_alloc_dev_extent(trans, device,
803 info->chunk_root->root_key.objectid,
804 BTRFS_FIRST_CHUNK_TREE_OBJECTID, key.offset,
805 calc_size, &dev_offset);
806 BUG_ON(ret);
808 device->bytes_used += calc_size;
809 ret = btrfs_update_device(trans, device);
810 BUG_ON(ret);
812 map->stripes[index].dev = device;
813 map->stripes[index].physical = dev_offset;
814 stripe = stripes + index;
815 btrfs_set_stack_stripe_devid(stripe, device->devid);
816 btrfs_set_stack_stripe_offset(stripe, dev_offset);
817 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
818 physical = dev_offset;
819 index++;
821 BUG_ON(!list_empty(&private_devs));
823 /* key was set above */
824 btrfs_set_stack_chunk_length(chunk, *num_bytes);
825 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
826 btrfs_set_stack_chunk_stripe_len(chunk, stripe_len);
827 btrfs_set_stack_chunk_type(chunk, type);
828 btrfs_set_stack_chunk_num_stripes(chunk, num_stripes);
829 btrfs_set_stack_chunk_io_align(chunk, stripe_len);
830 btrfs_set_stack_chunk_io_width(chunk, stripe_len);
831 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
832 btrfs_set_stack_chunk_sub_stripes(chunk, sub_stripes);
833 map->sector_size = extent_root->sectorsize;
834 map->stripe_len = stripe_len;
835 map->io_align = stripe_len;
836 map->io_width = stripe_len;
837 map->type = type;
838 map->num_stripes = num_stripes;
839 map->sub_stripes = sub_stripes;
841 ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
842 btrfs_chunk_item_size(num_stripes));
843 BUG_ON(ret);
844 *start = key.offset;;
846 map->ce.start = key.offset;
847 map->ce.size = *num_bytes;
849 ret = insert_existing_cache_extent(
850 &extent_root->fs_info->mapping_tree.cache_tree,
851 &map->ce);
852 BUG_ON(ret);
854 if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
855 ret = btrfs_add_system_chunk(trans, chunk_root, &key,
856 chunk, btrfs_chunk_item_size(num_stripes));
857 BUG_ON(ret);
860 kfree(chunk);
861 return ret;
864 void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
866 cache_tree_init(&tree->cache_tree);
869 int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
871 struct cache_extent *ce;
872 struct map_lookup *map;
873 int ret;
874 u64 offset;
876 ce = find_first_cache_extent(&map_tree->cache_tree, logical);
877 BUG_ON(!ce);
878 BUG_ON(ce->start > logical || ce->start + ce->size < logical);
879 map = container_of(ce, struct map_lookup, ce);
881 offset = logical - ce->start;
882 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
883 ret = map->num_stripes;
884 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
885 ret = map->sub_stripes;
886 else
887 ret = 1;
888 return ret;
891 int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
892 u64 logical, u64 *length,
893 struct btrfs_multi_bio **multi_ret, int mirror_num)
895 struct cache_extent *ce;
896 struct map_lookup *map;
897 u64 offset;
898 u64 stripe_offset;
899 u64 stripe_nr;
900 int stripes_allocated = 8;
901 int stripes_required = 1;
902 int stripe_index;
903 int i;
904 struct btrfs_multi_bio *multi = NULL;
906 if (multi_ret && rw == READ) {
907 stripes_allocated = 1;
909 again:
910 if (multi_ret) {
911 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
912 GFP_NOFS);
913 if (!multi)
914 return -ENOMEM;
917 ce = find_first_cache_extent(&map_tree->cache_tree, logical);
918 BUG_ON(!ce);
919 BUG_ON(ce->start > logical || ce->start + ce->size < logical);
920 map = container_of(ce, struct map_lookup, ce);
921 offset = logical - ce->start;
923 if (rw == WRITE) {
924 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
925 BTRFS_BLOCK_GROUP_DUP)) {
926 stripes_required = map->num_stripes;
927 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
928 stripes_required = map->sub_stripes;
931 /* if our multi bio struct is too small, back off and try again */
932 if (multi_ret && rw == WRITE &&
933 stripes_allocated < stripes_required) {
934 stripes_allocated = map->num_stripes;
935 kfree(multi);
936 goto again;
938 stripe_nr = offset;
940 * stripe_nr counts the total number of stripes we have to stride
941 * to get to this block
943 stripe_nr = stripe_nr / map->stripe_len;
945 stripe_offset = stripe_nr * map->stripe_len;
946 BUG_ON(offset < stripe_offset);
948 /* stripe_offset is the offset of this block in its stripe*/
949 stripe_offset = offset - stripe_offset;
951 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
952 BTRFS_BLOCK_GROUP_RAID10 |
953 BTRFS_BLOCK_GROUP_DUP)) {
954 /* we limit the length of each bio to what fits in a stripe */
955 *length = min_t(u64, ce->size - offset,
956 map->stripe_len - stripe_offset);
957 } else {
958 *length = ce->size - offset;
961 if (!multi_ret)
962 goto out;
964 multi->num_stripes = 1;
965 stripe_index = 0;
966 if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
967 if (rw == WRITE)
968 multi->num_stripes = map->num_stripes;
969 else if (mirror_num)
970 stripe_index = mirror_num - 1;
971 else
972 stripe_index = stripe_nr % map->num_stripes;
973 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
974 int factor = map->num_stripes / map->sub_stripes;
976 stripe_index = stripe_nr % factor;
977 stripe_index *= map->sub_stripes;
979 if (rw == WRITE)
980 multi->num_stripes = map->sub_stripes;
981 else if (mirror_num)
982 stripe_index += mirror_num - 1;
983 else
984 stripe_index = stripe_nr % map->sub_stripes;
986 stripe_nr = stripe_nr / factor;
987 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
988 if (rw == WRITE)
989 multi->num_stripes = map->num_stripes;
990 else if (mirror_num)
991 stripe_index = mirror_num - 1;
992 } else {
994 * after this do_div call, stripe_nr is the number of stripes
995 * on this device we have to walk to find the data, and
996 * stripe_index is the number of our device in the stripe array
998 stripe_index = stripe_nr % map->num_stripes;
999 stripe_nr = stripe_nr / map->num_stripes;
1001 BUG_ON(stripe_index >= map->num_stripes);
1003 BUG_ON(stripe_index != 0 && multi->num_stripes > 1);
1004 for (i = 0; i < multi->num_stripes; i++) {
1005 multi->stripes[i].physical =
1006 map->stripes[stripe_index].physical + stripe_offset +
1007 stripe_nr * map->stripe_len;
1008 multi->stripes[i].dev = map->stripes[stripe_index].dev;
1009 stripe_index++;
1011 *multi_ret = multi;
1012 out:
1013 return 0;
1016 struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
1017 u8 *uuid, u8 *fsid)
1019 struct btrfs_device *device;
1020 struct btrfs_fs_devices *cur_devices;
1022 cur_devices = root->fs_info->fs_devices;
1023 while (cur_devices) {
1024 if (!fsid ||
1025 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
1026 device = __find_device(&cur_devices->devices,
1027 devid, uuid);
1028 if (device)
1029 return device;
1031 cur_devices = cur_devices->seed;
1033 return NULL;
1036 int btrfs_bootstrap_super_map(struct btrfs_mapping_tree *map_tree,
1037 struct btrfs_fs_devices *fs_devices)
1039 struct map_lookup *map;
1040 u64 logical = BTRFS_SUPER_INFO_OFFSET;
1041 u64 length = BTRFS_SUPER_INFO_SIZE;
1042 int num_stripes = 0;
1043 int sub_stripes = 0;
1044 int ret;
1045 int i;
1046 struct list_head *cur;
1048 list_for_each(cur, &fs_devices->devices) {
1049 num_stripes++;
1051 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
1052 if (!map)
1053 return -ENOMEM;
1055 map->ce.start = logical;
1056 map->ce.size = length;
1057 map->num_stripes = num_stripes;
1058 map->sub_stripes = sub_stripes;
1059 map->io_width = length;
1060 map->io_align = length;
1061 map->sector_size = length;
1062 map->stripe_len = length;
1063 map->type = BTRFS_BLOCK_GROUP_RAID1;
1065 i = 0;
1066 list_for_each(cur, &fs_devices->devices) {
1067 struct btrfs_device *device = list_entry(cur,
1068 struct btrfs_device,
1069 dev_list);
1070 map->stripes[i].physical = logical;
1071 map->stripes[i].dev = device;
1072 i++;
1074 ret = insert_existing_cache_extent(&map_tree->cache_tree, &map->ce);
1075 if (ret == -EEXIST) {
1076 struct cache_extent *old;
1077 struct map_lookup *old_map;
1078 old = find_cache_extent(&map_tree->cache_tree, logical, length);
1079 old_map = container_of(old, struct map_lookup, ce);
1080 remove_cache_extent(&map_tree->cache_tree, old);
1081 kfree(old_map);
1082 ret = insert_existing_cache_extent(&map_tree->cache_tree,
1083 &map->ce);
1085 BUG_ON(ret);
1086 return 0;
1089 int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
1091 struct cache_extent *ce;
1092 struct map_lookup *map;
1093 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
1094 int readonly = 0;
1095 int i;
1097 ce = find_first_cache_extent(&map_tree->cache_tree, chunk_offset);
1098 BUG_ON(!ce);
1100 map = container_of(ce, struct map_lookup, ce);
1101 for (i = 0; i < map->num_stripes; i++) {
1102 if (!map->stripes[i].dev->writeable) {
1103 readonly = 1;
1104 break;
1108 return readonly;
1111 static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
1112 struct extent_buffer *leaf,
1113 struct btrfs_chunk *chunk)
1115 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
1116 struct map_lookup *map;
1117 struct cache_extent *ce;
1118 u64 logical;
1119 u64 length;
1120 u64 devid;
1121 u64 super_offset_diff = 0;
1122 u8 uuid[BTRFS_UUID_SIZE];
1123 int num_stripes;
1124 int ret;
1125 int i;
1127 logical = key->offset;
1128 length = btrfs_chunk_length(leaf, chunk);
1130 if (logical < BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE) {
1131 super_offset_diff = BTRFS_SUPER_INFO_OFFSET +
1132 BTRFS_SUPER_INFO_SIZE - logical;
1133 logical = BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE;
1136 ce = find_first_cache_extent(&map_tree->cache_tree, logical);
1138 /* already mapped? */
1139 if (ce && ce->start <= logical && ce->start + ce->size > logical) {
1140 return 0;
1143 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
1144 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
1145 if (!map)
1146 return -ENOMEM;
1148 map->ce.start = logical;
1149 map->ce.size = length - super_offset_diff;
1150 map->num_stripes = num_stripes;
1151 map->io_width = btrfs_chunk_io_width(leaf, chunk);
1152 map->io_align = btrfs_chunk_io_align(leaf, chunk);
1153 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
1154 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
1155 map->type = btrfs_chunk_type(leaf, chunk);
1156 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
1158 for (i = 0; i < num_stripes; i++) {
1159 map->stripes[i].physical =
1160 btrfs_stripe_offset_nr(leaf, chunk, i) +
1161 super_offset_diff;
1162 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
1163 read_extent_buffer(leaf, uuid, (unsigned long)
1164 btrfs_stripe_dev_uuid_nr(chunk, i),
1165 BTRFS_UUID_SIZE);
1166 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
1167 NULL);
1168 if (!map->stripes[i].dev) {
1169 kfree(map);
1170 return -EIO;
1174 ret = insert_existing_cache_extent(&map_tree->cache_tree, &map->ce);
1175 BUG_ON(ret);
1177 return 0;
1180 static int fill_device_from_item(struct extent_buffer *leaf,
1181 struct btrfs_dev_item *dev_item,
1182 struct btrfs_device *device)
1184 unsigned long ptr;
1186 device->devid = btrfs_device_id(leaf, dev_item);
1187 device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
1188 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
1189 device->type = btrfs_device_type(leaf, dev_item);
1190 device->io_align = btrfs_device_io_align(leaf, dev_item);
1191 device->io_width = btrfs_device_io_width(leaf, dev_item);
1192 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
1194 ptr = (unsigned long)btrfs_device_uuid(dev_item);
1195 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1197 return 0;
1200 static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
1202 struct btrfs_fs_devices *fs_devices;
1203 int ret;
1205 fs_devices = root->fs_info->fs_devices->seed;
1206 while (fs_devices) {
1207 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
1208 ret = 0;
1209 goto out;
1211 fs_devices = fs_devices->seed;
1214 fs_devices = find_fsid(fsid);
1215 if (!fs_devices) {
1216 ret = -ENOENT;
1217 goto out;
1220 ret = btrfs_open_devices(fs_devices, O_RDONLY);
1221 if (ret)
1222 goto out;
1224 fs_devices->seed = root->fs_info->fs_devices->seed;
1225 root->fs_info->fs_devices->seed = fs_devices;
1226 out:
1227 return ret;
1230 static int read_one_dev(struct btrfs_root *root,
1231 struct extent_buffer *leaf,
1232 struct btrfs_dev_item *dev_item)
1234 struct btrfs_device *device;
1235 u64 devid;
1236 int ret = 0;
1237 u8 fs_uuid[BTRFS_UUID_SIZE];
1238 u8 dev_uuid[BTRFS_UUID_SIZE];
1240 devid = btrfs_device_id(leaf, dev_item);
1241 read_extent_buffer(leaf, dev_uuid,
1242 (unsigned long)btrfs_device_uuid(dev_item),
1243 BTRFS_UUID_SIZE);
1244 read_extent_buffer(leaf, fs_uuid,
1245 (unsigned long)btrfs_device_fsid(dev_item),
1246 BTRFS_UUID_SIZE);
1248 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
1249 ret = open_seed_devices(root, fs_uuid);
1250 if (ret)
1251 return ret;
1254 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1255 if (!device) {
1256 printk("warning devid %llu not found already\n",
1257 (unsigned long long)devid);
1258 device = kmalloc(sizeof(*device), GFP_NOFS);
1259 if (!device)
1260 return -ENOMEM;
1261 device->total_ios = 0;
1262 list_add(&device->dev_list,
1263 &root->fs_info->fs_devices->devices);
1266 fill_device_from_item(leaf, dev_item, device);
1267 device->dev_root = root->fs_info->dev_root;
1268 return ret;
1271 int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
1273 struct btrfs_dev_item *dev_item;
1275 dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
1276 dev_item);
1277 return read_one_dev(root, buf, dev_item);
1280 int btrfs_read_sys_array(struct btrfs_root *root)
1282 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1283 struct extent_buffer *sb = root->fs_info->sb_buffer;
1284 struct btrfs_disk_key *disk_key;
1285 struct btrfs_chunk *chunk;
1286 struct btrfs_key key;
1287 u32 num_stripes;
1288 u32 array_size;
1289 u32 len = 0;
1290 u8 *ptr;
1291 unsigned long sb_ptr;
1292 u32 cur;
1293 int ret;
1295 array_size = btrfs_super_sys_array_size(super_copy);
1298 * we do this loop twice, once for the device items and
1299 * once for all of the chunks. This way there are device
1300 * structs filled in for every chunk
1302 ptr = super_copy->sys_chunk_array;
1303 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
1304 cur = 0;
1306 while (cur < array_size) {
1307 disk_key = (struct btrfs_disk_key *)ptr;
1308 btrfs_disk_key_to_cpu(&key, disk_key);
1310 len = sizeof(*disk_key);
1311 ptr += len;
1312 sb_ptr += len;
1313 cur += len;
1315 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1316 chunk = (struct btrfs_chunk *)sb_ptr;
1317 ret = read_one_chunk(root, &key, sb, chunk);
1318 BUG_ON(ret);
1319 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
1320 len = btrfs_chunk_item_size(num_stripes);
1321 } else {
1322 BUG();
1324 ptr += len;
1325 sb_ptr += len;
1326 cur += len;
1328 return 0;
1331 int btrfs_read_chunk_tree(struct btrfs_root *root)
1333 struct btrfs_path *path;
1334 struct extent_buffer *leaf;
1335 struct btrfs_key key;
1336 struct btrfs_key found_key;
1337 int ret;
1338 int slot;
1340 root = root->fs_info->chunk_root;
1342 path = btrfs_alloc_path();
1343 if (!path)
1344 return -ENOMEM;
1346 /* first we search for all of the device items, and then we
1347 * read in all of the chunk items. This way we can create chunk
1348 * mappings that reference all of the devices that are afound
1350 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1351 key.offset = 0;
1352 key.type = 0;
1353 again:
1354 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1355 while(1) {
1356 leaf = path->nodes[0];
1357 slot = path->slots[0];
1358 if (slot >= btrfs_header_nritems(leaf)) {
1359 ret = btrfs_next_leaf(root, path);
1360 if (ret == 0)
1361 continue;
1362 if (ret < 0)
1363 goto error;
1364 break;
1366 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1367 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
1368 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
1369 break;
1370 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
1371 struct btrfs_dev_item *dev_item;
1372 dev_item = btrfs_item_ptr(leaf, slot,
1373 struct btrfs_dev_item);
1374 ret = read_one_dev(root, leaf, dev_item);
1375 BUG_ON(ret);
1377 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
1378 struct btrfs_chunk *chunk;
1379 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
1380 ret = read_one_chunk(root, &found_key, leaf, chunk);
1381 BUG_ON(ret);
1383 path->slots[0]++;
1385 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
1386 key.objectid = 0;
1387 btrfs_release_path(root, path);
1388 goto again;
1391 btrfs_free_path(path);
1392 ret = 0;
1393 error:
1394 return ret;
1397 struct list_head *btrfs_scanned_uuids(void)
1399 return &fs_uuids;