2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
18 #include <linux/sched.h>
19 #include <linux/bio.h>
20 #include <linux/buffer_head.h>
21 #include <linux/blkdev.h>
22 #include <linux/random.h>
23 #include <linux/iocontext.h>
24 #include <asm/div64.h>
27 #include "extent_map.h"
29 #include "transaction.h"
30 #include "print-tree.h"
32 #include "async-thread.h"
42 struct btrfs_bio_stripe stripes[];
45 static int init_first_rw_device(struct btrfs_trans_handle *trans,
46 struct btrfs_root *root,
47 struct btrfs_device *device);
48 static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
50 #define map_lookup_size(n) (sizeof(struct map_lookup) + \
51 (sizeof(struct btrfs_bio_stripe) * (n)))
53 static DEFINE_MUTEX(uuid_mutex);
54 static LIST_HEAD(fs_uuids);
56 void btrfs_lock_volumes(void)
58 mutex_lock(&uuid_mutex);
61 void btrfs_unlock_volumes(void)
63 mutex_unlock(&uuid_mutex);
66 static void lock_chunks(struct btrfs_root *root)
68 mutex_lock(&root->fs_info->chunk_mutex);
71 static void unlock_chunks(struct btrfs_root *root)
73 mutex_unlock(&root->fs_info->chunk_mutex);
76 static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
78 struct btrfs_device *device;
79 WARN_ON(fs_devices->opened);
80 while (!list_empty(&fs_devices->devices)) {
81 device = list_entry(fs_devices->devices.next,
82 struct btrfs_device, dev_list);
83 list_del(&device->dev_list);
90 int btrfs_cleanup_fs_uuids(void)
92 struct btrfs_fs_devices *fs_devices;
94 while (!list_empty(&fs_uuids)) {
95 fs_devices = list_entry(fs_uuids.next,
96 struct btrfs_fs_devices, list);
97 list_del(&fs_devices->list);
98 free_fs_devices(fs_devices);
103 static noinline struct btrfs_device *__find_device(struct list_head *head,
106 struct btrfs_device *dev;
108 list_for_each_entry(dev, head, dev_list) {
109 if (dev->devid == devid &&
110 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
117 static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
119 struct btrfs_fs_devices *fs_devices;
121 list_for_each_entry(fs_devices, &fs_uuids, list) {
122 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
128 static void requeue_list(struct btrfs_pending_bios *pending_bios,
129 struct bio *head, struct bio *tail)
132 struct bio *old_head;
134 old_head = pending_bios->head;
135 pending_bios->head = head;
136 if (pending_bios->tail)
137 tail->bi_next = old_head;
139 pending_bios->tail = tail;
143 * we try to collect pending bios for a device so we don't get a large
144 * number of procs sending bios down to the same device. This greatly
145 * improves the schedulers ability to collect and merge the bios.
147 * But, it also turns into a long list of bios to process and that is sure
148 * to eventually make the worker thread block. The solution here is to
149 * make some progress and then put this work struct back at the end of
150 * the list if the block device is congested. This way, multiple devices
151 * can make progress from a single worker thread.
153 static noinline int run_scheduled_bios(struct btrfs_device *device)
156 struct backing_dev_info *bdi;
157 struct btrfs_fs_info *fs_info;
158 struct btrfs_pending_bios *pending_bios;
162 unsigned long num_run;
163 unsigned long num_sync_run;
164 unsigned long batch_run = 0;
166 unsigned long last_waited = 0;
169 bdi = blk_get_backing_dev_info(device->bdev);
170 fs_info = device->dev_root->fs_info;
171 limit = btrfs_async_submit_limit(fs_info);
172 limit = limit * 2 / 3;
174 /* we want to make sure that every time we switch from the sync
175 * list to the normal list, we unplug
180 spin_lock(&device->io_lock);
185 /* take all the bios off the list at once and process them
186 * later on (without the lock held). But, remember the
187 * tail and other pointers so the bios can be properly reinserted
188 * into the list if we hit congestion
190 if (!force_reg && device->pending_sync_bios.head) {
191 pending_bios = &device->pending_sync_bios;
194 pending_bios = &device->pending_bios;
198 pending = pending_bios->head;
199 tail = pending_bios->tail;
200 WARN_ON(pending && !tail);
203 * if pending was null this time around, no bios need processing
204 * at all and we can stop. Otherwise it'll loop back up again
205 * and do an additional check so no bios are missed.
207 * device->running_pending is used to synchronize with the
210 if (device->pending_sync_bios.head == NULL &&
211 device->pending_bios.head == NULL) {
213 device->running_pending = 0;
216 device->running_pending = 1;
219 pending_bios->head = NULL;
220 pending_bios->tail = NULL;
222 spin_unlock(&device->io_lock);
225 * if we're doing the regular priority list, make sure we unplug
226 * for any high prio bios we've sent down
228 if (pending_bios == &device->pending_bios && num_sync_run > 0) {
230 blk_run_backing_dev(bdi, NULL);
236 /* we want to work on both lists, but do more bios on the
237 * sync list than the regular list
240 pending_bios != &device->pending_sync_bios &&
241 device->pending_sync_bios.head) ||
242 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
243 device->pending_bios.head)) {
244 spin_lock(&device->io_lock);
245 requeue_list(pending_bios, pending, tail);
250 pending = pending->bi_next;
252 atomic_dec(&fs_info->nr_async_bios);
254 if (atomic_read(&fs_info->nr_async_bios) < limit &&
255 waitqueue_active(&fs_info->async_submit_wait))
256 wake_up(&fs_info->async_submit_wait);
258 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
259 submit_bio(cur->bi_rw, cur);
266 if (need_resched()) {
268 blk_run_backing_dev(bdi, NULL);
275 * we made progress, there is more work to do and the bdi
276 * is now congested. Back off and let other work structs
279 if (pending && bdi_write_congested(bdi) && batch_run > 32 &&
280 fs_info->fs_devices->open_devices > 1) {
281 struct io_context *ioc;
283 ioc = current->io_context;
286 * the main goal here is that we don't want to
287 * block if we're going to be able to submit
288 * more requests without blocking.
290 * This code does two great things, it pokes into
291 * the elevator code from a filesystem _and_
292 * it makes assumptions about how batching works.
294 if (ioc && ioc->nr_batch_requests > 0 &&
295 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
297 ioc->last_waited == last_waited)) {
299 * we want to go through our batch of
300 * requests and stop. So, we copy out
301 * the ioc->last_waited time and test
302 * against it before looping
304 last_waited = ioc->last_waited;
305 if (need_resched()) {
307 blk_run_backing_dev(bdi, NULL);
314 spin_lock(&device->io_lock);
315 requeue_list(pending_bios, pending, tail);
316 device->running_pending = 1;
318 spin_unlock(&device->io_lock);
319 btrfs_requeue_work(&device->work);
326 blk_run_backing_dev(bdi, NULL);
333 spin_lock(&device->io_lock);
334 if (device->pending_bios.head || device->pending_sync_bios.head)
336 spin_unlock(&device->io_lock);
339 * IO has already been through a long path to get here. Checksumming,
340 * async helper threads, perhaps compression. We've done a pretty
341 * good job of collecting a batch of IO and should just unplug
342 * the device right away.
344 * This will help anyone who is waiting on the IO, they might have
345 * already unplugged, but managed to do so before the bio they
346 * cared about found its way down here.
348 blk_run_backing_dev(bdi, NULL);
353 static void pending_bios_fn(struct btrfs_work *work)
355 struct btrfs_device *device;
357 device = container_of(work, struct btrfs_device, work);
358 run_scheduled_bios(device);
361 static noinline int device_list_add(const char *path,
362 struct btrfs_super_block *disk_super,
363 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
365 struct btrfs_device *device;
366 struct btrfs_fs_devices *fs_devices;
367 u64 found_transid = btrfs_super_generation(disk_super);
369 fs_devices = find_fsid(disk_super->fsid);
371 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
374 INIT_LIST_HEAD(&fs_devices->devices);
375 INIT_LIST_HEAD(&fs_devices->alloc_list);
376 list_add(&fs_devices->list, &fs_uuids);
377 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
378 fs_devices->latest_devid = devid;
379 fs_devices->latest_trans = found_transid;
382 device = __find_device(&fs_devices->devices, devid,
383 disk_super->dev_item.uuid);
386 if (fs_devices->opened)
389 device = kzalloc(sizeof(*device), GFP_NOFS);
391 /* we can safely leave the fs_devices entry around */
394 device->devid = devid;
395 device->work.func = pending_bios_fn;
396 memcpy(device->uuid, disk_super->dev_item.uuid,
398 device->barriers = 1;
399 spin_lock_init(&device->io_lock);
400 device->name = kstrdup(path, GFP_NOFS);
405 INIT_LIST_HEAD(&device->dev_alloc_list);
406 list_add(&device->dev_list, &fs_devices->devices);
407 device->fs_devices = fs_devices;
408 fs_devices->num_devices++;
411 if (found_transid > fs_devices->latest_trans) {
412 fs_devices->latest_devid = devid;
413 fs_devices->latest_trans = found_transid;
415 *fs_devices_ret = fs_devices;
419 static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
421 struct btrfs_fs_devices *fs_devices;
422 struct btrfs_device *device;
423 struct btrfs_device *orig_dev;
425 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
427 return ERR_PTR(-ENOMEM);
429 INIT_LIST_HEAD(&fs_devices->devices);
430 INIT_LIST_HEAD(&fs_devices->alloc_list);
431 INIT_LIST_HEAD(&fs_devices->list);
432 fs_devices->latest_devid = orig->latest_devid;
433 fs_devices->latest_trans = orig->latest_trans;
434 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
436 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
437 device = kzalloc(sizeof(*device), GFP_NOFS);
441 device->name = kstrdup(orig_dev->name, GFP_NOFS);
445 device->devid = orig_dev->devid;
446 device->work.func = pending_bios_fn;
447 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
448 device->barriers = 1;
449 spin_lock_init(&device->io_lock);
450 INIT_LIST_HEAD(&device->dev_list);
451 INIT_LIST_HEAD(&device->dev_alloc_list);
453 list_add(&device->dev_list, &fs_devices->devices);
454 device->fs_devices = fs_devices;
455 fs_devices->num_devices++;
459 free_fs_devices(fs_devices);
460 return ERR_PTR(-ENOMEM);
463 int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
465 struct btrfs_device *device, *next;
467 mutex_lock(&uuid_mutex);
469 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
470 if (device->in_fs_metadata)
474 close_bdev_exclusive(device->bdev, device->mode);
476 fs_devices->open_devices--;
478 if (device->writeable) {
479 list_del_init(&device->dev_alloc_list);
480 device->writeable = 0;
481 fs_devices->rw_devices--;
483 list_del_init(&device->dev_list);
484 fs_devices->num_devices--;
489 if (fs_devices->seed) {
490 fs_devices = fs_devices->seed;
494 mutex_unlock(&uuid_mutex);
498 static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
500 struct btrfs_device *device;
502 if (--fs_devices->opened > 0)
505 list_for_each_entry(device, &fs_devices->devices, dev_list) {
507 close_bdev_exclusive(device->bdev, device->mode);
508 fs_devices->open_devices--;
510 if (device->writeable) {
511 list_del_init(&device->dev_alloc_list);
512 fs_devices->rw_devices--;
516 device->writeable = 0;
517 device->in_fs_metadata = 0;
519 WARN_ON(fs_devices->open_devices);
520 WARN_ON(fs_devices->rw_devices);
521 fs_devices->opened = 0;
522 fs_devices->seeding = 0;
527 int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
529 struct btrfs_fs_devices *seed_devices = NULL;
532 mutex_lock(&uuid_mutex);
533 ret = __btrfs_close_devices(fs_devices);
534 if (!fs_devices->opened) {
535 seed_devices = fs_devices->seed;
536 fs_devices->seed = NULL;
538 mutex_unlock(&uuid_mutex);
540 while (seed_devices) {
541 fs_devices = seed_devices;
542 seed_devices = fs_devices->seed;
543 __btrfs_close_devices(fs_devices);
544 free_fs_devices(fs_devices);
549 static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
550 fmode_t flags, void *holder)
552 struct block_device *bdev;
553 struct list_head *head = &fs_devices->devices;
554 struct btrfs_device *device;
555 struct block_device *latest_bdev = NULL;
556 struct buffer_head *bh;
557 struct btrfs_super_block *disk_super;
558 u64 latest_devid = 0;
559 u64 latest_transid = 0;
564 list_for_each_entry(device, head, dev_list) {
570 bdev = open_bdev_exclusive(device->name, flags, holder);
572 printk(KERN_INFO "open %s failed\n", device->name);
575 set_blocksize(bdev, 4096);
577 bh = btrfs_read_dev_super(bdev);
581 disk_super = (struct btrfs_super_block *)bh->b_data;
582 devid = le64_to_cpu(disk_super->dev_item.devid);
583 if (devid != device->devid)
586 if (memcmp(device->uuid, disk_super->dev_item.uuid,
590 device->generation = btrfs_super_generation(disk_super);
591 if (!latest_transid || device->generation > latest_transid) {
592 latest_devid = devid;
593 latest_transid = device->generation;
597 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
598 device->writeable = 0;
600 device->writeable = !bdev_read_only(bdev);
605 device->in_fs_metadata = 0;
606 device->mode = flags;
608 fs_devices->open_devices++;
609 if (device->writeable) {
610 fs_devices->rw_devices++;
611 list_add(&device->dev_alloc_list,
612 &fs_devices->alloc_list);
619 close_bdev_exclusive(bdev, FMODE_READ);
623 if (fs_devices->open_devices == 0) {
627 fs_devices->seeding = seeding;
628 fs_devices->opened = 1;
629 fs_devices->latest_bdev = latest_bdev;
630 fs_devices->latest_devid = latest_devid;
631 fs_devices->latest_trans = latest_transid;
632 fs_devices->total_rw_bytes = 0;
637 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
638 fmode_t flags, void *holder)
642 mutex_lock(&uuid_mutex);
643 if (fs_devices->opened) {
644 fs_devices->opened++;
647 ret = __btrfs_open_devices(fs_devices, flags, holder);
649 mutex_unlock(&uuid_mutex);
653 int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
654 struct btrfs_fs_devices **fs_devices_ret)
656 struct btrfs_super_block *disk_super;
657 struct block_device *bdev;
658 struct buffer_head *bh;
663 mutex_lock(&uuid_mutex);
665 bdev = open_bdev_exclusive(path, flags, holder);
672 ret = set_blocksize(bdev, 4096);
675 bh = btrfs_read_dev_super(bdev);
680 disk_super = (struct btrfs_super_block *)bh->b_data;
681 devid = le64_to_cpu(disk_super->dev_item.devid);
682 transid = btrfs_super_generation(disk_super);
683 if (disk_super->label[0])
684 printk(KERN_INFO "device label %s ", disk_super->label);
686 /* FIXME, make a readl uuid parser */
687 printk(KERN_INFO "device fsid %llx-%llx ",
688 *(unsigned long long *)disk_super->fsid,
689 *(unsigned long long *)(disk_super->fsid + 8));
691 printk(KERN_CONT "devid %llu transid %llu %s\n",
692 (unsigned long long)devid, (unsigned long long)transid, path);
693 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
697 close_bdev_exclusive(bdev, flags);
699 mutex_unlock(&uuid_mutex);
704 * this uses a pretty simple search, the expectation is that it is
705 * called very infrequently and that a given device has a small number
708 static noinline int find_free_dev_extent(struct btrfs_trans_handle *trans,
709 struct btrfs_device *device,
710 u64 num_bytes, u64 *start)
712 struct btrfs_key key;
713 struct btrfs_root *root = device->dev_root;
714 struct btrfs_dev_extent *dev_extent = NULL;
715 struct btrfs_path *path;
718 u64 search_start = 0;
719 u64 search_end = device->total_bytes;
723 struct extent_buffer *l;
725 path = btrfs_alloc_path();
731 /* FIXME use last free of some kind */
733 /* we don't want to overwrite the superblock on the drive,
734 * so we make sure to start at an offset of at least 1MB
736 search_start = max((u64)1024 * 1024, search_start);
738 if (root->fs_info->alloc_start + num_bytes <= device->total_bytes)
739 search_start = max(root->fs_info->alloc_start, search_start);
741 key.objectid = device->devid;
742 key.offset = search_start;
743 key.type = BTRFS_DEV_EXTENT_KEY;
744 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
747 ret = btrfs_previous_item(root, path, 0, key.type);
751 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
754 slot = path->slots[0];
755 if (slot >= btrfs_header_nritems(l)) {
756 ret = btrfs_next_leaf(root, path);
763 if (search_start >= search_end) {
767 *start = search_start;
771 *start = last_byte > search_start ?
772 last_byte : search_start;
773 if (search_end <= *start) {
779 btrfs_item_key_to_cpu(l, &key, slot);
781 if (key.objectid < device->devid)
784 if (key.objectid > device->devid)
787 if (key.offset >= search_start && key.offset > last_byte &&
789 if (last_byte < search_start)
790 last_byte = search_start;
791 hole_size = key.offset - last_byte;
792 if (key.offset > last_byte &&
793 hole_size >= num_bytes) {
798 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
802 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
803 last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent);
809 /* we have to make sure we didn't find an extent that has already
810 * been allocated by the map tree or the original allocation
812 BUG_ON(*start < search_start);
814 if (*start + num_bytes > search_end) {
818 /* check for pending inserts here */
822 btrfs_free_path(path);
826 static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
827 struct btrfs_device *device,
831 struct btrfs_path *path;
832 struct btrfs_root *root = device->dev_root;
833 struct btrfs_key key;
834 struct btrfs_key found_key;
835 struct extent_buffer *leaf = NULL;
836 struct btrfs_dev_extent *extent = NULL;
838 path = btrfs_alloc_path();
842 key.objectid = device->devid;
844 key.type = BTRFS_DEV_EXTENT_KEY;
846 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
848 ret = btrfs_previous_item(root, path, key.objectid,
849 BTRFS_DEV_EXTENT_KEY);
851 leaf = path->nodes[0];
852 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
853 extent = btrfs_item_ptr(leaf, path->slots[0],
854 struct btrfs_dev_extent);
855 BUG_ON(found_key.offset > start || found_key.offset +
856 btrfs_dev_extent_length(leaf, extent) < start);
858 } else if (ret == 0) {
859 leaf = path->nodes[0];
860 extent = btrfs_item_ptr(leaf, path->slots[0],
861 struct btrfs_dev_extent);
865 if (device->bytes_used > 0)
866 device->bytes_used -= btrfs_dev_extent_length(leaf, extent);
867 ret = btrfs_del_item(trans, root, path);
870 btrfs_free_path(path);
874 int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
875 struct btrfs_device *device,
876 u64 chunk_tree, u64 chunk_objectid,
877 u64 chunk_offset, u64 start, u64 num_bytes)
880 struct btrfs_path *path;
881 struct btrfs_root *root = device->dev_root;
882 struct btrfs_dev_extent *extent;
883 struct extent_buffer *leaf;
884 struct btrfs_key key;
886 WARN_ON(!device->in_fs_metadata);
887 path = btrfs_alloc_path();
891 key.objectid = device->devid;
893 key.type = BTRFS_DEV_EXTENT_KEY;
894 ret = btrfs_insert_empty_item(trans, root, path, &key,
898 leaf = path->nodes[0];
899 extent = btrfs_item_ptr(leaf, path->slots[0],
900 struct btrfs_dev_extent);
901 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
902 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
903 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
905 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
906 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
909 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
910 btrfs_mark_buffer_dirty(leaf);
911 btrfs_free_path(path);
915 static noinline int find_next_chunk(struct btrfs_root *root,
916 u64 objectid, u64 *offset)
918 struct btrfs_path *path;
920 struct btrfs_key key;
921 struct btrfs_chunk *chunk;
922 struct btrfs_key found_key;
924 path = btrfs_alloc_path();
927 key.objectid = objectid;
928 key.offset = (u64)-1;
929 key.type = BTRFS_CHUNK_ITEM_KEY;
931 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
937 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
941 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
943 if (found_key.objectid != objectid)
946 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
948 *offset = found_key.offset +
949 btrfs_chunk_length(path->nodes[0], chunk);
954 btrfs_free_path(path);
958 static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
961 struct btrfs_key key;
962 struct btrfs_key found_key;
963 struct btrfs_path *path;
965 root = root->fs_info->chunk_root;
967 path = btrfs_alloc_path();
971 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
972 key.type = BTRFS_DEV_ITEM_KEY;
973 key.offset = (u64)-1;
975 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
981 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
986 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
988 *objectid = found_key.offset + 1;
992 btrfs_free_path(path);
997 * the device information is stored in the chunk root
998 * the btrfs_device struct should be fully filled in
1000 int btrfs_add_device(struct btrfs_trans_handle *trans,
1001 struct btrfs_root *root,
1002 struct btrfs_device *device)
1005 struct btrfs_path *path;
1006 struct btrfs_dev_item *dev_item;
1007 struct extent_buffer *leaf;
1008 struct btrfs_key key;
1011 root = root->fs_info->chunk_root;
1013 path = btrfs_alloc_path();
1017 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1018 key.type = BTRFS_DEV_ITEM_KEY;
1019 key.offset = device->devid;
1021 ret = btrfs_insert_empty_item(trans, root, path, &key,
1026 leaf = path->nodes[0];
1027 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1029 btrfs_set_device_id(leaf, dev_item, device->devid);
1030 btrfs_set_device_generation(leaf, dev_item, 0);
1031 btrfs_set_device_type(leaf, dev_item, device->type);
1032 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1033 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1034 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
1035 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1036 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1037 btrfs_set_device_group(leaf, dev_item, 0);
1038 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1039 btrfs_set_device_bandwidth(leaf, dev_item, 0);
1040 btrfs_set_device_start_offset(leaf, dev_item, 0);
1042 ptr = (unsigned long)btrfs_device_uuid(dev_item);
1043 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1044 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1045 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
1046 btrfs_mark_buffer_dirty(leaf);
1050 btrfs_free_path(path);
1054 static int btrfs_rm_dev_item(struct btrfs_root *root,
1055 struct btrfs_device *device)
1058 struct btrfs_path *path;
1059 struct btrfs_key key;
1060 struct btrfs_trans_handle *trans;
1062 root = root->fs_info->chunk_root;
1064 path = btrfs_alloc_path();
1068 trans = btrfs_start_transaction(root, 1);
1069 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1070 key.type = BTRFS_DEV_ITEM_KEY;
1071 key.offset = device->devid;
1074 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1083 ret = btrfs_del_item(trans, root, path);
1087 btrfs_free_path(path);
1088 unlock_chunks(root);
1089 btrfs_commit_transaction(trans, root);
1093 int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1095 struct btrfs_device *device;
1096 struct btrfs_device *next_device;
1097 struct block_device *bdev;
1098 struct buffer_head *bh = NULL;
1099 struct btrfs_super_block *disk_super;
1106 mutex_lock(&uuid_mutex);
1107 mutex_lock(&root->fs_info->volume_mutex);
1109 all_avail = root->fs_info->avail_data_alloc_bits |
1110 root->fs_info->avail_system_alloc_bits |
1111 root->fs_info->avail_metadata_alloc_bits;
1113 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
1114 root->fs_info->fs_devices->rw_devices <= 4) {
1115 printk(KERN_ERR "btrfs: unable to go below four devices "
1121 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
1122 root->fs_info->fs_devices->rw_devices <= 2) {
1123 printk(KERN_ERR "btrfs: unable to go below two "
1124 "devices on raid1\n");
1129 if (strcmp(device_path, "missing") == 0) {
1130 struct list_head *devices;
1131 struct btrfs_device *tmp;
1134 devices = &root->fs_info->fs_devices->devices;
1135 list_for_each_entry(tmp, devices, dev_list) {
1136 if (tmp->in_fs_metadata && !tmp->bdev) {
1145 printk(KERN_ERR "btrfs: no missing devices found to "
1150 bdev = open_bdev_exclusive(device_path, FMODE_READ,
1151 root->fs_info->bdev_holder);
1153 ret = PTR_ERR(bdev);
1157 set_blocksize(bdev, 4096);
1158 bh = btrfs_read_dev_super(bdev);
1163 disk_super = (struct btrfs_super_block *)bh->b_data;
1164 devid = le64_to_cpu(disk_super->dev_item.devid);
1165 dev_uuid = disk_super->dev_item.uuid;
1166 device = btrfs_find_device(root, devid, dev_uuid,
1174 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
1175 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1181 if (device->writeable) {
1182 list_del_init(&device->dev_alloc_list);
1183 root->fs_info->fs_devices->rw_devices--;
1186 ret = btrfs_shrink_device(device, 0);
1190 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1194 device->in_fs_metadata = 0;
1195 list_del_init(&device->dev_list);
1196 device->fs_devices->num_devices--;
1198 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1199 struct btrfs_device, dev_list);
1200 if (device->bdev == root->fs_info->sb->s_bdev)
1201 root->fs_info->sb->s_bdev = next_device->bdev;
1202 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1203 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1206 close_bdev_exclusive(device->bdev, device->mode);
1207 device->bdev = NULL;
1208 device->fs_devices->open_devices--;
1211 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
1212 btrfs_set_super_num_devices(&root->fs_info->super_copy, num_devices);
1214 if (device->fs_devices->open_devices == 0) {
1215 struct btrfs_fs_devices *fs_devices;
1216 fs_devices = root->fs_info->fs_devices;
1217 while (fs_devices) {
1218 if (fs_devices->seed == device->fs_devices)
1220 fs_devices = fs_devices->seed;
1222 fs_devices->seed = device->fs_devices->seed;
1223 device->fs_devices->seed = NULL;
1224 __btrfs_close_devices(device->fs_devices);
1225 free_fs_devices(device->fs_devices);
1229 * at this point, the device is zero sized. We want to
1230 * remove it from the devices list and zero out the old super
1232 if (device->writeable) {
1233 /* make sure this device isn't detected as part of
1236 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1237 set_buffer_dirty(bh);
1238 sync_dirty_buffer(bh);
1241 kfree(device->name);
1249 close_bdev_exclusive(bdev, FMODE_READ);
1251 mutex_unlock(&root->fs_info->volume_mutex);
1252 mutex_unlock(&uuid_mutex);
1257 * does all the dirty work required for changing file system's UUID.
1259 static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans,
1260 struct btrfs_root *root)
1262 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1263 struct btrfs_fs_devices *old_devices;
1264 struct btrfs_fs_devices *seed_devices;
1265 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
1266 struct btrfs_device *device;
1269 BUG_ON(!mutex_is_locked(&uuid_mutex));
1270 if (!fs_devices->seeding)
1273 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1277 old_devices = clone_fs_devices(fs_devices);
1278 if (IS_ERR(old_devices)) {
1279 kfree(seed_devices);
1280 return PTR_ERR(old_devices);
1283 list_add(&old_devices->list, &fs_uuids);
1285 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1286 seed_devices->opened = 1;
1287 INIT_LIST_HEAD(&seed_devices->devices);
1288 INIT_LIST_HEAD(&seed_devices->alloc_list);
1289 list_splice_init(&fs_devices->devices, &seed_devices->devices);
1290 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1291 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1292 device->fs_devices = seed_devices;
1295 fs_devices->seeding = 0;
1296 fs_devices->num_devices = 0;
1297 fs_devices->open_devices = 0;
1298 fs_devices->seed = seed_devices;
1300 generate_random_uuid(fs_devices->fsid);
1301 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1302 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1303 super_flags = btrfs_super_flags(disk_super) &
1304 ~BTRFS_SUPER_FLAG_SEEDING;
1305 btrfs_set_super_flags(disk_super, super_flags);
1311 * strore the expected generation for seed devices in device items.
1313 static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1314 struct btrfs_root *root)
1316 struct btrfs_path *path;
1317 struct extent_buffer *leaf;
1318 struct btrfs_dev_item *dev_item;
1319 struct btrfs_device *device;
1320 struct btrfs_key key;
1321 u8 fs_uuid[BTRFS_UUID_SIZE];
1322 u8 dev_uuid[BTRFS_UUID_SIZE];
1326 path = btrfs_alloc_path();
1330 root = root->fs_info->chunk_root;
1331 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1333 key.type = BTRFS_DEV_ITEM_KEY;
1336 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1340 leaf = path->nodes[0];
1342 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1343 ret = btrfs_next_leaf(root, path);
1348 leaf = path->nodes[0];
1349 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1350 btrfs_release_path(root, path);
1354 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1355 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1356 key.type != BTRFS_DEV_ITEM_KEY)
1359 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1360 struct btrfs_dev_item);
1361 devid = btrfs_device_id(leaf, dev_item);
1362 read_extent_buffer(leaf, dev_uuid,
1363 (unsigned long)btrfs_device_uuid(dev_item),
1365 read_extent_buffer(leaf, fs_uuid,
1366 (unsigned long)btrfs_device_fsid(dev_item),
1368 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1371 if (device->fs_devices->seeding) {
1372 btrfs_set_device_generation(leaf, dev_item,
1373 device->generation);
1374 btrfs_mark_buffer_dirty(leaf);
1382 btrfs_free_path(path);
1386 int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1388 struct btrfs_trans_handle *trans;
1389 struct btrfs_device *device;
1390 struct block_device *bdev;
1391 struct list_head *devices;
1392 struct super_block *sb = root->fs_info->sb;
1394 int seeding_dev = 0;
1397 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1400 bdev = open_bdev_exclusive(device_path, 0, root->fs_info->bdev_holder);
1404 if (root->fs_info->fs_devices->seeding) {
1406 down_write(&sb->s_umount);
1407 mutex_lock(&uuid_mutex);
1410 filemap_write_and_wait(bdev->bd_inode->i_mapping);
1411 mutex_lock(&root->fs_info->volume_mutex);
1413 devices = &root->fs_info->fs_devices->devices;
1414 list_for_each_entry(device, devices, dev_list) {
1415 if (device->bdev == bdev) {
1421 device = kzalloc(sizeof(*device), GFP_NOFS);
1423 /* we can safely leave the fs_devices entry around */
1428 device->name = kstrdup(device_path, GFP_NOFS);
1429 if (!device->name) {
1435 ret = find_next_devid(root, &device->devid);
1441 trans = btrfs_start_transaction(root, 1);
1444 device->barriers = 1;
1445 device->writeable = 1;
1446 device->work.func = pending_bios_fn;
1447 generate_random_uuid(device->uuid);
1448 spin_lock_init(&device->io_lock);
1449 device->generation = trans->transid;
1450 device->io_width = root->sectorsize;
1451 device->io_align = root->sectorsize;
1452 device->sector_size = root->sectorsize;
1453 device->total_bytes = i_size_read(bdev->bd_inode);
1454 device->disk_total_bytes = device->total_bytes;
1455 device->dev_root = root->fs_info->dev_root;
1456 device->bdev = bdev;
1457 device->in_fs_metadata = 1;
1459 set_blocksize(device->bdev, 4096);
1462 sb->s_flags &= ~MS_RDONLY;
1463 ret = btrfs_prepare_sprout(trans, root);
1467 device->fs_devices = root->fs_info->fs_devices;
1468 list_add(&device->dev_list, &root->fs_info->fs_devices->devices);
1469 list_add(&device->dev_alloc_list,
1470 &root->fs_info->fs_devices->alloc_list);
1471 root->fs_info->fs_devices->num_devices++;
1472 root->fs_info->fs_devices->open_devices++;
1473 root->fs_info->fs_devices->rw_devices++;
1474 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1476 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
1477 btrfs_set_super_total_bytes(&root->fs_info->super_copy,
1478 total_bytes + device->total_bytes);
1480 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
1481 btrfs_set_super_num_devices(&root->fs_info->super_copy,
1485 ret = init_first_rw_device(trans, root, device);
1487 ret = btrfs_finish_sprout(trans, root);
1490 ret = btrfs_add_device(trans, root, device);
1494 * we've got more storage, clear any full flags on the space
1497 btrfs_clear_space_info_full(root->fs_info);
1499 unlock_chunks(root);
1500 btrfs_commit_transaction(trans, root);
1503 mutex_unlock(&uuid_mutex);
1504 up_write(&sb->s_umount);
1506 ret = btrfs_relocate_sys_chunks(root);
1510 mutex_unlock(&root->fs_info->volume_mutex);
1513 close_bdev_exclusive(bdev, 0);
1515 mutex_unlock(&uuid_mutex);
1516 up_write(&sb->s_umount);
1521 static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1522 struct btrfs_device *device)
1525 struct btrfs_path *path;
1526 struct btrfs_root *root;
1527 struct btrfs_dev_item *dev_item;
1528 struct extent_buffer *leaf;
1529 struct btrfs_key key;
1531 root = device->dev_root->fs_info->chunk_root;
1533 path = btrfs_alloc_path();
1537 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1538 key.type = BTRFS_DEV_ITEM_KEY;
1539 key.offset = device->devid;
1541 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1550 leaf = path->nodes[0];
1551 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1553 btrfs_set_device_id(leaf, dev_item, device->devid);
1554 btrfs_set_device_type(leaf, dev_item, device->type);
1555 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1556 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1557 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
1558 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
1559 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1560 btrfs_mark_buffer_dirty(leaf);
1563 btrfs_free_path(path);
1567 static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
1568 struct btrfs_device *device, u64 new_size)
1570 struct btrfs_super_block *super_copy =
1571 &device->dev_root->fs_info->super_copy;
1572 u64 old_total = btrfs_super_total_bytes(super_copy);
1573 u64 diff = new_size - device->total_bytes;
1575 if (!device->writeable)
1577 if (new_size <= device->total_bytes)
1580 btrfs_set_super_total_bytes(super_copy, old_total + diff);
1581 device->fs_devices->total_rw_bytes += diff;
1583 device->total_bytes = new_size;
1584 btrfs_clear_space_info_full(device->dev_root->fs_info);
1586 return btrfs_update_device(trans, device);
1589 int btrfs_grow_device(struct btrfs_trans_handle *trans,
1590 struct btrfs_device *device, u64 new_size)
1593 lock_chunks(device->dev_root);
1594 ret = __btrfs_grow_device(trans, device, new_size);
1595 unlock_chunks(device->dev_root);
1599 static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1600 struct btrfs_root *root,
1601 u64 chunk_tree, u64 chunk_objectid,
1605 struct btrfs_path *path;
1606 struct btrfs_key key;
1608 root = root->fs_info->chunk_root;
1609 path = btrfs_alloc_path();
1613 key.objectid = chunk_objectid;
1614 key.offset = chunk_offset;
1615 key.type = BTRFS_CHUNK_ITEM_KEY;
1617 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1620 ret = btrfs_del_item(trans, root, path);
1623 btrfs_free_path(path);
1627 static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
1630 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1631 struct btrfs_disk_key *disk_key;
1632 struct btrfs_chunk *chunk;
1639 struct btrfs_key key;
1641 array_size = btrfs_super_sys_array_size(super_copy);
1643 ptr = super_copy->sys_chunk_array;
1646 while (cur < array_size) {
1647 disk_key = (struct btrfs_disk_key *)ptr;
1648 btrfs_disk_key_to_cpu(&key, disk_key);
1650 len = sizeof(*disk_key);
1652 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1653 chunk = (struct btrfs_chunk *)(ptr + len);
1654 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1655 len += btrfs_chunk_item_size(num_stripes);
1660 if (key.objectid == chunk_objectid &&
1661 key.offset == chunk_offset) {
1662 memmove(ptr, ptr + len, array_size - (cur + len));
1664 btrfs_set_super_sys_array_size(super_copy, array_size);
1673 static int btrfs_relocate_chunk(struct btrfs_root *root,
1674 u64 chunk_tree, u64 chunk_objectid,
1677 struct extent_map_tree *em_tree;
1678 struct btrfs_root *extent_root;
1679 struct btrfs_trans_handle *trans;
1680 struct extent_map *em;
1681 struct map_lookup *map;
1685 root = root->fs_info->chunk_root;
1686 extent_root = root->fs_info->extent_root;
1687 em_tree = &root->fs_info->mapping_tree.map_tree;
1689 /* step one, relocate all the extents inside this chunk */
1690 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
1693 trans = btrfs_start_transaction(root, 1);
1699 * step two, delete the device extents and the
1700 * chunk tree entries
1702 spin_lock(&em_tree->lock);
1703 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
1704 spin_unlock(&em_tree->lock);
1706 BUG_ON(em->start > chunk_offset ||
1707 em->start + em->len < chunk_offset);
1708 map = (struct map_lookup *)em->bdev;
1710 for (i = 0; i < map->num_stripes; i++) {
1711 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1712 map->stripes[i].physical);
1715 if (map->stripes[i].dev) {
1716 ret = btrfs_update_device(trans, map->stripes[i].dev);
1720 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1725 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1726 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1730 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
1733 spin_lock(&em_tree->lock);
1734 remove_extent_mapping(em_tree, em);
1735 spin_unlock(&em_tree->lock);
1740 /* once for the tree */
1741 free_extent_map(em);
1743 free_extent_map(em);
1745 unlock_chunks(root);
1746 btrfs_end_transaction(trans, root);
1750 static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
1752 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1753 struct btrfs_path *path;
1754 struct extent_buffer *leaf;
1755 struct btrfs_chunk *chunk;
1756 struct btrfs_key key;
1757 struct btrfs_key found_key;
1758 u64 chunk_tree = chunk_root->root_key.objectid;
1762 path = btrfs_alloc_path();
1766 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1767 key.offset = (u64)-1;
1768 key.type = BTRFS_CHUNK_ITEM_KEY;
1771 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1776 ret = btrfs_previous_item(chunk_root, path, key.objectid,
1783 leaf = path->nodes[0];
1784 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1786 chunk = btrfs_item_ptr(leaf, path->slots[0],
1787 struct btrfs_chunk);
1788 chunk_type = btrfs_chunk_type(leaf, chunk);
1789 btrfs_release_path(chunk_root, path);
1791 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
1792 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
1798 if (found_key.offset == 0)
1800 key.offset = found_key.offset - 1;
1804 btrfs_free_path(path);
1808 static u64 div_factor(u64 num, int factor)
1817 int btrfs_balance(struct btrfs_root *dev_root)
1820 struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
1821 struct btrfs_device *device;
1824 struct btrfs_path *path;
1825 struct btrfs_key key;
1826 struct btrfs_chunk *chunk;
1827 struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
1828 struct btrfs_trans_handle *trans;
1829 struct btrfs_key found_key;
1831 if (dev_root->fs_info->sb->s_flags & MS_RDONLY)
1834 mutex_lock(&dev_root->fs_info->volume_mutex);
1835 dev_root = dev_root->fs_info->dev_root;
1837 /* step one make some room on all the devices */
1838 list_for_each_entry(device, devices, dev_list) {
1839 old_size = device->total_bytes;
1840 size_to_free = div_factor(old_size, 1);
1841 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
1842 if (!device->writeable ||
1843 device->total_bytes - device->bytes_used > size_to_free)
1846 ret = btrfs_shrink_device(device, old_size - size_to_free);
1849 trans = btrfs_start_transaction(dev_root, 1);
1852 ret = btrfs_grow_device(trans, device, old_size);
1855 btrfs_end_transaction(trans, dev_root);
1858 /* step two, relocate all the chunks */
1859 path = btrfs_alloc_path();
1862 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1863 key.offset = (u64)-1;
1864 key.type = BTRFS_CHUNK_ITEM_KEY;
1867 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1872 * this shouldn't happen, it means the last relocate
1878 ret = btrfs_previous_item(chunk_root, path, 0,
1879 BTRFS_CHUNK_ITEM_KEY);
1883 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1885 if (found_key.objectid != key.objectid)
1888 chunk = btrfs_item_ptr(path->nodes[0],
1890 struct btrfs_chunk);
1891 key.offset = found_key.offset;
1892 /* chunk zero is special */
1893 if (key.offset == 0)
1896 btrfs_release_path(chunk_root, path);
1897 ret = btrfs_relocate_chunk(chunk_root,
1898 chunk_root->root_key.objectid,
1905 btrfs_free_path(path);
1906 mutex_unlock(&dev_root->fs_info->volume_mutex);
1911 * shrinking a device means finding all of the device extents past
1912 * the new size, and then following the back refs to the chunks.
1913 * The chunk relocation code actually frees the device extent
1915 int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
1917 struct btrfs_trans_handle *trans;
1918 struct btrfs_root *root = device->dev_root;
1919 struct btrfs_dev_extent *dev_extent = NULL;
1920 struct btrfs_path *path;
1927 struct extent_buffer *l;
1928 struct btrfs_key key;
1929 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1930 u64 old_total = btrfs_super_total_bytes(super_copy);
1931 u64 diff = device->total_bytes - new_size;
1933 if (new_size >= device->total_bytes)
1936 path = btrfs_alloc_path();
1940 trans = btrfs_start_transaction(root, 1);
1950 device->total_bytes = new_size;
1951 if (device->writeable)
1952 device->fs_devices->total_rw_bytes -= diff;
1953 unlock_chunks(root);
1954 btrfs_end_transaction(trans, root);
1956 key.objectid = device->devid;
1957 key.offset = (u64)-1;
1958 key.type = BTRFS_DEV_EXTENT_KEY;
1961 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1965 ret = btrfs_previous_item(root, path, 0, key.type);
1974 slot = path->slots[0];
1975 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
1977 if (key.objectid != device->devid)
1980 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
1981 length = btrfs_dev_extent_length(l, dev_extent);
1983 if (key.offset + length <= new_size)
1986 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
1987 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
1988 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
1989 btrfs_release_path(root, path);
1991 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
1997 /* Shrinking succeeded, else we would be at "done". */
1998 trans = btrfs_start_transaction(root, 1);
2005 device->disk_total_bytes = new_size;
2006 /* Now btrfs_update_device() will change the on-disk size. */
2007 ret = btrfs_update_device(trans, device);
2009 unlock_chunks(root);
2010 btrfs_end_transaction(trans, root);
2013 WARN_ON(diff > old_total);
2014 btrfs_set_super_total_bytes(super_copy, old_total - diff);
2015 unlock_chunks(root);
2016 btrfs_end_transaction(trans, root);
2018 btrfs_free_path(path);
2022 static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
2023 struct btrfs_root *root,
2024 struct btrfs_key *key,
2025 struct btrfs_chunk *chunk, int item_size)
2027 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2028 struct btrfs_disk_key disk_key;
2032 array_size = btrfs_super_sys_array_size(super_copy);
2033 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
2036 ptr = super_copy->sys_chunk_array + array_size;
2037 btrfs_cpu_key_to_disk(&disk_key, key);
2038 memcpy(ptr, &disk_key, sizeof(disk_key));
2039 ptr += sizeof(disk_key);
2040 memcpy(ptr, chunk, item_size);
2041 item_size += sizeof(disk_key);
2042 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
2046 static noinline u64 chunk_bytes_by_type(u64 type, u64 calc_size,
2047 int num_stripes, int sub_stripes)
2049 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
2051 else if (type & BTRFS_BLOCK_GROUP_RAID10)
2052 return calc_size * (num_stripes / sub_stripes);
2054 return calc_size * num_stripes;
2057 static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2058 struct btrfs_root *extent_root,
2059 struct map_lookup **map_ret,
2060 u64 *num_bytes, u64 *stripe_size,
2061 u64 start, u64 type)
2063 struct btrfs_fs_info *info = extent_root->fs_info;
2064 struct btrfs_device *device = NULL;
2065 struct btrfs_fs_devices *fs_devices = info->fs_devices;
2066 struct list_head *cur;
2067 struct map_lookup *map = NULL;
2068 struct extent_map_tree *em_tree;
2069 struct extent_map *em;
2070 struct list_head private_devs;
2071 int min_stripe_size = 1 * 1024 * 1024;
2072 u64 calc_size = 1024 * 1024 * 1024;
2073 u64 max_chunk_size = calc_size;
2078 int num_stripes = 1;
2079 int min_stripes = 1;
2080 int sub_stripes = 0;
2084 int stripe_len = 64 * 1024;
2086 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
2087 (type & BTRFS_BLOCK_GROUP_DUP)) {
2089 type &= ~BTRFS_BLOCK_GROUP_DUP;
2091 if (list_empty(&fs_devices->alloc_list))
2094 if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
2095 num_stripes = fs_devices->rw_devices;
2098 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
2102 if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
2103 num_stripes = min_t(u64, 2, fs_devices->rw_devices);
2104 if (num_stripes < 2)
2108 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2109 num_stripes = fs_devices->rw_devices;
2110 if (num_stripes < 4)
2112 num_stripes &= ~(u32)1;
2117 if (type & BTRFS_BLOCK_GROUP_DATA) {
2118 max_chunk_size = 10 * calc_size;
2119 min_stripe_size = 64 * 1024 * 1024;
2120 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
2121 max_chunk_size = 4 * calc_size;
2122 min_stripe_size = 32 * 1024 * 1024;
2123 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
2124 calc_size = 8 * 1024 * 1024;
2125 max_chunk_size = calc_size * 2;
2126 min_stripe_size = 1 * 1024 * 1024;
2129 /* we don't want a chunk larger than 10% of writeable space */
2130 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
2134 if (!map || map->num_stripes != num_stripes) {
2136 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
2139 map->num_stripes = num_stripes;
2142 if (calc_size * num_stripes > max_chunk_size) {
2143 calc_size = max_chunk_size;
2144 do_div(calc_size, num_stripes);
2145 do_div(calc_size, stripe_len);
2146 calc_size *= stripe_len;
2148 /* we don't want tiny stripes */
2149 calc_size = max_t(u64, min_stripe_size, calc_size);
2151 do_div(calc_size, stripe_len);
2152 calc_size *= stripe_len;
2154 cur = fs_devices->alloc_list.next;
2157 if (type & BTRFS_BLOCK_GROUP_DUP)
2158 min_free = calc_size * 2;
2160 min_free = calc_size;
2163 * we add 1MB because we never use the first 1MB of the device, unless
2164 * we've looped, then we are likely allocating the maximum amount of
2165 * space left already
2168 min_free += 1024 * 1024;
2170 INIT_LIST_HEAD(&private_devs);
2171 while (index < num_stripes) {
2172 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
2173 BUG_ON(!device->writeable);
2174 if (device->total_bytes > device->bytes_used)
2175 avail = device->total_bytes - device->bytes_used;
2180 if (device->in_fs_metadata && avail >= min_free) {
2181 ret = find_free_dev_extent(trans, device,
2182 min_free, &dev_offset);
2184 list_move_tail(&device->dev_alloc_list,
2186 map->stripes[index].dev = device;
2187 map->stripes[index].physical = dev_offset;
2189 if (type & BTRFS_BLOCK_GROUP_DUP) {
2190 map->stripes[index].dev = device;
2191 map->stripes[index].physical =
2192 dev_offset + calc_size;
2196 } else if (device->in_fs_metadata && avail > max_avail)
2198 if (cur == &fs_devices->alloc_list)
2201 list_splice(&private_devs, &fs_devices->alloc_list);
2202 if (index < num_stripes) {
2203 if (index >= min_stripes) {
2204 num_stripes = index;
2205 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2206 num_stripes /= sub_stripes;
2207 num_stripes *= sub_stripes;
2212 if (!looped && max_avail > 0) {
2214 calc_size = max_avail;
2220 map->sector_size = extent_root->sectorsize;
2221 map->stripe_len = stripe_len;
2222 map->io_align = stripe_len;
2223 map->io_width = stripe_len;
2225 map->num_stripes = num_stripes;
2226 map->sub_stripes = sub_stripes;
2229 *stripe_size = calc_size;
2230 *num_bytes = chunk_bytes_by_type(type, calc_size,
2231 num_stripes, sub_stripes);
2233 em = alloc_extent_map(GFP_NOFS);
2238 em->bdev = (struct block_device *)map;
2240 em->len = *num_bytes;
2241 em->block_start = 0;
2242 em->block_len = em->len;
2244 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
2245 spin_lock(&em_tree->lock);
2246 ret = add_extent_mapping(em_tree, em);
2247 spin_unlock(&em_tree->lock);
2249 free_extent_map(em);
2251 ret = btrfs_make_block_group(trans, extent_root, 0, type,
2252 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2257 while (index < map->num_stripes) {
2258 device = map->stripes[index].dev;
2259 dev_offset = map->stripes[index].physical;
2261 ret = btrfs_alloc_dev_extent(trans, device,
2262 info->chunk_root->root_key.objectid,
2263 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2264 start, dev_offset, calc_size);
2272 static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
2273 struct btrfs_root *extent_root,
2274 struct map_lookup *map, u64 chunk_offset,
2275 u64 chunk_size, u64 stripe_size)
2278 struct btrfs_key key;
2279 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2280 struct btrfs_device *device;
2281 struct btrfs_chunk *chunk;
2282 struct btrfs_stripe *stripe;
2283 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
2287 chunk = kzalloc(item_size, GFP_NOFS);
2292 while (index < map->num_stripes) {
2293 device = map->stripes[index].dev;
2294 device->bytes_used += stripe_size;
2295 ret = btrfs_update_device(trans, device);
2301 stripe = &chunk->stripe;
2302 while (index < map->num_stripes) {
2303 device = map->stripes[index].dev;
2304 dev_offset = map->stripes[index].physical;
2306 btrfs_set_stack_stripe_devid(stripe, device->devid);
2307 btrfs_set_stack_stripe_offset(stripe, dev_offset);
2308 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
2313 btrfs_set_stack_chunk_length(chunk, chunk_size);
2314 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
2315 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
2316 btrfs_set_stack_chunk_type(chunk, map->type);
2317 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
2318 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
2319 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
2320 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
2321 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
2323 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2324 key.type = BTRFS_CHUNK_ITEM_KEY;
2325 key.offset = chunk_offset;
2327 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
2330 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2331 ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
2340 * Chunk allocation falls into two parts. The first part does works
2341 * that make the new allocated chunk useable, but not do any operation
2342 * that modifies the chunk tree. The second part does the works that
2343 * require modifying the chunk tree. This division is important for the
2344 * bootstrap process of adding storage to a seed btrfs.
2346 int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2347 struct btrfs_root *extent_root, u64 type)
2352 struct map_lookup *map;
2353 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2356 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2361 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2362 &stripe_size, chunk_offset, type);
2366 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2367 chunk_size, stripe_size);
2372 static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
2373 struct btrfs_root *root,
2374 struct btrfs_device *device)
2377 u64 sys_chunk_offset;
2381 u64 sys_stripe_size;
2383 struct map_lookup *map;
2384 struct map_lookup *sys_map;
2385 struct btrfs_fs_info *fs_info = root->fs_info;
2386 struct btrfs_root *extent_root = fs_info->extent_root;
2389 ret = find_next_chunk(fs_info->chunk_root,
2390 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
2393 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
2394 (fs_info->metadata_alloc_profile &
2395 fs_info->avail_metadata_alloc_bits);
2396 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2398 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2399 &stripe_size, chunk_offset, alloc_profile);
2402 sys_chunk_offset = chunk_offset + chunk_size;
2404 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
2405 (fs_info->system_alloc_profile &
2406 fs_info->avail_system_alloc_bits);
2407 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2409 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
2410 &sys_chunk_size, &sys_stripe_size,
2411 sys_chunk_offset, alloc_profile);
2414 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
2418 * Modifying chunk tree needs allocating new blocks from both
2419 * system block group and metadata block group. So we only can
2420 * do operations require modifying the chunk tree after both
2421 * block groups were created.
2423 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2424 chunk_size, stripe_size);
2427 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
2428 sys_chunk_offset, sys_chunk_size,
2434 int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
2436 struct extent_map *em;
2437 struct map_lookup *map;
2438 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2442 spin_lock(&map_tree->map_tree.lock);
2443 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
2444 spin_unlock(&map_tree->map_tree.lock);
2448 map = (struct map_lookup *)em->bdev;
2449 for (i = 0; i < map->num_stripes; i++) {
2450 if (!map->stripes[i].dev->writeable) {
2455 free_extent_map(em);
2459 void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
2461 extent_map_tree_init(&tree->map_tree, GFP_NOFS);
2464 void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
2466 struct extent_map *em;
2469 spin_lock(&tree->map_tree.lock);
2470 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
2472 remove_extent_mapping(&tree->map_tree, em);
2473 spin_unlock(&tree->map_tree.lock);
2478 free_extent_map(em);
2479 /* once for the tree */
2480 free_extent_map(em);
2484 int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
2486 struct extent_map *em;
2487 struct map_lookup *map;
2488 struct extent_map_tree *em_tree = &map_tree->map_tree;
2491 spin_lock(&em_tree->lock);
2492 em = lookup_extent_mapping(em_tree, logical, len);
2493 spin_unlock(&em_tree->lock);
2496 BUG_ON(em->start > logical || em->start + em->len < logical);
2497 map = (struct map_lookup *)em->bdev;
2498 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
2499 ret = map->num_stripes;
2500 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2501 ret = map->sub_stripes;
2504 free_extent_map(em);
2508 static int find_live_mirror(struct map_lookup *map, int first, int num,
2512 if (map->stripes[optimal].dev->bdev)
2514 for (i = first; i < first + num; i++) {
2515 if (map->stripes[i].dev->bdev)
2518 /* we couldn't find one that doesn't fail. Just return something
2519 * and the io error handling code will clean up eventually
2524 static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2525 u64 logical, u64 *length,
2526 struct btrfs_multi_bio **multi_ret,
2527 int mirror_num, struct page *unplug_page)
2529 struct extent_map *em;
2530 struct map_lookup *map;
2531 struct extent_map_tree *em_tree = &map_tree->map_tree;
2535 int stripes_allocated = 8;
2536 int stripes_required = 1;
2541 struct btrfs_multi_bio *multi = NULL;
2543 if (multi_ret && !(rw & (1 << BIO_RW)))
2544 stripes_allocated = 1;
2547 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
2552 atomic_set(&multi->error, 0);
2555 spin_lock(&em_tree->lock);
2556 em = lookup_extent_mapping(em_tree, logical, *length);
2557 spin_unlock(&em_tree->lock);
2559 if (!em && unplug_page)
2563 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
2564 (unsigned long long)logical,
2565 (unsigned long long)*length);
2569 BUG_ON(em->start > logical || em->start + em->len < logical);
2570 map = (struct map_lookup *)em->bdev;
2571 offset = logical - em->start;
2573 if (mirror_num > map->num_stripes)
2576 /* if our multi bio struct is too small, back off and try again */
2577 if (rw & (1 << BIO_RW)) {
2578 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
2579 BTRFS_BLOCK_GROUP_DUP)) {
2580 stripes_required = map->num_stripes;
2582 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2583 stripes_required = map->sub_stripes;
2587 if (multi_ret && (rw & (1 << BIO_RW)) &&
2588 stripes_allocated < stripes_required) {
2589 stripes_allocated = map->num_stripes;
2590 free_extent_map(em);
2596 * stripe_nr counts the total number of stripes we have to stride
2597 * to get to this block
2599 do_div(stripe_nr, map->stripe_len);
2601 stripe_offset = stripe_nr * map->stripe_len;
2602 BUG_ON(offset < stripe_offset);
2604 /* stripe_offset is the offset of this block in its stripe*/
2605 stripe_offset = offset - stripe_offset;
2607 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
2608 BTRFS_BLOCK_GROUP_RAID10 |
2609 BTRFS_BLOCK_GROUP_DUP)) {
2610 /* we limit the length of each bio to what fits in a stripe */
2611 *length = min_t(u64, em->len - offset,
2612 map->stripe_len - stripe_offset);
2614 *length = em->len - offset;
2617 if (!multi_ret && !unplug_page)
2622 if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
2623 if (unplug_page || (rw & (1 << BIO_RW)))
2624 num_stripes = map->num_stripes;
2625 else if (mirror_num)
2626 stripe_index = mirror_num - 1;
2628 stripe_index = find_live_mirror(map, 0,
2630 current->pid % map->num_stripes);
2633 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
2634 if (rw & (1 << BIO_RW))
2635 num_stripes = map->num_stripes;
2636 else if (mirror_num)
2637 stripe_index = mirror_num - 1;
2639 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2640 int factor = map->num_stripes / map->sub_stripes;
2642 stripe_index = do_div(stripe_nr, factor);
2643 stripe_index *= map->sub_stripes;
2645 if (unplug_page || (rw & (1 << BIO_RW)))
2646 num_stripes = map->sub_stripes;
2647 else if (mirror_num)
2648 stripe_index += mirror_num - 1;
2650 stripe_index = find_live_mirror(map, stripe_index,
2651 map->sub_stripes, stripe_index +
2652 current->pid % map->sub_stripes);
2656 * after this do_div call, stripe_nr is the number of stripes
2657 * on this device we have to walk to find the data, and
2658 * stripe_index is the number of our device in the stripe array
2660 stripe_index = do_div(stripe_nr, map->num_stripes);
2662 BUG_ON(stripe_index >= map->num_stripes);
2664 for (i = 0; i < num_stripes; i++) {
2666 struct btrfs_device *device;
2667 struct backing_dev_info *bdi;
2669 device = map->stripes[stripe_index].dev;
2671 bdi = blk_get_backing_dev_info(device->bdev);
2672 if (bdi->unplug_io_fn)
2673 bdi->unplug_io_fn(bdi, unplug_page);
2676 multi->stripes[i].physical =
2677 map->stripes[stripe_index].physical +
2678 stripe_offset + stripe_nr * map->stripe_len;
2679 multi->stripes[i].dev = map->stripes[stripe_index].dev;
2685 multi->num_stripes = num_stripes;
2686 multi->max_errors = max_errors;
2689 free_extent_map(em);
2693 int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2694 u64 logical, u64 *length,
2695 struct btrfs_multi_bio **multi_ret, int mirror_num)
2697 return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
2701 int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
2702 u64 chunk_start, u64 physical, u64 devid,
2703 u64 **logical, int *naddrs, int *stripe_len)
2705 struct extent_map_tree *em_tree = &map_tree->map_tree;
2706 struct extent_map *em;
2707 struct map_lookup *map;
2714 spin_lock(&em_tree->lock);
2715 em = lookup_extent_mapping(em_tree, chunk_start, 1);
2716 spin_unlock(&em_tree->lock);
2718 BUG_ON(!em || em->start != chunk_start);
2719 map = (struct map_lookup *)em->bdev;
2722 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2723 do_div(length, map->num_stripes / map->sub_stripes);
2724 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
2725 do_div(length, map->num_stripes);
2727 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
2730 for (i = 0; i < map->num_stripes; i++) {
2731 if (devid && map->stripes[i].dev->devid != devid)
2733 if (map->stripes[i].physical > physical ||
2734 map->stripes[i].physical + length <= physical)
2737 stripe_nr = physical - map->stripes[i].physical;
2738 do_div(stripe_nr, map->stripe_len);
2740 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2741 stripe_nr = stripe_nr * map->num_stripes + i;
2742 do_div(stripe_nr, map->sub_stripes);
2743 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
2744 stripe_nr = stripe_nr * map->num_stripes + i;
2746 bytenr = chunk_start + stripe_nr * map->stripe_len;
2747 WARN_ON(nr >= map->num_stripes);
2748 for (j = 0; j < nr; j++) {
2749 if (buf[j] == bytenr)
2753 WARN_ON(nr >= map->num_stripes);
2758 for (i = 0; i > nr; i++) {
2759 struct btrfs_multi_bio *multi;
2760 struct btrfs_bio_stripe *stripe;
2764 ret = btrfs_map_block(map_tree, WRITE, buf[i],
2765 &length, &multi, 0);
2768 stripe = multi->stripes;
2769 for (j = 0; j < multi->num_stripes; j++) {
2770 if (stripe->physical >= physical &&
2771 physical < stripe->physical + length)
2774 BUG_ON(j >= multi->num_stripes);
2780 *stripe_len = map->stripe_len;
2782 free_extent_map(em);
2786 int btrfs_unplug_page(struct btrfs_mapping_tree *map_tree,
2787 u64 logical, struct page *page)
2789 u64 length = PAGE_CACHE_SIZE;
2790 return __btrfs_map_block(map_tree, READ, logical, &length,
2794 static void end_bio_multi_stripe(struct bio *bio, int err)
2796 struct btrfs_multi_bio *multi = bio->bi_private;
2797 int is_orig_bio = 0;
2800 atomic_inc(&multi->error);
2802 if (bio == multi->orig_bio)
2805 if (atomic_dec_and_test(&multi->stripes_pending)) {
2808 bio = multi->orig_bio;
2810 bio->bi_private = multi->private;
2811 bio->bi_end_io = multi->end_io;
2812 /* only send an error to the higher layers if it is
2813 * beyond the tolerance of the multi-bio
2815 if (atomic_read(&multi->error) > multi->max_errors) {
2819 * this bio is actually up to date, we didn't
2820 * go over the max number of errors
2822 set_bit(BIO_UPTODATE, &bio->bi_flags);
2827 bio_endio(bio, err);
2828 } else if (!is_orig_bio) {
2833 struct async_sched {
2836 struct btrfs_fs_info *info;
2837 struct btrfs_work work;
2841 * see run_scheduled_bios for a description of why bios are collected for
2844 * This will add one bio to the pending list for a device and make sure
2845 * the work struct is scheduled.
2847 static noinline int schedule_bio(struct btrfs_root *root,
2848 struct btrfs_device *device,
2849 int rw, struct bio *bio)
2851 int should_queue = 1;
2852 struct btrfs_pending_bios *pending_bios;
2854 /* don't bother with additional async steps for reads, right now */
2855 if (!(rw & (1 << BIO_RW))) {
2857 submit_bio(rw, bio);
2863 * nr_async_bios allows us to reliably return congestion to the
2864 * higher layers. Otherwise, the async bio makes it appear we have
2865 * made progress against dirty pages when we've really just put it
2866 * on a queue for later
2868 atomic_inc(&root->fs_info->nr_async_bios);
2869 WARN_ON(bio->bi_next);
2870 bio->bi_next = NULL;
2873 spin_lock(&device->io_lock);
2875 pending_bios = &device->pending_sync_bios;
2877 pending_bios = &device->pending_bios;
2879 if (pending_bios->tail)
2880 pending_bios->tail->bi_next = bio;
2882 pending_bios->tail = bio;
2883 if (!pending_bios->head)
2884 pending_bios->head = bio;
2885 if (device->running_pending)
2888 spin_unlock(&device->io_lock);
2891 btrfs_queue_worker(&root->fs_info->submit_workers,
2896 int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
2897 int mirror_num, int async_submit)
2899 struct btrfs_mapping_tree *map_tree;
2900 struct btrfs_device *dev;
2901 struct bio *first_bio = bio;
2902 u64 logical = (u64)bio->bi_sector << 9;
2905 struct btrfs_multi_bio *multi = NULL;
2910 length = bio->bi_size;
2911 map_tree = &root->fs_info->mapping_tree;
2912 map_length = length;
2914 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
2918 total_devs = multi->num_stripes;
2919 if (map_length < length) {
2920 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
2921 "len %llu\n", (unsigned long long)logical,
2922 (unsigned long long)length,
2923 (unsigned long long)map_length);
2926 multi->end_io = first_bio->bi_end_io;
2927 multi->private = first_bio->bi_private;
2928 multi->orig_bio = first_bio;
2929 atomic_set(&multi->stripes_pending, multi->num_stripes);
2931 while (dev_nr < total_devs) {
2932 if (total_devs > 1) {
2933 if (dev_nr < total_devs - 1) {
2934 bio = bio_clone(first_bio, GFP_NOFS);
2939 bio->bi_private = multi;
2940 bio->bi_end_io = end_bio_multi_stripe;
2942 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
2943 dev = multi->stripes[dev_nr].dev;
2944 BUG_ON(rw == WRITE && !dev->writeable);
2945 if (dev && dev->bdev) {
2946 bio->bi_bdev = dev->bdev;
2948 schedule_bio(root, dev, rw, bio);
2950 submit_bio(rw, bio);
2952 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
2953 bio->bi_sector = logical >> 9;
2954 bio_endio(bio, -EIO);
2958 if (total_devs == 1)
2963 struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
2966 struct btrfs_device *device;
2967 struct btrfs_fs_devices *cur_devices;
2969 cur_devices = root->fs_info->fs_devices;
2970 while (cur_devices) {
2972 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
2973 device = __find_device(&cur_devices->devices,
2978 cur_devices = cur_devices->seed;
2983 static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
2984 u64 devid, u8 *dev_uuid)
2986 struct btrfs_device *device;
2987 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2989 device = kzalloc(sizeof(*device), GFP_NOFS);
2992 list_add(&device->dev_list,
2993 &fs_devices->devices);
2994 device->barriers = 1;
2995 device->dev_root = root->fs_info->dev_root;
2996 device->devid = devid;
2997 device->work.func = pending_bios_fn;
2998 device->fs_devices = fs_devices;
2999 fs_devices->num_devices++;
3000 spin_lock_init(&device->io_lock);
3001 INIT_LIST_HEAD(&device->dev_alloc_list);
3002 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
3006 static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
3007 struct extent_buffer *leaf,
3008 struct btrfs_chunk *chunk)
3010 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3011 struct map_lookup *map;
3012 struct extent_map *em;
3016 u8 uuid[BTRFS_UUID_SIZE];
3021 logical = key->offset;
3022 length = btrfs_chunk_length(leaf, chunk);
3024 spin_lock(&map_tree->map_tree.lock);
3025 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
3026 spin_unlock(&map_tree->map_tree.lock);
3028 /* already mapped? */
3029 if (em && em->start <= logical && em->start + em->len > logical) {
3030 free_extent_map(em);
3033 free_extent_map(em);
3036 em = alloc_extent_map(GFP_NOFS);
3039 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3040 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
3042 free_extent_map(em);
3046 em->bdev = (struct block_device *)map;
3047 em->start = logical;
3049 em->block_start = 0;
3050 em->block_len = em->len;
3052 map->num_stripes = num_stripes;
3053 map->io_width = btrfs_chunk_io_width(leaf, chunk);
3054 map->io_align = btrfs_chunk_io_align(leaf, chunk);
3055 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
3056 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
3057 map->type = btrfs_chunk_type(leaf, chunk);
3058 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
3059 for (i = 0; i < num_stripes; i++) {
3060 map->stripes[i].physical =
3061 btrfs_stripe_offset_nr(leaf, chunk, i);
3062 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
3063 read_extent_buffer(leaf, uuid, (unsigned long)
3064 btrfs_stripe_dev_uuid_nr(chunk, i),
3066 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
3068 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
3070 free_extent_map(em);
3073 if (!map->stripes[i].dev) {
3074 map->stripes[i].dev =
3075 add_missing_dev(root, devid, uuid);
3076 if (!map->stripes[i].dev) {
3078 free_extent_map(em);
3082 map->stripes[i].dev->in_fs_metadata = 1;
3085 spin_lock(&map_tree->map_tree.lock);
3086 ret = add_extent_mapping(&map_tree->map_tree, em);
3087 spin_unlock(&map_tree->map_tree.lock);
3089 free_extent_map(em);
3094 static int fill_device_from_item(struct extent_buffer *leaf,
3095 struct btrfs_dev_item *dev_item,
3096 struct btrfs_device *device)
3100 device->devid = btrfs_device_id(leaf, dev_item);
3101 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
3102 device->total_bytes = device->disk_total_bytes;
3103 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
3104 device->type = btrfs_device_type(leaf, dev_item);
3105 device->io_align = btrfs_device_io_align(leaf, dev_item);
3106 device->io_width = btrfs_device_io_width(leaf, dev_item);
3107 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
3109 ptr = (unsigned long)btrfs_device_uuid(dev_item);
3110 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
3115 static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
3117 struct btrfs_fs_devices *fs_devices;
3120 mutex_lock(&uuid_mutex);
3122 fs_devices = root->fs_info->fs_devices->seed;
3123 while (fs_devices) {
3124 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3128 fs_devices = fs_devices->seed;
3131 fs_devices = find_fsid(fsid);
3137 fs_devices = clone_fs_devices(fs_devices);
3138 if (IS_ERR(fs_devices)) {
3139 ret = PTR_ERR(fs_devices);
3143 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
3144 root->fs_info->bdev_holder);
3148 if (!fs_devices->seeding) {
3149 __btrfs_close_devices(fs_devices);
3150 free_fs_devices(fs_devices);
3155 fs_devices->seed = root->fs_info->fs_devices->seed;
3156 root->fs_info->fs_devices->seed = fs_devices;
3158 mutex_unlock(&uuid_mutex);
3162 static int read_one_dev(struct btrfs_root *root,
3163 struct extent_buffer *leaf,
3164 struct btrfs_dev_item *dev_item)
3166 struct btrfs_device *device;
3169 u8 fs_uuid[BTRFS_UUID_SIZE];
3170 u8 dev_uuid[BTRFS_UUID_SIZE];
3172 devid = btrfs_device_id(leaf, dev_item);
3173 read_extent_buffer(leaf, dev_uuid,
3174 (unsigned long)btrfs_device_uuid(dev_item),
3176 read_extent_buffer(leaf, fs_uuid,
3177 (unsigned long)btrfs_device_fsid(dev_item),
3180 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
3181 ret = open_seed_devices(root, fs_uuid);
3182 if (ret && !btrfs_test_opt(root, DEGRADED))
3186 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
3187 if (!device || !device->bdev) {
3188 if (!btrfs_test_opt(root, DEGRADED))
3192 printk(KERN_WARNING "warning devid %llu missing\n",
3193 (unsigned long long)devid);
3194 device = add_missing_dev(root, devid, dev_uuid);
3200 if (device->fs_devices != root->fs_info->fs_devices) {
3201 BUG_ON(device->writeable);
3202 if (device->generation !=
3203 btrfs_device_generation(leaf, dev_item))
3207 fill_device_from_item(leaf, dev_item, device);
3208 device->dev_root = root->fs_info->dev_root;
3209 device->in_fs_metadata = 1;
3210 if (device->writeable)
3211 device->fs_devices->total_rw_bytes += device->total_bytes;
3216 int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
3218 struct btrfs_dev_item *dev_item;
3220 dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
3222 return read_one_dev(root, buf, dev_item);
3225 int btrfs_read_sys_array(struct btrfs_root *root)
3227 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
3228 struct extent_buffer *sb;
3229 struct btrfs_disk_key *disk_key;
3230 struct btrfs_chunk *chunk;
3232 unsigned long sb_ptr;
3238 struct btrfs_key key;
3240 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
3241 BTRFS_SUPER_INFO_SIZE);
3244 btrfs_set_buffer_uptodate(sb);
3245 btrfs_set_buffer_lockdep_class(sb, 0);
3247 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
3248 array_size = btrfs_super_sys_array_size(super_copy);
3250 ptr = super_copy->sys_chunk_array;
3251 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
3254 while (cur < array_size) {
3255 disk_key = (struct btrfs_disk_key *)ptr;
3256 btrfs_disk_key_to_cpu(&key, disk_key);
3258 len = sizeof(*disk_key); ptr += len;
3262 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
3263 chunk = (struct btrfs_chunk *)sb_ptr;
3264 ret = read_one_chunk(root, &key, sb, chunk);
3267 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
3268 len = btrfs_chunk_item_size(num_stripes);
3277 free_extent_buffer(sb);
3281 int btrfs_read_chunk_tree(struct btrfs_root *root)
3283 struct btrfs_path *path;
3284 struct extent_buffer *leaf;
3285 struct btrfs_key key;
3286 struct btrfs_key found_key;
3290 root = root->fs_info->chunk_root;
3292 path = btrfs_alloc_path();
3296 /* first we search for all of the device items, and then we
3297 * read in all of the chunk items. This way we can create chunk
3298 * mappings that reference all of the devices that are afound
3300 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
3304 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3306 leaf = path->nodes[0];
3307 slot = path->slots[0];
3308 if (slot >= btrfs_header_nritems(leaf)) {
3309 ret = btrfs_next_leaf(root, path);
3316 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3317 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3318 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
3320 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
3321 struct btrfs_dev_item *dev_item;
3322 dev_item = btrfs_item_ptr(leaf, slot,
3323 struct btrfs_dev_item);
3324 ret = read_one_dev(root, leaf, dev_item);
3328 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
3329 struct btrfs_chunk *chunk;
3330 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3331 ret = read_one_chunk(root, &found_key, leaf, chunk);
3337 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3339 btrfs_release_path(root, path);
3344 btrfs_free_path(path);