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 <asm/div64.h>
28 #include "extent_map.h"
30 #include "transaction.h"
31 #include "print-tree.h"
33 #include "async-thread.h"
43 struct btrfs_bio_stripe stripes[];
46 static int init_first_rw_device(struct btrfs_trans_handle *trans,
47 struct btrfs_root *root,
48 struct btrfs_device *device);
49 static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
51 #define map_lookup_size(n) (sizeof(struct map_lookup) + \
52 (sizeof(struct btrfs_bio_stripe) * (n)))
54 static DEFINE_MUTEX(uuid_mutex);
55 static LIST_HEAD(fs_uuids);
57 void btrfs_lock_volumes(void)
59 mutex_lock(&uuid_mutex);
62 void btrfs_unlock_volumes(void)
64 mutex_unlock(&uuid_mutex);
67 static void lock_chunks(struct btrfs_root *root)
69 mutex_lock(&root->fs_info->chunk_mutex);
72 static void unlock_chunks(struct btrfs_root *root)
74 mutex_unlock(&root->fs_info->chunk_mutex);
77 static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
79 struct btrfs_device *device;
80 WARN_ON(fs_devices->opened);
81 while (!list_empty(&fs_devices->devices)) {
82 device = list_entry(fs_devices->devices.next,
83 struct btrfs_device, dev_list);
84 list_del(&device->dev_list);
91 int btrfs_cleanup_fs_uuids(void)
93 struct btrfs_fs_devices *fs_devices;
95 while (!list_empty(&fs_uuids)) {
96 fs_devices = list_entry(fs_uuids.next,
97 struct btrfs_fs_devices, list);
98 list_del(&fs_devices->list);
99 free_fs_devices(fs_devices);
104 static noinline struct btrfs_device *__find_device(struct list_head *head,
107 struct btrfs_device *dev;
109 list_for_each_entry(dev, head, dev_list) {
110 if (dev->devid == devid &&
111 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
118 static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
120 struct btrfs_fs_devices *fs_devices;
122 list_for_each_entry(fs_devices, &fs_uuids, list) {
123 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
129 static void requeue_list(struct btrfs_pending_bios *pending_bios,
130 struct bio *head, struct bio *tail)
133 struct bio *old_head;
135 old_head = pending_bios->head;
136 pending_bios->head = head;
137 if (pending_bios->tail)
138 tail->bi_next = old_head;
140 pending_bios->tail = tail;
144 * we try to collect pending bios for a device so we don't get a large
145 * number of procs sending bios down to the same device. This greatly
146 * improves the schedulers ability to collect and merge the bios.
148 * But, it also turns into a long list of bios to process and that is sure
149 * to eventually make the worker thread block. The solution here is to
150 * make some progress and then put this work struct back at the end of
151 * the list if the block device is congested. This way, multiple devices
152 * can make progress from a single worker thread.
154 static noinline int run_scheduled_bios(struct btrfs_device *device)
157 struct backing_dev_info *bdi;
158 struct btrfs_fs_info *fs_info;
159 struct btrfs_pending_bios *pending_bios;
163 unsigned long num_run;
164 unsigned long num_sync_run;
165 unsigned long batch_run = 0;
167 unsigned long last_waited = 0;
170 bdi = blk_get_backing_dev_info(device->bdev);
171 fs_info = device->dev_root->fs_info;
172 limit = btrfs_async_submit_limit(fs_info);
173 limit = limit * 2 / 3;
175 /* we want to make sure that every time we switch from the sync
176 * list to the normal list, we unplug
181 spin_lock(&device->io_lock);
186 /* take all the bios off the list at once and process them
187 * later on (without the lock held). But, remember the
188 * tail and other pointers so the bios can be properly reinserted
189 * into the list if we hit congestion
191 if (!force_reg && device->pending_sync_bios.head) {
192 pending_bios = &device->pending_sync_bios;
195 pending_bios = &device->pending_bios;
199 pending = pending_bios->head;
200 tail = pending_bios->tail;
201 WARN_ON(pending && !tail);
204 * if pending was null this time around, no bios need processing
205 * at all and we can stop. Otherwise it'll loop back up again
206 * and do an additional check so no bios are missed.
208 * device->running_pending is used to synchronize with the
211 if (device->pending_sync_bios.head == NULL &&
212 device->pending_bios.head == NULL) {
214 device->running_pending = 0;
217 device->running_pending = 1;
220 pending_bios->head = NULL;
221 pending_bios->tail = NULL;
223 spin_unlock(&device->io_lock);
226 * if we're doing the regular priority list, make sure we unplug
227 * for any high prio bios we've sent down
229 if (pending_bios == &device->pending_bios && num_sync_run > 0) {
231 blk_run_backing_dev(bdi, NULL);
237 /* we want to work on both lists, but do more bios on the
238 * sync list than the regular list
241 pending_bios != &device->pending_sync_bios &&
242 device->pending_sync_bios.head) ||
243 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
244 device->pending_bios.head)) {
245 spin_lock(&device->io_lock);
246 requeue_list(pending_bios, pending, tail);
251 pending = pending->bi_next;
253 atomic_dec(&fs_info->nr_async_bios);
255 if (atomic_read(&fs_info->nr_async_bios) < limit &&
256 waitqueue_active(&fs_info->async_submit_wait))
257 wake_up(&fs_info->async_submit_wait);
259 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
261 if (bio_rw_flagged(cur, BIO_RW_SYNCIO))
264 submit_bio(cur->bi_rw, cur);
267 if (need_resched()) {
269 blk_run_backing_dev(bdi, NULL);
276 * we made progress, there is more work to do and the bdi
277 * is now congested. Back off and let other work structs
280 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
281 fs_info->fs_devices->open_devices > 1) {
282 struct io_context *ioc;
284 ioc = current->io_context;
287 * the main goal here is that we don't want to
288 * block if we're going to be able to submit
289 * more requests without blocking.
291 * This code does two great things, it pokes into
292 * the elevator code from a filesystem _and_
293 * it makes assumptions about how batching works.
295 if (ioc && ioc->nr_batch_requests > 0 &&
296 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
298 ioc->last_waited == last_waited)) {
300 * we want to go through our batch of
301 * requests and stop. So, we copy out
302 * the ioc->last_waited time and test
303 * against it before looping
305 last_waited = ioc->last_waited;
306 if (need_resched()) {
308 blk_run_backing_dev(bdi, NULL);
315 spin_lock(&device->io_lock);
316 requeue_list(pending_bios, pending, tail);
317 device->running_pending = 1;
319 spin_unlock(&device->io_lock);
320 btrfs_requeue_work(&device->work);
327 blk_run_backing_dev(bdi, NULL);
330 * IO has already been through a long path to get here. Checksumming,
331 * async helper threads, perhaps compression. We've done a pretty
332 * good job of collecting a batch of IO and should just unplug
333 * the device right away.
335 * This will help anyone who is waiting on the IO, they might have
336 * already unplugged, but managed to do so before the bio they
337 * cared about found its way down here.
339 blk_run_backing_dev(bdi, NULL);
345 spin_lock(&device->io_lock);
346 if (device->pending_bios.head || device->pending_sync_bios.head)
348 spin_unlock(&device->io_lock);
354 static void pending_bios_fn(struct btrfs_work *work)
356 struct btrfs_device *device;
358 device = container_of(work, struct btrfs_device, work);
359 run_scheduled_bios(device);
362 static noinline int device_list_add(const char *path,
363 struct btrfs_super_block *disk_super,
364 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
366 struct btrfs_device *device;
367 struct btrfs_fs_devices *fs_devices;
368 u64 found_transid = btrfs_super_generation(disk_super);
371 fs_devices = find_fsid(disk_super->fsid);
373 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
376 INIT_LIST_HEAD(&fs_devices->devices);
377 INIT_LIST_HEAD(&fs_devices->alloc_list);
378 list_add(&fs_devices->list, &fs_uuids);
379 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
380 fs_devices->latest_devid = devid;
381 fs_devices->latest_trans = found_transid;
382 mutex_init(&fs_devices->device_list_mutex);
385 device = __find_device(&fs_devices->devices, devid,
386 disk_super->dev_item.uuid);
389 if (fs_devices->opened)
392 device = kzalloc(sizeof(*device), GFP_NOFS);
394 /* we can safely leave the fs_devices entry around */
397 device->devid = devid;
398 device->work.func = pending_bios_fn;
399 memcpy(device->uuid, disk_super->dev_item.uuid,
401 device->barriers = 1;
402 spin_lock_init(&device->io_lock);
403 device->name = kstrdup(path, GFP_NOFS);
408 INIT_LIST_HEAD(&device->dev_alloc_list);
410 mutex_lock(&fs_devices->device_list_mutex);
411 list_add(&device->dev_list, &fs_devices->devices);
412 mutex_unlock(&fs_devices->device_list_mutex);
414 device->fs_devices = fs_devices;
415 fs_devices->num_devices++;
416 } else if (strcmp(device->name, path)) {
417 name = kstrdup(path, GFP_NOFS);
424 if (found_transid > fs_devices->latest_trans) {
425 fs_devices->latest_devid = devid;
426 fs_devices->latest_trans = found_transid;
428 *fs_devices_ret = fs_devices;
432 static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
434 struct btrfs_fs_devices *fs_devices;
435 struct btrfs_device *device;
436 struct btrfs_device *orig_dev;
438 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
440 return ERR_PTR(-ENOMEM);
442 INIT_LIST_HEAD(&fs_devices->devices);
443 INIT_LIST_HEAD(&fs_devices->alloc_list);
444 INIT_LIST_HEAD(&fs_devices->list);
445 mutex_init(&fs_devices->device_list_mutex);
446 fs_devices->latest_devid = orig->latest_devid;
447 fs_devices->latest_trans = orig->latest_trans;
448 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
450 mutex_lock(&orig->device_list_mutex);
451 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
452 device = kzalloc(sizeof(*device), GFP_NOFS);
456 device->name = kstrdup(orig_dev->name, GFP_NOFS);
462 device->devid = orig_dev->devid;
463 device->work.func = pending_bios_fn;
464 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
465 device->barriers = 1;
466 spin_lock_init(&device->io_lock);
467 INIT_LIST_HEAD(&device->dev_list);
468 INIT_LIST_HEAD(&device->dev_alloc_list);
470 list_add(&device->dev_list, &fs_devices->devices);
471 device->fs_devices = fs_devices;
472 fs_devices->num_devices++;
474 mutex_unlock(&orig->device_list_mutex);
477 mutex_unlock(&orig->device_list_mutex);
478 free_fs_devices(fs_devices);
479 return ERR_PTR(-ENOMEM);
482 int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
484 struct btrfs_device *device, *next;
486 mutex_lock(&uuid_mutex);
488 mutex_lock(&fs_devices->device_list_mutex);
489 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
490 if (device->in_fs_metadata)
494 close_bdev_exclusive(device->bdev, device->mode);
496 fs_devices->open_devices--;
498 if (device->writeable) {
499 list_del_init(&device->dev_alloc_list);
500 device->writeable = 0;
501 fs_devices->rw_devices--;
503 list_del_init(&device->dev_list);
504 fs_devices->num_devices--;
508 mutex_unlock(&fs_devices->device_list_mutex);
510 if (fs_devices->seed) {
511 fs_devices = fs_devices->seed;
515 mutex_unlock(&uuid_mutex);
519 static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
521 struct btrfs_device *device;
523 if (--fs_devices->opened > 0)
526 list_for_each_entry(device, &fs_devices->devices, dev_list) {
528 close_bdev_exclusive(device->bdev, device->mode);
529 fs_devices->open_devices--;
531 if (device->writeable) {
532 list_del_init(&device->dev_alloc_list);
533 fs_devices->rw_devices--;
537 device->writeable = 0;
538 device->in_fs_metadata = 0;
540 WARN_ON(fs_devices->open_devices);
541 WARN_ON(fs_devices->rw_devices);
542 fs_devices->opened = 0;
543 fs_devices->seeding = 0;
548 int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
550 struct btrfs_fs_devices *seed_devices = NULL;
553 mutex_lock(&uuid_mutex);
554 ret = __btrfs_close_devices(fs_devices);
555 if (!fs_devices->opened) {
556 seed_devices = fs_devices->seed;
557 fs_devices->seed = NULL;
559 mutex_unlock(&uuid_mutex);
561 while (seed_devices) {
562 fs_devices = seed_devices;
563 seed_devices = fs_devices->seed;
564 __btrfs_close_devices(fs_devices);
565 free_fs_devices(fs_devices);
570 static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
571 fmode_t flags, void *holder)
573 struct block_device *bdev;
574 struct list_head *head = &fs_devices->devices;
575 struct btrfs_device *device;
576 struct block_device *latest_bdev = NULL;
577 struct buffer_head *bh;
578 struct btrfs_super_block *disk_super;
579 u64 latest_devid = 0;
580 u64 latest_transid = 0;
585 list_for_each_entry(device, head, dev_list) {
591 bdev = open_bdev_exclusive(device->name, flags, holder);
593 printk(KERN_INFO "open %s failed\n", device->name);
596 set_blocksize(bdev, 4096);
598 bh = btrfs_read_dev_super(bdev);
602 disk_super = (struct btrfs_super_block *)bh->b_data;
603 devid = btrfs_stack_device_id(&disk_super->dev_item);
604 if (devid != device->devid)
607 if (memcmp(device->uuid, disk_super->dev_item.uuid,
611 device->generation = btrfs_super_generation(disk_super);
612 if (!latest_transid || device->generation > latest_transid) {
613 latest_devid = devid;
614 latest_transid = device->generation;
618 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
619 device->writeable = 0;
621 device->writeable = !bdev_read_only(bdev);
626 device->in_fs_metadata = 0;
627 device->mode = flags;
629 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
630 fs_devices->rotating = 1;
632 fs_devices->open_devices++;
633 if (device->writeable) {
634 fs_devices->rw_devices++;
635 list_add(&device->dev_alloc_list,
636 &fs_devices->alloc_list);
643 close_bdev_exclusive(bdev, FMODE_READ);
647 if (fs_devices->open_devices == 0) {
651 fs_devices->seeding = seeding;
652 fs_devices->opened = 1;
653 fs_devices->latest_bdev = latest_bdev;
654 fs_devices->latest_devid = latest_devid;
655 fs_devices->latest_trans = latest_transid;
656 fs_devices->total_rw_bytes = 0;
661 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
662 fmode_t flags, void *holder)
666 mutex_lock(&uuid_mutex);
667 if (fs_devices->opened) {
668 fs_devices->opened++;
671 ret = __btrfs_open_devices(fs_devices, flags, holder);
673 mutex_unlock(&uuid_mutex);
677 int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
678 struct btrfs_fs_devices **fs_devices_ret)
680 struct btrfs_super_block *disk_super;
681 struct block_device *bdev;
682 struct buffer_head *bh;
687 mutex_lock(&uuid_mutex);
689 bdev = open_bdev_exclusive(path, flags, holder);
696 ret = set_blocksize(bdev, 4096);
699 bh = btrfs_read_dev_super(bdev);
704 disk_super = (struct btrfs_super_block *)bh->b_data;
705 devid = btrfs_stack_device_id(&disk_super->dev_item);
706 transid = btrfs_super_generation(disk_super);
707 if (disk_super->label[0])
708 printk(KERN_INFO "device label %s ", disk_super->label);
710 /* FIXME, make a readl uuid parser */
711 printk(KERN_INFO "device fsid %llx-%llx ",
712 *(unsigned long long *)disk_super->fsid,
713 *(unsigned long long *)(disk_super->fsid + 8));
715 printk(KERN_CONT "devid %llu transid %llu %s\n",
716 (unsigned long long)devid, (unsigned long long)transid, path);
717 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
721 close_bdev_exclusive(bdev, flags);
723 mutex_unlock(&uuid_mutex);
728 * this uses a pretty simple search, the expectation is that it is
729 * called very infrequently and that a given device has a small number
732 int find_free_dev_extent(struct btrfs_trans_handle *trans,
733 struct btrfs_device *device, u64 num_bytes,
734 u64 *start, u64 *max_avail)
736 struct btrfs_key key;
737 struct btrfs_root *root = device->dev_root;
738 struct btrfs_dev_extent *dev_extent = NULL;
739 struct btrfs_path *path;
742 u64 search_start = 0;
743 u64 search_end = device->total_bytes;
747 struct extent_buffer *l;
749 path = btrfs_alloc_path();
755 /* FIXME use last free of some kind */
757 /* we don't want to overwrite the superblock on the drive,
758 * so we make sure to start at an offset of at least 1MB
760 search_start = max((u64)1024 * 1024, search_start);
762 if (root->fs_info->alloc_start + num_bytes <= device->total_bytes)
763 search_start = max(root->fs_info->alloc_start, search_start);
765 key.objectid = device->devid;
766 key.offset = search_start;
767 key.type = BTRFS_DEV_EXTENT_KEY;
768 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
772 ret = btrfs_previous_item(root, path, key.objectid, key.type);
779 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
782 slot = path->slots[0];
783 if (slot >= btrfs_header_nritems(l)) {
784 ret = btrfs_next_leaf(root, path);
791 if (search_start >= search_end) {
795 *start = search_start;
799 *start = last_byte > search_start ?
800 last_byte : search_start;
801 if (search_end <= *start) {
807 btrfs_item_key_to_cpu(l, &key, slot);
809 if (key.objectid < device->devid)
812 if (key.objectid > device->devid)
815 if (key.offset >= search_start && key.offset > last_byte &&
817 if (last_byte < search_start)
818 last_byte = search_start;
819 hole_size = key.offset - last_byte;
821 if (hole_size > *max_avail)
822 *max_avail = hole_size;
824 if (key.offset > last_byte &&
825 hole_size >= num_bytes) {
830 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
834 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
835 last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent);
841 /* we have to make sure we didn't find an extent that has already
842 * been allocated by the map tree or the original allocation
844 BUG_ON(*start < search_start);
846 if (*start + num_bytes > search_end) {
850 /* check for pending inserts here */
854 btrfs_free_path(path);
858 static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
859 struct btrfs_device *device,
863 struct btrfs_path *path;
864 struct btrfs_root *root = device->dev_root;
865 struct btrfs_key key;
866 struct btrfs_key found_key;
867 struct extent_buffer *leaf = NULL;
868 struct btrfs_dev_extent *extent = NULL;
870 path = btrfs_alloc_path();
874 key.objectid = device->devid;
876 key.type = BTRFS_DEV_EXTENT_KEY;
878 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
880 ret = btrfs_previous_item(root, path, key.objectid,
881 BTRFS_DEV_EXTENT_KEY);
883 leaf = path->nodes[0];
884 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
885 extent = btrfs_item_ptr(leaf, path->slots[0],
886 struct btrfs_dev_extent);
887 BUG_ON(found_key.offset > start || found_key.offset +
888 btrfs_dev_extent_length(leaf, extent) < start);
890 } else if (ret == 0) {
891 leaf = path->nodes[0];
892 extent = btrfs_item_ptr(leaf, path->slots[0],
893 struct btrfs_dev_extent);
897 if (device->bytes_used > 0)
898 device->bytes_used -= btrfs_dev_extent_length(leaf, extent);
899 ret = btrfs_del_item(trans, root, path);
902 btrfs_free_path(path);
906 int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
907 struct btrfs_device *device,
908 u64 chunk_tree, u64 chunk_objectid,
909 u64 chunk_offset, u64 start, u64 num_bytes)
912 struct btrfs_path *path;
913 struct btrfs_root *root = device->dev_root;
914 struct btrfs_dev_extent *extent;
915 struct extent_buffer *leaf;
916 struct btrfs_key key;
918 WARN_ON(!device->in_fs_metadata);
919 path = btrfs_alloc_path();
923 key.objectid = device->devid;
925 key.type = BTRFS_DEV_EXTENT_KEY;
926 ret = btrfs_insert_empty_item(trans, root, path, &key,
930 leaf = path->nodes[0];
931 extent = btrfs_item_ptr(leaf, path->slots[0],
932 struct btrfs_dev_extent);
933 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
934 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
935 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
937 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
938 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
941 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
942 btrfs_mark_buffer_dirty(leaf);
943 btrfs_free_path(path);
947 static noinline int find_next_chunk(struct btrfs_root *root,
948 u64 objectid, u64 *offset)
950 struct btrfs_path *path;
952 struct btrfs_key key;
953 struct btrfs_chunk *chunk;
954 struct btrfs_key found_key;
956 path = btrfs_alloc_path();
959 key.objectid = objectid;
960 key.offset = (u64)-1;
961 key.type = BTRFS_CHUNK_ITEM_KEY;
963 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
969 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
973 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
975 if (found_key.objectid != objectid)
978 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
980 *offset = found_key.offset +
981 btrfs_chunk_length(path->nodes[0], chunk);
986 btrfs_free_path(path);
990 static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
993 struct btrfs_key key;
994 struct btrfs_key found_key;
995 struct btrfs_path *path;
997 root = root->fs_info->chunk_root;
999 path = btrfs_alloc_path();
1003 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1004 key.type = BTRFS_DEV_ITEM_KEY;
1005 key.offset = (u64)-1;
1007 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1013 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1014 BTRFS_DEV_ITEM_KEY);
1018 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1020 *objectid = found_key.offset + 1;
1024 btrfs_free_path(path);
1029 * the device information is stored in the chunk root
1030 * the btrfs_device struct should be fully filled in
1032 int btrfs_add_device(struct btrfs_trans_handle *trans,
1033 struct btrfs_root *root,
1034 struct btrfs_device *device)
1037 struct btrfs_path *path;
1038 struct btrfs_dev_item *dev_item;
1039 struct extent_buffer *leaf;
1040 struct btrfs_key key;
1043 root = root->fs_info->chunk_root;
1045 path = btrfs_alloc_path();
1049 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1050 key.type = BTRFS_DEV_ITEM_KEY;
1051 key.offset = device->devid;
1053 ret = btrfs_insert_empty_item(trans, root, path, &key,
1058 leaf = path->nodes[0];
1059 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1061 btrfs_set_device_id(leaf, dev_item, device->devid);
1062 btrfs_set_device_generation(leaf, dev_item, 0);
1063 btrfs_set_device_type(leaf, dev_item, device->type);
1064 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1065 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1066 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
1067 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1068 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1069 btrfs_set_device_group(leaf, dev_item, 0);
1070 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1071 btrfs_set_device_bandwidth(leaf, dev_item, 0);
1072 btrfs_set_device_start_offset(leaf, dev_item, 0);
1074 ptr = (unsigned long)btrfs_device_uuid(dev_item);
1075 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1076 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1077 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
1078 btrfs_mark_buffer_dirty(leaf);
1082 btrfs_free_path(path);
1086 static int btrfs_rm_dev_item(struct btrfs_root *root,
1087 struct btrfs_device *device)
1090 struct btrfs_path *path;
1091 struct btrfs_key key;
1092 struct btrfs_trans_handle *trans;
1094 root = root->fs_info->chunk_root;
1096 path = btrfs_alloc_path();
1100 trans = btrfs_start_transaction(root, 1);
1101 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1102 key.type = BTRFS_DEV_ITEM_KEY;
1103 key.offset = device->devid;
1106 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1115 ret = btrfs_del_item(trans, root, path);
1119 btrfs_free_path(path);
1120 unlock_chunks(root);
1121 btrfs_commit_transaction(trans, root);
1125 int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1127 struct btrfs_device *device;
1128 struct btrfs_device *next_device;
1129 struct block_device *bdev;
1130 struct buffer_head *bh = NULL;
1131 struct btrfs_super_block *disk_super;
1138 mutex_lock(&uuid_mutex);
1139 mutex_lock(&root->fs_info->volume_mutex);
1141 all_avail = root->fs_info->avail_data_alloc_bits |
1142 root->fs_info->avail_system_alloc_bits |
1143 root->fs_info->avail_metadata_alloc_bits;
1145 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
1146 root->fs_info->fs_devices->num_devices <= 4) {
1147 printk(KERN_ERR "btrfs: unable to go below four devices "
1153 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
1154 root->fs_info->fs_devices->num_devices <= 2) {
1155 printk(KERN_ERR "btrfs: unable to go below two "
1156 "devices on raid1\n");
1161 if (strcmp(device_path, "missing") == 0) {
1162 struct list_head *devices;
1163 struct btrfs_device *tmp;
1166 devices = &root->fs_info->fs_devices->devices;
1167 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1168 list_for_each_entry(tmp, devices, dev_list) {
1169 if (tmp->in_fs_metadata && !tmp->bdev) {
1174 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1179 printk(KERN_ERR "btrfs: no missing devices found to "
1184 bdev = open_bdev_exclusive(device_path, FMODE_READ,
1185 root->fs_info->bdev_holder);
1187 ret = PTR_ERR(bdev);
1191 set_blocksize(bdev, 4096);
1192 bh = btrfs_read_dev_super(bdev);
1197 disk_super = (struct btrfs_super_block *)bh->b_data;
1198 devid = btrfs_stack_device_id(&disk_super->dev_item);
1199 dev_uuid = disk_super->dev_item.uuid;
1200 device = btrfs_find_device(root, devid, dev_uuid,
1208 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
1209 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1215 if (device->writeable) {
1216 list_del_init(&device->dev_alloc_list);
1217 root->fs_info->fs_devices->rw_devices--;
1220 ret = btrfs_shrink_device(device, 0);
1224 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1228 device->in_fs_metadata = 0;
1231 * the device list mutex makes sure that we don't change
1232 * the device list while someone else is writing out all
1233 * the device supers.
1235 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1236 list_del_init(&device->dev_list);
1237 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1239 device->fs_devices->num_devices--;
1241 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1242 struct btrfs_device, dev_list);
1243 if (device->bdev == root->fs_info->sb->s_bdev)
1244 root->fs_info->sb->s_bdev = next_device->bdev;
1245 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1246 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1249 close_bdev_exclusive(device->bdev, device->mode);
1250 device->bdev = NULL;
1251 device->fs_devices->open_devices--;
1254 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
1255 btrfs_set_super_num_devices(&root->fs_info->super_copy, num_devices);
1257 if (device->fs_devices->open_devices == 0) {
1258 struct btrfs_fs_devices *fs_devices;
1259 fs_devices = root->fs_info->fs_devices;
1260 while (fs_devices) {
1261 if (fs_devices->seed == device->fs_devices)
1263 fs_devices = fs_devices->seed;
1265 fs_devices->seed = device->fs_devices->seed;
1266 device->fs_devices->seed = NULL;
1267 __btrfs_close_devices(device->fs_devices);
1268 free_fs_devices(device->fs_devices);
1272 * at this point, the device is zero sized. We want to
1273 * remove it from the devices list and zero out the old super
1275 if (device->writeable) {
1276 /* make sure this device isn't detected as part of
1279 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1280 set_buffer_dirty(bh);
1281 sync_dirty_buffer(bh);
1284 kfree(device->name);
1292 close_bdev_exclusive(bdev, FMODE_READ);
1294 mutex_unlock(&root->fs_info->volume_mutex);
1295 mutex_unlock(&uuid_mutex);
1300 * does all the dirty work required for changing file system's UUID.
1302 static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans,
1303 struct btrfs_root *root)
1305 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1306 struct btrfs_fs_devices *old_devices;
1307 struct btrfs_fs_devices *seed_devices;
1308 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
1309 struct btrfs_device *device;
1312 BUG_ON(!mutex_is_locked(&uuid_mutex));
1313 if (!fs_devices->seeding)
1316 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1320 old_devices = clone_fs_devices(fs_devices);
1321 if (IS_ERR(old_devices)) {
1322 kfree(seed_devices);
1323 return PTR_ERR(old_devices);
1326 list_add(&old_devices->list, &fs_uuids);
1328 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1329 seed_devices->opened = 1;
1330 INIT_LIST_HEAD(&seed_devices->devices);
1331 INIT_LIST_HEAD(&seed_devices->alloc_list);
1332 mutex_init(&seed_devices->device_list_mutex);
1333 list_splice_init(&fs_devices->devices, &seed_devices->devices);
1334 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1335 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1336 device->fs_devices = seed_devices;
1339 fs_devices->seeding = 0;
1340 fs_devices->num_devices = 0;
1341 fs_devices->open_devices = 0;
1342 fs_devices->seed = seed_devices;
1344 generate_random_uuid(fs_devices->fsid);
1345 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1346 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1347 super_flags = btrfs_super_flags(disk_super) &
1348 ~BTRFS_SUPER_FLAG_SEEDING;
1349 btrfs_set_super_flags(disk_super, super_flags);
1355 * strore the expected generation for seed devices in device items.
1357 static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1358 struct btrfs_root *root)
1360 struct btrfs_path *path;
1361 struct extent_buffer *leaf;
1362 struct btrfs_dev_item *dev_item;
1363 struct btrfs_device *device;
1364 struct btrfs_key key;
1365 u8 fs_uuid[BTRFS_UUID_SIZE];
1366 u8 dev_uuid[BTRFS_UUID_SIZE];
1370 path = btrfs_alloc_path();
1374 root = root->fs_info->chunk_root;
1375 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1377 key.type = BTRFS_DEV_ITEM_KEY;
1380 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1384 leaf = path->nodes[0];
1386 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1387 ret = btrfs_next_leaf(root, path);
1392 leaf = path->nodes[0];
1393 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1394 btrfs_release_path(root, path);
1398 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1399 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1400 key.type != BTRFS_DEV_ITEM_KEY)
1403 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1404 struct btrfs_dev_item);
1405 devid = btrfs_device_id(leaf, dev_item);
1406 read_extent_buffer(leaf, dev_uuid,
1407 (unsigned long)btrfs_device_uuid(dev_item),
1409 read_extent_buffer(leaf, fs_uuid,
1410 (unsigned long)btrfs_device_fsid(dev_item),
1412 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1415 if (device->fs_devices->seeding) {
1416 btrfs_set_device_generation(leaf, dev_item,
1417 device->generation);
1418 btrfs_mark_buffer_dirty(leaf);
1426 btrfs_free_path(path);
1430 int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1432 struct btrfs_trans_handle *trans;
1433 struct btrfs_device *device;
1434 struct block_device *bdev;
1435 struct list_head *devices;
1436 struct super_block *sb = root->fs_info->sb;
1438 int seeding_dev = 0;
1441 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1444 bdev = open_bdev_exclusive(device_path, 0, root->fs_info->bdev_holder);
1446 return PTR_ERR(bdev);
1448 if (root->fs_info->fs_devices->seeding) {
1450 down_write(&sb->s_umount);
1451 mutex_lock(&uuid_mutex);
1454 filemap_write_and_wait(bdev->bd_inode->i_mapping);
1455 mutex_lock(&root->fs_info->volume_mutex);
1457 devices = &root->fs_info->fs_devices->devices;
1459 * we have the volume lock, so we don't need the extra
1460 * device list mutex while reading the list here.
1462 list_for_each_entry(device, devices, dev_list) {
1463 if (device->bdev == bdev) {
1469 device = kzalloc(sizeof(*device), GFP_NOFS);
1471 /* we can safely leave the fs_devices entry around */
1476 device->name = kstrdup(device_path, GFP_NOFS);
1477 if (!device->name) {
1483 ret = find_next_devid(root, &device->devid);
1489 trans = btrfs_start_transaction(root, 1);
1492 device->barriers = 1;
1493 device->writeable = 1;
1494 device->work.func = pending_bios_fn;
1495 generate_random_uuid(device->uuid);
1496 spin_lock_init(&device->io_lock);
1497 device->generation = trans->transid;
1498 device->io_width = root->sectorsize;
1499 device->io_align = root->sectorsize;
1500 device->sector_size = root->sectorsize;
1501 device->total_bytes = i_size_read(bdev->bd_inode);
1502 device->disk_total_bytes = device->total_bytes;
1503 device->dev_root = root->fs_info->dev_root;
1504 device->bdev = bdev;
1505 device->in_fs_metadata = 1;
1507 set_blocksize(device->bdev, 4096);
1510 sb->s_flags &= ~MS_RDONLY;
1511 ret = btrfs_prepare_sprout(trans, root);
1515 device->fs_devices = root->fs_info->fs_devices;
1518 * we don't want write_supers to jump in here with our device
1521 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1522 list_add(&device->dev_list, &root->fs_info->fs_devices->devices);
1523 list_add(&device->dev_alloc_list,
1524 &root->fs_info->fs_devices->alloc_list);
1525 root->fs_info->fs_devices->num_devices++;
1526 root->fs_info->fs_devices->open_devices++;
1527 root->fs_info->fs_devices->rw_devices++;
1528 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1530 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1531 root->fs_info->fs_devices->rotating = 1;
1533 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
1534 btrfs_set_super_total_bytes(&root->fs_info->super_copy,
1535 total_bytes + device->total_bytes);
1537 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
1538 btrfs_set_super_num_devices(&root->fs_info->super_copy,
1540 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1543 ret = init_first_rw_device(trans, root, device);
1545 ret = btrfs_finish_sprout(trans, root);
1548 ret = btrfs_add_device(trans, root, device);
1552 * we've got more storage, clear any full flags on the space
1555 btrfs_clear_space_info_full(root->fs_info);
1557 unlock_chunks(root);
1558 btrfs_commit_transaction(trans, root);
1561 mutex_unlock(&uuid_mutex);
1562 up_write(&sb->s_umount);
1564 ret = btrfs_relocate_sys_chunks(root);
1568 mutex_unlock(&root->fs_info->volume_mutex);
1571 close_bdev_exclusive(bdev, 0);
1573 mutex_unlock(&uuid_mutex);
1574 up_write(&sb->s_umount);
1579 static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1580 struct btrfs_device *device)
1583 struct btrfs_path *path;
1584 struct btrfs_root *root;
1585 struct btrfs_dev_item *dev_item;
1586 struct extent_buffer *leaf;
1587 struct btrfs_key key;
1589 root = device->dev_root->fs_info->chunk_root;
1591 path = btrfs_alloc_path();
1595 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1596 key.type = BTRFS_DEV_ITEM_KEY;
1597 key.offset = device->devid;
1599 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1608 leaf = path->nodes[0];
1609 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1611 btrfs_set_device_id(leaf, dev_item, device->devid);
1612 btrfs_set_device_type(leaf, dev_item, device->type);
1613 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1614 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1615 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
1616 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
1617 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1618 btrfs_mark_buffer_dirty(leaf);
1621 btrfs_free_path(path);
1625 static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
1626 struct btrfs_device *device, u64 new_size)
1628 struct btrfs_super_block *super_copy =
1629 &device->dev_root->fs_info->super_copy;
1630 u64 old_total = btrfs_super_total_bytes(super_copy);
1631 u64 diff = new_size - device->total_bytes;
1633 if (!device->writeable)
1635 if (new_size <= device->total_bytes)
1638 btrfs_set_super_total_bytes(super_copy, old_total + diff);
1639 device->fs_devices->total_rw_bytes += diff;
1641 device->total_bytes = new_size;
1642 device->disk_total_bytes = new_size;
1643 btrfs_clear_space_info_full(device->dev_root->fs_info);
1645 return btrfs_update_device(trans, device);
1648 int btrfs_grow_device(struct btrfs_trans_handle *trans,
1649 struct btrfs_device *device, u64 new_size)
1652 lock_chunks(device->dev_root);
1653 ret = __btrfs_grow_device(trans, device, new_size);
1654 unlock_chunks(device->dev_root);
1658 static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1659 struct btrfs_root *root,
1660 u64 chunk_tree, u64 chunk_objectid,
1664 struct btrfs_path *path;
1665 struct btrfs_key key;
1667 root = root->fs_info->chunk_root;
1668 path = btrfs_alloc_path();
1672 key.objectid = chunk_objectid;
1673 key.offset = chunk_offset;
1674 key.type = BTRFS_CHUNK_ITEM_KEY;
1676 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1679 ret = btrfs_del_item(trans, root, path);
1682 btrfs_free_path(path);
1686 static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
1689 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1690 struct btrfs_disk_key *disk_key;
1691 struct btrfs_chunk *chunk;
1698 struct btrfs_key key;
1700 array_size = btrfs_super_sys_array_size(super_copy);
1702 ptr = super_copy->sys_chunk_array;
1705 while (cur < array_size) {
1706 disk_key = (struct btrfs_disk_key *)ptr;
1707 btrfs_disk_key_to_cpu(&key, disk_key);
1709 len = sizeof(*disk_key);
1711 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1712 chunk = (struct btrfs_chunk *)(ptr + len);
1713 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1714 len += btrfs_chunk_item_size(num_stripes);
1719 if (key.objectid == chunk_objectid &&
1720 key.offset == chunk_offset) {
1721 memmove(ptr, ptr + len, array_size - (cur + len));
1723 btrfs_set_super_sys_array_size(super_copy, array_size);
1732 static int btrfs_relocate_chunk(struct btrfs_root *root,
1733 u64 chunk_tree, u64 chunk_objectid,
1736 struct extent_map_tree *em_tree;
1737 struct btrfs_root *extent_root;
1738 struct btrfs_trans_handle *trans;
1739 struct extent_map *em;
1740 struct map_lookup *map;
1744 root = root->fs_info->chunk_root;
1745 extent_root = root->fs_info->extent_root;
1746 em_tree = &root->fs_info->mapping_tree.map_tree;
1748 ret = btrfs_can_relocate(extent_root, chunk_offset);
1752 /* step one, relocate all the extents inside this chunk */
1753 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
1756 trans = btrfs_start_transaction(root, 1);
1762 * step two, delete the device extents and the
1763 * chunk tree entries
1765 read_lock(&em_tree->lock);
1766 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
1767 read_unlock(&em_tree->lock);
1769 BUG_ON(em->start > chunk_offset ||
1770 em->start + em->len < chunk_offset);
1771 map = (struct map_lookup *)em->bdev;
1773 for (i = 0; i < map->num_stripes; i++) {
1774 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1775 map->stripes[i].physical);
1778 if (map->stripes[i].dev) {
1779 ret = btrfs_update_device(trans, map->stripes[i].dev);
1783 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1788 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1789 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1793 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
1796 write_lock(&em_tree->lock);
1797 remove_extent_mapping(em_tree, em);
1798 write_unlock(&em_tree->lock);
1803 /* once for the tree */
1804 free_extent_map(em);
1806 free_extent_map(em);
1808 unlock_chunks(root);
1809 btrfs_end_transaction(trans, root);
1813 static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
1815 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1816 struct btrfs_path *path;
1817 struct extent_buffer *leaf;
1818 struct btrfs_chunk *chunk;
1819 struct btrfs_key key;
1820 struct btrfs_key found_key;
1821 u64 chunk_tree = chunk_root->root_key.objectid;
1823 bool retried = false;
1827 path = btrfs_alloc_path();
1832 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1833 key.offset = (u64)-1;
1834 key.type = BTRFS_CHUNK_ITEM_KEY;
1837 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1842 ret = btrfs_previous_item(chunk_root, path, key.objectid,
1849 leaf = path->nodes[0];
1850 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1852 chunk = btrfs_item_ptr(leaf, path->slots[0],
1853 struct btrfs_chunk);
1854 chunk_type = btrfs_chunk_type(leaf, chunk);
1855 btrfs_release_path(chunk_root, path);
1857 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
1858 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
1867 if (found_key.offset == 0)
1869 key.offset = found_key.offset - 1;
1872 if (failed && !retried) {
1876 } else if (failed && retried) {
1881 btrfs_free_path(path);
1885 static u64 div_factor(u64 num, int factor)
1894 int btrfs_balance(struct btrfs_root *dev_root)
1897 struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
1898 struct btrfs_device *device;
1901 struct btrfs_path *path;
1902 struct btrfs_key key;
1903 struct btrfs_chunk *chunk;
1904 struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
1905 struct btrfs_trans_handle *trans;
1906 struct btrfs_key found_key;
1908 if (dev_root->fs_info->sb->s_flags & MS_RDONLY)
1911 mutex_lock(&dev_root->fs_info->volume_mutex);
1912 dev_root = dev_root->fs_info->dev_root;
1914 /* step one make some room on all the devices */
1915 list_for_each_entry(device, devices, dev_list) {
1916 old_size = device->total_bytes;
1917 size_to_free = div_factor(old_size, 1);
1918 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
1919 if (!device->writeable ||
1920 device->total_bytes - device->bytes_used > size_to_free)
1923 ret = btrfs_shrink_device(device, old_size - size_to_free);
1928 trans = btrfs_start_transaction(dev_root, 1);
1931 ret = btrfs_grow_device(trans, device, old_size);
1934 btrfs_end_transaction(trans, dev_root);
1937 /* step two, relocate all the chunks */
1938 path = btrfs_alloc_path();
1941 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1942 key.offset = (u64)-1;
1943 key.type = BTRFS_CHUNK_ITEM_KEY;
1946 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1951 * this shouldn't happen, it means the last relocate
1957 ret = btrfs_previous_item(chunk_root, path, 0,
1958 BTRFS_CHUNK_ITEM_KEY);
1962 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1964 if (found_key.objectid != key.objectid)
1967 chunk = btrfs_item_ptr(path->nodes[0],
1969 struct btrfs_chunk);
1970 /* chunk zero is special */
1971 if (found_key.offset == 0)
1974 btrfs_release_path(chunk_root, path);
1975 ret = btrfs_relocate_chunk(chunk_root,
1976 chunk_root->root_key.objectid,
1979 BUG_ON(ret && ret != -ENOSPC);
1980 key.offset = found_key.offset - 1;
1984 btrfs_free_path(path);
1985 mutex_unlock(&dev_root->fs_info->volume_mutex);
1990 * shrinking a device means finding all of the device extents past
1991 * the new size, and then following the back refs to the chunks.
1992 * The chunk relocation code actually frees the device extent
1994 int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
1996 struct btrfs_trans_handle *trans;
1997 struct btrfs_root *root = device->dev_root;
1998 struct btrfs_dev_extent *dev_extent = NULL;
1999 struct btrfs_path *path;
2007 bool retried = false;
2008 struct extent_buffer *l;
2009 struct btrfs_key key;
2010 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2011 u64 old_total = btrfs_super_total_bytes(super_copy);
2012 u64 old_size = device->total_bytes;
2013 u64 diff = device->total_bytes - new_size;
2015 if (new_size >= device->total_bytes)
2018 path = btrfs_alloc_path();
2026 device->total_bytes = new_size;
2027 if (device->writeable)
2028 device->fs_devices->total_rw_bytes -= diff;
2029 unlock_chunks(root);
2032 key.objectid = device->devid;
2033 key.offset = (u64)-1;
2034 key.type = BTRFS_DEV_EXTENT_KEY;
2037 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2041 ret = btrfs_previous_item(root, path, 0, key.type);
2046 btrfs_release_path(root, path);
2051 slot = path->slots[0];
2052 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
2054 if (key.objectid != device->devid) {
2055 btrfs_release_path(root, path);
2059 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2060 length = btrfs_dev_extent_length(l, dev_extent);
2062 if (key.offset + length <= new_size) {
2063 btrfs_release_path(root, path);
2067 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
2068 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
2069 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
2070 btrfs_release_path(root, path);
2072 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
2074 if (ret && ret != -ENOSPC)
2081 if (failed && !retried) {
2085 } else if (failed && retried) {
2089 device->total_bytes = old_size;
2090 if (device->writeable)
2091 device->fs_devices->total_rw_bytes += diff;
2092 unlock_chunks(root);
2096 /* Shrinking succeeded, else we would be at "done". */
2097 trans = btrfs_start_transaction(root, 1);
2104 device->disk_total_bytes = new_size;
2105 /* Now btrfs_update_device() will change the on-disk size. */
2106 ret = btrfs_update_device(trans, device);
2108 unlock_chunks(root);
2109 btrfs_end_transaction(trans, root);
2112 WARN_ON(diff > old_total);
2113 btrfs_set_super_total_bytes(super_copy, old_total - diff);
2114 unlock_chunks(root);
2115 btrfs_end_transaction(trans, root);
2117 btrfs_free_path(path);
2121 static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
2122 struct btrfs_root *root,
2123 struct btrfs_key *key,
2124 struct btrfs_chunk *chunk, int item_size)
2126 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2127 struct btrfs_disk_key disk_key;
2131 array_size = btrfs_super_sys_array_size(super_copy);
2132 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
2135 ptr = super_copy->sys_chunk_array + array_size;
2136 btrfs_cpu_key_to_disk(&disk_key, key);
2137 memcpy(ptr, &disk_key, sizeof(disk_key));
2138 ptr += sizeof(disk_key);
2139 memcpy(ptr, chunk, item_size);
2140 item_size += sizeof(disk_key);
2141 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
2145 static noinline u64 chunk_bytes_by_type(u64 type, u64 calc_size,
2146 int num_stripes, int sub_stripes)
2148 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
2150 else if (type & BTRFS_BLOCK_GROUP_RAID10)
2151 return calc_size * (num_stripes / sub_stripes);
2153 return calc_size * num_stripes;
2156 static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2157 struct btrfs_root *extent_root,
2158 struct map_lookup **map_ret,
2159 u64 *num_bytes, u64 *stripe_size,
2160 u64 start, u64 type)
2162 struct btrfs_fs_info *info = extent_root->fs_info;
2163 struct btrfs_device *device = NULL;
2164 struct btrfs_fs_devices *fs_devices = info->fs_devices;
2165 struct list_head *cur;
2166 struct map_lookup *map = NULL;
2167 struct extent_map_tree *em_tree;
2168 struct extent_map *em;
2169 struct list_head private_devs;
2170 int min_stripe_size = 1 * 1024 * 1024;
2171 u64 calc_size = 1024 * 1024 * 1024;
2172 u64 max_chunk_size = calc_size;
2177 int num_stripes = 1;
2178 int min_stripes = 1;
2179 int sub_stripes = 0;
2183 int stripe_len = 64 * 1024;
2185 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
2186 (type & BTRFS_BLOCK_GROUP_DUP)) {
2188 type &= ~BTRFS_BLOCK_GROUP_DUP;
2190 if (list_empty(&fs_devices->alloc_list))
2193 if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
2194 num_stripes = fs_devices->rw_devices;
2197 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
2201 if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
2202 if (fs_devices->rw_devices < 2)
2207 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2208 num_stripes = fs_devices->rw_devices;
2209 if (num_stripes < 4)
2211 num_stripes &= ~(u32)1;
2216 if (type & BTRFS_BLOCK_GROUP_DATA) {
2217 max_chunk_size = 10 * calc_size;
2218 min_stripe_size = 64 * 1024 * 1024;
2219 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
2220 max_chunk_size = 256 * 1024 * 1024;
2221 min_stripe_size = 32 * 1024 * 1024;
2222 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
2223 calc_size = 8 * 1024 * 1024;
2224 max_chunk_size = calc_size * 2;
2225 min_stripe_size = 1 * 1024 * 1024;
2228 /* we don't want a chunk larger than 10% of writeable space */
2229 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
2234 if (!map || map->num_stripes != num_stripes) {
2236 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
2239 map->num_stripes = num_stripes;
2242 if (calc_size * num_stripes > max_chunk_size) {
2243 calc_size = max_chunk_size;
2244 do_div(calc_size, num_stripes);
2245 do_div(calc_size, stripe_len);
2246 calc_size *= stripe_len;
2249 /* we don't want tiny stripes */
2251 calc_size = max_t(u64, min_stripe_size, calc_size);
2254 * we're about to do_div by the stripe_len so lets make sure
2255 * we end up with something bigger than a stripe
2257 calc_size = max_t(u64, calc_size, stripe_len * 4);
2259 do_div(calc_size, stripe_len);
2260 calc_size *= stripe_len;
2262 cur = fs_devices->alloc_list.next;
2265 if (type & BTRFS_BLOCK_GROUP_DUP)
2266 min_free = calc_size * 2;
2268 min_free = calc_size;
2271 * we add 1MB because we never use the first 1MB of the device, unless
2272 * we've looped, then we are likely allocating the maximum amount of
2273 * space left already
2276 min_free += 1024 * 1024;
2278 INIT_LIST_HEAD(&private_devs);
2279 while (index < num_stripes) {
2280 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
2281 BUG_ON(!device->writeable);
2282 if (device->total_bytes > device->bytes_used)
2283 avail = device->total_bytes - device->bytes_used;
2288 if (device->in_fs_metadata && avail >= min_free) {
2289 ret = find_free_dev_extent(trans, device,
2290 min_free, &dev_offset,
2293 list_move_tail(&device->dev_alloc_list,
2295 map->stripes[index].dev = device;
2296 map->stripes[index].physical = dev_offset;
2298 if (type & BTRFS_BLOCK_GROUP_DUP) {
2299 map->stripes[index].dev = device;
2300 map->stripes[index].physical =
2301 dev_offset + calc_size;
2305 } else if (device->in_fs_metadata && avail > max_avail)
2307 if (cur == &fs_devices->alloc_list)
2310 list_splice(&private_devs, &fs_devices->alloc_list);
2311 if (index < num_stripes) {
2312 if (index >= min_stripes) {
2313 num_stripes = index;
2314 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2315 num_stripes /= sub_stripes;
2316 num_stripes *= sub_stripes;
2321 if (!looped && max_avail > 0) {
2323 calc_size = max_avail;
2329 map->sector_size = extent_root->sectorsize;
2330 map->stripe_len = stripe_len;
2331 map->io_align = stripe_len;
2332 map->io_width = stripe_len;
2334 map->num_stripes = num_stripes;
2335 map->sub_stripes = sub_stripes;
2338 *stripe_size = calc_size;
2339 *num_bytes = chunk_bytes_by_type(type, calc_size,
2340 num_stripes, sub_stripes);
2342 em = alloc_extent_map(GFP_NOFS);
2347 em->bdev = (struct block_device *)map;
2349 em->len = *num_bytes;
2350 em->block_start = 0;
2351 em->block_len = em->len;
2353 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
2354 write_lock(&em_tree->lock);
2355 ret = add_extent_mapping(em_tree, em);
2356 write_unlock(&em_tree->lock);
2358 free_extent_map(em);
2360 ret = btrfs_make_block_group(trans, extent_root, 0, type,
2361 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2366 while (index < map->num_stripes) {
2367 device = map->stripes[index].dev;
2368 dev_offset = map->stripes[index].physical;
2370 ret = btrfs_alloc_dev_extent(trans, device,
2371 info->chunk_root->root_key.objectid,
2372 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2373 start, dev_offset, calc_size);
2381 static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
2382 struct btrfs_root *extent_root,
2383 struct map_lookup *map, u64 chunk_offset,
2384 u64 chunk_size, u64 stripe_size)
2387 struct btrfs_key key;
2388 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2389 struct btrfs_device *device;
2390 struct btrfs_chunk *chunk;
2391 struct btrfs_stripe *stripe;
2392 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
2396 chunk = kzalloc(item_size, GFP_NOFS);
2401 while (index < map->num_stripes) {
2402 device = map->stripes[index].dev;
2403 device->bytes_used += stripe_size;
2404 ret = btrfs_update_device(trans, device);
2410 stripe = &chunk->stripe;
2411 while (index < map->num_stripes) {
2412 device = map->stripes[index].dev;
2413 dev_offset = map->stripes[index].physical;
2415 btrfs_set_stack_stripe_devid(stripe, device->devid);
2416 btrfs_set_stack_stripe_offset(stripe, dev_offset);
2417 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
2422 btrfs_set_stack_chunk_length(chunk, chunk_size);
2423 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
2424 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
2425 btrfs_set_stack_chunk_type(chunk, map->type);
2426 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
2427 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
2428 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
2429 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
2430 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
2432 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2433 key.type = BTRFS_CHUNK_ITEM_KEY;
2434 key.offset = chunk_offset;
2436 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
2439 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2440 ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
2449 * Chunk allocation falls into two parts. The first part does works
2450 * that make the new allocated chunk useable, but not do any operation
2451 * that modifies the chunk tree. The second part does the works that
2452 * require modifying the chunk tree. This division is important for the
2453 * bootstrap process of adding storage to a seed btrfs.
2455 int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2456 struct btrfs_root *extent_root, u64 type)
2461 struct map_lookup *map;
2462 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2465 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2470 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2471 &stripe_size, chunk_offset, type);
2475 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2476 chunk_size, stripe_size);
2481 static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
2482 struct btrfs_root *root,
2483 struct btrfs_device *device)
2486 u64 sys_chunk_offset;
2490 u64 sys_stripe_size;
2492 struct map_lookup *map;
2493 struct map_lookup *sys_map;
2494 struct btrfs_fs_info *fs_info = root->fs_info;
2495 struct btrfs_root *extent_root = fs_info->extent_root;
2498 ret = find_next_chunk(fs_info->chunk_root,
2499 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
2502 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
2503 (fs_info->metadata_alloc_profile &
2504 fs_info->avail_metadata_alloc_bits);
2505 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2507 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2508 &stripe_size, chunk_offset, alloc_profile);
2511 sys_chunk_offset = chunk_offset + chunk_size;
2513 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
2514 (fs_info->system_alloc_profile &
2515 fs_info->avail_system_alloc_bits);
2516 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2518 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
2519 &sys_chunk_size, &sys_stripe_size,
2520 sys_chunk_offset, alloc_profile);
2523 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
2527 * Modifying chunk tree needs allocating new blocks from both
2528 * system block group and metadata block group. So we only can
2529 * do operations require modifying the chunk tree after both
2530 * block groups were created.
2532 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2533 chunk_size, stripe_size);
2536 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
2537 sys_chunk_offset, sys_chunk_size,
2543 int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
2545 struct extent_map *em;
2546 struct map_lookup *map;
2547 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2551 read_lock(&map_tree->map_tree.lock);
2552 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
2553 read_unlock(&map_tree->map_tree.lock);
2557 if (btrfs_test_opt(root, DEGRADED)) {
2558 free_extent_map(em);
2562 map = (struct map_lookup *)em->bdev;
2563 for (i = 0; i < map->num_stripes; i++) {
2564 if (!map->stripes[i].dev->writeable) {
2569 free_extent_map(em);
2573 void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
2575 extent_map_tree_init(&tree->map_tree, GFP_NOFS);
2578 void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
2580 struct extent_map *em;
2583 write_lock(&tree->map_tree.lock);
2584 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
2586 remove_extent_mapping(&tree->map_tree, em);
2587 write_unlock(&tree->map_tree.lock);
2592 free_extent_map(em);
2593 /* once for the tree */
2594 free_extent_map(em);
2598 int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
2600 struct extent_map *em;
2601 struct map_lookup *map;
2602 struct extent_map_tree *em_tree = &map_tree->map_tree;
2605 read_lock(&em_tree->lock);
2606 em = lookup_extent_mapping(em_tree, logical, len);
2607 read_unlock(&em_tree->lock);
2610 BUG_ON(em->start > logical || em->start + em->len < logical);
2611 map = (struct map_lookup *)em->bdev;
2612 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
2613 ret = map->num_stripes;
2614 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2615 ret = map->sub_stripes;
2618 free_extent_map(em);
2622 static int find_live_mirror(struct map_lookup *map, int first, int num,
2626 if (map->stripes[optimal].dev->bdev)
2628 for (i = first; i < first + num; i++) {
2629 if (map->stripes[i].dev->bdev)
2632 /* we couldn't find one that doesn't fail. Just return something
2633 * and the io error handling code will clean up eventually
2638 static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2639 u64 logical, u64 *length,
2640 struct btrfs_multi_bio **multi_ret,
2641 int mirror_num, struct page *unplug_page)
2643 struct extent_map *em;
2644 struct map_lookup *map;
2645 struct extent_map_tree *em_tree = &map_tree->map_tree;
2649 int stripes_allocated = 8;
2650 int stripes_required = 1;
2655 struct btrfs_multi_bio *multi = NULL;
2657 if (multi_ret && !(rw & (1 << BIO_RW)))
2658 stripes_allocated = 1;
2661 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
2666 atomic_set(&multi->error, 0);
2669 read_lock(&em_tree->lock);
2670 em = lookup_extent_mapping(em_tree, logical, *length);
2671 read_unlock(&em_tree->lock);
2673 if (!em && unplug_page) {
2679 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
2680 (unsigned long long)logical,
2681 (unsigned long long)*length);
2685 BUG_ON(em->start > logical || em->start + em->len < logical);
2686 map = (struct map_lookup *)em->bdev;
2687 offset = logical - em->start;
2689 if (mirror_num > map->num_stripes)
2692 /* if our multi bio struct is too small, back off and try again */
2693 if (rw & (1 << BIO_RW)) {
2694 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
2695 BTRFS_BLOCK_GROUP_DUP)) {
2696 stripes_required = map->num_stripes;
2698 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2699 stripes_required = map->sub_stripes;
2703 if (multi_ret && (rw & (1 << BIO_RW)) &&
2704 stripes_allocated < stripes_required) {
2705 stripes_allocated = map->num_stripes;
2706 free_extent_map(em);
2712 * stripe_nr counts the total number of stripes we have to stride
2713 * to get to this block
2715 do_div(stripe_nr, map->stripe_len);
2717 stripe_offset = stripe_nr * map->stripe_len;
2718 BUG_ON(offset < stripe_offset);
2720 /* stripe_offset is the offset of this block in its stripe*/
2721 stripe_offset = offset - stripe_offset;
2723 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
2724 BTRFS_BLOCK_GROUP_RAID10 |
2725 BTRFS_BLOCK_GROUP_DUP)) {
2726 /* we limit the length of each bio to what fits in a stripe */
2727 *length = min_t(u64, em->len - offset,
2728 map->stripe_len - stripe_offset);
2730 *length = em->len - offset;
2733 if (!multi_ret && !unplug_page)
2738 if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
2739 if (unplug_page || (rw & (1 << BIO_RW)))
2740 num_stripes = map->num_stripes;
2741 else if (mirror_num)
2742 stripe_index = mirror_num - 1;
2744 stripe_index = find_live_mirror(map, 0,
2746 current->pid % map->num_stripes);
2749 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
2750 if (rw & (1 << BIO_RW))
2751 num_stripes = map->num_stripes;
2752 else if (mirror_num)
2753 stripe_index = mirror_num - 1;
2755 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2756 int factor = map->num_stripes / map->sub_stripes;
2758 stripe_index = do_div(stripe_nr, factor);
2759 stripe_index *= map->sub_stripes;
2761 if (unplug_page || (rw & (1 << BIO_RW)))
2762 num_stripes = map->sub_stripes;
2763 else if (mirror_num)
2764 stripe_index += mirror_num - 1;
2766 stripe_index = find_live_mirror(map, stripe_index,
2767 map->sub_stripes, stripe_index +
2768 current->pid % map->sub_stripes);
2772 * after this do_div call, stripe_nr is the number of stripes
2773 * on this device we have to walk to find the data, and
2774 * stripe_index is the number of our device in the stripe array
2776 stripe_index = do_div(stripe_nr, map->num_stripes);
2778 BUG_ON(stripe_index >= map->num_stripes);
2780 for (i = 0; i < num_stripes; i++) {
2782 struct btrfs_device *device;
2783 struct backing_dev_info *bdi;
2785 device = map->stripes[stripe_index].dev;
2787 bdi = blk_get_backing_dev_info(device->bdev);
2788 if (bdi->unplug_io_fn)
2789 bdi->unplug_io_fn(bdi, unplug_page);
2792 multi->stripes[i].physical =
2793 map->stripes[stripe_index].physical +
2794 stripe_offset + stripe_nr * map->stripe_len;
2795 multi->stripes[i].dev = map->stripes[stripe_index].dev;
2801 multi->num_stripes = num_stripes;
2802 multi->max_errors = max_errors;
2805 free_extent_map(em);
2809 int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2810 u64 logical, u64 *length,
2811 struct btrfs_multi_bio **multi_ret, int mirror_num)
2813 return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
2817 int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
2818 u64 chunk_start, u64 physical, u64 devid,
2819 u64 **logical, int *naddrs, int *stripe_len)
2821 struct extent_map_tree *em_tree = &map_tree->map_tree;
2822 struct extent_map *em;
2823 struct map_lookup *map;
2830 read_lock(&em_tree->lock);
2831 em = lookup_extent_mapping(em_tree, chunk_start, 1);
2832 read_unlock(&em_tree->lock);
2834 BUG_ON(!em || em->start != chunk_start);
2835 map = (struct map_lookup *)em->bdev;
2838 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2839 do_div(length, map->num_stripes / map->sub_stripes);
2840 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
2841 do_div(length, map->num_stripes);
2843 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
2846 for (i = 0; i < map->num_stripes; i++) {
2847 if (devid && map->stripes[i].dev->devid != devid)
2849 if (map->stripes[i].physical > physical ||
2850 map->stripes[i].physical + length <= physical)
2853 stripe_nr = physical - map->stripes[i].physical;
2854 do_div(stripe_nr, map->stripe_len);
2856 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2857 stripe_nr = stripe_nr * map->num_stripes + i;
2858 do_div(stripe_nr, map->sub_stripes);
2859 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
2860 stripe_nr = stripe_nr * map->num_stripes + i;
2862 bytenr = chunk_start + stripe_nr * map->stripe_len;
2863 WARN_ON(nr >= map->num_stripes);
2864 for (j = 0; j < nr; j++) {
2865 if (buf[j] == bytenr)
2869 WARN_ON(nr >= map->num_stripes);
2876 *stripe_len = map->stripe_len;
2878 free_extent_map(em);
2882 int btrfs_unplug_page(struct btrfs_mapping_tree *map_tree,
2883 u64 logical, struct page *page)
2885 u64 length = PAGE_CACHE_SIZE;
2886 return __btrfs_map_block(map_tree, READ, logical, &length,
2890 static void end_bio_multi_stripe(struct bio *bio, int err)
2892 struct btrfs_multi_bio *multi = bio->bi_private;
2893 int is_orig_bio = 0;
2896 atomic_inc(&multi->error);
2898 if (bio == multi->orig_bio)
2901 if (atomic_dec_and_test(&multi->stripes_pending)) {
2904 bio = multi->orig_bio;
2906 bio->bi_private = multi->private;
2907 bio->bi_end_io = multi->end_io;
2908 /* only send an error to the higher layers if it is
2909 * beyond the tolerance of the multi-bio
2911 if (atomic_read(&multi->error) > multi->max_errors) {
2915 * this bio is actually up to date, we didn't
2916 * go over the max number of errors
2918 set_bit(BIO_UPTODATE, &bio->bi_flags);
2923 bio_endio(bio, err);
2924 } else if (!is_orig_bio) {
2929 struct async_sched {
2932 struct btrfs_fs_info *info;
2933 struct btrfs_work work;
2937 * see run_scheduled_bios for a description of why bios are collected for
2940 * This will add one bio to the pending list for a device and make sure
2941 * the work struct is scheduled.
2943 static noinline int schedule_bio(struct btrfs_root *root,
2944 struct btrfs_device *device,
2945 int rw, struct bio *bio)
2947 int should_queue = 1;
2948 struct btrfs_pending_bios *pending_bios;
2950 /* don't bother with additional async steps for reads, right now */
2951 if (!(rw & (1 << BIO_RW))) {
2953 submit_bio(rw, bio);
2959 * nr_async_bios allows us to reliably return congestion to the
2960 * higher layers. Otherwise, the async bio makes it appear we have
2961 * made progress against dirty pages when we've really just put it
2962 * on a queue for later
2964 atomic_inc(&root->fs_info->nr_async_bios);
2965 WARN_ON(bio->bi_next);
2966 bio->bi_next = NULL;
2969 spin_lock(&device->io_lock);
2970 if (bio_rw_flagged(bio, BIO_RW_SYNCIO))
2971 pending_bios = &device->pending_sync_bios;
2973 pending_bios = &device->pending_bios;
2975 if (pending_bios->tail)
2976 pending_bios->tail->bi_next = bio;
2978 pending_bios->tail = bio;
2979 if (!pending_bios->head)
2980 pending_bios->head = bio;
2981 if (device->running_pending)
2984 spin_unlock(&device->io_lock);
2987 btrfs_queue_worker(&root->fs_info->submit_workers,
2992 int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
2993 int mirror_num, int async_submit)
2995 struct btrfs_mapping_tree *map_tree;
2996 struct btrfs_device *dev;
2997 struct bio *first_bio = bio;
2998 u64 logical = (u64)bio->bi_sector << 9;
3001 struct btrfs_multi_bio *multi = NULL;
3006 length = bio->bi_size;
3007 map_tree = &root->fs_info->mapping_tree;
3008 map_length = length;
3010 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
3014 total_devs = multi->num_stripes;
3015 if (map_length < length) {
3016 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
3017 "len %llu\n", (unsigned long long)logical,
3018 (unsigned long long)length,
3019 (unsigned long long)map_length);
3022 multi->end_io = first_bio->bi_end_io;
3023 multi->private = first_bio->bi_private;
3024 multi->orig_bio = first_bio;
3025 atomic_set(&multi->stripes_pending, multi->num_stripes);
3027 while (dev_nr < total_devs) {
3028 if (total_devs > 1) {
3029 if (dev_nr < total_devs - 1) {
3030 bio = bio_clone(first_bio, GFP_NOFS);
3035 bio->bi_private = multi;
3036 bio->bi_end_io = end_bio_multi_stripe;
3038 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
3039 dev = multi->stripes[dev_nr].dev;
3040 BUG_ON(rw == WRITE && !dev->writeable);
3041 if (dev && dev->bdev) {
3042 bio->bi_bdev = dev->bdev;
3044 schedule_bio(root, dev, rw, bio);
3046 submit_bio(rw, bio);
3048 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
3049 bio->bi_sector = logical >> 9;
3050 bio_endio(bio, -EIO);
3054 if (total_devs == 1)
3059 struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
3062 struct btrfs_device *device;
3063 struct btrfs_fs_devices *cur_devices;
3065 cur_devices = root->fs_info->fs_devices;
3066 while (cur_devices) {
3068 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3069 device = __find_device(&cur_devices->devices,
3074 cur_devices = cur_devices->seed;
3079 static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
3080 u64 devid, u8 *dev_uuid)
3082 struct btrfs_device *device;
3083 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
3085 device = kzalloc(sizeof(*device), GFP_NOFS);
3088 list_add(&device->dev_list,
3089 &fs_devices->devices);
3090 device->barriers = 1;
3091 device->dev_root = root->fs_info->dev_root;
3092 device->devid = devid;
3093 device->work.func = pending_bios_fn;
3094 device->fs_devices = fs_devices;
3095 fs_devices->num_devices++;
3096 spin_lock_init(&device->io_lock);
3097 INIT_LIST_HEAD(&device->dev_alloc_list);
3098 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
3102 static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
3103 struct extent_buffer *leaf,
3104 struct btrfs_chunk *chunk)
3106 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3107 struct map_lookup *map;
3108 struct extent_map *em;
3112 u8 uuid[BTRFS_UUID_SIZE];
3117 logical = key->offset;
3118 length = btrfs_chunk_length(leaf, chunk);
3120 read_lock(&map_tree->map_tree.lock);
3121 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
3122 read_unlock(&map_tree->map_tree.lock);
3124 /* already mapped? */
3125 if (em && em->start <= logical && em->start + em->len > logical) {
3126 free_extent_map(em);
3129 free_extent_map(em);
3132 em = alloc_extent_map(GFP_NOFS);
3135 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3136 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
3138 free_extent_map(em);
3142 em->bdev = (struct block_device *)map;
3143 em->start = logical;
3145 em->block_start = 0;
3146 em->block_len = em->len;
3148 map->num_stripes = num_stripes;
3149 map->io_width = btrfs_chunk_io_width(leaf, chunk);
3150 map->io_align = btrfs_chunk_io_align(leaf, chunk);
3151 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
3152 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
3153 map->type = btrfs_chunk_type(leaf, chunk);
3154 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
3155 for (i = 0; i < num_stripes; i++) {
3156 map->stripes[i].physical =
3157 btrfs_stripe_offset_nr(leaf, chunk, i);
3158 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
3159 read_extent_buffer(leaf, uuid, (unsigned long)
3160 btrfs_stripe_dev_uuid_nr(chunk, i),
3162 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
3164 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
3166 free_extent_map(em);
3169 if (!map->stripes[i].dev) {
3170 map->stripes[i].dev =
3171 add_missing_dev(root, devid, uuid);
3172 if (!map->stripes[i].dev) {
3174 free_extent_map(em);
3178 map->stripes[i].dev->in_fs_metadata = 1;
3181 write_lock(&map_tree->map_tree.lock);
3182 ret = add_extent_mapping(&map_tree->map_tree, em);
3183 write_unlock(&map_tree->map_tree.lock);
3185 free_extent_map(em);
3190 static int fill_device_from_item(struct extent_buffer *leaf,
3191 struct btrfs_dev_item *dev_item,
3192 struct btrfs_device *device)
3196 device->devid = btrfs_device_id(leaf, dev_item);
3197 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
3198 device->total_bytes = device->disk_total_bytes;
3199 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
3200 device->type = btrfs_device_type(leaf, dev_item);
3201 device->io_align = btrfs_device_io_align(leaf, dev_item);
3202 device->io_width = btrfs_device_io_width(leaf, dev_item);
3203 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
3205 ptr = (unsigned long)btrfs_device_uuid(dev_item);
3206 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
3211 static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
3213 struct btrfs_fs_devices *fs_devices;
3216 mutex_lock(&uuid_mutex);
3218 fs_devices = root->fs_info->fs_devices->seed;
3219 while (fs_devices) {
3220 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3224 fs_devices = fs_devices->seed;
3227 fs_devices = find_fsid(fsid);
3233 fs_devices = clone_fs_devices(fs_devices);
3234 if (IS_ERR(fs_devices)) {
3235 ret = PTR_ERR(fs_devices);
3239 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
3240 root->fs_info->bdev_holder);
3244 if (!fs_devices->seeding) {
3245 __btrfs_close_devices(fs_devices);
3246 free_fs_devices(fs_devices);
3251 fs_devices->seed = root->fs_info->fs_devices->seed;
3252 root->fs_info->fs_devices->seed = fs_devices;
3254 mutex_unlock(&uuid_mutex);
3258 static int read_one_dev(struct btrfs_root *root,
3259 struct extent_buffer *leaf,
3260 struct btrfs_dev_item *dev_item)
3262 struct btrfs_device *device;
3265 u8 fs_uuid[BTRFS_UUID_SIZE];
3266 u8 dev_uuid[BTRFS_UUID_SIZE];
3268 devid = btrfs_device_id(leaf, dev_item);
3269 read_extent_buffer(leaf, dev_uuid,
3270 (unsigned long)btrfs_device_uuid(dev_item),
3272 read_extent_buffer(leaf, fs_uuid,
3273 (unsigned long)btrfs_device_fsid(dev_item),
3276 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
3277 ret = open_seed_devices(root, fs_uuid);
3278 if (ret && !btrfs_test_opt(root, DEGRADED))
3282 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
3283 if (!device || !device->bdev) {
3284 if (!btrfs_test_opt(root, DEGRADED))
3288 printk(KERN_WARNING "warning devid %llu missing\n",
3289 (unsigned long long)devid);
3290 device = add_missing_dev(root, devid, dev_uuid);
3296 if (device->fs_devices != root->fs_info->fs_devices) {
3297 BUG_ON(device->writeable);
3298 if (device->generation !=
3299 btrfs_device_generation(leaf, dev_item))
3303 fill_device_from_item(leaf, dev_item, device);
3304 device->dev_root = root->fs_info->dev_root;
3305 device->in_fs_metadata = 1;
3306 if (device->writeable)
3307 device->fs_devices->total_rw_bytes += device->total_bytes;
3312 int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
3314 struct btrfs_dev_item *dev_item;
3316 dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
3318 return read_one_dev(root, buf, dev_item);
3321 int btrfs_read_sys_array(struct btrfs_root *root)
3323 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
3324 struct extent_buffer *sb;
3325 struct btrfs_disk_key *disk_key;
3326 struct btrfs_chunk *chunk;
3328 unsigned long sb_ptr;
3334 struct btrfs_key key;
3336 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
3337 BTRFS_SUPER_INFO_SIZE);
3340 btrfs_set_buffer_uptodate(sb);
3341 btrfs_set_buffer_lockdep_class(sb, 0);
3343 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
3344 array_size = btrfs_super_sys_array_size(super_copy);
3346 ptr = super_copy->sys_chunk_array;
3347 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
3350 while (cur < array_size) {
3351 disk_key = (struct btrfs_disk_key *)ptr;
3352 btrfs_disk_key_to_cpu(&key, disk_key);
3354 len = sizeof(*disk_key); ptr += len;
3358 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
3359 chunk = (struct btrfs_chunk *)sb_ptr;
3360 ret = read_one_chunk(root, &key, sb, chunk);
3363 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
3364 len = btrfs_chunk_item_size(num_stripes);
3373 free_extent_buffer(sb);
3377 int btrfs_read_chunk_tree(struct btrfs_root *root)
3379 struct btrfs_path *path;
3380 struct extent_buffer *leaf;
3381 struct btrfs_key key;
3382 struct btrfs_key found_key;
3386 root = root->fs_info->chunk_root;
3388 path = btrfs_alloc_path();
3392 /* first we search for all of the device items, and then we
3393 * read in all of the chunk items. This way we can create chunk
3394 * mappings that reference all of the devices that are afound
3396 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
3400 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3404 leaf = path->nodes[0];
3405 slot = path->slots[0];
3406 if (slot >= btrfs_header_nritems(leaf)) {
3407 ret = btrfs_next_leaf(root, path);
3414 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3415 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3416 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
3418 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
3419 struct btrfs_dev_item *dev_item;
3420 dev_item = btrfs_item_ptr(leaf, slot,
3421 struct btrfs_dev_item);
3422 ret = read_one_dev(root, leaf, dev_item);
3426 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
3427 struct btrfs_chunk *chunk;
3428 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3429 ret = read_one_chunk(root, &found_key, leaf, chunk);
3435 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3437 btrfs_release_path(root, path);
3442 btrfs_free_path(path);