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);
158 /* take all the bios off the list at once and process them
159 * later on (without the lock held). But, remember the
160 * tail and other pointers so the bios can be properly reinserted
161 * into the list if we hit congestion
163 pending = device->pending_bios;
164 tail = device->pending_bio_tail;
165 WARN_ON(pending && !tail);
166 device->pending_bios = NULL;
167 device->pending_bio_tail = NULL;
170 * if pending was null this time around, no bios need processing
171 * at all and we can stop. Otherwise it'll loop back up again
172 * and do an additional check so no bios are missed.
174 * device->running_pending is used to synchronize with the
179 device->running_pending = 1;
182 device->running_pending = 0;
184 spin_unlock(&device->io_lock);
188 pending = pending->bi_next;
190 atomic_dec(&fs_info->nr_async_bios);
192 if (atomic_read(&fs_info->nr_async_bios) < limit &&
193 waitqueue_active(&fs_info->async_submit_wait))
194 wake_up(&fs_info->async_submit_wait);
196 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
198 submit_bio(cur->bi_rw, cur);
203 * we made progress, there is more work to do and the bdi
204 * is now congested. Back off and let other work structs
207 if (pending && bdi_write_congested(bdi) && num_run > 16 &&
208 fs_info->fs_devices->open_devices > 1) {
209 struct bio *old_head;
211 spin_lock(&device->io_lock);
213 old_head = device->pending_bios;
214 device->pending_bios = pending;
215 if (device->pending_bio_tail)
216 tail->bi_next = old_head;
218 device->pending_bio_tail = tail;
220 device->running_pending = 1;
222 spin_unlock(&device->io_lock);
223 btrfs_requeue_work(&device->work);
230 spin_lock(&device->io_lock);
231 if (device->pending_bios)
233 spin_unlock(&device->io_lock);
238 static void pending_bios_fn(struct btrfs_work *work)
240 struct btrfs_device *device;
242 device = container_of(work, struct btrfs_device, work);
243 run_scheduled_bios(device);
246 static noinline int device_list_add(const char *path,
247 struct btrfs_super_block *disk_super,
248 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
250 struct btrfs_device *device;
251 struct btrfs_fs_devices *fs_devices;
252 u64 found_transid = btrfs_super_generation(disk_super);
254 fs_devices = find_fsid(disk_super->fsid);
256 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
259 INIT_LIST_HEAD(&fs_devices->devices);
260 INIT_LIST_HEAD(&fs_devices->alloc_list);
261 list_add(&fs_devices->list, &fs_uuids);
262 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
263 fs_devices->latest_devid = devid;
264 fs_devices->latest_trans = found_transid;
267 device = __find_device(&fs_devices->devices, devid,
268 disk_super->dev_item.uuid);
271 if (fs_devices->opened)
274 device = kzalloc(sizeof(*device), GFP_NOFS);
276 /* we can safely leave the fs_devices entry around */
279 device->devid = devid;
280 device->work.func = pending_bios_fn;
281 memcpy(device->uuid, disk_super->dev_item.uuid,
283 device->barriers = 1;
284 spin_lock_init(&device->io_lock);
285 device->name = kstrdup(path, GFP_NOFS);
290 INIT_LIST_HEAD(&device->dev_alloc_list);
291 list_add(&device->dev_list, &fs_devices->devices);
292 device->fs_devices = fs_devices;
293 fs_devices->num_devices++;
296 if (found_transid > fs_devices->latest_trans) {
297 fs_devices->latest_devid = devid;
298 fs_devices->latest_trans = found_transid;
300 *fs_devices_ret = fs_devices;
304 static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
306 struct btrfs_fs_devices *fs_devices;
307 struct btrfs_device *device;
308 struct btrfs_device *orig_dev;
310 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
312 return ERR_PTR(-ENOMEM);
314 INIT_LIST_HEAD(&fs_devices->devices);
315 INIT_LIST_HEAD(&fs_devices->alloc_list);
316 INIT_LIST_HEAD(&fs_devices->list);
317 fs_devices->latest_devid = orig->latest_devid;
318 fs_devices->latest_trans = orig->latest_trans;
319 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
321 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
322 device = kzalloc(sizeof(*device), GFP_NOFS);
326 device->name = kstrdup(orig_dev->name, GFP_NOFS);
330 device->devid = orig_dev->devid;
331 device->work.func = pending_bios_fn;
332 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
333 device->barriers = 1;
334 spin_lock_init(&device->io_lock);
335 INIT_LIST_HEAD(&device->dev_list);
336 INIT_LIST_HEAD(&device->dev_alloc_list);
338 list_add(&device->dev_list, &fs_devices->devices);
339 device->fs_devices = fs_devices;
340 fs_devices->num_devices++;
344 free_fs_devices(fs_devices);
345 return ERR_PTR(-ENOMEM);
348 int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
350 struct btrfs_device *device, *next;
352 mutex_lock(&uuid_mutex);
354 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
355 if (device->in_fs_metadata)
359 close_bdev_exclusive(device->bdev, device->mode);
361 fs_devices->open_devices--;
363 if (device->writeable) {
364 list_del_init(&device->dev_alloc_list);
365 device->writeable = 0;
366 fs_devices->rw_devices--;
368 list_del_init(&device->dev_list);
369 fs_devices->num_devices--;
374 if (fs_devices->seed) {
375 fs_devices = fs_devices->seed;
379 mutex_unlock(&uuid_mutex);
383 static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
385 struct btrfs_device *device;
387 if (--fs_devices->opened > 0)
390 list_for_each_entry(device, &fs_devices->devices, dev_list) {
392 close_bdev_exclusive(device->bdev, device->mode);
393 fs_devices->open_devices--;
395 if (device->writeable) {
396 list_del_init(&device->dev_alloc_list);
397 fs_devices->rw_devices--;
401 device->writeable = 0;
402 device->in_fs_metadata = 0;
404 WARN_ON(fs_devices->open_devices);
405 WARN_ON(fs_devices->rw_devices);
406 fs_devices->opened = 0;
407 fs_devices->seeding = 0;
412 int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
414 struct btrfs_fs_devices *seed_devices = NULL;
417 mutex_lock(&uuid_mutex);
418 ret = __btrfs_close_devices(fs_devices);
419 if (!fs_devices->opened) {
420 seed_devices = fs_devices->seed;
421 fs_devices->seed = NULL;
423 mutex_unlock(&uuid_mutex);
425 while (seed_devices) {
426 fs_devices = seed_devices;
427 seed_devices = fs_devices->seed;
428 __btrfs_close_devices(fs_devices);
429 free_fs_devices(fs_devices);
434 static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
435 fmode_t flags, void *holder)
437 struct block_device *bdev;
438 struct list_head *head = &fs_devices->devices;
439 struct btrfs_device *device;
440 struct block_device *latest_bdev = NULL;
441 struct buffer_head *bh;
442 struct btrfs_super_block *disk_super;
443 u64 latest_devid = 0;
444 u64 latest_transid = 0;
449 list_for_each_entry(device, head, dev_list) {
455 bdev = open_bdev_exclusive(device->name, flags, holder);
457 printk(KERN_INFO "open %s failed\n", device->name);
460 set_blocksize(bdev, 4096);
462 bh = btrfs_read_dev_super(bdev);
466 disk_super = (struct btrfs_super_block *)bh->b_data;
467 devid = le64_to_cpu(disk_super->dev_item.devid);
468 if (devid != device->devid)
471 if (memcmp(device->uuid, disk_super->dev_item.uuid,
475 device->generation = btrfs_super_generation(disk_super);
476 if (!latest_transid || device->generation > latest_transid) {
477 latest_devid = devid;
478 latest_transid = device->generation;
482 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
483 device->writeable = 0;
485 device->writeable = !bdev_read_only(bdev);
490 device->in_fs_metadata = 0;
491 device->mode = flags;
493 fs_devices->open_devices++;
494 if (device->writeable) {
495 fs_devices->rw_devices++;
496 list_add(&device->dev_alloc_list,
497 &fs_devices->alloc_list);
504 close_bdev_exclusive(bdev, FMODE_READ);
508 if (fs_devices->open_devices == 0) {
512 fs_devices->seeding = seeding;
513 fs_devices->opened = 1;
514 fs_devices->latest_bdev = latest_bdev;
515 fs_devices->latest_devid = latest_devid;
516 fs_devices->latest_trans = latest_transid;
517 fs_devices->total_rw_bytes = 0;
522 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
523 fmode_t flags, void *holder)
527 mutex_lock(&uuid_mutex);
528 if (fs_devices->opened) {
529 fs_devices->opened++;
532 ret = __btrfs_open_devices(fs_devices, flags, holder);
534 mutex_unlock(&uuid_mutex);
538 int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
539 struct btrfs_fs_devices **fs_devices_ret)
541 struct btrfs_super_block *disk_super;
542 struct block_device *bdev;
543 struct buffer_head *bh;
548 mutex_lock(&uuid_mutex);
550 bdev = open_bdev_exclusive(path, flags, holder);
557 ret = set_blocksize(bdev, 4096);
560 bh = btrfs_read_dev_super(bdev);
565 disk_super = (struct btrfs_super_block *)bh->b_data;
566 devid = le64_to_cpu(disk_super->dev_item.devid);
567 transid = btrfs_super_generation(disk_super);
568 if (disk_super->label[0])
569 printk(KERN_INFO "device label %s ", disk_super->label);
571 /* FIXME, make a readl uuid parser */
572 printk(KERN_INFO "device fsid %llx-%llx ",
573 *(unsigned long long *)disk_super->fsid,
574 *(unsigned long long *)(disk_super->fsid + 8));
576 printk(KERN_CONT "devid %llu transid %llu %s\n",
577 (unsigned long long)devid, (unsigned long long)transid, path);
578 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
582 close_bdev_exclusive(bdev, flags);
584 mutex_unlock(&uuid_mutex);
589 * this uses a pretty simple search, the expectation is that it is
590 * called very infrequently and that a given device has a small number
593 static noinline int find_free_dev_extent(struct btrfs_trans_handle *trans,
594 struct btrfs_device *device,
595 u64 num_bytes, u64 *start)
597 struct btrfs_key key;
598 struct btrfs_root *root = device->dev_root;
599 struct btrfs_dev_extent *dev_extent = NULL;
600 struct btrfs_path *path;
603 u64 search_start = 0;
604 u64 search_end = device->total_bytes;
608 struct extent_buffer *l;
610 path = btrfs_alloc_path();
616 /* FIXME use last free of some kind */
618 /* we don't want to overwrite the superblock on the drive,
619 * so we make sure to start at an offset of at least 1MB
621 search_start = max((u64)1024 * 1024, search_start);
623 if (root->fs_info->alloc_start + num_bytes <= device->total_bytes)
624 search_start = max(root->fs_info->alloc_start, search_start);
626 key.objectid = device->devid;
627 key.offset = search_start;
628 key.type = BTRFS_DEV_EXTENT_KEY;
629 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
632 ret = btrfs_previous_item(root, path, 0, key.type);
636 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
639 slot = path->slots[0];
640 if (slot >= btrfs_header_nritems(l)) {
641 ret = btrfs_next_leaf(root, path);
648 if (search_start >= search_end) {
652 *start = search_start;
656 *start = last_byte > search_start ?
657 last_byte : search_start;
658 if (search_end <= *start) {
664 btrfs_item_key_to_cpu(l, &key, slot);
666 if (key.objectid < device->devid)
669 if (key.objectid > device->devid)
672 if (key.offset >= search_start && key.offset > last_byte &&
674 if (last_byte < search_start)
675 last_byte = search_start;
676 hole_size = key.offset - last_byte;
677 if (key.offset > last_byte &&
678 hole_size >= num_bytes) {
683 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
687 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
688 last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent);
694 /* we have to make sure we didn't find an extent that has already
695 * been allocated by the map tree or the original allocation
697 BUG_ON(*start < search_start);
699 if (*start + num_bytes > search_end) {
703 /* check for pending inserts here */
707 btrfs_free_path(path);
711 static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
712 struct btrfs_device *device,
716 struct btrfs_path *path;
717 struct btrfs_root *root = device->dev_root;
718 struct btrfs_key key;
719 struct btrfs_key found_key;
720 struct extent_buffer *leaf = NULL;
721 struct btrfs_dev_extent *extent = NULL;
723 path = btrfs_alloc_path();
727 key.objectid = device->devid;
729 key.type = BTRFS_DEV_EXTENT_KEY;
731 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
733 ret = btrfs_previous_item(root, path, key.objectid,
734 BTRFS_DEV_EXTENT_KEY);
736 leaf = path->nodes[0];
737 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
738 extent = btrfs_item_ptr(leaf, path->slots[0],
739 struct btrfs_dev_extent);
740 BUG_ON(found_key.offset > start || found_key.offset +
741 btrfs_dev_extent_length(leaf, extent) < start);
743 } else if (ret == 0) {
744 leaf = path->nodes[0];
745 extent = btrfs_item_ptr(leaf, path->slots[0],
746 struct btrfs_dev_extent);
750 if (device->bytes_used > 0)
751 device->bytes_used -= btrfs_dev_extent_length(leaf, extent);
752 ret = btrfs_del_item(trans, root, path);
755 btrfs_free_path(path);
759 int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
760 struct btrfs_device *device,
761 u64 chunk_tree, u64 chunk_objectid,
762 u64 chunk_offset, u64 start, u64 num_bytes)
765 struct btrfs_path *path;
766 struct btrfs_root *root = device->dev_root;
767 struct btrfs_dev_extent *extent;
768 struct extent_buffer *leaf;
769 struct btrfs_key key;
771 WARN_ON(!device->in_fs_metadata);
772 path = btrfs_alloc_path();
776 key.objectid = device->devid;
778 key.type = BTRFS_DEV_EXTENT_KEY;
779 ret = btrfs_insert_empty_item(trans, root, path, &key,
783 leaf = path->nodes[0];
784 extent = btrfs_item_ptr(leaf, path->slots[0],
785 struct btrfs_dev_extent);
786 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
787 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
788 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
790 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
791 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
794 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
795 btrfs_mark_buffer_dirty(leaf);
796 btrfs_free_path(path);
800 static noinline int find_next_chunk(struct btrfs_root *root,
801 u64 objectid, u64 *offset)
803 struct btrfs_path *path;
805 struct btrfs_key key;
806 struct btrfs_chunk *chunk;
807 struct btrfs_key found_key;
809 path = btrfs_alloc_path();
812 key.objectid = objectid;
813 key.offset = (u64)-1;
814 key.type = BTRFS_CHUNK_ITEM_KEY;
816 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
822 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
826 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
828 if (found_key.objectid != objectid)
831 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
833 *offset = found_key.offset +
834 btrfs_chunk_length(path->nodes[0], chunk);
839 btrfs_free_path(path);
843 static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
846 struct btrfs_key key;
847 struct btrfs_key found_key;
848 struct btrfs_path *path;
850 root = root->fs_info->chunk_root;
852 path = btrfs_alloc_path();
856 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
857 key.type = BTRFS_DEV_ITEM_KEY;
858 key.offset = (u64)-1;
860 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
866 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
871 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
873 *objectid = found_key.offset + 1;
877 btrfs_free_path(path);
882 * the device information is stored in the chunk root
883 * the btrfs_device struct should be fully filled in
885 int btrfs_add_device(struct btrfs_trans_handle *trans,
886 struct btrfs_root *root,
887 struct btrfs_device *device)
890 struct btrfs_path *path;
891 struct btrfs_dev_item *dev_item;
892 struct extent_buffer *leaf;
893 struct btrfs_key key;
896 root = root->fs_info->chunk_root;
898 path = btrfs_alloc_path();
902 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
903 key.type = BTRFS_DEV_ITEM_KEY;
904 key.offset = device->devid;
906 ret = btrfs_insert_empty_item(trans, root, path, &key,
911 leaf = path->nodes[0];
912 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
914 btrfs_set_device_id(leaf, dev_item, device->devid);
915 btrfs_set_device_generation(leaf, dev_item, 0);
916 btrfs_set_device_type(leaf, dev_item, device->type);
917 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
918 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
919 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
920 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
921 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
922 btrfs_set_device_group(leaf, dev_item, 0);
923 btrfs_set_device_seek_speed(leaf, dev_item, 0);
924 btrfs_set_device_bandwidth(leaf, dev_item, 0);
925 btrfs_set_device_start_offset(leaf, dev_item, 0);
927 ptr = (unsigned long)btrfs_device_uuid(dev_item);
928 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
929 ptr = (unsigned long)btrfs_device_fsid(dev_item);
930 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
931 btrfs_mark_buffer_dirty(leaf);
935 btrfs_free_path(path);
939 static int btrfs_rm_dev_item(struct btrfs_root *root,
940 struct btrfs_device *device)
943 struct btrfs_path *path;
944 struct btrfs_key key;
945 struct btrfs_trans_handle *trans;
947 root = root->fs_info->chunk_root;
949 path = btrfs_alloc_path();
953 trans = btrfs_start_transaction(root, 1);
954 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
955 key.type = BTRFS_DEV_ITEM_KEY;
956 key.offset = device->devid;
959 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
968 ret = btrfs_del_item(trans, root, path);
972 btrfs_free_path(path);
974 btrfs_commit_transaction(trans, root);
978 int btrfs_rm_device(struct btrfs_root *root, char *device_path)
980 struct btrfs_device *device;
981 struct btrfs_device *next_device;
982 struct block_device *bdev;
983 struct buffer_head *bh = NULL;
984 struct btrfs_super_block *disk_super;
991 mutex_lock(&uuid_mutex);
992 mutex_lock(&root->fs_info->volume_mutex);
994 all_avail = root->fs_info->avail_data_alloc_bits |
995 root->fs_info->avail_system_alloc_bits |
996 root->fs_info->avail_metadata_alloc_bits;
998 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
999 root->fs_info->fs_devices->rw_devices <= 4) {
1000 printk(KERN_ERR "btrfs: unable to go below four devices "
1006 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
1007 root->fs_info->fs_devices->rw_devices <= 2) {
1008 printk(KERN_ERR "btrfs: unable to go below two "
1009 "devices on raid1\n");
1014 if (strcmp(device_path, "missing") == 0) {
1015 struct list_head *devices;
1016 struct btrfs_device *tmp;
1019 devices = &root->fs_info->fs_devices->devices;
1020 list_for_each_entry(tmp, devices, dev_list) {
1021 if (tmp->in_fs_metadata && !tmp->bdev) {
1030 printk(KERN_ERR "btrfs: no missing devices found to "
1035 bdev = open_bdev_exclusive(device_path, FMODE_READ,
1036 root->fs_info->bdev_holder);
1038 ret = PTR_ERR(bdev);
1042 set_blocksize(bdev, 4096);
1043 bh = btrfs_read_dev_super(bdev);
1048 disk_super = (struct btrfs_super_block *)bh->b_data;
1049 devid = le64_to_cpu(disk_super->dev_item.devid);
1050 dev_uuid = disk_super->dev_item.uuid;
1051 device = btrfs_find_device(root, devid, dev_uuid,
1059 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
1060 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1066 if (device->writeable) {
1067 list_del_init(&device->dev_alloc_list);
1068 root->fs_info->fs_devices->rw_devices--;
1071 ret = btrfs_shrink_device(device, 0);
1075 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1079 device->in_fs_metadata = 0;
1080 list_del_init(&device->dev_list);
1081 device->fs_devices->num_devices--;
1083 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1084 struct btrfs_device, dev_list);
1085 if (device->bdev == root->fs_info->sb->s_bdev)
1086 root->fs_info->sb->s_bdev = next_device->bdev;
1087 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1088 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1091 close_bdev_exclusive(device->bdev, device->mode);
1092 device->bdev = NULL;
1093 device->fs_devices->open_devices--;
1096 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
1097 btrfs_set_super_num_devices(&root->fs_info->super_copy, num_devices);
1099 if (device->fs_devices->open_devices == 0) {
1100 struct btrfs_fs_devices *fs_devices;
1101 fs_devices = root->fs_info->fs_devices;
1102 while (fs_devices) {
1103 if (fs_devices->seed == device->fs_devices)
1105 fs_devices = fs_devices->seed;
1107 fs_devices->seed = device->fs_devices->seed;
1108 device->fs_devices->seed = NULL;
1109 __btrfs_close_devices(device->fs_devices);
1110 free_fs_devices(device->fs_devices);
1114 * at this point, the device is zero sized. We want to
1115 * remove it from the devices list and zero out the old super
1117 if (device->writeable) {
1118 /* make sure this device isn't detected as part of
1121 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1122 set_buffer_dirty(bh);
1123 sync_dirty_buffer(bh);
1126 kfree(device->name);
1134 close_bdev_exclusive(bdev, FMODE_READ);
1136 mutex_unlock(&root->fs_info->volume_mutex);
1137 mutex_unlock(&uuid_mutex);
1142 * does all the dirty work required for changing file system's UUID.
1144 static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans,
1145 struct btrfs_root *root)
1147 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1148 struct btrfs_fs_devices *old_devices;
1149 struct btrfs_fs_devices *seed_devices;
1150 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
1151 struct btrfs_device *device;
1154 BUG_ON(!mutex_is_locked(&uuid_mutex));
1155 if (!fs_devices->seeding)
1158 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1162 old_devices = clone_fs_devices(fs_devices);
1163 if (IS_ERR(old_devices)) {
1164 kfree(seed_devices);
1165 return PTR_ERR(old_devices);
1168 list_add(&old_devices->list, &fs_uuids);
1170 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1171 seed_devices->opened = 1;
1172 INIT_LIST_HEAD(&seed_devices->devices);
1173 INIT_LIST_HEAD(&seed_devices->alloc_list);
1174 list_splice_init(&fs_devices->devices, &seed_devices->devices);
1175 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1176 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1177 device->fs_devices = seed_devices;
1180 fs_devices->seeding = 0;
1181 fs_devices->num_devices = 0;
1182 fs_devices->open_devices = 0;
1183 fs_devices->seed = seed_devices;
1185 generate_random_uuid(fs_devices->fsid);
1186 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1187 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1188 super_flags = btrfs_super_flags(disk_super) &
1189 ~BTRFS_SUPER_FLAG_SEEDING;
1190 btrfs_set_super_flags(disk_super, super_flags);
1196 * strore the expected generation for seed devices in device items.
1198 static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1199 struct btrfs_root *root)
1201 struct btrfs_path *path;
1202 struct extent_buffer *leaf;
1203 struct btrfs_dev_item *dev_item;
1204 struct btrfs_device *device;
1205 struct btrfs_key key;
1206 u8 fs_uuid[BTRFS_UUID_SIZE];
1207 u8 dev_uuid[BTRFS_UUID_SIZE];
1211 path = btrfs_alloc_path();
1215 root = root->fs_info->chunk_root;
1216 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1218 key.type = BTRFS_DEV_ITEM_KEY;
1221 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1225 leaf = path->nodes[0];
1227 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1228 ret = btrfs_next_leaf(root, path);
1233 leaf = path->nodes[0];
1234 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1235 btrfs_release_path(root, path);
1239 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1240 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1241 key.type != BTRFS_DEV_ITEM_KEY)
1244 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1245 struct btrfs_dev_item);
1246 devid = btrfs_device_id(leaf, dev_item);
1247 read_extent_buffer(leaf, dev_uuid,
1248 (unsigned long)btrfs_device_uuid(dev_item),
1250 read_extent_buffer(leaf, fs_uuid,
1251 (unsigned long)btrfs_device_fsid(dev_item),
1253 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1256 if (device->fs_devices->seeding) {
1257 btrfs_set_device_generation(leaf, dev_item,
1258 device->generation);
1259 btrfs_mark_buffer_dirty(leaf);
1267 btrfs_free_path(path);
1271 int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1273 struct btrfs_trans_handle *trans;
1274 struct btrfs_device *device;
1275 struct block_device *bdev;
1276 struct list_head *devices;
1277 struct super_block *sb = root->fs_info->sb;
1279 int seeding_dev = 0;
1282 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1285 bdev = open_bdev_exclusive(device_path, 0, root->fs_info->bdev_holder);
1289 if (root->fs_info->fs_devices->seeding) {
1291 down_write(&sb->s_umount);
1292 mutex_lock(&uuid_mutex);
1295 filemap_write_and_wait(bdev->bd_inode->i_mapping);
1296 mutex_lock(&root->fs_info->volume_mutex);
1298 devices = &root->fs_info->fs_devices->devices;
1299 list_for_each_entry(device, devices, dev_list) {
1300 if (device->bdev == bdev) {
1306 device = kzalloc(sizeof(*device), GFP_NOFS);
1308 /* we can safely leave the fs_devices entry around */
1313 device->name = kstrdup(device_path, GFP_NOFS);
1314 if (!device->name) {
1320 ret = find_next_devid(root, &device->devid);
1326 trans = btrfs_start_transaction(root, 1);
1329 device->barriers = 1;
1330 device->writeable = 1;
1331 device->work.func = pending_bios_fn;
1332 generate_random_uuid(device->uuid);
1333 spin_lock_init(&device->io_lock);
1334 device->generation = trans->transid;
1335 device->io_width = root->sectorsize;
1336 device->io_align = root->sectorsize;
1337 device->sector_size = root->sectorsize;
1338 device->total_bytes = i_size_read(bdev->bd_inode);
1339 device->dev_root = root->fs_info->dev_root;
1340 device->bdev = bdev;
1341 device->in_fs_metadata = 1;
1343 set_blocksize(device->bdev, 4096);
1346 sb->s_flags &= ~MS_RDONLY;
1347 ret = btrfs_prepare_sprout(trans, root);
1351 device->fs_devices = root->fs_info->fs_devices;
1352 list_add(&device->dev_list, &root->fs_info->fs_devices->devices);
1353 list_add(&device->dev_alloc_list,
1354 &root->fs_info->fs_devices->alloc_list);
1355 root->fs_info->fs_devices->num_devices++;
1356 root->fs_info->fs_devices->open_devices++;
1357 root->fs_info->fs_devices->rw_devices++;
1358 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1360 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
1361 btrfs_set_super_total_bytes(&root->fs_info->super_copy,
1362 total_bytes + device->total_bytes);
1364 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
1365 btrfs_set_super_num_devices(&root->fs_info->super_copy,
1369 ret = init_first_rw_device(trans, root, device);
1371 ret = btrfs_finish_sprout(trans, root);
1374 ret = btrfs_add_device(trans, root, device);
1377 unlock_chunks(root);
1378 btrfs_commit_transaction(trans, root);
1381 mutex_unlock(&uuid_mutex);
1382 up_write(&sb->s_umount);
1384 ret = btrfs_relocate_sys_chunks(root);
1388 mutex_unlock(&root->fs_info->volume_mutex);
1391 close_bdev_exclusive(bdev, 0);
1393 mutex_unlock(&uuid_mutex);
1394 up_write(&sb->s_umount);
1399 static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1400 struct btrfs_device *device)
1403 struct btrfs_path *path;
1404 struct btrfs_root *root;
1405 struct btrfs_dev_item *dev_item;
1406 struct extent_buffer *leaf;
1407 struct btrfs_key key;
1409 root = device->dev_root->fs_info->chunk_root;
1411 path = btrfs_alloc_path();
1415 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1416 key.type = BTRFS_DEV_ITEM_KEY;
1417 key.offset = device->devid;
1419 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1428 leaf = path->nodes[0];
1429 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1431 btrfs_set_device_id(leaf, dev_item, device->devid);
1432 btrfs_set_device_type(leaf, dev_item, device->type);
1433 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1434 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1435 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
1436 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1437 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1438 btrfs_mark_buffer_dirty(leaf);
1441 btrfs_free_path(path);
1445 static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
1446 struct btrfs_device *device, u64 new_size)
1448 struct btrfs_super_block *super_copy =
1449 &device->dev_root->fs_info->super_copy;
1450 u64 old_total = btrfs_super_total_bytes(super_copy);
1451 u64 diff = new_size - device->total_bytes;
1453 if (!device->writeable)
1455 if (new_size <= device->total_bytes)
1458 btrfs_set_super_total_bytes(super_copy, old_total + diff);
1459 device->fs_devices->total_rw_bytes += diff;
1461 device->total_bytes = new_size;
1462 btrfs_clear_space_info_full(device->dev_root->fs_info);
1464 return btrfs_update_device(trans, device);
1467 int btrfs_grow_device(struct btrfs_trans_handle *trans,
1468 struct btrfs_device *device, u64 new_size)
1471 lock_chunks(device->dev_root);
1472 ret = __btrfs_grow_device(trans, device, new_size);
1473 unlock_chunks(device->dev_root);
1477 static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1478 struct btrfs_root *root,
1479 u64 chunk_tree, u64 chunk_objectid,
1483 struct btrfs_path *path;
1484 struct btrfs_key key;
1486 root = root->fs_info->chunk_root;
1487 path = btrfs_alloc_path();
1491 key.objectid = chunk_objectid;
1492 key.offset = chunk_offset;
1493 key.type = BTRFS_CHUNK_ITEM_KEY;
1495 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1498 ret = btrfs_del_item(trans, root, path);
1501 btrfs_free_path(path);
1505 static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
1508 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1509 struct btrfs_disk_key *disk_key;
1510 struct btrfs_chunk *chunk;
1517 struct btrfs_key key;
1519 array_size = btrfs_super_sys_array_size(super_copy);
1521 ptr = super_copy->sys_chunk_array;
1524 while (cur < array_size) {
1525 disk_key = (struct btrfs_disk_key *)ptr;
1526 btrfs_disk_key_to_cpu(&key, disk_key);
1528 len = sizeof(*disk_key);
1530 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1531 chunk = (struct btrfs_chunk *)(ptr + len);
1532 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1533 len += btrfs_chunk_item_size(num_stripes);
1538 if (key.objectid == chunk_objectid &&
1539 key.offset == chunk_offset) {
1540 memmove(ptr, ptr + len, array_size - (cur + len));
1542 btrfs_set_super_sys_array_size(super_copy, array_size);
1551 static int btrfs_relocate_chunk(struct btrfs_root *root,
1552 u64 chunk_tree, u64 chunk_objectid,
1555 struct extent_map_tree *em_tree;
1556 struct btrfs_root *extent_root;
1557 struct btrfs_trans_handle *trans;
1558 struct extent_map *em;
1559 struct map_lookup *map;
1563 printk(KERN_INFO "btrfs relocating chunk %llu\n",
1564 (unsigned long long)chunk_offset);
1565 root = root->fs_info->chunk_root;
1566 extent_root = root->fs_info->extent_root;
1567 em_tree = &root->fs_info->mapping_tree.map_tree;
1569 /* step one, relocate all the extents inside this chunk */
1570 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
1573 trans = btrfs_start_transaction(root, 1);
1579 * step two, delete the device extents and the
1580 * chunk tree entries
1582 spin_lock(&em_tree->lock);
1583 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
1584 spin_unlock(&em_tree->lock);
1586 BUG_ON(em->start > chunk_offset ||
1587 em->start + em->len < chunk_offset);
1588 map = (struct map_lookup *)em->bdev;
1590 for (i = 0; i < map->num_stripes; i++) {
1591 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1592 map->stripes[i].physical);
1595 if (map->stripes[i].dev) {
1596 ret = btrfs_update_device(trans, map->stripes[i].dev);
1600 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1605 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1606 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1610 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
1613 spin_lock(&em_tree->lock);
1614 remove_extent_mapping(em_tree, em);
1615 spin_unlock(&em_tree->lock);
1620 /* once for the tree */
1621 free_extent_map(em);
1623 free_extent_map(em);
1625 unlock_chunks(root);
1626 btrfs_end_transaction(trans, root);
1630 static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
1632 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1633 struct btrfs_path *path;
1634 struct extent_buffer *leaf;
1635 struct btrfs_chunk *chunk;
1636 struct btrfs_key key;
1637 struct btrfs_key found_key;
1638 u64 chunk_tree = chunk_root->root_key.objectid;
1642 path = btrfs_alloc_path();
1646 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1647 key.offset = (u64)-1;
1648 key.type = BTRFS_CHUNK_ITEM_KEY;
1651 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1656 ret = btrfs_previous_item(chunk_root, path, key.objectid,
1663 leaf = path->nodes[0];
1664 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1666 chunk = btrfs_item_ptr(leaf, path->slots[0],
1667 struct btrfs_chunk);
1668 chunk_type = btrfs_chunk_type(leaf, chunk);
1669 btrfs_release_path(chunk_root, path);
1671 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
1672 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
1678 if (found_key.offset == 0)
1680 key.offset = found_key.offset - 1;
1684 btrfs_free_path(path);
1688 static u64 div_factor(u64 num, int factor)
1697 int btrfs_balance(struct btrfs_root *dev_root)
1700 struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
1701 struct btrfs_device *device;
1704 struct btrfs_path *path;
1705 struct btrfs_key key;
1706 struct btrfs_chunk *chunk;
1707 struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
1708 struct btrfs_trans_handle *trans;
1709 struct btrfs_key found_key;
1711 if (dev_root->fs_info->sb->s_flags & MS_RDONLY)
1714 mutex_lock(&dev_root->fs_info->volume_mutex);
1715 dev_root = dev_root->fs_info->dev_root;
1717 /* step one make some room on all the devices */
1718 list_for_each_entry(device, devices, dev_list) {
1719 old_size = device->total_bytes;
1720 size_to_free = div_factor(old_size, 1);
1721 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
1722 if (!device->writeable ||
1723 device->total_bytes - device->bytes_used > size_to_free)
1726 ret = btrfs_shrink_device(device, old_size - size_to_free);
1729 trans = btrfs_start_transaction(dev_root, 1);
1732 ret = btrfs_grow_device(trans, device, old_size);
1735 btrfs_end_transaction(trans, dev_root);
1738 /* step two, relocate all the chunks */
1739 path = btrfs_alloc_path();
1742 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1743 key.offset = (u64)-1;
1744 key.type = BTRFS_CHUNK_ITEM_KEY;
1747 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1752 * this shouldn't happen, it means the last relocate
1758 ret = btrfs_previous_item(chunk_root, path, 0,
1759 BTRFS_CHUNK_ITEM_KEY);
1763 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1765 if (found_key.objectid != key.objectid)
1768 chunk = btrfs_item_ptr(path->nodes[0],
1770 struct btrfs_chunk);
1771 key.offset = found_key.offset;
1772 /* chunk zero is special */
1773 if (key.offset == 0)
1776 btrfs_release_path(chunk_root, path);
1777 ret = btrfs_relocate_chunk(chunk_root,
1778 chunk_root->root_key.objectid,
1785 btrfs_free_path(path);
1786 mutex_unlock(&dev_root->fs_info->volume_mutex);
1791 * shrinking a device means finding all of the device extents past
1792 * the new size, and then following the back refs to the chunks.
1793 * The chunk relocation code actually frees the device extent
1795 int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
1797 struct btrfs_trans_handle *trans;
1798 struct btrfs_root *root = device->dev_root;
1799 struct btrfs_dev_extent *dev_extent = NULL;
1800 struct btrfs_path *path;
1807 struct extent_buffer *l;
1808 struct btrfs_key key;
1809 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1810 u64 old_total = btrfs_super_total_bytes(super_copy);
1811 u64 diff = device->total_bytes - new_size;
1813 if (new_size >= device->total_bytes)
1816 path = btrfs_alloc_path();
1820 trans = btrfs_start_transaction(root, 1);
1830 device->total_bytes = new_size;
1831 if (device->writeable)
1832 device->fs_devices->total_rw_bytes -= diff;
1833 ret = btrfs_update_device(trans, device);
1835 unlock_chunks(root);
1836 btrfs_end_transaction(trans, root);
1839 WARN_ON(diff > old_total);
1840 btrfs_set_super_total_bytes(super_copy, old_total - diff);
1841 unlock_chunks(root);
1842 btrfs_end_transaction(trans, root);
1844 key.objectid = device->devid;
1845 key.offset = (u64)-1;
1846 key.type = BTRFS_DEV_EXTENT_KEY;
1849 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1853 ret = btrfs_previous_item(root, path, 0, key.type);
1862 slot = path->slots[0];
1863 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
1865 if (key.objectid != device->devid)
1868 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
1869 length = btrfs_dev_extent_length(l, dev_extent);
1871 if (key.offset + length <= new_size)
1874 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
1875 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
1876 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
1877 btrfs_release_path(root, path);
1879 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
1886 btrfs_free_path(path);
1890 static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
1891 struct btrfs_root *root,
1892 struct btrfs_key *key,
1893 struct btrfs_chunk *chunk, int item_size)
1895 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1896 struct btrfs_disk_key disk_key;
1900 array_size = btrfs_super_sys_array_size(super_copy);
1901 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
1904 ptr = super_copy->sys_chunk_array + array_size;
1905 btrfs_cpu_key_to_disk(&disk_key, key);
1906 memcpy(ptr, &disk_key, sizeof(disk_key));
1907 ptr += sizeof(disk_key);
1908 memcpy(ptr, chunk, item_size);
1909 item_size += sizeof(disk_key);
1910 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
1914 static noinline u64 chunk_bytes_by_type(u64 type, u64 calc_size,
1915 int num_stripes, int sub_stripes)
1917 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
1919 else if (type & BTRFS_BLOCK_GROUP_RAID10)
1920 return calc_size * (num_stripes / sub_stripes);
1922 return calc_size * num_stripes;
1925 static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
1926 struct btrfs_root *extent_root,
1927 struct map_lookup **map_ret,
1928 u64 *num_bytes, u64 *stripe_size,
1929 u64 start, u64 type)
1931 struct btrfs_fs_info *info = extent_root->fs_info;
1932 struct btrfs_device *device = NULL;
1933 struct btrfs_fs_devices *fs_devices = info->fs_devices;
1934 struct list_head *cur;
1935 struct map_lookup *map = NULL;
1936 struct extent_map_tree *em_tree;
1937 struct extent_map *em;
1938 struct list_head private_devs;
1939 int min_stripe_size = 1 * 1024 * 1024;
1940 u64 calc_size = 1024 * 1024 * 1024;
1941 u64 max_chunk_size = calc_size;
1946 int num_stripes = 1;
1947 int min_stripes = 1;
1948 int sub_stripes = 0;
1952 int stripe_len = 64 * 1024;
1954 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
1955 (type & BTRFS_BLOCK_GROUP_DUP)) {
1957 type &= ~BTRFS_BLOCK_GROUP_DUP;
1959 if (list_empty(&fs_devices->alloc_list))
1962 if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
1963 num_stripes = fs_devices->rw_devices;
1966 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
1970 if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
1971 num_stripes = min_t(u64, 2, fs_devices->rw_devices);
1972 if (num_stripes < 2)
1976 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
1977 num_stripes = fs_devices->rw_devices;
1978 if (num_stripes < 4)
1980 num_stripes &= ~(u32)1;
1985 if (type & BTRFS_BLOCK_GROUP_DATA) {
1986 max_chunk_size = 10 * calc_size;
1987 min_stripe_size = 64 * 1024 * 1024;
1988 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
1989 max_chunk_size = 4 * calc_size;
1990 min_stripe_size = 32 * 1024 * 1024;
1991 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
1992 calc_size = 8 * 1024 * 1024;
1993 max_chunk_size = calc_size * 2;
1994 min_stripe_size = 1 * 1024 * 1024;
1997 /* we don't want a chunk larger than 10% of writeable space */
1998 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
2002 if (!map || map->num_stripes != num_stripes) {
2004 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
2007 map->num_stripes = num_stripes;
2010 if (calc_size * num_stripes > max_chunk_size) {
2011 calc_size = max_chunk_size;
2012 do_div(calc_size, num_stripes);
2013 do_div(calc_size, stripe_len);
2014 calc_size *= stripe_len;
2016 /* we don't want tiny stripes */
2017 calc_size = max_t(u64, min_stripe_size, calc_size);
2019 do_div(calc_size, stripe_len);
2020 calc_size *= stripe_len;
2022 cur = fs_devices->alloc_list.next;
2025 if (type & BTRFS_BLOCK_GROUP_DUP)
2026 min_free = calc_size * 2;
2028 min_free = calc_size;
2031 * we add 1MB because we never use the first 1MB of the device, unless
2032 * we've looped, then we are likely allocating the maximum amount of
2033 * space left already
2036 min_free += 1024 * 1024;
2038 INIT_LIST_HEAD(&private_devs);
2039 while (index < num_stripes) {
2040 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
2041 BUG_ON(!device->writeable);
2042 if (device->total_bytes > device->bytes_used)
2043 avail = device->total_bytes - device->bytes_used;
2048 if (device->in_fs_metadata && avail >= min_free) {
2049 ret = find_free_dev_extent(trans, device,
2050 min_free, &dev_offset);
2052 list_move_tail(&device->dev_alloc_list,
2054 map->stripes[index].dev = device;
2055 map->stripes[index].physical = dev_offset;
2057 if (type & BTRFS_BLOCK_GROUP_DUP) {
2058 map->stripes[index].dev = device;
2059 map->stripes[index].physical =
2060 dev_offset + calc_size;
2064 } else if (device->in_fs_metadata && avail > max_avail)
2066 if (cur == &fs_devices->alloc_list)
2069 list_splice(&private_devs, &fs_devices->alloc_list);
2070 if (index < num_stripes) {
2071 if (index >= min_stripes) {
2072 num_stripes = index;
2073 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2074 num_stripes /= sub_stripes;
2075 num_stripes *= sub_stripes;
2080 if (!looped && max_avail > 0) {
2082 calc_size = max_avail;
2088 map->sector_size = extent_root->sectorsize;
2089 map->stripe_len = stripe_len;
2090 map->io_align = stripe_len;
2091 map->io_width = stripe_len;
2093 map->num_stripes = num_stripes;
2094 map->sub_stripes = sub_stripes;
2097 *stripe_size = calc_size;
2098 *num_bytes = chunk_bytes_by_type(type, calc_size,
2099 num_stripes, sub_stripes);
2101 em = alloc_extent_map(GFP_NOFS);
2106 em->bdev = (struct block_device *)map;
2108 em->len = *num_bytes;
2109 em->block_start = 0;
2110 em->block_len = em->len;
2112 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
2113 spin_lock(&em_tree->lock);
2114 ret = add_extent_mapping(em_tree, em);
2115 spin_unlock(&em_tree->lock);
2117 free_extent_map(em);
2119 ret = btrfs_make_block_group(trans, extent_root, 0, type,
2120 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2125 while (index < map->num_stripes) {
2126 device = map->stripes[index].dev;
2127 dev_offset = map->stripes[index].physical;
2129 ret = btrfs_alloc_dev_extent(trans, device,
2130 info->chunk_root->root_key.objectid,
2131 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2132 start, dev_offset, calc_size);
2140 static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
2141 struct btrfs_root *extent_root,
2142 struct map_lookup *map, u64 chunk_offset,
2143 u64 chunk_size, u64 stripe_size)
2146 struct btrfs_key key;
2147 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2148 struct btrfs_device *device;
2149 struct btrfs_chunk *chunk;
2150 struct btrfs_stripe *stripe;
2151 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
2155 chunk = kzalloc(item_size, GFP_NOFS);
2160 while (index < map->num_stripes) {
2161 device = map->stripes[index].dev;
2162 device->bytes_used += stripe_size;
2163 ret = btrfs_update_device(trans, device);
2169 stripe = &chunk->stripe;
2170 while (index < map->num_stripes) {
2171 device = map->stripes[index].dev;
2172 dev_offset = map->stripes[index].physical;
2174 btrfs_set_stack_stripe_devid(stripe, device->devid);
2175 btrfs_set_stack_stripe_offset(stripe, dev_offset);
2176 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
2181 btrfs_set_stack_chunk_length(chunk, chunk_size);
2182 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
2183 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
2184 btrfs_set_stack_chunk_type(chunk, map->type);
2185 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
2186 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
2187 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
2188 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
2189 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
2191 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2192 key.type = BTRFS_CHUNK_ITEM_KEY;
2193 key.offset = chunk_offset;
2195 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
2198 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2199 ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
2208 * Chunk allocation falls into two parts. The first part does works
2209 * that make the new allocated chunk useable, but not do any operation
2210 * that modifies the chunk tree. The second part does the works that
2211 * require modifying the chunk tree. This division is important for the
2212 * bootstrap process of adding storage to a seed btrfs.
2214 int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2215 struct btrfs_root *extent_root, u64 type)
2220 struct map_lookup *map;
2221 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2224 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2229 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2230 &stripe_size, chunk_offset, type);
2234 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2235 chunk_size, stripe_size);
2240 static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
2241 struct btrfs_root *root,
2242 struct btrfs_device *device)
2245 u64 sys_chunk_offset;
2249 u64 sys_stripe_size;
2251 struct map_lookup *map;
2252 struct map_lookup *sys_map;
2253 struct btrfs_fs_info *fs_info = root->fs_info;
2254 struct btrfs_root *extent_root = fs_info->extent_root;
2257 ret = find_next_chunk(fs_info->chunk_root,
2258 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
2261 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
2262 (fs_info->metadata_alloc_profile &
2263 fs_info->avail_metadata_alloc_bits);
2264 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2266 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2267 &stripe_size, chunk_offset, alloc_profile);
2270 sys_chunk_offset = chunk_offset + chunk_size;
2272 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
2273 (fs_info->system_alloc_profile &
2274 fs_info->avail_system_alloc_bits);
2275 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2277 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
2278 &sys_chunk_size, &sys_stripe_size,
2279 sys_chunk_offset, alloc_profile);
2282 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
2286 * Modifying chunk tree needs allocating new blocks from both
2287 * system block group and metadata block group. So we only can
2288 * do operations require modifying the chunk tree after both
2289 * block groups were created.
2291 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2292 chunk_size, stripe_size);
2295 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
2296 sys_chunk_offset, sys_chunk_size,
2302 int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
2304 struct extent_map *em;
2305 struct map_lookup *map;
2306 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2310 spin_lock(&map_tree->map_tree.lock);
2311 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
2312 spin_unlock(&map_tree->map_tree.lock);
2316 map = (struct map_lookup *)em->bdev;
2317 for (i = 0; i < map->num_stripes; i++) {
2318 if (!map->stripes[i].dev->writeable) {
2323 free_extent_map(em);
2327 void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
2329 extent_map_tree_init(&tree->map_tree, GFP_NOFS);
2332 void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
2334 struct extent_map *em;
2337 spin_lock(&tree->map_tree.lock);
2338 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
2340 remove_extent_mapping(&tree->map_tree, em);
2341 spin_unlock(&tree->map_tree.lock);
2346 free_extent_map(em);
2347 /* once for the tree */
2348 free_extent_map(em);
2352 int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
2354 struct extent_map *em;
2355 struct map_lookup *map;
2356 struct extent_map_tree *em_tree = &map_tree->map_tree;
2359 spin_lock(&em_tree->lock);
2360 em = lookup_extent_mapping(em_tree, logical, len);
2361 spin_unlock(&em_tree->lock);
2364 BUG_ON(em->start > logical || em->start + em->len < logical);
2365 map = (struct map_lookup *)em->bdev;
2366 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
2367 ret = map->num_stripes;
2368 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2369 ret = map->sub_stripes;
2372 free_extent_map(em);
2376 static int find_live_mirror(struct map_lookup *map, int first, int num,
2380 if (map->stripes[optimal].dev->bdev)
2382 for (i = first; i < first + num; i++) {
2383 if (map->stripes[i].dev->bdev)
2386 /* we couldn't find one that doesn't fail. Just return something
2387 * and the io error handling code will clean up eventually
2392 static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2393 u64 logical, u64 *length,
2394 struct btrfs_multi_bio **multi_ret,
2395 int mirror_num, struct page *unplug_page)
2397 struct extent_map *em;
2398 struct map_lookup *map;
2399 struct extent_map_tree *em_tree = &map_tree->map_tree;
2403 int stripes_allocated = 8;
2404 int stripes_required = 1;
2409 struct btrfs_multi_bio *multi = NULL;
2411 if (multi_ret && !(rw & (1 << BIO_RW)))
2412 stripes_allocated = 1;
2415 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
2420 atomic_set(&multi->error, 0);
2423 spin_lock(&em_tree->lock);
2424 em = lookup_extent_mapping(em_tree, logical, *length);
2425 spin_unlock(&em_tree->lock);
2427 if (!em && unplug_page)
2431 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
2432 (unsigned long long)logical,
2433 (unsigned long long)*length);
2437 BUG_ON(em->start > logical || em->start + em->len < logical);
2438 map = (struct map_lookup *)em->bdev;
2439 offset = logical - em->start;
2441 if (mirror_num > map->num_stripes)
2444 /* if our multi bio struct is too small, back off and try again */
2445 if (rw & (1 << BIO_RW)) {
2446 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
2447 BTRFS_BLOCK_GROUP_DUP)) {
2448 stripes_required = map->num_stripes;
2450 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2451 stripes_required = map->sub_stripes;
2455 if (multi_ret && rw == WRITE &&
2456 stripes_allocated < stripes_required) {
2457 stripes_allocated = map->num_stripes;
2458 free_extent_map(em);
2464 * stripe_nr counts the total number of stripes we have to stride
2465 * to get to this block
2467 do_div(stripe_nr, map->stripe_len);
2469 stripe_offset = stripe_nr * map->stripe_len;
2470 BUG_ON(offset < stripe_offset);
2472 /* stripe_offset is the offset of this block in its stripe*/
2473 stripe_offset = offset - stripe_offset;
2475 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
2476 BTRFS_BLOCK_GROUP_RAID10 |
2477 BTRFS_BLOCK_GROUP_DUP)) {
2478 /* we limit the length of each bio to what fits in a stripe */
2479 *length = min_t(u64, em->len - offset,
2480 map->stripe_len - stripe_offset);
2482 *length = em->len - offset;
2485 if (!multi_ret && !unplug_page)
2490 if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
2491 if (unplug_page || (rw & (1 << BIO_RW)))
2492 num_stripes = map->num_stripes;
2493 else if (mirror_num)
2494 stripe_index = mirror_num - 1;
2496 stripe_index = find_live_mirror(map, 0,
2498 current->pid % map->num_stripes);
2501 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
2502 if (rw & (1 << BIO_RW))
2503 num_stripes = map->num_stripes;
2504 else if (mirror_num)
2505 stripe_index = mirror_num - 1;
2507 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2508 int factor = map->num_stripes / map->sub_stripes;
2510 stripe_index = do_div(stripe_nr, factor);
2511 stripe_index *= map->sub_stripes;
2513 if (unplug_page || (rw & (1 << BIO_RW)))
2514 num_stripes = map->sub_stripes;
2515 else if (mirror_num)
2516 stripe_index += mirror_num - 1;
2518 stripe_index = find_live_mirror(map, stripe_index,
2519 map->sub_stripes, stripe_index +
2520 current->pid % map->sub_stripes);
2524 * after this do_div call, stripe_nr is the number of stripes
2525 * on this device we have to walk to find the data, and
2526 * stripe_index is the number of our device in the stripe array
2528 stripe_index = do_div(stripe_nr, map->num_stripes);
2530 BUG_ON(stripe_index >= map->num_stripes);
2532 for (i = 0; i < num_stripes; i++) {
2534 struct btrfs_device *device;
2535 struct backing_dev_info *bdi;
2537 device = map->stripes[stripe_index].dev;
2539 bdi = blk_get_backing_dev_info(device->bdev);
2540 if (bdi->unplug_io_fn)
2541 bdi->unplug_io_fn(bdi, unplug_page);
2544 multi->stripes[i].physical =
2545 map->stripes[stripe_index].physical +
2546 stripe_offset + stripe_nr * map->stripe_len;
2547 multi->stripes[i].dev = map->stripes[stripe_index].dev;
2553 multi->num_stripes = num_stripes;
2554 multi->max_errors = max_errors;
2557 free_extent_map(em);
2561 int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2562 u64 logical, u64 *length,
2563 struct btrfs_multi_bio **multi_ret, int mirror_num)
2565 return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
2569 int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
2570 u64 chunk_start, u64 physical, u64 devid,
2571 u64 **logical, int *naddrs, int *stripe_len)
2573 struct extent_map_tree *em_tree = &map_tree->map_tree;
2574 struct extent_map *em;
2575 struct map_lookup *map;
2582 spin_lock(&em_tree->lock);
2583 em = lookup_extent_mapping(em_tree, chunk_start, 1);
2584 spin_unlock(&em_tree->lock);
2586 BUG_ON(!em || em->start != chunk_start);
2587 map = (struct map_lookup *)em->bdev;
2590 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2591 do_div(length, map->num_stripes / map->sub_stripes);
2592 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
2593 do_div(length, map->num_stripes);
2595 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
2598 for (i = 0; i < map->num_stripes; i++) {
2599 if (devid && map->stripes[i].dev->devid != devid)
2601 if (map->stripes[i].physical > physical ||
2602 map->stripes[i].physical + length <= physical)
2605 stripe_nr = physical - map->stripes[i].physical;
2606 do_div(stripe_nr, map->stripe_len);
2608 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2609 stripe_nr = stripe_nr * map->num_stripes + i;
2610 do_div(stripe_nr, map->sub_stripes);
2611 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
2612 stripe_nr = stripe_nr * map->num_stripes + i;
2614 bytenr = chunk_start + stripe_nr * map->stripe_len;
2615 WARN_ON(nr >= map->num_stripes);
2616 for (j = 0; j < nr; j++) {
2617 if (buf[j] == bytenr)
2621 WARN_ON(nr >= map->num_stripes);
2626 for (i = 0; i > nr; i++) {
2627 struct btrfs_multi_bio *multi;
2628 struct btrfs_bio_stripe *stripe;
2632 ret = btrfs_map_block(map_tree, WRITE, buf[i],
2633 &length, &multi, 0);
2636 stripe = multi->stripes;
2637 for (j = 0; j < multi->num_stripes; j++) {
2638 if (stripe->physical >= physical &&
2639 physical < stripe->physical + length)
2642 BUG_ON(j >= multi->num_stripes);
2648 *stripe_len = map->stripe_len;
2650 free_extent_map(em);
2654 int btrfs_unplug_page(struct btrfs_mapping_tree *map_tree,
2655 u64 logical, struct page *page)
2657 u64 length = PAGE_CACHE_SIZE;
2658 return __btrfs_map_block(map_tree, READ, logical, &length,
2662 static void end_bio_multi_stripe(struct bio *bio, int err)
2664 struct btrfs_multi_bio *multi = bio->bi_private;
2665 int is_orig_bio = 0;
2668 atomic_inc(&multi->error);
2670 if (bio == multi->orig_bio)
2673 if (atomic_dec_and_test(&multi->stripes_pending)) {
2676 bio = multi->orig_bio;
2678 bio->bi_private = multi->private;
2679 bio->bi_end_io = multi->end_io;
2680 /* only send an error to the higher layers if it is
2681 * beyond the tolerance of the multi-bio
2683 if (atomic_read(&multi->error) > multi->max_errors) {
2687 * this bio is actually up to date, we didn't
2688 * go over the max number of errors
2690 set_bit(BIO_UPTODATE, &bio->bi_flags);
2695 bio_endio(bio, err);
2696 } else if (!is_orig_bio) {
2701 struct async_sched {
2704 struct btrfs_fs_info *info;
2705 struct btrfs_work work;
2709 * see run_scheduled_bios for a description of why bios are collected for
2712 * This will add one bio to the pending list for a device and make sure
2713 * the work struct is scheduled.
2715 static noinline int schedule_bio(struct btrfs_root *root,
2716 struct btrfs_device *device,
2717 int rw, struct bio *bio)
2719 int should_queue = 1;
2721 /* don't bother with additional async steps for reads, right now */
2722 if (!(rw & (1 << BIO_RW))) {
2724 submit_bio(rw, bio);
2730 * nr_async_bios allows us to reliably return congestion to the
2731 * higher layers. Otherwise, the async bio makes it appear we have
2732 * made progress against dirty pages when we've really just put it
2733 * on a queue for later
2735 atomic_inc(&root->fs_info->nr_async_bios);
2736 WARN_ON(bio->bi_next);
2737 bio->bi_next = NULL;
2740 spin_lock(&device->io_lock);
2742 if (device->pending_bio_tail)
2743 device->pending_bio_tail->bi_next = bio;
2745 device->pending_bio_tail = bio;
2746 if (!device->pending_bios)
2747 device->pending_bios = bio;
2748 if (device->running_pending)
2751 spin_unlock(&device->io_lock);
2754 btrfs_queue_worker(&root->fs_info->submit_workers,
2759 int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
2760 int mirror_num, int async_submit)
2762 struct btrfs_mapping_tree *map_tree;
2763 struct btrfs_device *dev;
2764 struct bio *first_bio = bio;
2765 u64 logical = (u64)bio->bi_sector << 9;
2768 struct btrfs_multi_bio *multi = NULL;
2773 length = bio->bi_size;
2774 map_tree = &root->fs_info->mapping_tree;
2775 map_length = length;
2777 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
2781 total_devs = multi->num_stripes;
2782 if (map_length < length) {
2783 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
2784 "len %llu\n", (unsigned long long)logical,
2785 (unsigned long long)length,
2786 (unsigned long long)map_length);
2789 multi->end_io = first_bio->bi_end_io;
2790 multi->private = first_bio->bi_private;
2791 multi->orig_bio = first_bio;
2792 atomic_set(&multi->stripes_pending, multi->num_stripes);
2794 while (dev_nr < total_devs) {
2795 if (total_devs > 1) {
2796 if (dev_nr < total_devs - 1) {
2797 bio = bio_clone(first_bio, GFP_NOFS);
2802 bio->bi_private = multi;
2803 bio->bi_end_io = end_bio_multi_stripe;
2805 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
2806 dev = multi->stripes[dev_nr].dev;
2807 BUG_ON(rw == WRITE && !dev->writeable);
2808 if (dev && dev->bdev) {
2809 bio->bi_bdev = dev->bdev;
2811 schedule_bio(root, dev, rw, bio);
2813 submit_bio(rw, bio);
2815 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
2816 bio->bi_sector = logical >> 9;
2817 bio_endio(bio, -EIO);
2821 if (total_devs == 1)
2826 struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
2829 struct btrfs_device *device;
2830 struct btrfs_fs_devices *cur_devices;
2832 cur_devices = root->fs_info->fs_devices;
2833 while (cur_devices) {
2835 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
2836 device = __find_device(&cur_devices->devices,
2841 cur_devices = cur_devices->seed;
2846 static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
2847 u64 devid, u8 *dev_uuid)
2849 struct btrfs_device *device;
2850 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2852 device = kzalloc(sizeof(*device), GFP_NOFS);
2855 list_add(&device->dev_list,
2856 &fs_devices->devices);
2857 device->barriers = 1;
2858 device->dev_root = root->fs_info->dev_root;
2859 device->devid = devid;
2860 device->work.func = pending_bios_fn;
2861 device->fs_devices = fs_devices;
2862 fs_devices->num_devices++;
2863 spin_lock_init(&device->io_lock);
2864 INIT_LIST_HEAD(&device->dev_alloc_list);
2865 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
2869 static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
2870 struct extent_buffer *leaf,
2871 struct btrfs_chunk *chunk)
2873 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2874 struct map_lookup *map;
2875 struct extent_map *em;
2879 u8 uuid[BTRFS_UUID_SIZE];
2884 logical = key->offset;
2885 length = btrfs_chunk_length(leaf, chunk);
2887 spin_lock(&map_tree->map_tree.lock);
2888 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
2889 spin_unlock(&map_tree->map_tree.lock);
2891 /* already mapped? */
2892 if (em && em->start <= logical && em->start + em->len > logical) {
2893 free_extent_map(em);
2896 free_extent_map(em);
2899 em = alloc_extent_map(GFP_NOFS);
2902 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2903 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
2905 free_extent_map(em);
2909 em->bdev = (struct block_device *)map;
2910 em->start = logical;
2912 em->block_start = 0;
2913 em->block_len = em->len;
2915 map->num_stripes = num_stripes;
2916 map->io_width = btrfs_chunk_io_width(leaf, chunk);
2917 map->io_align = btrfs_chunk_io_align(leaf, chunk);
2918 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
2919 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
2920 map->type = btrfs_chunk_type(leaf, chunk);
2921 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
2922 for (i = 0; i < num_stripes; i++) {
2923 map->stripes[i].physical =
2924 btrfs_stripe_offset_nr(leaf, chunk, i);
2925 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
2926 read_extent_buffer(leaf, uuid, (unsigned long)
2927 btrfs_stripe_dev_uuid_nr(chunk, i),
2929 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
2931 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
2933 free_extent_map(em);
2936 if (!map->stripes[i].dev) {
2937 map->stripes[i].dev =
2938 add_missing_dev(root, devid, uuid);
2939 if (!map->stripes[i].dev) {
2941 free_extent_map(em);
2945 map->stripes[i].dev->in_fs_metadata = 1;
2948 spin_lock(&map_tree->map_tree.lock);
2949 ret = add_extent_mapping(&map_tree->map_tree, em);
2950 spin_unlock(&map_tree->map_tree.lock);
2952 free_extent_map(em);
2957 static int fill_device_from_item(struct extent_buffer *leaf,
2958 struct btrfs_dev_item *dev_item,
2959 struct btrfs_device *device)
2963 device->devid = btrfs_device_id(leaf, dev_item);
2964 device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
2965 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
2966 device->type = btrfs_device_type(leaf, dev_item);
2967 device->io_align = btrfs_device_io_align(leaf, dev_item);
2968 device->io_width = btrfs_device_io_width(leaf, dev_item);
2969 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
2971 ptr = (unsigned long)btrfs_device_uuid(dev_item);
2972 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
2977 static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
2979 struct btrfs_fs_devices *fs_devices;
2982 mutex_lock(&uuid_mutex);
2984 fs_devices = root->fs_info->fs_devices->seed;
2985 while (fs_devices) {
2986 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
2990 fs_devices = fs_devices->seed;
2993 fs_devices = find_fsid(fsid);
2999 fs_devices = clone_fs_devices(fs_devices);
3000 if (IS_ERR(fs_devices)) {
3001 ret = PTR_ERR(fs_devices);
3005 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
3006 root->fs_info->bdev_holder);
3010 if (!fs_devices->seeding) {
3011 __btrfs_close_devices(fs_devices);
3012 free_fs_devices(fs_devices);
3017 fs_devices->seed = root->fs_info->fs_devices->seed;
3018 root->fs_info->fs_devices->seed = fs_devices;
3020 mutex_unlock(&uuid_mutex);
3024 static int read_one_dev(struct btrfs_root *root,
3025 struct extent_buffer *leaf,
3026 struct btrfs_dev_item *dev_item)
3028 struct btrfs_device *device;
3031 u8 fs_uuid[BTRFS_UUID_SIZE];
3032 u8 dev_uuid[BTRFS_UUID_SIZE];
3034 devid = btrfs_device_id(leaf, dev_item);
3035 read_extent_buffer(leaf, dev_uuid,
3036 (unsigned long)btrfs_device_uuid(dev_item),
3038 read_extent_buffer(leaf, fs_uuid,
3039 (unsigned long)btrfs_device_fsid(dev_item),
3042 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
3043 ret = open_seed_devices(root, fs_uuid);
3044 if (ret && !btrfs_test_opt(root, DEGRADED))
3048 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
3049 if (!device || !device->bdev) {
3050 if (!btrfs_test_opt(root, DEGRADED))
3054 printk(KERN_WARNING "warning devid %llu missing\n",
3055 (unsigned long long)devid);
3056 device = add_missing_dev(root, devid, dev_uuid);
3062 if (device->fs_devices != root->fs_info->fs_devices) {
3063 BUG_ON(device->writeable);
3064 if (device->generation !=
3065 btrfs_device_generation(leaf, dev_item))
3069 fill_device_from_item(leaf, dev_item, device);
3070 device->dev_root = root->fs_info->dev_root;
3071 device->in_fs_metadata = 1;
3072 if (device->writeable)
3073 device->fs_devices->total_rw_bytes += device->total_bytes;
3078 int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
3080 struct btrfs_dev_item *dev_item;
3082 dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
3084 return read_one_dev(root, buf, dev_item);
3087 int btrfs_read_sys_array(struct btrfs_root *root)
3089 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
3090 struct extent_buffer *sb;
3091 struct btrfs_disk_key *disk_key;
3092 struct btrfs_chunk *chunk;
3094 unsigned long sb_ptr;
3100 struct btrfs_key key;
3102 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
3103 BTRFS_SUPER_INFO_SIZE);
3106 btrfs_set_buffer_uptodate(sb);
3107 btrfs_set_buffer_lockdep_class(sb, 0);
3109 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
3110 array_size = btrfs_super_sys_array_size(super_copy);
3112 ptr = super_copy->sys_chunk_array;
3113 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
3116 while (cur < array_size) {
3117 disk_key = (struct btrfs_disk_key *)ptr;
3118 btrfs_disk_key_to_cpu(&key, disk_key);
3120 len = sizeof(*disk_key); ptr += len;
3124 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
3125 chunk = (struct btrfs_chunk *)sb_ptr;
3126 ret = read_one_chunk(root, &key, sb, chunk);
3129 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
3130 len = btrfs_chunk_item_size(num_stripes);
3139 free_extent_buffer(sb);
3143 int btrfs_read_chunk_tree(struct btrfs_root *root)
3145 struct btrfs_path *path;
3146 struct extent_buffer *leaf;
3147 struct btrfs_key key;
3148 struct btrfs_key found_key;
3152 root = root->fs_info->chunk_root;
3154 path = btrfs_alloc_path();
3158 /* first we search for all of the device items, and then we
3159 * read in all of the chunk items. This way we can create chunk
3160 * mappings that reference all of the devices that are afound
3162 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
3166 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3168 leaf = path->nodes[0];
3169 slot = path->slots[0];
3170 if (slot >= btrfs_header_nritems(leaf)) {
3171 ret = btrfs_next_leaf(root, path);
3178 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3179 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3180 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
3182 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
3183 struct btrfs_dev_item *dev_item;
3184 dev_item = btrfs_item_ptr(leaf, slot,
3185 struct btrfs_dev_item);
3186 ret = read_one_dev(root, leaf, dev_item);
3190 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
3191 struct btrfs_chunk *chunk;
3192 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3193 ret = read_one_chunk(root, &found_key, leaf, chunk);
3199 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3201 btrfs_release_path(root, path);
3206 btrfs_free_path(path);