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>
26 #include "extent_map.h"
28 #include "transaction.h"
29 #include "print-tree.h"
31 #include "async-thread.h"
41 struct btrfs_bio_stripe stripes[];
44 static int init_first_rw_device(struct btrfs_trans_handle *trans,
45 struct btrfs_root *root,
46 struct btrfs_device *device);
47 static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
49 #define map_lookup_size(n) (sizeof(struct map_lookup) + \
50 (sizeof(struct btrfs_bio_stripe) * (n)))
52 static DEFINE_MUTEX(uuid_mutex);
53 static LIST_HEAD(fs_uuids);
55 void btrfs_lock_volumes(void)
57 mutex_lock(&uuid_mutex);
60 void btrfs_unlock_volumes(void)
62 mutex_unlock(&uuid_mutex);
65 static void lock_chunks(struct btrfs_root *root)
67 mutex_lock(&root->fs_info->chunk_mutex);
70 static void unlock_chunks(struct btrfs_root *root)
72 mutex_unlock(&root->fs_info->chunk_mutex);
75 static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
77 struct btrfs_device *device;
78 WARN_ON(fs_devices->opened);
79 while (!list_empty(&fs_devices->devices)) {
80 device = list_entry(fs_devices->devices.next,
81 struct btrfs_device, dev_list);
82 list_del(&device->dev_list);
89 int btrfs_cleanup_fs_uuids(void)
91 struct btrfs_fs_devices *fs_devices;
93 while (!list_empty(&fs_uuids)) {
94 fs_devices = list_entry(fs_uuids.next,
95 struct btrfs_fs_devices, list);
96 list_del(&fs_devices->list);
97 free_fs_devices(fs_devices);
102 static noinline struct btrfs_device *__find_device(struct list_head *head,
105 struct btrfs_device *dev;
107 list_for_each_entry(dev, head, dev_list) {
108 if (dev->devid == devid &&
109 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
116 static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
118 struct btrfs_fs_devices *fs_devices;
120 list_for_each_entry(fs_devices, &fs_uuids, list) {
121 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
128 * we try to collect pending bios for a device so we don't get a large
129 * number of procs sending bios down to the same device. This greatly
130 * improves the schedulers ability to collect and merge the bios.
132 * But, it also turns into a long list of bios to process and that is sure
133 * to eventually make the worker thread block. The solution here is to
134 * make some progress and then put this work struct back at the end of
135 * the list if the block device is congested. This way, multiple devices
136 * can make progress from a single worker thread.
138 static noinline int run_scheduled_bios(struct btrfs_device *device)
141 struct backing_dev_info *bdi;
142 struct btrfs_fs_info *fs_info;
146 unsigned long num_run = 0;
149 bdi = device->bdev->bd_inode->i_mapping->backing_dev_info;
150 fs_info = device->dev_root->fs_info;
151 limit = btrfs_async_submit_limit(fs_info);
152 limit = limit * 2 / 3;
155 spin_lock(&device->io_lock);
157 /* take all the bios off the list at once and process them
158 * later on (without the lock held). But, remember the
159 * tail and other pointers so the bios can be properly reinserted
160 * into the list if we hit congestion
162 pending = device->pending_bios;
163 tail = device->pending_bio_tail;
164 WARN_ON(pending && !tail);
165 device->pending_bios = NULL;
166 device->pending_bio_tail = NULL;
169 * if pending was null this time around, no bios need processing
170 * at all and we can stop. Otherwise it'll loop back up again
171 * and do an additional check so no bios are missed.
173 * device->running_pending is used to synchronize with the
178 device->running_pending = 1;
181 device->running_pending = 0;
183 spin_unlock(&device->io_lock);
187 pending = pending->bi_next;
189 atomic_dec(&fs_info->nr_async_bios);
191 if (atomic_read(&fs_info->nr_async_bios) < limit &&
192 waitqueue_active(&fs_info->async_submit_wait))
193 wake_up(&fs_info->async_submit_wait);
195 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
197 submit_bio(cur->bi_rw, cur);
202 * we made progress, there is more work to do and the bdi
203 * is now congested. Back off and let other work structs
206 if (pending && bdi_write_congested(bdi) &&
207 fs_info->fs_devices->open_devices > 1) {
208 struct bio *old_head;
210 spin_lock(&device->io_lock);
212 old_head = device->pending_bios;
213 device->pending_bios = pending;
214 if (device->pending_bio_tail)
215 tail->bi_next = old_head;
217 device->pending_bio_tail = tail;
218 device->running_pending = 0;
220 spin_unlock(&device->io_lock);
221 btrfs_requeue_work(&device->work);
231 static void pending_bios_fn(struct btrfs_work *work)
233 struct btrfs_device *device;
235 device = container_of(work, struct btrfs_device, work);
236 run_scheduled_bios(device);
239 static noinline int device_list_add(const char *path,
240 struct btrfs_super_block *disk_super,
241 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
243 struct btrfs_device *device;
244 struct btrfs_fs_devices *fs_devices;
245 u64 found_transid = btrfs_super_generation(disk_super);
247 fs_devices = find_fsid(disk_super->fsid);
249 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
252 INIT_LIST_HEAD(&fs_devices->devices);
253 INIT_LIST_HEAD(&fs_devices->alloc_list);
254 list_add(&fs_devices->list, &fs_uuids);
255 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
256 fs_devices->latest_devid = devid;
257 fs_devices->latest_trans = found_transid;
260 device = __find_device(&fs_devices->devices, devid,
261 disk_super->dev_item.uuid);
264 if (fs_devices->opened)
267 device = kzalloc(sizeof(*device), GFP_NOFS);
269 /* we can safely leave the fs_devices entry around */
272 device->devid = devid;
273 device->work.func = pending_bios_fn;
274 memcpy(device->uuid, disk_super->dev_item.uuid,
276 device->barriers = 1;
277 spin_lock_init(&device->io_lock);
278 device->name = kstrdup(path, GFP_NOFS);
283 INIT_LIST_HEAD(&device->dev_alloc_list);
284 list_add(&device->dev_list, &fs_devices->devices);
285 device->fs_devices = fs_devices;
286 fs_devices->num_devices++;
289 if (found_transid > fs_devices->latest_trans) {
290 fs_devices->latest_devid = devid;
291 fs_devices->latest_trans = found_transid;
293 *fs_devices_ret = fs_devices;
297 static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
299 struct btrfs_fs_devices *fs_devices;
300 struct btrfs_device *device;
301 struct btrfs_device *orig_dev;
303 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
305 return ERR_PTR(-ENOMEM);
307 INIT_LIST_HEAD(&fs_devices->devices);
308 INIT_LIST_HEAD(&fs_devices->alloc_list);
309 INIT_LIST_HEAD(&fs_devices->list);
310 fs_devices->latest_devid = orig->latest_devid;
311 fs_devices->latest_trans = orig->latest_trans;
312 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
314 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
315 device = kzalloc(sizeof(*device), GFP_NOFS);
319 device->name = kstrdup(orig_dev->name, GFP_NOFS);
323 device->devid = orig_dev->devid;
324 device->work.func = pending_bios_fn;
325 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
326 device->barriers = 1;
327 spin_lock_init(&device->io_lock);
328 INIT_LIST_HEAD(&device->dev_list);
329 INIT_LIST_HEAD(&device->dev_alloc_list);
331 list_add(&device->dev_list, &fs_devices->devices);
332 device->fs_devices = fs_devices;
333 fs_devices->num_devices++;
337 free_fs_devices(fs_devices);
338 return ERR_PTR(-ENOMEM);
341 int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
343 struct btrfs_device *device, *next;
345 mutex_lock(&uuid_mutex);
347 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
348 if (device->in_fs_metadata)
352 close_bdev_exclusive(device->bdev, device->mode);
354 fs_devices->open_devices--;
356 if (device->writeable) {
357 list_del_init(&device->dev_alloc_list);
358 device->writeable = 0;
359 fs_devices->rw_devices--;
361 list_del_init(&device->dev_list);
362 fs_devices->num_devices--;
367 if (fs_devices->seed) {
368 fs_devices = fs_devices->seed;
372 mutex_unlock(&uuid_mutex);
376 static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
378 struct btrfs_device *device;
380 if (--fs_devices->opened > 0)
383 list_for_each_entry(device, &fs_devices->devices, dev_list) {
385 close_bdev_exclusive(device->bdev, device->mode);
386 fs_devices->open_devices--;
388 if (device->writeable) {
389 list_del_init(&device->dev_alloc_list);
390 fs_devices->rw_devices--;
394 device->writeable = 0;
395 device->in_fs_metadata = 0;
397 WARN_ON(fs_devices->open_devices);
398 WARN_ON(fs_devices->rw_devices);
399 fs_devices->opened = 0;
400 fs_devices->seeding = 0;
405 int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
407 struct btrfs_fs_devices *seed_devices = NULL;
410 mutex_lock(&uuid_mutex);
411 ret = __btrfs_close_devices(fs_devices);
412 if (!fs_devices->opened) {
413 seed_devices = fs_devices->seed;
414 fs_devices->seed = NULL;
416 mutex_unlock(&uuid_mutex);
418 while (seed_devices) {
419 fs_devices = seed_devices;
420 seed_devices = fs_devices->seed;
421 __btrfs_close_devices(fs_devices);
422 free_fs_devices(fs_devices);
427 static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
428 fmode_t flags, void *holder)
430 struct block_device *bdev;
431 struct list_head *head = &fs_devices->devices;
432 struct btrfs_device *device;
433 struct block_device *latest_bdev = NULL;
434 struct buffer_head *bh;
435 struct btrfs_super_block *disk_super;
436 u64 latest_devid = 0;
437 u64 latest_transid = 0;
442 list_for_each_entry(device, head, dev_list) {
448 bdev = open_bdev_exclusive(device->name, flags, holder);
450 printk(KERN_INFO "open %s failed\n", device->name);
453 set_blocksize(bdev, 4096);
455 bh = btrfs_read_dev_super(bdev);
459 disk_super = (struct btrfs_super_block *)bh->b_data;
460 devid = le64_to_cpu(disk_super->dev_item.devid);
461 if (devid != device->devid)
464 if (memcmp(device->uuid, disk_super->dev_item.uuid,
468 device->generation = btrfs_super_generation(disk_super);
469 if (!latest_transid || device->generation > latest_transid) {
470 latest_devid = devid;
471 latest_transid = device->generation;
475 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
476 device->writeable = 0;
478 device->writeable = !bdev_read_only(bdev);
483 device->in_fs_metadata = 0;
484 device->mode = flags;
486 fs_devices->open_devices++;
487 if (device->writeable) {
488 fs_devices->rw_devices++;
489 list_add(&device->dev_alloc_list,
490 &fs_devices->alloc_list);
497 close_bdev_exclusive(bdev, FMODE_READ);
501 if (fs_devices->open_devices == 0) {
505 fs_devices->seeding = seeding;
506 fs_devices->opened = 1;
507 fs_devices->latest_bdev = latest_bdev;
508 fs_devices->latest_devid = latest_devid;
509 fs_devices->latest_trans = latest_transid;
510 fs_devices->total_rw_bytes = 0;
515 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
516 fmode_t flags, void *holder)
520 mutex_lock(&uuid_mutex);
521 if (fs_devices->opened) {
522 fs_devices->opened++;
525 ret = __btrfs_open_devices(fs_devices, flags, holder);
527 mutex_unlock(&uuid_mutex);
531 int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
532 struct btrfs_fs_devices **fs_devices_ret)
534 struct btrfs_super_block *disk_super;
535 struct block_device *bdev;
536 struct buffer_head *bh;
541 mutex_lock(&uuid_mutex);
543 bdev = open_bdev_exclusive(path, flags, holder);
550 ret = set_blocksize(bdev, 4096);
553 bh = btrfs_read_dev_super(bdev);
558 disk_super = (struct btrfs_super_block *)bh->b_data;
559 devid = le64_to_cpu(disk_super->dev_item.devid);
560 transid = btrfs_super_generation(disk_super);
561 if (disk_super->label[0])
562 printk(KERN_INFO "device label %s ", disk_super->label);
564 /* FIXME, make a readl uuid parser */
565 printk(KERN_INFO "device fsid %llx-%llx ",
566 *(unsigned long long *)disk_super->fsid,
567 *(unsigned long long *)(disk_super->fsid + 8));
569 printk(KERN_CONT "devid %llu transid %llu %s\n",
570 (unsigned long long)devid, (unsigned long long)transid, path);
571 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
575 close_bdev_exclusive(bdev, flags);
577 mutex_unlock(&uuid_mutex);
582 * this uses a pretty simple search, the expectation is that it is
583 * called very infrequently and that a given device has a small number
586 static noinline int find_free_dev_extent(struct btrfs_trans_handle *trans,
587 struct btrfs_device *device,
588 u64 num_bytes, u64 *start)
590 struct btrfs_key key;
591 struct btrfs_root *root = device->dev_root;
592 struct btrfs_dev_extent *dev_extent = NULL;
593 struct btrfs_path *path;
596 u64 search_start = 0;
597 u64 search_end = device->total_bytes;
601 struct extent_buffer *l;
603 path = btrfs_alloc_path();
609 /* FIXME use last free of some kind */
611 /* we don't want to overwrite the superblock on the drive,
612 * so we make sure to start at an offset of at least 1MB
614 search_start = max((u64)1024 * 1024, search_start);
616 if (root->fs_info->alloc_start + num_bytes <= device->total_bytes)
617 search_start = max(root->fs_info->alloc_start, search_start);
619 key.objectid = device->devid;
620 key.offset = search_start;
621 key.type = BTRFS_DEV_EXTENT_KEY;
622 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
625 ret = btrfs_previous_item(root, path, 0, key.type);
629 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
632 slot = path->slots[0];
633 if (slot >= btrfs_header_nritems(l)) {
634 ret = btrfs_next_leaf(root, path);
641 if (search_start >= search_end) {
645 *start = search_start;
649 *start = last_byte > search_start ?
650 last_byte : search_start;
651 if (search_end <= *start) {
657 btrfs_item_key_to_cpu(l, &key, slot);
659 if (key.objectid < device->devid)
662 if (key.objectid > device->devid)
665 if (key.offset >= search_start && key.offset > last_byte &&
667 if (last_byte < search_start)
668 last_byte = search_start;
669 hole_size = key.offset - last_byte;
670 if (key.offset > last_byte &&
671 hole_size >= num_bytes) {
676 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
680 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
681 last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent);
687 /* we have to make sure we didn't find an extent that has already
688 * been allocated by the map tree or the original allocation
690 BUG_ON(*start < search_start);
692 if (*start + num_bytes > search_end) {
696 /* check for pending inserts here */
700 btrfs_free_path(path);
704 static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
705 struct btrfs_device *device,
709 struct btrfs_path *path;
710 struct btrfs_root *root = device->dev_root;
711 struct btrfs_key key;
712 struct btrfs_key found_key;
713 struct extent_buffer *leaf = NULL;
714 struct btrfs_dev_extent *extent = NULL;
716 path = btrfs_alloc_path();
720 key.objectid = device->devid;
722 key.type = BTRFS_DEV_EXTENT_KEY;
724 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
726 ret = btrfs_previous_item(root, path, key.objectid,
727 BTRFS_DEV_EXTENT_KEY);
729 leaf = path->nodes[0];
730 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
731 extent = btrfs_item_ptr(leaf, path->slots[0],
732 struct btrfs_dev_extent);
733 BUG_ON(found_key.offset > start || found_key.offset +
734 btrfs_dev_extent_length(leaf, extent) < start);
736 } else if (ret == 0) {
737 leaf = path->nodes[0];
738 extent = btrfs_item_ptr(leaf, path->slots[0],
739 struct btrfs_dev_extent);
743 if (device->bytes_used > 0)
744 device->bytes_used -= btrfs_dev_extent_length(leaf, extent);
745 ret = btrfs_del_item(trans, root, path);
748 btrfs_free_path(path);
752 int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
753 struct btrfs_device *device,
754 u64 chunk_tree, u64 chunk_objectid,
755 u64 chunk_offset, u64 start, u64 num_bytes)
758 struct btrfs_path *path;
759 struct btrfs_root *root = device->dev_root;
760 struct btrfs_dev_extent *extent;
761 struct extent_buffer *leaf;
762 struct btrfs_key key;
764 WARN_ON(!device->in_fs_metadata);
765 path = btrfs_alloc_path();
769 key.objectid = device->devid;
771 key.type = BTRFS_DEV_EXTENT_KEY;
772 ret = btrfs_insert_empty_item(trans, root, path, &key,
776 leaf = path->nodes[0];
777 extent = btrfs_item_ptr(leaf, path->slots[0],
778 struct btrfs_dev_extent);
779 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
780 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
781 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
783 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
784 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
787 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
788 btrfs_mark_buffer_dirty(leaf);
789 btrfs_free_path(path);
793 static noinline int find_next_chunk(struct btrfs_root *root,
794 u64 objectid, u64 *offset)
796 struct btrfs_path *path;
798 struct btrfs_key key;
799 struct btrfs_chunk *chunk;
800 struct btrfs_key found_key;
802 path = btrfs_alloc_path();
805 key.objectid = objectid;
806 key.offset = (u64)-1;
807 key.type = BTRFS_CHUNK_ITEM_KEY;
809 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
815 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
819 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
821 if (found_key.objectid != objectid)
824 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
826 *offset = found_key.offset +
827 btrfs_chunk_length(path->nodes[0], chunk);
832 btrfs_free_path(path);
836 static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
839 struct btrfs_key key;
840 struct btrfs_key found_key;
841 struct btrfs_path *path;
843 root = root->fs_info->chunk_root;
845 path = btrfs_alloc_path();
849 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
850 key.type = BTRFS_DEV_ITEM_KEY;
851 key.offset = (u64)-1;
853 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
859 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
864 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
866 *objectid = found_key.offset + 1;
870 btrfs_free_path(path);
875 * the device information is stored in the chunk root
876 * the btrfs_device struct should be fully filled in
878 int btrfs_add_device(struct btrfs_trans_handle *trans,
879 struct btrfs_root *root,
880 struct btrfs_device *device)
883 struct btrfs_path *path;
884 struct btrfs_dev_item *dev_item;
885 struct extent_buffer *leaf;
886 struct btrfs_key key;
889 root = root->fs_info->chunk_root;
891 path = btrfs_alloc_path();
895 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
896 key.type = BTRFS_DEV_ITEM_KEY;
897 key.offset = device->devid;
899 ret = btrfs_insert_empty_item(trans, root, path, &key,
904 leaf = path->nodes[0];
905 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
907 btrfs_set_device_id(leaf, dev_item, device->devid);
908 btrfs_set_device_generation(leaf, dev_item, 0);
909 btrfs_set_device_type(leaf, dev_item, device->type);
910 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
911 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
912 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
913 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
914 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
915 btrfs_set_device_group(leaf, dev_item, 0);
916 btrfs_set_device_seek_speed(leaf, dev_item, 0);
917 btrfs_set_device_bandwidth(leaf, dev_item, 0);
918 btrfs_set_device_start_offset(leaf, dev_item, 0);
920 ptr = (unsigned long)btrfs_device_uuid(dev_item);
921 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
922 ptr = (unsigned long)btrfs_device_fsid(dev_item);
923 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
924 btrfs_mark_buffer_dirty(leaf);
928 btrfs_free_path(path);
932 static int btrfs_rm_dev_item(struct btrfs_root *root,
933 struct btrfs_device *device)
936 struct btrfs_path *path;
937 struct btrfs_key key;
938 struct btrfs_trans_handle *trans;
940 root = root->fs_info->chunk_root;
942 path = btrfs_alloc_path();
946 trans = btrfs_start_transaction(root, 1);
947 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
948 key.type = BTRFS_DEV_ITEM_KEY;
949 key.offset = device->devid;
952 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
961 ret = btrfs_del_item(trans, root, path);
965 btrfs_free_path(path);
967 btrfs_commit_transaction(trans, root);
971 int btrfs_rm_device(struct btrfs_root *root, char *device_path)
973 struct btrfs_device *device;
974 struct btrfs_device *next_device;
975 struct block_device *bdev;
976 struct buffer_head *bh = NULL;
977 struct btrfs_super_block *disk_super;
984 mutex_lock(&uuid_mutex);
985 mutex_lock(&root->fs_info->volume_mutex);
987 all_avail = root->fs_info->avail_data_alloc_bits |
988 root->fs_info->avail_system_alloc_bits |
989 root->fs_info->avail_metadata_alloc_bits;
991 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
992 root->fs_info->fs_devices->rw_devices <= 4) {
993 printk(KERN_ERR "btrfs: unable to go below four devices "
999 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
1000 root->fs_info->fs_devices->rw_devices <= 2) {
1001 printk(KERN_ERR "btrfs: unable to go below two "
1002 "devices on raid1\n");
1007 if (strcmp(device_path, "missing") == 0) {
1008 struct list_head *devices;
1009 struct btrfs_device *tmp;
1012 devices = &root->fs_info->fs_devices->devices;
1013 list_for_each_entry(tmp, devices, dev_list) {
1014 if (tmp->in_fs_metadata && !tmp->bdev) {
1023 printk(KERN_ERR "btrfs: no missing devices found to "
1028 bdev = open_bdev_exclusive(device_path, FMODE_READ,
1029 root->fs_info->bdev_holder);
1031 ret = PTR_ERR(bdev);
1035 set_blocksize(bdev, 4096);
1036 bh = btrfs_read_dev_super(bdev);
1041 disk_super = (struct btrfs_super_block *)bh->b_data;
1042 devid = le64_to_cpu(disk_super->dev_item.devid);
1043 dev_uuid = disk_super->dev_item.uuid;
1044 device = btrfs_find_device(root, devid, dev_uuid,
1052 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
1053 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1059 if (device->writeable) {
1060 list_del_init(&device->dev_alloc_list);
1061 root->fs_info->fs_devices->rw_devices--;
1064 ret = btrfs_shrink_device(device, 0);
1068 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1072 device->in_fs_metadata = 0;
1073 list_del_init(&device->dev_list);
1074 device->fs_devices->num_devices--;
1076 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1077 struct btrfs_device, dev_list);
1078 if (device->bdev == root->fs_info->sb->s_bdev)
1079 root->fs_info->sb->s_bdev = next_device->bdev;
1080 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1081 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1084 close_bdev_exclusive(device->bdev, device->mode);
1085 device->bdev = NULL;
1086 device->fs_devices->open_devices--;
1089 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
1090 btrfs_set_super_num_devices(&root->fs_info->super_copy, num_devices);
1092 if (device->fs_devices->open_devices == 0) {
1093 struct btrfs_fs_devices *fs_devices;
1094 fs_devices = root->fs_info->fs_devices;
1095 while (fs_devices) {
1096 if (fs_devices->seed == device->fs_devices)
1098 fs_devices = fs_devices->seed;
1100 fs_devices->seed = device->fs_devices->seed;
1101 device->fs_devices->seed = NULL;
1102 __btrfs_close_devices(device->fs_devices);
1103 free_fs_devices(device->fs_devices);
1107 * at this point, the device is zero sized. We want to
1108 * remove it from the devices list and zero out the old super
1110 if (device->writeable) {
1111 /* make sure this device isn't detected as part of
1114 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1115 set_buffer_dirty(bh);
1116 sync_dirty_buffer(bh);
1119 kfree(device->name);
1127 close_bdev_exclusive(bdev, FMODE_READ);
1129 mutex_unlock(&root->fs_info->volume_mutex);
1130 mutex_unlock(&uuid_mutex);
1135 * does all the dirty work required for changing file system's UUID.
1137 static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans,
1138 struct btrfs_root *root)
1140 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1141 struct btrfs_fs_devices *old_devices;
1142 struct btrfs_fs_devices *seed_devices;
1143 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
1144 struct btrfs_device *device;
1147 BUG_ON(!mutex_is_locked(&uuid_mutex));
1148 if (!fs_devices->seeding)
1151 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1155 old_devices = clone_fs_devices(fs_devices);
1156 if (IS_ERR(old_devices)) {
1157 kfree(seed_devices);
1158 return PTR_ERR(old_devices);
1161 list_add(&old_devices->list, &fs_uuids);
1163 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1164 seed_devices->opened = 1;
1165 INIT_LIST_HEAD(&seed_devices->devices);
1166 INIT_LIST_HEAD(&seed_devices->alloc_list);
1167 list_splice_init(&fs_devices->devices, &seed_devices->devices);
1168 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1169 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1170 device->fs_devices = seed_devices;
1173 fs_devices->seeding = 0;
1174 fs_devices->num_devices = 0;
1175 fs_devices->open_devices = 0;
1176 fs_devices->seed = seed_devices;
1178 generate_random_uuid(fs_devices->fsid);
1179 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1180 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1181 super_flags = btrfs_super_flags(disk_super) &
1182 ~BTRFS_SUPER_FLAG_SEEDING;
1183 btrfs_set_super_flags(disk_super, super_flags);
1189 * strore the expected generation for seed devices in device items.
1191 static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1192 struct btrfs_root *root)
1194 struct btrfs_path *path;
1195 struct extent_buffer *leaf;
1196 struct btrfs_dev_item *dev_item;
1197 struct btrfs_device *device;
1198 struct btrfs_key key;
1199 u8 fs_uuid[BTRFS_UUID_SIZE];
1200 u8 dev_uuid[BTRFS_UUID_SIZE];
1204 path = btrfs_alloc_path();
1208 root = root->fs_info->chunk_root;
1209 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1211 key.type = BTRFS_DEV_ITEM_KEY;
1214 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1218 leaf = path->nodes[0];
1220 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1221 ret = btrfs_next_leaf(root, path);
1226 leaf = path->nodes[0];
1227 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1228 btrfs_release_path(root, path);
1232 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1233 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1234 key.type != BTRFS_DEV_ITEM_KEY)
1237 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1238 struct btrfs_dev_item);
1239 devid = btrfs_device_id(leaf, dev_item);
1240 read_extent_buffer(leaf, dev_uuid,
1241 (unsigned long)btrfs_device_uuid(dev_item),
1243 read_extent_buffer(leaf, fs_uuid,
1244 (unsigned long)btrfs_device_fsid(dev_item),
1246 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1249 if (device->fs_devices->seeding) {
1250 btrfs_set_device_generation(leaf, dev_item,
1251 device->generation);
1252 btrfs_mark_buffer_dirty(leaf);
1260 btrfs_free_path(path);
1264 int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1266 struct btrfs_trans_handle *trans;
1267 struct btrfs_device *device;
1268 struct block_device *bdev;
1269 struct list_head *devices;
1270 struct super_block *sb = root->fs_info->sb;
1272 int seeding_dev = 0;
1275 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1278 bdev = open_bdev_exclusive(device_path, 0, root->fs_info->bdev_holder);
1282 if (root->fs_info->fs_devices->seeding) {
1284 down_write(&sb->s_umount);
1285 mutex_lock(&uuid_mutex);
1288 filemap_write_and_wait(bdev->bd_inode->i_mapping);
1289 mutex_lock(&root->fs_info->volume_mutex);
1291 devices = &root->fs_info->fs_devices->devices;
1292 list_for_each_entry(device, devices, dev_list) {
1293 if (device->bdev == bdev) {
1299 device = kzalloc(sizeof(*device), GFP_NOFS);
1301 /* we can safely leave the fs_devices entry around */
1306 device->name = kstrdup(device_path, GFP_NOFS);
1307 if (!device->name) {
1313 ret = find_next_devid(root, &device->devid);
1319 trans = btrfs_start_transaction(root, 1);
1322 device->barriers = 1;
1323 device->writeable = 1;
1324 device->work.func = pending_bios_fn;
1325 generate_random_uuid(device->uuid);
1326 spin_lock_init(&device->io_lock);
1327 device->generation = trans->transid;
1328 device->io_width = root->sectorsize;
1329 device->io_align = root->sectorsize;
1330 device->sector_size = root->sectorsize;
1331 device->total_bytes = i_size_read(bdev->bd_inode);
1332 device->dev_root = root->fs_info->dev_root;
1333 device->bdev = bdev;
1334 device->in_fs_metadata = 1;
1336 set_blocksize(device->bdev, 4096);
1339 sb->s_flags &= ~MS_RDONLY;
1340 ret = btrfs_prepare_sprout(trans, root);
1344 device->fs_devices = root->fs_info->fs_devices;
1345 list_add(&device->dev_list, &root->fs_info->fs_devices->devices);
1346 list_add(&device->dev_alloc_list,
1347 &root->fs_info->fs_devices->alloc_list);
1348 root->fs_info->fs_devices->num_devices++;
1349 root->fs_info->fs_devices->open_devices++;
1350 root->fs_info->fs_devices->rw_devices++;
1351 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1353 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
1354 btrfs_set_super_total_bytes(&root->fs_info->super_copy,
1355 total_bytes + device->total_bytes);
1357 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
1358 btrfs_set_super_num_devices(&root->fs_info->super_copy,
1362 ret = init_first_rw_device(trans, root, device);
1364 ret = btrfs_finish_sprout(trans, root);
1367 ret = btrfs_add_device(trans, root, device);
1370 unlock_chunks(root);
1371 btrfs_commit_transaction(trans, root);
1374 mutex_unlock(&uuid_mutex);
1375 up_write(&sb->s_umount);
1377 ret = btrfs_relocate_sys_chunks(root);
1381 mutex_unlock(&root->fs_info->volume_mutex);
1384 close_bdev_exclusive(bdev, 0);
1386 mutex_unlock(&uuid_mutex);
1387 up_write(&sb->s_umount);
1392 static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1393 struct btrfs_device *device)
1396 struct btrfs_path *path;
1397 struct btrfs_root *root;
1398 struct btrfs_dev_item *dev_item;
1399 struct extent_buffer *leaf;
1400 struct btrfs_key key;
1402 root = device->dev_root->fs_info->chunk_root;
1404 path = btrfs_alloc_path();
1408 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1409 key.type = BTRFS_DEV_ITEM_KEY;
1410 key.offset = device->devid;
1412 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1421 leaf = path->nodes[0];
1422 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1424 btrfs_set_device_id(leaf, dev_item, device->devid);
1425 btrfs_set_device_type(leaf, dev_item, device->type);
1426 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1427 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1428 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
1429 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1430 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1431 btrfs_mark_buffer_dirty(leaf);
1434 btrfs_free_path(path);
1438 static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
1439 struct btrfs_device *device, u64 new_size)
1441 struct btrfs_super_block *super_copy =
1442 &device->dev_root->fs_info->super_copy;
1443 u64 old_total = btrfs_super_total_bytes(super_copy);
1444 u64 diff = new_size - device->total_bytes;
1446 if (!device->writeable)
1448 if (new_size <= device->total_bytes)
1451 btrfs_set_super_total_bytes(super_copy, old_total + diff);
1452 device->fs_devices->total_rw_bytes += diff;
1454 device->total_bytes = new_size;
1455 return btrfs_update_device(trans, device);
1458 int btrfs_grow_device(struct btrfs_trans_handle *trans,
1459 struct btrfs_device *device, u64 new_size)
1462 lock_chunks(device->dev_root);
1463 ret = __btrfs_grow_device(trans, device, new_size);
1464 unlock_chunks(device->dev_root);
1468 static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1469 struct btrfs_root *root,
1470 u64 chunk_tree, u64 chunk_objectid,
1474 struct btrfs_path *path;
1475 struct btrfs_key key;
1477 root = root->fs_info->chunk_root;
1478 path = btrfs_alloc_path();
1482 key.objectid = chunk_objectid;
1483 key.offset = chunk_offset;
1484 key.type = BTRFS_CHUNK_ITEM_KEY;
1486 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1489 ret = btrfs_del_item(trans, root, path);
1492 btrfs_free_path(path);
1496 static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
1499 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1500 struct btrfs_disk_key *disk_key;
1501 struct btrfs_chunk *chunk;
1508 struct btrfs_key key;
1510 array_size = btrfs_super_sys_array_size(super_copy);
1512 ptr = super_copy->sys_chunk_array;
1515 while (cur < array_size) {
1516 disk_key = (struct btrfs_disk_key *)ptr;
1517 btrfs_disk_key_to_cpu(&key, disk_key);
1519 len = sizeof(*disk_key);
1521 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1522 chunk = (struct btrfs_chunk *)(ptr + len);
1523 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1524 len += btrfs_chunk_item_size(num_stripes);
1529 if (key.objectid == chunk_objectid &&
1530 key.offset == chunk_offset) {
1531 memmove(ptr, ptr + len, array_size - (cur + len));
1533 btrfs_set_super_sys_array_size(super_copy, array_size);
1542 static int btrfs_relocate_chunk(struct btrfs_root *root,
1543 u64 chunk_tree, u64 chunk_objectid,
1546 struct extent_map_tree *em_tree;
1547 struct btrfs_root *extent_root;
1548 struct btrfs_trans_handle *trans;
1549 struct extent_map *em;
1550 struct map_lookup *map;
1554 printk(KERN_INFO "btrfs relocating chunk %llu\n",
1555 (unsigned long long)chunk_offset);
1556 root = root->fs_info->chunk_root;
1557 extent_root = root->fs_info->extent_root;
1558 em_tree = &root->fs_info->mapping_tree.map_tree;
1560 /* step one, relocate all the extents inside this chunk */
1561 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
1564 trans = btrfs_start_transaction(root, 1);
1570 * step two, delete the device extents and the
1571 * chunk tree entries
1573 spin_lock(&em_tree->lock);
1574 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
1575 spin_unlock(&em_tree->lock);
1577 BUG_ON(em->start > chunk_offset ||
1578 em->start + em->len < chunk_offset);
1579 map = (struct map_lookup *)em->bdev;
1581 for (i = 0; i < map->num_stripes; i++) {
1582 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1583 map->stripes[i].physical);
1586 if (map->stripes[i].dev) {
1587 ret = btrfs_update_device(trans, map->stripes[i].dev);
1591 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1596 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1597 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1601 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
1604 spin_lock(&em_tree->lock);
1605 remove_extent_mapping(em_tree, em);
1606 spin_unlock(&em_tree->lock);
1611 /* once for the tree */
1612 free_extent_map(em);
1614 free_extent_map(em);
1616 unlock_chunks(root);
1617 btrfs_end_transaction(trans, root);
1621 static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
1623 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1624 struct btrfs_path *path;
1625 struct extent_buffer *leaf;
1626 struct btrfs_chunk *chunk;
1627 struct btrfs_key key;
1628 struct btrfs_key found_key;
1629 u64 chunk_tree = chunk_root->root_key.objectid;
1633 path = btrfs_alloc_path();
1637 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1638 key.offset = (u64)-1;
1639 key.type = BTRFS_CHUNK_ITEM_KEY;
1642 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1647 ret = btrfs_previous_item(chunk_root, path, key.objectid,
1654 leaf = path->nodes[0];
1655 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1657 chunk = btrfs_item_ptr(leaf, path->slots[0],
1658 struct btrfs_chunk);
1659 chunk_type = btrfs_chunk_type(leaf, chunk);
1660 btrfs_release_path(chunk_root, path);
1662 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
1663 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
1669 if (found_key.offset == 0)
1671 key.offset = found_key.offset - 1;
1675 btrfs_free_path(path);
1679 static u64 div_factor(u64 num, int factor)
1688 int btrfs_balance(struct btrfs_root *dev_root)
1691 struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
1692 struct btrfs_device *device;
1695 struct btrfs_path *path;
1696 struct btrfs_key key;
1697 struct btrfs_chunk *chunk;
1698 struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
1699 struct btrfs_trans_handle *trans;
1700 struct btrfs_key found_key;
1702 if (dev_root->fs_info->sb->s_flags & MS_RDONLY)
1705 mutex_lock(&dev_root->fs_info->volume_mutex);
1706 dev_root = dev_root->fs_info->dev_root;
1708 /* step one make some room on all the devices */
1709 list_for_each_entry(device, devices, dev_list) {
1710 old_size = device->total_bytes;
1711 size_to_free = div_factor(old_size, 1);
1712 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
1713 if (!device->writeable ||
1714 device->total_bytes - device->bytes_used > size_to_free)
1717 ret = btrfs_shrink_device(device, old_size - size_to_free);
1720 trans = btrfs_start_transaction(dev_root, 1);
1723 ret = btrfs_grow_device(trans, device, old_size);
1726 btrfs_end_transaction(trans, dev_root);
1729 /* step two, relocate all the chunks */
1730 path = btrfs_alloc_path();
1733 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1734 key.offset = (u64)-1;
1735 key.type = BTRFS_CHUNK_ITEM_KEY;
1738 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1743 * this shouldn't happen, it means the last relocate
1749 ret = btrfs_previous_item(chunk_root, path, 0,
1750 BTRFS_CHUNK_ITEM_KEY);
1754 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1756 if (found_key.objectid != key.objectid)
1759 chunk = btrfs_item_ptr(path->nodes[0],
1761 struct btrfs_chunk);
1762 key.offset = found_key.offset;
1763 /* chunk zero is special */
1764 if (key.offset == 0)
1767 btrfs_release_path(chunk_root, path);
1768 ret = btrfs_relocate_chunk(chunk_root,
1769 chunk_root->root_key.objectid,
1776 btrfs_free_path(path);
1777 mutex_unlock(&dev_root->fs_info->volume_mutex);
1782 * shrinking a device means finding all of the device extents past
1783 * the new size, and then following the back refs to the chunks.
1784 * The chunk relocation code actually frees the device extent
1786 int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
1788 struct btrfs_trans_handle *trans;
1789 struct btrfs_root *root = device->dev_root;
1790 struct btrfs_dev_extent *dev_extent = NULL;
1791 struct btrfs_path *path;
1798 struct extent_buffer *l;
1799 struct btrfs_key key;
1800 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1801 u64 old_total = btrfs_super_total_bytes(super_copy);
1802 u64 diff = device->total_bytes - new_size;
1804 if (new_size >= device->total_bytes)
1807 path = btrfs_alloc_path();
1811 trans = btrfs_start_transaction(root, 1);
1821 device->total_bytes = new_size;
1822 if (device->writeable)
1823 device->fs_devices->total_rw_bytes -= diff;
1824 ret = btrfs_update_device(trans, device);
1826 unlock_chunks(root);
1827 btrfs_end_transaction(trans, root);
1830 WARN_ON(diff > old_total);
1831 btrfs_set_super_total_bytes(super_copy, old_total - diff);
1832 unlock_chunks(root);
1833 btrfs_end_transaction(trans, root);
1835 key.objectid = device->devid;
1836 key.offset = (u64)-1;
1837 key.type = BTRFS_DEV_EXTENT_KEY;
1840 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1844 ret = btrfs_previous_item(root, path, 0, key.type);
1853 slot = path->slots[0];
1854 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
1856 if (key.objectid != device->devid)
1859 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
1860 length = btrfs_dev_extent_length(l, dev_extent);
1862 if (key.offset + length <= new_size)
1865 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
1866 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
1867 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
1868 btrfs_release_path(root, path);
1870 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
1877 btrfs_free_path(path);
1881 static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
1882 struct btrfs_root *root,
1883 struct btrfs_key *key,
1884 struct btrfs_chunk *chunk, int item_size)
1886 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1887 struct btrfs_disk_key disk_key;
1891 array_size = btrfs_super_sys_array_size(super_copy);
1892 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
1895 ptr = super_copy->sys_chunk_array + array_size;
1896 btrfs_cpu_key_to_disk(&disk_key, key);
1897 memcpy(ptr, &disk_key, sizeof(disk_key));
1898 ptr += sizeof(disk_key);
1899 memcpy(ptr, chunk, item_size);
1900 item_size += sizeof(disk_key);
1901 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
1905 static noinline u64 chunk_bytes_by_type(u64 type, u64 calc_size,
1906 int num_stripes, int sub_stripes)
1908 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
1910 else if (type & BTRFS_BLOCK_GROUP_RAID10)
1911 return calc_size * (num_stripes / sub_stripes);
1913 return calc_size * num_stripes;
1916 static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
1917 struct btrfs_root *extent_root,
1918 struct map_lookup **map_ret,
1919 u64 *num_bytes, u64 *stripe_size,
1920 u64 start, u64 type)
1922 struct btrfs_fs_info *info = extent_root->fs_info;
1923 struct btrfs_device *device = NULL;
1924 struct btrfs_fs_devices *fs_devices = info->fs_devices;
1925 struct list_head *cur;
1926 struct map_lookup *map = NULL;
1927 struct extent_map_tree *em_tree;
1928 struct extent_map *em;
1929 struct list_head private_devs;
1930 int min_stripe_size = 1 * 1024 * 1024;
1931 u64 calc_size = 1024 * 1024 * 1024;
1932 u64 max_chunk_size = calc_size;
1937 int num_stripes = 1;
1938 int min_stripes = 1;
1939 int sub_stripes = 0;
1943 int stripe_len = 64 * 1024;
1945 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
1946 (type & BTRFS_BLOCK_GROUP_DUP)) {
1948 type &= ~BTRFS_BLOCK_GROUP_DUP;
1950 if (list_empty(&fs_devices->alloc_list))
1953 if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
1954 num_stripes = fs_devices->rw_devices;
1957 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
1961 if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
1962 num_stripes = min_t(u64, 2, fs_devices->rw_devices);
1963 if (num_stripes < 2)
1967 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
1968 num_stripes = fs_devices->rw_devices;
1969 if (num_stripes < 4)
1971 num_stripes &= ~(u32)1;
1976 if (type & BTRFS_BLOCK_GROUP_DATA) {
1977 max_chunk_size = 10 * calc_size;
1978 min_stripe_size = 64 * 1024 * 1024;
1979 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
1980 max_chunk_size = 4 * calc_size;
1981 min_stripe_size = 32 * 1024 * 1024;
1982 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
1983 calc_size = 8 * 1024 * 1024;
1984 max_chunk_size = calc_size * 2;
1985 min_stripe_size = 1 * 1024 * 1024;
1988 /* we don't want a chunk larger than 10% of writeable space */
1989 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
1993 if (!map || map->num_stripes != num_stripes) {
1995 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
1998 map->num_stripes = num_stripes;
2001 if (calc_size * num_stripes > max_chunk_size) {
2002 calc_size = max_chunk_size;
2003 do_div(calc_size, num_stripes);
2004 do_div(calc_size, stripe_len);
2005 calc_size *= stripe_len;
2007 /* we don't want tiny stripes */
2008 calc_size = max_t(u64, min_stripe_size, calc_size);
2010 do_div(calc_size, stripe_len);
2011 calc_size *= stripe_len;
2013 cur = fs_devices->alloc_list.next;
2016 if (type & BTRFS_BLOCK_GROUP_DUP)
2017 min_free = calc_size * 2;
2019 min_free = calc_size;
2022 * we add 1MB because we never use the first 1MB of the device, unless
2023 * we've looped, then we are likely allocating the maximum amount of
2024 * space left already
2027 min_free += 1024 * 1024;
2029 INIT_LIST_HEAD(&private_devs);
2030 while (index < num_stripes) {
2031 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
2032 BUG_ON(!device->writeable);
2033 if (device->total_bytes > device->bytes_used)
2034 avail = device->total_bytes - device->bytes_used;
2039 if (device->in_fs_metadata && avail >= min_free) {
2040 ret = find_free_dev_extent(trans, device,
2041 min_free, &dev_offset);
2043 list_move_tail(&device->dev_alloc_list,
2045 map->stripes[index].dev = device;
2046 map->stripes[index].physical = dev_offset;
2048 if (type & BTRFS_BLOCK_GROUP_DUP) {
2049 map->stripes[index].dev = device;
2050 map->stripes[index].physical =
2051 dev_offset + calc_size;
2055 } else if (device->in_fs_metadata && avail > max_avail)
2057 if (cur == &fs_devices->alloc_list)
2060 list_splice(&private_devs, &fs_devices->alloc_list);
2061 if (index < num_stripes) {
2062 if (index >= min_stripes) {
2063 num_stripes = index;
2064 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2065 num_stripes /= sub_stripes;
2066 num_stripes *= sub_stripes;
2071 if (!looped && max_avail > 0) {
2073 calc_size = max_avail;
2079 map->sector_size = extent_root->sectorsize;
2080 map->stripe_len = stripe_len;
2081 map->io_align = stripe_len;
2082 map->io_width = stripe_len;
2084 map->num_stripes = num_stripes;
2085 map->sub_stripes = sub_stripes;
2088 *stripe_size = calc_size;
2089 *num_bytes = chunk_bytes_by_type(type, calc_size,
2090 num_stripes, sub_stripes);
2092 em = alloc_extent_map(GFP_NOFS);
2097 em->bdev = (struct block_device *)map;
2099 em->len = *num_bytes;
2100 em->block_start = 0;
2101 em->block_len = em->len;
2103 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
2104 spin_lock(&em_tree->lock);
2105 ret = add_extent_mapping(em_tree, em);
2106 spin_unlock(&em_tree->lock);
2108 free_extent_map(em);
2110 ret = btrfs_make_block_group(trans, extent_root, 0, type,
2111 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2116 while (index < map->num_stripes) {
2117 device = map->stripes[index].dev;
2118 dev_offset = map->stripes[index].physical;
2120 ret = btrfs_alloc_dev_extent(trans, device,
2121 info->chunk_root->root_key.objectid,
2122 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2123 start, dev_offset, calc_size);
2131 static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
2132 struct btrfs_root *extent_root,
2133 struct map_lookup *map, u64 chunk_offset,
2134 u64 chunk_size, u64 stripe_size)
2137 struct btrfs_key key;
2138 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2139 struct btrfs_device *device;
2140 struct btrfs_chunk *chunk;
2141 struct btrfs_stripe *stripe;
2142 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
2146 chunk = kzalloc(item_size, GFP_NOFS);
2151 while (index < map->num_stripes) {
2152 device = map->stripes[index].dev;
2153 device->bytes_used += stripe_size;
2154 ret = btrfs_update_device(trans, device);
2160 stripe = &chunk->stripe;
2161 while (index < map->num_stripes) {
2162 device = map->stripes[index].dev;
2163 dev_offset = map->stripes[index].physical;
2165 btrfs_set_stack_stripe_devid(stripe, device->devid);
2166 btrfs_set_stack_stripe_offset(stripe, dev_offset);
2167 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
2172 btrfs_set_stack_chunk_length(chunk, chunk_size);
2173 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
2174 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
2175 btrfs_set_stack_chunk_type(chunk, map->type);
2176 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
2177 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
2178 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
2179 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
2180 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
2182 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2183 key.type = BTRFS_CHUNK_ITEM_KEY;
2184 key.offset = chunk_offset;
2186 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
2189 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2190 ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
2199 * Chunk allocation falls into two parts. The first part does works
2200 * that make the new allocated chunk useable, but not do any operation
2201 * that modifies the chunk tree. The second part does the works that
2202 * require modifying the chunk tree. This division is important for the
2203 * bootstrap process of adding storage to a seed btrfs.
2205 int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2206 struct btrfs_root *extent_root, u64 type)
2211 struct map_lookup *map;
2212 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2215 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2220 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2221 &stripe_size, chunk_offset, type);
2225 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2226 chunk_size, stripe_size);
2231 static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
2232 struct btrfs_root *root,
2233 struct btrfs_device *device)
2236 u64 sys_chunk_offset;
2240 u64 sys_stripe_size;
2242 struct map_lookup *map;
2243 struct map_lookup *sys_map;
2244 struct btrfs_fs_info *fs_info = root->fs_info;
2245 struct btrfs_root *extent_root = fs_info->extent_root;
2248 ret = find_next_chunk(fs_info->chunk_root,
2249 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
2252 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
2253 (fs_info->metadata_alloc_profile &
2254 fs_info->avail_metadata_alloc_bits);
2255 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2257 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2258 &stripe_size, chunk_offset, alloc_profile);
2261 sys_chunk_offset = chunk_offset + chunk_size;
2263 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
2264 (fs_info->system_alloc_profile &
2265 fs_info->avail_system_alloc_bits);
2266 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2268 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
2269 &sys_chunk_size, &sys_stripe_size,
2270 sys_chunk_offset, alloc_profile);
2273 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
2277 * Modifying chunk tree needs allocating new blocks from both
2278 * system block group and metadata block group. So we only can
2279 * do operations require modifying the chunk tree after both
2280 * block groups were created.
2282 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2283 chunk_size, stripe_size);
2286 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
2287 sys_chunk_offset, sys_chunk_size,
2293 int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
2295 struct extent_map *em;
2296 struct map_lookup *map;
2297 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2301 spin_lock(&map_tree->map_tree.lock);
2302 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
2303 spin_unlock(&map_tree->map_tree.lock);
2307 map = (struct map_lookup *)em->bdev;
2308 for (i = 0; i < map->num_stripes; i++) {
2309 if (!map->stripes[i].dev->writeable) {
2314 free_extent_map(em);
2318 void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
2320 extent_map_tree_init(&tree->map_tree, GFP_NOFS);
2323 void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
2325 struct extent_map *em;
2328 spin_lock(&tree->map_tree.lock);
2329 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
2331 remove_extent_mapping(&tree->map_tree, em);
2332 spin_unlock(&tree->map_tree.lock);
2337 free_extent_map(em);
2338 /* once for the tree */
2339 free_extent_map(em);
2343 int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
2345 struct extent_map *em;
2346 struct map_lookup *map;
2347 struct extent_map_tree *em_tree = &map_tree->map_tree;
2350 spin_lock(&em_tree->lock);
2351 em = lookup_extent_mapping(em_tree, logical, len);
2352 spin_unlock(&em_tree->lock);
2355 BUG_ON(em->start > logical || em->start + em->len < logical);
2356 map = (struct map_lookup *)em->bdev;
2357 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
2358 ret = map->num_stripes;
2359 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2360 ret = map->sub_stripes;
2363 free_extent_map(em);
2367 static int find_live_mirror(struct map_lookup *map, int first, int num,
2371 if (map->stripes[optimal].dev->bdev)
2373 for (i = first; i < first + num; i++) {
2374 if (map->stripes[i].dev->bdev)
2377 /* we couldn't find one that doesn't fail. Just return something
2378 * and the io error handling code will clean up eventually
2383 static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2384 u64 logical, u64 *length,
2385 struct btrfs_multi_bio **multi_ret,
2386 int mirror_num, struct page *unplug_page)
2388 struct extent_map *em;
2389 struct map_lookup *map;
2390 struct extent_map_tree *em_tree = &map_tree->map_tree;
2394 int stripes_allocated = 8;
2395 int stripes_required = 1;
2400 struct btrfs_multi_bio *multi = NULL;
2402 if (multi_ret && !(rw & (1 << BIO_RW)))
2403 stripes_allocated = 1;
2406 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
2411 atomic_set(&multi->error, 0);
2414 spin_lock(&em_tree->lock);
2415 em = lookup_extent_mapping(em_tree, logical, *length);
2416 spin_unlock(&em_tree->lock);
2418 if (!em && unplug_page)
2422 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
2423 (unsigned long long)logical,
2424 (unsigned long long)*length);
2428 BUG_ON(em->start > logical || em->start + em->len < logical);
2429 map = (struct map_lookup *)em->bdev;
2430 offset = logical - em->start;
2432 if (mirror_num > map->num_stripes)
2435 /* if our multi bio struct is too small, back off and try again */
2436 if (rw & (1 << BIO_RW)) {
2437 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
2438 BTRFS_BLOCK_GROUP_DUP)) {
2439 stripes_required = map->num_stripes;
2441 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2442 stripes_required = map->sub_stripes;
2446 if (multi_ret && rw == WRITE &&
2447 stripes_allocated < stripes_required) {
2448 stripes_allocated = map->num_stripes;
2449 free_extent_map(em);
2455 * stripe_nr counts the total number of stripes we have to stride
2456 * to get to this block
2458 do_div(stripe_nr, map->stripe_len);
2460 stripe_offset = stripe_nr * map->stripe_len;
2461 BUG_ON(offset < stripe_offset);
2463 /* stripe_offset is the offset of this block in its stripe*/
2464 stripe_offset = offset - stripe_offset;
2466 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
2467 BTRFS_BLOCK_GROUP_RAID10 |
2468 BTRFS_BLOCK_GROUP_DUP)) {
2469 /* we limit the length of each bio to what fits in a stripe */
2470 *length = min_t(u64, em->len - offset,
2471 map->stripe_len - stripe_offset);
2473 *length = em->len - offset;
2476 if (!multi_ret && !unplug_page)
2481 if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
2482 if (unplug_page || (rw & (1 << BIO_RW)))
2483 num_stripes = map->num_stripes;
2484 else if (mirror_num)
2485 stripe_index = mirror_num - 1;
2487 stripe_index = find_live_mirror(map, 0,
2489 current->pid % map->num_stripes);
2492 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
2493 if (rw & (1 << BIO_RW))
2494 num_stripes = map->num_stripes;
2495 else if (mirror_num)
2496 stripe_index = mirror_num - 1;
2498 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2499 int factor = map->num_stripes / map->sub_stripes;
2501 stripe_index = do_div(stripe_nr, factor);
2502 stripe_index *= map->sub_stripes;
2504 if (unplug_page || (rw & (1 << BIO_RW)))
2505 num_stripes = map->sub_stripes;
2506 else if (mirror_num)
2507 stripe_index += mirror_num - 1;
2509 stripe_index = find_live_mirror(map, stripe_index,
2510 map->sub_stripes, stripe_index +
2511 current->pid % map->sub_stripes);
2515 * after this do_div call, stripe_nr is the number of stripes
2516 * on this device we have to walk to find the data, and
2517 * stripe_index is the number of our device in the stripe array
2519 stripe_index = do_div(stripe_nr, map->num_stripes);
2521 BUG_ON(stripe_index >= map->num_stripes);
2523 for (i = 0; i < num_stripes; i++) {
2525 struct btrfs_device *device;
2526 struct backing_dev_info *bdi;
2528 device = map->stripes[stripe_index].dev;
2530 bdi = blk_get_backing_dev_info(device->bdev);
2531 if (bdi->unplug_io_fn)
2532 bdi->unplug_io_fn(bdi, unplug_page);
2535 multi->stripes[i].physical =
2536 map->stripes[stripe_index].physical +
2537 stripe_offset + stripe_nr * map->stripe_len;
2538 multi->stripes[i].dev = map->stripes[stripe_index].dev;
2544 multi->num_stripes = num_stripes;
2545 multi->max_errors = max_errors;
2548 free_extent_map(em);
2552 int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2553 u64 logical, u64 *length,
2554 struct btrfs_multi_bio **multi_ret, int mirror_num)
2556 return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
2560 int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
2561 u64 chunk_start, u64 physical, u64 devid,
2562 u64 **logical, int *naddrs, int *stripe_len)
2564 struct extent_map_tree *em_tree = &map_tree->map_tree;
2565 struct extent_map *em;
2566 struct map_lookup *map;
2573 spin_lock(&em_tree->lock);
2574 em = lookup_extent_mapping(em_tree, chunk_start, 1);
2575 spin_unlock(&em_tree->lock);
2577 BUG_ON(!em || em->start != chunk_start);
2578 map = (struct map_lookup *)em->bdev;
2581 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2582 do_div(length, map->num_stripes / map->sub_stripes);
2583 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
2584 do_div(length, map->num_stripes);
2586 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
2589 for (i = 0; i < map->num_stripes; i++) {
2590 if (devid && map->stripes[i].dev->devid != devid)
2592 if (map->stripes[i].physical > physical ||
2593 map->stripes[i].physical + length <= physical)
2596 stripe_nr = physical - map->stripes[i].physical;
2597 do_div(stripe_nr, map->stripe_len);
2599 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2600 stripe_nr = stripe_nr * map->num_stripes + i;
2601 do_div(stripe_nr, map->sub_stripes);
2602 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
2603 stripe_nr = stripe_nr * map->num_stripes + i;
2605 bytenr = chunk_start + stripe_nr * map->stripe_len;
2606 WARN_ON(nr >= map->num_stripes);
2607 for (j = 0; j < nr; j++) {
2608 if (buf[j] == bytenr)
2612 WARN_ON(nr >= map->num_stripes);
2617 for (i = 0; i > nr; i++) {
2618 struct btrfs_multi_bio *multi;
2619 struct btrfs_bio_stripe *stripe;
2623 ret = btrfs_map_block(map_tree, WRITE, buf[i],
2624 &length, &multi, 0);
2627 stripe = multi->stripes;
2628 for (j = 0; j < multi->num_stripes; j++) {
2629 if (stripe->physical >= physical &&
2630 physical < stripe->physical + length)
2633 BUG_ON(j >= multi->num_stripes);
2639 *stripe_len = map->stripe_len;
2641 free_extent_map(em);
2645 int btrfs_unplug_page(struct btrfs_mapping_tree *map_tree,
2646 u64 logical, struct page *page)
2648 u64 length = PAGE_CACHE_SIZE;
2649 return __btrfs_map_block(map_tree, READ, logical, &length,
2653 static void end_bio_multi_stripe(struct bio *bio, int err)
2655 struct btrfs_multi_bio *multi = bio->bi_private;
2656 int is_orig_bio = 0;
2659 atomic_inc(&multi->error);
2661 if (bio == multi->orig_bio)
2664 if (atomic_dec_and_test(&multi->stripes_pending)) {
2667 bio = multi->orig_bio;
2669 bio->bi_private = multi->private;
2670 bio->bi_end_io = multi->end_io;
2671 /* only send an error to the higher layers if it is
2672 * beyond the tolerance of the multi-bio
2674 if (atomic_read(&multi->error) > multi->max_errors) {
2678 * this bio is actually up to date, we didn't
2679 * go over the max number of errors
2681 set_bit(BIO_UPTODATE, &bio->bi_flags);
2686 bio_endio(bio, err);
2687 } else if (!is_orig_bio) {
2692 struct async_sched {
2695 struct btrfs_fs_info *info;
2696 struct btrfs_work work;
2700 * see run_scheduled_bios for a description of why bios are collected for
2703 * This will add one bio to the pending list for a device and make sure
2704 * the work struct is scheduled.
2706 static noinline int schedule_bio(struct btrfs_root *root,
2707 struct btrfs_device *device,
2708 int rw, struct bio *bio)
2710 int should_queue = 1;
2712 /* don't bother with additional async steps for reads, right now */
2713 if (!(rw & (1 << BIO_RW))) {
2715 submit_bio(rw, bio);
2721 * nr_async_bios allows us to reliably return congestion to the
2722 * higher layers. Otherwise, the async bio makes it appear we have
2723 * made progress against dirty pages when we've really just put it
2724 * on a queue for later
2726 atomic_inc(&root->fs_info->nr_async_bios);
2727 WARN_ON(bio->bi_next);
2728 bio->bi_next = NULL;
2731 spin_lock(&device->io_lock);
2733 if (device->pending_bio_tail)
2734 device->pending_bio_tail->bi_next = bio;
2736 device->pending_bio_tail = bio;
2737 if (!device->pending_bios)
2738 device->pending_bios = bio;
2739 if (device->running_pending)
2742 spin_unlock(&device->io_lock);
2745 btrfs_queue_worker(&root->fs_info->submit_workers,
2750 int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
2751 int mirror_num, int async_submit)
2753 struct btrfs_mapping_tree *map_tree;
2754 struct btrfs_device *dev;
2755 struct bio *first_bio = bio;
2756 u64 logical = (u64)bio->bi_sector << 9;
2759 struct btrfs_multi_bio *multi = NULL;
2764 length = bio->bi_size;
2765 map_tree = &root->fs_info->mapping_tree;
2766 map_length = length;
2768 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
2772 total_devs = multi->num_stripes;
2773 if (map_length < length) {
2774 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
2775 "len %llu\n", (unsigned long long)logical,
2776 (unsigned long long)length,
2777 (unsigned long long)map_length);
2780 multi->end_io = first_bio->bi_end_io;
2781 multi->private = first_bio->bi_private;
2782 multi->orig_bio = first_bio;
2783 atomic_set(&multi->stripes_pending, multi->num_stripes);
2785 while (dev_nr < total_devs) {
2786 if (total_devs > 1) {
2787 if (dev_nr < total_devs - 1) {
2788 bio = bio_clone(first_bio, GFP_NOFS);
2793 bio->bi_private = multi;
2794 bio->bi_end_io = end_bio_multi_stripe;
2796 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
2797 dev = multi->stripes[dev_nr].dev;
2798 BUG_ON(rw == WRITE && !dev->writeable);
2799 if (dev && dev->bdev) {
2800 bio->bi_bdev = dev->bdev;
2802 schedule_bio(root, dev, rw, bio);
2804 submit_bio(rw, bio);
2806 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
2807 bio->bi_sector = logical >> 9;
2808 bio_endio(bio, -EIO);
2812 if (total_devs == 1)
2817 struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
2820 struct btrfs_device *device;
2821 struct btrfs_fs_devices *cur_devices;
2823 cur_devices = root->fs_info->fs_devices;
2824 while (cur_devices) {
2826 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
2827 device = __find_device(&cur_devices->devices,
2832 cur_devices = cur_devices->seed;
2837 static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
2838 u64 devid, u8 *dev_uuid)
2840 struct btrfs_device *device;
2841 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2843 device = kzalloc(sizeof(*device), GFP_NOFS);
2846 list_add(&device->dev_list,
2847 &fs_devices->devices);
2848 device->barriers = 1;
2849 device->dev_root = root->fs_info->dev_root;
2850 device->devid = devid;
2851 device->work.func = pending_bios_fn;
2852 device->fs_devices = fs_devices;
2853 fs_devices->num_devices++;
2854 spin_lock_init(&device->io_lock);
2855 INIT_LIST_HEAD(&device->dev_alloc_list);
2856 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
2860 static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
2861 struct extent_buffer *leaf,
2862 struct btrfs_chunk *chunk)
2864 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2865 struct map_lookup *map;
2866 struct extent_map *em;
2870 u8 uuid[BTRFS_UUID_SIZE];
2875 logical = key->offset;
2876 length = btrfs_chunk_length(leaf, chunk);
2878 spin_lock(&map_tree->map_tree.lock);
2879 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
2880 spin_unlock(&map_tree->map_tree.lock);
2882 /* already mapped? */
2883 if (em && em->start <= logical && em->start + em->len > logical) {
2884 free_extent_map(em);
2887 free_extent_map(em);
2890 map = kzalloc(sizeof(*map), GFP_NOFS);
2894 em = alloc_extent_map(GFP_NOFS);
2897 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2898 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
2900 free_extent_map(em);
2904 em->bdev = (struct block_device *)map;
2905 em->start = logical;
2907 em->block_start = 0;
2908 em->block_len = em->len;
2910 map->num_stripes = num_stripes;
2911 map->io_width = btrfs_chunk_io_width(leaf, chunk);
2912 map->io_align = btrfs_chunk_io_align(leaf, chunk);
2913 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
2914 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
2915 map->type = btrfs_chunk_type(leaf, chunk);
2916 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
2917 for (i = 0; i < num_stripes; i++) {
2918 map->stripes[i].physical =
2919 btrfs_stripe_offset_nr(leaf, chunk, i);
2920 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
2921 read_extent_buffer(leaf, uuid, (unsigned long)
2922 btrfs_stripe_dev_uuid_nr(chunk, i),
2924 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
2926 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
2928 free_extent_map(em);
2931 if (!map->stripes[i].dev) {
2932 map->stripes[i].dev =
2933 add_missing_dev(root, devid, uuid);
2934 if (!map->stripes[i].dev) {
2936 free_extent_map(em);
2940 map->stripes[i].dev->in_fs_metadata = 1;
2943 spin_lock(&map_tree->map_tree.lock);
2944 ret = add_extent_mapping(&map_tree->map_tree, em);
2945 spin_unlock(&map_tree->map_tree.lock);
2947 free_extent_map(em);
2952 static int fill_device_from_item(struct extent_buffer *leaf,
2953 struct btrfs_dev_item *dev_item,
2954 struct btrfs_device *device)
2958 device->devid = btrfs_device_id(leaf, dev_item);
2959 device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
2960 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
2961 device->type = btrfs_device_type(leaf, dev_item);
2962 device->io_align = btrfs_device_io_align(leaf, dev_item);
2963 device->io_width = btrfs_device_io_width(leaf, dev_item);
2964 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
2966 ptr = (unsigned long)btrfs_device_uuid(dev_item);
2967 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
2972 static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
2974 struct btrfs_fs_devices *fs_devices;
2977 mutex_lock(&uuid_mutex);
2979 fs_devices = root->fs_info->fs_devices->seed;
2980 while (fs_devices) {
2981 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
2985 fs_devices = fs_devices->seed;
2988 fs_devices = find_fsid(fsid);
2994 fs_devices = clone_fs_devices(fs_devices);
2995 if (IS_ERR(fs_devices)) {
2996 ret = PTR_ERR(fs_devices);
3000 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
3001 root->fs_info->bdev_holder);
3005 if (!fs_devices->seeding) {
3006 __btrfs_close_devices(fs_devices);
3007 free_fs_devices(fs_devices);
3012 fs_devices->seed = root->fs_info->fs_devices->seed;
3013 root->fs_info->fs_devices->seed = fs_devices;
3015 mutex_unlock(&uuid_mutex);
3019 static int read_one_dev(struct btrfs_root *root,
3020 struct extent_buffer *leaf,
3021 struct btrfs_dev_item *dev_item)
3023 struct btrfs_device *device;
3026 u8 fs_uuid[BTRFS_UUID_SIZE];
3027 u8 dev_uuid[BTRFS_UUID_SIZE];
3029 devid = btrfs_device_id(leaf, dev_item);
3030 read_extent_buffer(leaf, dev_uuid,
3031 (unsigned long)btrfs_device_uuid(dev_item),
3033 read_extent_buffer(leaf, fs_uuid,
3034 (unsigned long)btrfs_device_fsid(dev_item),
3037 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
3038 ret = open_seed_devices(root, fs_uuid);
3039 if (ret && !btrfs_test_opt(root, DEGRADED))
3043 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
3044 if (!device || !device->bdev) {
3045 if (!btrfs_test_opt(root, DEGRADED))
3049 printk(KERN_WARNING "warning devid %llu missing\n",
3050 (unsigned long long)devid);
3051 device = add_missing_dev(root, devid, dev_uuid);
3057 if (device->fs_devices != root->fs_info->fs_devices) {
3058 BUG_ON(device->writeable);
3059 if (device->generation !=
3060 btrfs_device_generation(leaf, dev_item))
3064 fill_device_from_item(leaf, dev_item, device);
3065 device->dev_root = root->fs_info->dev_root;
3066 device->in_fs_metadata = 1;
3067 if (device->writeable)
3068 device->fs_devices->total_rw_bytes += device->total_bytes;
3073 int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
3075 struct btrfs_dev_item *dev_item;
3077 dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
3079 return read_one_dev(root, buf, dev_item);
3082 int btrfs_read_sys_array(struct btrfs_root *root)
3084 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
3085 struct extent_buffer *sb;
3086 struct btrfs_disk_key *disk_key;
3087 struct btrfs_chunk *chunk;
3089 unsigned long sb_ptr;
3095 struct btrfs_key key;
3097 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
3098 BTRFS_SUPER_INFO_SIZE);
3101 btrfs_set_buffer_uptodate(sb);
3102 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
3103 array_size = btrfs_super_sys_array_size(super_copy);
3105 ptr = super_copy->sys_chunk_array;
3106 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
3109 while (cur < array_size) {
3110 disk_key = (struct btrfs_disk_key *)ptr;
3111 btrfs_disk_key_to_cpu(&key, disk_key);
3113 len = sizeof(*disk_key); ptr += len;
3117 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
3118 chunk = (struct btrfs_chunk *)sb_ptr;
3119 ret = read_one_chunk(root, &key, sb, chunk);
3122 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
3123 len = btrfs_chunk_item_size(num_stripes);
3132 free_extent_buffer(sb);
3136 int btrfs_read_chunk_tree(struct btrfs_root *root)
3138 struct btrfs_path *path;
3139 struct extent_buffer *leaf;
3140 struct btrfs_key key;
3141 struct btrfs_key found_key;
3145 root = root->fs_info->chunk_root;
3147 path = btrfs_alloc_path();
3151 /* first we search for all of the device items, and then we
3152 * read in all of the chunk items. This way we can create chunk
3153 * mappings that reference all of the devices that are afound
3155 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
3159 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3161 leaf = path->nodes[0];
3162 slot = path->slots[0];
3163 if (slot >= btrfs_header_nritems(leaf)) {
3164 ret = btrfs_next_leaf(root, path);
3171 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3172 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3173 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
3175 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
3176 struct btrfs_dev_item *dev_item;
3177 dev_item = btrfs_item_ptr(leaf, slot,
3178 struct btrfs_dev_item);
3179 ret = read_one_dev(root, leaf, dev_item);
3183 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
3184 struct btrfs_chunk *chunk;
3185 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3186 ret = read_one_chunk(root, &found_key, leaf, chunk);
3192 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3194 btrfs_release_path(root, path);
3199 btrfs_free_path(path);