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/slab.h>
21 #include <linux/buffer_head.h>
22 #include <linux/blkdev.h>
23 #include <linux/random.h>
24 #include <linux/iocontext.h>
25 #include <linux/capability.h>
26 #include <asm/div64.h>
29 #include "extent_map.h"
31 #include "transaction.h"
32 #include "print-tree.h"
34 #include "async-thread.h"
36 static int init_first_rw_device(struct btrfs_trans_handle *trans,
37 struct btrfs_root *root,
38 struct btrfs_device *device);
39 static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
41 #define map_lookup_size(n) (sizeof(struct map_lookup) + \
42 (sizeof(struct btrfs_bio_stripe) * (n)))
44 static DEFINE_MUTEX(uuid_mutex);
45 static LIST_HEAD(fs_uuids);
47 void btrfs_lock_volumes(void)
49 mutex_lock(&uuid_mutex);
52 void btrfs_unlock_volumes(void)
54 mutex_unlock(&uuid_mutex);
57 static void lock_chunks(struct btrfs_root *root)
59 mutex_lock(&root->fs_info->chunk_mutex);
62 static void unlock_chunks(struct btrfs_root *root)
64 mutex_unlock(&root->fs_info->chunk_mutex);
67 static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
69 struct btrfs_device *device;
70 WARN_ON(fs_devices->opened);
71 while (!list_empty(&fs_devices->devices)) {
72 device = list_entry(fs_devices->devices.next,
73 struct btrfs_device, dev_list);
74 list_del(&device->dev_list);
81 int btrfs_cleanup_fs_uuids(void)
83 struct btrfs_fs_devices *fs_devices;
85 while (!list_empty(&fs_uuids)) {
86 fs_devices = list_entry(fs_uuids.next,
87 struct btrfs_fs_devices, list);
88 list_del(&fs_devices->list);
89 free_fs_devices(fs_devices);
94 static noinline struct btrfs_device *__find_device(struct list_head *head,
97 struct btrfs_device *dev;
99 list_for_each_entry(dev, head, dev_list) {
100 if (dev->devid == devid &&
101 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
108 static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
110 struct btrfs_fs_devices *fs_devices;
112 list_for_each_entry(fs_devices, &fs_uuids, list) {
113 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
119 static void requeue_list(struct btrfs_pending_bios *pending_bios,
120 struct bio *head, struct bio *tail)
123 struct bio *old_head;
125 old_head = pending_bios->head;
126 pending_bios->head = head;
127 if (pending_bios->tail)
128 tail->bi_next = old_head;
130 pending_bios->tail = tail;
134 * we try to collect pending bios for a device so we don't get a large
135 * number of procs sending bios down to the same device. This greatly
136 * improves the schedulers ability to collect and merge the bios.
138 * But, it also turns into a long list of bios to process and that is sure
139 * to eventually make the worker thread block. The solution here is to
140 * make some progress and then put this work struct back at the end of
141 * the list if the block device is congested. This way, multiple devices
142 * can make progress from a single worker thread.
144 static noinline int run_scheduled_bios(struct btrfs_device *device)
147 struct backing_dev_info *bdi;
148 struct btrfs_fs_info *fs_info;
149 struct btrfs_pending_bios *pending_bios;
153 unsigned long num_run;
154 unsigned long batch_run = 0;
156 unsigned long last_waited = 0;
158 struct blk_plug plug;
161 * this function runs all the bios we've collected for
162 * a particular device. We don't want to wander off to
163 * another device without first sending all of these down.
164 * So, setup a plug here and finish it off before we return
166 blk_start_plug(&plug);
168 bdi = blk_get_backing_dev_info(device->bdev);
169 fs_info = device->dev_root->fs_info;
170 limit = btrfs_async_submit_limit(fs_info);
171 limit = limit * 2 / 3;
174 spin_lock(&device->io_lock);
179 /* take all the bios off the list at once and process them
180 * later on (without the lock held). But, remember the
181 * tail and other pointers so the bios can be properly reinserted
182 * into the list if we hit congestion
184 if (!force_reg && device->pending_sync_bios.head) {
185 pending_bios = &device->pending_sync_bios;
188 pending_bios = &device->pending_bios;
192 pending = pending_bios->head;
193 tail = pending_bios->tail;
194 WARN_ON(pending && !tail);
197 * if pending was null this time around, no bios need processing
198 * at all and we can stop. Otherwise it'll loop back up again
199 * and do an additional check so no bios are missed.
201 * device->running_pending is used to synchronize with the
204 if (device->pending_sync_bios.head == NULL &&
205 device->pending_bios.head == NULL) {
207 device->running_pending = 0;
210 device->running_pending = 1;
213 pending_bios->head = NULL;
214 pending_bios->tail = NULL;
216 spin_unlock(&device->io_lock);
221 /* we want to work on both lists, but do more bios on the
222 * sync list than the regular list
225 pending_bios != &device->pending_sync_bios &&
226 device->pending_sync_bios.head) ||
227 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
228 device->pending_bios.head)) {
229 spin_lock(&device->io_lock);
230 requeue_list(pending_bios, pending, tail);
235 pending = pending->bi_next;
237 atomic_dec(&fs_info->nr_async_bios);
239 if (atomic_read(&fs_info->nr_async_bios) < limit &&
240 waitqueue_active(&fs_info->async_submit_wait))
241 wake_up(&fs_info->async_submit_wait);
243 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
245 submit_bio(cur->bi_rw, cur);
252 * we made progress, there is more work to do and the bdi
253 * is now congested. Back off and let other work structs
256 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
257 fs_info->fs_devices->open_devices > 1) {
258 struct io_context *ioc;
260 ioc = current->io_context;
263 * the main goal here is that we don't want to
264 * block if we're going to be able to submit
265 * more requests without blocking.
267 * This code does two great things, it pokes into
268 * the elevator code from a filesystem _and_
269 * it makes assumptions about how batching works.
271 if (ioc && ioc->nr_batch_requests > 0 &&
272 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
274 ioc->last_waited == last_waited)) {
276 * we want to go through our batch of
277 * requests and stop. So, we copy out
278 * the ioc->last_waited time and test
279 * against it before looping
281 last_waited = ioc->last_waited;
286 spin_lock(&device->io_lock);
287 requeue_list(pending_bios, pending, tail);
288 device->running_pending = 1;
290 spin_unlock(&device->io_lock);
291 btrfs_requeue_work(&device->work);
300 spin_lock(&device->io_lock);
301 if (device->pending_bios.head || device->pending_sync_bios.head)
303 spin_unlock(&device->io_lock);
306 blk_finish_plug(&plug);
310 static void pending_bios_fn(struct btrfs_work *work)
312 struct btrfs_device *device;
314 device = container_of(work, struct btrfs_device, work);
315 run_scheduled_bios(device);
318 static noinline int device_list_add(const char *path,
319 struct btrfs_super_block *disk_super,
320 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
322 struct btrfs_device *device;
323 struct btrfs_fs_devices *fs_devices;
324 u64 found_transid = btrfs_super_generation(disk_super);
327 fs_devices = find_fsid(disk_super->fsid);
329 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
332 INIT_LIST_HEAD(&fs_devices->devices);
333 INIT_LIST_HEAD(&fs_devices->alloc_list);
334 list_add(&fs_devices->list, &fs_uuids);
335 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
336 fs_devices->latest_devid = devid;
337 fs_devices->latest_trans = found_transid;
338 mutex_init(&fs_devices->device_list_mutex);
341 device = __find_device(&fs_devices->devices, devid,
342 disk_super->dev_item.uuid);
345 if (fs_devices->opened)
348 device = kzalloc(sizeof(*device), GFP_NOFS);
350 /* we can safely leave the fs_devices entry around */
353 device->devid = devid;
354 device->work.func = pending_bios_fn;
355 memcpy(device->uuid, disk_super->dev_item.uuid,
357 spin_lock_init(&device->io_lock);
358 device->name = kstrdup(path, GFP_NOFS);
363 INIT_LIST_HEAD(&device->dev_alloc_list);
365 mutex_lock(&fs_devices->device_list_mutex);
366 list_add(&device->dev_list, &fs_devices->devices);
367 mutex_unlock(&fs_devices->device_list_mutex);
369 device->fs_devices = fs_devices;
370 fs_devices->num_devices++;
371 } else if (!device->name || strcmp(device->name, path)) {
372 name = kstrdup(path, GFP_NOFS);
377 if (device->missing) {
378 fs_devices->missing_devices--;
383 if (found_transid > fs_devices->latest_trans) {
384 fs_devices->latest_devid = devid;
385 fs_devices->latest_trans = found_transid;
387 *fs_devices_ret = fs_devices;
391 static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
393 struct btrfs_fs_devices *fs_devices;
394 struct btrfs_device *device;
395 struct btrfs_device *orig_dev;
397 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
399 return ERR_PTR(-ENOMEM);
401 INIT_LIST_HEAD(&fs_devices->devices);
402 INIT_LIST_HEAD(&fs_devices->alloc_list);
403 INIT_LIST_HEAD(&fs_devices->list);
404 mutex_init(&fs_devices->device_list_mutex);
405 fs_devices->latest_devid = orig->latest_devid;
406 fs_devices->latest_trans = orig->latest_trans;
407 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
409 mutex_lock(&orig->device_list_mutex);
410 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
411 device = kzalloc(sizeof(*device), GFP_NOFS);
415 device->name = kstrdup(orig_dev->name, GFP_NOFS);
421 device->devid = orig_dev->devid;
422 device->work.func = pending_bios_fn;
423 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
424 spin_lock_init(&device->io_lock);
425 INIT_LIST_HEAD(&device->dev_list);
426 INIT_LIST_HEAD(&device->dev_alloc_list);
428 list_add(&device->dev_list, &fs_devices->devices);
429 device->fs_devices = fs_devices;
430 fs_devices->num_devices++;
432 mutex_unlock(&orig->device_list_mutex);
435 mutex_unlock(&orig->device_list_mutex);
436 free_fs_devices(fs_devices);
437 return ERR_PTR(-ENOMEM);
440 int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
442 struct btrfs_device *device, *next;
444 mutex_lock(&uuid_mutex);
446 mutex_lock(&fs_devices->device_list_mutex);
447 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
448 if (device->in_fs_metadata)
452 blkdev_put(device->bdev, device->mode);
454 fs_devices->open_devices--;
456 if (device->writeable) {
457 list_del_init(&device->dev_alloc_list);
458 device->writeable = 0;
459 fs_devices->rw_devices--;
461 list_del_init(&device->dev_list);
462 fs_devices->num_devices--;
466 mutex_unlock(&fs_devices->device_list_mutex);
468 if (fs_devices->seed) {
469 fs_devices = fs_devices->seed;
473 mutex_unlock(&uuid_mutex);
477 static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
479 struct btrfs_device *device;
481 if (--fs_devices->opened > 0)
484 mutex_lock(&fs_devices->device_list_mutex);
485 list_for_each_entry(device, &fs_devices->devices, dev_list) {
487 blkdev_put(device->bdev, device->mode);
488 fs_devices->open_devices--;
490 if (device->writeable) {
491 list_del_init(&device->dev_alloc_list);
492 fs_devices->rw_devices--;
496 device->writeable = 0;
497 device->in_fs_metadata = 0;
499 mutex_unlock(&fs_devices->device_list_mutex);
501 WARN_ON(fs_devices->open_devices);
502 WARN_ON(fs_devices->rw_devices);
503 fs_devices->opened = 0;
504 fs_devices->seeding = 0;
509 int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
511 struct btrfs_fs_devices *seed_devices = NULL;
514 mutex_lock(&uuid_mutex);
515 ret = __btrfs_close_devices(fs_devices);
516 if (!fs_devices->opened) {
517 seed_devices = fs_devices->seed;
518 fs_devices->seed = NULL;
520 mutex_unlock(&uuid_mutex);
522 while (seed_devices) {
523 fs_devices = seed_devices;
524 seed_devices = fs_devices->seed;
525 __btrfs_close_devices(fs_devices);
526 free_fs_devices(fs_devices);
531 static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
532 fmode_t flags, void *holder)
534 struct block_device *bdev;
535 struct list_head *head = &fs_devices->devices;
536 struct btrfs_device *device;
537 struct block_device *latest_bdev = NULL;
538 struct buffer_head *bh;
539 struct btrfs_super_block *disk_super;
540 u64 latest_devid = 0;
541 u64 latest_transid = 0;
548 list_for_each_entry(device, head, dev_list) {
554 bdev = blkdev_get_by_path(device->name, flags, holder);
556 printk(KERN_INFO "open %s failed\n", device->name);
559 set_blocksize(bdev, 4096);
561 bh = btrfs_read_dev_super(bdev);
567 disk_super = (struct btrfs_super_block *)bh->b_data;
568 devid = btrfs_stack_device_id(&disk_super->dev_item);
569 if (devid != device->devid)
572 if (memcmp(device->uuid, disk_super->dev_item.uuid,
576 device->generation = btrfs_super_generation(disk_super);
577 if (!latest_transid || device->generation > latest_transid) {
578 latest_devid = devid;
579 latest_transid = device->generation;
583 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
584 device->writeable = 0;
586 device->writeable = !bdev_read_only(bdev);
591 device->in_fs_metadata = 0;
592 device->mode = flags;
594 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
595 fs_devices->rotating = 1;
597 fs_devices->open_devices++;
598 if (device->writeable) {
599 fs_devices->rw_devices++;
600 list_add(&device->dev_alloc_list,
601 &fs_devices->alloc_list);
609 blkdev_put(bdev, flags);
613 if (fs_devices->open_devices == 0) {
617 fs_devices->seeding = seeding;
618 fs_devices->opened = 1;
619 fs_devices->latest_bdev = latest_bdev;
620 fs_devices->latest_devid = latest_devid;
621 fs_devices->latest_trans = latest_transid;
622 fs_devices->total_rw_bytes = 0;
627 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
628 fmode_t flags, void *holder)
632 mutex_lock(&uuid_mutex);
633 if (fs_devices->opened) {
634 fs_devices->opened++;
637 ret = __btrfs_open_devices(fs_devices, flags, holder);
639 mutex_unlock(&uuid_mutex);
643 int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
644 struct btrfs_fs_devices **fs_devices_ret)
646 struct btrfs_super_block *disk_super;
647 struct block_device *bdev;
648 struct buffer_head *bh;
653 mutex_lock(&uuid_mutex);
656 bdev = blkdev_get_by_path(path, flags, holder);
663 ret = set_blocksize(bdev, 4096);
666 bh = btrfs_read_dev_super(bdev);
671 disk_super = (struct btrfs_super_block *)bh->b_data;
672 devid = btrfs_stack_device_id(&disk_super->dev_item);
673 transid = btrfs_super_generation(disk_super);
674 if (disk_super->label[0])
675 printk(KERN_INFO "device label %s ", disk_super->label);
677 /* FIXME, make a readl uuid parser */
678 printk(KERN_INFO "device fsid %llx-%llx ",
679 *(unsigned long long *)disk_super->fsid,
680 *(unsigned long long *)(disk_super->fsid + 8));
682 printk(KERN_CONT "devid %llu transid %llu %s\n",
683 (unsigned long long)devid, (unsigned long long)transid, path);
684 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
688 blkdev_put(bdev, flags);
690 mutex_unlock(&uuid_mutex);
694 /* helper to account the used device space in the range */
695 int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
696 u64 end, u64 *length)
698 struct btrfs_key key;
699 struct btrfs_root *root = device->dev_root;
700 struct btrfs_dev_extent *dev_extent;
701 struct btrfs_path *path;
705 struct extent_buffer *l;
709 if (start >= device->total_bytes)
712 path = btrfs_alloc_path();
717 key.objectid = device->devid;
719 key.type = BTRFS_DEV_EXTENT_KEY;
721 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
725 ret = btrfs_previous_item(root, path, key.objectid, key.type);
732 slot = path->slots[0];
733 if (slot >= btrfs_header_nritems(l)) {
734 ret = btrfs_next_leaf(root, path);
742 btrfs_item_key_to_cpu(l, &key, slot);
744 if (key.objectid < device->devid)
747 if (key.objectid > device->devid)
750 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
753 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
754 extent_end = key.offset + btrfs_dev_extent_length(l,
756 if (key.offset <= start && extent_end > end) {
757 *length = end - start + 1;
759 } else if (key.offset <= start && extent_end > start)
760 *length += extent_end - start;
761 else if (key.offset > start && extent_end <= end)
762 *length += extent_end - key.offset;
763 else if (key.offset > start && key.offset <= end) {
764 *length += end - key.offset + 1;
766 } else if (key.offset > end)
774 btrfs_free_path(path);
779 * find_free_dev_extent - find free space in the specified device
780 * @trans: transaction handler
781 * @device: the device which we search the free space in
782 * @num_bytes: the size of the free space that we need
783 * @start: store the start of the free space.
784 * @len: the size of the free space. that we find, or the size of the max
785 * free space if we don't find suitable free space
787 * this uses a pretty simple search, the expectation is that it is
788 * called very infrequently and that a given device has a small number
791 * @start is used to store the start of the free space if we find. But if we
792 * don't find suitable free space, it will be used to store the start position
793 * of the max free space.
795 * @len is used to store the size of the free space that we find.
796 * But if we don't find suitable free space, it is used to store the size of
797 * the max free space.
799 int find_free_dev_extent(struct btrfs_trans_handle *trans,
800 struct btrfs_device *device, u64 num_bytes,
801 u64 *start, u64 *len)
803 struct btrfs_key key;
804 struct btrfs_root *root = device->dev_root;
805 struct btrfs_dev_extent *dev_extent;
806 struct btrfs_path *path;
812 u64 search_end = device->total_bytes;
815 struct extent_buffer *l;
817 /* FIXME use last free of some kind */
819 /* we don't want to overwrite the superblock on the drive,
820 * so we make sure to start at an offset of at least 1MB
822 search_start = 1024 * 1024;
824 if (root->fs_info->alloc_start + num_bytes <= search_end)
825 search_start = max(root->fs_info->alloc_start, search_start);
827 max_hole_start = search_start;
830 if (search_start >= search_end) {
835 path = btrfs_alloc_path();
842 key.objectid = device->devid;
843 key.offset = search_start;
844 key.type = BTRFS_DEV_EXTENT_KEY;
846 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
850 ret = btrfs_previous_item(root, path, key.objectid, key.type);
857 slot = path->slots[0];
858 if (slot >= btrfs_header_nritems(l)) {
859 ret = btrfs_next_leaf(root, path);
867 btrfs_item_key_to_cpu(l, &key, slot);
869 if (key.objectid < device->devid)
872 if (key.objectid > device->devid)
875 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
878 if (key.offset > search_start) {
879 hole_size = key.offset - search_start;
881 if (hole_size > max_hole_size) {
882 max_hole_start = search_start;
883 max_hole_size = hole_size;
887 * If this free space is greater than which we need,
888 * it must be the max free space that we have found
889 * until now, so max_hole_start must point to the start
890 * of this free space and the length of this free space
891 * is stored in max_hole_size. Thus, we return
892 * max_hole_start and max_hole_size and go back to the
895 if (hole_size >= num_bytes) {
901 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
902 extent_end = key.offset + btrfs_dev_extent_length(l,
904 if (extent_end > search_start)
905 search_start = extent_end;
911 hole_size = search_end- search_start;
912 if (hole_size > max_hole_size) {
913 max_hole_start = search_start;
914 max_hole_size = hole_size;
918 if (hole_size < num_bytes)
924 btrfs_free_path(path);
926 *start = max_hole_start;
928 *len = max_hole_size;
932 static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
933 struct btrfs_device *device,
937 struct btrfs_path *path;
938 struct btrfs_root *root = device->dev_root;
939 struct btrfs_key key;
940 struct btrfs_key found_key;
941 struct extent_buffer *leaf = NULL;
942 struct btrfs_dev_extent *extent = NULL;
944 path = btrfs_alloc_path();
948 key.objectid = device->devid;
950 key.type = BTRFS_DEV_EXTENT_KEY;
952 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
954 ret = btrfs_previous_item(root, path, key.objectid,
955 BTRFS_DEV_EXTENT_KEY);
958 leaf = path->nodes[0];
959 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
960 extent = btrfs_item_ptr(leaf, path->slots[0],
961 struct btrfs_dev_extent);
962 BUG_ON(found_key.offset > start || found_key.offset +
963 btrfs_dev_extent_length(leaf, extent) < start);
964 } else if (ret == 0) {
965 leaf = path->nodes[0];
966 extent = btrfs_item_ptr(leaf, path->slots[0],
967 struct btrfs_dev_extent);
971 if (device->bytes_used > 0)
972 device->bytes_used -= btrfs_dev_extent_length(leaf, extent);
973 ret = btrfs_del_item(trans, root, path);
976 btrfs_free_path(path);
980 int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
981 struct btrfs_device *device,
982 u64 chunk_tree, u64 chunk_objectid,
983 u64 chunk_offset, u64 start, u64 num_bytes)
986 struct btrfs_path *path;
987 struct btrfs_root *root = device->dev_root;
988 struct btrfs_dev_extent *extent;
989 struct extent_buffer *leaf;
990 struct btrfs_key key;
992 WARN_ON(!device->in_fs_metadata);
993 path = btrfs_alloc_path();
997 key.objectid = device->devid;
999 key.type = BTRFS_DEV_EXTENT_KEY;
1000 ret = btrfs_insert_empty_item(trans, root, path, &key,
1004 leaf = path->nodes[0];
1005 extent = btrfs_item_ptr(leaf, path->slots[0],
1006 struct btrfs_dev_extent);
1007 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1008 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1009 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1011 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1012 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1015 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1016 btrfs_mark_buffer_dirty(leaf);
1017 btrfs_free_path(path);
1021 static noinline int find_next_chunk(struct btrfs_root *root,
1022 u64 objectid, u64 *offset)
1024 struct btrfs_path *path;
1026 struct btrfs_key key;
1027 struct btrfs_chunk *chunk;
1028 struct btrfs_key found_key;
1030 path = btrfs_alloc_path();
1033 key.objectid = objectid;
1034 key.offset = (u64)-1;
1035 key.type = BTRFS_CHUNK_ITEM_KEY;
1037 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1043 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1047 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1049 if (found_key.objectid != objectid)
1052 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1053 struct btrfs_chunk);
1054 *offset = found_key.offset +
1055 btrfs_chunk_length(path->nodes[0], chunk);
1060 btrfs_free_path(path);
1064 static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
1067 struct btrfs_key key;
1068 struct btrfs_key found_key;
1069 struct btrfs_path *path;
1071 root = root->fs_info->chunk_root;
1073 path = btrfs_alloc_path();
1077 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1078 key.type = BTRFS_DEV_ITEM_KEY;
1079 key.offset = (u64)-1;
1081 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1087 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1088 BTRFS_DEV_ITEM_KEY);
1092 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1094 *objectid = found_key.offset + 1;
1098 btrfs_free_path(path);
1103 * the device information is stored in the chunk root
1104 * the btrfs_device struct should be fully filled in
1106 int btrfs_add_device(struct btrfs_trans_handle *trans,
1107 struct btrfs_root *root,
1108 struct btrfs_device *device)
1111 struct btrfs_path *path;
1112 struct btrfs_dev_item *dev_item;
1113 struct extent_buffer *leaf;
1114 struct btrfs_key key;
1117 root = root->fs_info->chunk_root;
1119 path = btrfs_alloc_path();
1123 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1124 key.type = BTRFS_DEV_ITEM_KEY;
1125 key.offset = device->devid;
1127 ret = btrfs_insert_empty_item(trans, root, path, &key,
1132 leaf = path->nodes[0];
1133 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1135 btrfs_set_device_id(leaf, dev_item, device->devid);
1136 btrfs_set_device_generation(leaf, dev_item, 0);
1137 btrfs_set_device_type(leaf, dev_item, device->type);
1138 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1139 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1140 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
1141 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1142 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1143 btrfs_set_device_group(leaf, dev_item, 0);
1144 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1145 btrfs_set_device_bandwidth(leaf, dev_item, 0);
1146 btrfs_set_device_start_offset(leaf, dev_item, 0);
1148 ptr = (unsigned long)btrfs_device_uuid(dev_item);
1149 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1150 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1151 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
1152 btrfs_mark_buffer_dirty(leaf);
1156 btrfs_free_path(path);
1160 static int btrfs_rm_dev_item(struct btrfs_root *root,
1161 struct btrfs_device *device)
1164 struct btrfs_path *path;
1165 struct btrfs_key key;
1166 struct btrfs_trans_handle *trans;
1168 root = root->fs_info->chunk_root;
1170 path = btrfs_alloc_path();
1174 trans = btrfs_start_transaction(root, 0);
1175 if (IS_ERR(trans)) {
1176 btrfs_free_path(path);
1177 return PTR_ERR(trans);
1179 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1180 key.type = BTRFS_DEV_ITEM_KEY;
1181 key.offset = device->devid;
1184 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1193 ret = btrfs_del_item(trans, root, path);
1197 btrfs_free_path(path);
1198 unlock_chunks(root);
1199 btrfs_commit_transaction(trans, root);
1203 int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1205 struct btrfs_device *device;
1206 struct btrfs_device *next_device;
1207 struct block_device *bdev;
1208 struct buffer_head *bh = NULL;
1209 struct btrfs_super_block *disk_super;
1216 mutex_lock(&uuid_mutex);
1217 mutex_lock(&root->fs_info->volume_mutex);
1219 all_avail = root->fs_info->avail_data_alloc_bits |
1220 root->fs_info->avail_system_alloc_bits |
1221 root->fs_info->avail_metadata_alloc_bits;
1223 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
1224 root->fs_info->fs_devices->num_devices <= 4) {
1225 printk(KERN_ERR "btrfs: unable to go below four devices "
1231 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
1232 root->fs_info->fs_devices->num_devices <= 2) {
1233 printk(KERN_ERR "btrfs: unable to go below two "
1234 "devices on raid1\n");
1239 if (strcmp(device_path, "missing") == 0) {
1240 struct list_head *devices;
1241 struct btrfs_device *tmp;
1244 devices = &root->fs_info->fs_devices->devices;
1245 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1246 list_for_each_entry(tmp, devices, dev_list) {
1247 if (tmp->in_fs_metadata && !tmp->bdev) {
1252 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1257 printk(KERN_ERR "btrfs: no missing devices found to "
1262 bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL,
1263 root->fs_info->bdev_holder);
1265 ret = PTR_ERR(bdev);
1269 set_blocksize(bdev, 4096);
1270 bh = btrfs_read_dev_super(bdev);
1275 disk_super = (struct btrfs_super_block *)bh->b_data;
1276 devid = btrfs_stack_device_id(&disk_super->dev_item);
1277 dev_uuid = disk_super->dev_item.uuid;
1278 device = btrfs_find_device(root, devid, dev_uuid,
1286 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
1287 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1293 if (device->writeable) {
1295 list_del_init(&device->dev_alloc_list);
1296 unlock_chunks(root);
1297 root->fs_info->fs_devices->rw_devices--;
1300 ret = btrfs_shrink_device(device, 0);
1304 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1308 device->in_fs_metadata = 0;
1311 * the device list mutex makes sure that we don't change
1312 * the device list while someone else is writing out all
1313 * the device supers.
1315 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1316 list_del_init(&device->dev_list);
1317 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1319 device->fs_devices->num_devices--;
1321 if (device->missing)
1322 root->fs_info->fs_devices->missing_devices--;
1324 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1325 struct btrfs_device, dev_list);
1326 if (device->bdev == root->fs_info->sb->s_bdev)
1327 root->fs_info->sb->s_bdev = next_device->bdev;
1328 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1329 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1332 blkdev_put(device->bdev, device->mode);
1333 device->bdev = NULL;
1334 device->fs_devices->open_devices--;
1337 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
1338 btrfs_set_super_num_devices(&root->fs_info->super_copy, num_devices);
1340 if (device->fs_devices->open_devices == 0) {
1341 struct btrfs_fs_devices *fs_devices;
1342 fs_devices = root->fs_info->fs_devices;
1343 while (fs_devices) {
1344 if (fs_devices->seed == device->fs_devices)
1346 fs_devices = fs_devices->seed;
1348 fs_devices->seed = device->fs_devices->seed;
1349 device->fs_devices->seed = NULL;
1351 __btrfs_close_devices(device->fs_devices);
1352 unlock_chunks(root);
1353 free_fs_devices(device->fs_devices);
1357 * at this point, the device is zero sized. We want to
1358 * remove it from the devices list and zero out the old super
1360 if (device->writeable) {
1361 /* make sure this device isn't detected as part of
1364 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1365 set_buffer_dirty(bh);
1366 sync_dirty_buffer(bh);
1369 kfree(device->name);
1377 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
1379 mutex_unlock(&root->fs_info->volume_mutex);
1380 mutex_unlock(&uuid_mutex);
1383 if (device->writeable) {
1385 list_add(&device->dev_alloc_list,
1386 &root->fs_info->fs_devices->alloc_list);
1387 unlock_chunks(root);
1388 root->fs_info->fs_devices->rw_devices++;
1394 * does all the dirty work required for changing file system's UUID.
1396 static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans,
1397 struct btrfs_root *root)
1399 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1400 struct btrfs_fs_devices *old_devices;
1401 struct btrfs_fs_devices *seed_devices;
1402 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
1403 struct btrfs_device *device;
1406 BUG_ON(!mutex_is_locked(&uuid_mutex));
1407 if (!fs_devices->seeding)
1410 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1414 old_devices = clone_fs_devices(fs_devices);
1415 if (IS_ERR(old_devices)) {
1416 kfree(seed_devices);
1417 return PTR_ERR(old_devices);
1420 list_add(&old_devices->list, &fs_uuids);
1422 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1423 seed_devices->opened = 1;
1424 INIT_LIST_HEAD(&seed_devices->devices);
1425 INIT_LIST_HEAD(&seed_devices->alloc_list);
1426 mutex_init(&seed_devices->device_list_mutex);
1428 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1429 list_splice_init(&fs_devices->devices, &seed_devices->devices);
1430 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1432 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1433 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1434 device->fs_devices = seed_devices;
1437 fs_devices->seeding = 0;
1438 fs_devices->num_devices = 0;
1439 fs_devices->open_devices = 0;
1440 fs_devices->seed = seed_devices;
1442 generate_random_uuid(fs_devices->fsid);
1443 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1444 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1445 super_flags = btrfs_super_flags(disk_super) &
1446 ~BTRFS_SUPER_FLAG_SEEDING;
1447 btrfs_set_super_flags(disk_super, super_flags);
1453 * strore the expected generation for seed devices in device items.
1455 static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1456 struct btrfs_root *root)
1458 struct btrfs_path *path;
1459 struct extent_buffer *leaf;
1460 struct btrfs_dev_item *dev_item;
1461 struct btrfs_device *device;
1462 struct btrfs_key key;
1463 u8 fs_uuid[BTRFS_UUID_SIZE];
1464 u8 dev_uuid[BTRFS_UUID_SIZE];
1468 path = btrfs_alloc_path();
1472 root = root->fs_info->chunk_root;
1473 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1475 key.type = BTRFS_DEV_ITEM_KEY;
1478 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1482 leaf = path->nodes[0];
1484 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1485 ret = btrfs_next_leaf(root, path);
1490 leaf = path->nodes[0];
1491 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1492 btrfs_release_path(root, path);
1496 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1497 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1498 key.type != BTRFS_DEV_ITEM_KEY)
1501 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1502 struct btrfs_dev_item);
1503 devid = btrfs_device_id(leaf, dev_item);
1504 read_extent_buffer(leaf, dev_uuid,
1505 (unsigned long)btrfs_device_uuid(dev_item),
1507 read_extent_buffer(leaf, fs_uuid,
1508 (unsigned long)btrfs_device_fsid(dev_item),
1510 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1513 if (device->fs_devices->seeding) {
1514 btrfs_set_device_generation(leaf, dev_item,
1515 device->generation);
1516 btrfs_mark_buffer_dirty(leaf);
1524 btrfs_free_path(path);
1528 int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1530 struct btrfs_trans_handle *trans;
1531 struct btrfs_device *device;
1532 struct block_device *bdev;
1533 struct list_head *devices;
1534 struct super_block *sb = root->fs_info->sb;
1536 int seeding_dev = 0;
1539 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1542 bdev = blkdev_get_by_path(device_path, FMODE_EXCL,
1543 root->fs_info->bdev_holder);
1545 return PTR_ERR(bdev);
1547 if (root->fs_info->fs_devices->seeding) {
1549 down_write(&sb->s_umount);
1550 mutex_lock(&uuid_mutex);
1553 filemap_write_and_wait(bdev->bd_inode->i_mapping);
1554 mutex_lock(&root->fs_info->volume_mutex);
1556 devices = &root->fs_info->fs_devices->devices;
1558 * we have the volume lock, so we don't need the extra
1559 * device list mutex while reading the list here.
1561 list_for_each_entry(device, devices, dev_list) {
1562 if (device->bdev == bdev) {
1568 device = kzalloc(sizeof(*device), GFP_NOFS);
1570 /* we can safely leave the fs_devices entry around */
1575 device->name = kstrdup(device_path, GFP_NOFS);
1576 if (!device->name) {
1582 ret = find_next_devid(root, &device->devid);
1584 kfree(device->name);
1589 trans = btrfs_start_transaction(root, 0);
1590 if (IS_ERR(trans)) {
1591 kfree(device->name);
1593 ret = PTR_ERR(trans);
1599 device->writeable = 1;
1600 device->work.func = pending_bios_fn;
1601 generate_random_uuid(device->uuid);
1602 spin_lock_init(&device->io_lock);
1603 device->generation = trans->transid;
1604 device->io_width = root->sectorsize;
1605 device->io_align = root->sectorsize;
1606 device->sector_size = root->sectorsize;
1607 device->total_bytes = i_size_read(bdev->bd_inode);
1608 device->disk_total_bytes = device->total_bytes;
1609 device->dev_root = root->fs_info->dev_root;
1610 device->bdev = bdev;
1611 device->in_fs_metadata = 1;
1612 device->mode = FMODE_EXCL;
1613 set_blocksize(device->bdev, 4096);
1616 sb->s_flags &= ~MS_RDONLY;
1617 ret = btrfs_prepare_sprout(trans, root);
1621 device->fs_devices = root->fs_info->fs_devices;
1624 * we don't want write_supers to jump in here with our device
1627 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1628 list_add(&device->dev_list, &root->fs_info->fs_devices->devices);
1629 list_add(&device->dev_alloc_list,
1630 &root->fs_info->fs_devices->alloc_list);
1631 root->fs_info->fs_devices->num_devices++;
1632 root->fs_info->fs_devices->open_devices++;
1633 root->fs_info->fs_devices->rw_devices++;
1634 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1636 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1637 root->fs_info->fs_devices->rotating = 1;
1639 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
1640 btrfs_set_super_total_bytes(&root->fs_info->super_copy,
1641 total_bytes + device->total_bytes);
1643 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
1644 btrfs_set_super_num_devices(&root->fs_info->super_copy,
1646 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1649 ret = init_first_rw_device(trans, root, device);
1651 ret = btrfs_finish_sprout(trans, root);
1654 ret = btrfs_add_device(trans, root, device);
1658 * we've got more storage, clear any full flags on the space
1661 btrfs_clear_space_info_full(root->fs_info);
1663 unlock_chunks(root);
1664 btrfs_commit_transaction(trans, root);
1667 mutex_unlock(&uuid_mutex);
1668 up_write(&sb->s_umount);
1670 ret = btrfs_relocate_sys_chunks(root);
1674 mutex_unlock(&root->fs_info->volume_mutex);
1677 blkdev_put(bdev, FMODE_EXCL);
1679 mutex_unlock(&uuid_mutex);
1680 up_write(&sb->s_umount);
1685 static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1686 struct btrfs_device *device)
1689 struct btrfs_path *path;
1690 struct btrfs_root *root;
1691 struct btrfs_dev_item *dev_item;
1692 struct extent_buffer *leaf;
1693 struct btrfs_key key;
1695 root = device->dev_root->fs_info->chunk_root;
1697 path = btrfs_alloc_path();
1701 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1702 key.type = BTRFS_DEV_ITEM_KEY;
1703 key.offset = device->devid;
1705 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1714 leaf = path->nodes[0];
1715 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1717 btrfs_set_device_id(leaf, dev_item, device->devid);
1718 btrfs_set_device_type(leaf, dev_item, device->type);
1719 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1720 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1721 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
1722 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
1723 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1724 btrfs_mark_buffer_dirty(leaf);
1727 btrfs_free_path(path);
1731 static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
1732 struct btrfs_device *device, u64 new_size)
1734 struct btrfs_super_block *super_copy =
1735 &device->dev_root->fs_info->super_copy;
1736 u64 old_total = btrfs_super_total_bytes(super_copy);
1737 u64 diff = new_size - device->total_bytes;
1739 if (!device->writeable)
1741 if (new_size <= device->total_bytes)
1744 btrfs_set_super_total_bytes(super_copy, old_total + diff);
1745 device->fs_devices->total_rw_bytes += diff;
1747 device->total_bytes = new_size;
1748 device->disk_total_bytes = new_size;
1749 btrfs_clear_space_info_full(device->dev_root->fs_info);
1751 return btrfs_update_device(trans, device);
1754 int btrfs_grow_device(struct btrfs_trans_handle *trans,
1755 struct btrfs_device *device, u64 new_size)
1758 lock_chunks(device->dev_root);
1759 ret = __btrfs_grow_device(trans, device, new_size);
1760 unlock_chunks(device->dev_root);
1764 static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1765 struct btrfs_root *root,
1766 u64 chunk_tree, u64 chunk_objectid,
1770 struct btrfs_path *path;
1771 struct btrfs_key key;
1773 root = root->fs_info->chunk_root;
1774 path = btrfs_alloc_path();
1778 key.objectid = chunk_objectid;
1779 key.offset = chunk_offset;
1780 key.type = BTRFS_CHUNK_ITEM_KEY;
1782 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1785 ret = btrfs_del_item(trans, root, path);
1787 btrfs_free_path(path);
1791 static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
1794 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1795 struct btrfs_disk_key *disk_key;
1796 struct btrfs_chunk *chunk;
1803 struct btrfs_key key;
1805 array_size = btrfs_super_sys_array_size(super_copy);
1807 ptr = super_copy->sys_chunk_array;
1810 while (cur < array_size) {
1811 disk_key = (struct btrfs_disk_key *)ptr;
1812 btrfs_disk_key_to_cpu(&key, disk_key);
1814 len = sizeof(*disk_key);
1816 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1817 chunk = (struct btrfs_chunk *)(ptr + len);
1818 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1819 len += btrfs_chunk_item_size(num_stripes);
1824 if (key.objectid == chunk_objectid &&
1825 key.offset == chunk_offset) {
1826 memmove(ptr, ptr + len, array_size - (cur + len));
1828 btrfs_set_super_sys_array_size(super_copy, array_size);
1837 static int btrfs_relocate_chunk(struct btrfs_root *root,
1838 u64 chunk_tree, u64 chunk_objectid,
1841 struct extent_map_tree *em_tree;
1842 struct btrfs_root *extent_root;
1843 struct btrfs_trans_handle *trans;
1844 struct extent_map *em;
1845 struct map_lookup *map;
1849 root = root->fs_info->chunk_root;
1850 extent_root = root->fs_info->extent_root;
1851 em_tree = &root->fs_info->mapping_tree.map_tree;
1853 ret = btrfs_can_relocate(extent_root, chunk_offset);
1857 /* step one, relocate all the extents inside this chunk */
1858 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
1862 trans = btrfs_start_transaction(root, 0);
1863 BUG_ON(IS_ERR(trans));
1868 * step two, delete the device extents and the
1869 * chunk tree entries
1871 read_lock(&em_tree->lock);
1872 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
1873 read_unlock(&em_tree->lock);
1875 BUG_ON(em->start > chunk_offset ||
1876 em->start + em->len < chunk_offset);
1877 map = (struct map_lookup *)em->bdev;
1879 for (i = 0; i < map->num_stripes; i++) {
1880 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1881 map->stripes[i].physical);
1884 if (map->stripes[i].dev) {
1885 ret = btrfs_update_device(trans, map->stripes[i].dev);
1889 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1894 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
1896 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1897 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1901 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
1904 write_lock(&em_tree->lock);
1905 remove_extent_mapping(em_tree, em);
1906 write_unlock(&em_tree->lock);
1911 /* once for the tree */
1912 free_extent_map(em);
1914 free_extent_map(em);
1916 unlock_chunks(root);
1917 btrfs_end_transaction(trans, root);
1921 static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
1923 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1924 struct btrfs_path *path;
1925 struct extent_buffer *leaf;
1926 struct btrfs_chunk *chunk;
1927 struct btrfs_key key;
1928 struct btrfs_key found_key;
1929 u64 chunk_tree = chunk_root->root_key.objectid;
1931 bool retried = false;
1935 path = btrfs_alloc_path();
1940 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1941 key.offset = (u64)-1;
1942 key.type = BTRFS_CHUNK_ITEM_KEY;
1945 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1950 ret = btrfs_previous_item(chunk_root, path, key.objectid,
1957 leaf = path->nodes[0];
1958 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1960 chunk = btrfs_item_ptr(leaf, path->slots[0],
1961 struct btrfs_chunk);
1962 chunk_type = btrfs_chunk_type(leaf, chunk);
1963 btrfs_release_path(chunk_root, path);
1965 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
1966 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
1975 if (found_key.offset == 0)
1977 key.offset = found_key.offset - 1;
1980 if (failed && !retried) {
1984 } else if (failed && retried) {
1989 btrfs_free_path(path);
1993 static u64 div_factor(u64 num, int factor)
2002 int btrfs_balance(struct btrfs_root *dev_root)
2005 struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
2006 struct btrfs_device *device;
2009 struct btrfs_path *path;
2010 struct btrfs_key key;
2011 struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
2012 struct btrfs_trans_handle *trans;
2013 struct btrfs_key found_key;
2015 if (dev_root->fs_info->sb->s_flags & MS_RDONLY)
2018 if (!capable(CAP_SYS_ADMIN))
2021 mutex_lock(&dev_root->fs_info->volume_mutex);
2022 dev_root = dev_root->fs_info->dev_root;
2024 /* step one make some room on all the devices */
2025 list_for_each_entry(device, devices, dev_list) {
2026 old_size = device->total_bytes;
2027 size_to_free = div_factor(old_size, 1);
2028 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
2029 if (!device->writeable ||
2030 device->total_bytes - device->bytes_used > size_to_free)
2033 ret = btrfs_shrink_device(device, old_size - size_to_free);
2038 trans = btrfs_start_transaction(dev_root, 0);
2039 BUG_ON(IS_ERR(trans));
2041 ret = btrfs_grow_device(trans, device, old_size);
2044 btrfs_end_transaction(trans, dev_root);
2047 /* step two, relocate all the chunks */
2048 path = btrfs_alloc_path();
2051 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2052 key.offset = (u64)-1;
2053 key.type = BTRFS_CHUNK_ITEM_KEY;
2056 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2061 * this shouldn't happen, it means the last relocate
2067 ret = btrfs_previous_item(chunk_root, path, 0,
2068 BTRFS_CHUNK_ITEM_KEY);
2072 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2074 if (found_key.objectid != key.objectid)
2077 /* chunk zero is special */
2078 if (found_key.offset == 0)
2081 btrfs_release_path(chunk_root, path);
2082 ret = btrfs_relocate_chunk(chunk_root,
2083 chunk_root->root_key.objectid,
2086 BUG_ON(ret && ret != -ENOSPC);
2087 key.offset = found_key.offset - 1;
2091 btrfs_free_path(path);
2092 mutex_unlock(&dev_root->fs_info->volume_mutex);
2097 * shrinking a device means finding all of the device extents past
2098 * the new size, and then following the back refs to the chunks.
2099 * The chunk relocation code actually frees the device extent
2101 int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
2103 struct btrfs_trans_handle *trans;
2104 struct btrfs_root *root = device->dev_root;
2105 struct btrfs_dev_extent *dev_extent = NULL;
2106 struct btrfs_path *path;
2114 bool retried = false;
2115 struct extent_buffer *l;
2116 struct btrfs_key key;
2117 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2118 u64 old_total = btrfs_super_total_bytes(super_copy);
2119 u64 old_size = device->total_bytes;
2120 u64 diff = device->total_bytes - new_size;
2122 if (new_size >= device->total_bytes)
2125 path = btrfs_alloc_path();
2133 device->total_bytes = new_size;
2134 if (device->writeable)
2135 device->fs_devices->total_rw_bytes -= diff;
2136 unlock_chunks(root);
2139 key.objectid = device->devid;
2140 key.offset = (u64)-1;
2141 key.type = BTRFS_DEV_EXTENT_KEY;
2144 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2148 ret = btrfs_previous_item(root, path, 0, key.type);
2153 btrfs_release_path(root, path);
2158 slot = path->slots[0];
2159 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
2161 if (key.objectid != device->devid) {
2162 btrfs_release_path(root, path);
2166 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2167 length = btrfs_dev_extent_length(l, dev_extent);
2169 if (key.offset + length <= new_size) {
2170 btrfs_release_path(root, path);
2174 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
2175 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
2176 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
2177 btrfs_release_path(root, path);
2179 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
2181 if (ret && ret != -ENOSPC)
2188 if (failed && !retried) {
2192 } else if (failed && retried) {
2196 device->total_bytes = old_size;
2197 if (device->writeable)
2198 device->fs_devices->total_rw_bytes += diff;
2199 unlock_chunks(root);
2203 /* Shrinking succeeded, else we would be at "done". */
2204 trans = btrfs_start_transaction(root, 0);
2205 if (IS_ERR(trans)) {
2206 ret = PTR_ERR(trans);
2212 device->disk_total_bytes = new_size;
2213 /* Now btrfs_update_device() will change the on-disk size. */
2214 ret = btrfs_update_device(trans, device);
2216 unlock_chunks(root);
2217 btrfs_end_transaction(trans, root);
2220 WARN_ON(diff > old_total);
2221 btrfs_set_super_total_bytes(super_copy, old_total - diff);
2222 unlock_chunks(root);
2223 btrfs_end_transaction(trans, root);
2225 btrfs_free_path(path);
2229 static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
2230 struct btrfs_root *root,
2231 struct btrfs_key *key,
2232 struct btrfs_chunk *chunk, int item_size)
2234 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2235 struct btrfs_disk_key disk_key;
2239 array_size = btrfs_super_sys_array_size(super_copy);
2240 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
2243 ptr = super_copy->sys_chunk_array + array_size;
2244 btrfs_cpu_key_to_disk(&disk_key, key);
2245 memcpy(ptr, &disk_key, sizeof(disk_key));
2246 ptr += sizeof(disk_key);
2247 memcpy(ptr, chunk, item_size);
2248 item_size += sizeof(disk_key);
2249 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
2253 static noinline u64 chunk_bytes_by_type(u64 type, u64 calc_size,
2254 int num_stripes, int sub_stripes)
2256 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
2258 else if (type & BTRFS_BLOCK_GROUP_RAID10)
2259 return calc_size * (num_stripes / sub_stripes);
2261 return calc_size * num_stripes;
2264 /* Used to sort the devices by max_avail(descending sort) */
2265 int btrfs_cmp_device_free_bytes(const void *dev_info1, const void *dev_info2)
2267 if (((struct btrfs_device_info *)dev_info1)->max_avail >
2268 ((struct btrfs_device_info *)dev_info2)->max_avail)
2270 else if (((struct btrfs_device_info *)dev_info1)->max_avail <
2271 ((struct btrfs_device_info *)dev_info2)->max_avail)
2277 static int __btrfs_calc_nstripes(struct btrfs_fs_devices *fs_devices, u64 type,
2278 int *num_stripes, int *min_stripes,
2285 if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
2286 *num_stripes = fs_devices->rw_devices;
2289 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
2293 if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
2294 if (fs_devices->rw_devices < 2)
2299 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2300 *num_stripes = fs_devices->rw_devices;
2301 if (*num_stripes < 4)
2303 *num_stripes &= ~(u32)1;
2311 static u64 __btrfs_calc_stripe_size(struct btrfs_fs_devices *fs_devices,
2312 u64 proposed_size, u64 type,
2313 int num_stripes, int small_stripe)
2315 int min_stripe_size = 1 * 1024 * 1024;
2316 u64 calc_size = proposed_size;
2317 u64 max_chunk_size = calc_size;
2320 if (type & (BTRFS_BLOCK_GROUP_RAID1 |
2321 BTRFS_BLOCK_GROUP_DUP |
2322 BTRFS_BLOCK_GROUP_RAID10))
2325 if (type & BTRFS_BLOCK_GROUP_DATA) {
2326 max_chunk_size = 10 * calc_size;
2327 min_stripe_size = 64 * 1024 * 1024;
2328 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
2329 max_chunk_size = 256 * 1024 * 1024;
2330 min_stripe_size = 32 * 1024 * 1024;
2331 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
2332 calc_size = 8 * 1024 * 1024;
2333 max_chunk_size = calc_size * 2;
2334 min_stripe_size = 1 * 1024 * 1024;
2337 /* we don't want a chunk larger than 10% of writeable space */
2338 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
2341 if (calc_size * num_stripes > max_chunk_size * ncopies) {
2342 calc_size = max_chunk_size * ncopies;
2343 do_div(calc_size, num_stripes);
2344 do_div(calc_size, BTRFS_STRIPE_LEN);
2345 calc_size *= BTRFS_STRIPE_LEN;
2348 /* we don't want tiny stripes */
2350 calc_size = max_t(u64, min_stripe_size, calc_size);
2353 * we're about to do_div by the BTRFS_STRIPE_LEN so lets make sure
2354 * we end up with something bigger than a stripe
2356 calc_size = max_t(u64, calc_size, BTRFS_STRIPE_LEN);
2358 do_div(calc_size, BTRFS_STRIPE_LEN);
2359 calc_size *= BTRFS_STRIPE_LEN;
2364 static struct map_lookup *__shrink_map_lookup_stripes(struct map_lookup *map,
2367 struct map_lookup *new;
2368 size_t len = map_lookup_size(num_stripes);
2370 BUG_ON(map->num_stripes < num_stripes);
2372 if (map->num_stripes == num_stripes)
2375 new = kmalloc(len, GFP_NOFS);
2377 /* just change map->num_stripes */
2378 map->num_stripes = num_stripes;
2382 memcpy(new, map, len);
2383 new->num_stripes = num_stripes;
2389 * helper to allocate device space from btrfs_device_info, in which we stored
2390 * max free space information of every device. It is used when we can not
2391 * allocate chunks by default size.
2393 * By this helper, we can allocate a new chunk as larger as possible.
2395 static int __btrfs_alloc_tiny_space(struct btrfs_trans_handle *trans,
2396 struct btrfs_fs_devices *fs_devices,
2397 struct btrfs_device_info *devices,
2398 int nr_device, u64 type,
2399 struct map_lookup **map_lookup,
2400 int min_stripes, u64 *stripe_size)
2402 int i, index, sort_again = 0;
2403 int min_devices = min_stripes;
2404 u64 max_avail, min_free;
2405 struct map_lookup *map = *map_lookup;
2408 if (nr_device < min_stripes)
2411 btrfs_descending_sort_devices(devices, nr_device);
2413 max_avail = devices[0].max_avail;
2417 for (i = 0; i < nr_device; i++) {
2419 * if dev_offset = 0, it means the free space of this device
2420 * is less than what we need, and we didn't search max avail
2421 * extent on this device, so do it now.
2423 if (!devices[i].dev_offset) {
2424 ret = find_free_dev_extent(trans, devices[i].dev,
2426 &devices[i].dev_offset,
2427 &devices[i].max_avail);
2428 if (ret != 0 && ret != -ENOSPC)
2434 /* we update the max avail free extent of each devices, sort again */
2436 btrfs_descending_sort_devices(devices, nr_device);
2438 if (type & BTRFS_BLOCK_GROUP_DUP)
2441 if (!devices[min_devices - 1].max_avail)
2444 max_avail = devices[min_devices - 1].max_avail;
2445 if (type & BTRFS_BLOCK_GROUP_DUP)
2446 do_div(max_avail, 2);
2448 max_avail = __btrfs_calc_stripe_size(fs_devices, max_avail, type,
2450 if (type & BTRFS_BLOCK_GROUP_DUP)
2451 min_free = max_avail * 2;
2453 min_free = max_avail;
2455 if (min_free > devices[min_devices - 1].max_avail)
2458 map = __shrink_map_lookup_stripes(map, min_stripes);
2459 *stripe_size = max_avail;
2462 for (i = 0; i < min_stripes; i++) {
2463 map->stripes[i].dev = devices[index].dev;
2464 map->stripes[i].physical = devices[index].dev_offset;
2465 if (type & BTRFS_BLOCK_GROUP_DUP) {
2467 map->stripes[i].dev = devices[index].dev;
2468 map->stripes[i].physical = devices[index].dev_offset +
2478 static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2479 struct btrfs_root *extent_root,
2480 struct map_lookup **map_ret,
2481 u64 *num_bytes, u64 *stripe_size,
2482 u64 start, u64 type)
2484 struct btrfs_fs_info *info = extent_root->fs_info;
2485 struct btrfs_device *device = NULL;
2486 struct btrfs_fs_devices *fs_devices = info->fs_devices;
2487 struct list_head *cur;
2488 struct map_lookup *map;
2489 struct extent_map_tree *em_tree;
2490 struct extent_map *em;
2491 struct btrfs_device_info *devices_info;
2492 struct list_head private_devs;
2493 u64 calc_size = 1024 * 1024 * 1024;
2500 int min_devices; /* the min number of devices we need */
2505 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
2506 (type & BTRFS_BLOCK_GROUP_DUP)) {
2508 type &= ~BTRFS_BLOCK_GROUP_DUP;
2510 if (list_empty(&fs_devices->alloc_list))
2513 ret = __btrfs_calc_nstripes(fs_devices, type, &num_stripes,
2514 &min_stripes, &sub_stripes);
2518 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
2523 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
2528 map->num_stripes = num_stripes;
2530 cur = fs_devices->alloc_list.next;
2534 calc_size = __btrfs_calc_stripe_size(fs_devices, calc_size, type,
2537 if (type & BTRFS_BLOCK_GROUP_DUP) {
2538 min_free = calc_size * 2;
2541 min_free = calc_size;
2542 min_devices = min_stripes;
2545 INIT_LIST_HEAD(&private_devs);
2546 while (index < num_stripes) {
2547 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
2548 BUG_ON(!device->writeable);
2549 if (device->total_bytes > device->bytes_used)
2550 avail = device->total_bytes - device->bytes_used;
2555 if (device->in_fs_metadata && avail >= min_free) {
2556 ret = find_free_dev_extent(trans, device, min_free,
2557 &devices_info[i].dev_offset,
2558 &devices_info[i].max_avail);
2560 list_move_tail(&device->dev_alloc_list,
2562 map->stripes[index].dev = device;
2563 map->stripes[index].physical =
2564 devices_info[i].dev_offset;
2566 if (type & BTRFS_BLOCK_GROUP_DUP) {
2567 map->stripes[index].dev = device;
2568 map->stripes[index].physical =
2569 devices_info[i].dev_offset +
2573 } else if (ret != -ENOSPC)
2576 devices_info[i].dev = device;
2578 } else if (device->in_fs_metadata &&
2579 avail >= BTRFS_STRIPE_LEN) {
2580 devices_info[i].dev = device;
2581 devices_info[i].max_avail = avail;
2585 if (cur == &fs_devices->alloc_list)
2589 list_splice(&private_devs, &fs_devices->alloc_list);
2590 if (index < num_stripes) {
2591 if (index >= min_stripes) {
2592 num_stripes = index;
2593 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2594 num_stripes /= sub_stripes;
2595 num_stripes *= sub_stripes;
2598 map = __shrink_map_lookup_stripes(map, num_stripes);
2599 } else if (i >= min_devices) {
2600 ret = __btrfs_alloc_tiny_space(trans, fs_devices,
2601 devices_info, i, type,
2611 map->sector_size = extent_root->sectorsize;
2612 map->stripe_len = BTRFS_STRIPE_LEN;
2613 map->io_align = BTRFS_STRIPE_LEN;
2614 map->io_width = BTRFS_STRIPE_LEN;
2616 map->sub_stripes = sub_stripes;
2619 *stripe_size = calc_size;
2620 *num_bytes = chunk_bytes_by_type(type, calc_size,
2621 map->num_stripes, sub_stripes);
2623 trace_btrfs_chunk_alloc(info->chunk_root, map, start, *num_bytes);
2625 em = alloc_extent_map(GFP_NOFS);
2630 em->bdev = (struct block_device *)map;
2632 em->len = *num_bytes;
2633 em->block_start = 0;
2634 em->block_len = em->len;
2636 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
2637 write_lock(&em_tree->lock);
2638 ret = add_extent_mapping(em_tree, em);
2639 write_unlock(&em_tree->lock);
2641 free_extent_map(em);
2643 ret = btrfs_make_block_group(trans, extent_root, 0, type,
2644 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2649 while (index < map->num_stripes) {
2650 device = map->stripes[index].dev;
2651 dev_offset = map->stripes[index].physical;
2653 ret = btrfs_alloc_dev_extent(trans, device,
2654 info->chunk_root->root_key.objectid,
2655 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2656 start, dev_offset, calc_size);
2661 kfree(devices_info);
2666 kfree(devices_info);
2670 static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
2671 struct btrfs_root *extent_root,
2672 struct map_lookup *map, u64 chunk_offset,
2673 u64 chunk_size, u64 stripe_size)
2676 struct btrfs_key key;
2677 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2678 struct btrfs_device *device;
2679 struct btrfs_chunk *chunk;
2680 struct btrfs_stripe *stripe;
2681 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
2685 chunk = kzalloc(item_size, GFP_NOFS);
2690 while (index < map->num_stripes) {
2691 device = map->stripes[index].dev;
2692 device->bytes_used += stripe_size;
2693 ret = btrfs_update_device(trans, device);
2699 stripe = &chunk->stripe;
2700 while (index < map->num_stripes) {
2701 device = map->stripes[index].dev;
2702 dev_offset = map->stripes[index].physical;
2704 btrfs_set_stack_stripe_devid(stripe, device->devid);
2705 btrfs_set_stack_stripe_offset(stripe, dev_offset);
2706 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
2711 btrfs_set_stack_chunk_length(chunk, chunk_size);
2712 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
2713 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
2714 btrfs_set_stack_chunk_type(chunk, map->type);
2715 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
2716 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
2717 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
2718 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
2719 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
2721 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2722 key.type = BTRFS_CHUNK_ITEM_KEY;
2723 key.offset = chunk_offset;
2725 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
2728 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2729 ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
2739 * Chunk allocation falls into two parts. The first part does works
2740 * that make the new allocated chunk useable, but not do any operation
2741 * that modifies the chunk tree. The second part does the works that
2742 * require modifying the chunk tree. This division is important for the
2743 * bootstrap process of adding storage to a seed btrfs.
2745 int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2746 struct btrfs_root *extent_root, u64 type)
2751 struct map_lookup *map;
2752 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2755 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2760 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2761 &stripe_size, chunk_offset, type);
2765 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2766 chunk_size, stripe_size);
2771 static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
2772 struct btrfs_root *root,
2773 struct btrfs_device *device)
2776 u64 sys_chunk_offset;
2780 u64 sys_stripe_size;
2782 struct map_lookup *map;
2783 struct map_lookup *sys_map;
2784 struct btrfs_fs_info *fs_info = root->fs_info;
2785 struct btrfs_root *extent_root = fs_info->extent_root;
2788 ret = find_next_chunk(fs_info->chunk_root,
2789 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
2792 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
2793 (fs_info->metadata_alloc_profile &
2794 fs_info->avail_metadata_alloc_bits);
2795 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2797 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2798 &stripe_size, chunk_offset, alloc_profile);
2801 sys_chunk_offset = chunk_offset + chunk_size;
2803 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
2804 (fs_info->system_alloc_profile &
2805 fs_info->avail_system_alloc_bits);
2806 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2808 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
2809 &sys_chunk_size, &sys_stripe_size,
2810 sys_chunk_offset, alloc_profile);
2813 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
2817 * Modifying chunk tree needs allocating new blocks from both
2818 * system block group and metadata block group. So we only can
2819 * do operations require modifying the chunk tree after both
2820 * block groups were created.
2822 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2823 chunk_size, stripe_size);
2826 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
2827 sys_chunk_offset, sys_chunk_size,
2833 int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
2835 struct extent_map *em;
2836 struct map_lookup *map;
2837 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2841 read_lock(&map_tree->map_tree.lock);
2842 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
2843 read_unlock(&map_tree->map_tree.lock);
2847 if (btrfs_test_opt(root, DEGRADED)) {
2848 free_extent_map(em);
2852 map = (struct map_lookup *)em->bdev;
2853 for (i = 0; i < map->num_stripes; i++) {
2854 if (!map->stripes[i].dev->writeable) {
2859 free_extent_map(em);
2863 void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
2865 extent_map_tree_init(&tree->map_tree, GFP_NOFS);
2868 void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
2870 struct extent_map *em;
2873 write_lock(&tree->map_tree.lock);
2874 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
2876 remove_extent_mapping(&tree->map_tree, em);
2877 write_unlock(&tree->map_tree.lock);
2882 free_extent_map(em);
2883 /* once for the tree */
2884 free_extent_map(em);
2888 int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
2890 struct extent_map *em;
2891 struct map_lookup *map;
2892 struct extent_map_tree *em_tree = &map_tree->map_tree;
2895 read_lock(&em_tree->lock);
2896 em = lookup_extent_mapping(em_tree, logical, len);
2897 read_unlock(&em_tree->lock);
2900 BUG_ON(em->start > logical || em->start + em->len < logical);
2901 map = (struct map_lookup *)em->bdev;
2902 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
2903 ret = map->num_stripes;
2904 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2905 ret = map->sub_stripes;
2908 free_extent_map(em);
2912 static int find_live_mirror(struct map_lookup *map, int first, int num,
2916 if (map->stripes[optimal].dev->bdev)
2918 for (i = first; i < first + num; i++) {
2919 if (map->stripes[i].dev->bdev)
2922 /* we couldn't find one that doesn't fail. Just return something
2923 * and the io error handling code will clean up eventually
2928 static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2929 u64 logical, u64 *length,
2930 struct btrfs_multi_bio **multi_ret,
2933 struct extent_map *em;
2934 struct map_lookup *map;
2935 struct extent_map_tree *em_tree = &map_tree->map_tree;
2938 u64 stripe_end_offset;
2942 int stripes_allocated = 8;
2943 int stripes_required = 1;
2948 struct btrfs_multi_bio *multi = NULL;
2950 if (multi_ret && !(rw & (REQ_WRITE | REQ_DISCARD)))
2951 stripes_allocated = 1;
2954 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
2959 atomic_set(&multi->error, 0);
2962 read_lock(&em_tree->lock);
2963 em = lookup_extent_mapping(em_tree, logical, *length);
2964 read_unlock(&em_tree->lock);
2967 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
2968 (unsigned long long)logical,
2969 (unsigned long long)*length);
2973 BUG_ON(em->start > logical || em->start + em->len < logical);
2974 map = (struct map_lookup *)em->bdev;
2975 offset = logical - em->start;
2977 if (mirror_num > map->num_stripes)
2980 /* if our multi bio struct is too small, back off and try again */
2981 if (rw & REQ_WRITE) {
2982 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
2983 BTRFS_BLOCK_GROUP_DUP)) {
2984 stripes_required = map->num_stripes;
2986 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2987 stripes_required = map->sub_stripes;
2991 if (rw & REQ_DISCARD) {
2992 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
2993 BTRFS_BLOCK_GROUP_RAID1 |
2994 BTRFS_BLOCK_GROUP_DUP |
2995 BTRFS_BLOCK_GROUP_RAID10)) {
2996 stripes_required = map->num_stripes;
2999 if (multi_ret && (rw & (REQ_WRITE | REQ_DISCARD)) &&
3000 stripes_allocated < stripes_required) {
3001 stripes_allocated = map->num_stripes;
3002 free_extent_map(em);
3008 * stripe_nr counts the total number of stripes we have to stride
3009 * to get to this block
3011 do_div(stripe_nr, map->stripe_len);
3013 stripe_offset = stripe_nr * map->stripe_len;
3014 BUG_ON(offset < stripe_offset);
3016 /* stripe_offset is the offset of this block in its stripe*/
3017 stripe_offset = offset - stripe_offset;
3019 if (rw & REQ_DISCARD)
3020 *length = min_t(u64, em->len - offset, *length);
3021 else if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
3022 BTRFS_BLOCK_GROUP_RAID1 |
3023 BTRFS_BLOCK_GROUP_RAID10 |
3024 BTRFS_BLOCK_GROUP_DUP)) {
3025 /* we limit the length of each bio to what fits in a stripe */
3026 *length = min_t(u64, em->len - offset,
3027 map->stripe_len - stripe_offset);
3029 *length = em->len - offset;
3037 stripe_nr_orig = stripe_nr;
3038 stripe_nr_end = (offset + *length + map->stripe_len - 1) &
3039 (~(map->stripe_len - 1));
3040 do_div(stripe_nr_end, map->stripe_len);
3041 stripe_end_offset = stripe_nr_end * map->stripe_len -
3043 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3044 if (rw & REQ_DISCARD)
3045 num_stripes = min_t(u64, map->num_stripes,
3046 stripe_nr_end - stripe_nr_orig);
3047 stripe_index = do_div(stripe_nr, map->num_stripes);
3048 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
3049 if (rw & (REQ_WRITE | REQ_DISCARD))
3050 num_stripes = map->num_stripes;
3051 else if (mirror_num)
3052 stripe_index = mirror_num - 1;
3054 stripe_index = find_live_mirror(map, 0,
3056 current->pid % map->num_stripes);
3059 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
3060 if (rw & (REQ_WRITE | REQ_DISCARD))
3061 num_stripes = map->num_stripes;
3062 else if (mirror_num)
3063 stripe_index = mirror_num - 1;
3065 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3066 int factor = map->num_stripes / map->sub_stripes;
3068 stripe_index = do_div(stripe_nr, factor);
3069 stripe_index *= map->sub_stripes;
3072 num_stripes = map->sub_stripes;
3073 else if (rw & REQ_DISCARD)
3074 num_stripes = min_t(u64, map->sub_stripes *
3075 (stripe_nr_end - stripe_nr_orig),
3077 else if (mirror_num)
3078 stripe_index += mirror_num - 1;
3080 stripe_index = find_live_mirror(map, stripe_index,
3081 map->sub_stripes, stripe_index +
3082 current->pid % map->sub_stripes);
3086 * after this do_div call, stripe_nr is the number of stripes
3087 * on this device we have to walk to find the data, and
3088 * stripe_index is the number of our device in the stripe array
3090 stripe_index = do_div(stripe_nr, map->num_stripes);
3092 BUG_ON(stripe_index >= map->num_stripes);
3094 if (rw & REQ_DISCARD) {
3095 for (i = 0; i < num_stripes; i++) {
3096 multi->stripes[i].physical =
3097 map->stripes[stripe_index].physical +
3098 stripe_offset + stripe_nr * map->stripe_len;
3099 multi->stripes[i].dev = map->stripes[stripe_index].dev;
3101 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3103 u32 last_stripe = 0;
3106 div_u64_rem(stripe_nr_end - 1,
3110 for (j = 0; j < map->num_stripes; j++) {
3113 div_u64_rem(stripe_nr_end - 1 - j,
3114 map->num_stripes, &test);
3115 if (test == stripe_index)
3118 stripes = stripe_nr_end - 1 - j;
3119 do_div(stripes, map->num_stripes);
3120 multi->stripes[i].length = map->stripe_len *
3121 (stripes - stripe_nr + 1);
3124 multi->stripes[i].length -=
3128 if (stripe_index == last_stripe)
3129 multi->stripes[i].length -=
3131 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3134 int factor = map->num_stripes /
3136 u32 last_stripe = 0;
3138 div_u64_rem(stripe_nr_end - 1,
3139 factor, &last_stripe);
3140 last_stripe *= map->sub_stripes;
3142 for (j = 0; j < factor; j++) {
3145 div_u64_rem(stripe_nr_end - 1 - j,
3149 stripe_index / map->sub_stripes)
3152 stripes = stripe_nr_end - 1 - j;
3153 do_div(stripes, factor);
3154 multi->stripes[i].length = map->stripe_len *
3155 (stripes - stripe_nr + 1);
3157 if (i < map->sub_stripes) {
3158 multi->stripes[i].length -=
3160 if (i == map->sub_stripes - 1)
3163 if (stripe_index >= last_stripe &&
3164 stripe_index <= (last_stripe +
3165 map->sub_stripes - 1)) {
3166 multi->stripes[i].length -=
3170 multi->stripes[i].length = *length;
3173 if (stripe_index == map->num_stripes) {
3174 /* This could only happen for RAID0/10 */
3180 for (i = 0; i < num_stripes; i++) {
3181 multi->stripes[i].physical =
3182 map->stripes[stripe_index].physical +
3184 stripe_nr * map->stripe_len;
3185 multi->stripes[i].dev =
3186 map->stripes[stripe_index].dev;
3192 multi->num_stripes = num_stripes;
3193 multi->max_errors = max_errors;
3196 free_extent_map(em);
3200 int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3201 u64 logical, u64 *length,
3202 struct btrfs_multi_bio **multi_ret, int mirror_num)
3204 return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
3208 int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
3209 u64 chunk_start, u64 physical, u64 devid,
3210 u64 **logical, int *naddrs, int *stripe_len)
3212 struct extent_map_tree *em_tree = &map_tree->map_tree;
3213 struct extent_map *em;
3214 struct map_lookup *map;
3221 read_lock(&em_tree->lock);
3222 em = lookup_extent_mapping(em_tree, chunk_start, 1);
3223 read_unlock(&em_tree->lock);
3225 BUG_ON(!em || em->start != chunk_start);
3226 map = (struct map_lookup *)em->bdev;
3229 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3230 do_div(length, map->num_stripes / map->sub_stripes);
3231 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3232 do_div(length, map->num_stripes);
3234 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
3237 for (i = 0; i < map->num_stripes; i++) {
3238 if (devid && map->stripes[i].dev->devid != devid)
3240 if (map->stripes[i].physical > physical ||
3241 map->stripes[i].physical + length <= physical)
3244 stripe_nr = physical - map->stripes[i].physical;
3245 do_div(stripe_nr, map->stripe_len);
3247 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3248 stripe_nr = stripe_nr * map->num_stripes + i;
3249 do_div(stripe_nr, map->sub_stripes);
3250 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3251 stripe_nr = stripe_nr * map->num_stripes + i;
3253 bytenr = chunk_start + stripe_nr * map->stripe_len;
3254 WARN_ON(nr >= map->num_stripes);
3255 for (j = 0; j < nr; j++) {
3256 if (buf[j] == bytenr)
3260 WARN_ON(nr >= map->num_stripes);
3267 *stripe_len = map->stripe_len;
3269 free_extent_map(em);
3273 static void end_bio_multi_stripe(struct bio *bio, int err)
3275 struct btrfs_multi_bio *multi = bio->bi_private;
3276 int is_orig_bio = 0;
3279 atomic_inc(&multi->error);
3281 if (bio == multi->orig_bio)
3284 if (atomic_dec_and_test(&multi->stripes_pending)) {
3287 bio = multi->orig_bio;
3289 bio->bi_private = multi->private;
3290 bio->bi_end_io = multi->end_io;
3291 /* only send an error to the higher layers if it is
3292 * beyond the tolerance of the multi-bio
3294 if (atomic_read(&multi->error) > multi->max_errors) {
3298 * this bio is actually up to date, we didn't
3299 * go over the max number of errors
3301 set_bit(BIO_UPTODATE, &bio->bi_flags);
3306 bio_endio(bio, err);
3307 } else if (!is_orig_bio) {
3312 struct async_sched {
3315 struct btrfs_fs_info *info;
3316 struct btrfs_work work;
3320 * see run_scheduled_bios for a description of why bios are collected for
3323 * This will add one bio to the pending list for a device and make sure
3324 * the work struct is scheduled.
3326 static noinline int schedule_bio(struct btrfs_root *root,
3327 struct btrfs_device *device,
3328 int rw, struct bio *bio)
3330 int should_queue = 1;
3331 struct btrfs_pending_bios *pending_bios;
3333 /* don't bother with additional async steps for reads, right now */
3334 if (!(rw & REQ_WRITE)) {
3336 submit_bio(rw, bio);
3342 * nr_async_bios allows us to reliably return congestion to the
3343 * higher layers. Otherwise, the async bio makes it appear we have
3344 * made progress against dirty pages when we've really just put it
3345 * on a queue for later
3347 atomic_inc(&root->fs_info->nr_async_bios);
3348 WARN_ON(bio->bi_next);
3349 bio->bi_next = NULL;
3352 spin_lock(&device->io_lock);
3353 if (bio->bi_rw & REQ_SYNC)
3354 pending_bios = &device->pending_sync_bios;
3356 pending_bios = &device->pending_bios;
3358 if (pending_bios->tail)
3359 pending_bios->tail->bi_next = bio;
3361 pending_bios->tail = bio;
3362 if (!pending_bios->head)
3363 pending_bios->head = bio;
3364 if (device->running_pending)
3367 spin_unlock(&device->io_lock);
3370 btrfs_queue_worker(&root->fs_info->submit_workers,
3375 int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
3376 int mirror_num, int async_submit)
3378 struct btrfs_mapping_tree *map_tree;
3379 struct btrfs_device *dev;
3380 struct bio *first_bio = bio;
3381 u64 logical = (u64)bio->bi_sector << 9;
3384 struct btrfs_multi_bio *multi = NULL;
3389 length = bio->bi_size;
3390 map_tree = &root->fs_info->mapping_tree;
3391 map_length = length;
3393 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
3397 total_devs = multi->num_stripes;
3398 if (map_length < length) {
3399 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
3400 "len %llu\n", (unsigned long long)logical,
3401 (unsigned long long)length,
3402 (unsigned long long)map_length);
3405 multi->end_io = first_bio->bi_end_io;
3406 multi->private = first_bio->bi_private;
3407 multi->orig_bio = first_bio;
3408 atomic_set(&multi->stripes_pending, multi->num_stripes);
3410 while (dev_nr < total_devs) {
3411 if (total_devs > 1) {
3412 if (dev_nr < total_devs - 1) {
3413 bio = bio_clone(first_bio, GFP_NOFS);
3418 bio->bi_private = multi;
3419 bio->bi_end_io = end_bio_multi_stripe;
3421 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
3422 dev = multi->stripes[dev_nr].dev;
3423 if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
3424 bio->bi_bdev = dev->bdev;
3426 schedule_bio(root, dev, rw, bio);
3428 submit_bio(rw, bio);
3430 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
3431 bio->bi_sector = logical >> 9;
3432 bio_endio(bio, -EIO);
3436 if (total_devs == 1)
3441 struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
3444 struct btrfs_device *device;
3445 struct btrfs_fs_devices *cur_devices;
3447 cur_devices = root->fs_info->fs_devices;
3448 while (cur_devices) {
3450 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3451 device = __find_device(&cur_devices->devices,
3456 cur_devices = cur_devices->seed;
3461 static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
3462 u64 devid, u8 *dev_uuid)
3464 struct btrfs_device *device;
3465 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
3467 device = kzalloc(sizeof(*device), GFP_NOFS);
3470 list_add(&device->dev_list,
3471 &fs_devices->devices);
3472 device->dev_root = root->fs_info->dev_root;
3473 device->devid = devid;
3474 device->work.func = pending_bios_fn;
3475 device->fs_devices = fs_devices;
3476 device->missing = 1;
3477 fs_devices->num_devices++;
3478 fs_devices->missing_devices++;
3479 spin_lock_init(&device->io_lock);
3480 INIT_LIST_HEAD(&device->dev_alloc_list);
3481 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
3485 static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
3486 struct extent_buffer *leaf,
3487 struct btrfs_chunk *chunk)
3489 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3490 struct map_lookup *map;
3491 struct extent_map *em;
3495 u8 uuid[BTRFS_UUID_SIZE];
3500 logical = key->offset;
3501 length = btrfs_chunk_length(leaf, chunk);
3503 read_lock(&map_tree->map_tree.lock);
3504 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
3505 read_unlock(&map_tree->map_tree.lock);
3507 /* already mapped? */
3508 if (em && em->start <= logical && em->start + em->len > logical) {
3509 free_extent_map(em);
3512 free_extent_map(em);
3515 em = alloc_extent_map(GFP_NOFS);
3518 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3519 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
3521 free_extent_map(em);
3525 em->bdev = (struct block_device *)map;
3526 em->start = logical;
3528 em->block_start = 0;
3529 em->block_len = em->len;
3531 map->num_stripes = num_stripes;
3532 map->io_width = btrfs_chunk_io_width(leaf, chunk);
3533 map->io_align = btrfs_chunk_io_align(leaf, chunk);
3534 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
3535 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
3536 map->type = btrfs_chunk_type(leaf, chunk);
3537 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
3538 for (i = 0; i < num_stripes; i++) {
3539 map->stripes[i].physical =
3540 btrfs_stripe_offset_nr(leaf, chunk, i);
3541 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
3542 read_extent_buffer(leaf, uuid, (unsigned long)
3543 btrfs_stripe_dev_uuid_nr(chunk, i),
3545 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
3547 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
3549 free_extent_map(em);
3552 if (!map->stripes[i].dev) {
3553 map->stripes[i].dev =
3554 add_missing_dev(root, devid, uuid);
3555 if (!map->stripes[i].dev) {
3557 free_extent_map(em);
3561 map->stripes[i].dev->in_fs_metadata = 1;
3564 write_lock(&map_tree->map_tree.lock);
3565 ret = add_extent_mapping(&map_tree->map_tree, em);
3566 write_unlock(&map_tree->map_tree.lock);
3568 free_extent_map(em);
3573 static int fill_device_from_item(struct extent_buffer *leaf,
3574 struct btrfs_dev_item *dev_item,
3575 struct btrfs_device *device)
3579 device->devid = btrfs_device_id(leaf, dev_item);
3580 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
3581 device->total_bytes = device->disk_total_bytes;
3582 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
3583 device->type = btrfs_device_type(leaf, dev_item);
3584 device->io_align = btrfs_device_io_align(leaf, dev_item);
3585 device->io_width = btrfs_device_io_width(leaf, dev_item);
3586 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
3588 ptr = (unsigned long)btrfs_device_uuid(dev_item);
3589 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
3594 static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
3596 struct btrfs_fs_devices *fs_devices;
3599 mutex_lock(&uuid_mutex);
3601 fs_devices = root->fs_info->fs_devices->seed;
3602 while (fs_devices) {
3603 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3607 fs_devices = fs_devices->seed;
3610 fs_devices = find_fsid(fsid);
3616 fs_devices = clone_fs_devices(fs_devices);
3617 if (IS_ERR(fs_devices)) {
3618 ret = PTR_ERR(fs_devices);
3622 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
3623 root->fs_info->bdev_holder);
3627 if (!fs_devices->seeding) {
3628 __btrfs_close_devices(fs_devices);
3629 free_fs_devices(fs_devices);
3634 fs_devices->seed = root->fs_info->fs_devices->seed;
3635 root->fs_info->fs_devices->seed = fs_devices;
3637 mutex_unlock(&uuid_mutex);
3641 static int read_one_dev(struct btrfs_root *root,
3642 struct extent_buffer *leaf,
3643 struct btrfs_dev_item *dev_item)
3645 struct btrfs_device *device;
3648 u8 fs_uuid[BTRFS_UUID_SIZE];
3649 u8 dev_uuid[BTRFS_UUID_SIZE];
3651 devid = btrfs_device_id(leaf, dev_item);
3652 read_extent_buffer(leaf, dev_uuid,
3653 (unsigned long)btrfs_device_uuid(dev_item),
3655 read_extent_buffer(leaf, fs_uuid,
3656 (unsigned long)btrfs_device_fsid(dev_item),
3659 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
3660 ret = open_seed_devices(root, fs_uuid);
3661 if (ret && !btrfs_test_opt(root, DEGRADED))
3665 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
3666 if (!device || !device->bdev) {
3667 if (!btrfs_test_opt(root, DEGRADED))
3671 printk(KERN_WARNING "warning devid %llu missing\n",
3672 (unsigned long long)devid);
3673 device = add_missing_dev(root, devid, dev_uuid);
3676 } else if (!device->missing) {
3678 * this happens when a device that was properly setup
3679 * in the device info lists suddenly goes bad.
3680 * device->bdev is NULL, and so we have to set
3681 * device->missing to one here
3683 root->fs_info->fs_devices->missing_devices++;
3684 device->missing = 1;
3688 if (device->fs_devices != root->fs_info->fs_devices) {
3689 BUG_ON(device->writeable);
3690 if (device->generation !=
3691 btrfs_device_generation(leaf, dev_item))
3695 fill_device_from_item(leaf, dev_item, device);
3696 device->dev_root = root->fs_info->dev_root;
3697 device->in_fs_metadata = 1;
3698 if (device->writeable)
3699 device->fs_devices->total_rw_bytes += device->total_bytes;
3704 int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
3706 struct btrfs_dev_item *dev_item;
3708 dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
3710 return read_one_dev(root, buf, dev_item);
3713 int btrfs_read_sys_array(struct btrfs_root *root)
3715 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
3716 struct extent_buffer *sb;
3717 struct btrfs_disk_key *disk_key;
3718 struct btrfs_chunk *chunk;
3720 unsigned long sb_ptr;
3726 struct btrfs_key key;
3728 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
3729 BTRFS_SUPER_INFO_SIZE);
3732 btrfs_set_buffer_uptodate(sb);
3733 btrfs_set_buffer_lockdep_class(sb, 0);
3735 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
3736 array_size = btrfs_super_sys_array_size(super_copy);
3738 ptr = super_copy->sys_chunk_array;
3739 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
3742 while (cur < array_size) {
3743 disk_key = (struct btrfs_disk_key *)ptr;
3744 btrfs_disk_key_to_cpu(&key, disk_key);
3746 len = sizeof(*disk_key); ptr += len;
3750 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
3751 chunk = (struct btrfs_chunk *)sb_ptr;
3752 ret = read_one_chunk(root, &key, sb, chunk);
3755 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
3756 len = btrfs_chunk_item_size(num_stripes);
3765 free_extent_buffer(sb);
3769 int btrfs_read_chunk_tree(struct btrfs_root *root)
3771 struct btrfs_path *path;
3772 struct extent_buffer *leaf;
3773 struct btrfs_key key;
3774 struct btrfs_key found_key;
3778 root = root->fs_info->chunk_root;
3780 path = btrfs_alloc_path();
3784 /* first we search for all of the device items, and then we
3785 * read in all of the chunk items. This way we can create chunk
3786 * mappings that reference all of the devices that are afound
3788 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
3792 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3796 leaf = path->nodes[0];
3797 slot = path->slots[0];
3798 if (slot >= btrfs_header_nritems(leaf)) {
3799 ret = btrfs_next_leaf(root, path);
3806 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3807 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3808 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
3810 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
3811 struct btrfs_dev_item *dev_item;
3812 dev_item = btrfs_item_ptr(leaf, slot,
3813 struct btrfs_dev_item);
3814 ret = read_one_dev(root, leaf, dev_item);
3818 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
3819 struct btrfs_chunk *chunk;
3820 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3821 ret = read_one_chunk(root, &found_key, leaf, chunk);
3827 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3829 btrfs_release_path(root, path);
3834 btrfs_free_path(path);