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 #include <linux/sched.h>
19 #include <linux/bio.h>
20 #include <linux/buffer_head.h>
21 #include <linux/blkdev.h>
22 #include <linux/random.h>
23 #include <asm/div64.h>
25 #include "extent_map.h"
27 #include "transaction.h"
28 #include "print-tree.h"
39 struct btrfs_bio_stripe stripes[];
42 #define map_lookup_size(n) (sizeof(struct map_lookup) + \
43 (sizeof(struct btrfs_bio_stripe) * (n)))
45 static DEFINE_MUTEX(uuid_mutex);
46 static LIST_HEAD(fs_uuids);
48 void btrfs_lock_volumes(void)
50 mutex_lock(&uuid_mutex);
53 void btrfs_unlock_volumes(void)
55 mutex_unlock(&uuid_mutex);
58 int btrfs_cleanup_fs_uuids(void)
60 struct btrfs_fs_devices *fs_devices;
61 struct list_head *uuid_cur;
62 struct list_head *devices_cur;
63 struct btrfs_device *dev;
65 list_for_each(uuid_cur, &fs_uuids) {
66 fs_devices = list_entry(uuid_cur, struct btrfs_fs_devices,
68 while(!list_empty(&fs_devices->devices)) {
69 devices_cur = fs_devices->devices.next;
70 dev = list_entry(devices_cur, struct btrfs_device,
73 close_bdev_excl(dev->bdev);
75 list_del(&dev->dev_list);
82 static struct btrfs_device *__find_device(struct list_head *head, u64 devid,
85 struct btrfs_device *dev;
86 struct list_head *cur;
88 list_for_each(cur, head) {
89 dev = list_entry(cur, struct btrfs_device, dev_list);
90 if (dev->devid == devid &&
91 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
98 static struct btrfs_fs_devices *find_fsid(u8 *fsid)
100 struct list_head *cur;
101 struct btrfs_fs_devices *fs_devices;
103 list_for_each(cur, &fs_uuids) {
104 fs_devices = list_entry(cur, struct btrfs_fs_devices, list);
105 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
111 static int device_list_add(const char *path,
112 struct btrfs_super_block *disk_super,
113 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
115 struct btrfs_device *device;
116 struct btrfs_fs_devices *fs_devices;
117 u64 found_transid = btrfs_super_generation(disk_super);
119 fs_devices = find_fsid(disk_super->fsid);
121 fs_devices = kmalloc(sizeof(*fs_devices), GFP_NOFS);
124 INIT_LIST_HEAD(&fs_devices->devices);
125 INIT_LIST_HEAD(&fs_devices->alloc_list);
126 list_add(&fs_devices->list, &fs_uuids);
127 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
128 fs_devices->latest_devid = devid;
129 fs_devices->latest_trans = found_transid;
130 fs_devices->lowest_devid = (u64)-1;
131 fs_devices->num_devices = 0;
134 device = __find_device(&fs_devices->devices, devid,
135 disk_super->dev_item.uuid);
138 device = kzalloc(sizeof(*device), GFP_NOFS);
140 /* we can safely leave the fs_devices entry around */
143 device->devid = devid;
144 memcpy(device->uuid, disk_super->dev_item.uuid,
146 device->barriers = 1;
147 spin_lock_init(&device->io_lock);
148 device->name = kstrdup(path, GFP_NOFS);
153 list_add(&device->dev_list, &fs_devices->devices);
154 list_add(&device->dev_alloc_list, &fs_devices->alloc_list);
155 fs_devices->num_devices++;
158 if (found_transid > fs_devices->latest_trans) {
159 fs_devices->latest_devid = devid;
160 fs_devices->latest_trans = found_transid;
162 if (fs_devices->lowest_devid > devid) {
163 fs_devices->lowest_devid = devid;
165 *fs_devices_ret = fs_devices;
169 int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
171 struct list_head *head = &fs_devices->devices;
172 struct list_head *cur;
173 struct btrfs_device *device;
175 mutex_lock(&uuid_mutex);
176 list_for_each(cur, head) {
177 device = list_entry(cur, struct btrfs_device, dev_list);
179 close_bdev_excl(device->bdev);
183 mutex_unlock(&uuid_mutex);
187 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
188 int flags, void *holder)
190 struct block_device *bdev;
191 struct list_head *head = &fs_devices->devices;
192 struct list_head *cur;
193 struct btrfs_device *device;
196 mutex_lock(&uuid_mutex);
197 list_for_each(cur, head) {
198 device = list_entry(cur, struct btrfs_device, dev_list);
202 bdev = open_bdev_excl(device->name, flags, holder);
205 printk("open %s failed\n", device->name);
209 set_blocksize(bdev, 4096);
210 if (device->devid == fs_devices->latest_devid)
211 fs_devices->latest_bdev = bdev;
212 if (device->devid == fs_devices->lowest_devid) {
213 fs_devices->lowest_bdev = bdev;
218 mutex_unlock(&uuid_mutex);
221 mutex_unlock(&uuid_mutex);
222 btrfs_close_devices(fs_devices);
226 int btrfs_scan_one_device(const char *path, int flags, void *holder,
227 struct btrfs_fs_devices **fs_devices_ret)
229 struct btrfs_super_block *disk_super;
230 struct block_device *bdev;
231 struct buffer_head *bh;
236 mutex_lock(&uuid_mutex);
238 bdev = open_bdev_excl(path, flags, holder);
245 ret = set_blocksize(bdev, 4096);
248 bh = __bread(bdev, BTRFS_SUPER_INFO_OFFSET / 4096, 4096);
253 disk_super = (struct btrfs_super_block *)bh->b_data;
254 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
255 sizeof(disk_super->magic))) {
259 devid = le64_to_cpu(disk_super->dev_item.devid);
260 transid = btrfs_super_generation(disk_super);
261 if (disk_super->label[0])
262 printk("device label %s ", disk_super->label);
264 /* FIXME, make a readl uuid parser */
265 printk("device fsid %llx-%llx ",
266 *(unsigned long long *)disk_super->fsid,
267 *(unsigned long long *)(disk_super->fsid + 8));
269 printk("devid %Lu transid %Lu %s\n", devid, transid, path);
270 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
275 close_bdev_excl(bdev);
277 mutex_unlock(&uuid_mutex);
282 * this uses a pretty simple search, the expectation is that it is
283 * called very infrequently and that a given device has a small number
286 static int find_free_dev_extent(struct btrfs_trans_handle *trans,
287 struct btrfs_device *device,
288 struct btrfs_path *path,
289 u64 num_bytes, u64 *start)
291 struct btrfs_key key;
292 struct btrfs_root *root = device->dev_root;
293 struct btrfs_dev_extent *dev_extent = NULL;
296 u64 search_start = 0;
297 u64 search_end = device->total_bytes;
301 struct extent_buffer *l;
306 /* FIXME use last free of some kind */
308 /* we don't want to overwrite the superblock on the drive,
309 * so we make sure to start at an offset of at least 1MB
311 search_start = max((u64)1024 * 1024, search_start);
313 if (root->fs_info->alloc_start + num_bytes <= device->total_bytes)
314 search_start = max(root->fs_info->alloc_start, search_start);
316 key.objectid = device->devid;
317 key.offset = search_start;
318 key.type = BTRFS_DEV_EXTENT_KEY;
319 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
322 ret = btrfs_previous_item(root, path, 0, key.type);
326 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
329 slot = path->slots[0];
330 if (slot >= btrfs_header_nritems(l)) {
331 ret = btrfs_next_leaf(root, path);
338 if (search_start >= search_end) {
342 *start = search_start;
346 *start = last_byte > search_start ?
347 last_byte : search_start;
348 if (search_end <= *start) {
354 btrfs_item_key_to_cpu(l, &key, slot);
356 if (key.objectid < device->devid)
359 if (key.objectid > device->devid)
362 if (key.offset >= search_start && key.offset > last_byte &&
364 if (last_byte < search_start)
365 last_byte = search_start;
366 hole_size = key.offset - last_byte;
367 if (key.offset > last_byte &&
368 hole_size >= num_bytes) {
373 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) {
378 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
379 last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent);
385 /* we have to make sure we didn't find an extent that has already
386 * been allocated by the map tree or the original allocation
388 btrfs_release_path(root, path);
389 BUG_ON(*start < search_start);
391 if (*start + num_bytes > search_end) {
395 /* check for pending inserts here */
399 btrfs_release_path(root, path);
403 int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
404 struct btrfs_device *device,
408 struct btrfs_path *path;
409 struct btrfs_root *root = device->dev_root;
410 struct btrfs_key key;
411 struct btrfs_key found_key;
412 struct extent_buffer *leaf = NULL;
413 struct btrfs_dev_extent *extent = NULL;
415 path = btrfs_alloc_path();
419 key.objectid = device->devid;
421 key.type = BTRFS_DEV_EXTENT_KEY;
423 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
425 ret = btrfs_previous_item(root, path, key.objectid,
426 BTRFS_DEV_EXTENT_KEY);
428 leaf = path->nodes[0];
429 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
430 extent = btrfs_item_ptr(leaf, path->slots[0],
431 struct btrfs_dev_extent);
432 BUG_ON(found_key.offset > start || found_key.offset +
433 btrfs_dev_extent_length(leaf, extent) < start);
435 } else if (ret == 0) {
436 leaf = path->nodes[0];
437 extent = btrfs_item_ptr(leaf, path->slots[0],
438 struct btrfs_dev_extent);
442 device->bytes_used -= btrfs_dev_extent_length(leaf, extent);
443 ret = btrfs_del_item(trans, root, path);
446 btrfs_free_path(path);
450 int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
451 struct btrfs_device *device,
452 u64 chunk_tree, u64 chunk_objectid,
454 u64 num_bytes, u64 *start)
457 struct btrfs_path *path;
458 struct btrfs_root *root = device->dev_root;
459 struct btrfs_dev_extent *extent;
460 struct extent_buffer *leaf;
461 struct btrfs_key key;
463 path = btrfs_alloc_path();
467 ret = find_free_dev_extent(trans, device, path, num_bytes, start);
472 key.objectid = device->devid;
474 key.type = BTRFS_DEV_EXTENT_KEY;
475 ret = btrfs_insert_empty_item(trans, root, path, &key,
479 leaf = path->nodes[0];
480 extent = btrfs_item_ptr(leaf, path->slots[0],
481 struct btrfs_dev_extent);
482 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
483 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
484 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
486 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
487 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
490 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
491 btrfs_mark_buffer_dirty(leaf);
493 btrfs_free_path(path);
497 static int find_next_chunk(struct btrfs_root *root, u64 objectid, u64 *offset)
499 struct btrfs_path *path;
501 struct btrfs_key key;
502 struct btrfs_chunk *chunk;
503 struct btrfs_key found_key;
505 path = btrfs_alloc_path();
508 key.objectid = objectid;
509 key.offset = (u64)-1;
510 key.type = BTRFS_CHUNK_ITEM_KEY;
512 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
518 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
522 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
524 if (found_key.objectid != objectid)
527 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
529 *offset = found_key.offset +
530 btrfs_chunk_length(path->nodes[0], chunk);
535 btrfs_free_path(path);
539 static int find_next_devid(struct btrfs_root *root, struct btrfs_path *path,
543 struct btrfs_key key;
544 struct btrfs_key found_key;
546 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
547 key.type = BTRFS_DEV_ITEM_KEY;
548 key.offset = (u64)-1;
550 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
556 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
561 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
563 *objectid = found_key.offset + 1;
567 btrfs_release_path(root, path);
572 * the device information is stored in the chunk root
573 * the btrfs_device struct should be fully filled in
575 int btrfs_add_device(struct btrfs_trans_handle *trans,
576 struct btrfs_root *root,
577 struct btrfs_device *device)
580 struct btrfs_path *path;
581 struct btrfs_dev_item *dev_item;
582 struct extent_buffer *leaf;
583 struct btrfs_key key;
587 root = root->fs_info->chunk_root;
589 path = btrfs_alloc_path();
593 ret = find_next_devid(root, path, &free_devid);
597 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
598 key.type = BTRFS_DEV_ITEM_KEY;
599 key.offset = free_devid;
601 ret = btrfs_insert_empty_item(trans, root, path, &key,
606 leaf = path->nodes[0];
607 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
609 device->devid = free_devid;
610 btrfs_set_device_id(leaf, dev_item, device->devid);
611 btrfs_set_device_type(leaf, dev_item, device->type);
612 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
613 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
614 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
615 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
616 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
617 btrfs_set_device_group(leaf, dev_item, 0);
618 btrfs_set_device_seek_speed(leaf, dev_item, 0);
619 btrfs_set_device_bandwidth(leaf, dev_item, 0);
621 ptr = (unsigned long)btrfs_device_uuid(dev_item);
622 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
623 btrfs_mark_buffer_dirty(leaf);
627 btrfs_free_path(path);
631 static int btrfs_rm_dev_item(struct btrfs_root *root,
632 struct btrfs_device *device)
635 struct btrfs_path *path;
636 struct block_device *bdev = device->bdev;
637 struct btrfs_device *next_dev;
638 struct btrfs_key key;
640 struct btrfs_fs_devices *fs_devices;
641 struct btrfs_trans_handle *trans;
643 root = root->fs_info->chunk_root;
645 path = btrfs_alloc_path();
649 trans = btrfs_start_transaction(root, 1);
650 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
651 key.type = BTRFS_DEV_ITEM_KEY;
652 key.offset = device->devid;
654 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
663 ret = btrfs_del_item(trans, root, path);
668 * at this point, the device is zero sized. We want to
669 * remove it from the devices list and zero out the old super
671 list_del_init(&device->dev_list);
672 list_del_init(&device->dev_alloc_list);
673 fs_devices = root->fs_info->fs_devices;
675 next_dev = list_entry(fs_devices->devices.next, struct btrfs_device,
677 if (bdev == fs_devices->lowest_bdev)
678 fs_devices->lowest_bdev = next_dev->bdev;
679 if (bdev == root->fs_info->sb->s_bdev)
680 root->fs_info->sb->s_bdev = next_dev->bdev;
681 if (bdev == fs_devices->latest_bdev)
682 fs_devices->latest_bdev = next_dev->bdev;
684 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
685 btrfs_set_super_total_bytes(&root->fs_info->super_copy,
686 total_bytes - device->total_bytes);
688 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
689 btrfs_set_super_num_devices(&root->fs_info->super_copy,
692 btrfs_free_path(path);
693 btrfs_commit_transaction(trans, root);
697 int btrfs_rm_device(struct btrfs_root *root, char *device_path)
699 struct btrfs_device *device;
700 struct block_device *bdev;
701 struct buffer_head *bh;
702 struct btrfs_super_block *disk_super;
707 mutex_lock(&root->fs_info->fs_mutex);
708 mutex_lock(&uuid_mutex);
710 all_avail = root->fs_info->avail_data_alloc_bits |
711 root->fs_info->avail_system_alloc_bits |
712 root->fs_info->avail_metadata_alloc_bits;
714 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
715 root->fs_info->fs_devices->num_devices <= 4) {
716 printk("btrfs: unable to go below four devices on raid10\n");
721 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
722 root->fs_info->fs_devices->num_devices <= 2) {
723 printk("btrfs: unable to go below two devices on raid1\n");
728 bdev = open_bdev_excl(device_path, 0, root->fs_info->bdev_holder);
734 bh = __bread(bdev, BTRFS_SUPER_INFO_OFFSET / 4096, 4096);
739 disk_super = (struct btrfs_super_block *)bh->b_data;
740 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
741 sizeof(disk_super->magic))) {
745 if (memcmp(disk_super->fsid, root->fs_info->fsid, BTRFS_FSID_SIZE)) {
749 devid = le64_to_cpu(disk_super->dev_item.devid);
750 device = btrfs_find_device(root, devid, NULL);
756 root->fs_info->fs_devices->num_devices--;
758 ret = btrfs_shrink_device(device, 0);
763 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
767 /* make sure this device isn't detected as part of the FS anymore */
768 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
769 set_buffer_dirty(bh);
770 sync_dirty_buffer(bh);
774 /* one close for the device struct or super_block */
775 close_bdev_excl(device->bdev);
777 /* one close for us */
778 close_bdev_excl(device->bdev);
788 close_bdev_excl(bdev);
790 mutex_unlock(&uuid_mutex);
791 mutex_unlock(&root->fs_info->fs_mutex);
795 int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
797 struct btrfs_trans_handle *trans;
798 struct btrfs_device *device;
799 struct block_device *bdev;
800 struct list_head *cur;
801 struct list_head *devices;
806 bdev = open_bdev_excl(device_path, 0, root->fs_info->bdev_holder);
810 mutex_lock(&root->fs_info->fs_mutex);
811 trans = btrfs_start_transaction(root, 1);
812 devices = &root->fs_info->fs_devices->devices;
813 list_for_each(cur, devices) {
814 device = list_entry(cur, struct btrfs_device, dev_list);
815 if (device->bdev == bdev) {
821 device = kzalloc(sizeof(*device), GFP_NOFS);
823 /* we can safely leave the fs_devices entry around */
828 device->barriers = 1;
829 generate_random_uuid(device->uuid);
830 spin_lock_init(&device->io_lock);
831 device->name = kstrdup(device_path, GFP_NOFS);
836 device->io_width = root->sectorsize;
837 device->io_align = root->sectorsize;
838 device->sector_size = root->sectorsize;
839 device->total_bytes = i_size_read(bdev->bd_inode);
840 device->dev_root = root->fs_info->dev_root;
843 ret = btrfs_add_device(trans, root, device);
847 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
848 btrfs_set_super_total_bytes(&root->fs_info->super_copy,
849 total_bytes + device->total_bytes);
851 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
852 btrfs_set_super_num_devices(&root->fs_info->super_copy,
855 list_add(&device->dev_list, &root->fs_info->fs_devices->devices);
856 list_add(&device->dev_alloc_list,
857 &root->fs_info->fs_devices->alloc_list);
858 root->fs_info->fs_devices->num_devices++;
860 btrfs_end_transaction(trans, root);
861 mutex_unlock(&root->fs_info->fs_mutex);
865 close_bdev_excl(bdev);
869 int btrfs_update_device(struct btrfs_trans_handle *trans,
870 struct btrfs_device *device)
873 struct btrfs_path *path;
874 struct btrfs_root *root;
875 struct btrfs_dev_item *dev_item;
876 struct extent_buffer *leaf;
877 struct btrfs_key key;
879 root = device->dev_root->fs_info->chunk_root;
881 path = btrfs_alloc_path();
885 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
886 key.type = BTRFS_DEV_ITEM_KEY;
887 key.offset = device->devid;
889 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
898 leaf = path->nodes[0];
899 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
901 btrfs_set_device_id(leaf, dev_item, device->devid);
902 btrfs_set_device_type(leaf, dev_item, device->type);
903 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
904 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
905 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
906 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
907 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
908 btrfs_mark_buffer_dirty(leaf);
911 btrfs_free_path(path);
915 int btrfs_grow_device(struct btrfs_trans_handle *trans,
916 struct btrfs_device *device, u64 new_size)
918 struct btrfs_super_block *super_copy =
919 &device->dev_root->fs_info->super_copy;
920 u64 old_total = btrfs_super_total_bytes(super_copy);
921 u64 diff = new_size - device->total_bytes;
923 btrfs_set_super_total_bytes(super_copy, old_total + diff);
924 return btrfs_update_device(trans, device);
927 static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
928 struct btrfs_root *root,
929 u64 chunk_tree, u64 chunk_objectid,
933 struct btrfs_path *path;
934 struct btrfs_key key;
936 root = root->fs_info->chunk_root;
937 path = btrfs_alloc_path();
941 key.objectid = chunk_objectid;
942 key.offset = chunk_offset;
943 key.type = BTRFS_CHUNK_ITEM_KEY;
945 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
948 ret = btrfs_del_item(trans, root, path);
951 btrfs_free_path(path);
955 int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
958 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
959 struct btrfs_disk_key *disk_key;
960 struct btrfs_chunk *chunk;
967 struct btrfs_key key;
969 array_size = btrfs_super_sys_array_size(super_copy);
971 ptr = super_copy->sys_chunk_array;
974 while (cur < array_size) {
975 disk_key = (struct btrfs_disk_key *)ptr;
976 btrfs_disk_key_to_cpu(&key, disk_key);
978 len = sizeof(*disk_key);
980 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
981 chunk = (struct btrfs_chunk *)(ptr + len);
982 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
983 len += btrfs_chunk_item_size(num_stripes);
988 if (key.objectid == chunk_objectid &&
989 key.offset == chunk_offset) {
990 memmove(ptr, ptr + len, array_size - (cur + len));
992 btrfs_set_super_sys_array_size(super_copy, array_size);
1002 int btrfs_relocate_chunk(struct btrfs_root *root,
1003 u64 chunk_tree, u64 chunk_objectid,
1006 struct extent_map_tree *em_tree;
1007 struct btrfs_root *extent_root;
1008 struct btrfs_trans_handle *trans;
1009 struct extent_map *em;
1010 struct map_lookup *map;
1014 root = root->fs_info->chunk_root;
1015 extent_root = root->fs_info->extent_root;
1016 em_tree = &root->fs_info->mapping_tree.map_tree;
1018 /* step one, relocate all the extents inside this chunk */
1019 ret = btrfs_shrink_extent_tree(extent_root, chunk_offset);
1022 trans = btrfs_start_transaction(root, 1);
1026 * step two, delete the device extents and the
1027 * chunk tree entries
1029 spin_lock(&em_tree->lock);
1030 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
1031 spin_unlock(&em_tree->lock);
1033 BUG_ON(em->start > chunk_offset ||
1034 em->start + em->len < chunk_offset);
1035 map = (struct map_lookup *)em->bdev;
1037 for (i = 0; i < map->num_stripes; i++) {
1038 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1039 map->stripes[i].physical);
1042 ret = btrfs_update_device(trans, map->stripes[i].dev);
1045 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1050 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1051 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1055 spin_lock(&em_tree->lock);
1056 remove_extent_mapping(em_tree, em);
1060 /* once for the tree */
1061 free_extent_map(em);
1062 spin_unlock(&em_tree->lock);
1065 free_extent_map(em);
1067 btrfs_end_transaction(trans, root);
1071 static u64 div_factor(u64 num, int factor)
1081 int btrfs_balance(struct btrfs_root *dev_root)
1084 struct list_head *cur;
1085 struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
1086 struct btrfs_device *device;
1089 struct btrfs_path *path;
1090 struct btrfs_key key;
1091 struct btrfs_chunk *chunk;
1092 struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
1093 struct btrfs_trans_handle *trans;
1094 struct btrfs_key found_key;
1097 dev_root = dev_root->fs_info->dev_root;
1099 mutex_lock(&dev_root->fs_info->fs_mutex);
1100 /* step one make some room on all the devices */
1101 list_for_each(cur, devices) {
1102 device = list_entry(cur, struct btrfs_device, dev_list);
1103 old_size = device->total_bytes;
1104 size_to_free = div_factor(old_size, 1);
1105 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
1106 if (device->total_bytes - device->bytes_used > size_to_free)
1109 ret = btrfs_shrink_device(device, old_size - size_to_free);
1112 trans = btrfs_start_transaction(dev_root, 1);
1115 ret = btrfs_grow_device(trans, device, old_size);
1118 btrfs_end_transaction(trans, dev_root);
1121 /* step two, relocate all the chunks */
1122 path = btrfs_alloc_path();
1125 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1126 key.offset = (u64)-1;
1127 key.type = BTRFS_CHUNK_ITEM_KEY;
1130 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1135 * this shouldn't happen, it means the last relocate
1141 ret = btrfs_previous_item(chunk_root, path, 0,
1142 BTRFS_CHUNK_ITEM_KEY);
1146 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1148 if (found_key.objectid != key.objectid)
1150 chunk = btrfs_item_ptr(path->nodes[0],
1152 struct btrfs_chunk);
1153 key.offset = found_key.offset;
1154 /* chunk zero is special */
1155 if (key.offset == 0)
1158 ret = btrfs_relocate_chunk(chunk_root,
1159 chunk_root->root_key.objectid,
1163 btrfs_release_path(chunk_root, path);
1167 btrfs_free_path(path);
1168 mutex_unlock(&dev_root->fs_info->fs_mutex);
1173 * shrinking a device means finding all of the device extents past
1174 * the new size, and then following the back refs to the chunks.
1175 * The chunk relocation code actually frees the device extent
1177 int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
1179 struct btrfs_trans_handle *trans;
1180 struct btrfs_root *root = device->dev_root;
1181 struct btrfs_dev_extent *dev_extent = NULL;
1182 struct btrfs_path *path;
1189 struct extent_buffer *l;
1190 struct btrfs_key key;
1191 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1192 u64 old_total = btrfs_super_total_bytes(super_copy);
1193 u64 diff = device->total_bytes - new_size;
1196 path = btrfs_alloc_path();
1200 trans = btrfs_start_transaction(root, 1);
1208 device->total_bytes = new_size;
1209 ret = btrfs_update_device(trans, device);
1211 btrfs_end_transaction(trans, root);
1214 WARN_ON(diff > old_total);
1215 btrfs_set_super_total_bytes(super_copy, old_total - diff);
1216 btrfs_end_transaction(trans, root);
1218 key.objectid = device->devid;
1219 key.offset = (u64)-1;
1220 key.type = BTRFS_DEV_EXTENT_KEY;
1223 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1227 ret = btrfs_previous_item(root, path, 0, key.type);
1236 slot = path->slots[0];
1237 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
1239 if (key.objectid != device->devid)
1242 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
1243 length = btrfs_dev_extent_length(l, dev_extent);
1245 if (key.offset + length <= new_size)
1248 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
1249 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
1250 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
1251 btrfs_release_path(root, path);
1253 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
1260 btrfs_free_path(path);
1264 int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
1265 struct btrfs_root *root,
1266 struct btrfs_key *key,
1267 struct btrfs_chunk *chunk, int item_size)
1269 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1270 struct btrfs_disk_key disk_key;
1274 array_size = btrfs_super_sys_array_size(super_copy);
1275 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
1278 ptr = super_copy->sys_chunk_array + array_size;
1279 btrfs_cpu_key_to_disk(&disk_key, key);
1280 memcpy(ptr, &disk_key, sizeof(disk_key));
1281 ptr += sizeof(disk_key);
1282 memcpy(ptr, chunk, item_size);
1283 item_size += sizeof(disk_key);
1284 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
1288 static u64 chunk_bytes_by_type(u64 type, u64 calc_size, int num_stripes,
1291 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
1293 else if (type & BTRFS_BLOCK_GROUP_RAID10)
1294 return calc_size * (num_stripes / sub_stripes);
1296 return calc_size * num_stripes;
1300 int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
1301 struct btrfs_root *extent_root, u64 *start,
1302 u64 *num_bytes, u64 type)
1305 struct btrfs_fs_info *info = extent_root->fs_info;
1306 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
1307 struct btrfs_path *path;
1308 struct btrfs_stripe *stripes;
1309 struct btrfs_device *device = NULL;
1310 struct btrfs_chunk *chunk;
1311 struct list_head private_devs;
1312 struct list_head *dev_list;
1313 struct list_head *cur;
1314 struct extent_map_tree *em_tree;
1315 struct map_lookup *map;
1316 struct extent_map *em;
1317 int min_stripe_size = 1 * 1024 * 1024;
1319 u64 calc_size = 1024 * 1024 * 1024;
1320 u64 max_chunk_size = calc_size;
1325 int num_stripes = 1;
1326 int min_stripes = 1;
1327 int sub_stripes = 0;
1331 int stripe_len = 64 * 1024;
1332 struct btrfs_key key;
1334 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
1335 (type & BTRFS_BLOCK_GROUP_DUP)) {
1337 type &= ~BTRFS_BLOCK_GROUP_DUP;
1339 dev_list = &extent_root->fs_info->fs_devices->alloc_list;
1340 if (list_empty(dev_list))
1343 if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
1344 num_stripes = btrfs_super_num_devices(&info->super_copy);
1347 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
1351 if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
1352 num_stripes = min_t(u64, 2,
1353 btrfs_super_num_devices(&info->super_copy));
1354 if (num_stripes < 2)
1358 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
1359 num_stripes = btrfs_super_num_devices(&info->super_copy);
1360 if (num_stripes < 4)
1362 num_stripes &= ~(u32)1;
1367 if (type & BTRFS_BLOCK_GROUP_DATA) {
1368 max_chunk_size = 10 * calc_size;
1369 min_stripe_size = 64 * 1024 * 1024;
1370 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
1371 max_chunk_size = 4 * calc_size;
1372 min_stripe_size = 32 * 1024 * 1024;
1373 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
1374 calc_size = 8 * 1024 * 1024;
1375 max_chunk_size = calc_size * 2;
1376 min_stripe_size = 1 * 1024 * 1024;
1379 path = btrfs_alloc_path();
1383 /* we don't want a chunk larger than 10% of the FS */
1384 percent_max = div_factor(btrfs_super_total_bytes(&info->super_copy), 1);
1385 max_chunk_size = min(percent_max, max_chunk_size);
1388 if (calc_size * num_stripes > max_chunk_size) {
1389 calc_size = max_chunk_size;
1390 do_div(calc_size, num_stripes);
1391 do_div(calc_size, stripe_len);
1392 calc_size *= stripe_len;
1394 /* we don't want tiny stripes */
1395 calc_size = max_t(u64, min_stripe_size, calc_size);
1397 do_div(calc_size, stripe_len);
1398 calc_size *= stripe_len;
1400 INIT_LIST_HEAD(&private_devs);
1401 cur = dev_list->next;
1404 if (type & BTRFS_BLOCK_GROUP_DUP)
1405 min_free = calc_size * 2;
1407 min_free = calc_size;
1409 /* we add 1MB because we never use the first 1MB of the device */
1410 min_free += 1024 * 1024;
1412 /* build a private list of devices we will allocate from */
1413 while(index < num_stripes) {
1414 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
1416 avail = device->total_bytes - device->bytes_used;
1419 if (avail >= min_free) {
1420 u64 ignored_start = 0;
1421 ret = find_free_dev_extent(trans, device, path,
1425 list_move_tail(&device->dev_alloc_list,
1428 if (type & BTRFS_BLOCK_GROUP_DUP)
1431 } else if (avail > max_avail)
1433 if (cur == dev_list)
1436 if (index < num_stripes) {
1437 list_splice(&private_devs, dev_list);
1438 if (index >= min_stripes) {
1439 num_stripes = index;
1440 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
1441 num_stripes /= sub_stripes;
1442 num_stripes *= sub_stripes;
1447 if (!looped && max_avail > 0) {
1449 calc_size = max_avail;
1452 btrfs_free_path(path);
1455 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1456 key.type = BTRFS_CHUNK_ITEM_KEY;
1457 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
1460 btrfs_free_path(path);
1464 chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS);
1466 btrfs_free_path(path);
1470 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
1473 btrfs_free_path(path);
1476 btrfs_free_path(path);
1479 stripes = &chunk->stripe;
1480 *num_bytes = chunk_bytes_by_type(type, calc_size,
1481 num_stripes, sub_stripes);
1484 while(index < num_stripes) {
1485 struct btrfs_stripe *stripe;
1486 BUG_ON(list_empty(&private_devs));
1487 cur = private_devs.next;
1488 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
1490 /* loop over this device again if we're doing a dup group */
1491 if (!(type & BTRFS_BLOCK_GROUP_DUP) ||
1492 (index == num_stripes - 1))
1493 list_move_tail(&device->dev_alloc_list, dev_list);
1495 ret = btrfs_alloc_dev_extent(trans, device,
1496 info->chunk_root->root_key.objectid,
1497 BTRFS_FIRST_CHUNK_TREE_OBJECTID, key.offset,
1498 calc_size, &dev_offset);
1500 device->bytes_used += calc_size;
1501 ret = btrfs_update_device(trans, device);
1504 map->stripes[index].dev = device;
1505 map->stripes[index].physical = dev_offset;
1506 stripe = stripes + index;
1507 btrfs_set_stack_stripe_devid(stripe, device->devid);
1508 btrfs_set_stack_stripe_offset(stripe, dev_offset);
1509 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
1510 physical = dev_offset;
1513 BUG_ON(!list_empty(&private_devs));
1515 /* key was set above */
1516 btrfs_set_stack_chunk_length(chunk, *num_bytes);
1517 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
1518 btrfs_set_stack_chunk_stripe_len(chunk, stripe_len);
1519 btrfs_set_stack_chunk_type(chunk, type);
1520 btrfs_set_stack_chunk_num_stripes(chunk, num_stripes);
1521 btrfs_set_stack_chunk_io_align(chunk, stripe_len);
1522 btrfs_set_stack_chunk_io_width(chunk, stripe_len);
1523 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
1524 btrfs_set_stack_chunk_sub_stripes(chunk, sub_stripes);
1525 map->sector_size = extent_root->sectorsize;
1526 map->stripe_len = stripe_len;
1527 map->io_align = stripe_len;
1528 map->io_width = stripe_len;
1530 map->num_stripes = num_stripes;
1531 map->sub_stripes = sub_stripes;
1533 ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
1534 btrfs_chunk_item_size(num_stripes));
1536 *start = key.offset;;
1538 em = alloc_extent_map(GFP_NOFS);
1541 em->bdev = (struct block_device *)map;
1542 em->start = key.offset;
1543 em->len = *num_bytes;
1544 em->block_start = 0;
1546 if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
1547 ret = btrfs_add_system_chunk(trans, chunk_root, &key,
1548 chunk, btrfs_chunk_item_size(num_stripes));
1553 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
1554 spin_lock(&em_tree->lock);
1555 ret = add_extent_mapping(em_tree, em);
1556 spin_unlock(&em_tree->lock);
1558 free_extent_map(em);
1562 void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
1564 extent_map_tree_init(&tree->map_tree, GFP_NOFS);
1567 void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
1569 struct extent_map *em;
1572 spin_lock(&tree->map_tree.lock);
1573 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
1575 remove_extent_mapping(&tree->map_tree, em);
1576 spin_unlock(&tree->map_tree.lock);
1581 free_extent_map(em);
1582 /* once for the tree */
1583 free_extent_map(em);
1587 int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
1589 struct extent_map *em;
1590 struct map_lookup *map;
1591 struct extent_map_tree *em_tree = &map_tree->map_tree;
1594 spin_lock(&em_tree->lock);
1595 em = lookup_extent_mapping(em_tree, logical, len);
1596 spin_unlock(&em_tree->lock);
1599 BUG_ON(em->start > logical || em->start + em->len < logical);
1600 map = (struct map_lookup *)em->bdev;
1601 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
1602 ret = map->num_stripes;
1603 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
1604 ret = map->sub_stripes;
1607 free_extent_map(em);
1611 static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
1612 u64 logical, u64 *length,
1613 struct btrfs_multi_bio **multi_ret,
1614 int mirror_num, struct page *unplug_page)
1616 struct extent_map *em;
1617 struct map_lookup *map;
1618 struct extent_map_tree *em_tree = &map_tree->map_tree;
1622 int stripes_allocated = 8;
1623 int stripes_required = 1;
1628 struct btrfs_multi_bio *multi = NULL;
1630 if (multi_ret && !(rw & (1 << BIO_RW))) {
1631 stripes_allocated = 1;
1635 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
1640 atomic_set(&multi->error, 0);
1643 spin_lock(&em_tree->lock);
1644 em = lookup_extent_mapping(em_tree, logical, *length);
1645 spin_unlock(&em_tree->lock);
1647 if (!em && unplug_page)
1651 printk("unable to find logical %Lu len %Lu\n", logical, *length);
1655 BUG_ON(em->start > logical || em->start + em->len < logical);
1656 map = (struct map_lookup *)em->bdev;
1657 offset = logical - em->start;
1659 if (mirror_num > map->num_stripes)
1662 /* if our multi bio struct is too small, back off and try again */
1663 if (rw & (1 << BIO_RW)) {
1664 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
1665 BTRFS_BLOCK_GROUP_DUP)) {
1666 stripes_required = map->num_stripes;
1668 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1669 stripes_required = map->sub_stripes;
1673 if (multi_ret && rw == WRITE &&
1674 stripes_allocated < stripes_required) {
1675 stripes_allocated = map->num_stripes;
1676 free_extent_map(em);
1682 * stripe_nr counts the total number of stripes we have to stride
1683 * to get to this block
1685 do_div(stripe_nr, map->stripe_len);
1687 stripe_offset = stripe_nr * map->stripe_len;
1688 BUG_ON(offset < stripe_offset);
1690 /* stripe_offset is the offset of this block in its stripe*/
1691 stripe_offset = offset - stripe_offset;
1693 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
1694 BTRFS_BLOCK_GROUP_RAID10 |
1695 BTRFS_BLOCK_GROUP_DUP)) {
1696 /* we limit the length of each bio to what fits in a stripe */
1697 *length = min_t(u64, em->len - offset,
1698 map->stripe_len - stripe_offset);
1700 *length = em->len - offset;
1703 if (!multi_ret && !unplug_page)
1708 if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
1709 if (unplug_page || (rw & (1 << BIO_RW)))
1710 num_stripes = map->num_stripes;
1711 else if (mirror_num)
1712 stripe_index = mirror_num - 1;
1714 stripe_index = current->pid % map->num_stripes;
1716 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
1717 if (rw & (1 << BIO_RW))
1718 num_stripes = map->num_stripes;
1719 else if (mirror_num)
1720 stripe_index = mirror_num - 1;
1722 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1723 int factor = map->num_stripes / map->sub_stripes;
1725 stripe_index = do_div(stripe_nr, factor);
1726 stripe_index *= map->sub_stripes;
1728 if (unplug_page || (rw & (1 << BIO_RW)))
1729 num_stripes = map->sub_stripes;
1730 else if (mirror_num)
1731 stripe_index += mirror_num - 1;
1733 stripe_index += current->pid % map->sub_stripes;
1736 * after this do_div call, stripe_nr is the number of stripes
1737 * on this device we have to walk to find the data, and
1738 * stripe_index is the number of our device in the stripe array
1740 stripe_index = do_div(stripe_nr, map->num_stripes);
1742 BUG_ON(stripe_index >= map->num_stripes);
1744 for (i = 0; i < num_stripes; i++) {
1746 struct btrfs_device *device;
1747 struct backing_dev_info *bdi;
1749 device = map->stripes[stripe_index].dev;
1750 bdi = blk_get_backing_dev_info(device->bdev);
1751 if (bdi->unplug_io_fn) {
1752 bdi->unplug_io_fn(bdi, unplug_page);
1755 multi->stripes[i].physical =
1756 map->stripes[stripe_index].physical +
1757 stripe_offset + stripe_nr * map->stripe_len;
1758 multi->stripes[i].dev = map->stripes[stripe_index].dev;
1764 multi->num_stripes = num_stripes;
1765 multi->max_errors = max_errors;
1768 free_extent_map(em);
1772 int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
1773 u64 logical, u64 *length,
1774 struct btrfs_multi_bio **multi_ret, int mirror_num)
1776 return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
1780 int btrfs_unplug_page(struct btrfs_mapping_tree *map_tree,
1781 u64 logical, struct page *page)
1783 u64 length = PAGE_CACHE_SIZE;
1784 return __btrfs_map_block(map_tree, READ, logical, &length,
1789 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
1790 static void end_bio_multi_stripe(struct bio *bio, int err)
1792 static int end_bio_multi_stripe(struct bio *bio,
1793 unsigned int bytes_done, int err)
1796 struct btrfs_multi_bio *multi = bio->bi_private;
1798 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1803 atomic_inc(&multi->error);
1805 if (atomic_dec_and_test(&multi->stripes_pending)) {
1806 bio->bi_private = multi->private;
1807 bio->bi_end_io = multi->end_io;
1809 /* only send an error to the higher layers if it is
1810 * beyond the tolerance of the multi-bio
1812 if (atomic_read(&multi->error) > multi->max_errors)
1818 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1819 bio_endio(bio, bio->bi_size, err);
1821 bio_endio(bio, err);
1826 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1831 int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
1834 struct btrfs_mapping_tree *map_tree;
1835 struct btrfs_device *dev;
1836 struct bio *first_bio = bio;
1837 u64 logical = bio->bi_sector << 9;
1840 struct btrfs_multi_bio *multi = NULL;
1845 length = bio->bi_size;
1846 map_tree = &root->fs_info->mapping_tree;
1847 map_length = length;
1849 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
1853 total_devs = multi->num_stripes;
1854 if (map_length < length) {
1855 printk("mapping failed logical %Lu bio len %Lu "
1856 "len %Lu\n", logical, length, map_length);
1859 multi->end_io = first_bio->bi_end_io;
1860 multi->private = first_bio->bi_private;
1861 atomic_set(&multi->stripes_pending, multi->num_stripes);
1863 while(dev_nr < total_devs) {
1864 if (total_devs > 1) {
1865 if (dev_nr < total_devs - 1) {
1866 bio = bio_clone(first_bio, GFP_NOFS);
1871 bio->bi_private = multi;
1872 bio->bi_end_io = end_bio_multi_stripe;
1874 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
1875 dev = multi->stripes[dev_nr].dev;
1877 bio->bi_bdev = dev->bdev;
1878 spin_lock(&dev->io_lock);
1880 spin_unlock(&dev->io_lock);
1881 submit_bio(rw, bio);
1884 if (total_devs == 1)
1889 struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
1892 struct list_head *head = &root->fs_info->fs_devices->devices;
1894 return __find_device(head, devid, uuid);
1897 static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
1898 struct extent_buffer *leaf,
1899 struct btrfs_chunk *chunk)
1901 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
1902 struct map_lookup *map;
1903 struct extent_map *em;
1907 u8 uuid[BTRFS_UUID_SIZE];
1912 logical = key->offset;
1913 length = btrfs_chunk_length(leaf, chunk);
1915 spin_lock(&map_tree->map_tree.lock);
1916 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
1917 spin_unlock(&map_tree->map_tree.lock);
1919 /* already mapped? */
1920 if (em && em->start <= logical && em->start + em->len > logical) {
1921 free_extent_map(em);
1924 free_extent_map(em);
1927 map = kzalloc(sizeof(*map), GFP_NOFS);
1931 em = alloc_extent_map(GFP_NOFS);
1934 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
1935 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
1937 free_extent_map(em);
1941 em->bdev = (struct block_device *)map;
1942 em->start = logical;
1944 em->block_start = 0;
1946 map->num_stripes = num_stripes;
1947 map->io_width = btrfs_chunk_io_width(leaf, chunk);
1948 map->io_align = btrfs_chunk_io_align(leaf, chunk);
1949 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
1950 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
1951 map->type = btrfs_chunk_type(leaf, chunk);
1952 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
1953 for (i = 0; i < num_stripes; i++) {
1954 map->stripes[i].physical =
1955 btrfs_stripe_offset_nr(leaf, chunk, i);
1956 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
1957 read_extent_buffer(leaf, uuid, (unsigned long)
1958 btrfs_stripe_dev_uuid_nr(chunk, i),
1960 map->stripes[i].dev = btrfs_find_device(root, devid, uuid);
1961 if (!map->stripes[i].dev) {
1963 free_extent_map(em);
1968 spin_lock(&map_tree->map_tree.lock);
1969 ret = add_extent_mapping(&map_tree->map_tree, em);
1970 spin_unlock(&map_tree->map_tree.lock);
1972 free_extent_map(em);
1977 static int fill_device_from_item(struct extent_buffer *leaf,
1978 struct btrfs_dev_item *dev_item,
1979 struct btrfs_device *device)
1983 device->devid = btrfs_device_id(leaf, dev_item);
1984 device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
1985 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
1986 device->type = btrfs_device_type(leaf, dev_item);
1987 device->io_align = btrfs_device_io_align(leaf, dev_item);
1988 device->io_width = btrfs_device_io_width(leaf, dev_item);
1989 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
1991 ptr = (unsigned long)btrfs_device_uuid(dev_item);
1992 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1997 static int read_one_dev(struct btrfs_root *root,
1998 struct extent_buffer *leaf,
1999 struct btrfs_dev_item *dev_item)
2001 struct btrfs_device *device;
2004 u8 dev_uuid[BTRFS_UUID_SIZE];
2006 devid = btrfs_device_id(leaf, dev_item);
2007 read_extent_buffer(leaf, dev_uuid,
2008 (unsigned long)btrfs_device_uuid(dev_item),
2010 device = btrfs_find_device(root, devid, dev_uuid);
2012 printk("warning devid %Lu not found already\n", devid);
2013 device = kzalloc(sizeof(*device), GFP_NOFS);
2016 list_add(&device->dev_list,
2017 &root->fs_info->fs_devices->devices);
2018 list_add(&device->dev_alloc_list,
2019 &root->fs_info->fs_devices->alloc_list);
2020 device->barriers = 1;
2021 spin_lock_init(&device->io_lock);
2024 fill_device_from_item(leaf, dev_item, device);
2025 device->dev_root = root->fs_info->dev_root;
2028 ret = btrfs_open_device(device);
2036 int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
2038 struct btrfs_dev_item *dev_item;
2040 dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
2042 return read_one_dev(root, buf, dev_item);
2045 int btrfs_read_sys_array(struct btrfs_root *root)
2047 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2048 struct extent_buffer *sb;
2049 struct btrfs_disk_key *disk_key;
2050 struct btrfs_chunk *chunk;
2052 unsigned long sb_ptr;
2058 struct btrfs_key key;
2060 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
2061 BTRFS_SUPER_INFO_SIZE);
2064 btrfs_set_buffer_uptodate(sb);
2065 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
2066 array_size = btrfs_super_sys_array_size(super_copy);
2068 ptr = super_copy->sys_chunk_array;
2069 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
2072 while (cur < array_size) {
2073 disk_key = (struct btrfs_disk_key *)ptr;
2074 btrfs_disk_key_to_cpu(&key, disk_key);
2076 len = sizeof(*disk_key); ptr += len;
2080 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2081 chunk = (struct btrfs_chunk *)sb_ptr;
2082 ret = read_one_chunk(root, &key, sb, chunk);
2085 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
2086 len = btrfs_chunk_item_size(num_stripes);
2095 free_extent_buffer(sb);
2099 int btrfs_read_chunk_tree(struct btrfs_root *root)
2101 struct btrfs_path *path;
2102 struct extent_buffer *leaf;
2103 struct btrfs_key key;
2104 struct btrfs_key found_key;
2108 root = root->fs_info->chunk_root;
2110 path = btrfs_alloc_path();
2114 /* first we search for all of the device items, and then we
2115 * read in all of the chunk items. This way we can create chunk
2116 * mappings that reference all of the devices that are afound
2118 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2122 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2124 leaf = path->nodes[0];
2125 slot = path->slots[0];
2126 if (slot >= btrfs_header_nritems(leaf)) {
2127 ret = btrfs_next_leaf(root, path);
2134 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2135 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
2136 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
2138 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
2139 struct btrfs_dev_item *dev_item;
2140 dev_item = btrfs_item_ptr(leaf, slot,
2141 struct btrfs_dev_item);
2142 ret = read_one_dev(root, leaf, dev_item);
2145 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
2146 struct btrfs_chunk *chunk;
2147 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
2148 ret = read_one_chunk(root, &found_key, leaf, chunk);
2152 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
2154 btrfs_release_path(root, path);
2158 btrfs_free_path(path);