Use better chunk sizes for small and large filesystems
[btrfs-progs-unstable/devel.git] / volumes.c
blobce44c09221a2185b616520b05160a478ebe32616
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 list_add(&device->dev_list, &fs_devices->devices);
125 if (found_transid > fs_devices->latest_trans) {
126 fs_devices->latest_devid = devid;
127 fs_devices->latest_trans = found_transid;
129 if (fs_devices->lowest_devid > devid) {
130 fs_devices->lowest_devid = devid;
132 *fs_devices_ret = fs_devices;
133 return 0;
136 int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
138 struct list_head *head = &fs_devices->devices;
139 struct list_head *cur;
140 struct btrfs_device *device;
142 list_for_each(cur, head) {
143 device = list_entry(cur, struct btrfs_device, dev_list);
144 device->fd = 0;
146 return 0;
149 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, int flags)
151 int fd;
152 struct list_head *head = &fs_devices->devices;
153 struct list_head *cur;
154 struct btrfs_device *device;
155 int ret;
157 list_for_each(cur, head) {
158 device = list_entry(cur, struct btrfs_device, dev_list);
159 fd = open(device->name, flags);
160 printk("opening %s devid %llu fd %d\n", device->name,
161 (unsigned long long)device->devid, fd);
162 if (fd < 0) {
163 ret = -errno;
164 goto fail;
166 if (device->devid == fs_devices->latest_devid)
167 fs_devices->latest_bdev = fd;
168 if (device->devid == fs_devices->lowest_devid)
169 fs_devices->lowest_bdev = fd;
170 device->fd = fd;
172 return 0;
173 fail:
174 btrfs_close_devices(fs_devices);
175 return ret;
178 int btrfs_scan_one_device(int fd, const char *path,
179 struct btrfs_fs_devices **fs_devices_ret,
180 u64 *total_devs, u64 super_offset)
182 struct btrfs_super_block *disk_super;
183 char *buf;
184 int ret;
185 u64 devid;
186 char uuidbuf[37];
188 buf = malloc(4096);
189 if (!buf) {
190 ret = -ENOMEM;
191 goto error;
193 ret = pread(fd, buf, 4096, super_offset);
194 if (ret != 4096) {
195 ret = -EIO;
196 goto error;
198 disk_super = (struct btrfs_super_block *)buf;
199 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
200 sizeof(disk_super->magic))) {
201 ret = -ENOENT;
202 goto error_brelse;
204 devid = le64_to_cpu(disk_super->dev_item.devid);
205 *total_devs = btrfs_super_num_devices(disk_super);
206 uuid_unparse(disk_super->fsid, uuidbuf);
208 printf("device ");
209 if (disk_super->label[0])
210 printf("label %s ", disk_super->label);
211 else
212 printf("fsuuid %s ", uuidbuf);
213 printf("devid %llu %s\n", (unsigned long long)devid, path);
214 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
216 error_brelse:
217 free(buf);
218 error:
219 return ret;
223 * this uses a pretty simple search, the expectation is that it is
224 * called very infrequently and that a given device has a small number
225 * of extents
227 static int find_free_dev_extent(struct btrfs_trans_handle *trans,
228 struct btrfs_device *device,
229 struct btrfs_path *path,
230 u64 num_bytes, u64 *start)
232 struct btrfs_key key;
233 struct btrfs_root *root = device->dev_root;
234 struct btrfs_dev_extent *dev_extent = NULL;
235 u64 hole_size = 0;
236 u64 last_byte = 0;
237 u64 search_start = 0;
238 u64 search_end = device->total_bytes;
239 int ret;
240 int slot = 0;
241 int start_found;
242 struct extent_buffer *l;
244 start_found = 0;
245 path->reada = 2;
247 /* FIXME use last free of some kind */
249 /* we don't want to overwrite the superblock on the drive,
250 * so we make sure to start at an offset of at least 1MB
252 search_start = max((u64)1024 * 1024, search_start);
253 key.objectid = device->devid;
254 key.offset = search_start;
255 key.type = BTRFS_DEV_EXTENT_KEY;
256 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
257 if (ret < 0)
258 goto error;
259 ret = btrfs_previous_item(root, path, 0, key.type);
260 if (ret < 0)
261 goto error;
262 l = path->nodes[0];
263 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
264 while (1) {
265 l = path->nodes[0];
266 slot = path->slots[0];
267 if (slot >= btrfs_header_nritems(l)) {
268 ret = btrfs_next_leaf(root, path);
269 if (ret == 0)
270 continue;
271 if (ret < 0)
272 goto error;
273 no_more_items:
274 if (!start_found) {
275 if (search_start >= search_end) {
276 ret = -ENOSPC;
277 goto error;
279 *start = search_start;
280 start_found = 1;
281 goto check_pending;
283 *start = last_byte > search_start ?
284 last_byte : search_start;
285 if (search_end <= *start) {
286 ret = -ENOSPC;
287 goto error;
289 goto check_pending;
291 btrfs_item_key_to_cpu(l, &key, slot);
293 if (key.objectid < device->devid)
294 goto next;
296 if (key.objectid > device->devid)
297 goto no_more_items;
299 if (key.offset >= search_start && key.offset > last_byte &&
300 start_found) {
301 if (last_byte < search_start)
302 last_byte = search_start;
303 hole_size = key.offset - last_byte;
304 if (key.offset > last_byte &&
305 hole_size >= num_bytes) {
306 *start = last_byte;
307 goto check_pending;
310 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) {
311 goto next;
314 start_found = 1;
315 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
316 last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent);
317 next:
318 path->slots[0]++;
319 cond_resched();
321 check_pending:
322 /* we have to make sure we didn't find an extent that has already
323 * been allocated by the map tree or the original allocation
325 btrfs_release_path(root, path);
326 BUG_ON(*start < search_start);
328 if (*start + num_bytes > search_end) {
329 ret = -ENOSPC;
330 goto error;
332 /* check for pending inserts here */
333 return 0;
335 error:
336 btrfs_release_path(root, path);
337 return ret;
340 int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
341 struct btrfs_device *device,
342 u64 chunk_tree, u64 chunk_objectid,
343 u64 chunk_offset,
344 u64 num_bytes, u64 *start)
346 int ret;
347 struct btrfs_path *path;
348 struct btrfs_root *root = device->dev_root;
349 struct btrfs_dev_extent *extent;
350 struct extent_buffer *leaf;
351 struct btrfs_key key;
353 path = btrfs_alloc_path();
354 if (!path)
355 return -ENOMEM;
357 ret = find_free_dev_extent(trans, device, path, num_bytes, start);
358 if (ret) {
359 goto err;
362 key.objectid = device->devid;
363 key.offset = *start;
364 key.type = BTRFS_DEV_EXTENT_KEY;
365 ret = btrfs_insert_empty_item(trans, root, path, &key,
366 sizeof(*extent));
367 BUG_ON(ret);
369 leaf = path->nodes[0];
370 extent = btrfs_item_ptr(leaf, path->slots[0],
371 struct btrfs_dev_extent);
372 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
373 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
374 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
376 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
377 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
378 BTRFS_UUID_SIZE);
380 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
381 btrfs_mark_buffer_dirty(leaf);
382 err:
383 btrfs_free_path(path);
384 return ret;
387 static int find_next_chunk(struct btrfs_root *root, u64 objectid, u64 *offset)
389 struct btrfs_path *path;
390 int ret;
391 struct btrfs_key key;
392 struct btrfs_chunk *chunk;
393 struct btrfs_key found_key;
395 path = btrfs_alloc_path();
396 BUG_ON(!path);
398 key.objectid = objectid;
399 key.offset = (u64)-1;
400 key.type = BTRFS_CHUNK_ITEM_KEY;
402 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
403 if (ret < 0)
404 goto error;
406 BUG_ON(ret == 0);
408 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
409 if (ret) {
410 *offset = 0;
411 } else {
412 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
413 path->slots[0]);
414 if (found_key.objectid != objectid)
415 *offset = 0;
416 else {
417 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
418 struct btrfs_chunk);
419 *offset = found_key.offset +
420 btrfs_chunk_length(path->nodes[0], chunk);
423 ret = 0;
424 error:
425 btrfs_free_path(path);
426 return ret;
429 static int find_next_devid(struct btrfs_root *root, struct btrfs_path *path,
430 u64 *objectid)
432 int ret;
433 struct btrfs_key key;
434 struct btrfs_key found_key;
436 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
437 key.type = BTRFS_DEV_ITEM_KEY;
438 key.offset = (u64)-1;
440 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
441 if (ret < 0)
442 goto error;
444 BUG_ON(ret == 0);
446 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
447 BTRFS_DEV_ITEM_KEY);
448 if (ret) {
449 *objectid = 1;
450 } else {
451 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
452 path->slots[0]);
453 *objectid = found_key.offset + 1;
455 ret = 0;
456 error:
457 btrfs_release_path(root, path);
458 return ret;
462 * the device information is stored in the chunk root
463 * the btrfs_device struct should be fully filled in
465 int btrfs_add_device(struct btrfs_trans_handle *trans,
466 struct btrfs_root *root,
467 struct btrfs_device *device)
469 int ret;
470 struct btrfs_path *path;
471 struct btrfs_dev_item *dev_item;
472 struct extent_buffer *leaf;
473 struct btrfs_key key;
474 unsigned long ptr;
475 u64 free_devid;
477 root = root->fs_info->chunk_root;
479 path = btrfs_alloc_path();
480 if (!path)
481 return -ENOMEM;
483 ret = find_next_devid(root, path, &free_devid);
484 if (ret)
485 goto out;
487 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
488 key.type = BTRFS_DEV_ITEM_KEY;
489 key.offset = free_devid;
491 ret = btrfs_insert_empty_item(trans, root, path, &key,
492 sizeof(*dev_item));
493 if (ret)
494 goto out;
496 leaf = path->nodes[0];
497 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
499 device->devid = free_devid;
500 btrfs_set_device_id(leaf, dev_item, device->devid);
501 btrfs_set_device_type(leaf, dev_item, device->type);
502 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
503 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
504 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
505 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
506 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
507 btrfs_set_device_group(leaf, dev_item, 0);
508 btrfs_set_device_seek_speed(leaf, dev_item, 0);
509 btrfs_set_device_bandwidth(leaf, dev_item, 0);
511 ptr = (unsigned long)btrfs_device_uuid(dev_item);
512 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
513 btrfs_mark_buffer_dirty(leaf);
514 ret = 0;
516 out:
517 btrfs_free_path(path);
518 return ret;
521 int btrfs_update_device(struct btrfs_trans_handle *trans,
522 struct btrfs_device *device)
524 int ret;
525 struct btrfs_path *path;
526 struct btrfs_root *root;
527 struct btrfs_dev_item *dev_item;
528 struct extent_buffer *leaf;
529 struct btrfs_key key;
531 root = device->dev_root->fs_info->chunk_root;
533 path = btrfs_alloc_path();
534 if (!path)
535 return -ENOMEM;
537 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
538 key.type = BTRFS_DEV_ITEM_KEY;
539 key.offset = device->devid;
541 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
542 if (ret < 0)
543 goto out;
545 if (ret > 0) {
546 ret = -ENOENT;
547 goto out;
550 leaf = path->nodes[0];
551 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
553 btrfs_set_device_id(leaf, dev_item, device->devid);
554 btrfs_set_device_type(leaf, dev_item, device->type);
555 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
556 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
557 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
558 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
559 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
560 btrfs_mark_buffer_dirty(leaf);
562 out:
563 btrfs_free_path(path);
564 return ret;
567 int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
568 struct btrfs_root *root,
569 struct btrfs_key *key,
570 struct btrfs_chunk *chunk, int item_size)
572 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
573 struct btrfs_disk_key disk_key;
574 u32 array_size;
575 u8 *ptr;
577 array_size = btrfs_super_sys_array_size(super_copy);
578 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
579 return -EFBIG;
581 ptr = super_copy->sys_chunk_array + array_size;
582 btrfs_cpu_key_to_disk(&disk_key, key);
583 memcpy(ptr, &disk_key, sizeof(disk_key));
584 ptr += sizeof(disk_key);
585 memcpy(ptr, chunk, item_size);
586 item_size += sizeof(disk_key);
587 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
588 return 0;
591 static u64 div_factor(u64 num, int factor)
593 if (factor == 10)
594 return num;
595 num *= factor;
596 return num / 10;
599 static u64 chunk_bytes_by_type(u64 type, u64 calc_size, int num_stripes,
600 int sub_stripes)
602 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
603 return calc_size;
604 else if (type & BTRFS_BLOCK_GROUP_RAID10)
605 return calc_size * (num_stripes / sub_stripes);
606 else
607 return calc_size * num_stripes;
611 int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
612 struct btrfs_root *extent_root, u64 *start,
613 u64 *num_bytes, u64 type)
615 u64 dev_offset;
616 struct btrfs_fs_info *info = extent_root->fs_info;
617 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
618 struct btrfs_stripe *stripes;
619 struct btrfs_device *device = NULL;
620 struct btrfs_chunk *chunk;
621 struct list_head private_devs;
622 struct list_head *dev_list = &extent_root->fs_info->fs_devices->devices;
623 struct list_head *cur;
624 struct map_lookup *map;
625 int min_chunk_size = 8 * 1024 * 1024;
626 u64 physical;
627 u64 calc_size = 8 * 1024 * 1024;
628 u64 min_free;
629 u64 max_chunk_size = 4 * calc_size;
630 u64 avail;
631 u64 max_avail = 0;
632 u64 percent_max;
633 int num_stripes = 1;
634 int sub_stripes = 0;
635 int looped = 0;
636 int ret;
637 int index;
638 int stripe_len = 64 * 1024;
639 struct btrfs_key key;
641 if (list_empty(dev_list)) {
642 return -ENOSPC;
645 if (type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
646 BTRFS_BLOCK_GROUP_RAID10 |
647 BTRFS_BLOCK_GROUP_DUP)) {
648 if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
649 calc_size = 128 * 1024 * 1024;
650 max_chunk_size = 4 * calc_size;
651 min_chunk_size = 32 * 1024 * 1024;
652 } else if (type & BTRFS_BLOCK_GROUP_DATA) {
653 calc_size = 1024 * 1024 * 1024;
654 max_chunk_size = 10 * calc_size;
655 min_chunk_size = 256 * 1024 * 1024;
656 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
657 calc_size = 1024 * 1024 * 1024;
658 max_chunk_size = 4 * calc_size;
659 min_chunk_size = 64 * 1024 * 1024;
662 if (type & BTRFS_BLOCK_GROUP_RAID1) {
663 num_stripes = min_t(u64, 2,
664 btrfs_super_num_devices(&info->super_copy));
665 if (num_stripes < 2)
666 return -ENOSPC;
668 if (type & BTRFS_BLOCK_GROUP_DUP)
669 num_stripes = 2;
670 if (type & (BTRFS_BLOCK_GROUP_RAID0))
671 num_stripes = btrfs_super_num_devices(&info->super_copy);
672 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
673 num_stripes = btrfs_super_num_devices(&info->super_copy);
674 if (num_stripes < 4)
675 return -ENOSPC;
676 num_stripes &= ~(u32)1;
677 sub_stripes = 2;
680 /* we don't want a chunk larger than 10% of the FS */
681 percent_max = div_factor(btrfs_super_total_bytes(&info->super_copy), 1);
682 max_chunk_size = min(percent_max, max_chunk_size);
684 if (calc_size * num_stripes > max_chunk_size) {
685 calc_size = max_chunk_size;
686 calc_size /= num_stripes;
687 calc_size /= stripe_len;
688 calc_size *= stripe_len;
690 /* we don't want tiny stripes */
691 *num_bytes = chunk_bytes_by_type(type, calc_size,
692 num_stripes, sub_stripes);
693 calc_size = max_t(u64, chunk_bytes_by_type(type, min_chunk_size,
694 num_stripes, sub_stripes), calc_size);
696 again:
697 calc_size /= stripe_len;
698 calc_size *= stripe_len;
700 INIT_LIST_HEAD(&private_devs);
701 cur = dev_list->next;
702 index = 0;
704 if (type & BTRFS_BLOCK_GROUP_DUP)
705 min_free = calc_size * 2;
706 else
707 min_free = calc_size;
709 /* build a private list of devices we will allocate from */
710 while(index < num_stripes) {
711 device = list_entry(cur, struct btrfs_device, dev_list);
712 avail = device->total_bytes - device->bytes_used;
713 cur = cur->next;
714 if (avail > max_avail)
715 max_avail = avail;
716 if (avail >= min_free) {
717 list_move_tail(&device->dev_list, &private_devs);
718 index++;
719 if (type & BTRFS_BLOCK_GROUP_DUP)
720 index++;
722 if (cur == dev_list)
723 break;
725 if (index < num_stripes) {
726 list_splice(&private_devs, dev_list);
727 if (!looped && max_avail > 0) {
728 looped = 1;
729 calc_size = max_avail;
730 goto again;
732 return -ENOSPC;
735 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
736 key.type = BTRFS_CHUNK_ITEM_KEY;
737 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
738 &key.offset);
739 if (ret)
740 return ret;
742 chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS);
743 if (!chunk)
744 return -ENOMEM;
746 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
747 if (!map) {
748 kfree(chunk);
749 return -ENOMEM;
752 stripes = &chunk->stripe;
753 *num_bytes = chunk_bytes_by_type(type, calc_size,
754 num_stripes, sub_stripes);
755 index = 0;
756 printk("new chunk type %Lu start %Lu size %Lu\n", type, key.offset, *num_bytes);
757 while(index < num_stripes) {
758 struct btrfs_stripe *stripe;
759 BUG_ON(list_empty(&private_devs));
760 cur = private_devs.next;
761 device = list_entry(cur, struct btrfs_device, dev_list);
763 /* loop over this device again if we're doing a dup group */
764 if (!(type & BTRFS_BLOCK_GROUP_DUP) ||
765 (index == num_stripes - 1))
766 list_move_tail(&device->dev_list, dev_list);
768 ret = btrfs_alloc_dev_extent(trans, device,
769 info->chunk_root->root_key.objectid,
770 BTRFS_FIRST_CHUNK_TREE_OBJECTID, key.offset,
771 calc_size, &dev_offset);
772 BUG_ON(ret);
773 printk("\talloc chunk size %llu from dev %llu phys %llu\n",
774 (unsigned long long)calc_size,
775 (unsigned long long)device->devid,
776 (unsigned long long)dev_offset);
777 device->bytes_used += calc_size;
778 ret = btrfs_update_device(trans, device);
779 BUG_ON(ret);
781 map->stripes[index].dev = device;
782 map->stripes[index].physical = dev_offset;
783 stripe = stripes + index;
784 btrfs_set_stack_stripe_devid(stripe, device->devid);
785 btrfs_set_stack_stripe_offset(stripe, dev_offset);
786 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
787 physical = dev_offset;
788 index++;
790 BUG_ON(!list_empty(&private_devs));
792 /* key was set above */
793 btrfs_set_stack_chunk_length(chunk, *num_bytes);
794 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
795 btrfs_set_stack_chunk_stripe_len(chunk, stripe_len);
796 btrfs_set_stack_chunk_type(chunk, type);
797 btrfs_set_stack_chunk_num_stripes(chunk, num_stripes);
798 btrfs_set_stack_chunk_io_align(chunk, stripe_len);
799 btrfs_set_stack_chunk_io_width(chunk, stripe_len);
800 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
801 btrfs_set_stack_chunk_sub_stripes(chunk, sub_stripes);
802 map->sector_size = extent_root->sectorsize;
803 map->stripe_len = stripe_len;
804 map->io_align = stripe_len;
805 map->io_width = stripe_len;
806 map->type = type;
807 map->num_stripes = num_stripes;
808 map->sub_stripes = sub_stripes;
810 ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
811 btrfs_chunk_item_size(num_stripes));
812 BUG_ON(ret);
813 *start = key.offset;;
815 map->ce.start = key.offset;
816 map->ce.size = *num_bytes;
818 ret = insert_existing_cache_extent(
819 &extent_root->fs_info->mapping_tree.cache_tree,
820 &map->ce);
821 BUG_ON(ret);
823 if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
824 ret = btrfs_add_system_chunk(trans, chunk_root, &key,
825 chunk, btrfs_chunk_item_size(num_stripes));
826 BUG_ON(ret);
829 kfree(chunk);
830 return ret;
833 void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
835 cache_tree_init(&tree->cache_tree);
838 int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
840 struct cache_extent *ce;
841 struct map_lookup *map;
842 int ret;
843 u64 offset;
845 ce = find_first_cache_extent(&map_tree->cache_tree, logical);
846 BUG_ON(!ce);
847 BUG_ON(ce->start > logical || ce->start + ce->size < logical);
848 map = container_of(ce, struct map_lookup, ce);
850 offset = logical - ce->start;
851 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
852 ret = map->num_stripes;
853 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
854 ret = map->sub_stripes;
855 else
856 ret = 1;
857 return ret;
860 int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
861 u64 logical, u64 *length,
862 struct btrfs_multi_bio **multi_ret, int mirror_num)
864 struct cache_extent *ce;
865 struct map_lookup *map;
866 u64 offset;
867 u64 stripe_offset;
868 u64 stripe_nr;
869 int stripes_allocated = 8;
870 int stripes_required = 1;
871 int stripe_index;
872 int i;
873 struct btrfs_multi_bio *multi = NULL;
875 if (multi_ret && rw == READ) {
876 stripes_allocated = 1;
878 again:
879 if (multi_ret) {
880 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
881 GFP_NOFS);
882 if (!multi)
883 return -ENOMEM;
886 ce = find_first_cache_extent(&map_tree->cache_tree, logical);
887 BUG_ON(!ce);
888 BUG_ON(ce->start > logical || ce->start + ce->size < logical);
889 map = container_of(ce, struct map_lookup, ce);
890 offset = logical - ce->start;
892 if (rw == WRITE) {
893 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
894 BTRFS_BLOCK_GROUP_DUP)) {
895 stripes_required = map->num_stripes;
896 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
897 stripes_required = map->sub_stripes;
900 /* if our multi bio struct is too small, back off and try again */
901 if (multi_ret && rw == WRITE &&
902 stripes_allocated < stripes_required) {
903 stripes_allocated = map->num_stripes;
904 kfree(multi);
905 goto again;
907 stripe_nr = offset;
909 * stripe_nr counts the total number of stripes we have to stride
910 * to get to this block
912 stripe_nr = stripe_nr / map->stripe_len;
914 stripe_offset = stripe_nr * map->stripe_len;
915 BUG_ON(offset < stripe_offset);
917 /* stripe_offset is the offset of this block in its stripe*/
918 stripe_offset = offset - stripe_offset;
920 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
921 BTRFS_BLOCK_GROUP_RAID10 |
922 BTRFS_BLOCK_GROUP_DUP)) {
923 /* we limit the length of each bio to what fits in a stripe */
924 *length = min_t(u64, ce->size - offset,
925 map->stripe_len - stripe_offset);
926 } else {
927 *length = ce->size - offset;
930 if (!multi_ret)
931 goto out;
933 multi->num_stripes = 1;
934 stripe_index = 0;
935 if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
936 if (rw == WRITE)
937 multi->num_stripes = map->num_stripes;
938 else if (mirror_num)
939 stripe_index = mirror_num - 1;
940 else
941 stripe_index = stripe_nr % map->num_stripes;
942 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
943 int factor = map->num_stripes / map->sub_stripes;
945 stripe_index = stripe_nr % factor;
946 stripe_index *= map->sub_stripes;
948 if (rw == WRITE)
949 multi->num_stripes = map->sub_stripes;
950 else if (mirror_num)
951 stripe_index += mirror_num - 1;
952 else
953 stripe_index = stripe_nr % map->sub_stripes;
955 stripe_nr = stripe_nr / factor;
956 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
957 if (rw == WRITE)
958 multi->num_stripes = map->num_stripes;
959 else if (mirror_num)
960 stripe_index = mirror_num - 1;
961 } else {
963 * after this do_div call, stripe_nr is the number of stripes
964 * on this device we have to walk to find the data, and
965 * stripe_index is the number of our device in the stripe array
967 stripe_index = stripe_nr % map->num_stripes;
968 stripe_nr = stripe_nr / map->num_stripes;
970 BUG_ON(stripe_index >= map->num_stripes);
972 BUG_ON(stripe_index != 0 && multi->num_stripes > 1);
973 for (i = 0; i < multi->num_stripes; i++) {
974 multi->stripes[i].physical =
975 map->stripes[stripe_index].physical + stripe_offset +
976 stripe_nr * map->stripe_len;
977 multi->stripes[i].dev = map->stripes[stripe_index].dev;
978 stripe_index++;
980 *multi_ret = multi;
981 out:
982 return 0;
985 struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
986 u8 *uuid)
988 struct list_head *head = &root->fs_info->fs_devices->devices;
990 return __find_device(head, devid, uuid);
993 int btrfs_bootstrap_super_map(struct btrfs_mapping_tree *map_tree,
994 struct btrfs_fs_devices *fs_devices)
996 struct map_lookup *map;
997 u64 logical = BTRFS_SUPER_INFO_OFFSET;
998 u64 length = BTRFS_SUPER_INFO_SIZE;
999 int num_stripes = 0;
1000 int sub_stripes = 0;
1001 int ret;
1002 int i;
1003 struct list_head *cur;
1005 list_for_each(cur, &fs_devices->devices) {
1006 num_stripes++;
1008 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
1009 if (!map)
1010 return -ENOMEM;
1012 map->ce.start = logical;
1013 map->ce.size = length;
1014 map->num_stripes = num_stripes;
1015 map->sub_stripes = sub_stripes;
1016 map->io_width = length;
1017 map->io_align = length;
1018 map->sector_size = length;
1019 map->stripe_len = length;
1020 map->type = BTRFS_BLOCK_GROUP_RAID1;
1022 i = 0;
1023 list_for_each(cur, &fs_devices->devices) {
1024 struct btrfs_device *device = list_entry(cur,
1025 struct btrfs_device,
1026 dev_list);
1027 map->stripes[i].physical = logical;
1028 map->stripes[i].dev = device;
1029 i++;
1031 ret = insert_existing_cache_extent(&map_tree->cache_tree, &map->ce);
1032 if (ret == -EEXIST) {
1033 struct cache_extent *old;
1034 struct map_lookup *old_map;
1035 old = find_cache_extent(&map_tree->cache_tree, logical, length);
1036 old_map = container_of(old, struct map_lookup, ce);
1037 remove_cache_extent(&map_tree->cache_tree, old);
1038 kfree(old_map);
1039 ret = insert_existing_cache_extent(&map_tree->cache_tree,
1040 &map->ce);
1042 BUG_ON(ret);
1043 return 0;
1046 static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
1047 struct extent_buffer *leaf,
1048 struct btrfs_chunk *chunk)
1050 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
1051 struct map_lookup *map;
1052 struct cache_extent *ce;
1053 u64 logical;
1054 u64 length;
1055 u64 devid;
1056 u64 super_offset_diff = 0;
1057 u8 uuid[BTRFS_UUID_SIZE];
1058 int num_stripes;
1059 int ret;
1060 int i;
1062 logical = key->offset;
1063 length = btrfs_chunk_length(leaf, chunk);
1065 if (logical < BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE) {
1066 super_offset_diff = BTRFS_SUPER_INFO_OFFSET +
1067 BTRFS_SUPER_INFO_SIZE - logical;
1068 logical = BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE;
1071 ce = find_first_cache_extent(&map_tree->cache_tree, logical);
1073 /* already mapped? */
1074 if (ce && ce->start <= logical && ce->start + ce->size > logical) {
1075 return 0;
1078 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
1079 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
1080 if (!map)
1081 return -ENOMEM;
1083 map->ce.start = logical;
1084 map->ce.size = length - super_offset_diff;
1085 map->num_stripes = num_stripes;
1086 map->io_width = btrfs_chunk_io_width(leaf, chunk);
1087 map->io_align = btrfs_chunk_io_align(leaf, chunk);
1088 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
1089 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
1090 map->type = btrfs_chunk_type(leaf, chunk);
1091 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
1093 for (i = 0; i < num_stripes; i++) {
1094 map->stripes[i].physical =
1095 btrfs_stripe_offset_nr(leaf, chunk, i) +
1096 super_offset_diff;
1097 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
1098 read_extent_buffer(leaf, uuid, (unsigned long)
1099 btrfs_stripe_dev_uuid_nr(chunk, i),
1100 BTRFS_UUID_SIZE);
1101 map->stripes[i].dev = btrfs_find_device(root, devid, uuid);
1102 if (!map->stripes[i].dev) {
1103 kfree(map);
1104 return -EIO;
1108 ret = insert_existing_cache_extent(&map_tree->cache_tree, &map->ce);
1109 BUG_ON(ret);
1111 return 0;
1114 static int fill_device_from_item(struct extent_buffer *leaf,
1115 struct btrfs_dev_item *dev_item,
1116 struct btrfs_device *device)
1118 unsigned long ptr;
1120 device->devid = btrfs_device_id(leaf, dev_item);
1121 device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
1122 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
1123 device->type = btrfs_device_type(leaf, dev_item);
1124 device->io_align = btrfs_device_io_align(leaf, dev_item);
1125 device->io_width = btrfs_device_io_width(leaf, dev_item);
1126 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
1128 ptr = (unsigned long)btrfs_device_uuid(dev_item);
1129 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1131 return 0;
1134 static int read_one_dev(struct btrfs_root *root,
1135 struct extent_buffer *leaf,
1136 struct btrfs_dev_item *dev_item)
1138 struct btrfs_device *device;
1139 u64 devid;
1140 int ret = 0;
1141 u8 dev_uuid[BTRFS_UUID_SIZE];
1143 devid = btrfs_device_id(leaf, dev_item);
1144 read_extent_buffer(leaf, dev_uuid,
1145 (unsigned long)btrfs_device_uuid(dev_item),
1146 BTRFS_UUID_SIZE);
1147 device = btrfs_find_device(root, devid, dev_uuid);
1148 if (!device) {
1149 printk("warning devid %llu not found already\n",
1150 (unsigned long long)devid);
1151 device = kmalloc(sizeof(*device), GFP_NOFS);
1152 if (!device)
1153 return -ENOMEM;
1154 device->total_ios = 0;
1155 list_add(&device->dev_list,
1156 &root->fs_info->fs_devices->devices);
1159 fill_device_from_item(leaf, dev_item, device);
1160 device->dev_root = root->fs_info->dev_root;
1161 return ret;
1164 int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
1166 struct btrfs_dev_item *dev_item;
1168 dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
1169 dev_item);
1170 return read_one_dev(root, buf, dev_item);
1173 int btrfs_read_sys_array(struct btrfs_root *root)
1175 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1176 struct extent_buffer *sb = root->fs_info->sb_buffer;
1177 struct btrfs_disk_key *disk_key;
1178 struct btrfs_chunk *chunk;
1179 struct btrfs_key key;
1180 u32 num_stripes;
1181 u32 array_size;
1182 u32 len = 0;
1183 u8 *ptr;
1184 unsigned long sb_ptr;
1185 u32 cur;
1186 int ret;
1188 array_size = btrfs_super_sys_array_size(super_copy);
1191 * we do this loop twice, once for the device items and
1192 * once for all of the chunks. This way there are device
1193 * structs filled in for every chunk
1195 ptr = super_copy->sys_chunk_array;
1196 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
1197 cur = 0;
1199 while (cur < array_size) {
1200 disk_key = (struct btrfs_disk_key *)ptr;
1201 btrfs_disk_key_to_cpu(&key, disk_key);
1203 len = sizeof(*disk_key);
1204 ptr += len;
1205 sb_ptr += len;
1206 cur += len;
1208 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1209 chunk = (struct btrfs_chunk *)sb_ptr;
1210 ret = read_one_chunk(root, &key, sb, chunk);
1211 BUG_ON(ret);
1212 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
1213 len = btrfs_chunk_item_size(num_stripes);
1214 } else {
1215 BUG();
1217 ptr += len;
1218 sb_ptr += len;
1219 cur += len;
1221 return 0;
1224 int btrfs_read_chunk_tree(struct btrfs_root *root)
1226 struct btrfs_path *path;
1227 struct extent_buffer *leaf;
1228 struct btrfs_key key;
1229 struct btrfs_key found_key;
1230 int ret;
1231 int slot;
1233 root = root->fs_info->chunk_root;
1235 path = btrfs_alloc_path();
1236 if (!path)
1237 return -ENOMEM;
1239 /* first we search for all of the device items, and then we
1240 * read in all of the chunk items. This way we can create chunk
1241 * mappings that reference all of the devices that are afound
1243 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1244 key.offset = 0;
1245 key.type = 0;
1246 again:
1247 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1248 while(1) {
1249 leaf = path->nodes[0];
1250 slot = path->slots[0];
1251 if (slot >= btrfs_header_nritems(leaf)) {
1252 ret = btrfs_next_leaf(root, path);
1253 if (ret == 0)
1254 continue;
1255 if (ret < 0)
1256 goto error;
1257 break;
1259 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1260 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
1261 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
1262 break;
1263 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
1264 struct btrfs_dev_item *dev_item;
1265 dev_item = btrfs_item_ptr(leaf, slot,
1266 struct btrfs_dev_item);
1267 ret = read_one_dev(root, leaf, dev_item);
1268 BUG_ON(ret);
1270 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
1271 struct btrfs_chunk *chunk;
1272 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
1273 ret = read_one_chunk(root, &found_key, leaf, chunk);
1275 path->slots[0]++;
1277 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
1278 key.objectid = 0;
1279 btrfs_release_path(root, path);
1280 goto again;
1283 btrfs_free_path(path);
1284 ret = 0;
1285 error:
1286 return ret;