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"
30 #include "async-thread.h"
40 struct btrfs_bio_stripe stripes[];
43 #define map_lookup_size(n) (sizeof(struct map_lookup) + \
44 (sizeof(struct btrfs_bio_stripe) * (n)))
46 static DEFINE_MUTEX(uuid_mutex);
47 static LIST_HEAD(fs_uuids);
49 void btrfs_lock_volumes(void)
51 mutex_lock(&uuid_mutex);
54 void btrfs_unlock_volumes(void)
56 mutex_unlock(&uuid_mutex);
59 int btrfs_cleanup_fs_uuids(void)
61 struct btrfs_fs_devices *fs_devices;
62 struct list_head *uuid_cur;
63 struct list_head *devices_cur;
64 struct btrfs_device *dev;
66 list_for_each(uuid_cur, &fs_uuids) {
67 fs_devices = list_entry(uuid_cur, struct btrfs_fs_devices,
69 while(!list_empty(&fs_devices->devices)) {
70 devices_cur = fs_devices->devices.next;
71 dev = list_entry(devices_cur, struct btrfs_device,
74 close_bdev_excl(dev->bdev);
75 fs_devices->open_devices--;
77 list_del(&dev->dev_list);
85 static struct btrfs_device *__find_device(struct list_head *head, u64 devid,
88 struct btrfs_device *dev;
89 struct list_head *cur;
91 list_for_each(cur, head) {
92 dev = list_entry(cur, struct btrfs_device, dev_list);
93 if (dev->devid == devid &&
94 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
101 static struct btrfs_fs_devices *find_fsid(u8 *fsid)
103 struct list_head *cur;
104 struct btrfs_fs_devices *fs_devices;
106 list_for_each(cur, &fs_uuids) {
107 fs_devices = list_entry(cur, struct btrfs_fs_devices, list);
108 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
115 * we try to collect pending bios for a device so we don't get a large
116 * number of procs sending bios down to the same device. This greatly
117 * improves the schedulers ability to collect and merge the bios.
119 * But, it also turns into a long list of bios to process and that is sure
120 * to eventually make the worker thread block. The solution here is to
121 * make some progress and then put this work struct back at the end of
122 * the list if the block device is congested. This way, multiple devices
123 * can make progress from a single worker thread.
125 int run_scheduled_bios(struct btrfs_device *device)
128 struct backing_dev_info *bdi;
132 unsigned long num_run = 0;
134 bdi = device->bdev->bd_inode->i_mapping->backing_dev_info;
136 spin_lock(&device->io_lock);
138 /* take all the bios off the list at once and process them
139 * later on (without the lock held). But, remember the
140 * tail and other pointers so the bios can be properly reinserted
141 * into the list if we hit congestion
143 pending = device->pending_bios;
144 tail = device->pending_bio_tail;
145 WARN_ON(pending && !tail);
146 device->pending_bios = NULL;
147 device->pending_bio_tail = NULL;
150 * if pending was null this time around, no bios need processing
151 * at all and we can stop. Otherwise it'll loop back up again
152 * and do an additional check so no bios are missed.
154 * device->running_pending is used to synchronize with the
159 device->running_pending = 1;
162 device->running_pending = 0;
164 spin_unlock(&device->io_lock);
168 pending = pending->bi_next;
170 atomic_dec(&device->dev_root->fs_info->nr_async_submits);
171 submit_bio(cur->bi_rw, cur);
175 * we made progress, there is more work to do and the bdi
176 * is now congested. Back off and let other work structs
179 if (pending && num_run && bdi_write_congested(bdi)) {
180 struct bio *old_head;
182 spin_lock(&device->io_lock);
183 old_head = device->pending_bios;
184 device->pending_bios = pending;
185 if (device->pending_bio_tail)
186 tail->bi_next = old_head;
188 device->pending_bio_tail = tail;
190 spin_unlock(&device->io_lock);
191 btrfs_requeue_work(&device->work);
201 void pending_bios_fn(struct btrfs_work *work)
203 struct btrfs_device *device;
205 device = container_of(work, struct btrfs_device, work);
206 run_scheduled_bios(device);
209 static int device_list_add(const char *path,
210 struct btrfs_super_block *disk_super,
211 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
213 struct btrfs_device *device;
214 struct btrfs_fs_devices *fs_devices;
215 u64 found_transid = btrfs_super_generation(disk_super);
217 fs_devices = find_fsid(disk_super->fsid);
219 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
222 INIT_LIST_HEAD(&fs_devices->devices);
223 INIT_LIST_HEAD(&fs_devices->alloc_list);
224 list_add(&fs_devices->list, &fs_uuids);
225 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
226 fs_devices->latest_devid = devid;
227 fs_devices->latest_trans = found_transid;
230 device = __find_device(&fs_devices->devices, devid,
231 disk_super->dev_item.uuid);
234 device = kzalloc(sizeof(*device), GFP_NOFS);
236 /* we can safely leave the fs_devices entry around */
239 device->devid = devid;
240 device->work.func = pending_bios_fn;
241 memcpy(device->uuid, disk_super->dev_item.uuid,
243 device->barriers = 1;
244 spin_lock_init(&device->io_lock);
245 device->name = kstrdup(path, GFP_NOFS);
250 list_add(&device->dev_list, &fs_devices->devices);
251 list_add(&device->dev_alloc_list, &fs_devices->alloc_list);
252 fs_devices->num_devices++;
255 if (found_transid > fs_devices->latest_trans) {
256 fs_devices->latest_devid = devid;
257 fs_devices->latest_trans = found_transid;
259 *fs_devices_ret = fs_devices;
263 int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
265 struct list_head *head = &fs_devices->devices;
266 struct list_head *cur;
267 struct btrfs_device *device;
269 mutex_lock(&uuid_mutex);
271 list_for_each(cur, head) {
272 device = list_entry(cur, struct btrfs_device, dev_list);
273 if (!device->in_fs_metadata) {
274 struct block_device *bdev;
275 list_del(&device->dev_list);
276 list_del(&device->dev_alloc_list);
277 fs_devices->num_devices--;
280 fs_devices->open_devices--;
281 mutex_unlock(&uuid_mutex);
282 close_bdev_excl(bdev);
283 mutex_lock(&uuid_mutex);
290 mutex_unlock(&uuid_mutex);
294 int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
296 struct list_head *head = &fs_devices->devices;
297 struct list_head *cur;
298 struct btrfs_device *device;
300 mutex_lock(&uuid_mutex);
301 list_for_each(cur, head) {
302 device = list_entry(cur, struct btrfs_device, dev_list);
304 close_bdev_excl(device->bdev);
305 fs_devices->open_devices--;
308 device->in_fs_metadata = 0;
310 fs_devices->mounted = 0;
311 mutex_unlock(&uuid_mutex);
315 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
316 int flags, void *holder)
318 struct block_device *bdev;
319 struct list_head *head = &fs_devices->devices;
320 struct list_head *cur;
321 struct btrfs_device *device;
322 struct block_device *latest_bdev = NULL;
323 struct buffer_head *bh;
324 struct btrfs_super_block *disk_super;
325 u64 latest_devid = 0;
326 u64 latest_transid = 0;
331 mutex_lock(&uuid_mutex);
332 if (fs_devices->mounted)
335 list_for_each(cur, head) {
336 device = list_entry(cur, struct btrfs_device, dev_list);
343 bdev = open_bdev_excl(device->name, flags, holder);
346 printk("open %s failed\n", device->name);
349 set_blocksize(bdev, 4096);
351 bh = __bread(bdev, BTRFS_SUPER_INFO_OFFSET / 4096, 4096);
355 disk_super = (struct btrfs_super_block *)bh->b_data;
356 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
357 sizeof(disk_super->magic)))
360 devid = le64_to_cpu(disk_super->dev_item.devid);
361 if (devid != device->devid)
364 transid = btrfs_super_generation(disk_super);
365 if (!latest_transid || transid > latest_transid) {
366 latest_devid = devid;
367 latest_transid = transid;
372 device->in_fs_metadata = 0;
373 fs_devices->open_devices++;
379 close_bdev_excl(bdev);
383 if (fs_devices->open_devices == 0) {
387 fs_devices->mounted = 1;
388 fs_devices->latest_bdev = latest_bdev;
389 fs_devices->latest_devid = latest_devid;
390 fs_devices->latest_trans = latest_transid;
392 mutex_unlock(&uuid_mutex);
396 int btrfs_scan_one_device(const char *path, int flags, void *holder,
397 struct btrfs_fs_devices **fs_devices_ret)
399 struct btrfs_super_block *disk_super;
400 struct block_device *bdev;
401 struct buffer_head *bh;
406 mutex_lock(&uuid_mutex);
408 bdev = open_bdev_excl(path, flags, holder);
415 ret = set_blocksize(bdev, 4096);
418 bh = __bread(bdev, BTRFS_SUPER_INFO_OFFSET / 4096, 4096);
423 disk_super = (struct btrfs_super_block *)bh->b_data;
424 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
425 sizeof(disk_super->magic))) {
429 devid = le64_to_cpu(disk_super->dev_item.devid);
430 transid = btrfs_super_generation(disk_super);
431 if (disk_super->label[0])
432 printk("device label %s ", disk_super->label);
434 /* FIXME, make a readl uuid parser */
435 printk("device fsid %llx-%llx ",
436 *(unsigned long long *)disk_super->fsid,
437 *(unsigned long long *)(disk_super->fsid + 8));
439 printk("devid %Lu transid %Lu %s\n", devid, transid, path);
440 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
445 close_bdev_excl(bdev);
447 mutex_unlock(&uuid_mutex);
452 * this uses a pretty simple search, the expectation is that it is
453 * called very infrequently and that a given device has a small number
456 static int find_free_dev_extent(struct btrfs_trans_handle *trans,
457 struct btrfs_device *device,
458 struct btrfs_path *path,
459 u64 num_bytes, u64 *start)
461 struct btrfs_key key;
462 struct btrfs_root *root = device->dev_root;
463 struct btrfs_dev_extent *dev_extent = NULL;
466 u64 search_start = 0;
467 u64 search_end = device->total_bytes;
471 struct extent_buffer *l;
476 /* FIXME use last free of some kind */
478 /* we don't want to overwrite the superblock on the drive,
479 * so we make sure to start at an offset of at least 1MB
481 search_start = max((u64)1024 * 1024, search_start);
483 if (root->fs_info->alloc_start + num_bytes <= device->total_bytes)
484 search_start = max(root->fs_info->alloc_start, search_start);
486 key.objectid = device->devid;
487 key.offset = search_start;
488 key.type = BTRFS_DEV_EXTENT_KEY;
489 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
492 ret = btrfs_previous_item(root, path, 0, key.type);
496 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
499 slot = path->slots[0];
500 if (slot >= btrfs_header_nritems(l)) {
501 ret = btrfs_next_leaf(root, path);
508 if (search_start >= search_end) {
512 *start = search_start;
516 *start = last_byte > search_start ?
517 last_byte : search_start;
518 if (search_end <= *start) {
524 btrfs_item_key_to_cpu(l, &key, slot);
526 if (key.objectid < device->devid)
529 if (key.objectid > device->devid)
532 if (key.offset >= search_start && key.offset > last_byte &&
534 if (last_byte < search_start)
535 last_byte = search_start;
536 hole_size = key.offset - last_byte;
537 if (key.offset > last_byte &&
538 hole_size >= num_bytes) {
543 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) {
548 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
549 last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent);
555 /* we have to make sure we didn't find an extent that has already
556 * been allocated by the map tree or the original allocation
558 btrfs_release_path(root, path);
559 BUG_ON(*start < search_start);
561 if (*start + num_bytes > search_end) {
565 /* check for pending inserts here */
569 btrfs_release_path(root, path);
573 int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
574 struct btrfs_device *device,
578 struct btrfs_path *path;
579 struct btrfs_root *root = device->dev_root;
580 struct btrfs_key key;
581 struct btrfs_key found_key;
582 struct extent_buffer *leaf = NULL;
583 struct btrfs_dev_extent *extent = NULL;
585 path = btrfs_alloc_path();
589 key.objectid = device->devid;
591 key.type = BTRFS_DEV_EXTENT_KEY;
593 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
595 ret = btrfs_previous_item(root, path, key.objectid,
596 BTRFS_DEV_EXTENT_KEY);
598 leaf = path->nodes[0];
599 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
600 extent = btrfs_item_ptr(leaf, path->slots[0],
601 struct btrfs_dev_extent);
602 BUG_ON(found_key.offset > start || found_key.offset +
603 btrfs_dev_extent_length(leaf, extent) < start);
605 } else if (ret == 0) {
606 leaf = path->nodes[0];
607 extent = btrfs_item_ptr(leaf, path->slots[0],
608 struct btrfs_dev_extent);
612 if (device->bytes_used > 0)
613 device->bytes_used -= btrfs_dev_extent_length(leaf, extent);
614 ret = btrfs_del_item(trans, root, path);
617 btrfs_free_path(path);
621 int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
622 struct btrfs_device *device,
623 u64 chunk_tree, u64 chunk_objectid,
625 u64 num_bytes, u64 *start)
628 struct btrfs_path *path;
629 struct btrfs_root *root = device->dev_root;
630 struct btrfs_dev_extent *extent;
631 struct extent_buffer *leaf;
632 struct btrfs_key key;
634 WARN_ON(!device->in_fs_metadata);
635 path = btrfs_alloc_path();
639 ret = find_free_dev_extent(trans, device, path, num_bytes, start);
644 key.objectid = device->devid;
646 key.type = BTRFS_DEV_EXTENT_KEY;
647 ret = btrfs_insert_empty_item(trans, root, path, &key,
651 leaf = path->nodes[0];
652 extent = btrfs_item_ptr(leaf, path->slots[0],
653 struct btrfs_dev_extent);
654 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
655 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
656 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
658 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
659 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
662 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
663 btrfs_mark_buffer_dirty(leaf);
665 btrfs_free_path(path);
669 static int find_next_chunk(struct btrfs_root *root, u64 objectid, u64 *offset)
671 struct btrfs_path *path;
673 struct btrfs_key key;
674 struct btrfs_chunk *chunk;
675 struct btrfs_key found_key;
677 path = btrfs_alloc_path();
680 key.objectid = objectid;
681 key.offset = (u64)-1;
682 key.type = BTRFS_CHUNK_ITEM_KEY;
684 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
690 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
694 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
696 if (found_key.objectid != objectid)
699 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
701 *offset = found_key.offset +
702 btrfs_chunk_length(path->nodes[0], chunk);
707 btrfs_free_path(path);
711 static int find_next_devid(struct btrfs_root *root, struct btrfs_path *path,
715 struct btrfs_key key;
716 struct btrfs_key found_key;
718 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
719 key.type = BTRFS_DEV_ITEM_KEY;
720 key.offset = (u64)-1;
722 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
728 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
733 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
735 *objectid = found_key.offset + 1;
739 btrfs_release_path(root, path);
744 * the device information is stored in the chunk root
745 * the btrfs_device struct should be fully filled in
747 int btrfs_add_device(struct btrfs_trans_handle *trans,
748 struct btrfs_root *root,
749 struct btrfs_device *device)
752 struct btrfs_path *path;
753 struct btrfs_dev_item *dev_item;
754 struct extent_buffer *leaf;
755 struct btrfs_key key;
759 root = root->fs_info->chunk_root;
761 path = btrfs_alloc_path();
765 ret = find_next_devid(root, path, &free_devid);
769 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
770 key.type = BTRFS_DEV_ITEM_KEY;
771 key.offset = free_devid;
773 ret = btrfs_insert_empty_item(trans, root, path, &key,
778 leaf = path->nodes[0];
779 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
781 device->devid = free_devid;
782 btrfs_set_device_id(leaf, dev_item, device->devid);
783 btrfs_set_device_type(leaf, dev_item, device->type);
784 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
785 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
786 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
787 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
788 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
789 btrfs_set_device_group(leaf, dev_item, 0);
790 btrfs_set_device_seek_speed(leaf, dev_item, 0);
791 btrfs_set_device_bandwidth(leaf, dev_item, 0);
793 ptr = (unsigned long)btrfs_device_uuid(dev_item);
794 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
795 btrfs_mark_buffer_dirty(leaf);
799 btrfs_free_path(path);
803 static int btrfs_rm_dev_item(struct btrfs_root *root,
804 struct btrfs_device *device)
807 struct btrfs_path *path;
808 struct block_device *bdev = device->bdev;
809 struct btrfs_device *next_dev;
810 struct btrfs_key key;
812 struct btrfs_fs_devices *fs_devices;
813 struct btrfs_trans_handle *trans;
815 root = root->fs_info->chunk_root;
817 path = btrfs_alloc_path();
821 trans = btrfs_start_transaction(root, 1);
822 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
823 key.type = BTRFS_DEV_ITEM_KEY;
824 key.offset = device->devid;
826 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
835 ret = btrfs_del_item(trans, root, path);
840 * at this point, the device is zero sized. We want to
841 * remove it from the devices list and zero out the old super
843 list_del_init(&device->dev_list);
844 list_del_init(&device->dev_alloc_list);
845 fs_devices = root->fs_info->fs_devices;
847 next_dev = list_entry(fs_devices->devices.next, struct btrfs_device,
849 if (bdev == root->fs_info->sb->s_bdev)
850 root->fs_info->sb->s_bdev = next_dev->bdev;
851 if (bdev == fs_devices->latest_bdev)
852 fs_devices->latest_bdev = next_dev->bdev;
854 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
855 btrfs_set_super_num_devices(&root->fs_info->super_copy,
858 btrfs_free_path(path);
859 btrfs_commit_transaction(trans, root);
863 int btrfs_rm_device(struct btrfs_root *root, char *device_path)
865 struct btrfs_device *device;
866 struct block_device *bdev;
867 struct buffer_head *bh = NULL;
868 struct btrfs_super_block *disk_super;
873 mutex_lock(&root->fs_info->alloc_mutex);
874 mutex_lock(&root->fs_info->chunk_mutex);
875 mutex_lock(&uuid_mutex);
877 all_avail = root->fs_info->avail_data_alloc_bits |
878 root->fs_info->avail_system_alloc_bits |
879 root->fs_info->avail_metadata_alloc_bits;
881 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
882 btrfs_super_num_devices(&root->fs_info->super_copy) <= 4) {
883 printk("btrfs: unable to go below four devices on raid10\n");
888 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
889 btrfs_super_num_devices(&root->fs_info->super_copy) <= 2) {
890 printk("btrfs: unable to go below two devices on raid1\n");
895 if (strcmp(device_path, "missing") == 0) {
896 struct list_head *cur;
897 struct list_head *devices;
898 struct btrfs_device *tmp;
901 devices = &root->fs_info->fs_devices->devices;
902 list_for_each(cur, devices) {
903 tmp = list_entry(cur, struct btrfs_device, dev_list);
904 if (tmp->in_fs_metadata && !tmp->bdev) {
913 printk("btrfs: no missing devices found to remove\n");
918 bdev = open_bdev_excl(device_path, 0,
919 root->fs_info->bdev_holder);
925 bh = __bread(bdev, BTRFS_SUPER_INFO_OFFSET / 4096, 4096);
930 disk_super = (struct btrfs_super_block *)bh->b_data;
931 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
932 sizeof(disk_super->magic))) {
936 if (memcmp(disk_super->fsid, root->fs_info->fsid,
941 devid = le64_to_cpu(disk_super->dev_item.devid);
942 device = btrfs_find_device(root, devid, NULL);
949 root->fs_info->fs_devices->num_devices--;
950 root->fs_info->fs_devices->open_devices--;
952 ret = btrfs_shrink_device(device, 0);
957 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
962 /* make sure this device isn't detected as part of
965 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
966 set_buffer_dirty(bh);
967 sync_dirty_buffer(bh);
973 /* one close for the device struct or super_block */
974 close_bdev_excl(device->bdev);
977 /* one close for us */
978 close_bdev_excl(bdev);
989 close_bdev_excl(bdev);
991 mutex_unlock(&uuid_mutex);
992 mutex_unlock(&root->fs_info->chunk_mutex);
993 mutex_unlock(&root->fs_info->alloc_mutex);
997 int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
999 struct btrfs_trans_handle *trans;
1000 struct btrfs_device *device;
1001 struct block_device *bdev;
1002 struct list_head *cur;
1003 struct list_head *devices;
1008 bdev = open_bdev_excl(device_path, 0, root->fs_info->bdev_holder);
1013 mutex_lock(&root->fs_info->alloc_mutex);
1014 mutex_lock(&root->fs_info->chunk_mutex);
1016 trans = btrfs_start_transaction(root, 1);
1017 devices = &root->fs_info->fs_devices->devices;
1018 list_for_each(cur, devices) {
1019 device = list_entry(cur, struct btrfs_device, dev_list);
1020 if (device->bdev == bdev) {
1026 device = kzalloc(sizeof(*device), GFP_NOFS);
1028 /* we can safely leave the fs_devices entry around */
1030 goto out_close_bdev;
1033 device->barriers = 1;
1034 device->work.func = pending_bios_fn;
1035 generate_random_uuid(device->uuid);
1036 spin_lock_init(&device->io_lock);
1037 device->name = kstrdup(device_path, GFP_NOFS);
1038 if (!device->name) {
1040 goto out_close_bdev;
1042 device->io_width = root->sectorsize;
1043 device->io_align = root->sectorsize;
1044 device->sector_size = root->sectorsize;
1045 device->total_bytes = i_size_read(bdev->bd_inode);
1046 device->dev_root = root->fs_info->dev_root;
1047 device->bdev = bdev;
1048 device->in_fs_metadata = 1;
1050 ret = btrfs_add_device(trans, root, device);
1052 goto out_close_bdev;
1054 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
1055 btrfs_set_super_total_bytes(&root->fs_info->super_copy,
1056 total_bytes + device->total_bytes);
1058 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
1059 btrfs_set_super_num_devices(&root->fs_info->super_copy,
1062 list_add(&device->dev_list, &root->fs_info->fs_devices->devices);
1063 list_add(&device->dev_alloc_list,
1064 &root->fs_info->fs_devices->alloc_list);
1065 root->fs_info->fs_devices->num_devices++;
1066 root->fs_info->fs_devices->open_devices++;
1068 btrfs_end_transaction(trans, root);
1069 mutex_unlock(&root->fs_info->chunk_mutex);
1070 mutex_unlock(&root->fs_info->alloc_mutex);
1075 close_bdev_excl(bdev);
1079 int btrfs_update_device(struct btrfs_trans_handle *trans,
1080 struct btrfs_device *device)
1083 struct btrfs_path *path;
1084 struct btrfs_root *root;
1085 struct btrfs_dev_item *dev_item;
1086 struct extent_buffer *leaf;
1087 struct btrfs_key key;
1089 root = device->dev_root->fs_info->chunk_root;
1091 path = btrfs_alloc_path();
1095 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1096 key.type = BTRFS_DEV_ITEM_KEY;
1097 key.offset = device->devid;
1099 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1108 leaf = path->nodes[0];
1109 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1111 btrfs_set_device_id(leaf, dev_item, device->devid);
1112 btrfs_set_device_type(leaf, dev_item, device->type);
1113 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1114 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1115 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
1116 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1117 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1118 btrfs_mark_buffer_dirty(leaf);
1121 btrfs_free_path(path);
1125 int btrfs_grow_device(struct btrfs_trans_handle *trans,
1126 struct btrfs_device *device, u64 new_size)
1128 struct btrfs_super_block *super_copy =
1129 &device->dev_root->fs_info->super_copy;
1130 u64 old_total = btrfs_super_total_bytes(super_copy);
1131 u64 diff = new_size - device->total_bytes;
1133 btrfs_set_super_total_bytes(super_copy, old_total + diff);
1134 return btrfs_update_device(trans, device);
1137 static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1138 struct btrfs_root *root,
1139 u64 chunk_tree, u64 chunk_objectid,
1143 struct btrfs_path *path;
1144 struct btrfs_key key;
1146 root = root->fs_info->chunk_root;
1147 path = btrfs_alloc_path();
1151 key.objectid = chunk_objectid;
1152 key.offset = chunk_offset;
1153 key.type = BTRFS_CHUNK_ITEM_KEY;
1155 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1158 ret = btrfs_del_item(trans, root, path);
1161 btrfs_free_path(path);
1165 int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
1168 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1169 struct btrfs_disk_key *disk_key;
1170 struct btrfs_chunk *chunk;
1177 struct btrfs_key key;
1179 array_size = btrfs_super_sys_array_size(super_copy);
1181 ptr = super_copy->sys_chunk_array;
1184 while (cur < array_size) {
1185 disk_key = (struct btrfs_disk_key *)ptr;
1186 btrfs_disk_key_to_cpu(&key, disk_key);
1188 len = sizeof(*disk_key);
1190 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1191 chunk = (struct btrfs_chunk *)(ptr + len);
1192 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1193 len += btrfs_chunk_item_size(num_stripes);
1198 if (key.objectid == chunk_objectid &&
1199 key.offset == chunk_offset) {
1200 memmove(ptr, ptr + len, array_size - (cur + len));
1202 btrfs_set_super_sys_array_size(super_copy, array_size);
1212 int btrfs_relocate_chunk(struct btrfs_root *root,
1213 u64 chunk_tree, u64 chunk_objectid,
1216 struct extent_map_tree *em_tree;
1217 struct btrfs_root *extent_root;
1218 struct btrfs_trans_handle *trans;
1219 struct extent_map *em;
1220 struct map_lookup *map;
1224 printk("btrfs relocating chunk %llu\n",
1225 (unsigned long long)chunk_offset);
1226 root = root->fs_info->chunk_root;
1227 extent_root = root->fs_info->extent_root;
1228 em_tree = &root->fs_info->mapping_tree.map_tree;
1230 /* step one, relocate all the extents inside this chunk */
1231 ret = btrfs_shrink_extent_tree(extent_root, chunk_offset);
1234 trans = btrfs_start_transaction(root, 1);
1238 * step two, delete the device extents and the
1239 * chunk tree entries
1241 spin_lock(&em_tree->lock);
1242 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
1243 spin_unlock(&em_tree->lock);
1245 BUG_ON(em->start > chunk_offset ||
1246 em->start + em->len < chunk_offset);
1247 map = (struct map_lookup *)em->bdev;
1249 for (i = 0; i < map->num_stripes; i++) {
1250 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1251 map->stripes[i].physical);
1254 if (map->stripes[i].dev) {
1255 ret = btrfs_update_device(trans, map->stripes[i].dev);
1259 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1264 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1265 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1269 spin_lock(&em_tree->lock);
1270 remove_extent_mapping(em_tree, em);
1274 /* once for the tree */
1275 free_extent_map(em);
1276 spin_unlock(&em_tree->lock);
1279 free_extent_map(em);
1281 btrfs_end_transaction(trans, root);
1285 static u64 div_factor(u64 num, int factor)
1295 int btrfs_balance(struct btrfs_root *dev_root)
1298 struct list_head *cur;
1299 struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
1300 struct btrfs_device *device;
1303 struct btrfs_path *path;
1304 struct btrfs_key key;
1305 struct btrfs_chunk *chunk;
1306 struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
1307 struct btrfs_trans_handle *trans;
1308 struct btrfs_key found_key;
1311 BUG(); /* FIXME, needs locking */
1313 dev_root = dev_root->fs_info->dev_root;
1315 /* step one make some room on all the devices */
1316 list_for_each(cur, devices) {
1317 device = list_entry(cur, struct btrfs_device, dev_list);
1318 old_size = device->total_bytes;
1319 size_to_free = div_factor(old_size, 1);
1320 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
1321 if (device->total_bytes - device->bytes_used > size_to_free)
1324 ret = btrfs_shrink_device(device, old_size - size_to_free);
1327 trans = btrfs_start_transaction(dev_root, 1);
1330 ret = btrfs_grow_device(trans, device, old_size);
1333 btrfs_end_transaction(trans, dev_root);
1336 /* step two, relocate all the chunks */
1337 path = btrfs_alloc_path();
1340 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1341 key.offset = (u64)-1;
1342 key.type = BTRFS_CHUNK_ITEM_KEY;
1345 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1350 * this shouldn't happen, it means the last relocate
1356 ret = btrfs_previous_item(chunk_root, path, 0,
1357 BTRFS_CHUNK_ITEM_KEY);
1361 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1363 if (found_key.objectid != key.objectid)
1365 chunk = btrfs_item_ptr(path->nodes[0],
1367 struct btrfs_chunk);
1368 key.offset = found_key.offset;
1369 /* chunk zero is special */
1370 if (key.offset == 0)
1373 ret = btrfs_relocate_chunk(chunk_root,
1374 chunk_root->root_key.objectid,
1378 btrfs_release_path(chunk_root, path);
1382 btrfs_free_path(path);
1387 * shrinking a device means finding all of the device extents past
1388 * the new size, and then following the back refs to the chunks.
1389 * The chunk relocation code actually frees the device extent
1391 int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
1393 struct btrfs_trans_handle *trans;
1394 struct btrfs_root *root = device->dev_root;
1395 struct btrfs_dev_extent *dev_extent = NULL;
1396 struct btrfs_path *path;
1403 struct extent_buffer *l;
1404 struct btrfs_key key;
1405 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1406 u64 old_total = btrfs_super_total_bytes(super_copy);
1407 u64 diff = device->total_bytes - new_size;
1410 path = btrfs_alloc_path();
1414 trans = btrfs_start_transaction(root, 1);
1422 device->total_bytes = new_size;
1423 ret = btrfs_update_device(trans, device);
1425 btrfs_end_transaction(trans, root);
1428 WARN_ON(diff > old_total);
1429 btrfs_set_super_total_bytes(super_copy, old_total - diff);
1430 btrfs_end_transaction(trans, root);
1432 key.objectid = device->devid;
1433 key.offset = (u64)-1;
1434 key.type = BTRFS_DEV_EXTENT_KEY;
1437 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1441 ret = btrfs_previous_item(root, path, 0, key.type);
1450 slot = path->slots[0];
1451 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
1453 if (key.objectid != device->devid)
1456 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
1457 length = btrfs_dev_extent_length(l, dev_extent);
1459 if (key.offset + length <= new_size)
1462 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
1463 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
1464 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
1465 btrfs_release_path(root, path);
1467 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
1474 btrfs_free_path(path);
1478 int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
1479 struct btrfs_root *root,
1480 struct btrfs_key *key,
1481 struct btrfs_chunk *chunk, int item_size)
1483 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1484 struct btrfs_disk_key disk_key;
1488 array_size = btrfs_super_sys_array_size(super_copy);
1489 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
1492 ptr = super_copy->sys_chunk_array + array_size;
1493 btrfs_cpu_key_to_disk(&disk_key, key);
1494 memcpy(ptr, &disk_key, sizeof(disk_key));
1495 ptr += sizeof(disk_key);
1496 memcpy(ptr, chunk, item_size);
1497 item_size += sizeof(disk_key);
1498 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
1502 static u64 chunk_bytes_by_type(u64 type, u64 calc_size, int num_stripes,
1505 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
1507 else if (type & BTRFS_BLOCK_GROUP_RAID10)
1508 return calc_size * (num_stripes / sub_stripes);
1510 return calc_size * num_stripes;
1514 int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
1515 struct btrfs_root *extent_root, u64 *start,
1516 u64 *num_bytes, u64 type)
1519 struct btrfs_fs_info *info = extent_root->fs_info;
1520 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
1521 struct btrfs_path *path;
1522 struct btrfs_stripe *stripes;
1523 struct btrfs_device *device = NULL;
1524 struct btrfs_chunk *chunk;
1525 struct list_head private_devs;
1526 struct list_head *dev_list;
1527 struct list_head *cur;
1528 struct extent_map_tree *em_tree;
1529 struct map_lookup *map;
1530 struct extent_map *em;
1531 int min_stripe_size = 1 * 1024 * 1024;
1533 u64 calc_size = 1024 * 1024 * 1024;
1534 u64 max_chunk_size = calc_size;
1539 int num_stripes = 1;
1540 int min_stripes = 1;
1541 int sub_stripes = 0;
1545 int stripe_len = 64 * 1024;
1546 struct btrfs_key key;
1548 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
1549 (type & BTRFS_BLOCK_GROUP_DUP)) {
1551 type &= ~BTRFS_BLOCK_GROUP_DUP;
1553 dev_list = &extent_root->fs_info->fs_devices->alloc_list;
1554 if (list_empty(dev_list))
1557 if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
1558 num_stripes = extent_root->fs_info->fs_devices->open_devices;
1561 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
1565 if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
1566 num_stripes = min_t(u64, 2,
1567 extent_root->fs_info->fs_devices->open_devices);
1568 if (num_stripes < 2)
1572 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
1573 num_stripes = extent_root->fs_info->fs_devices->open_devices;
1574 if (num_stripes < 4)
1576 num_stripes &= ~(u32)1;
1581 if (type & BTRFS_BLOCK_GROUP_DATA) {
1582 max_chunk_size = 10 * calc_size;
1583 min_stripe_size = 64 * 1024 * 1024;
1584 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
1585 max_chunk_size = 4 * calc_size;
1586 min_stripe_size = 32 * 1024 * 1024;
1587 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
1588 calc_size = 8 * 1024 * 1024;
1589 max_chunk_size = calc_size * 2;
1590 min_stripe_size = 1 * 1024 * 1024;
1593 path = btrfs_alloc_path();
1597 /* we don't want a chunk larger than 10% of the FS */
1598 percent_max = div_factor(btrfs_super_total_bytes(&info->super_copy), 1);
1599 max_chunk_size = min(percent_max, max_chunk_size);
1602 if (calc_size * num_stripes > max_chunk_size) {
1603 calc_size = max_chunk_size;
1604 do_div(calc_size, num_stripes);
1605 do_div(calc_size, stripe_len);
1606 calc_size *= stripe_len;
1608 /* we don't want tiny stripes */
1609 calc_size = max_t(u64, min_stripe_size, calc_size);
1611 do_div(calc_size, stripe_len);
1612 calc_size *= stripe_len;
1614 INIT_LIST_HEAD(&private_devs);
1615 cur = dev_list->next;
1618 if (type & BTRFS_BLOCK_GROUP_DUP)
1619 min_free = calc_size * 2;
1621 min_free = calc_size;
1623 /* we add 1MB because we never use the first 1MB of the device */
1624 min_free += 1024 * 1024;
1626 /* build a private list of devices we will allocate from */
1627 while(index < num_stripes) {
1628 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
1630 if (device->total_bytes > device->bytes_used)
1631 avail = device->total_bytes - device->bytes_used;
1636 if (device->in_fs_metadata && avail >= min_free) {
1637 u64 ignored_start = 0;
1638 ret = find_free_dev_extent(trans, device, path,
1642 list_move_tail(&device->dev_alloc_list,
1645 if (type & BTRFS_BLOCK_GROUP_DUP)
1648 } else if (device->in_fs_metadata && avail > max_avail)
1650 if (cur == dev_list)
1653 if (index < num_stripes) {
1654 list_splice(&private_devs, dev_list);
1655 if (index >= min_stripes) {
1656 num_stripes = index;
1657 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
1658 num_stripes /= sub_stripes;
1659 num_stripes *= sub_stripes;
1664 if (!looped && max_avail > 0) {
1666 calc_size = max_avail;
1669 btrfs_free_path(path);
1672 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1673 key.type = BTRFS_CHUNK_ITEM_KEY;
1674 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
1677 btrfs_free_path(path);
1681 chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS);
1683 btrfs_free_path(path);
1687 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
1690 btrfs_free_path(path);
1693 btrfs_free_path(path);
1696 stripes = &chunk->stripe;
1697 *num_bytes = chunk_bytes_by_type(type, calc_size,
1698 num_stripes, sub_stripes);
1701 while(index < num_stripes) {
1702 struct btrfs_stripe *stripe;
1703 BUG_ON(list_empty(&private_devs));
1704 cur = private_devs.next;
1705 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
1707 /* loop over this device again if we're doing a dup group */
1708 if (!(type & BTRFS_BLOCK_GROUP_DUP) ||
1709 (index == num_stripes - 1))
1710 list_move_tail(&device->dev_alloc_list, dev_list);
1712 ret = btrfs_alloc_dev_extent(trans, device,
1713 info->chunk_root->root_key.objectid,
1714 BTRFS_FIRST_CHUNK_TREE_OBJECTID, key.offset,
1715 calc_size, &dev_offset);
1717 device->bytes_used += calc_size;
1718 ret = btrfs_update_device(trans, device);
1721 map->stripes[index].dev = device;
1722 map->stripes[index].physical = dev_offset;
1723 stripe = stripes + index;
1724 btrfs_set_stack_stripe_devid(stripe, device->devid);
1725 btrfs_set_stack_stripe_offset(stripe, dev_offset);
1726 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
1727 physical = dev_offset;
1730 BUG_ON(!list_empty(&private_devs));
1732 /* key was set above */
1733 btrfs_set_stack_chunk_length(chunk, *num_bytes);
1734 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
1735 btrfs_set_stack_chunk_stripe_len(chunk, stripe_len);
1736 btrfs_set_stack_chunk_type(chunk, type);
1737 btrfs_set_stack_chunk_num_stripes(chunk, num_stripes);
1738 btrfs_set_stack_chunk_io_align(chunk, stripe_len);
1739 btrfs_set_stack_chunk_io_width(chunk, stripe_len);
1740 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
1741 btrfs_set_stack_chunk_sub_stripes(chunk, sub_stripes);
1742 map->sector_size = extent_root->sectorsize;
1743 map->stripe_len = stripe_len;
1744 map->io_align = stripe_len;
1745 map->io_width = stripe_len;
1747 map->num_stripes = num_stripes;
1748 map->sub_stripes = sub_stripes;
1750 ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
1751 btrfs_chunk_item_size(num_stripes));
1753 *start = key.offset;;
1755 em = alloc_extent_map(GFP_NOFS);
1758 em->bdev = (struct block_device *)map;
1759 em->start = key.offset;
1760 em->len = *num_bytes;
1761 em->block_start = 0;
1763 if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
1764 ret = btrfs_add_system_chunk(trans, chunk_root, &key,
1765 chunk, btrfs_chunk_item_size(num_stripes));
1770 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
1771 spin_lock(&em_tree->lock);
1772 ret = add_extent_mapping(em_tree, em);
1773 spin_unlock(&em_tree->lock);
1775 free_extent_map(em);
1779 void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
1781 extent_map_tree_init(&tree->map_tree, GFP_NOFS);
1784 void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
1786 struct extent_map *em;
1789 spin_lock(&tree->map_tree.lock);
1790 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
1792 remove_extent_mapping(&tree->map_tree, em);
1793 spin_unlock(&tree->map_tree.lock);
1798 free_extent_map(em);
1799 /* once for the tree */
1800 free_extent_map(em);
1804 int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
1806 struct extent_map *em;
1807 struct map_lookup *map;
1808 struct extent_map_tree *em_tree = &map_tree->map_tree;
1811 spin_lock(&em_tree->lock);
1812 em = lookup_extent_mapping(em_tree, logical, len);
1813 spin_unlock(&em_tree->lock);
1816 BUG_ON(em->start > logical || em->start + em->len < logical);
1817 map = (struct map_lookup *)em->bdev;
1818 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
1819 ret = map->num_stripes;
1820 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
1821 ret = map->sub_stripes;
1824 free_extent_map(em);
1828 static int find_live_mirror(struct map_lookup *map, int first, int num,
1832 if (map->stripes[optimal].dev->bdev)
1834 for (i = first; i < first + num; i++) {
1835 if (map->stripes[i].dev->bdev)
1838 /* we couldn't find one that doesn't fail. Just return something
1839 * and the io error handling code will clean up eventually
1844 static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
1845 u64 logical, u64 *length,
1846 struct btrfs_multi_bio **multi_ret,
1847 int mirror_num, struct page *unplug_page)
1849 struct extent_map *em;
1850 struct map_lookup *map;
1851 struct extent_map_tree *em_tree = &map_tree->map_tree;
1855 int stripes_allocated = 8;
1856 int stripes_required = 1;
1861 struct btrfs_multi_bio *multi = NULL;
1863 if (multi_ret && !(rw & (1 << BIO_RW))) {
1864 stripes_allocated = 1;
1868 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
1873 atomic_set(&multi->error, 0);
1876 spin_lock(&em_tree->lock);
1877 em = lookup_extent_mapping(em_tree, logical, *length);
1878 spin_unlock(&em_tree->lock);
1880 if (!em && unplug_page)
1884 printk("unable to find logical %Lu len %Lu\n", logical, *length);
1888 BUG_ON(em->start > logical || em->start + em->len < logical);
1889 map = (struct map_lookup *)em->bdev;
1890 offset = logical - em->start;
1892 if (mirror_num > map->num_stripes)
1895 /* if our multi bio struct is too small, back off and try again */
1896 if (rw & (1 << BIO_RW)) {
1897 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
1898 BTRFS_BLOCK_GROUP_DUP)) {
1899 stripes_required = map->num_stripes;
1901 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1902 stripes_required = map->sub_stripes;
1906 if (multi_ret && rw == WRITE &&
1907 stripes_allocated < stripes_required) {
1908 stripes_allocated = map->num_stripes;
1909 free_extent_map(em);
1915 * stripe_nr counts the total number of stripes we have to stride
1916 * to get to this block
1918 do_div(stripe_nr, map->stripe_len);
1920 stripe_offset = stripe_nr * map->stripe_len;
1921 BUG_ON(offset < stripe_offset);
1923 /* stripe_offset is the offset of this block in its stripe*/
1924 stripe_offset = offset - stripe_offset;
1926 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
1927 BTRFS_BLOCK_GROUP_RAID10 |
1928 BTRFS_BLOCK_GROUP_DUP)) {
1929 /* we limit the length of each bio to what fits in a stripe */
1930 *length = min_t(u64, em->len - offset,
1931 map->stripe_len - stripe_offset);
1933 *length = em->len - offset;
1936 if (!multi_ret && !unplug_page)
1941 if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
1942 if (unplug_page || (rw & (1 << BIO_RW)))
1943 num_stripes = map->num_stripes;
1944 else if (mirror_num)
1945 stripe_index = mirror_num - 1;
1947 stripe_index = find_live_mirror(map, 0,
1949 current->pid % map->num_stripes);
1952 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
1953 if (rw & (1 << BIO_RW))
1954 num_stripes = map->num_stripes;
1955 else if (mirror_num)
1956 stripe_index = mirror_num - 1;
1958 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1959 int factor = map->num_stripes / map->sub_stripes;
1961 stripe_index = do_div(stripe_nr, factor);
1962 stripe_index *= map->sub_stripes;
1964 if (unplug_page || (rw & (1 << BIO_RW)))
1965 num_stripes = map->sub_stripes;
1966 else if (mirror_num)
1967 stripe_index += mirror_num - 1;
1969 stripe_index = find_live_mirror(map, stripe_index,
1970 map->sub_stripes, stripe_index +
1971 current->pid % map->sub_stripes);
1975 * after this do_div call, stripe_nr is the number of stripes
1976 * on this device we have to walk to find the data, and
1977 * stripe_index is the number of our device in the stripe array
1979 stripe_index = do_div(stripe_nr, map->num_stripes);
1981 BUG_ON(stripe_index >= map->num_stripes);
1983 for (i = 0; i < num_stripes; i++) {
1985 struct btrfs_device *device;
1986 struct backing_dev_info *bdi;
1988 device = map->stripes[stripe_index].dev;
1990 bdi = blk_get_backing_dev_info(device->bdev);
1991 if (bdi->unplug_io_fn) {
1992 bdi->unplug_io_fn(bdi, unplug_page);
1996 multi->stripes[i].physical =
1997 map->stripes[stripe_index].physical +
1998 stripe_offset + stripe_nr * map->stripe_len;
1999 multi->stripes[i].dev = map->stripes[stripe_index].dev;
2005 multi->num_stripes = num_stripes;
2006 multi->max_errors = max_errors;
2009 free_extent_map(em);
2013 int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2014 u64 logical, u64 *length,
2015 struct btrfs_multi_bio **multi_ret, int mirror_num)
2017 return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
2021 int btrfs_unplug_page(struct btrfs_mapping_tree *map_tree,
2022 u64 logical, struct page *page)
2024 u64 length = PAGE_CACHE_SIZE;
2025 return __btrfs_map_block(map_tree, READ, logical, &length,
2030 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2031 static void end_bio_multi_stripe(struct bio *bio, int err)
2033 static int end_bio_multi_stripe(struct bio *bio,
2034 unsigned int bytes_done, int err)
2037 struct btrfs_multi_bio *multi = bio->bi_private;
2039 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
2044 atomic_inc(&multi->error);
2046 if (atomic_dec_and_test(&multi->stripes_pending)) {
2047 bio->bi_private = multi->private;
2048 bio->bi_end_io = multi->end_io;
2049 /* only send an error to the higher layers if it is
2050 * beyond the tolerance of the multi-bio
2052 if (atomic_read(&multi->error) > multi->max_errors) {
2056 * this bio is actually up to date, we didn't
2057 * go over the max number of errors
2059 set_bit(BIO_UPTODATE, &bio->bi_flags);
2064 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
2065 bio_endio(bio, bio->bi_size, err);
2067 bio_endio(bio, err);
2072 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
2077 struct async_sched {
2080 struct btrfs_fs_info *info;
2081 struct btrfs_work work;
2085 * see run_scheduled_bios for a description of why bios are collected for
2088 * This will add one bio to the pending list for a device and make sure
2089 * the work struct is scheduled.
2091 int schedule_bio(struct btrfs_root *root, struct btrfs_device *device,
2092 int rw, struct bio *bio)
2094 int should_queue = 1;
2096 /* don't bother with additional async steps for reads, right now */
2097 if (!(rw & (1 << BIO_RW))) {
2098 submit_bio(rw, bio);
2103 * nr_async_sumbits allows us to reliably return congestion to the
2104 * higher layers. Otherwise, the async bio makes it appear we have
2105 * made progress against dirty pages when we've really just put it
2106 * on a queue for later
2108 atomic_inc(&root->fs_info->nr_async_submits);
2109 bio->bi_next = NULL;
2112 spin_lock(&device->io_lock);
2114 if (device->pending_bio_tail)
2115 device->pending_bio_tail->bi_next = bio;
2117 device->pending_bio_tail = bio;
2118 if (!device->pending_bios)
2119 device->pending_bios = bio;
2120 if (device->running_pending)
2123 spin_unlock(&device->io_lock);
2126 btrfs_queue_worker(&root->fs_info->submit_workers,
2131 int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
2132 int mirror_num, int async_submit)
2134 struct btrfs_mapping_tree *map_tree;
2135 struct btrfs_device *dev;
2136 struct bio *first_bio = bio;
2137 u64 logical = bio->bi_sector << 9;
2140 struct btrfs_multi_bio *multi = NULL;
2145 length = bio->bi_size;
2146 map_tree = &root->fs_info->mapping_tree;
2147 map_length = length;
2149 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
2153 total_devs = multi->num_stripes;
2154 if (map_length < length) {
2155 printk("mapping failed logical %Lu bio len %Lu "
2156 "len %Lu\n", logical, length, map_length);
2159 multi->end_io = first_bio->bi_end_io;
2160 multi->private = first_bio->bi_private;
2161 atomic_set(&multi->stripes_pending, multi->num_stripes);
2163 while(dev_nr < total_devs) {
2164 if (total_devs > 1) {
2165 if (dev_nr < total_devs - 1) {
2166 bio = bio_clone(first_bio, GFP_NOFS);
2171 bio->bi_private = multi;
2172 bio->bi_end_io = end_bio_multi_stripe;
2174 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
2175 dev = multi->stripes[dev_nr].dev;
2176 if (dev && dev->bdev) {
2177 bio->bi_bdev = dev->bdev;
2179 schedule_bio(root, dev, rw, bio);
2181 submit_bio(rw, bio);
2183 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
2184 bio->bi_sector = logical >> 9;
2185 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
2186 bio_endio(bio, bio->bi_size, -EIO);
2188 bio_endio(bio, -EIO);
2193 if (total_devs == 1)
2198 struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
2201 struct list_head *head = &root->fs_info->fs_devices->devices;
2203 return __find_device(head, devid, uuid);
2206 static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
2207 u64 devid, u8 *dev_uuid)
2209 struct btrfs_device *device;
2210 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2212 device = kzalloc(sizeof(*device), GFP_NOFS);
2213 list_add(&device->dev_list,
2214 &fs_devices->devices);
2215 list_add(&device->dev_alloc_list,
2216 &fs_devices->alloc_list);
2217 device->barriers = 1;
2218 device->dev_root = root->fs_info->dev_root;
2219 device->devid = devid;
2220 device->work.func = pending_bios_fn;
2221 fs_devices->num_devices++;
2222 spin_lock_init(&device->io_lock);
2223 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
2228 static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
2229 struct extent_buffer *leaf,
2230 struct btrfs_chunk *chunk)
2232 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2233 struct map_lookup *map;
2234 struct extent_map *em;
2238 u8 uuid[BTRFS_UUID_SIZE];
2243 logical = key->offset;
2244 length = btrfs_chunk_length(leaf, chunk);
2246 spin_lock(&map_tree->map_tree.lock);
2247 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
2248 spin_unlock(&map_tree->map_tree.lock);
2250 /* already mapped? */
2251 if (em && em->start <= logical && em->start + em->len > logical) {
2252 free_extent_map(em);
2255 free_extent_map(em);
2258 map = kzalloc(sizeof(*map), GFP_NOFS);
2262 em = alloc_extent_map(GFP_NOFS);
2265 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2266 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
2268 free_extent_map(em);
2272 em->bdev = (struct block_device *)map;
2273 em->start = logical;
2275 em->block_start = 0;
2277 map->num_stripes = num_stripes;
2278 map->io_width = btrfs_chunk_io_width(leaf, chunk);
2279 map->io_align = btrfs_chunk_io_align(leaf, chunk);
2280 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
2281 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
2282 map->type = btrfs_chunk_type(leaf, chunk);
2283 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
2284 for (i = 0; i < num_stripes; i++) {
2285 map->stripes[i].physical =
2286 btrfs_stripe_offset_nr(leaf, chunk, i);
2287 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
2288 read_extent_buffer(leaf, uuid, (unsigned long)
2289 btrfs_stripe_dev_uuid_nr(chunk, i),
2291 map->stripes[i].dev = btrfs_find_device(root, devid, uuid);
2293 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
2295 free_extent_map(em);
2298 if (!map->stripes[i].dev) {
2299 map->stripes[i].dev =
2300 add_missing_dev(root, devid, uuid);
2301 if (!map->stripes[i].dev) {
2303 free_extent_map(em);
2307 map->stripes[i].dev->in_fs_metadata = 1;
2310 spin_lock(&map_tree->map_tree.lock);
2311 ret = add_extent_mapping(&map_tree->map_tree, em);
2312 spin_unlock(&map_tree->map_tree.lock);
2314 free_extent_map(em);
2319 static int fill_device_from_item(struct extent_buffer *leaf,
2320 struct btrfs_dev_item *dev_item,
2321 struct btrfs_device *device)
2325 device->devid = btrfs_device_id(leaf, dev_item);
2326 device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
2327 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
2328 device->type = btrfs_device_type(leaf, dev_item);
2329 device->io_align = btrfs_device_io_align(leaf, dev_item);
2330 device->io_width = btrfs_device_io_width(leaf, dev_item);
2331 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
2333 ptr = (unsigned long)btrfs_device_uuid(dev_item);
2334 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
2339 static int read_one_dev(struct btrfs_root *root,
2340 struct extent_buffer *leaf,
2341 struct btrfs_dev_item *dev_item)
2343 struct btrfs_device *device;
2346 u8 dev_uuid[BTRFS_UUID_SIZE];
2348 devid = btrfs_device_id(leaf, dev_item);
2349 read_extent_buffer(leaf, dev_uuid,
2350 (unsigned long)btrfs_device_uuid(dev_item),
2352 device = btrfs_find_device(root, devid, dev_uuid);
2354 printk("warning devid %Lu missing\n", devid);
2355 device = add_missing_dev(root, devid, dev_uuid);
2360 fill_device_from_item(leaf, dev_item, device);
2361 device->dev_root = root->fs_info->dev_root;
2362 device->in_fs_metadata = 1;
2365 ret = btrfs_open_device(device);
2373 int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
2375 struct btrfs_dev_item *dev_item;
2377 dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
2379 return read_one_dev(root, buf, dev_item);
2382 int btrfs_read_sys_array(struct btrfs_root *root)
2384 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2385 struct extent_buffer *sb;
2386 struct btrfs_disk_key *disk_key;
2387 struct btrfs_chunk *chunk;
2389 unsigned long sb_ptr;
2395 struct btrfs_key key;
2397 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
2398 BTRFS_SUPER_INFO_SIZE);
2401 btrfs_set_buffer_uptodate(sb);
2402 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
2403 array_size = btrfs_super_sys_array_size(super_copy);
2405 ptr = super_copy->sys_chunk_array;
2406 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
2409 while (cur < array_size) {
2410 disk_key = (struct btrfs_disk_key *)ptr;
2411 btrfs_disk_key_to_cpu(&key, disk_key);
2413 len = sizeof(*disk_key); ptr += len;
2417 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2418 chunk = (struct btrfs_chunk *)sb_ptr;
2419 ret = read_one_chunk(root, &key, sb, chunk);
2422 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
2423 len = btrfs_chunk_item_size(num_stripes);
2432 free_extent_buffer(sb);
2436 int btrfs_read_chunk_tree(struct btrfs_root *root)
2438 struct btrfs_path *path;
2439 struct extent_buffer *leaf;
2440 struct btrfs_key key;
2441 struct btrfs_key found_key;
2445 root = root->fs_info->chunk_root;
2447 path = btrfs_alloc_path();
2451 /* first we search for all of the device items, and then we
2452 * read in all of the chunk items. This way we can create chunk
2453 * mappings that reference all of the devices that are afound
2455 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2459 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2461 leaf = path->nodes[0];
2462 slot = path->slots[0];
2463 if (slot >= btrfs_header_nritems(leaf)) {
2464 ret = btrfs_next_leaf(root, path);
2471 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2472 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
2473 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
2475 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
2476 struct btrfs_dev_item *dev_item;
2477 dev_item = btrfs_item_ptr(leaf, slot,
2478 struct btrfs_dev_item);
2479 ret = read_one_dev(root, leaf, dev_item);
2482 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
2483 struct btrfs_chunk *chunk;
2484 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
2485 ret = read_one_chunk(root, &found_key, leaf, chunk);
2489 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
2491 btrfs_release_path(root, path);
2495 btrfs_free_path(path);