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 list_for_each_entry(device, &fs_devices->devices, dev_list) {
486 blkdev_put(device->bdev, device->mode);
487 fs_devices->open_devices--;
489 if (device->writeable) {
490 list_del_init(&device->dev_alloc_list);
491 fs_devices->rw_devices--;
495 device->writeable = 0;
496 device->in_fs_metadata = 0;
498 WARN_ON(fs_devices->open_devices);
499 WARN_ON(fs_devices->rw_devices);
500 fs_devices->opened = 0;
501 fs_devices->seeding = 0;
506 int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
508 struct btrfs_fs_devices *seed_devices = NULL;
511 mutex_lock(&uuid_mutex);
512 ret = __btrfs_close_devices(fs_devices);
513 if (!fs_devices->opened) {
514 seed_devices = fs_devices->seed;
515 fs_devices->seed = NULL;
517 mutex_unlock(&uuid_mutex);
519 while (seed_devices) {
520 fs_devices = seed_devices;
521 seed_devices = fs_devices->seed;
522 __btrfs_close_devices(fs_devices);
523 free_fs_devices(fs_devices);
528 static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
529 fmode_t flags, void *holder)
531 struct block_device *bdev;
532 struct list_head *head = &fs_devices->devices;
533 struct btrfs_device *device;
534 struct block_device *latest_bdev = NULL;
535 struct buffer_head *bh;
536 struct btrfs_super_block *disk_super;
537 u64 latest_devid = 0;
538 u64 latest_transid = 0;
545 list_for_each_entry(device, head, dev_list) {
551 bdev = blkdev_get_by_path(device->name, flags, holder);
553 printk(KERN_INFO "open %s failed\n", device->name);
556 set_blocksize(bdev, 4096);
558 bh = btrfs_read_dev_super(bdev);
564 disk_super = (struct btrfs_super_block *)bh->b_data;
565 devid = btrfs_stack_device_id(&disk_super->dev_item);
566 if (devid != device->devid)
569 if (memcmp(device->uuid, disk_super->dev_item.uuid,
573 device->generation = btrfs_super_generation(disk_super);
574 if (!latest_transid || device->generation > latest_transid) {
575 latest_devid = devid;
576 latest_transid = device->generation;
580 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
581 device->writeable = 0;
583 device->writeable = !bdev_read_only(bdev);
588 device->in_fs_metadata = 0;
589 device->mode = flags;
591 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
592 fs_devices->rotating = 1;
594 fs_devices->open_devices++;
595 if (device->writeable) {
596 fs_devices->rw_devices++;
597 list_add(&device->dev_alloc_list,
598 &fs_devices->alloc_list);
605 blkdev_put(bdev, flags);
609 if (fs_devices->open_devices == 0) {
613 fs_devices->seeding = seeding;
614 fs_devices->opened = 1;
615 fs_devices->latest_bdev = latest_bdev;
616 fs_devices->latest_devid = latest_devid;
617 fs_devices->latest_trans = latest_transid;
618 fs_devices->total_rw_bytes = 0;
623 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
624 fmode_t flags, void *holder)
628 mutex_lock(&uuid_mutex);
629 if (fs_devices->opened) {
630 fs_devices->opened++;
633 ret = __btrfs_open_devices(fs_devices, flags, holder);
635 mutex_unlock(&uuid_mutex);
639 int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
640 struct btrfs_fs_devices **fs_devices_ret)
642 struct btrfs_super_block *disk_super;
643 struct block_device *bdev;
644 struct buffer_head *bh;
649 mutex_lock(&uuid_mutex);
652 bdev = blkdev_get_by_path(path, flags, holder);
659 ret = set_blocksize(bdev, 4096);
662 bh = btrfs_read_dev_super(bdev);
667 disk_super = (struct btrfs_super_block *)bh->b_data;
668 devid = btrfs_stack_device_id(&disk_super->dev_item);
669 transid = btrfs_super_generation(disk_super);
670 if (disk_super->label[0])
671 printk(KERN_INFO "device label %s ", disk_super->label);
673 /* FIXME, make a readl uuid parser */
674 printk(KERN_INFO "device fsid %llx-%llx ",
675 *(unsigned long long *)disk_super->fsid,
676 *(unsigned long long *)(disk_super->fsid + 8));
678 printk(KERN_CONT "devid %llu transid %llu %s\n",
679 (unsigned long long)devid, (unsigned long long)transid, path);
680 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
684 blkdev_put(bdev, flags);
686 mutex_unlock(&uuid_mutex);
690 /* helper to account the used device space in the range */
691 int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
692 u64 end, u64 *length)
694 struct btrfs_key key;
695 struct btrfs_root *root = device->dev_root;
696 struct btrfs_dev_extent *dev_extent;
697 struct btrfs_path *path;
701 struct extent_buffer *l;
705 if (start >= device->total_bytes)
708 path = btrfs_alloc_path();
713 key.objectid = device->devid;
715 key.type = BTRFS_DEV_EXTENT_KEY;
717 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
721 ret = btrfs_previous_item(root, path, key.objectid, key.type);
728 slot = path->slots[0];
729 if (slot >= btrfs_header_nritems(l)) {
730 ret = btrfs_next_leaf(root, path);
738 btrfs_item_key_to_cpu(l, &key, slot);
740 if (key.objectid < device->devid)
743 if (key.objectid > device->devid)
746 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
749 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
750 extent_end = key.offset + btrfs_dev_extent_length(l,
752 if (key.offset <= start && extent_end > end) {
753 *length = end - start + 1;
755 } else if (key.offset <= start && extent_end > start)
756 *length += extent_end - start;
757 else if (key.offset > start && extent_end <= end)
758 *length += extent_end - key.offset;
759 else if (key.offset > start && key.offset <= end) {
760 *length += end - key.offset + 1;
762 } else if (key.offset > end)
770 btrfs_free_path(path);
775 * find_free_dev_extent - find free space in the specified device
776 * @trans: transaction handler
777 * @device: the device which we search the free space in
778 * @num_bytes: the size of the free space that we need
779 * @start: store the start of the free space.
780 * @len: the size of the free space. that we find, or the size of the max
781 * free space if we don't find suitable free space
783 * this uses a pretty simple search, the expectation is that it is
784 * called very infrequently and that a given device has a small number
787 * @start is used to store the start of the free space if we find. But if we
788 * don't find suitable free space, it will be used to store the start position
789 * of the max free space.
791 * @len is used to store the size of the free space that we find.
792 * But if we don't find suitable free space, it is used to store the size of
793 * the max free space.
795 int find_free_dev_extent(struct btrfs_trans_handle *trans,
796 struct btrfs_device *device, u64 num_bytes,
797 u64 *start, u64 *len)
799 struct btrfs_key key;
800 struct btrfs_root *root = device->dev_root;
801 struct btrfs_dev_extent *dev_extent;
802 struct btrfs_path *path;
808 u64 search_end = device->total_bytes;
811 struct extent_buffer *l;
813 /* FIXME use last free of some kind */
815 /* we don't want to overwrite the superblock on the drive,
816 * so we make sure to start at an offset of at least 1MB
818 search_start = 1024 * 1024;
820 if (root->fs_info->alloc_start + num_bytes <= search_end)
821 search_start = max(root->fs_info->alloc_start, search_start);
823 max_hole_start = search_start;
826 if (search_start >= search_end) {
831 path = btrfs_alloc_path();
838 key.objectid = device->devid;
839 key.offset = search_start;
840 key.type = BTRFS_DEV_EXTENT_KEY;
842 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
846 ret = btrfs_previous_item(root, path, key.objectid, key.type);
853 slot = path->slots[0];
854 if (slot >= btrfs_header_nritems(l)) {
855 ret = btrfs_next_leaf(root, path);
863 btrfs_item_key_to_cpu(l, &key, slot);
865 if (key.objectid < device->devid)
868 if (key.objectid > device->devid)
871 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
874 if (key.offset > search_start) {
875 hole_size = key.offset - search_start;
877 if (hole_size > max_hole_size) {
878 max_hole_start = search_start;
879 max_hole_size = hole_size;
883 * If this free space is greater than which we need,
884 * it must be the max free space that we have found
885 * until now, so max_hole_start must point to the start
886 * of this free space and the length of this free space
887 * is stored in max_hole_size. Thus, we return
888 * max_hole_start and max_hole_size and go back to the
891 if (hole_size >= num_bytes) {
897 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
898 extent_end = key.offset + btrfs_dev_extent_length(l,
900 if (extent_end > search_start)
901 search_start = extent_end;
907 hole_size = search_end- search_start;
908 if (hole_size > max_hole_size) {
909 max_hole_start = search_start;
910 max_hole_size = hole_size;
914 if (hole_size < num_bytes)
920 btrfs_free_path(path);
922 *start = max_hole_start;
924 *len = max_hole_size;
928 static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
929 struct btrfs_device *device,
933 struct btrfs_path *path;
934 struct btrfs_root *root = device->dev_root;
935 struct btrfs_key key;
936 struct btrfs_key found_key;
937 struct extent_buffer *leaf = NULL;
938 struct btrfs_dev_extent *extent = NULL;
940 path = btrfs_alloc_path();
944 key.objectid = device->devid;
946 key.type = BTRFS_DEV_EXTENT_KEY;
948 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
950 ret = btrfs_previous_item(root, path, key.objectid,
951 BTRFS_DEV_EXTENT_KEY);
954 leaf = path->nodes[0];
955 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
956 extent = btrfs_item_ptr(leaf, path->slots[0],
957 struct btrfs_dev_extent);
958 BUG_ON(found_key.offset > start || found_key.offset +
959 btrfs_dev_extent_length(leaf, extent) < start);
960 } else if (ret == 0) {
961 leaf = path->nodes[0];
962 extent = btrfs_item_ptr(leaf, path->slots[0],
963 struct btrfs_dev_extent);
967 if (device->bytes_used > 0)
968 device->bytes_used -= btrfs_dev_extent_length(leaf, extent);
969 ret = btrfs_del_item(trans, root, path);
973 btrfs_free_path(path);
977 int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
978 struct btrfs_device *device,
979 u64 chunk_tree, u64 chunk_objectid,
980 u64 chunk_offset, u64 start, u64 num_bytes)
983 struct btrfs_path *path;
984 struct btrfs_root *root = device->dev_root;
985 struct btrfs_dev_extent *extent;
986 struct extent_buffer *leaf;
987 struct btrfs_key key;
989 WARN_ON(!device->in_fs_metadata);
990 path = btrfs_alloc_path();
994 key.objectid = device->devid;
996 key.type = BTRFS_DEV_EXTENT_KEY;
997 ret = btrfs_insert_empty_item(trans, root, path, &key,
1001 leaf = path->nodes[0];
1002 extent = btrfs_item_ptr(leaf, path->slots[0],
1003 struct btrfs_dev_extent);
1004 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1005 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1006 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1008 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1009 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1012 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1013 btrfs_mark_buffer_dirty(leaf);
1014 btrfs_free_path(path);
1018 static noinline int find_next_chunk(struct btrfs_root *root,
1019 u64 objectid, u64 *offset)
1021 struct btrfs_path *path;
1023 struct btrfs_key key;
1024 struct btrfs_chunk *chunk;
1025 struct btrfs_key found_key;
1027 path = btrfs_alloc_path();
1030 key.objectid = objectid;
1031 key.offset = (u64)-1;
1032 key.type = BTRFS_CHUNK_ITEM_KEY;
1034 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1040 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1044 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1046 if (found_key.objectid != objectid)
1049 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1050 struct btrfs_chunk);
1051 *offset = found_key.offset +
1052 btrfs_chunk_length(path->nodes[0], chunk);
1057 btrfs_free_path(path);
1061 static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
1064 struct btrfs_key key;
1065 struct btrfs_key found_key;
1066 struct btrfs_path *path;
1068 root = root->fs_info->chunk_root;
1070 path = btrfs_alloc_path();
1074 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1075 key.type = BTRFS_DEV_ITEM_KEY;
1076 key.offset = (u64)-1;
1078 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1084 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1085 BTRFS_DEV_ITEM_KEY);
1089 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1091 *objectid = found_key.offset + 1;
1095 btrfs_free_path(path);
1100 * the device information is stored in the chunk root
1101 * the btrfs_device struct should be fully filled in
1103 int btrfs_add_device(struct btrfs_trans_handle *trans,
1104 struct btrfs_root *root,
1105 struct btrfs_device *device)
1108 struct btrfs_path *path;
1109 struct btrfs_dev_item *dev_item;
1110 struct extent_buffer *leaf;
1111 struct btrfs_key key;
1114 root = root->fs_info->chunk_root;
1116 path = btrfs_alloc_path();
1120 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1121 key.type = BTRFS_DEV_ITEM_KEY;
1122 key.offset = device->devid;
1124 ret = btrfs_insert_empty_item(trans, root, path, &key,
1129 leaf = path->nodes[0];
1130 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1132 btrfs_set_device_id(leaf, dev_item, device->devid);
1133 btrfs_set_device_generation(leaf, dev_item, 0);
1134 btrfs_set_device_type(leaf, dev_item, device->type);
1135 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1136 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1137 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
1138 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1139 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1140 btrfs_set_device_group(leaf, dev_item, 0);
1141 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1142 btrfs_set_device_bandwidth(leaf, dev_item, 0);
1143 btrfs_set_device_start_offset(leaf, dev_item, 0);
1145 ptr = (unsigned long)btrfs_device_uuid(dev_item);
1146 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1147 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1148 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
1149 btrfs_mark_buffer_dirty(leaf);
1153 btrfs_free_path(path);
1157 static int btrfs_rm_dev_item(struct btrfs_root *root,
1158 struct btrfs_device *device)
1161 struct btrfs_path *path;
1162 struct btrfs_key key;
1163 struct btrfs_trans_handle *trans;
1165 root = root->fs_info->chunk_root;
1167 path = btrfs_alloc_path();
1171 trans = btrfs_start_transaction(root, 0);
1172 if (IS_ERR(trans)) {
1173 btrfs_free_path(path);
1174 return PTR_ERR(trans);
1176 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1177 key.type = BTRFS_DEV_ITEM_KEY;
1178 key.offset = device->devid;
1181 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1190 ret = btrfs_del_item(trans, root, path);
1194 btrfs_free_path(path);
1195 unlock_chunks(root);
1196 btrfs_commit_transaction(trans, root);
1200 int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1202 struct btrfs_device *device;
1203 struct btrfs_device *next_device;
1204 struct block_device *bdev;
1205 struct buffer_head *bh = NULL;
1206 struct btrfs_super_block *disk_super;
1213 mutex_lock(&uuid_mutex);
1214 mutex_lock(&root->fs_info->volume_mutex);
1216 all_avail = root->fs_info->avail_data_alloc_bits |
1217 root->fs_info->avail_system_alloc_bits |
1218 root->fs_info->avail_metadata_alloc_bits;
1220 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
1221 root->fs_info->fs_devices->num_devices <= 4) {
1222 printk(KERN_ERR "btrfs: unable to go below four devices "
1228 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
1229 root->fs_info->fs_devices->num_devices <= 2) {
1230 printk(KERN_ERR "btrfs: unable to go below two "
1231 "devices on raid1\n");
1236 if (strcmp(device_path, "missing") == 0) {
1237 struct list_head *devices;
1238 struct btrfs_device *tmp;
1241 devices = &root->fs_info->fs_devices->devices;
1242 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1243 list_for_each_entry(tmp, devices, dev_list) {
1244 if (tmp->in_fs_metadata && !tmp->bdev) {
1249 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1254 printk(KERN_ERR "btrfs: no missing devices found to "
1259 bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL,
1260 root->fs_info->bdev_holder);
1262 ret = PTR_ERR(bdev);
1266 set_blocksize(bdev, 4096);
1267 bh = btrfs_read_dev_super(bdev);
1272 disk_super = (struct btrfs_super_block *)bh->b_data;
1273 devid = btrfs_stack_device_id(&disk_super->dev_item);
1274 dev_uuid = disk_super->dev_item.uuid;
1275 device = btrfs_find_device(root, devid, dev_uuid,
1283 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
1284 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1290 if (device->writeable) {
1291 list_del_init(&device->dev_alloc_list);
1292 root->fs_info->fs_devices->rw_devices--;
1295 ret = btrfs_shrink_device(device, 0);
1299 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1303 device->in_fs_metadata = 0;
1306 * the device list mutex makes sure that we don't change
1307 * the device list while someone else is writing out all
1308 * the device supers.
1310 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1311 list_del_init(&device->dev_list);
1312 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1314 device->fs_devices->num_devices--;
1316 if (device->missing)
1317 root->fs_info->fs_devices->missing_devices--;
1319 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1320 struct btrfs_device, dev_list);
1321 if (device->bdev == root->fs_info->sb->s_bdev)
1322 root->fs_info->sb->s_bdev = next_device->bdev;
1323 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1324 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1327 blkdev_put(device->bdev, device->mode);
1328 device->bdev = NULL;
1329 device->fs_devices->open_devices--;
1332 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
1333 btrfs_set_super_num_devices(&root->fs_info->super_copy, num_devices);
1335 if (device->fs_devices->open_devices == 0) {
1336 struct btrfs_fs_devices *fs_devices;
1337 fs_devices = root->fs_info->fs_devices;
1338 while (fs_devices) {
1339 if (fs_devices->seed == device->fs_devices)
1341 fs_devices = fs_devices->seed;
1343 fs_devices->seed = device->fs_devices->seed;
1344 device->fs_devices->seed = NULL;
1345 __btrfs_close_devices(device->fs_devices);
1346 free_fs_devices(device->fs_devices);
1350 * at this point, the device is zero sized. We want to
1351 * remove it from the devices list and zero out the old super
1353 if (device->writeable) {
1354 /* make sure this device isn't detected as part of
1357 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1358 set_buffer_dirty(bh);
1359 sync_dirty_buffer(bh);
1362 kfree(device->name);
1370 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
1372 mutex_unlock(&root->fs_info->volume_mutex);
1373 mutex_unlock(&uuid_mutex);
1376 if (device->writeable) {
1377 list_add(&device->dev_alloc_list,
1378 &root->fs_info->fs_devices->alloc_list);
1379 root->fs_info->fs_devices->rw_devices++;
1385 * does all the dirty work required for changing file system's UUID.
1387 static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans,
1388 struct btrfs_root *root)
1390 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1391 struct btrfs_fs_devices *old_devices;
1392 struct btrfs_fs_devices *seed_devices;
1393 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
1394 struct btrfs_device *device;
1397 BUG_ON(!mutex_is_locked(&uuid_mutex));
1398 if (!fs_devices->seeding)
1401 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1405 old_devices = clone_fs_devices(fs_devices);
1406 if (IS_ERR(old_devices)) {
1407 kfree(seed_devices);
1408 return PTR_ERR(old_devices);
1411 list_add(&old_devices->list, &fs_uuids);
1413 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1414 seed_devices->opened = 1;
1415 INIT_LIST_HEAD(&seed_devices->devices);
1416 INIT_LIST_HEAD(&seed_devices->alloc_list);
1417 mutex_init(&seed_devices->device_list_mutex);
1418 list_splice_init(&fs_devices->devices, &seed_devices->devices);
1419 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1420 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1421 device->fs_devices = seed_devices;
1424 fs_devices->seeding = 0;
1425 fs_devices->num_devices = 0;
1426 fs_devices->open_devices = 0;
1427 fs_devices->seed = seed_devices;
1429 generate_random_uuid(fs_devices->fsid);
1430 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1431 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1432 super_flags = btrfs_super_flags(disk_super) &
1433 ~BTRFS_SUPER_FLAG_SEEDING;
1434 btrfs_set_super_flags(disk_super, super_flags);
1440 * strore the expected generation for seed devices in device items.
1442 static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1443 struct btrfs_root *root)
1445 struct btrfs_path *path;
1446 struct extent_buffer *leaf;
1447 struct btrfs_dev_item *dev_item;
1448 struct btrfs_device *device;
1449 struct btrfs_key key;
1450 u8 fs_uuid[BTRFS_UUID_SIZE];
1451 u8 dev_uuid[BTRFS_UUID_SIZE];
1455 path = btrfs_alloc_path();
1459 root = root->fs_info->chunk_root;
1460 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1462 key.type = BTRFS_DEV_ITEM_KEY;
1465 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1469 leaf = path->nodes[0];
1471 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1472 ret = btrfs_next_leaf(root, path);
1477 leaf = path->nodes[0];
1478 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1479 btrfs_release_path(root, path);
1483 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1484 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1485 key.type != BTRFS_DEV_ITEM_KEY)
1488 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1489 struct btrfs_dev_item);
1490 devid = btrfs_device_id(leaf, dev_item);
1491 read_extent_buffer(leaf, dev_uuid,
1492 (unsigned long)btrfs_device_uuid(dev_item),
1494 read_extent_buffer(leaf, fs_uuid,
1495 (unsigned long)btrfs_device_fsid(dev_item),
1497 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1500 if (device->fs_devices->seeding) {
1501 btrfs_set_device_generation(leaf, dev_item,
1502 device->generation);
1503 btrfs_mark_buffer_dirty(leaf);
1511 btrfs_free_path(path);
1515 int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1517 struct btrfs_trans_handle *trans;
1518 struct btrfs_device *device;
1519 struct block_device *bdev;
1520 struct list_head *devices;
1521 struct super_block *sb = root->fs_info->sb;
1523 int seeding_dev = 0;
1526 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1529 bdev = blkdev_get_by_path(device_path, FMODE_EXCL,
1530 root->fs_info->bdev_holder);
1532 return PTR_ERR(bdev);
1534 if (root->fs_info->fs_devices->seeding) {
1536 down_write(&sb->s_umount);
1537 mutex_lock(&uuid_mutex);
1540 filemap_write_and_wait(bdev->bd_inode->i_mapping);
1541 mutex_lock(&root->fs_info->volume_mutex);
1543 devices = &root->fs_info->fs_devices->devices;
1545 * we have the volume lock, so we don't need the extra
1546 * device list mutex while reading the list here.
1548 list_for_each_entry(device, devices, dev_list) {
1549 if (device->bdev == bdev) {
1555 device = kzalloc(sizeof(*device), GFP_NOFS);
1557 /* we can safely leave the fs_devices entry around */
1562 device->name = kstrdup(device_path, GFP_NOFS);
1563 if (!device->name) {
1569 ret = find_next_devid(root, &device->devid);
1571 kfree(device->name);
1576 trans = btrfs_start_transaction(root, 0);
1577 if (IS_ERR(trans)) {
1578 kfree(device->name);
1580 ret = PTR_ERR(trans);
1586 device->writeable = 1;
1587 device->work.func = pending_bios_fn;
1588 generate_random_uuid(device->uuid);
1589 spin_lock_init(&device->io_lock);
1590 device->generation = trans->transid;
1591 device->io_width = root->sectorsize;
1592 device->io_align = root->sectorsize;
1593 device->sector_size = root->sectorsize;
1594 device->total_bytes = i_size_read(bdev->bd_inode);
1595 device->disk_total_bytes = device->total_bytes;
1596 device->dev_root = root->fs_info->dev_root;
1597 device->bdev = bdev;
1598 device->in_fs_metadata = 1;
1599 device->mode = FMODE_EXCL;
1600 set_blocksize(device->bdev, 4096);
1603 sb->s_flags &= ~MS_RDONLY;
1604 ret = btrfs_prepare_sprout(trans, root);
1608 device->fs_devices = root->fs_info->fs_devices;
1611 * we don't want write_supers to jump in here with our device
1614 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1615 list_add(&device->dev_list, &root->fs_info->fs_devices->devices);
1616 list_add(&device->dev_alloc_list,
1617 &root->fs_info->fs_devices->alloc_list);
1618 root->fs_info->fs_devices->num_devices++;
1619 root->fs_info->fs_devices->open_devices++;
1620 root->fs_info->fs_devices->rw_devices++;
1621 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1623 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1624 root->fs_info->fs_devices->rotating = 1;
1626 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
1627 btrfs_set_super_total_bytes(&root->fs_info->super_copy,
1628 total_bytes + device->total_bytes);
1630 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
1631 btrfs_set_super_num_devices(&root->fs_info->super_copy,
1633 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1636 ret = init_first_rw_device(trans, root, device);
1638 ret = btrfs_finish_sprout(trans, root);
1641 ret = btrfs_add_device(trans, root, device);
1645 * we've got more storage, clear any full flags on the space
1648 btrfs_clear_space_info_full(root->fs_info);
1650 unlock_chunks(root);
1651 btrfs_commit_transaction(trans, root);
1654 mutex_unlock(&uuid_mutex);
1655 up_write(&sb->s_umount);
1657 ret = btrfs_relocate_sys_chunks(root);
1661 mutex_unlock(&root->fs_info->volume_mutex);
1664 blkdev_put(bdev, FMODE_EXCL);
1666 mutex_unlock(&uuid_mutex);
1667 up_write(&sb->s_umount);
1672 static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1673 struct btrfs_device *device)
1676 struct btrfs_path *path;
1677 struct btrfs_root *root;
1678 struct btrfs_dev_item *dev_item;
1679 struct extent_buffer *leaf;
1680 struct btrfs_key key;
1682 root = device->dev_root->fs_info->chunk_root;
1684 path = btrfs_alloc_path();
1688 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1689 key.type = BTRFS_DEV_ITEM_KEY;
1690 key.offset = device->devid;
1692 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1701 leaf = path->nodes[0];
1702 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1704 btrfs_set_device_id(leaf, dev_item, device->devid);
1705 btrfs_set_device_type(leaf, dev_item, device->type);
1706 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1707 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1708 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
1709 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
1710 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1711 btrfs_mark_buffer_dirty(leaf);
1714 btrfs_free_path(path);
1718 static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
1719 struct btrfs_device *device, u64 new_size)
1721 struct btrfs_super_block *super_copy =
1722 &device->dev_root->fs_info->super_copy;
1723 u64 old_total = btrfs_super_total_bytes(super_copy);
1724 u64 diff = new_size - device->total_bytes;
1726 if (!device->writeable)
1728 if (new_size <= device->total_bytes)
1731 btrfs_set_super_total_bytes(super_copy, old_total + diff);
1732 device->fs_devices->total_rw_bytes += diff;
1734 device->total_bytes = new_size;
1735 device->disk_total_bytes = new_size;
1736 btrfs_clear_space_info_full(device->dev_root->fs_info);
1738 return btrfs_update_device(trans, device);
1741 int btrfs_grow_device(struct btrfs_trans_handle *trans,
1742 struct btrfs_device *device, u64 new_size)
1745 lock_chunks(device->dev_root);
1746 ret = __btrfs_grow_device(trans, device, new_size);
1747 unlock_chunks(device->dev_root);
1751 static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1752 struct btrfs_root *root,
1753 u64 chunk_tree, u64 chunk_objectid,
1757 struct btrfs_path *path;
1758 struct btrfs_key key;
1760 root = root->fs_info->chunk_root;
1761 path = btrfs_alloc_path();
1765 key.objectid = chunk_objectid;
1766 key.offset = chunk_offset;
1767 key.type = BTRFS_CHUNK_ITEM_KEY;
1769 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1772 ret = btrfs_del_item(trans, root, path);
1775 btrfs_free_path(path);
1779 static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
1782 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1783 struct btrfs_disk_key *disk_key;
1784 struct btrfs_chunk *chunk;
1791 struct btrfs_key key;
1793 array_size = btrfs_super_sys_array_size(super_copy);
1795 ptr = super_copy->sys_chunk_array;
1798 while (cur < array_size) {
1799 disk_key = (struct btrfs_disk_key *)ptr;
1800 btrfs_disk_key_to_cpu(&key, disk_key);
1802 len = sizeof(*disk_key);
1804 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1805 chunk = (struct btrfs_chunk *)(ptr + len);
1806 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1807 len += btrfs_chunk_item_size(num_stripes);
1812 if (key.objectid == chunk_objectid &&
1813 key.offset == chunk_offset) {
1814 memmove(ptr, ptr + len, array_size - (cur + len));
1816 btrfs_set_super_sys_array_size(super_copy, array_size);
1825 static int btrfs_relocate_chunk(struct btrfs_root *root,
1826 u64 chunk_tree, u64 chunk_objectid,
1829 struct extent_map_tree *em_tree;
1830 struct btrfs_root *extent_root;
1831 struct btrfs_trans_handle *trans;
1832 struct extent_map *em;
1833 struct map_lookup *map;
1837 root = root->fs_info->chunk_root;
1838 extent_root = root->fs_info->extent_root;
1839 em_tree = &root->fs_info->mapping_tree.map_tree;
1841 ret = btrfs_can_relocate(extent_root, chunk_offset);
1845 /* step one, relocate all the extents inside this chunk */
1846 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
1850 trans = btrfs_start_transaction(root, 0);
1851 BUG_ON(IS_ERR(trans));
1856 * step two, delete the device extents and the
1857 * chunk tree entries
1859 read_lock(&em_tree->lock);
1860 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
1861 read_unlock(&em_tree->lock);
1863 BUG_ON(em->start > chunk_offset ||
1864 em->start + em->len < chunk_offset);
1865 map = (struct map_lookup *)em->bdev;
1867 for (i = 0; i < map->num_stripes; i++) {
1868 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1869 map->stripes[i].physical);
1872 if (map->stripes[i].dev) {
1873 ret = btrfs_update_device(trans, map->stripes[i].dev);
1877 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1882 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
1884 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1885 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1889 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
1892 write_lock(&em_tree->lock);
1893 remove_extent_mapping(em_tree, em);
1894 write_unlock(&em_tree->lock);
1899 /* once for the tree */
1900 free_extent_map(em);
1902 free_extent_map(em);
1904 unlock_chunks(root);
1905 btrfs_end_transaction(trans, root);
1909 static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
1911 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1912 struct btrfs_path *path;
1913 struct extent_buffer *leaf;
1914 struct btrfs_chunk *chunk;
1915 struct btrfs_key key;
1916 struct btrfs_key found_key;
1917 u64 chunk_tree = chunk_root->root_key.objectid;
1919 bool retried = false;
1923 path = btrfs_alloc_path();
1928 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1929 key.offset = (u64)-1;
1930 key.type = BTRFS_CHUNK_ITEM_KEY;
1933 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1938 ret = btrfs_previous_item(chunk_root, path, key.objectid,
1945 leaf = path->nodes[0];
1946 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1948 chunk = btrfs_item_ptr(leaf, path->slots[0],
1949 struct btrfs_chunk);
1950 chunk_type = btrfs_chunk_type(leaf, chunk);
1951 btrfs_release_path(chunk_root, path);
1953 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
1954 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
1963 if (found_key.offset == 0)
1965 key.offset = found_key.offset - 1;
1968 if (failed && !retried) {
1972 } else if (failed && retried) {
1977 btrfs_free_path(path);
1981 static u64 div_factor(u64 num, int factor)
1990 int btrfs_balance(struct btrfs_root *dev_root)
1993 struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
1994 struct btrfs_device *device;
1997 struct btrfs_path *path;
1998 struct btrfs_key key;
1999 struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
2000 struct btrfs_trans_handle *trans;
2001 struct btrfs_key found_key;
2003 if (dev_root->fs_info->sb->s_flags & MS_RDONLY)
2006 if (!capable(CAP_SYS_ADMIN))
2009 mutex_lock(&dev_root->fs_info->volume_mutex);
2010 dev_root = dev_root->fs_info->dev_root;
2012 /* step one make some room on all the devices */
2013 list_for_each_entry(device, devices, dev_list) {
2014 old_size = device->total_bytes;
2015 size_to_free = div_factor(old_size, 1);
2016 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
2017 if (!device->writeable ||
2018 device->total_bytes - device->bytes_used > size_to_free)
2021 ret = btrfs_shrink_device(device, old_size - size_to_free);
2026 trans = btrfs_start_transaction(dev_root, 0);
2027 BUG_ON(IS_ERR(trans));
2029 ret = btrfs_grow_device(trans, device, old_size);
2032 btrfs_end_transaction(trans, dev_root);
2035 /* step two, relocate all the chunks */
2036 path = btrfs_alloc_path();
2039 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2040 key.offset = (u64)-1;
2041 key.type = BTRFS_CHUNK_ITEM_KEY;
2044 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2049 * this shouldn't happen, it means the last relocate
2055 ret = btrfs_previous_item(chunk_root, path, 0,
2056 BTRFS_CHUNK_ITEM_KEY);
2060 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2062 if (found_key.objectid != key.objectid)
2065 /* chunk zero is special */
2066 if (found_key.offset == 0)
2069 btrfs_release_path(chunk_root, path);
2070 ret = btrfs_relocate_chunk(chunk_root,
2071 chunk_root->root_key.objectid,
2074 BUG_ON(ret && ret != -ENOSPC);
2075 key.offset = found_key.offset - 1;
2079 btrfs_free_path(path);
2080 mutex_unlock(&dev_root->fs_info->volume_mutex);
2085 * shrinking a device means finding all of the device extents past
2086 * the new size, and then following the back refs to the chunks.
2087 * The chunk relocation code actually frees the device extent
2089 int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
2091 struct btrfs_trans_handle *trans;
2092 struct btrfs_root *root = device->dev_root;
2093 struct btrfs_dev_extent *dev_extent = NULL;
2094 struct btrfs_path *path;
2102 bool retried = false;
2103 struct extent_buffer *l;
2104 struct btrfs_key key;
2105 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2106 u64 old_total = btrfs_super_total_bytes(super_copy);
2107 u64 old_size = device->total_bytes;
2108 u64 diff = device->total_bytes - new_size;
2110 if (new_size >= device->total_bytes)
2113 path = btrfs_alloc_path();
2121 device->total_bytes = new_size;
2122 if (device->writeable)
2123 device->fs_devices->total_rw_bytes -= diff;
2124 unlock_chunks(root);
2127 key.objectid = device->devid;
2128 key.offset = (u64)-1;
2129 key.type = BTRFS_DEV_EXTENT_KEY;
2132 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2136 ret = btrfs_previous_item(root, path, 0, key.type);
2141 btrfs_release_path(root, path);
2146 slot = path->slots[0];
2147 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
2149 if (key.objectid != device->devid) {
2150 btrfs_release_path(root, path);
2154 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2155 length = btrfs_dev_extent_length(l, dev_extent);
2157 if (key.offset + length <= new_size) {
2158 btrfs_release_path(root, path);
2162 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
2163 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
2164 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
2165 btrfs_release_path(root, path);
2167 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
2169 if (ret && ret != -ENOSPC)
2176 if (failed && !retried) {
2180 } else if (failed && retried) {
2184 device->total_bytes = old_size;
2185 if (device->writeable)
2186 device->fs_devices->total_rw_bytes += diff;
2187 unlock_chunks(root);
2191 /* Shrinking succeeded, else we would be at "done". */
2192 trans = btrfs_start_transaction(root, 0);
2193 if (IS_ERR(trans)) {
2194 ret = PTR_ERR(trans);
2200 device->disk_total_bytes = new_size;
2201 /* Now btrfs_update_device() will change the on-disk size. */
2202 ret = btrfs_update_device(trans, device);
2204 unlock_chunks(root);
2205 btrfs_end_transaction(trans, root);
2208 WARN_ON(diff > old_total);
2209 btrfs_set_super_total_bytes(super_copy, old_total - diff);
2210 unlock_chunks(root);
2211 btrfs_end_transaction(trans, root);
2213 btrfs_free_path(path);
2217 static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
2218 struct btrfs_root *root,
2219 struct btrfs_key *key,
2220 struct btrfs_chunk *chunk, int item_size)
2222 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2223 struct btrfs_disk_key disk_key;
2227 array_size = btrfs_super_sys_array_size(super_copy);
2228 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
2231 ptr = super_copy->sys_chunk_array + array_size;
2232 btrfs_cpu_key_to_disk(&disk_key, key);
2233 memcpy(ptr, &disk_key, sizeof(disk_key));
2234 ptr += sizeof(disk_key);
2235 memcpy(ptr, chunk, item_size);
2236 item_size += sizeof(disk_key);
2237 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
2241 static noinline u64 chunk_bytes_by_type(u64 type, u64 calc_size,
2242 int num_stripes, int sub_stripes)
2244 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
2246 else if (type & BTRFS_BLOCK_GROUP_RAID10)
2247 return calc_size * (num_stripes / sub_stripes);
2249 return calc_size * num_stripes;
2252 /* Used to sort the devices by max_avail(descending sort) */
2253 int btrfs_cmp_device_free_bytes(const void *dev_info1, const void *dev_info2)
2255 if (((struct btrfs_device_info *)dev_info1)->max_avail >
2256 ((struct btrfs_device_info *)dev_info2)->max_avail)
2258 else if (((struct btrfs_device_info *)dev_info1)->max_avail <
2259 ((struct btrfs_device_info *)dev_info2)->max_avail)
2265 static int __btrfs_calc_nstripes(struct btrfs_fs_devices *fs_devices, u64 type,
2266 int *num_stripes, int *min_stripes,
2273 if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
2274 *num_stripes = fs_devices->rw_devices;
2277 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
2281 if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
2282 if (fs_devices->rw_devices < 2)
2287 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2288 *num_stripes = fs_devices->rw_devices;
2289 if (*num_stripes < 4)
2291 *num_stripes &= ~(u32)1;
2299 static u64 __btrfs_calc_stripe_size(struct btrfs_fs_devices *fs_devices,
2300 u64 proposed_size, u64 type,
2301 int num_stripes, int small_stripe)
2303 int min_stripe_size = 1 * 1024 * 1024;
2304 u64 calc_size = proposed_size;
2305 u64 max_chunk_size = calc_size;
2308 if (type & (BTRFS_BLOCK_GROUP_RAID1 |
2309 BTRFS_BLOCK_GROUP_DUP |
2310 BTRFS_BLOCK_GROUP_RAID10))
2313 if (type & BTRFS_BLOCK_GROUP_DATA) {
2314 max_chunk_size = 10 * calc_size;
2315 min_stripe_size = 64 * 1024 * 1024;
2316 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
2317 max_chunk_size = 256 * 1024 * 1024;
2318 min_stripe_size = 32 * 1024 * 1024;
2319 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
2320 calc_size = 8 * 1024 * 1024;
2321 max_chunk_size = calc_size * 2;
2322 min_stripe_size = 1 * 1024 * 1024;
2325 /* we don't want a chunk larger than 10% of writeable space */
2326 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
2329 if (calc_size * num_stripes > max_chunk_size * ncopies) {
2330 calc_size = max_chunk_size * ncopies;
2331 do_div(calc_size, num_stripes);
2332 do_div(calc_size, BTRFS_STRIPE_LEN);
2333 calc_size *= BTRFS_STRIPE_LEN;
2336 /* we don't want tiny stripes */
2338 calc_size = max_t(u64, min_stripe_size, calc_size);
2341 * we're about to do_div by the BTRFS_STRIPE_LEN so lets make sure
2342 * we end up with something bigger than a stripe
2344 calc_size = max_t(u64, calc_size, BTRFS_STRIPE_LEN);
2346 do_div(calc_size, BTRFS_STRIPE_LEN);
2347 calc_size *= BTRFS_STRIPE_LEN;
2352 static struct map_lookup *__shrink_map_lookup_stripes(struct map_lookup *map,
2355 struct map_lookup *new;
2356 size_t len = map_lookup_size(num_stripes);
2358 BUG_ON(map->num_stripes < num_stripes);
2360 if (map->num_stripes == num_stripes)
2363 new = kmalloc(len, GFP_NOFS);
2365 /* just change map->num_stripes */
2366 map->num_stripes = num_stripes;
2370 memcpy(new, map, len);
2371 new->num_stripes = num_stripes;
2377 * helper to allocate device space from btrfs_device_info, in which we stored
2378 * max free space information of every device. It is used when we can not
2379 * allocate chunks by default size.
2381 * By this helper, we can allocate a new chunk as larger as possible.
2383 static int __btrfs_alloc_tiny_space(struct btrfs_trans_handle *trans,
2384 struct btrfs_fs_devices *fs_devices,
2385 struct btrfs_device_info *devices,
2386 int nr_device, u64 type,
2387 struct map_lookup **map_lookup,
2388 int min_stripes, u64 *stripe_size)
2390 int i, index, sort_again = 0;
2391 int min_devices = min_stripes;
2392 u64 max_avail, min_free;
2393 struct map_lookup *map = *map_lookup;
2396 if (nr_device < min_stripes)
2399 btrfs_descending_sort_devices(devices, nr_device);
2401 max_avail = devices[0].max_avail;
2405 for (i = 0; i < nr_device; i++) {
2407 * if dev_offset = 0, it means the free space of this device
2408 * is less than what we need, and we didn't search max avail
2409 * extent on this device, so do it now.
2411 if (!devices[i].dev_offset) {
2412 ret = find_free_dev_extent(trans, devices[i].dev,
2414 &devices[i].dev_offset,
2415 &devices[i].max_avail);
2416 if (ret != 0 && ret != -ENOSPC)
2422 /* we update the max avail free extent of each devices, sort again */
2424 btrfs_descending_sort_devices(devices, nr_device);
2426 if (type & BTRFS_BLOCK_GROUP_DUP)
2429 if (!devices[min_devices - 1].max_avail)
2432 max_avail = devices[min_devices - 1].max_avail;
2433 if (type & BTRFS_BLOCK_GROUP_DUP)
2434 do_div(max_avail, 2);
2436 max_avail = __btrfs_calc_stripe_size(fs_devices, max_avail, type,
2438 if (type & BTRFS_BLOCK_GROUP_DUP)
2439 min_free = max_avail * 2;
2441 min_free = max_avail;
2443 if (min_free > devices[min_devices - 1].max_avail)
2446 map = __shrink_map_lookup_stripes(map, min_stripes);
2447 *stripe_size = max_avail;
2450 for (i = 0; i < min_stripes; i++) {
2451 map->stripes[i].dev = devices[index].dev;
2452 map->stripes[i].physical = devices[index].dev_offset;
2453 if (type & BTRFS_BLOCK_GROUP_DUP) {
2455 map->stripes[i].dev = devices[index].dev;
2456 map->stripes[i].physical = devices[index].dev_offset +
2466 static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2467 struct btrfs_root *extent_root,
2468 struct map_lookup **map_ret,
2469 u64 *num_bytes, u64 *stripe_size,
2470 u64 start, u64 type)
2472 struct btrfs_fs_info *info = extent_root->fs_info;
2473 struct btrfs_device *device = NULL;
2474 struct btrfs_fs_devices *fs_devices = info->fs_devices;
2475 struct list_head *cur;
2476 struct map_lookup *map;
2477 struct extent_map_tree *em_tree;
2478 struct extent_map *em;
2479 struct btrfs_device_info *devices_info;
2480 struct list_head private_devs;
2481 u64 calc_size = 1024 * 1024 * 1024;
2488 int min_devices; /* the min number of devices we need */
2493 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
2494 (type & BTRFS_BLOCK_GROUP_DUP)) {
2496 type &= ~BTRFS_BLOCK_GROUP_DUP;
2498 if (list_empty(&fs_devices->alloc_list))
2501 ret = __btrfs_calc_nstripes(fs_devices, type, &num_stripes,
2502 &min_stripes, &sub_stripes);
2506 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
2511 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
2516 map->num_stripes = num_stripes;
2518 cur = fs_devices->alloc_list.next;
2522 calc_size = __btrfs_calc_stripe_size(fs_devices, calc_size, type,
2525 if (type & BTRFS_BLOCK_GROUP_DUP) {
2526 min_free = calc_size * 2;
2529 min_free = calc_size;
2530 min_devices = min_stripes;
2533 INIT_LIST_HEAD(&private_devs);
2534 while (index < num_stripes) {
2535 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
2536 BUG_ON(!device->writeable);
2537 if (device->total_bytes > device->bytes_used)
2538 avail = device->total_bytes - device->bytes_used;
2543 if (device->in_fs_metadata && avail >= min_free) {
2544 ret = find_free_dev_extent(trans, device, min_free,
2545 &devices_info[i].dev_offset,
2546 &devices_info[i].max_avail);
2548 list_move_tail(&device->dev_alloc_list,
2550 map->stripes[index].dev = device;
2551 map->stripes[index].physical =
2552 devices_info[i].dev_offset;
2554 if (type & BTRFS_BLOCK_GROUP_DUP) {
2555 map->stripes[index].dev = device;
2556 map->stripes[index].physical =
2557 devices_info[i].dev_offset +
2561 } else if (ret != -ENOSPC)
2564 devices_info[i].dev = device;
2566 } else if (device->in_fs_metadata &&
2567 avail >= BTRFS_STRIPE_LEN) {
2568 devices_info[i].dev = device;
2569 devices_info[i].max_avail = avail;
2573 if (cur == &fs_devices->alloc_list)
2577 list_splice(&private_devs, &fs_devices->alloc_list);
2578 if (index < num_stripes) {
2579 if (index >= min_stripes) {
2580 num_stripes = index;
2581 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2582 num_stripes /= sub_stripes;
2583 num_stripes *= sub_stripes;
2586 map = __shrink_map_lookup_stripes(map, num_stripes);
2587 } else if (i >= min_devices) {
2588 ret = __btrfs_alloc_tiny_space(trans, fs_devices,
2589 devices_info, i, type,
2599 map->sector_size = extent_root->sectorsize;
2600 map->stripe_len = BTRFS_STRIPE_LEN;
2601 map->io_align = BTRFS_STRIPE_LEN;
2602 map->io_width = BTRFS_STRIPE_LEN;
2604 map->sub_stripes = sub_stripes;
2607 *stripe_size = calc_size;
2608 *num_bytes = chunk_bytes_by_type(type, calc_size,
2609 map->num_stripes, sub_stripes);
2611 trace_btrfs_chunk_alloc(info->chunk_root, map, start, *num_bytes);
2613 em = alloc_extent_map(GFP_NOFS);
2618 em->bdev = (struct block_device *)map;
2620 em->len = *num_bytes;
2621 em->block_start = 0;
2622 em->block_len = em->len;
2624 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
2625 write_lock(&em_tree->lock);
2626 ret = add_extent_mapping(em_tree, em);
2627 write_unlock(&em_tree->lock);
2629 free_extent_map(em);
2631 ret = btrfs_make_block_group(trans, extent_root, 0, type,
2632 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2637 while (index < map->num_stripes) {
2638 device = map->stripes[index].dev;
2639 dev_offset = map->stripes[index].physical;
2641 ret = btrfs_alloc_dev_extent(trans, device,
2642 info->chunk_root->root_key.objectid,
2643 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2644 start, dev_offset, calc_size);
2649 kfree(devices_info);
2654 kfree(devices_info);
2658 static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
2659 struct btrfs_root *extent_root,
2660 struct map_lookup *map, u64 chunk_offset,
2661 u64 chunk_size, u64 stripe_size)
2664 struct btrfs_key key;
2665 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2666 struct btrfs_device *device;
2667 struct btrfs_chunk *chunk;
2668 struct btrfs_stripe *stripe;
2669 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
2673 chunk = kzalloc(item_size, GFP_NOFS);
2678 while (index < map->num_stripes) {
2679 device = map->stripes[index].dev;
2680 device->bytes_used += stripe_size;
2681 ret = btrfs_update_device(trans, device);
2687 stripe = &chunk->stripe;
2688 while (index < map->num_stripes) {
2689 device = map->stripes[index].dev;
2690 dev_offset = map->stripes[index].physical;
2692 btrfs_set_stack_stripe_devid(stripe, device->devid);
2693 btrfs_set_stack_stripe_offset(stripe, dev_offset);
2694 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
2699 btrfs_set_stack_chunk_length(chunk, chunk_size);
2700 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
2701 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
2702 btrfs_set_stack_chunk_type(chunk, map->type);
2703 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
2704 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
2705 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
2706 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
2707 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
2709 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2710 key.type = BTRFS_CHUNK_ITEM_KEY;
2711 key.offset = chunk_offset;
2713 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
2716 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2717 ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
2727 * Chunk allocation falls into two parts. The first part does works
2728 * that make the new allocated chunk useable, but not do any operation
2729 * that modifies the chunk tree. The second part does the works that
2730 * require modifying the chunk tree. This division is important for the
2731 * bootstrap process of adding storage to a seed btrfs.
2733 int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2734 struct btrfs_root *extent_root, u64 type)
2739 struct map_lookup *map;
2740 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2743 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2748 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2749 &stripe_size, chunk_offset, type);
2753 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2754 chunk_size, stripe_size);
2759 static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
2760 struct btrfs_root *root,
2761 struct btrfs_device *device)
2764 u64 sys_chunk_offset;
2768 u64 sys_stripe_size;
2770 struct map_lookup *map;
2771 struct map_lookup *sys_map;
2772 struct btrfs_fs_info *fs_info = root->fs_info;
2773 struct btrfs_root *extent_root = fs_info->extent_root;
2776 ret = find_next_chunk(fs_info->chunk_root,
2777 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
2780 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
2781 (fs_info->metadata_alloc_profile &
2782 fs_info->avail_metadata_alloc_bits);
2783 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2785 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2786 &stripe_size, chunk_offset, alloc_profile);
2789 sys_chunk_offset = chunk_offset + chunk_size;
2791 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
2792 (fs_info->system_alloc_profile &
2793 fs_info->avail_system_alloc_bits);
2794 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2796 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
2797 &sys_chunk_size, &sys_stripe_size,
2798 sys_chunk_offset, alloc_profile);
2801 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
2805 * Modifying chunk tree needs allocating new blocks from both
2806 * system block group and metadata block group. So we only can
2807 * do operations require modifying the chunk tree after both
2808 * block groups were created.
2810 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2811 chunk_size, stripe_size);
2814 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
2815 sys_chunk_offset, sys_chunk_size,
2821 int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
2823 struct extent_map *em;
2824 struct map_lookup *map;
2825 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2829 read_lock(&map_tree->map_tree.lock);
2830 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
2831 read_unlock(&map_tree->map_tree.lock);
2835 if (btrfs_test_opt(root, DEGRADED)) {
2836 free_extent_map(em);
2840 map = (struct map_lookup *)em->bdev;
2841 for (i = 0; i < map->num_stripes; i++) {
2842 if (!map->stripes[i].dev->writeable) {
2847 free_extent_map(em);
2851 void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
2853 extent_map_tree_init(&tree->map_tree, GFP_NOFS);
2856 void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
2858 struct extent_map *em;
2861 write_lock(&tree->map_tree.lock);
2862 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
2864 remove_extent_mapping(&tree->map_tree, em);
2865 write_unlock(&tree->map_tree.lock);
2870 free_extent_map(em);
2871 /* once for the tree */
2872 free_extent_map(em);
2876 int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
2878 struct extent_map *em;
2879 struct map_lookup *map;
2880 struct extent_map_tree *em_tree = &map_tree->map_tree;
2883 read_lock(&em_tree->lock);
2884 em = lookup_extent_mapping(em_tree, logical, len);
2885 read_unlock(&em_tree->lock);
2888 BUG_ON(em->start > logical || em->start + em->len < logical);
2889 map = (struct map_lookup *)em->bdev;
2890 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
2891 ret = map->num_stripes;
2892 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2893 ret = map->sub_stripes;
2896 free_extent_map(em);
2900 static int find_live_mirror(struct map_lookup *map, int first, int num,
2904 if (map->stripes[optimal].dev->bdev)
2906 for (i = first; i < first + num; i++) {
2907 if (map->stripes[i].dev->bdev)
2910 /* we couldn't find one that doesn't fail. Just return something
2911 * and the io error handling code will clean up eventually
2916 static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2917 u64 logical, u64 *length,
2918 struct btrfs_multi_bio **multi_ret,
2921 struct extent_map *em;
2922 struct map_lookup *map;
2923 struct extent_map_tree *em_tree = &map_tree->map_tree;
2926 u64 stripe_end_offset;
2930 int stripes_allocated = 8;
2931 int stripes_required = 1;
2936 struct btrfs_multi_bio *multi = NULL;
2938 if (multi_ret && !(rw & (REQ_WRITE | REQ_DISCARD)))
2939 stripes_allocated = 1;
2942 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
2947 atomic_set(&multi->error, 0);
2950 read_lock(&em_tree->lock);
2951 em = lookup_extent_mapping(em_tree, logical, *length);
2952 read_unlock(&em_tree->lock);
2955 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
2956 (unsigned long long)logical,
2957 (unsigned long long)*length);
2961 BUG_ON(em->start > logical || em->start + em->len < logical);
2962 map = (struct map_lookup *)em->bdev;
2963 offset = logical - em->start;
2965 if (mirror_num > map->num_stripes)
2968 /* if our multi bio struct is too small, back off and try again */
2969 if (rw & REQ_WRITE) {
2970 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
2971 BTRFS_BLOCK_GROUP_DUP)) {
2972 stripes_required = map->num_stripes;
2974 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2975 stripes_required = map->sub_stripes;
2979 if (rw & REQ_DISCARD) {
2980 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
2981 BTRFS_BLOCK_GROUP_RAID1 |
2982 BTRFS_BLOCK_GROUP_DUP |
2983 BTRFS_BLOCK_GROUP_RAID10)) {
2984 stripes_required = map->num_stripes;
2987 if (multi_ret && (rw & (REQ_WRITE | REQ_DISCARD)) &&
2988 stripes_allocated < stripes_required) {
2989 stripes_allocated = map->num_stripes;
2990 free_extent_map(em);
2996 * stripe_nr counts the total number of stripes we have to stride
2997 * to get to this block
2999 do_div(stripe_nr, map->stripe_len);
3001 stripe_offset = stripe_nr * map->stripe_len;
3002 BUG_ON(offset < stripe_offset);
3004 /* stripe_offset is the offset of this block in its stripe*/
3005 stripe_offset = offset - stripe_offset;
3007 if (rw & REQ_DISCARD)
3008 *length = min_t(u64, em->len - offset, *length);
3009 else if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
3010 BTRFS_BLOCK_GROUP_RAID1 |
3011 BTRFS_BLOCK_GROUP_RAID10 |
3012 BTRFS_BLOCK_GROUP_DUP)) {
3013 /* we limit the length of each bio to what fits in a stripe */
3014 *length = min_t(u64, em->len - offset,
3015 map->stripe_len - stripe_offset);
3017 *length = em->len - offset;
3025 stripe_nr_orig = stripe_nr;
3026 stripe_nr_end = (offset + *length + map->stripe_len - 1) &
3027 (~(map->stripe_len - 1));
3028 do_div(stripe_nr_end, map->stripe_len);
3029 stripe_end_offset = stripe_nr_end * map->stripe_len -
3031 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3032 if (rw & REQ_DISCARD)
3033 num_stripes = min_t(u64, map->num_stripes,
3034 stripe_nr_end - stripe_nr_orig);
3035 stripe_index = do_div(stripe_nr, map->num_stripes);
3036 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
3037 if (rw & (REQ_WRITE | REQ_DISCARD))
3038 num_stripes = map->num_stripes;
3039 else if (mirror_num)
3040 stripe_index = mirror_num - 1;
3042 stripe_index = find_live_mirror(map, 0,
3044 current->pid % map->num_stripes);
3047 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
3048 if (rw & (REQ_WRITE | REQ_DISCARD))
3049 num_stripes = map->num_stripes;
3050 else if (mirror_num)
3051 stripe_index = mirror_num - 1;
3053 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3054 int factor = map->num_stripes / map->sub_stripes;
3056 stripe_index = do_div(stripe_nr, factor);
3057 stripe_index *= map->sub_stripes;
3060 num_stripes = map->sub_stripes;
3061 else if (rw & REQ_DISCARD)
3062 num_stripes = min_t(u64, map->sub_stripes *
3063 (stripe_nr_end - stripe_nr_orig),
3065 else if (mirror_num)
3066 stripe_index += mirror_num - 1;
3068 stripe_index = find_live_mirror(map, stripe_index,
3069 map->sub_stripes, stripe_index +
3070 current->pid % map->sub_stripes);
3074 * after this do_div call, stripe_nr is the number of stripes
3075 * on this device we have to walk to find the data, and
3076 * stripe_index is the number of our device in the stripe array
3078 stripe_index = do_div(stripe_nr, map->num_stripes);
3080 BUG_ON(stripe_index >= map->num_stripes);
3082 if (rw & REQ_DISCARD) {
3083 for (i = 0; i < num_stripes; i++) {
3084 multi->stripes[i].physical =
3085 map->stripes[stripe_index].physical +
3086 stripe_offset + stripe_nr * map->stripe_len;
3087 multi->stripes[i].dev = map->stripes[stripe_index].dev;
3089 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3091 u32 last_stripe = 0;
3094 div_u64_rem(stripe_nr_end - 1,
3098 for (j = 0; j < map->num_stripes; j++) {
3101 div_u64_rem(stripe_nr_end - 1 - j,
3102 map->num_stripes, &test);
3103 if (test == stripe_index)
3106 stripes = stripe_nr_end - 1 - j;
3107 do_div(stripes, map->num_stripes);
3108 multi->stripes[i].length = map->stripe_len *
3109 (stripes - stripe_nr + 1);
3112 multi->stripes[i].length -=
3116 if (stripe_index == last_stripe)
3117 multi->stripes[i].length -=
3119 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3122 int factor = map->num_stripes /
3124 u32 last_stripe = 0;
3126 div_u64_rem(stripe_nr_end - 1,
3127 factor, &last_stripe);
3128 last_stripe *= map->sub_stripes;
3130 for (j = 0; j < factor; j++) {
3133 div_u64_rem(stripe_nr_end - 1 - j,
3137 stripe_index / map->sub_stripes)
3140 stripes = stripe_nr_end - 1 - j;
3141 do_div(stripes, factor);
3142 multi->stripes[i].length = map->stripe_len *
3143 (stripes - stripe_nr + 1);
3145 if (i < map->sub_stripes) {
3146 multi->stripes[i].length -=
3148 if (i == map->sub_stripes - 1)
3151 if (stripe_index >= last_stripe &&
3152 stripe_index <= (last_stripe +
3153 map->sub_stripes - 1)) {
3154 multi->stripes[i].length -=
3158 multi->stripes[i].length = *length;
3161 if (stripe_index == map->num_stripes) {
3162 /* This could only happen for RAID0/10 */
3168 for (i = 0; i < num_stripes; i++) {
3169 multi->stripes[i].physical =
3170 map->stripes[stripe_index].physical +
3172 stripe_nr * map->stripe_len;
3173 multi->stripes[i].dev =
3174 map->stripes[stripe_index].dev;
3180 multi->num_stripes = num_stripes;
3181 multi->max_errors = max_errors;
3184 free_extent_map(em);
3188 int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3189 u64 logical, u64 *length,
3190 struct btrfs_multi_bio **multi_ret, int mirror_num)
3192 return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
3196 int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
3197 u64 chunk_start, u64 physical, u64 devid,
3198 u64 **logical, int *naddrs, int *stripe_len)
3200 struct extent_map_tree *em_tree = &map_tree->map_tree;
3201 struct extent_map *em;
3202 struct map_lookup *map;
3209 read_lock(&em_tree->lock);
3210 em = lookup_extent_mapping(em_tree, chunk_start, 1);
3211 read_unlock(&em_tree->lock);
3213 BUG_ON(!em || em->start != chunk_start);
3214 map = (struct map_lookup *)em->bdev;
3217 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3218 do_div(length, map->num_stripes / map->sub_stripes);
3219 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3220 do_div(length, map->num_stripes);
3222 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
3225 for (i = 0; i < map->num_stripes; i++) {
3226 if (devid && map->stripes[i].dev->devid != devid)
3228 if (map->stripes[i].physical > physical ||
3229 map->stripes[i].physical + length <= physical)
3232 stripe_nr = physical - map->stripes[i].physical;
3233 do_div(stripe_nr, map->stripe_len);
3235 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3236 stripe_nr = stripe_nr * map->num_stripes + i;
3237 do_div(stripe_nr, map->sub_stripes);
3238 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3239 stripe_nr = stripe_nr * map->num_stripes + i;
3241 bytenr = chunk_start + stripe_nr * map->stripe_len;
3242 WARN_ON(nr >= map->num_stripes);
3243 for (j = 0; j < nr; j++) {
3244 if (buf[j] == bytenr)
3248 WARN_ON(nr >= map->num_stripes);
3255 *stripe_len = map->stripe_len;
3257 free_extent_map(em);
3261 static void end_bio_multi_stripe(struct bio *bio, int err)
3263 struct btrfs_multi_bio *multi = bio->bi_private;
3264 int is_orig_bio = 0;
3267 atomic_inc(&multi->error);
3269 if (bio == multi->orig_bio)
3272 if (atomic_dec_and_test(&multi->stripes_pending)) {
3275 bio = multi->orig_bio;
3277 bio->bi_private = multi->private;
3278 bio->bi_end_io = multi->end_io;
3279 /* only send an error to the higher layers if it is
3280 * beyond the tolerance of the multi-bio
3282 if (atomic_read(&multi->error) > multi->max_errors) {
3286 * this bio is actually up to date, we didn't
3287 * go over the max number of errors
3289 set_bit(BIO_UPTODATE, &bio->bi_flags);
3294 bio_endio(bio, err);
3295 } else if (!is_orig_bio) {
3300 struct async_sched {
3303 struct btrfs_fs_info *info;
3304 struct btrfs_work work;
3308 * see run_scheduled_bios for a description of why bios are collected for
3311 * This will add one bio to the pending list for a device and make sure
3312 * the work struct is scheduled.
3314 static noinline int schedule_bio(struct btrfs_root *root,
3315 struct btrfs_device *device,
3316 int rw, struct bio *bio)
3318 int should_queue = 1;
3319 struct btrfs_pending_bios *pending_bios;
3321 /* don't bother with additional async steps for reads, right now */
3322 if (!(rw & REQ_WRITE)) {
3324 submit_bio(rw, bio);
3330 * nr_async_bios allows us to reliably return congestion to the
3331 * higher layers. Otherwise, the async bio makes it appear we have
3332 * made progress against dirty pages when we've really just put it
3333 * on a queue for later
3335 atomic_inc(&root->fs_info->nr_async_bios);
3336 WARN_ON(bio->bi_next);
3337 bio->bi_next = NULL;
3340 spin_lock(&device->io_lock);
3341 if (bio->bi_rw & REQ_SYNC)
3342 pending_bios = &device->pending_sync_bios;
3344 pending_bios = &device->pending_bios;
3346 if (pending_bios->tail)
3347 pending_bios->tail->bi_next = bio;
3349 pending_bios->tail = bio;
3350 if (!pending_bios->head)
3351 pending_bios->head = bio;
3352 if (device->running_pending)
3355 spin_unlock(&device->io_lock);
3358 btrfs_queue_worker(&root->fs_info->submit_workers,
3363 int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
3364 int mirror_num, int async_submit)
3366 struct btrfs_mapping_tree *map_tree;
3367 struct btrfs_device *dev;
3368 struct bio *first_bio = bio;
3369 u64 logical = (u64)bio->bi_sector << 9;
3372 struct btrfs_multi_bio *multi = NULL;
3377 length = bio->bi_size;
3378 map_tree = &root->fs_info->mapping_tree;
3379 map_length = length;
3381 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
3385 total_devs = multi->num_stripes;
3386 if (map_length < length) {
3387 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
3388 "len %llu\n", (unsigned long long)logical,
3389 (unsigned long long)length,
3390 (unsigned long long)map_length);
3393 multi->end_io = first_bio->bi_end_io;
3394 multi->private = first_bio->bi_private;
3395 multi->orig_bio = first_bio;
3396 atomic_set(&multi->stripes_pending, multi->num_stripes);
3398 while (dev_nr < total_devs) {
3399 if (total_devs > 1) {
3400 if (dev_nr < total_devs - 1) {
3401 bio = bio_clone(first_bio, GFP_NOFS);
3406 bio->bi_private = multi;
3407 bio->bi_end_io = end_bio_multi_stripe;
3409 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
3410 dev = multi->stripes[dev_nr].dev;
3411 if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
3412 bio->bi_bdev = dev->bdev;
3414 schedule_bio(root, dev, rw, bio);
3416 submit_bio(rw, bio);
3418 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
3419 bio->bi_sector = logical >> 9;
3420 bio_endio(bio, -EIO);
3424 if (total_devs == 1)
3429 struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
3432 struct btrfs_device *device;
3433 struct btrfs_fs_devices *cur_devices;
3435 cur_devices = root->fs_info->fs_devices;
3436 while (cur_devices) {
3438 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3439 device = __find_device(&cur_devices->devices,
3444 cur_devices = cur_devices->seed;
3449 static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
3450 u64 devid, u8 *dev_uuid)
3452 struct btrfs_device *device;
3453 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
3455 device = kzalloc(sizeof(*device), GFP_NOFS);
3458 list_add(&device->dev_list,
3459 &fs_devices->devices);
3460 device->dev_root = root->fs_info->dev_root;
3461 device->devid = devid;
3462 device->work.func = pending_bios_fn;
3463 device->fs_devices = fs_devices;
3464 device->missing = 1;
3465 fs_devices->num_devices++;
3466 fs_devices->missing_devices++;
3467 spin_lock_init(&device->io_lock);
3468 INIT_LIST_HEAD(&device->dev_alloc_list);
3469 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
3473 static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
3474 struct extent_buffer *leaf,
3475 struct btrfs_chunk *chunk)
3477 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3478 struct map_lookup *map;
3479 struct extent_map *em;
3483 u8 uuid[BTRFS_UUID_SIZE];
3488 logical = key->offset;
3489 length = btrfs_chunk_length(leaf, chunk);
3491 read_lock(&map_tree->map_tree.lock);
3492 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
3493 read_unlock(&map_tree->map_tree.lock);
3495 /* already mapped? */
3496 if (em && em->start <= logical && em->start + em->len > logical) {
3497 free_extent_map(em);
3500 free_extent_map(em);
3503 em = alloc_extent_map(GFP_NOFS);
3506 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3507 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
3509 free_extent_map(em);
3513 em->bdev = (struct block_device *)map;
3514 em->start = logical;
3516 em->block_start = 0;
3517 em->block_len = em->len;
3519 map->num_stripes = num_stripes;
3520 map->io_width = btrfs_chunk_io_width(leaf, chunk);
3521 map->io_align = btrfs_chunk_io_align(leaf, chunk);
3522 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
3523 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
3524 map->type = btrfs_chunk_type(leaf, chunk);
3525 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
3526 for (i = 0; i < num_stripes; i++) {
3527 map->stripes[i].physical =
3528 btrfs_stripe_offset_nr(leaf, chunk, i);
3529 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
3530 read_extent_buffer(leaf, uuid, (unsigned long)
3531 btrfs_stripe_dev_uuid_nr(chunk, i),
3533 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
3535 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
3537 free_extent_map(em);
3540 if (!map->stripes[i].dev) {
3541 map->stripes[i].dev =
3542 add_missing_dev(root, devid, uuid);
3543 if (!map->stripes[i].dev) {
3545 free_extent_map(em);
3549 map->stripes[i].dev->in_fs_metadata = 1;
3552 write_lock(&map_tree->map_tree.lock);
3553 ret = add_extent_mapping(&map_tree->map_tree, em);
3554 write_unlock(&map_tree->map_tree.lock);
3556 free_extent_map(em);
3561 static int fill_device_from_item(struct extent_buffer *leaf,
3562 struct btrfs_dev_item *dev_item,
3563 struct btrfs_device *device)
3567 device->devid = btrfs_device_id(leaf, dev_item);
3568 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
3569 device->total_bytes = device->disk_total_bytes;
3570 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
3571 device->type = btrfs_device_type(leaf, dev_item);
3572 device->io_align = btrfs_device_io_align(leaf, dev_item);
3573 device->io_width = btrfs_device_io_width(leaf, dev_item);
3574 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
3576 ptr = (unsigned long)btrfs_device_uuid(dev_item);
3577 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
3582 static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
3584 struct btrfs_fs_devices *fs_devices;
3587 mutex_lock(&uuid_mutex);
3589 fs_devices = root->fs_info->fs_devices->seed;
3590 while (fs_devices) {
3591 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3595 fs_devices = fs_devices->seed;
3598 fs_devices = find_fsid(fsid);
3604 fs_devices = clone_fs_devices(fs_devices);
3605 if (IS_ERR(fs_devices)) {
3606 ret = PTR_ERR(fs_devices);
3610 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
3611 root->fs_info->bdev_holder);
3615 if (!fs_devices->seeding) {
3616 __btrfs_close_devices(fs_devices);
3617 free_fs_devices(fs_devices);
3622 fs_devices->seed = root->fs_info->fs_devices->seed;
3623 root->fs_info->fs_devices->seed = fs_devices;
3625 mutex_unlock(&uuid_mutex);
3629 static int read_one_dev(struct btrfs_root *root,
3630 struct extent_buffer *leaf,
3631 struct btrfs_dev_item *dev_item)
3633 struct btrfs_device *device;
3636 u8 fs_uuid[BTRFS_UUID_SIZE];
3637 u8 dev_uuid[BTRFS_UUID_SIZE];
3639 devid = btrfs_device_id(leaf, dev_item);
3640 read_extent_buffer(leaf, dev_uuid,
3641 (unsigned long)btrfs_device_uuid(dev_item),
3643 read_extent_buffer(leaf, fs_uuid,
3644 (unsigned long)btrfs_device_fsid(dev_item),
3647 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
3648 ret = open_seed_devices(root, fs_uuid);
3649 if (ret && !btrfs_test_opt(root, DEGRADED))
3653 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
3654 if (!device || !device->bdev) {
3655 if (!btrfs_test_opt(root, DEGRADED))
3659 printk(KERN_WARNING "warning devid %llu missing\n",
3660 (unsigned long long)devid);
3661 device = add_missing_dev(root, devid, dev_uuid);
3664 } else if (!device->missing) {
3666 * this happens when a device that was properly setup
3667 * in the device info lists suddenly goes bad.
3668 * device->bdev is NULL, and so we have to set
3669 * device->missing to one here
3671 root->fs_info->fs_devices->missing_devices++;
3672 device->missing = 1;
3676 if (device->fs_devices != root->fs_info->fs_devices) {
3677 BUG_ON(device->writeable);
3678 if (device->generation !=
3679 btrfs_device_generation(leaf, dev_item))
3683 fill_device_from_item(leaf, dev_item, device);
3684 device->dev_root = root->fs_info->dev_root;
3685 device->in_fs_metadata = 1;
3686 if (device->writeable)
3687 device->fs_devices->total_rw_bytes += device->total_bytes;
3692 int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
3694 struct btrfs_dev_item *dev_item;
3696 dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
3698 return read_one_dev(root, buf, dev_item);
3701 int btrfs_read_sys_array(struct btrfs_root *root)
3703 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
3704 struct extent_buffer *sb;
3705 struct btrfs_disk_key *disk_key;
3706 struct btrfs_chunk *chunk;
3708 unsigned long sb_ptr;
3714 struct btrfs_key key;
3716 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
3717 BTRFS_SUPER_INFO_SIZE);
3720 btrfs_set_buffer_uptodate(sb);
3721 btrfs_set_buffer_lockdep_class(sb, 0);
3723 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
3724 array_size = btrfs_super_sys_array_size(super_copy);
3726 ptr = super_copy->sys_chunk_array;
3727 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
3730 while (cur < array_size) {
3731 disk_key = (struct btrfs_disk_key *)ptr;
3732 btrfs_disk_key_to_cpu(&key, disk_key);
3734 len = sizeof(*disk_key); ptr += len;
3738 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
3739 chunk = (struct btrfs_chunk *)sb_ptr;
3740 ret = read_one_chunk(root, &key, sb, chunk);
3743 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
3744 len = btrfs_chunk_item_size(num_stripes);
3753 free_extent_buffer(sb);
3757 int btrfs_read_chunk_tree(struct btrfs_root *root)
3759 struct btrfs_path *path;
3760 struct extent_buffer *leaf;
3761 struct btrfs_key key;
3762 struct btrfs_key found_key;
3766 root = root->fs_info->chunk_root;
3768 path = btrfs_alloc_path();
3772 /* first we search for all of the device items, and then we
3773 * read in all of the chunk items. This way we can create chunk
3774 * mappings that reference all of the devices that are afound
3776 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
3780 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3784 leaf = path->nodes[0];
3785 slot = path->slots[0];
3786 if (slot >= btrfs_header_nritems(leaf)) {
3787 ret = btrfs_next_leaf(root, path);
3794 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3795 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3796 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
3798 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
3799 struct btrfs_dev_item *dev_item;
3800 dev_item = btrfs_item_ptr(leaf, slot,
3801 struct btrfs_dev_item);
3802 ret = read_one_dev(root, leaf, dev_item);
3806 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
3807 struct btrfs_chunk *chunk;
3808 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3809 ret = read_one_chunk(root, &found_key, leaf, chunk);
3815 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3817 btrfs_release_path(root, path);
3822 btrfs_free_path(path);