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 <linux/version.h>
24 #include <asm/div64.h>
27 #include "extent_map.h"
29 #include "transaction.h"
30 #include "print-tree.h"
32 #include "async-thread.h"
42 struct btrfs_bio_stripe stripes[];
45 static int init_first_rw_device(struct btrfs_trans_handle *trans,
46 struct btrfs_root *root,
47 struct btrfs_device *device);
48 static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
51 #define map_lookup_size(n) (sizeof(struct map_lookup) + \
52 (sizeof(struct btrfs_bio_stripe) * (n)))
54 static DEFINE_MUTEX(uuid_mutex);
55 static LIST_HEAD(fs_uuids);
57 void btrfs_lock_volumes(void)
59 mutex_lock(&uuid_mutex);
62 void btrfs_unlock_volumes(void)
64 mutex_unlock(&uuid_mutex);
67 static void lock_chunks(struct btrfs_root *root)
69 mutex_lock(&root->fs_info->chunk_mutex);
72 static void unlock_chunks(struct btrfs_root *root)
74 mutex_unlock(&root->fs_info->chunk_mutex);
77 int btrfs_cleanup_fs_uuids(void)
79 struct btrfs_fs_devices *fs_devices;
80 struct btrfs_device *dev;
82 while (!list_empty(&fs_uuids)) {
83 fs_devices = list_entry(fs_uuids.next,
84 struct btrfs_fs_devices, list);
85 list_del(&fs_devices->list);
86 while(!list_empty(&fs_devices->devices)) {
87 dev = list_entry(fs_devices->devices.next,
88 struct btrfs_device, dev_list);
90 close_bdev_exclusive(dev->bdev, dev->mode);
91 fs_devices->open_devices--;
93 fs_devices->num_devices--;
95 fs_devices->rw_devices--;
96 list_del(&dev->dev_list);
97 list_del(&dev->dev_alloc_list);
101 WARN_ON(fs_devices->num_devices);
102 WARN_ON(fs_devices->open_devices);
103 WARN_ON(fs_devices->rw_devices);
109 static noinline struct btrfs_device *__find_device(struct list_head *head,
112 struct btrfs_device *dev;
113 struct list_head *cur;
115 list_for_each(cur, head) {
116 dev = list_entry(cur, struct btrfs_device, dev_list);
117 if (dev->devid == devid &&
118 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
125 static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
127 struct list_head *cur;
128 struct btrfs_fs_devices *fs_devices;
130 list_for_each(cur, &fs_uuids) {
131 fs_devices = list_entry(cur, struct btrfs_fs_devices, list);
132 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
139 * we try to collect pending bios for a device so we don't get a large
140 * number of procs sending bios down to the same device. This greatly
141 * improves the schedulers ability to collect and merge the bios.
143 * But, it also turns into a long list of bios to process and that is sure
144 * to eventually make the worker thread block. The solution here is to
145 * make some progress and then put this work struct back at the end of
146 * the list if the block device is congested. This way, multiple devices
147 * can make progress from a single worker thread.
149 static int noinline run_scheduled_bios(struct btrfs_device *device)
152 struct backing_dev_info *bdi;
153 struct btrfs_fs_info *fs_info;
157 unsigned long num_run = 0;
160 bdi = device->bdev->bd_inode->i_mapping->backing_dev_info;
161 fs_info = device->dev_root->fs_info;
162 limit = btrfs_async_submit_limit(fs_info);
163 limit = limit * 2 / 3;
166 spin_lock(&device->io_lock);
168 /* take all the bios off the list at once and process them
169 * later on (without the lock held). But, remember the
170 * tail and other pointers so the bios can be properly reinserted
171 * into the list if we hit congestion
173 pending = device->pending_bios;
174 tail = device->pending_bio_tail;
175 WARN_ON(pending && !tail);
176 device->pending_bios = NULL;
177 device->pending_bio_tail = NULL;
180 * if pending was null this time around, no bios need processing
181 * at all and we can stop. Otherwise it'll loop back up again
182 * and do an additional check so no bios are missed.
184 * device->running_pending is used to synchronize with the
189 device->running_pending = 1;
192 device->running_pending = 0;
194 spin_unlock(&device->io_lock);
198 pending = pending->bi_next;
200 atomic_dec(&fs_info->nr_async_bios);
202 if (atomic_read(&fs_info->nr_async_bios) < limit &&
203 waitqueue_active(&fs_info->async_submit_wait))
204 wake_up(&fs_info->async_submit_wait);
206 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
208 submit_bio(cur->bi_rw, cur);
213 * we made progress, there is more work to do and the bdi
214 * is now congested. Back off and let other work structs
217 if (pending && bdi_write_congested(bdi) &&
218 fs_info->fs_devices->open_devices > 1) {
219 struct bio *old_head;
221 spin_lock(&device->io_lock);
223 old_head = device->pending_bios;
224 device->pending_bios = pending;
225 if (device->pending_bio_tail)
226 tail->bi_next = old_head;
228 device->pending_bio_tail = tail;
230 spin_unlock(&device->io_lock);
231 btrfs_requeue_work(&device->work);
241 static void pending_bios_fn(struct btrfs_work *work)
243 struct btrfs_device *device;
245 device = container_of(work, struct btrfs_device, work);
246 run_scheduled_bios(device);
249 static noinline int device_list_add(const char *path,
250 struct btrfs_super_block *disk_super,
251 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
253 struct btrfs_device *device;
254 struct btrfs_fs_devices *fs_devices;
255 u64 found_transid = btrfs_super_generation(disk_super);
257 fs_devices = find_fsid(disk_super->fsid);
259 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
262 INIT_LIST_HEAD(&fs_devices->devices);
263 INIT_LIST_HEAD(&fs_devices->alloc_list);
264 list_add(&fs_devices->list, &fs_uuids);
265 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
266 fs_devices->latest_devid = devid;
267 fs_devices->latest_trans = found_transid;
270 device = __find_device(&fs_devices->devices, devid,
271 disk_super->dev_item.uuid);
274 if (fs_devices->opened)
277 device = kzalloc(sizeof(*device), GFP_NOFS);
279 /* we can safely leave the fs_devices entry around */
282 device->devid = devid;
283 device->work.func = pending_bios_fn;
284 memcpy(device->uuid, disk_super->dev_item.uuid,
286 device->barriers = 1;
287 spin_lock_init(&device->io_lock);
288 device->name = kstrdup(path, GFP_NOFS);
293 INIT_LIST_HEAD(&device->dev_alloc_list);
294 list_add(&device->dev_list, &fs_devices->devices);
295 device->fs_devices = fs_devices;
296 fs_devices->num_devices++;
299 if (found_transid > fs_devices->latest_trans) {
300 fs_devices->latest_devid = devid;
301 fs_devices->latest_trans = found_transid;
303 *fs_devices_ret = fs_devices;
307 int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
309 struct list_head *tmp;
310 struct list_head *cur;
311 struct btrfs_device *device;
312 int seed_devices = 0;
314 mutex_lock(&uuid_mutex);
316 list_for_each_safe(cur, tmp, &fs_devices->devices) {
317 device = list_entry(cur, struct btrfs_device, dev_list);
318 if (device->in_fs_metadata)
322 close_bdev_exclusive(device->bdev, device->mode);
324 fs_devices->open_devices--;
326 if (device->writeable) {
327 list_del_init(&device->dev_alloc_list);
328 device->writeable = 0;
329 fs_devices->rw_devices--;
332 list_del_init(&device->dev_list);
333 fs_devices->num_devices--;
339 if (fs_devices->seed) {
340 fs_devices = fs_devices->seed;
345 mutex_unlock(&uuid_mutex);
349 static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
351 struct btrfs_fs_devices *seed_devices;
352 struct list_head *cur;
353 struct btrfs_device *device;
355 if (--fs_devices->opened > 0)
358 list_for_each(cur, &fs_devices->devices) {
359 device = list_entry(cur, struct btrfs_device, dev_list);
361 close_bdev_exclusive(device->bdev, device->mode);
362 fs_devices->open_devices--;
364 if (device->writeable) {
365 list_del_init(&device->dev_alloc_list);
366 fs_devices->rw_devices--;
370 device->writeable = 0;
371 device->in_fs_metadata = 0;
373 fs_devices->opened = 0;
374 fs_devices->seeding = 0;
375 fs_devices->sprouted = 0;
377 seed_devices = fs_devices->seed;
378 fs_devices->seed = NULL;
380 fs_devices = seed_devices;
386 int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
390 mutex_lock(&uuid_mutex);
391 ret = __btrfs_close_devices(fs_devices);
392 mutex_unlock(&uuid_mutex);
396 int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
397 fmode_t flags, void *holder)
399 struct block_device *bdev;
400 struct list_head *head = &fs_devices->devices;
401 struct list_head *cur;
402 struct btrfs_device *device;
403 struct block_device *latest_bdev = NULL;
404 struct buffer_head *bh;
405 struct btrfs_super_block *disk_super;
406 u64 latest_devid = 0;
407 u64 latest_transid = 0;
412 list_for_each(cur, head) {
413 device = list_entry(cur, struct btrfs_device, dev_list);
419 bdev = open_bdev_exclusive(device->name, flags, holder);
421 printk("open %s failed\n", device->name);
424 set_blocksize(bdev, 4096);
426 bh = btrfs_read_dev_super(bdev);
430 disk_super = (struct btrfs_super_block *)bh->b_data;
431 devid = le64_to_cpu(disk_super->dev_item.devid);
432 if (devid != device->devid)
435 if (memcmp(device->uuid, disk_super->dev_item.uuid,
439 device->generation = btrfs_super_generation(disk_super);
440 if (!latest_transid || device->generation > latest_transid) {
441 latest_devid = devid;
442 latest_transid = device->generation;
446 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
447 device->writeable = 0;
449 device->writeable = !bdev_read_only(bdev);
454 device->in_fs_metadata = 0;
455 device->mode = flags;
457 fs_devices->open_devices++;
458 if (device->writeable) {
459 fs_devices->rw_devices++;
460 list_add(&device->dev_alloc_list,
461 &fs_devices->alloc_list);
468 close_bdev_exclusive(bdev, FMODE_READ);
472 if (fs_devices->open_devices == 0) {
476 fs_devices->seeding = seeding;
477 fs_devices->opened = 1;
478 fs_devices->latest_bdev = latest_bdev;
479 fs_devices->latest_devid = latest_devid;
480 fs_devices->latest_trans = latest_transid;
481 fs_devices->total_rw_bytes = 0;
486 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
487 fmode_t flags, void *holder)
491 mutex_lock(&uuid_mutex);
492 if (fs_devices->opened) {
493 if (fs_devices->sprouted) {
496 fs_devices->opened++;
500 ret = __btrfs_open_devices(fs_devices, flags, holder);
502 mutex_unlock(&uuid_mutex);
506 int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
507 struct btrfs_fs_devices **fs_devices_ret)
509 struct btrfs_super_block *disk_super;
510 struct block_device *bdev;
511 struct buffer_head *bh;
516 mutex_lock(&uuid_mutex);
518 bdev = open_bdev_exclusive(path, flags, holder);
525 ret = set_blocksize(bdev, 4096);
528 bh = btrfs_read_dev_super(bdev);
533 disk_super = (struct btrfs_super_block *)bh->b_data;
534 devid = le64_to_cpu(disk_super->dev_item.devid);
535 transid = btrfs_super_generation(disk_super);
536 if (disk_super->label[0])
537 printk("device label %s ", disk_super->label);
539 /* FIXME, make a readl uuid parser */
540 printk("device fsid %llx-%llx ",
541 *(unsigned long long *)disk_super->fsid,
542 *(unsigned long long *)(disk_super->fsid + 8));
544 printk("devid %Lu transid %Lu %s\n", devid, transid, path);
545 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
549 close_bdev_exclusive(bdev, flags);
551 mutex_unlock(&uuid_mutex);
556 * this uses a pretty simple search, the expectation is that it is
557 * called very infrequently and that a given device has a small number
560 static noinline int find_free_dev_extent(struct btrfs_trans_handle *trans,
561 struct btrfs_device *device,
562 u64 num_bytes, u64 *start)
564 struct btrfs_key key;
565 struct btrfs_root *root = device->dev_root;
566 struct btrfs_dev_extent *dev_extent = NULL;
567 struct btrfs_path *path;
570 u64 search_start = 0;
571 u64 search_end = device->total_bytes;
575 struct extent_buffer *l;
577 path = btrfs_alloc_path();
583 /* FIXME use last free of some kind */
585 /* we don't want to overwrite the superblock on the drive,
586 * so we make sure to start at an offset of at least 1MB
588 search_start = max((u64)1024 * 1024, search_start);
590 if (root->fs_info->alloc_start + num_bytes <= device->total_bytes)
591 search_start = max(root->fs_info->alloc_start, search_start);
593 key.objectid = device->devid;
594 key.offset = search_start;
595 key.type = BTRFS_DEV_EXTENT_KEY;
596 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
599 ret = btrfs_previous_item(root, path, 0, key.type);
603 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
606 slot = path->slots[0];
607 if (slot >= btrfs_header_nritems(l)) {
608 ret = btrfs_next_leaf(root, path);
615 if (search_start >= search_end) {
619 *start = search_start;
623 *start = last_byte > search_start ?
624 last_byte : search_start;
625 if (search_end <= *start) {
631 btrfs_item_key_to_cpu(l, &key, slot);
633 if (key.objectid < device->devid)
636 if (key.objectid > device->devid)
639 if (key.offset >= search_start && key.offset > last_byte &&
641 if (last_byte < search_start)
642 last_byte = search_start;
643 hole_size = key.offset - last_byte;
644 if (key.offset > last_byte &&
645 hole_size >= num_bytes) {
650 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) {
655 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
656 last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent);
662 /* we have to make sure we didn't find an extent that has already
663 * been allocated by the map tree or the original allocation
665 BUG_ON(*start < search_start);
667 if (*start + num_bytes > search_end) {
671 /* check for pending inserts here */
675 btrfs_free_path(path);
679 static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
680 struct btrfs_device *device,
684 struct btrfs_path *path;
685 struct btrfs_root *root = device->dev_root;
686 struct btrfs_key key;
687 struct btrfs_key found_key;
688 struct extent_buffer *leaf = NULL;
689 struct btrfs_dev_extent *extent = NULL;
691 path = btrfs_alloc_path();
695 key.objectid = device->devid;
697 key.type = BTRFS_DEV_EXTENT_KEY;
699 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
701 ret = btrfs_previous_item(root, path, key.objectid,
702 BTRFS_DEV_EXTENT_KEY);
704 leaf = path->nodes[0];
705 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
706 extent = btrfs_item_ptr(leaf, path->slots[0],
707 struct btrfs_dev_extent);
708 BUG_ON(found_key.offset > start || found_key.offset +
709 btrfs_dev_extent_length(leaf, extent) < start);
711 } else if (ret == 0) {
712 leaf = path->nodes[0];
713 extent = btrfs_item_ptr(leaf, path->slots[0],
714 struct btrfs_dev_extent);
718 if (device->bytes_used > 0)
719 device->bytes_used -= btrfs_dev_extent_length(leaf, extent);
720 ret = btrfs_del_item(trans, root, path);
723 btrfs_free_path(path);
727 int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
728 struct btrfs_device *device,
729 u64 chunk_tree, u64 chunk_objectid,
730 u64 chunk_offset, u64 start, u64 num_bytes)
733 struct btrfs_path *path;
734 struct btrfs_root *root = device->dev_root;
735 struct btrfs_dev_extent *extent;
736 struct extent_buffer *leaf;
737 struct btrfs_key key;
739 WARN_ON(!device->in_fs_metadata);
740 path = btrfs_alloc_path();
744 key.objectid = device->devid;
746 key.type = BTRFS_DEV_EXTENT_KEY;
747 ret = btrfs_insert_empty_item(trans, root, path, &key,
751 leaf = path->nodes[0];
752 extent = btrfs_item_ptr(leaf, path->slots[0],
753 struct btrfs_dev_extent);
754 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
755 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
756 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
758 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
759 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
762 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
763 btrfs_mark_buffer_dirty(leaf);
764 btrfs_free_path(path);
768 static noinline int find_next_chunk(struct btrfs_root *root,
769 u64 objectid, u64 *offset)
771 struct btrfs_path *path;
773 struct btrfs_key key;
774 struct btrfs_chunk *chunk;
775 struct btrfs_key found_key;
777 path = btrfs_alloc_path();
780 key.objectid = objectid;
781 key.offset = (u64)-1;
782 key.type = BTRFS_CHUNK_ITEM_KEY;
784 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
790 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
794 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
796 if (found_key.objectid != objectid)
799 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
801 *offset = found_key.offset +
802 btrfs_chunk_length(path->nodes[0], chunk);
807 btrfs_free_path(path);
811 static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
814 struct btrfs_key key;
815 struct btrfs_key found_key;
816 struct btrfs_path *path;
818 root = root->fs_info->chunk_root;
820 path = btrfs_alloc_path();
824 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
825 key.type = BTRFS_DEV_ITEM_KEY;
826 key.offset = (u64)-1;
828 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
834 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
839 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
841 *objectid = found_key.offset + 1;
845 btrfs_free_path(path);
850 * the device information is stored in the chunk root
851 * the btrfs_device struct should be fully filled in
853 int btrfs_add_device(struct btrfs_trans_handle *trans,
854 struct btrfs_root *root,
855 struct btrfs_device *device)
858 struct btrfs_path *path;
859 struct btrfs_dev_item *dev_item;
860 struct extent_buffer *leaf;
861 struct btrfs_key key;
864 root = root->fs_info->chunk_root;
866 path = btrfs_alloc_path();
870 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
871 key.type = BTRFS_DEV_ITEM_KEY;
872 key.offset = device->devid;
874 ret = btrfs_insert_empty_item(trans, root, path, &key,
879 leaf = path->nodes[0];
880 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
882 btrfs_set_device_id(leaf, dev_item, device->devid);
883 btrfs_set_device_generation(leaf, dev_item, 0);
884 btrfs_set_device_type(leaf, dev_item, device->type);
885 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
886 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
887 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
888 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
889 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
890 btrfs_set_device_group(leaf, dev_item, 0);
891 btrfs_set_device_seek_speed(leaf, dev_item, 0);
892 btrfs_set_device_bandwidth(leaf, dev_item, 0);
894 ptr = (unsigned long)btrfs_device_uuid(dev_item);
895 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
896 ptr = (unsigned long)btrfs_device_fsid(dev_item);
897 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
898 btrfs_mark_buffer_dirty(leaf);
902 btrfs_free_path(path);
906 static int btrfs_rm_dev_item(struct btrfs_root *root,
907 struct btrfs_device *device)
910 struct btrfs_path *path;
911 struct btrfs_key key;
912 struct btrfs_trans_handle *trans;
914 root = root->fs_info->chunk_root;
916 path = btrfs_alloc_path();
920 trans = btrfs_start_transaction(root, 1);
921 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
922 key.type = BTRFS_DEV_ITEM_KEY;
923 key.offset = device->devid;
926 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
935 ret = btrfs_del_item(trans, root, path);
939 btrfs_free_path(path);
941 btrfs_commit_transaction(trans, root);
945 int btrfs_rm_device(struct btrfs_root *root, char *device_path)
947 struct btrfs_device *device;
948 struct btrfs_device *next_device;
949 struct block_device *bdev;
950 struct buffer_head *bh = NULL;
951 struct btrfs_super_block *disk_super;
958 mutex_lock(&uuid_mutex);
959 mutex_lock(&root->fs_info->volume_mutex);
961 all_avail = root->fs_info->avail_data_alloc_bits |
962 root->fs_info->avail_system_alloc_bits |
963 root->fs_info->avail_metadata_alloc_bits;
965 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
966 root->fs_info->fs_devices->rw_devices <= 4) {
967 printk("btrfs: unable to go below four devices on raid10\n");
972 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
973 root->fs_info->fs_devices->rw_devices <= 2) {
974 printk("btrfs: unable to go below two devices on raid1\n");
979 if (strcmp(device_path, "missing") == 0) {
980 struct list_head *cur;
981 struct list_head *devices;
982 struct btrfs_device *tmp;
985 devices = &root->fs_info->fs_devices->devices;
986 list_for_each(cur, devices) {
987 tmp = list_entry(cur, struct btrfs_device, dev_list);
988 if (tmp->in_fs_metadata && !tmp->bdev) {
997 printk("btrfs: no missing devices found to remove\n");
1001 bdev = open_bdev_exclusive(device_path, FMODE_READ,
1002 root->fs_info->bdev_holder);
1004 ret = PTR_ERR(bdev);
1008 set_blocksize(bdev, 4096);
1009 bh = btrfs_read_dev_super(bdev);
1014 disk_super = (struct btrfs_super_block *)bh->b_data;
1015 devid = le64_to_cpu(disk_super->dev_item.devid);
1016 dev_uuid = disk_super->dev_item.uuid;
1017 device = btrfs_find_device(root, devid, dev_uuid,
1025 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
1026 printk("btrfs: unable to remove the only writeable device\n");
1031 if (device->writeable) {
1032 list_del_init(&device->dev_alloc_list);
1033 root->fs_info->fs_devices->rw_devices--;
1036 ret = btrfs_shrink_device(device, 0);
1040 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1044 device->in_fs_metadata = 0;
1045 if (device->fs_devices == root->fs_info->fs_devices) {
1046 list_del_init(&device->dev_list);
1047 root->fs_info->fs_devices->num_devices--;
1049 device->fs_devices->open_devices--;
1052 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1053 struct btrfs_device, dev_list);
1054 if (device->bdev == root->fs_info->sb->s_bdev)
1055 root->fs_info->sb->s_bdev = next_device->bdev;
1056 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1057 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1059 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
1060 btrfs_set_super_num_devices(&root->fs_info->super_copy, num_devices);
1062 if (device->fs_devices != root->fs_info->fs_devices) {
1063 BUG_ON(device->writeable);
1066 close_bdev_exclusive(bdev, FMODE_READ);
1069 close_bdev_exclusive(device->bdev, device->mode);
1070 device->bdev = NULL;
1071 device->fs_devices->open_devices--;
1073 if (device->fs_devices->open_devices == 0) {
1074 struct btrfs_fs_devices *fs_devices;
1075 fs_devices = root->fs_info->fs_devices;
1076 while (fs_devices) {
1077 if (fs_devices->seed == device->fs_devices)
1079 fs_devices = fs_devices->seed;
1081 fs_devices->seed = device->fs_devices->seed;
1082 device->fs_devices->seed = NULL;
1083 __btrfs_close_devices(device->fs_devices);
1090 * at this point, the device is zero sized. We want to
1091 * remove it from the devices list and zero out the old super
1093 if (device->writeable) {
1094 /* make sure this device isn't detected as part of
1097 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1098 set_buffer_dirty(bh);
1099 sync_dirty_buffer(bh);
1104 /* one close for the device struct or super_block */
1105 close_bdev_exclusive(device->bdev, device->mode);
1108 /* one close for us */
1109 close_bdev_exclusive(bdev, FMODE_READ);
1111 kfree(device->name);
1120 close_bdev_exclusive(bdev, FMODE_READ);
1122 mutex_unlock(&root->fs_info->volume_mutex);
1123 mutex_unlock(&uuid_mutex);
1128 * does all the dirty work required for changing file system's UUID.
1130 static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans,
1131 struct btrfs_root *root)
1133 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1134 struct btrfs_fs_devices *old_devices;
1135 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
1136 struct btrfs_device *device;
1139 BUG_ON(!mutex_is_locked(&uuid_mutex));
1140 if (!fs_devices->seeding || fs_devices->opened != 1)
1143 old_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1147 memcpy(old_devices, fs_devices, sizeof(*old_devices));
1148 old_devices->opened = 1;
1149 old_devices->sprouted = 1;
1150 INIT_LIST_HEAD(&old_devices->devices);
1151 INIT_LIST_HEAD(&old_devices->alloc_list);
1152 list_splice_init(&fs_devices->devices, &old_devices->devices);
1153 list_splice_init(&fs_devices->alloc_list, &old_devices->alloc_list);
1154 list_for_each_entry(device, &old_devices->devices, dev_list) {
1155 device->fs_devices = old_devices;
1157 list_add(&old_devices->list, &fs_uuids);
1159 fs_devices->seeding = 0;
1160 fs_devices->num_devices = 0;
1161 fs_devices->open_devices = 0;
1162 fs_devices->seed = old_devices;
1164 generate_random_uuid(fs_devices->fsid);
1165 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1166 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1167 super_flags = btrfs_super_flags(disk_super) &
1168 ~BTRFS_SUPER_FLAG_SEEDING;
1169 btrfs_set_super_flags(disk_super, super_flags);
1175 * strore the expected generation for seed devices in device items.
1177 static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1178 struct btrfs_root *root)
1180 struct btrfs_path *path;
1181 struct extent_buffer *leaf;
1182 struct btrfs_dev_item *dev_item;
1183 struct btrfs_device *device;
1184 struct btrfs_key key;
1185 u8 fs_uuid[BTRFS_UUID_SIZE];
1186 u8 dev_uuid[BTRFS_UUID_SIZE];
1190 path = btrfs_alloc_path();
1194 root = root->fs_info->chunk_root;
1195 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1197 key.type = BTRFS_DEV_ITEM_KEY;
1200 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1204 leaf = path->nodes[0];
1206 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1207 ret = btrfs_next_leaf(root, path);
1212 leaf = path->nodes[0];
1213 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1214 btrfs_release_path(root, path);
1218 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1219 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1220 key.type != BTRFS_DEV_ITEM_KEY)
1223 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1224 struct btrfs_dev_item);
1225 devid = btrfs_device_id(leaf, dev_item);
1226 read_extent_buffer(leaf, dev_uuid,
1227 (unsigned long)btrfs_device_uuid(dev_item),
1229 read_extent_buffer(leaf, fs_uuid,
1230 (unsigned long)btrfs_device_fsid(dev_item),
1232 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1235 if (device->fs_devices->seeding) {
1236 btrfs_set_device_generation(leaf, dev_item,
1237 device->generation);
1238 btrfs_mark_buffer_dirty(leaf);
1246 btrfs_free_path(path);
1250 int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1252 struct btrfs_trans_handle *trans;
1253 struct btrfs_device *device;
1254 struct block_device *bdev;
1255 struct list_head *cur;
1256 struct list_head *devices;
1257 struct super_block *sb = root->fs_info->sb;
1259 int seeding_dev = 0;
1262 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1265 bdev = open_bdev_exclusive(device_path, 0, root->fs_info->bdev_holder);
1270 if (root->fs_info->fs_devices->seeding) {
1272 down_write(&sb->s_umount);
1273 mutex_lock(&uuid_mutex);
1276 filemap_write_and_wait(bdev->bd_inode->i_mapping);
1277 mutex_lock(&root->fs_info->volume_mutex);
1279 devices = &root->fs_info->fs_devices->devices;
1280 list_for_each(cur, devices) {
1281 device = list_entry(cur, struct btrfs_device, dev_list);
1282 if (device->bdev == bdev) {
1288 device = kzalloc(sizeof(*device), GFP_NOFS);
1290 /* we can safely leave the fs_devices entry around */
1295 device->name = kstrdup(device_path, GFP_NOFS);
1296 if (!device->name) {
1302 ret = find_next_devid(root, &device->devid);
1308 trans = btrfs_start_transaction(root, 1);
1311 device->barriers = 1;
1312 device->writeable = 1;
1313 device->work.func = pending_bios_fn;
1314 generate_random_uuid(device->uuid);
1315 spin_lock_init(&device->io_lock);
1316 device->generation = trans->transid;
1317 device->io_width = root->sectorsize;
1318 device->io_align = root->sectorsize;
1319 device->sector_size = root->sectorsize;
1320 device->total_bytes = i_size_read(bdev->bd_inode);
1321 device->dev_root = root->fs_info->dev_root;
1322 device->bdev = bdev;
1323 device->in_fs_metadata = 1;
1325 set_blocksize(device->bdev, 4096);
1328 sb->s_flags &= ~MS_RDONLY;
1329 ret = btrfs_prepare_sprout(trans, root);
1333 device->fs_devices = root->fs_info->fs_devices;
1334 list_add(&device->dev_list, &root->fs_info->fs_devices->devices);
1335 list_add(&device->dev_alloc_list,
1336 &root->fs_info->fs_devices->alloc_list);
1337 root->fs_info->fs_devices->num_devices++;
1338 root->fs_info->fs_devices->open_devices++;
1339 root->fs_info->fs_devices->rw_devices++;
1340 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1342 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
1343 btrfs_set_super_total_bytes(&root->fs_info->super_copy,
1344 total_bytes + device->total_bytes);
1346 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
1347 btrfs_set_super_num_devices(&root->fs_info->super_copy,
1351 ret = init_first_rw_device(trans, root, device);
1353 ret = btrfs_finish_sprout(trans, root);
1356 ret = btrfs_add_device(trans, root, device);
1359 unlock_chunks(root);
1360 btrfs_commit_transaction(trans, root);
1363 mutex_unlock(&uuid_mutex);
1364 up_write(&sb->s_umount);
1366 ret = btrfs_relocate_sys_chunks(root);
1370 mutex_unlock(&root->fs_info->volume_mutex);
1373 close_bdev_exclusive(bdev, 0);
1375 mutex_unlock(&uuid_mutex);
1376 up_write(&sb->s_umount);
1381 static int noinline btrfs_update_device(struct btrfs_trans_handle *trans,
1382 struct btrfs_device *device)
1385 struct btrfs_path *path;
1386 struct btrfs_root *root;
1387 struct btrfs_dev_item *dev_item;
1388 struct extent_buffer *leaf;
1389 struct btrfs_key key;
1391 root = device->dev_root->fs_info->chunk_root;
1393 path = btrfs_alloc_path();
1397 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1398 key.type = BTRFS_DEV_ITEM_KEY;
1399 key.offset = device->devid;
1401 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1410 leaf = path->nodes[0];
1411 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1413 btrfs_set_device_id(leaf, dev_item, device->devid);
1414 btrfs_set_device_type(leaf, dev_item, device->type);
1415 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1416 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1417 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
1418 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1419 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1420 btrfs_mark_buffer_dirty(leaf);
1423 btrfs_free_path(path);
1427 static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
1428 struct btrfs_device *device, u64 new_size)
1430 struct btrfs_super_block *super_copy =
1431 &device->dev_root->fs_info->super_copy;
1432 u64 old_total = btrfs_super_total_bytes(super_copy);
1433 u64 diff = new_size - device->total_bytes;
1435 if (!device->writeable)
1437 if (new_size <= device->total_bytes)
1440 btrfs_set_super_total_bytes(super_copy, old_total + diff);
1441 device->fs_devices->total_rw_bytes += diff;
1443 device->total_bytes = new_size;
1444 return btrfs_update_device(trans, device);
1447 int btrfs_grow_device(struct btrfs_trans_handle *trans,
1448 struct btrfs_device *device, u64 new_size)
1451 lock_chunks(device->dev_root);
1452 ret = __btrfs_grow_device(trans, device, new_size);
1453 unlock_chunks(device->dev_root);
1457 static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1458 struct btrfs_root *root,
1459 u64 chunk_tree, u64 chunk_objectid,
1463 struct btrfs_path *path;
1464 struct btrfs_key key;
1466 root = root->fs_info->chunk_root;
1467 path = btrfs_alloc_path();
1471 key.objectid = chunk_objectid;
1472 key.offset = chunk_offset;
1473 key.type = BTRFS_CHUNK_ITEM_KEY;
1475 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1478 ret = btrfs_del_item(trans, root, path);
1481 btrfs_free_path(path);
1485 static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
1488 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1489 struct btrfs_disk_key *disk_key;
1490 struct btrfs_chunk *chunk;
1497 struct btrfs_key key;
1499 array_size = btrfs_super_sys_array_size(super_copy);
1501 ptr = super_copy->sys_chunk_array;
1504 while (cur < array_size) {
1505 disk_key = (struct btrfs_disk_key *)ptr;
1506 btrfs_disk_key_to_cpu(&key, disk_key);
1508 len = sizeof(*disk_key);
1510 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1511 chunk = (struct btrfs_chunk *)(ptr + len);
1512 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1513 len += btrfs_chunk_item_size(num_stripes);
1518 if (key.objectid == chunk_objectid &&
1519 key.offset == chunk_offset) {
1520 memmove(ptr, ptr + len, array_size - (cur + len));
1522 btrfs_set_super_sys_array_size(super_copy, array_size);
1531 static int btrfs_relocate_chunk(struct btrfs_root *root,
1532 u64 chunk_tree, u64 chunk_objectid,
1535 struct extent_map_tree *em_tree;
1536 struct btrfs_root *extent_root;
1537 struct btrfs_trans_handle *trans;
1538 struct extent_map *em;
1539 struct map_lookup *map;
1543 printk("btrfs relocating chunk %llu\n",
1544 (unsigned long long)chunk_offset);
1545 root = root->fs_info->chunk_root;
1546 extent_root = root->fs_info->extent_root;
1547 em_tree = &root->fs_info->mapping_tree.map_tree;
1549 /* step one, relocate all the extents inside this chunk */
1550 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
1553 trans = btrfs_start_transaction(root, 1);
1559 * step two, delete the device extents and the
1560 * chunk tree entries
1562 spin_lock(&em_tree->lock);
1563 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
1564 spin_unlock(&em_tree->lock);
1566 BUG_ON(em->start > chunk_offset ||
1567 em->start + em->len < chunk_offset);
1568 map = (struct map_lookup *)em->bdev;
1570 for (i = 0; i < map->num_stripes; i++) {
1571 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1572 map->stripes[i].physical);
1575 if (map->stripes[i].dev) {
1576 ret = btrfs_update_device(trans, map->stripes[i].dev);
1580 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1585 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1586 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1590 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
1593 spin_lock(&em_tree->lock);
1594 remove_extent_mapping(em_tree, em);
1595 spin_unlock(&em_tree->lock);
1600 /* once for the tree */
1601 free_extent_map(em);
1603 free_extent_map(em);
1605 unlock_chunks(root);
1606 btrfs_end_transaction(trans, root);
1610 static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
1612 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1613 struct btrfs_path *path;
1614 struct extent_buffer *leaf;
1615 struct btrfs_chunk *chunk;
1616 struct btrfs_key key;
1617 struct btrfs_key found_key;
1618 u64 chunk_tree = chunk_root->root_key.objectid;
1622 path = btrfs_alloc_path();
1626 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1627 key.offset = (u64)-1;
1628 key.type = BTRFS_CHUNK_ITEM_KEY;
1631 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1636 ret = btrfs_previous_item(chunk_root, path, key.objectid,
1643 leaf = path->nodes[0];
1644 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1646 chunk = btrfs_item_ptr(leaf, path->slots[0],
1647 struct btrfs_chunk);
1648 chunk_type = btrfs_chunk_type(leaf, chunk);
1649 btrfs_release_path(chunk_root, path);
1651 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
1652 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
1658 if (found_key.offset == 0)
1660 key.offset = found_key.offset - 1;
1664 btrfs_free_path(path);
1668 static u64 div_factor(u64 num, int factor)
1677 int btrfs_balance(struct btrfs_root *dev_root)
1680 struct list_head *cur;
1681 struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
1682 struct btrfs_device *device;
1685 struct btrfs_path *path;
1686 struct btrfs_key key;
1687 struct btrfs_chunk *chunk;
1688 struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
1689 struct btrfs_trans_handle *trans;
1690 struct btrfs_key found_key;
1692 if (dev_root->fs_info->sb->s_flags & MS_RDONLY)
1695 mutex_lock(&dev_root->fs_info->volume_mutex);
1696 dev_root = dev_root->fs_info->dev_root;
1698 /* step one make some room on all the devices */
1699 list_for_each(cur, devices) {
1700 device = list_entry(cur, struct btrfs_device, dev_list);
1701 old_size = device->total_bytes;
1702 size_to_free = div_factor(old_size, 1);
1703 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
1704 if (!device->writeable ||
1705 device->total_bytes - device->bytes_used > size_to_free)
1708 ret = btrfs_shrink_device(device, old_size - size_to_free);
1711 trans = btrfs_start_transaction(dev_root, 1);
1714 ret = btrfs_grow_device(trans, device, old_size);
1717 btrfs_end_transaction(trans, dev_root);
1720 /* step two, relocate all the chunks */
1721 path = btrfs_alloc_path();
1724 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1725 key.offset = (u64)-1;
1726 key.type = BTRFS_CHUNK_ITEM_KEY;
1729 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1734 * this shouldn't happen, it means the last relocate
1740 ret = btrfs_previous_item(chunk_root, path, 0,
1741 BTRFS_CHUNK_ITEM_KEY);
1745 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1747 if (found_key.objectid != key.objectid)
1750 chunk = btrfs_item_ptr(path->nodes[0],
1752 struct btrfs_chunk);
1753 key.offset = found_key.offset;
1754 /* chunk zero is special */
1755 if (key.offset == 0)
1758 btrfs_release_path(chunk_root, path);
1759 ret = btrfs_relocate_chunk(chunk_root,
1760 chunk_root->root_key.objectid,
1767 btrfs_free_path(path);
1768 mutex_unlock(&dev_root->fs_info->volume_mutex);
1773 * shrinking a device means finding all of the device extents past
1774 * the new size, and then following the back refs to the chunks.
1775 * The chunk relocation code actually frees the device extent
1777 int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
1779 struct btrfs_trans_handle *trans;
1780 struct btrfs_root *root = device->dev_root;
1781 struct btrfs_dev_extent *dev_extent = NULL;
1782 struct btrfs_path *path;
1789 struct extent_buffer *l;
1790 struct btrfs_key key;
1791 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1792 u64 old_total = btrfs_super_total_bytes(super_copy);
1793 u64 diff = device->total_bytes - new_size;
1795 if (new_size >= device->total_bytes)
1798 path = btrfs_alloc_path();
1802 trans = btrfs_start_transaction(root, 1);
1812 device->total_bytes = new_size;
1813 if (device->writeable)
1814 device->fs_devices->total_rw_bytes -= diff;
1815 ret = btrfs_update_device(trans, device);
1817 unlock_chunks(root);
1818 btrfs_end_transaction(trans, root);
1821 WARN_ON(diff > old_total);
1822 btrfs_set_super_total_bytes(super_copy, old_total - diff);
1823 unlock_chunks(root);
1824 btrfs_end_transaction(trans, root);
1826 key.objectid = device->devid;
1827 key.offset = (u64)-1;
1828 key.type = BTRFS_DEV_EXTENT_KEY;
1831 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1835 ret = btrfs_previous_item(root, path, 0, key.type);
1844 slot = path->slots[0];
1845 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
1847 if (key.objectid != device->devid)
1850 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
1851 length = btrfs_dev_extent_length(l, dev_extent);
1853 if (key.offset + length <= new_size)
1856 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
1857 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
1858 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
1859 btrfs_release_path(root, path);
1861 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
1868 btrfs_free_path(path);
1872 static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
1873 struct btrfs_root *root,
1874 struct btrfs_key *key,
1875 struct btrfs_chunk *chunk, int item_size)
1877 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1878 struct btrfs_disk_key disk_key;
1882 array_size = btrfs_super_sys_array_size(super_copy);
1883 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
1886 ptr = super_copy->sys_chunk_array + array_size;
1887 btrfs_cpu_key_to_disk(&disk_key, key);
1888 memcpy(ptr, &disk_key, sizeof(disk_key));
1889 ptr += sizeof(disk_key);
1890 memcpy(ptr, chunk, item_size);
1891 item_size += sizeof(disk_key);
1892 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
1896 static u64 noinline chunk_bytes_by_type(u64 type, u64 calc_size,
1897 int num_stripes, int sub_stripes)
1899 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
1901 else if (type & BTRFS_BLOCK_GROUP_RAID10)
1902 return calc_size * (num_stripes / sub_stripes);
1904 return calc_size * num_stripes;
1907 static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
1908 struct btrfs_root *extent_root,
1909 struct map_lookup **map_ret,
1910 u64 *num_bytes, u64 *stripe_size,
1911 u64 start, u64 type)
1913 struct btrfs_fs_info *info = extent_root->fs_info;
1914 struct btrfs_device *device = NULL;
1915 struct btrfs_fs_devices *fs_devices = info->fs_devices;
1916 struct list_head *cur;
1917 struct map_lookup *map = NULL;
1918 struct extent_map_tree *em_tree;
1919 struct extent_map *em;
1920 struct list_head private_devs;
1921 int min_stripe_size = 1 * 1024 * 1024;
1922 u64 calc_size = 1024 * 1024 * 1024;
1923 u64 max_chunk_size = calc_size;
1928 int num_stripes = 1;
1929 int min_stripes = 1;
1930 int sub_stripes = 0;
1934 int stripe_len = 64 * 1024;
1936 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
1937 (type & BTRFS_BLOCK_GROUP_DUP)) {
1939 type &= ~BTRFS_BLOCK_GROUP_DUP;
1941 if (list_empty(&fs_devices->alloc_list))
1944 if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
1945 num_stripes = fs_devices->rw_devices;
1948 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
1952 if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
1953 num_stripes = min_t(u64, 2, fs_devices->rw_devices);
1954 if (num_stripes < 2)
1958 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
1959 num_stripes = fs_devices->rw_devices;
1960 if (num_stripes < 4)
1962 num_stripes &= ~(u32)1;
1967 if (type & BTRFS_BLOCK_GROUP_DATA) {
1968 max_chunk_size = 10 * calc_size;
1969 min_stripe_size = 64 * 1024 * 1024;
1970 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
1971 max_chunk_size = 4 * calc_size;
1972 min_stripe_size = 32 * 1024 * 1024;
1973 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
1974 calc_size = 8 * 1024 * 1024;
1975 max_chunk_size = calc_size * 2;
1976 min_stripe_size = 1 * 1024 * 1024;
1979 /* we don't want a chunk larger than 10% of writeable space */
1980 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
1984 if (!map || map->num_stripes != num_stripes) {
1986 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
1989 map->num_stripes = num_stripes;
1992 if (calc_size * num_stripes > max_chunk_size) {
1993 calc_size = max_chunk_size;
1994 do_div(calc_size, num_stripes);
1995 do_div(calc_size, stripe_len);
1996 calc_size *= stripe_len;
1998 /* we don't want tiny stripes */
1999 calc_size = max_t(u64, min_stripe_size, calc_size);
2001 do_div(calc_size, stripe_len);
2002 calc_size *= stripe_len;
2004 cur = fs_devices->alloc_list.next;
2007 if (type & BTRFS_BLOCK_GROUP_DUP)
2008 min_free = calc_size * 2;
2010 min_free = calc_size;
2013 * we add 1MB because we never use the first 1MB of the device, unless
2014 * we've looped, then we are likely allocating the maximum amount of
2015 * space left already
2018 min_free += 1024 * 1024;
2020 INIT_LIST_HEAD(&private_devs);
2021 while(index < num_stripes) {
2022 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
2023 BUG_ON(!device->writeable);
2024 if (device->total_bytes > device->bytes_used)
2025 avail = device->total_bytes - device->bytes_used;
2030 if (device->in_fs_metadata && avail >= min_free) {
2031 ret = find_free_dev_extent(trans, device,
2032 min_free, &dev_offset);
2034 list_move_tail(&device->dev_alloc_list,
2036 map->stripes[index].dev = device;
2037 map->stripes[index].physical = dev_offset;
2039 if (type & BTRFS_BLOCK_GROUP_DUP) {
2040 map->stripes[index].dev = device;
2041 map->stripes[index].physical =
2042 dev_offset + calc_size;
2046 } else if (device->in_fs_metadata && avail > max_avail)
2048 if (cur == &fs_devices->alloc_list)
2051 list_splice(&private_devs, &fs_devices->alloc_list);
2052 if (index < num_stripes) {
2053 if (index >= min_stripes) {
2054 num_stripes = index;
2055 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2056 num_stripes /= sub_stripes;
2057 num_stripes *= sub_stripes;
2062 if (!looped && max_avail > 0) {
2064 calc_size = max_avail;
2070 map->sector_size = extent_root->sectorsize;
2071 map->stripe_len = stripe_len;
2072 map->io_align = stripe_len;
2073 map->io_width = stripe_len;
2075 map->num_stripes = num_stripes;
2076 map->sub_stripes = sub_stripes;
2079 *stripe_size = calc_size;
2080 *num_bytes = chunk_bytes_by_type(type, calc_size,
2081 num_stripes, sub_stripes);
2083 em = alloc_extent_map(GFP_NOFS);
2088 em->bdev = (struct block_device *)map;
2090 em->len = *num_bytes;
2091 em->block_start = 0;
2092 em->block_len = em->len;
2094 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
2095 spin_lock(&em_tree->lock);
2096 ret = add_extent_mapping(em_tree, em);
2097 spin_unlock(&em_tree->lock);
2099 free_extent_map(em);
2101 ret = btrfs_make_block_group(trans, extent_root, 0, type,
2102 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2107 while (index < map->num_stripes) {
2108 device = map->stripes[index].dev;
2109 dev_offset = map->stripes[index].physical;
2111 ret = btrfs_alloc_dev_extent(trans, device,
2112 info->chunk_root->root_key.objectid,
2113 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2114 start, dev_offset, calc_size);
2122 static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
2123 struct btrfs_root *extent_root,
2124 struct map_lookup *map, u64 chunk_offset,
2125 u64 chunk_size, u64 stripe_size)
2128 struct btrfs_key key;
2129 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2130 struct btrfs_device *device;
2131 struct btrfs_chunk *chunk;
2132 struct btrfs_stripe *stripe;
2133 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
2137 chunk = kzalloc(item_size, GFP_NOFS);
2142 while (index < map->num_stripes) {
2143 device = map->stripes[index].dev;
2144 device->bytes_used += stripe_size;
2145 ret = btrfs_update_device(trans, device);
2151 stripe = &chunk->stripe;
2152 while (index < map->num_stripes) {
2153 device = map->stripes[index].dev;
2154 dev_offset = map->stripes[index].physical;
2156 btrfs_set_stack_stripe_devid(stripe, device->devid);
2157 btrfs_set_stack_stripe_offset(stripe, dev_offset);
2158 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
2163 btrfs_set_stack_chunk_length(chunk, chunk_size);
2164 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
2165 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
2166 btrfs_set_stack_chunk_type(chunk, map->type);
2167 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
2168 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
2169 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
2170 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
2171 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
2173 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2174 key.type = BTRFS_CHUNK_ITEM_KEY;
2175 key.offset = chunk_offset;
2177 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
2180 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2181 ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
2190 * Chunk allocation falls into two parts. The first part does works
2191 * that make the new allocated chunk useable, but not do any operation
2192 * that modifies the chunk tree. The second part does the works that
2193 * require modifying the chunk tree. This division is important for the
2194 * bootstrap process of adding storage to a seed btrfs.
2196 int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2197 struct btrfs_root *extent_root, u64 type)
2202 struct map_lookup *map;
2203 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2206 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2211 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2212 &stripe_size, chunk_offset, type);
2216 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2217 chunk_size, stripe_size);
2222 static int noinline init_first_rw_device(struct btrfs_trans_handle *trans,
2223 struct btrfs_root *root,
2224 struct btrfs_device *device)
2227 u64 sys_chunk_offset;
2231 u64 sys_stripe_size;
2233 struct map_lookup *map;
2234 struct map_lookup *sys_map;
2235 struct btrfs_fs_info *fs_info = root->fs_info;
2236 struct btrfs_root *extent_root = fs_info->extent_root;
2239 ret = find_next_chunk(fs_info->chunk_root,
2240 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
2243 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
2244 (fs_info->metadata_alloc_profile &
2245 fs_info->avail_metadata_alloc_bits);
2246 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2248 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2249 &stripe_size, chunk_offset, alloc_profile);
2252 sys_chunk_offset = chunk_offset + chunk_size;
2254 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
2255 (fs_info->system_alloc_profile &
2256 fs_info->avail_system_alloc_bits);
2257 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2259 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
2260 &sys_chunk_size, &sys_stripe_size,
2261 sys_chunk_offset, alloc_profile);
2264 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
2268 * Modifying chunk tree needs allocating new blocks from both
2269 * system block group and metadata block group. So we only can
2270 * do operations require modifying the chunk tree after both
2271 * block groups were created.
2273 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2274 chunk_size, stripe_size);
2277 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
2278 sys_chunk_offset, sys_chunk_size,
2284 int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
2286 struct extent_map *em;
2287 struct map_lookup *map;
2288 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2292 spin_lock(&map_tree->map_tree.lock);
2293 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
2294 spin_unlock(&map_tree->map_tree.lock);
2298 map = (struct map_lookup *)em->bdev;
2299 for (i = 0; i < map->num_stripes; i++) {
2300 if (!map->stripes[i].dev->writeable) {
2305 free_extent_map(em);
2309 void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
2311 extent_map_tree_init(&tree->map_tree, GFP_NOFS);
2314 void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
2316 struct extent_map *em;
2319 spin_lock(&tree->map_tree.lock);
2320 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
2322 remove_extent_mapping(&tree->map_tree, em);
2323 spin_unlock(&tree->map_tree.lock);
2328 free_extent_map(em);
2329 /* once for the tree */
2330 free_extent_map(em);
2334 int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
2336 struct extent_map *em;
2337 struct map_lookup *map;
2338 struct extent_map_tree *em_tree = &map_tree->map_tree;
2341 spin_lock(&em_tree->lock);
2342 em = lookup_extent_mapping(em_tree, logical, len);
2343 spin_unlock(&em_tree->lock);
2346 BUG_ON(em->start > logical || em->start + em->len < logical);
2347 map = (struct map_lookup *)em->bdev;
2348 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
2349 ret = map->num_stripes;
2350 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2351 ret = map->sub_stripes;
2354 free_extent_map(em);
2358 static int find_live_mirror(struct map_lookup *map, int first, int num,
2362 if (map->stripes[optimal].dev->bdev)
2364 for (i = first; i < first + num; i++) {
2365 if (map->stripes[i].dev->bdev)
2368 /* we couldn't find one that doesn't fail. Just return something
2369 * and the io error handling code will clean up eventually
2374 static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2375 u64 logical, u64 *length,
2376 struct btrfs_multi_bio **multi_ret,
2377 int mirror_num, struct page *unplug_page)
2379 struct extent_map *em;
2380 struct map_lookup *map;
2381 struct extent_map_tree *em_tree = &map_tree->map_tree;
2385 int stripes_allocated = 8;
2386 int stripes_required = 1;
2391 struct btrfs_multi_bio *multi = NULL;
2393 if (multi_ret && !(rw & (1 << BIO_RW))) {
2394 stripes_allocated = 1;
2398 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
2403 atomic_set(&multi->error, 0);
2406 spin_lock(&em_tree->lock);
2407 em = lookup_extent_mapping(em_tree, logical, *length);
2408 spin_unlock(&em_tree->lock);
2410 if (!em && unplug_page)
2414 printk("unable to find logical %Lu len %Lu\n", logical, *length);
2418 BUG_ON(em->start > logical || em->start + em->len < logical);
2419 map = (struct map_lookup *)em->bdev;
2420 offset = logical - em->start;
2422 if (mirror_num > map->num_stripes)
2425 /* if our multi bio struct is too small, back off and try again */
2426 if (rw & (1 << BIO_RW)) {
2427 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
2428 BTRFS_BLOCK_GROUP_DUP)) {
2429 stripes_required = map->num_stripes;
2431 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2432 stripes_required = map->sub_stripes;
2436 if (multi_ret && rw == WRITE &&
2437 stripes_allocated < stripes_required) {
2438 stripes_allocated = map->num_stripes;
2439 free_extent_map(em);
2445 * stripe_nr counts the total number of stripes we have to stride
2446 * to get to this block
2448 do_div(stripe_nr, map->stripe_len);
2450 stripe_offset = stripe_nr * map->stripe_len;
2451 BUG_ON(offset < stripe_offset);
2453 /* stripe_offset is the offset of this block in its stripe*/
2454 stripe_offset = offset - stripe_offset;
2456 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
2457 BTRFS_BLOCK_GROUP_RAID10 |
2458 BTRFS_BLOCK_GROUP_DUP)) {
2459 /* we limit the length of each bio to what fits in a stripe */
2460 *length = min_t(u64, em->len - offset,
2461 map->stripe_len - stripe_offset);
2463 *length = em->len - offset;
2466 if (!multi_ret && !unplug_page)
2471 if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
2472 if (unplug_page || (rw & (1 << BIO_RW)))
2473 num_stripes = map->num_stripes;
2474 else if (mirror_num)
2475 stripe_index = mirror_num - 1;
2477 stripe_index = find_live_mirror(map, 0,
2479 current->pid % map->num_stripes);
2482 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
2483 if (rw & (1 << BIO_RW))
2484 num_stripes = map->num_stripes;
2485 else if (mirror_num)
2486 stripe_index = mirror_num - 1;
2488 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2489 int factor = map->num_stripes / map->sub_stripes;
2491 stripe_index = do_div(stripe_nr, factor);
2492 stripe_index *= map->sub_stripes;
2494 if (unplug_page || (rw & (1 << BIO_RW)))
2495 num_stripes = map->sub_stripes;
2496 else if (mirror_num)
2497 stripe_index += mirror_num - 1;
2499 stripe_index = find_live_mirror(map, stripe_index,
2500 map->sub_stripes, stripe_index +
2501 current->pid % map->sub_stripes);
2505 * after this do_div call, stripe_nr is the number of stripes
2506 * on this device we have to walk to find the data, and
2507 * stripe_index is the number of our device in the stripe array
2509 stripe_index = do_div(stripe_nr, map->num_stripes);
2511 BUG_ON(stripe_index >= map->num_stripes);
2513 for (i = 0; i < num_stripes; i++) {
2515 struct btrfs_device *device;
2516 struct backing_dev_info *bdi;
2518 device = map->stripes[stripe_index].dev;
2520 bdi = blk_get_backing_dev_info(device->bdev);
2521 if (bdi->unplug_io_fn) {
2522 bdi->unplug_io_fn(bdi, unplug_page);
2526 multi->stripes[i].physical =
2527 map->stripes[stripe_index].physical +
2528 stripe_offset + stripe_nr * map->stripe_len;
2529 multi->stripes[i].dev = map->stripes[stripe_index].dev;
2535 multi->num_stripes = num_stripes;
2536 multi->max_errors = max_errors;
2539 free_extent_map(em);
2543 int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2544 u64 logical, u64 *length,
2545 struct btrfs_multi_bio **multi_ret, int mirror_num)
2547 return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
2551 int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
2552 u64 chunk_start, u64 physical, u64 devid,
2553 u64 **logical, int *naddrs, int *stripe_len)
2555 struct extent_map_tree *em_tree = &map_tree->map_tree;
2556 struct extent_map *em;
2557 struct map_lookup *map;
2564 spin_lock(&em_tree->lock);
2565 em = lookup_extent_mapping(em_tree, chunk_start, 1);
2566 spin_unlock(&em_tree->lock);
2568 BUG_ON(!em || em->start != chunk_start);
2569 map = (struct map_lookup *)em->bdev;
2572 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2573 do_div(length, map->num_stripes / map->sub_stripes);
2574 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
2575 do_div(length, map->num_stripes);
2577 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
2580 for (i = 0; i < map->num_stripes; i++) {
2581 if (devid && map->stripes[i].dev->devid != devid)
2583 if (map->stripes[i].physical > physical ||
2584 map->stripes[i].physical + length <= physical)
2587 stripe_nr = physical - map->stripes[i].physical;
2588 do_div(stripe_nr, map->stripe_len);
2590 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2591 stripe_nr = stripe_nr * map->num_stripes + i;
2592 do_div(stripe_nr, map->sub_stripes);
2593 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
2594 stripe_nr = stripe_nr * map->num_stripes + i;
2596 bytenr = chunk_start + stripe_nr * map->stripe_len;
2597 for (j = 0; j < nr; j++) {
2598 if (buf[j] == bytenr)
2605 for (i = 0; i > nr; i++) {
2606 struct btrfs_multi_bio *multi;
2607 struct btrfs_bio_stripe *stripe;
2611 ret = btrfs_map_block(map_tree, WRITE, buf[i],
2612 &length, &multi, 0);
2615 stripe = multi->stripes;
2616 for (j = 0; j < multi->num_stripes; j++) {
2617 if (stripe->physical >= physical &&
2618 physical < stripe->physical + length)
2621 BUG_ON(j >= multi->num_stripes);
2627 *stripe_len = map->stripe_len;
2629 free_extent_map(em);
2633 int btrfs_unplug_page(struct btrfs_mapping_tree *map_tree,
2634 u64 logical, struct page *page)
2636 u64 length = PAGE_CACHE_SIZE;
2637 return __btrfs_map_block(map_tree, READ, logical, &length,
2642 static void end_bio_multi_stripe(struct bio *bio, int err)
2644 struct btrfs_multi_bio *multi = bio->bi_private;
2645 int is_orig_bio = 0;
2648 atomic_inc(&multi->error);
2650 if (bio == multi->orig_bio)
2653 if (atomic_dec_and_test(&multi->stripes_pending)) {
2656 bio = multi->orig_bio;
2658 bio->bi_private = multi->private;
2659 bio->bi_end_io = multi->end_io;
2660 /* only send an error to the higher layers if it is
2661 * beyond the tolerance of the multi-bio
2663 if (atomic_read(&multi->error) > multi->max_errors) {
2667 * this bio is actually up to date, we didn't
2668 * go over the max number of errors
2670 set_bit(BIO_UPTODATE, &bio->bi_flags);
2675 bio_endio(bio, err);
2676 } else if (!is_orig_bio) {
2681 struct async_sched {
2684 struct btrfs_fs_info *info;
2685 struct btrfs_work work;
2689 * see run_scheduled_bios for a description of why bios are collected for
2692 * This will add one bio to the pending list for a device and make sure
2693 * the work struct is scheduled.
2695 static int noinline schedule_bio(struct btrfs_root *root,
2696 struct btrfs_device *device,
2697 int rw, struct bio *bio)
2699 int should_queue = 1;
2701 /* don't bother with additional async steps for reads, right now */
2702 if (!(rw & (1 << BIO_RW))) {
2704 submit_bio(rw, bio);
2710 * nr_async_bios allows us to reliably return congestion to the
2711 * higher layers. Otherwise, the async bio makes it appear we have
2712 * made progress against dirty pages when we've really just put it
2713 * on a queue for later
2715 atomic_inc(&root->fs_info->nr_async_bios);
2716 WARN_ON(bio->bi_next);
2717 bio->bi_next = NULL;
2720 spin_lock(&device->io_lock);
2722 if (device->pending_bio_tail)
2723 device->pending_bio_tail->bi_next = bio;
2725 device->pending_bio_tail = bio;
2726 if (!device->pending_bios)
2727 device->pending_bios = bio;
2728 if (device->running_pending)
2731 spin_unlock(&device->io_lock);
2734 btrfs_queue_worker(&root->fs_info->submit_workers,
2739 int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
2740 int mirror_num, int async_submit)
2742 struct btrfs_mapping_tree *map_tree;
2743 struct btrfs_device *dev;
2744 struct bio *first_bio = bio;
2745 u64 logical = (u64)bio->bi_sector << 9;
2748 struct btrfs_multi_bio *multi = NULL;
2753 length = bio->bi_size;
2754 map_tree = &root->fs_info->mapping_tree;
2755 map_length = length;
2757 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
2761 total_devs = multi->num_stripes;
2762 if (map_length < length) {
2763 printk("mapping failed logical %Lu bio len %Lu "
2764 "len %Lu\n", logical, length, map_length);
2767 multi->end_io = first_bio->bi_end_io;
2768 multi->private = first_bio->bi_private;
2769 multi->orig_bio = first_bio;
2770 atomic_set(&multi->stripes_pending, multi->num_stripes);
2772 while(dev_nr < total_devs) {
2773 if (total_devs > 1) {
2774 if (dev_nr < total_devs - 1) {
2775 bio = bio_clone(first_bio, GFP_NOFS);
2780 bio->bi_private = multi;
2781 bio->bi_end_io = end_bio_multi_stripe;
2783 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
2784 dev = multi->stripes[dev_nr].dev;
2785 BUG_ON(rw == WRITE && !dev->writeable);
2786 if (dev && dev->bdev) {
2787 bio->bi_bdev = dev->bdev;
2789 schedule_bio(root, dev, rw, bio);
2791 submit_bio(rw, bio);
2793 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
2794 bio->bi_sector = logical >> 9;
2795 bio_endio(bio, -EIO);
2799 if (total_devs == 1)
2804 struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
2807 struct btrfs_device *device;
2808 struct btrfs_fs_devices *cur_devices;
2810 cur_devices = root->fs_info->fs_devices;
2811 while (cur_devices) {
2813 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
2814 device = __find_device(&cur_devices->devices,
2819 cur_devices = cur_devices->seed;
2824 static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
2825 u64 devid, u8 *dev_uuid)
2827 struct btrfs_device *device;
2828 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2830 device = kzalloc(sizeof(*device), GFP_NOFS);
2833 list_add(&device->dev_list,
2834 &fs_devices->devices);
2835 device->barriers = 1;
2836 device->dev_root = root->fs_info->dev_root;
2837 device->devid = devid;
2838 device->work.func = pending_bios_fn;
2839 fs_devices->num_devices++;
2840 spin_lock_init(&device->io_lock);
2841 INIT_LIST_HEAD(&device->dev_alloc_list);
2842 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
2846 static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
2847 struct extent_buffer *leaf,
2848 struct btrfs_chunk *chunk)
2850 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2851 struct map_lookup *map;
2852 struct extent_map *em;
2856 u8 uuid[BTRFS_UUID_SIZE];
2861 logical = key->offset;
2862 length = btrfs_chunk_length(leaf, chunk);
2864 spin_lock(&map_tree->map_tree.lock);
2865 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
2866 spin_unlock(&map_tree->map_tree.lock);
2868 /* already mapped? */
2869 if (em && em->start <= logical && em->start + em->len > logical) {
2870 free_extent_map(em);
2873 free_extent_map(em);
2876 map = kzalloc(sizeof(*map), GFP_NOFS);
2880 em = alloc_extent_map(GFP_NOFS);
2883 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2884 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
2886 free_extent_map(em);
2890 em->bdev = (struct block_device *)map;
2891 em->start = logical;
2893 em->block_start = 0;
2894 em->block_len = em->len;
2896 map->num_stripes = num_stripes;
2897 map->io_width = btrfs_chunk_io_width(leaf, chunk);
2898 map->io_align = btrfs_chunk_io_align(leaf, chunk);
2899 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
2900 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
2901 map->type = btrfs_chunk_type(leaf, chunk);
2902 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
2903 for (i = 0; i < num_stripes; i++) {
2904 map->stripes[i].physical =
2905 btrfs_stripe_offset_nr(leaf, chunk, i);
2906 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
2907 read_extent_buffer(leaf, uuid, (unsigned long)
2908 btrfs_stripe_dev_uuid_nr(chunk, i),
2910 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
2912 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
2914 free_extent_map(em);
2917 if (!map->stripes[i].dev) {
2918 map->stripes[i].dev =
2919 add_missing_dev(root, devid, uuid);
2920 if (!map->stripes[i].dev) {
2922 free_extent_map(em);
2926 map->stripes[i].dev->in_fs_metadata = 1;
2929 spin_lock(&map_tree->map_tree.lock);
2930 ret = add_extent_mapping(&map_tree->map_tree, em);
2931 spin_unlock(&map_tree->map_tree.lock);
2933 free_extent_map(em);
2938 static int fill_device_from_item(struct extent_buffer *leaf,
2939 struct btrfs_dev_item *dev_item,
2940 struct btrfs_device *device)
2944 device->devid = btrfs_device_id(leaf, dev_item);
2945 device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
2946 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
2947 device->type = btrfs_device_type(leaf, dev_item);
2948 device->io_align = btrfs_device_io_align(leaf, dev_item);
2949 device->io_width = btrfs_device_io_width(leaf, dev_item);
2950 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
2952 ptr = (unsigned long)btrfs_device_uuid(dev_item);
2953 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
2958 static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
2960 struct btrfs_fs_devices *fs_devices;
2963 mutex_lock(&uuid_mutex);
2965 fs_devices = root->fs_info->fs_devices->seed;
2966 while (fs_devices) {
2967 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
2971 fs_devices = fs_devices->seed;
2974 fs_devices = find_fsid(fsid);
2979 if (fs_devices->opened) {
2984 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
2985 root->fs_info->bdev_holder);
2989 if (!fs_devices->seeding) {
2990 __btrfs_close_devices(fs_devices);
2995 fs_devices->seed = root->fs_info->fs_devices->seed;
2996 root->fs_info->fs_devices->seed = fs_devices;
2997 fs_devices->sprouted = 1;
2999 mutex_unlock(&uuid_mutex);
3003 static int read_one_dev(struct btrfs_root *root,
3004 struct extent_buffer *leaf,
3005 struct btrfs_dev_item *dev_item)
3007 struct btrfs_device *device;
3010 int seed_devices = 0;
3011 u8 fs_uuid[BTRFS_UUID_SIZE];
3012 u8 dev_uuid[BTRFS_UUID_SIZE];
3014 devid = btrfs_device_id(leaf, dev_item);
3015 read_extent_buffer(leaf, dev_uuid,
3016 (unsigned long)btrfs_device_uuid(dev_item),
3018 read_extent_buffer(leaf, fs_uuid,
3019 (unsigned long)btrfs_device_fsid(dev_item),
3022 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
3023 ret = open_seed_devices(root, fs_uuid);
3029 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
3030 if (!device || !device->bdev) {
3031 if (!btrfs_test_opt(root, DEGRADED) || seed_devices)
3035 printk("warning devid %Lu missing\n", devid);
3036 device = add_missing_dev(root, devid, dev_uuid);
3042 if (device->fs_devices != root->fs_info->fs_devices) {
3043 BUG_ON(device->writeable);
3044 if (device->generation !=
3045 btrfs_device_generation(leaf, dev_item))
3049 fill_device_from_item(leaf, dev_item, device);
3050 device->dev_root = root->fs_info->dev_root;
3051 device->in_fs_metadata = 1;
3052 if (device->writeable)
3053 device->fs_devices->total_rw_bytes += device->total_bytes;
3056 ret = btrfs_open_device(device);
3064 int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
3066 struct btrfs_dev_item *dev_item;
3068 dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
3070 return read_one_dev(root, buf, dev_item);
3073 int btrfs_read_sys_array(struct btrfs_root *root, u64 sb_bytenr)
3075 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
3076 struct extent_buffer *sb;
3077 struct btrfs_disk_key *disk_key;
3078 struct btrfs_chunk *chunk;
3080 unsigned long sb_ptr;
3086 struct btrfs_key key;
3088 sb = btrfs_find_create_tree_block(root, sb_bytenr,
3089 BTRFS_SUPER_INFO_SIZE);
3092 btrfs_set_buffer_uptodate(sb);
3093 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
3094 array_size = btrfs_super_sys_array_size(super_copy);
3096 ptr = super_copy->sys_chunk_array;
3097 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
3100 while (cur < array_size) {
3101 disk_key = (struct btrfs_disk_key *)ptr;
3102 btrfs_disk_key_to_cpu(&key, disk_key);
3104 len = sizeof(*disk_key); ptr += len;
3108 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
3109 chunk = (struct btrfs_chunk *)sb_ptr;
3110 ret = read_one_chunk(root, &key, sb, chunk);
3113 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
3114 len = btrfs_chunk_item_size(num_stripes);
3123 free_extent_buffer(sb);
3127 int btrfs_read_chunk_tree(struct btrfs_root *root)
3129 struct btrfs_path *path;
3130 struct extent_buffer *leaf;
3131 struct btrfs_key key;
3132 struct btrfs_key found_key;
3136 root = root->fs_info->chunk_root;
3138 path = btrfs_alloc_path();
3142 /* first we search for all of the device items, and then we
3143 * read in all of the chunk items. This way we can create chunk
3144 * mappings that reference all of the devices that are afound
3146 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
3150 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3152 leaf = path->nodes[0];
3153 slot = path->slots[0];
3154 if (slot >= btrfs_header_nritems(leaf)) {
3155 ret = btrfs_next_leaf(root, path);
3162 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3163 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3164 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
3166 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
3167 struct btrfs_dev_item *dev_item;
3168 dev_item = btrfs_item_ptr(leaf, slot,
3169 struct btrfs_dev_item);
3170 ret = read_one_dev(root, leaf, dev_item);
3174 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
3175 struct btrfs_chunk *chunk;
3176 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3177 ret = read_one_chunk(root, &found_key, leaf, chunk);
3183 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3185 btrfs_release_path(root, path);
3190 btrfs_free_path(path);